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