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