Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpMbScanLine.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Compute the visibility of 3D polygons already transformed in the camera
33  *frame
34  *
35  * Authors:
36  * Aurelien Yol
37  *
38 *****************************************************************************/
39 
40 #ifndef vpMbScanLine_HH
41 #define vpMbScanLine_HH
42 
43 #include <deque>
44 #include <limits> // numeric_limits
45 #include <list>
46 #include <map>
47 #include <set>
48 #include <vector>
49 
50 #include <visp3/core/vpConfig.h>
51 #include <visp3/core/vpCameraParameters.h>
52 #include <visp3/core/vpColVector.h>
53 #include <visp3/core/vpImage.h>
54 #include <visp3/core/vpImageConvert.h>
55 #include <visp3/core/vpImagePoint.h>
56 #include <visp3/core/vpPoint.h>
57 
58 //#define DEBUG_DISP // Uncomment to get visibility debug display
59 
60 #if defined(DEBUG_DISP)
61 #include <visp3/core/vpDisplay.h>
62 #endif
63 
64 #ifndef DOXYGEN_SHOULD_SKIP_THIS
66 
73 class VISP_EXPORT vpMbScanLine
74 {
75 public:
78  typedef enum { START = 1, END = 0, POINT = 2 } vpMbScanLineType;
79 
82  typedef std::pair<vpColVector, vpColVector> vpMbScanLineEdge;
83 
85  struct vpMbScanLineSegment
86  {
87  vpMbScanLineSegment() : type(START), edge(), p(0), P1(0), P2(0), Z1(0), Z2(0), ID(0), b_sample_Y(false) { }
88  vpMbScanLineType type;
89  vpMbScanLineEdge edge;
90  double p; // This value can be either x or y-coordinate value depending if
91  // the structure is used in X or Y-axis scanlines computation.
92  double P1, P2; // Same comment as previous value.
93  double Z1, Z2;
94  int ID;
95  bool b_sample_Y;
96  };
97 
99  struct vpMbScanLineEdgeComparator
100  {
101  inline bool operator()(const vpMbScanLineEdge &l0, const vpMbScanLineEdge &l1) const
102  {
103  for (unsigned int i = 0; i < 3; ++i)
104  if (l0.first[i] < l1.first[i])
105  return true;
106  else if (l0.first[i] > l1.first[i])
107  return false;
108  for (unsigned int i = 0; i < 3; ++i)
109  if (l0.second[i] < l1.second[i])
110  return true;
111  else if (l0.second[i] > l1.second[i])
112  return false;
113  return false;
114  }
115  };
116 
118  struct vpMbScanLineSegmentComparator
119  {
120  inline bool operator()(const vpMbScanLineSegment &a, const vpMbScanLineSegment &b) const
121  {
122  // return a.p == b.p ? a.type < b.type : a.p < b.p;
123  return (std::fabs(a.p - b.p) <= std::numeric_limits<double>::epsilon()) ? a.type < b.type : a.p < b.p;
124  }
125 
126  inline bool operator()(const std::pair<double, vpMbScanLineSegment> &a,
127  const std::pair<double, vpMbScanLineSegment> &b) const
128  {
129  return a.first < b.first;
130  }
131  };
132 
133 private:
134  unsigned int w, h;
136  unsigned int maskBorder;
138  vpImage<int> primitive_ids;
139  std::map<vpMbScanLineEdge, std::set<int>, vpMbScanLineEdgeComparator> visibility_samples;
140  double depthTreshold;
141 
142 public:
143 #if defined(DEBUG_DISP)
144  vpDisplay *dispMaskDebug;
145  vpDisplay *dispLineDebug;
146  vpImage<unsigned char> linedebugImg;
147 #endif
148 
149  vpMbScanLine();
150  virtual ~vpMbScanLine();
151 
152  void drawScene(const std::vector<std::vector<std::pair<vpPoint, unsigned int> > *> &polygons,
153  std::vector<int> listPolyIndices, const vpCameraParameters &K, unsigned int w, unsigned int h);
154 
162  double getDepthTreshold() { return depthTreshold; }
163  unsigned int getMaskBorder() { return maskBorder; }
164  const vpImage<unsigned char> &getMask() const { return mask; }
165  const vpImage<int> &getPrimitiveIDs() const { return primitive_ids; }
166 
167  void queryLineVisibility(const vpPoint &a, const vpPoint &b, std::vector<std::pair<vpPoint, vpPoint> > &lines,
168  const bool &displayResults = false);
169 
177  void setDepthTreshold(const double &treshold) { depthTreshold = treshold; }
178  void setMaskBorder(const unsigned int &mb) { maskBorder = mb; }
179 
180 private:
181  void createScanLinesFromLocals(std::vector<std::vector<vpMbScanLineSegment> > &scanlines,
182  std::vector<std::vector<vpMbScanLineSegment> > &localScanlines,
183  const unsigned int &size);
184 
185  void drawLineY(const vpColVector &a, const vpColVector &b, const vpMbScanLineEdge &line_ID, const int ID,
186  std::vector<std::vector<vpMbScanLineSegment> > &scanlines);
187 
188  void drawLineX(const vpColVector &a, const vpColVector &b, const vpMbScanLineEdge &line_ID, const int ID,
189  std::vector<std::vector<vpMbScanLineSegment> > &scanlines);
190 
191  void drawPolygonY(const std::vector<std::pair<vpPoint, unsigned int> > &polygon, const int ID,
192  std::vector<std::vector<vpMbScanLineSegment> > &scanlines);
193 
194  void drawPolygonX(const std::vector<std::pair<vpPoint, unsigned int> > &polygon, const int ID,
195  std::vector<std::vector<vpMbScanLineSegment> > &scanlines);
196 
197  // Static functions
198  static vpMbScanLineEdge makeMbScanLineEdge(const vpPoint &a, const vpPoint &b);
199  static void createVectorFromPoint(const vpPoint &p, vpColVector &v, const vpCameraParameters &K);
200  static double getAlpha(double x, double X0, double Z0, double X1, double Z1);
201  static double mix(double a, double b, double alpha);
202  static vpPoint mix(const vpPoint &a, const vpPoint &b, double alpha);
203  static double norm(const vpPoint &a, const vpPoint &b);
204 };
205 END_VISP_NAMESPACE
206 #endif // doxygen should skip this
207 
208 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79