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