Visual Servoing Platform  version 3.6.1 under development (2024-10-13)
vpPolygon3D.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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  * Implements a polygon of the model used by the model-based tracker.
32  */
33 
39 #ifndef vpPolygon3D_HH
40 #define vpPolygon3D_HH
41 
42 #include <vector>
43 
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/core/vpMeterPixelConversion.h>
46 #include <visp3/core/vpPixelMeterConversion.h>
47 #include <visp3/core/vpPoint.h>
48 
49 BEGIN_VISP_NAMESPACE
56 class VISP_EXPORT vpPolygon3D
57 {
58 public:
59  typedef enum
60  {
61  NO_CLIPPING = 0,
62  NEAR_CLIPPING = 1,
63  FAR_CLIPPING = 2,
64  LEFT_CLIPPING = 4,
65  RIGHT_CLIPPING = 8,
66  UP_CLIPPING = 16,
67  DOWN_CLIPPING = 32,
68  FOV_CLIPPING = 60,
69  ALL_CLIPPING = 63
70  } vpPolygon3DClippingType;
71 
72 public:
74  unsigned int nbpt;
77  unsigned int nbCornersInsidePrev;
81  std::vector<std::pair<vpPoint, unsigned int> > polyClipped;
83  unsigned int clippingFlag;
85  double distNearClip;
87  double distFarClip;
88 
89 private:
90  bool getClippedPointsFovGeneric(const vpPoint &p1, const vpPoint &p2, vpPoint &p1Clipped, vpPoint &p2Clipped,
91  unsigned int &p1ClippedInfo, unsigned int &p2ClippedInfo, const vpColVector &normal,
92  const unsigned int &flag);
93 
94  bool getClippedPointsDistance(const vpPoint &p1, const vpPoint &p2, vpPoint &p1Clipped, vpPoint &p2Clipped,
95  unsigned int &p1ClippedInfo, unsigned int &p2ClippedInfo, const unsigned int &flag,
96  const double &distance);
97 
98 public:
99  vpPolygon3D();
100  vpPolygon3D(const vpPolygon3D &mbtp);
101  virtual ~vpPolygon3D();
102 
103  void addPoint(unsigned int n, const vpPoint &P);
104 
105  void changeFrame(const vpHomogeneousMatrix &cMo);
106 
107  void computePolygonClipped(const vpCameraParameters &cam = vpCameraParameters());
108 
116  inline unsigned int getClipping() const { return clippingFlag; }
117 
123  inline double getFarClippingDistance() const { return distFarClip; }
124 
130  inline unsigned int getNbPoint() const { return nbpt; }
131 
137  inline unsigned int getNbCornerInsidePrevImage() const { return nbCornersInsidePrev; }
138 
139  unsigned int getNbCornerInsideImage(const vpImage<unsigned char> &I, const vpCameraParameters &cam);
140 
146  inline double getNearClippingDistance() const { return distNearClip; }
147 
148  vpPoint &getPoint(const unsigned int _index);
149 
150  std::vector<vpImagePoint> getRoi(const vpCameraParameters &cam);
151 
152  std::vector<vpImagePoint> getRoi(const vpCameraParameters &cam, const vpHomogeneousMatrix &cMo);
153 
154  void getRoiClipped(const vpCameraParameters &cam, std::vector<vpImagePoint> &roi);
155 
156  void getRoiClipped(const vpCameraParameters &cam, std::vector<vpImagePoint> &roi, const vpHomogeneousMatrix &cMo);
157 
158  void getRoiClipped(const vpCameraParameters &cam, std::vector<std::pair<vpImagePoint, unsigned int> > &roi);
159 
160  void getRoiClipped(const vpCameraParameters &cam, std::vector<std::pair<vpImagePoint, unsigned int> > &roi,
161  const vpHomogeneousMatrix &cMo);
162 
163 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
168  VP_DEPRECATED void getRoiClipped(std::vector<vpPoint> &points);
170 #endif
171 
172  void getPolygonClipped(std::vector<std::pair<vpPoint, unsigned int> > &poly);
173 
174  void getPolygonClipped(std::vector<vpPoint> &poly);
175 
176  vpPolygon3D &operator=(const vpPolygon3D &mbtp);
177 
185  inline void setClipping(const unsigned int &flags) { clippingFlag = flags; }
186 
192  inline void setFarClippingDistance(const double &dist)
193  {
194  distFarClip = dist;
195  clippingFlag = (clippingFlag | vpPolygon3D::FAR_CLIPPING);
196  }
197 
198  virtual void setNbPoint(unsigned int nb);
199 
205  inline void setNearClippingDistance(const double &dist)
206  {
207  distNearClip = dist;
208  clippingFlag = (clippingFlag | vpPolygon3D::NEAR_CLIPPING);
209  }
210 
211 public:
212  static void getClippedPolygon(const std::vector<vpPoint> &ptIn, std::vector<vpPoint> &ptOut,
213  const vpHomogeneousMatrix &cMo, const unsigned int &clippingFlags,
214  const vpCameraParameters &cam = vpCameraParameters(), const double &znear = 0.001,
215  const double &zfar = 100);
216  static void getMinMaxRoi(const std::vector<vpImagePoint> &roi, int &i_min, int &i_max, int &j_min, int &j_max);
217  static bool roiInsideImage(const vpImage<unsigned char> &I, const std::vector<vpImagePoint> &corners);
218 };
219 END_VISP_NAMESPACE
220 
221 #ifdef VISP_HAVE_NLOHMANN_JSON
222 #include <nlohmann/json.hpp>
223 #include <visp3/core/vpJsonParsing.h>
224 NLOHMANN_JSON_SERIALIZE_ENUM(VISP_NAMESPACE_ADDRESSING vpPolygon3D::vpPolygon3DClippingType, {
225  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::NO_CLIPPING, "none"},
226  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::NEAR_CLIPPING, "near"},
227  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::FAR_CLIPPING, "far"},
228  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::LEFT_CLIPPING, "left"},
229  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::RIGHT_CLIPPING, "right"},
230  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::UP_CLIPPING, "up"},
231  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::DOWN_CLIPPING, "down"},
232  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::FOV_CLIPPING, "fov"},
233  {VISP_NAMESPACE_ADDRESSING vpPolygon3D::ALL_CLIPPING, "all"}
234 });
235 
236 inline nlohmann::json clippingFlagsToJSON(const int flags)
237 {
238 #ifdef ENABLE_VISP_NAMESPACE
239  using namespace VISP_NAMESPACE_NAME;
240 #endif
241  constexpr std::array<vpPolygon3D::vpPolygon3DClippingType, 3> specificFlags = {
245  };
246  for (const auto f: specificFlags) {
247  if (flags == f) {
248  return nlohmann::json::array({ f });
249  }
250  }
251  return flagsToJSON<vpPolygon3D::vpPolygon3DClippingType>(flags, {
258  });
259 }
260 
261 #endif
262 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Implements a 3D polygon with render functionalities like clipping.
Definition: vpPolygon3D.h:57
void setFarClippingDistance(const double &dist)
Definition: vpPolygon3D.h:192
unsigned int nbpt
Number of points used to define the polygon.
Definition: vpPolygon3D.h:74
unsigned int getNbPoint() const
Definition: vpPolygon3D.h:130
void setNearClippingDistance(const double &dist)
Definition: vpPolygon3D.h:205
vpPolygon3DClippingType
Definition: vpPolygon3D.h:60
double distNearClip
Distance for near clipping.
Definition: vpPolygon3D.h:85
vpPoint * p
corners in the object frame
Definition: vpPolygon3D.h:79
double getNearClippingDistance() const
Definition: vpPolygon3D.h:146
unsigned int getClipping() const
Definition: vpPolygon3D.h:116
unsigned int getNbCornerInsidePrevImage() const
Definition: vpPolygon3D.h:137
double getFarClippingDistance() const
Definition: vpPolygon3D.h:123
unsigned int clippingFlag
Clipping flag.
Definition: vpPolygon3D.h:83
void setClipping(const unsigned int &flags)
Definition: vpPolygon3D.h:185
std::vector< std::pair< vpPoint, unsigned int > > polyClipped
Region of interest clipped.
Definition: vpPolygon3D.h:81
double distFarClip
Distance for near clipping.
Definition: vpPolygon3D.h:87
unsigned int nbCornersInsidePrev
Definition: vpPolygon3D.h:77