Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpMbScanLine.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
34#ifndef VP_MB_SCANLINE_H
35#define VP_MB_SCANLINE_H
36
37#include <deque>
38#include <limits> // numeric_limits
39#include <list>
40#include <map>
41#include <set>
42#include <vector>
43
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpCameraParameters.h>
46#include <visp3/core/vpColVector.h>
47#include <visp3/core/vpImage.h>
48#include <visp3/core/vpImageConvert.h>
49#include <visp3/core/vpImagePoint.h>
50#include <visp3/core/vpPoint.h>
51
52//#define DEBUG_DISP // Uncomment to get visibility debug display
53
54#if defined(DEBUG_DISP)
55#include <visp3/core/vpDisplay.h>
56#endif
57
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
60
78class VISP_EXPORT vpMbScanLine
79{
80public:
83 typedef enum { START = 1, END = 0, POINT = 2 } vpMbScanLineType;
84
87 typedef std::pair<vpColVector, vpColVector> vpMbScanLineEdge;
88
90 struct vpMbScanLineSegment
91 {
92 vpMbScanLineSegment() : type(START), edge(), p(0), P1(0), P2(0), Z1(0), Z2(0), ID(0), b_sample_Y(false) { }
93 vpMbScanLineType type;
94 vpMbScanLineEdge edge;
95 double p; // This value can be either x or y-coordinate value depending if
96 // the structure is used in X or Y-axis scanlines computation.
97 double P1, P2; // Same comment as previous value.
98 double Z1, Z2;
99 int ID;
100 bool b_sample_Y;
101 };
102
104 struct vpMbScanLineEdgeComparator
105 {
106 inline bool operator()(const vpMbScanLineEdge &l0, const vpMbScanLineEdge &l1) const
107 {
108 for (unsigned int i = 0; i < 3; ++i)
109 if (l0.first[i] < l1.first[i])
110 return true;
111 else if (l0.first[i] > l1.first[i])
112 return false;
113 for (unsigned int i = 0; i < 3; ++i)
114 if (l0.second[i] < l1.second[i])
115 return true;
116 else if (l0.second[i] > l1.second[i])
117 return false;
118 return false;
119 }
120 };
121
123 struct vpMbScanLineSegmentComparator
124 {
125 inline bool operator()(const vpMbScanLineSegment &a, const vpMbScanLineSegment &b) const
126 {
127 // return a.p == b.p ? a.type < b.type : a.p < b.p;
128 return (std::fabs(a.p - b.p) <= std::numeric_limits<double>::epsilon()) ? a.type < b.type : a.p < b.p;
129 }
130
131 inline bool operator()(const std::pair<double, vpMbScanLineSegment> &a,
132 const std::pair<double, vpMbScanLineSegment> &b) const
133 {
134 return a.first < b.first;
135 }
136 };
137
138private:
139 unsigned int w, h;
140 vpCameraParameters K;
141 unsigned int maskBorder;
142 vpImage<unsigned char> mask;
143 vpImage<int> primitive_ids;
144 std::map<vpMbScanLineEdge, std::set<int>, vpMbScanLineEdgeComparator> visibility_samples;
145 double depthTreshold;
146
147public:
148#if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)) && defined(DEBUG_DISP)
149 vpDisplay *dispMaskDebug;
150 vpDisplay *dispLineDebug;
151 vpImage<unsigned char> linedebugImg;
152#endif
153
154 vpMbScanLine();
155 vpMbScanLine(const vpMbScanLine &scanline);
156 virtual ~vpMbScanLine();
157 vpMbScanLine &operator=(const vpMbScanLine &scanline);
158
159 void drawScene(const std::vector<std::vector<std::pair<vpPoint, unsigned int> > *> &polygons,
160 std::vector<int> listPolyIndices, const vpCameraParameters &K, unsigned int w, unsigned int h);
161
169 double getDepthTreshold() { return depthTreshold; }
170 unsigned int getMaskBorder() { return maskBorder; }
171 const vpImage<unsigned char> &getMask() const { return mask; }
172 const vpImage<int> &getPrimitiveIDs() const { return primitive_ids; }
173
174 void queryLineVisibility(const vpPoint &a, const vpPoint &b, std::vector<std::pair<vpPoint, vpPoint> > &lines,
175 const bool &displayResults = false);
176
184 void setDepthTreshold(const double &treshold) { depthTreshold = treshold; }
185 void setMaskBorder(const unsigned int &mb) { maskBorder = mb; }
186
187private:
188 void createScanLinesFromLocals(std::vector<std::vector<vpMbScanLineSegment> > &scanlines,
189 std::vector<std::vector<vpMbScanLineSegment> > &localScanlines,
190 const unsigned int &size);
191
192 void drawLineY(const vpColVector &a, const vpColVector &b, const vpMbScanLineEdge &line_ID, const int ID,
193 std::vector<std::vector<vpMbScanLineSegment> > &scanlines);
194
195 void drawLineX(const vpColVector &a, const vpColVector &b, const vpMbScanLineEdge &line_ID, const int ID,
196 std::vector<std::vector<vpMbScanLineSegment> > &scanlines);
197
198 void drawPolygonY(const std::vector<std::pair<vpPoint, unsigned int> > &polygon, const int ID,
199 std::vector<std::vector<vpMbScanLineSegment> > &scanlines);
200
201 void drawPolygonX(const std::vector<std::pair<vpPoint, unsigned int> > &polygon, const int ID,
202 std::vector<std::vector<vpMbScanLineSegment> > &scanlines);
203
204 // Static functions
205 static vpMbScanLineEdge makeMbScanLineEdge(const vpPoint &a, const vpPoint &b);
206 static void createVectorFromPoint(const vpPoint &p, vpColVector &v, const vpCameraParameters &K);
207 static double getAlpha(double x, double X0, double Z0, double X1, double Z1);
208 static double mix(double a, double b, double alpha);
209 static vpPoint mix(const vpPoint &a, const vpPoint &b, double alpha);
210 static double norm(const vpPoint &a, const vpPoint &b);
211};
212END_VISP_NAMESPACE
213#endif // doxygen should skip this
214
215#endif