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