Visual Servoing Platform  version 3.6.1 under development (2024-04-18)
vpImageSimulator.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: Class which enables to project an image in the 3D space
32  * and get the view of a virtual camera.
33  *
34  *
35  * Authors:
36  * Amaury Dame
37  *
38 *****************************************************************************/
39 
40 #ifndef vpImageSimulator_h
41 #define vpImageSimulator_h
42 
130 #include <visp3/core/vpCameraParameters.h>
131 #include <visp3/core/vpColVector.h>
132 #include <visp3/core/vpHomogeneousMatrix.h>
133 #include <visp3/core/vpImage.h>
134 #include <visp3/core/vpPoint.h>
135 #include <visp3/core/vpRect.h>
136 #include <visp3/core/vpTriangle.h>
137 
138 #include <list>
139 #include <vector>
140 
141 class VISP_EXPORT vpImageSimulator
142 {
143 public:
144  typedef enum { COLORED, GRAY_SCALED } vpColorPlan;
145 
146  typedef enum { SIMPLE, BILINEAR_INTERPOLATION } vpInterpolationType;
147 
148 private:
149  vpColVector X[4];
151  vpColVector X2[4];
152  std::vector<vpPoint> pt;
153  std::vector<vpPoint> ptClipped;
154 
155  vpInterpolationType interp;
156 
157  // normal repere objet
158  vpColVector normal_obj;
159  // normal repere cam
160  vpColVector normal_Cam;
161  // normal repere cam sous forme de pointeur sinon vpColVector prend trop de
162  // temps
163  double *normal_Cam_optim;
164 
165  // distance du plan au centre de camera
166  double distance;
167  double visible_result;
168  bool visible;
169 
170  // point 3D du plan definissant ou sera le pixel (0,0) de l'image
171  double *X0_2_optim;
172 
173  // variable pour intersection algebre : vecteurs de la base 2D et leur norme
174  double frobeniusNorm_u, fronbniusNorm_v;
175 
176  // 2 vecteur 3D (typiquement deux cotes du plan) qui definissent le repere
177  // 2D u,v de l'image
178  vpColVector vbase_u, vbase_v;
179  // version pointeur des vecteurs de base
180  double *vbase_u_optim;
181  double *vbase_v_optim;
182 
183  // allocation memoire du point 3D d'intersection entre le vecteur
184  // (centre_cam - point_plan_image) et le plan
185  double *Xinter_optim;
186 
187  // triangles de projection du plan
188  std::vector<vpTriangle> listTriangle;
189 
190  // image de texture
191  vpColorPlan colorI;
193  vpImage<vpRGBa> Ic;
194 
195  vpRect rect;
196  bool cleanPrevImage;
197  bool setBackgroundTexture; // flag set when the background is to a texture
198  // using setBackGroundTexture()
199  vpColor bgColor;
200 
201  vpColVector focal;
202 
203  // boolean to tell if the points in the camera frame have to be clipped
204  bool needClipping;
205 
206 public:
207  explicit vpImageSimulator(const vpColorPlan &col = COLORED);
208  vpImageSimulator(const vpImageSimulator &text);
209  virtual ~vpImageSimulator();
210 
211  vpImageSimulator &operator=(const vpImageSimulator &sim);
212 
213  // creation du plan a partir de ses coordonnees 3D ds repere objet et de son
214  // image texture
215  void init(const vpImage<unsigned char> &I, vpColVector *X);
216  void init(const vpImage<vpRGBa> &I, vpColVector *X);
217 #ifdef VISP_HAVE_MODULE_IO
218  void init(const char *file_image, vpColVector *X);
219 #endif
220  void init(const vpImage<unsigned char> &I, const std::vector<vpPoint> &X);
221  void init(const vpImage<vpRGBa> &I, const std::vector<vpPoint> &X);
222 #ifdef VISP_HAVE_MODULE_IO
223  void init(const char *file_image, const std::vector<vpPoint> &X);
224 #endif
225 
226  // projection du plan par cMo => creation des deux triangles definissant
227  // projection du plan sur plan image (coord en metre)
228  void setCameraPosition(const vpHomogeneousMatrix &cMt);
229 
230  void setInterpolationType(const vpInterpolationType interplt) { this->interp = interplt; }
231 
232  void getImage(vpImage<unsigned char> &I, const vpCameraParameters &cam);
233  void getImage(vpImage<vpRGBa> &I, const vpCameraParameters &cam);
234 
235  void getImage(vpImage<unsigned char> &I, vpImage<unsigned char> &Isrc, const vpCameraParameters &cam);
236  void getImage(vpImage<vpRGBa> &I, vpImage<vpRGBa> &Isrc, const vpCameraParameters &cam);
237 
238  void getImage(vpImage<unsigned char> &I, const vpCameraParameters &cam, vpMatrix &zBuffer);
239  void getImage(vpImage<vpRGBa> &I, const vpCameraParameters &cam, vpMatrix &zBuffer);
240 
241  static void getImage(vpImage<unsigned char> &I, std::list<vpImageSimulator> &list, const vpCameraParameters &cam);
242  static void getImage(vpImage<vpRGBa> &I, std::list<vpImageSimulator> &list, const vpCameraParameters &cam);
243 
244  std::vector<vpColVector> get3DcornersTextureRectangle();
245 
246  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpImageSimulator & /*ip*/);
247 
259  void setCleanPreviousImage(const bool &clean, const vpColor &color = vpColor::white)
260  {
261  cleanPrevImage = clean;
262  bgColor = color;
263  }
264 
272  {
273  setBackgroundTexture = true;
274  Ig = Iback;
275  }
276 
277 private:
278  void initPlan(vpColVector *X);
279 
280  // result = plan est visible.
281  // ie: un plan est oriente dans si normal_plan.focal < 0 => plan est visible
282  // sinon invisible.
283  bool isVisible() { return visible; }
284 
285  // function that project a point x,y on the plane, return true if the
286  // projection is on the limited plane
287  // and in this case return the corresponding image pixel Ipixelplan
288  bool getPixel(const vpImagePoint &iP, unsigned char &Ipixelplan);
289  bool getPixel(const vpImagePoint &iP, vpRGBa &Ipixelplan);
290  bool getPixel(vpImage<unsigned char> &Isrc, const vpImagePoint &iP, unsigned char &Ipixelplan);
291  bool getPixel(vpImage<vpRGBa> &Isrc, const vpImagePoint &iP, vpRGBa &Ipixelplan);
292  bool getPixelDepth(const vpImagePoint &iP, double &Zpixelplan);
293  bool getPixelVisibility(const vpImagePoint &iP, double &Zpixelplan);
294 
295  // operation 3D de base :
296  void project(const vpColVector &_vin, const vpHomogeneousMatrix &_cMt, vpColVector &_vout);
297  // donne coordonnes homogenes de _v;
298  void getHomogCoord(const vpColVector &_v, vpColVector &_vH);
299  // donne coordonnes _v en fction coord homogenes _vH;
300  void getCoordFromHomog(const vpColVector &_vH, vpColVector &_v);
301 
302  void getRoi(const unsigned int &Iwidth, const unsigned int &Iheight, const vpCameraParameters &cam,
303  const std::vector<vpPoint> &point, vpRect &rect);
304 };
305 
306 #endif
307 
308 /*
309  * Local variables:
310  * c-basic-offset: 2
311  * End:
312  */
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor white
Definition: vpColor.h:206
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Class which enables to project an image in the 3D space and get the view of a virtual camera.
void setBackGroundTexture(const vpImage< unsigned char > &Iback)
void setCleanPreviousImage(const bool &clean, const vpColor &color=vpColor::white)
void setInterpolationType(const vpInterpolationType interplt)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Definition: vpRGBa.h:61
Defines a rectangle in the plane.
Definition: vpRect.h:76