ViSP  2.9.0
vpWireFrameSimulator.cpp
1 /****************************************************************************
2  *
3  * $Id: vpWireFrameSimulator.cpp 4649 2014-02-07 14:57:11Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Wire frame simulator
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
41 
47 #include <visp/vpWireFrameSimulator.h>
48 #include <fcntl.h>
49 #include <string.h>
50 #include <stdio.h>
51 #include <vector>
52 
53 #include <visp/vpSimulatorException.h>
54 #include <visp/vpPoint.h>
55 #include <visp/vpCameraParameters.h>
56 #include <visp/vpMeterPixelConversion.h>
57 #include <visp/vpPoint.h>
58 #include <visp/vpIoTools.h>
59 
60 //Inventor includes
61 #if defined(VISP_HAVE_COIN)
62 #include <Inventor/nodes/SoSeparator.h>
63 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
64 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
65 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
66 #include <Inventor/actions/SoWriteAction.h>
67 #include <Inventor/actions/SoSearchAction.h>
68 #include <Inventor/misc/SoChildList.h>
69 #include <Inventor/actions/SoGetMatrixAction.h>
70 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
71 #include <Inventor/actions/SoToVRML2Action.h>
72 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
73 #include <Inventor/VRMLnodes/SoVRMLShape.h>
74 #endif
75 
76 extern "C"{extern Point2i *point2i;}
77 extern "C"{extern Point2i *listpoint2i;}
78 
79 typedef enum
80 {
81  BND_MODEL,
82  WRL_MODEL,
83  UNKNOWN_MODEL
84 } Model_3D;
85 
86 Model_3D getExtension(const char* file);
87 void set_scene_wrl (const char* str, Bound_scene *sc, float factor);
88 
89 /*
90  Get the extension of the file and return it
91 */
92 Model_3D
93 getExtension(const char* file)
94 {
95  std::string sfilename(file);
96 
97  size_t bnd = sfilename.find("bnd");
98  size_t BND = sfilename.find("BND");
99  size_t wrl = sfilename.find("wrl");
100  size_t WRL = sfilename.find("WRL");
101 
102  size_t size = sfilename.size();
103 
104  if ((bnd>0 && bnd<size ) || (BND>0 && BND<size))
105  return BND_MODEL;
106  else if ((wrl>0 && wrl<size) || ( WRL>0 && WRL<size))
107  {
108 #if defined(VISP_HAVE_COIN)
109  return WRL_MODEL;
110 #else
111  std::cout << "Coin not installed, cannot read VRML files" << std::endl;
112  throw std::string("Coin not installed, cannot read VRML files");
113 #endif
114  }
115  return UNKNOWN_MODEL;
116 }
117 
118 /*
119  Enable to initialize the scene
120 */
121 void set_scene (const char* str, Bound_scene *sc, float factor)
122 {
123  FILE *fd;
124 
125  //if ((fd = fopen (str, 0)) == -1)
126  if ((fd = fopen (str, "r")) == NULL)
127  {
128  std::string error = "The file " + std::string(str) + " can not be opened";
129 
130  throw(vpException(vpSimulatorException::ioError, error.c_str())) ;
131  }
132  open_keyword (keyword_tbl);
133  open_lex ();
134  open_source (fd, str);
135  malloc_Bound_scene (sc, str,(Index)BOUND_NBR);
136  parser (sc);
137 
138  //if (factor != 1)
139  if (std::fabs(factor) > std::numeric_limits<double>::epsilon())
140  {
141  for (int i = 0; i < sc->bound.nbr; i++)
142  {
143  for (int j = 0; j < sc->bound.ptr[i].point.nbr; j++)
144  {
145  sc->bound.ptr[i].point.ptr[j].x = sc->bound.ptr[i].point.ptr[j].x*factor;
146  sc->bound.ptr[i].point.ptr[j].y = sc->bound.ptr[i].point.ptr[j].y*factor;
147  sc->bound.ptr[i].point.ptr[j].z = sc->bound.ptr[i].point.ptr[j].z*factor;
148  }
149  }
150  }
151 
152  close_source ();
153  close_lex ();
154  close_keyword ();
155  fclose(fd);
156 }
157 
158 #if defined(VISP_HAVE_COIN)
159 
160 #ifndef DOXYGEN_SHOULD_SKIP_THIS
161 typedef struct
162 {
163  int nbPt;
164  std::vector<vpPoint> pt;
165  int nbIndex;
166  std::vector<int> index;
167 } indexFaceSet;
168 #endif // DOXYGEN_SHOULD_SKIP_THIS
169 
170 void extractFaces(SoVRMLIndexedFaceSet*, indexFaceSet *ifs);
171 void ifsToBound (Bound*, std::list<indexFaceSet*> &);
172 void destroyIfs(std::list<indexFaceSet*> &);
173 
174 
175 void set_scene_wrl (const char* str, Bound_scene *sc, float factor)
176 {
177  //Load the sceneGraph
178  SoDB::init();
179  SoInput in;
180  SbBool ok = in.openFile(str);
181  SoSeparator *sceneGraph;
182  SoVRMLGroup *sceneGraphVRML2;
183 
184  if (!ok) {
185  vpERROR_TRACE("can't open file \"%s\" \n Please check the Marker_Less.ini file", str);
186  exit(1);
187  }
188 
189  if(!in.isFileVRML2())
190  {
191  sceneGraph = SoDB::readAll(&in);
192  if (sceneGraph == NULL) { /*return -1;*/ }
193  sceneGraph->ref();
194 
195  SoToVRML2Action tovrml2;
196  tovrml2.apply(sceneGraph);
197  sceneGraphVRML2 =tovrml2.getVRML2SceneGraph();
198  sceneGraphVRML2->ref();
199  sceneGraph->unref();
200  }
201  else
202  {
203  sceneGraphVRML2 = SoDB::readAllVRML(&in);
204  if (sceneGraphVRML2 == NULL) {
205  /*return -1;*/
206  throw(vpException(vpException::notInitialized, "Cannot read VRML file"));
207  }
208  sceneGraphVRML2->ref();
209  }
210 
211  in.closeFile();
212 
213  int nbShapes = sceneGraphVRML2->getNumChildren();
214 
215  SoNode * child;
216 
217  malloc_Bound_scene (sc, str,(Index)BOUND_NBR);
218 
219  int iterShapes = 0;
220  for (int i = 0; i < nbShapes; i++)
221  {
222  int nbFaces = 0;
223  child = sceneGraphVRML2->getChild(i);
224  if (child->getTypeId() == SoVRMLShape::getClassTypeId())
225  {
226  std::list<indexFaceSet*> ifs_list;
227  SoChildList * child2list = child->getChildren();
228  for (int j = 0; j < child2list->getLength(); j++)
229  {
230  if (((SoNode*)child2list->get(j))->getTypeId() == SoVRMLIndexedFaceSet::getClassTypeId())
231  {
232  indexFaceSet *ifs = new indexFaceSet;
233  SoVRMLIndexedFaceSet * face_set;
234  face_set = (SoVRMLIndexedFaceSet*)child2list->get(j);
235  extractFaces(face_set,ifs);
236  ifs_list.push_back(ifs);
237  nbFaces++;
238  }
239 // if (((SoNode*)child2list->get(j))->getTypeId() == SoVRMLIndexedLineSet::getClassTypeId())
240 // {
241 // std::cout << "> We found a line" << std::endl;
242 // SoVRMLIndexedLineSet * line_set;
243 // line_set = (SoVRMLIndexedLineSet*)child2list->get(j);
244 // extractLines(line_set);
245 // }
246  }
247  sc->bound.nbr++;
248  ifsToBound (&(sc->bound.ptr[iterShapes]), ifs_list);
249  destroyIfs(ifs_list);
250  iterShapes++;
251  }
252  }
253 
254  //if (factor != 1)
255  if (std::fabs(factor) > std::numeric_limits<double>::epsilon())
256  {
257  for (int i = 0; i < sc->bound.nbr; i++)
258  {
259  for (int j = 0; j < sc->bound.ptr[i].point.nbr; j++)
260  {
261  sc->bound.ptr[i].point.ptr[j].x = sc->bound.ptr[i].point.ptr[j].x*factor;
262  sc->bound.ptr[i].point.ptr[j].y = sc->bound.ptr[i].point.ptr[j].y*factor;
263  sc->bound.ptr[i].point.ptr[j].z = sc->bound.ptr[i].point.ptr[j].z*factor;
264  }
265  }
266  }
267 }
268 
269 
270 void
271 extractFaces(SoVRMLIndexedFaceSet* face_set, indexFaceSet *ifs)
272 {
273 // vpList<vpPoint> pointList;
274 // pointList.kill();
275  SoVRMLCoordinate *coord = (SoVRMLCoordinate *)(face_set->coord.getValue());
276  int coordSize = coord->point.getNum();
277 
278  ifs->nbPt = coordSize;
279  for (int i = 0; i < coordSize; i++)
280  {
281  SbVec3f point(0,0,0);
282  point[0]=coord->point[i].getValue()[0];
283  point[1]=coord->point[i].getValue()[1];
284  point[2]=coord->point[i].getValue()[2];
285  vpPoint pt;
286  pt.setWorldCoordinates(point[0],point[1],point[2]);
287  ifs->pt.push_back(pt);
288  }
289 
290  SoMFInt32 indexList = face_set->coordIndex;
291  int indexListSize = indexList.getNum();
292 
293  ifs->nbIndex = indexListSize;
294  for (int i = 0; i < indexListSize; i++)
295  {
296  int index = face_set->coordIndex[i];
297  ifs->index.push_back(index);
298  }
299 }
300 
301 void ifsToBound (Bound* bptr, std::list<indexFaceSet*> &ifs_list)
302 {
303  int nbPt = 0;
304  for(std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it){
305  nbPt += (*it)->nbPt;
306  }
307  bptr->point.nbr = (Index)nbPt;
308  bptr->point.ptr = (Point3f *) malloc ((unsigned int)nbPt * sizeof (Point3f));
309 
310  ifs_list.front();
311  unsigned int iter = 0;
312  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
313  {
314  indexFaceSet* ifs = *it;
315  for (unsigned int j = 0; j < (unsigned int)ifs->nbPt; j++)
316  {
317  bptr->point.ptr[iter].x = (float)ifs->pt[j].get_oX();
318  bptr->point.ptr[iter].y = (float)ifs->pt[j].get_oY();
319  bptr->point.ptr[iter].z = (float)ifs->pt[j].get_oZ();
320  iter++;
321  }
322  }
323 
324  unsigned int nbFace = 0;
325  ifs_list.front();
326  std::list<int> indSize;
327  int indice = 0;
328  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
329  {
330  indexFaceSet* ifs = *it;
331  for (unsigned int j = 0; j < (unsigned int)ifs->nbIndex; j++)
332  {
333  if(ifs->index[j] == -1)
334  {
335  nbFace++;
336  indSize.push_back(indice);
337  indice = 0;
338  }
339  else indice++;
340  }
341  }
342 
343  bptr->face.nbr = (Index)nbFace;
344  bptr->face.ptr = (Face *) malloc (nbFace * sizeof (Face));
345 
346 
347  std::list<int>::const_iterator iter_indSize = indSize.begin();
348  for (unsigned int i = 0; i < indSize.size(); i++)
349  {
350  bptr->face.ptr[i].vertex.nbr = (Index)*iter_indSize;
351  bptr->face.ptr[i].vertex.ptr = (Index *) malloc ((unsigned int)*iter_indSize * sizeof (Index));
352  ++iter_indSize;
353  }
354 
355  int offset = 0;
356  indice = 0;
357  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
358  {
359  indexFaceSet* ifs = *it;
360  iter = 0;
361  for (unsigned int j = 0; j < (unsigned int)ifs->nbIndex; j++)
362  {
363  if(ifs->index[j] != -1)
364  {
365  bptr->face.ptr[indice].vertex.ptr[iter] = (Index)(ifs->index[j] + offset);
366  iter++;
367  }
368  else
369  {
370  iter = 0;
371  indice++;
372  }
373  }
374  offset += ifs->nbPt;
375  }
376 }
377 
378 void destroyIfs(std::list<indexFaceSet*> &ifs_list)
379 {
380  for(std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it){
381  delete *it;
382  }
383  ifs_list.clear();
384 }
385 #else
386 void set_scene_wrl (const char* /*str*/, Bound_scene* /*sc*/, float /*factor*/)
387 {
388 }
389 #endif
390 
391 
392 /*
393  Convert the matrix format to deal with the one in the simulator
394 */
395 void vp2jlc_matrix (const vpHomogeneousMatrix vpM, Matrix &jlcM)
396 {
397  for (unsigned int i = 0; i < 4; i++) {
398  for (unsigned int j = 0; j < 4; j++)
399  jlcM[j][i] = (float)vpM[i][j];
400  }
401 }
402 
403 /*
404  Copy the scene corresponding to the registeresd parameters in the image.
405 */
406 void
407 vpWireFrameSimulator::display_scene(Matrix mat, Bound_scene &sc, const vpImage<vpRGBa> &I, const vpColor &color)
408 {
409  // extern Bound *clipping_Bound ();
410  Bound *bp, *bend;
411  Bound *clip; /* surface apres clipping */
412  Byte b = (Byte) *get_rfstack ();
413  Matrix m;
414 
415  //bcopy ((char *) mat, (char *) m, sizeof (Matrix));
416  memmove((char *) m, (char *) mat, sizeof (Matrix));
417  View_to_Matrix (get_vwstack (), *(get_tmstack ()));
418  postmult_matrix (m, *(get_tmstack ()));
419  bp = sc.bound.ptr;
420  bend = bp + sc.bound.nbr;
421  for (; bp < bend; bp++)
422  {
423  if ((clip = clipping_Bound (bp, m)) != NULL)
424  {
425  Face *fp = clip->face.ptr;
426  Face *fend = fp + clip->face.nbr;
427 
428  set_Bound_face_display (clip, b); //regarde si is_visible
429 
430  point_3D_2D (clip->point.ptr, clip->point.nbr,I.getWidth(),I.getHeight(),point2i);
431  for (; fp < fend; fp++)
432  {
433  if (fp->is_visible)
434  {
435  wireframe_Face (fp, point2i);
436  Point2i *pt = listpoint2i;
437  for (int i = 1; i < fp->vertex.nbr; i++)
438  {
439  vpDisplay::displayLine(I,vpImagePoint((pt)->y,(pt)->x),vpImagePoint((pt+1)->y,(pt+1)->x),color,thickness_);
440  pt++;
441  }
442  if (fp->vertex.nbr > 2)
443  {
444  vpDisplay::displayLine(I,vpImagePoint((listpoint2i)->y,(listpoint2i)->x),vpImagePoint((pt)->y,(pt)->x),color,thickness_);
445  }
446  }
447  }
448  }
449  }
450 }
451 
452 /*
453  Copy the scene corresponding to the registeresd parameters in the image.
454 */
455 void
456 vpWireFrameSimulator::display_scene(Matrix mat, Bound_scene &sc, const vpImage<unsigned char> &I, const vpColor &color)
457 {
458  //extern Bound *clipping_Bound ();
459 
460  Bound *bp, *bend;
461  Bound *clip; /* surface apres clipping */
462  Byte b = (Byte) *get_rfstack ();
463  Matrix m;
464 
465  //bcopy ((char *) mat, (char *) m, sizeof (Matrix));
466  memmove((char *) m, (char *) mat, sizeof (Matrix));
467  View_to_Matrix (get_vwstack (), *(get_tmstack ()));
468  postmult_matrix (m, *(get_tmstack ()));
469  bp = sc.bound.ptr;
470  bend = bp + sc.bound.nbr;
471  for (; bp < bend; bp++)
472  {
473  if ((clip = clipping_Bound (bp, m)) != NULL)
474  {
475  Face *fp = clip->face.ptr;
476  Face *fend = fp + clip->face.nbr;
477 
478  set_Bound_face_display (clip, b); //regarde si is_visible
479 
480  point_3D_2D (clip->point.ptr, clip->point.nbr,I.getWidth(),I.getHeight(),point2i);
481  for (; fp < fend; fp++)
482  {
483  if (fp->is_visible)
484  {
485  wireframe_Face (fp, point2i);
486  Point2i *pt = listpoint2i;
487  for (int i = 1; i < fp->vertex.nbr; i++)
488  {
489  vpDisplay::displayLine(I,vpImagePoint((pt)->y,(pt)->x),vpImagePoint((pt+1)->y,(pt+1)->x),color,thickness_);
490  pt++;
491  }
492  if (fp->vertex.nbr > 2)
493  {
494  vpDisplay::displayLine(I,vpImagePoint((listpoint2i)->y,(listpoint2i)->x),vpImagePoint((pt)->y,(pt)->x),color,thickness_);
495  }
496  }
497  }
498  }
499  }
500 }
501 
502 /*************************************************************************************************************/
503 
513  : scene(), desiredScene(), camera(), objectImage(), fMo(), fMc(), camMf(),
514  refMo(), cMo(), cdMo(), object(PLATE), desiredObject(D_STANDARD),
515  camColor(vpColor::green), camTrajColor(vpColor::green), curColor(vpColor::blue),
516  desColor(vpColor::red), sceneInitialized(false), displayCameraTrajectory(true),
517  cameraTrajectory(), poseList(), fMoList(), nbrPtLimit(1000), old_iPr(), old_iPz(),
518  old_iPt(), blockedr(false), blockedz(false), blockedt(false), blocked(false),
519  camMf2(), f2Mf(), px_int(1), py_int(1), px_ext(1), py_ext(1), displayObject(false),
520  displayDesiredObject(false), displayCamera(false), displayImageSimulator(false),
521  cameraFactor(1.), camTrajType(CT_LINE), extCamChanged(false), rotz(), thickness_(1), scene_dir()
522 {
523  // set scene_dir from #define VISP_SCENE_DIR if it exists
524  if (vpIoTools::checkDirectory(VISP_SCENES_DIR) == true) // directory exists
525  scene_dir = VISP_SCENES_DIR;
526  else {
527  try {
528  scene_dir = vpIoTools::getenv("VISP_SCENES_DIR");
529  std::cout << "The simulator uses data from VISP_SCENES_DIR=" << scene_dir << std::endl;
530  }
531  catch (...) {
532  std::cout << "Cannot get VISP_SCENES_DIR environment variable" << std::endl;
533  }
534  }
535 
536  open_display();
537  open_clipping();
538 
539  old_iPr = vpImagePoint(-1,-1);
540  old_iPz = vpImagePoint(-1,-1);
541  old_iPt = vpImagePoint(-1,-1);
542 
543  rotz.buildFrom(0,0,0,0,0,vpMath::rad(180));
544 
545  scene.name = NULL;
546  scene.bound.ptr = NULL;
547  scene.bound.nbr = 0;
548 
549  desiredScene.name = NULL;
550  desiredScene.bound.ptr = NULL;
551  desiredScene.bound.nbr = 0;
552 
553  camera.name = NULL;
554  camera.bound.ptr = NULL;
555  camera.bound.nbr = 0;
556 }
557 
558 
563 {
564  if(sceneInitialized)
565  {
566  if(displayObject)
567  free_Bound_scene (&(this->scene));
568  if(displayCamera){
569  free_Bound_scene (&(this->camera));
570  }
572  free_Bound_scene (&(this->desiredScene));
573  }
574  close_clipping();
575  close_display ();
576 
577  cameraTrajectory.clear();
578  poseList.clear();
579  fMoList.clear();
580 }
581 
582 
592 void
594 {
595  char name_cam[FILENAME_MAX];
596  char name[FILENAME_MAX];
597 
598  object = obj;
599  this->desiredObject = desired_object;
600 
601  const char *scene_dir_ = scene_dir.c_str();
602  if (strlen(scene_dir_) >= FILENAME_MAX) {
604  "Not enough memory to intialize the camera name"));
605  }
606 
607  strcpy(name_cam, scene_dir_);
608  if (desiredObject != D_TOOL)
609  {
610  strcat(name_cam,"/camera.bnd");
611  set_scene(name_cam,&camera,cameraFactor);
612  }
613  else
614  {
615  strcat(name_cam,"/tool.bnd");
616  set_scene(name_cam,&(this->camera),1.0);
617  }
618 
619  strcpy(name, scene_dir_);
620  switch (obj)
621  {
622  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
623  case CUBE : { strcat(name, "/cube.bnd"); break; }
624  case PLATE : { strcat(name, "/plate.bnd"); break; }
625  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
626  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
627  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
628  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
629  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
630  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
631  case ROAD : { strcat(name, "/road.bnd"); break; }
632  case TIRE : { strcat(name, "/circles2.bnd"); break; }
633  case PIPE : { strcat(name, "/pipe.bnd"); break; }
634  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
635  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
636  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
637  case PLAN: { strcat(name, "/plan.bnd"); break; }
638  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
639  }
640  set_scene(name,&(this->scene),1.0);
641 
642  scene_dir_ = scene_dir.c_str();
643  if (strlen(scene_dir_) >= FILENAME_MAX) {
645  "Not enough memory to intialize the desired object name"));
646  }
647 
648  switch (desiredObject)
649  {
650  case D_STANDARD : { break; }
651  case D_CIRCLE : {
652  strcpy(name, scene_dir_);
653  strcat(name, "/circle_sq2.bnd");
654  break; }
655  case D_TOOL : {
656  strcpy(name, scene_dir_);
657  strcat(name, "/tool.bnd");
658  break; }
659  }
660  set_scene(name, &(this->desiredScene), 1.0);
661 
662  if (obj == PIPE) load_rfstack(IS_INSIDE);
663  else add_rfstack(IS_BACK);
664 
665  add_vwstack ("start","depth", 0.0, 100.0);
666  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
667  add_vwstack ("start","type", PERSPECTIVE);
668 
669  sceneInitialized = true;
670  displayObject = true;
671  displayDesiredObject = true;
672  displayCamera = true;
673  displayImageSimulator = true;
674 }
675 
676 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
677 
693 void
695 {
696  initScene(obj, desired_object);
697  objectImage.clear();
698  for(imObj.front(); !imObj.outside(); imObj.next()){
699  objectImage.push_back(imObj.value());
700  }
701  displayImageSimulator = true;
702 }
703 #endif
704 
717 void
718 vpWireFrameSimulator::initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desired_object, const std::list<vpImageSimulator> &imObj)
719 {
720  initScene(obj, desired_object);
721  objectImage = imObj;
722  displayImageSimulator = true;
723 }
724 
725 
735 void
736 vpWireFrameSimulator::initScene(const char* obj, const char* desired_object)
737 {
738  char name_cam[FILENAME_MAX];
739  char name[FILENAME_MAX];
740 
741  object = THREE_PTS;
742  this->desiredObject = D_STANDARD;
743 
744  const char *scene_dir_ = scene_dir.c_str();
745  if (strlen(scene_dir_) >= FILENAME_MAX) {
747  "Not enough memory to intialize the camera name"));
748  }
749 
750  strcpy(name_cam, scene_dir_);
751  strcat(name_cam,"/camera.bnd");
752  set_scene(name_cam,&camera,cameraFactor);
753 
754  if (strlen(obj) >= FILENAME_MAX) {
756  "Not enough memory to intialize the name"));
757  }
758 
759  strcpy(name,obj);
760  Model_3D model;
761  model = getExtension(obj);
762  if (model == BND_MODEL)
763  set_scene(name,&(this->scene),1.0);
764  else if (model == WRL_MODEL)
765  set_scene_wrl(name,&(this->scene),1.0);
766  else if (model == UNKNOWN_MODEL)
767  {
768  vpERROR_TRACE("Unknown file extension for the 3D model");
769  }
770 
771  if (strlen(desired_object) >= FILENAME_MAX) {
773  "Not enough memory to intialize the camera name"));
774  }
775 
776  strcpy(name,desired_object);
777  model = getExtension(desired_object);
778  if (model == BND_MODEL)
779  set_scene(name,&(this->desiredScene),1.0);
780  else if (model == WRL_MODEL)
781  set_scene_wrl(name,&(this->desiredScene),1.0);
782  else if (model == UNKNOWN_MODEL)
783  {
784  vpERROR_TRACE("Unknown file extension for the 3D model");
785  }
786 
787  add_rfstack(IS_BACK);
788 
789  add_vwstack ("start","depth", 0.0, 100.0);
790  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
791  add_vwstack ("start","type", PERSPECTIVE);
792 
793  sceneInitialized = true;
794  displayObject = true;
795  displayDesiredObject = true;
796  displayCamera = true;
797 }
798 
799 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
800 
815 void
816 vpWireFrameSimulator::initScene(const char* obj, const char* desired_object, vpList<vpImageSimulator> &imObj)
817 {
818  initScene(obj, desired_object);
819  objectImage.clear();
820  for(imObj.front(); !imObj.outside(); imObj.next()){
821  objectImage.push_back(imObj.value());
822  }
823  displayImageSimulator = true;
824 }
825 #endif
826 
839 void
840 vpWireFrameSimulator::initScene(const char* obj, const char* desired_object, const std::list<vpImageSimulator> &imObj)
841 {
842  initScene(obj, desired_object);
843  objectImage = imObj;
844  displayImageSimulator = true;
845 }
846 
855 void
857 {
858  char name_cam[FILENAME_MAX];
859  char name[FILENAME_MAX];
860 
861  object = obj;
862 
863  const char *scene_dir_ = scene_dir.c_str();
864  if (strlen(scene_dir_) >= FILENAME_MAX) {
866  "Not enough memory to intialize the camera name"));
867  }
868 
869  strcpy(name_cam, scene_dir_);
870  strcat(name_cam,"/camera.bnd");
871  set_scene(name_cam,&camera,cameraFactor);
872 
873  strcpy(name, scene_dir_);
874  switch (obj)
875  {
876  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
877  case CUBE : { strcat(name, "/cube.bnd"); break; }
878  case PLATE : { strcat(name, "/plate.bnd"); break; }
879  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
880  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
881  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
882  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
883  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
884  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
885  case ROAD : { strcat(name, "/road.bnd"); break; }
886  case TIRE : { strcat(name, "/circles2.bnd"); break; }
887  case PIPE : { strcat(name, "/pipe.bnd"); break; }
888  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
889  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
890  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
891  case PLAN: { strcat(name, "/plan.bnd"); break; }
892  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
893  }
894  set_scene(name,&(this->scene),1.0);
895 
896  if (obj == PIPE) load_rfstack(IS_INSIDE);
897  else add_rfstack(IS_BACK);
898 
899  add_vwstack ("start","depth", 0.0, 100.0);
900  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
901  add_vwstack ("start","type", PERSPECTIVE);
902 
903  sceneInitialized = true;
904  displayObject = true;
905  displayCamera = true;
906 
907  displayDesiredObject = false;
908  displayImageSimulator = false;
909 }
910 
911 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
912 
926 void
928 {
929  initScene(obj);
930  objectImage.clear();
931  for(imObj.front(); !imObj.outside(); imObj.next()){
932  objectImage.push_back(imObj.value());
933  }
934  displayImageSimulator = true;
935 }
936 #endif
937 
949 void
950 vpWireFrameSimulator::initScene(const vpSceneObject &obj, const std::list<vpImageSimulator> &imObj)
951 {
952  initScene(obj);
953  objectImage = imObj;
954  displayImageSimulator = true;
955 }
956 
965 void
967 {
968  char name_cam[FILENAME_MAX];
969  char name[FILENAME_MAX];
970 
971  object = THREE_PTS;
972 
973  const char *scene_dir_ = scene_dir.c_str();
974  if (strlen(scene_dir_) >= FILENAME_MAX) {
976  "Not enough memory to intialize the camera name"));
977  }
978 
979  strcpy(name_cam, scene_dir_);
980  strcat(name_cam,"/camera.bnd");
981  set_scene(name_cam,&camera,cameraFactor);
982 
983  if (strlen(obj) >= FILENAME_MAX) {
985  "Not enough memory to intialize the name"));
986  }
987 
988  strcpy(name,obj);
989  Model_3D model;
990  model = getExtension(obj);
991  if (model == BND_MODEL)
992  set_scene(name,&(this->scene),1.0);
993  else if (model == WRL_MODEL)
994  set_scene_wrl(name,&(this->scene),1.0);
995  else if (model == UNKNOWN_MODEL)
996  {
997  vpERROR_TRACE("Unknown file extension for the 3D model");
998  }
999 
1000  add_rfstack(IS_BACK);
1001 
1002  add_vwstack ("start","depth", 0.0, 100.0);
1003  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
1004  add_vwstack ("start","type", PERSPECTIVE);
1005 
1006  sceneInitialized = true;
1007  displayObject = true;
1008  displayCamera = true;
1009 }
1010 
1011 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
1012 
1026 void
1028 {
1029  initScene(obj);
1030  objectImage.clear();
1031  for(imObj.front(); !imObj.outside(); imObj.next()){
1032  objectImage.push_back(imObj.value());
1033  }
1034  displayImageSimulator = true;
1035 }
1036 #endif
1037 
1049 void
1050 vpWireFrameSimulator::initScene(const char* obj, const std::list<vpImageSimulator> &imObj)
1051 {
1052  initScene(obj);
1053  objectImage = imObj;
1054  displayImageSimulator = true;
1055 }
1056 
1064 void
1066 {
1067  if (!sceneInitialized)
1068  throw(vpException(vpSimulatorException::notInitializedError,"The scene has to be initialized")) ;
1069 
1070  double u;
1071  double v;
1072  //if(px_int != 1 && py_int != 1)
1073  // we assume px_int and py_int > 0
1074  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1075  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1076  {
1077  u = (double)I.getWidth()/(2*px_int);
1078  v = (double)I.getHeight()/(2*py_int);
1079  }
1080  else
1081  {
1082  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1083  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1084  }
1085 
1086  float o44c[4][4],o44cd[4][4],x,y,z;
1087  Matrix id = IDENTITY_MATRIX;
1088 
1089  vp2jlc_matrix(cMo.inverse(),o44c);
1090  vp2jlc_matrix(cdMo.inverse(),o44cd);
1091 
1093  {
1094  I = 255;
1095 
1096  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1097  vpImageSimulator* imSim = &(*it);
1098  imSim->setCameraPosition(rotz*cMo);
1100  }
1101 
1102  if (I.display != NULL)
1103  vpDisplay::display(I);
1104  }
1105 
1106  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1107  x = o44c[2][0] + o44c[3][0];
1108  y = o44c[2][1] + o44c[3][1];
1109  z = o44c[2][2] + o44c[3][2];
1110  add_vwstack ("start","vrp", x,y,z);
1111  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1112  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1113  add_vwstack ("start","window", -u, u, -v, v);
1114  if (displayObject)
1115  display_scene(id,this->scene,I, curColor);
1116 
1117 
1118  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1119  x = o44cd[2][0] + o44cd[3][0];
1120  y = o44cd[2][1] + o44cd[3][1];
1121  z = o44cd[2][2] + o44cd[3][2];
1122  add_vwstack ("start","vrp", x,y,z);
1123  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1124  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1125  add_vwstack ("start","window", -u, u, -v, v);
1127  {
1129  else display_scene(id,desiredScene,I, desColor);
1130  }
1131 }
1132 
1133 
1142 void
1144 {
1145  bool changed = false;
1146  vpHomogeneousMatrix displacement = navigation(I,changed);
1147 
1148  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1149  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1150  camMf2 = camMf2*displacement;
1151 
1152  f2Mf = camMf2.inverse()*camMf;
1153 
1154  camMf = camMf2* displacement * f2Mf;
1155 
1156  double u;
1157  double v;
1158  //if(px_ext != 1 && py_ext != 1)
1159  // we assume px_ext and py_ext > 0
1160  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1161  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1162  {
1163  u = (double)I.getWidth()/(2*px_ext);
1164  v = (double)I.getHeight()/(2*py_ext);
1165  }
1166  else
1167  {
1168  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1169  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1170  }
1171 
1172  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1173 
1174  vp2jlc_matrix(camMf.inverse(),w44cext);
1175  vp2jlc_matrix(fMc,w44c);
1176  vp2jlc_matrix(fMo,w44o);
1177 
1178 
1179  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1180  x = w44cext[2][0] + w44cext[3][0];
1181  y = w44cext[2][1] + w44cext[3][1];
1182  z = w44cext[2][2] + w44cext[3][2];
1183  add_vwstack ("start","vrp", x,y,z);
1184  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1185  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1186  add_vwstack ("start","window", -u, u, -v, v);
1187  if ((object == CUBE) || (object == SPHERE))
1188  {
1189  add_vwstack ("start","type", PERSPECTIVE);
1190  }
1191 
1193  {
1194  I = 255;
1195 
1196  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1197  vpImageSimulator* imSim = &(*it);
1198  imSim->setCameraPosition(rotz*camMf*fMo);
1200  }
1201 
1202  if (I.display != NULL)
1203  vpDisplay::display(I);
1204  }
1205 
1206  if (displayObject)
1207  display_scene(w44o,this->scene,I, curColor);
1208 
1209  if (displayCamera)
1210  display_scene(w44c,camera, I, camColor);
1211 
1213  {
1214  vpImagePoint iP;
1215  vpImagePoint iP_1;
1216  poseList.push_back(cMo);
1217  fMoList.push_back(fMo);
1218 
1219  int iter = 0;
1220 
1221  if (changed || extCamChanged)
1222  {
1223  cameraTrajectory.clear();
1224  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1225  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1226 
1227  while ((iter_poseList != poseList.end()) && (iter_fMoList != fMoList.end()))
1228  {
1229  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1230  cameraTrajectory.push_back(iP);
1231  if (camTrajType == CT_LINE)
1232  {
1233  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor, thickness_);
1234  }
1235  else if (camTrajType == CT_POINT)
1237  ++iter_poseList;
1238  ++iter_fMoList;
1239  iter++;
1240  iP_1 = iP;
1241  }
1242  extCamChanged = false;
1243  }
1244  else
1245  {
1246  iP = projectCameraTrajectory(I, cMo, fMo);
1247  cameraTrajectory.push_back(iP);
1248 
1249  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1250  if (camTrajType == CT_LINE)
1251  {
1252  if (iter != 0) vpDisplay::displayLine(I, iP_1, *it, camTrajColor, thickness_);
1253  }
1254  else if (camTrajType == CT_POINT)
1256  iter++;
1257  iP_1 = *it;
1258  }
1259  }
1260 
1261  if (poseList.size() > nbrPtLimit)
1262  {
1263  poseList.pop_front();
1264  }
1265  if (fMoList.size() > nbrPtLimit)
1266  {
1267  fMoList.pop_front();
1268  }
1269  if (cameraTrajectory.size() > nbrPtLimit)
1270  {
1271  cameraTrajectory.pop_front();
1272  }
1273  }
1274 }
1275 
1276 
1285 void
1287 {
1288  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1289 
1290  vpHomogeneousMatrix camMft = rotz * cam_Mf;
1291 
1292  double u;
1293  double v;
1294  //if(px_ext != 1 && py_ext != 1)
1295  // we assume px_ext and py_ext > 0
1296  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1297  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1298  {
1299  u = (double)I.getWidth()/(2*px_ext);
1300  v = (double)I.getHeight()/(2*py_ext);
1301  }
1302  else
1303  {
1304  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1305  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1306  }
1307 
1308  vp2jlc_matrix(camMft.inverse(),w44cext);
1309  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1310  vp2jlc_matrix(fMo,w44o);
1311 
1312  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1313  x = w44cext[2][0] + w44cext[3][0];
1314  y = w44cext[2][1] + w44cext[3][1];
1315  z = w44cext[2][2] + w44cext[3][2];
1316  add_vwstack ("start","vrp", x,y,z);
1317  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1318  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1319  add_vwstack ("start","window", -u, u, -v, v);
1320 
1322  {
1323  I = 255;
1324 
1325  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1326  vpImageSimulator* imSim = &(*it);
1327  imSim->setCameraPosition(rotz*cam_Mf*fMo);
1329  }
1330 
1331  if (I.display != NULL)
1332  vpDisplay::display(I);
1333  }
1334 
1335  if (displayObject)
1336  display_scene(w44o,this->scene,I, curColor);
1337  if (displayCamera)
1338  display_scene(w44c,camera, I, camColor);
1339 }
1340 
1341 
1349 void
1351 {
1352  if (!sceneInitialized)
1353  throw(vpException(vpSimulatorException::notInitializedError,"The scene has to be initialized")) ;
1354 
1355  double u;
1356  double v;
1357  //if(px_int != 1 && py_int != 1)
1358  // we assume px_int and py_int > 0
1359  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1360  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1361  {
1362  u = (double)I.getWidth()/(2*px_int);
1363  v = (double)I.getHeight()/(2*py_int);
1364  }
1365  else
1366  {
1367  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1368  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1369  }
1370 
1371  float o44c[4][4],o44cd[4][4],x,y,z;
1372  Matrix id = IDENTITY_MATRIX;
1373 
1374  vp2jlc_matrix(cMo.inverse(),o44c);
1375  vp2jlc_matrix(cdMo.inverse(),o44cd);
1376 
1378  {
1379  I = 255;
1380 
1381  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1382  vpImageSimulator* imSim = &(*it);
1383  imSim->setCameraPosition(rotz*camMf*fMo);
1385  }
1386 
1387  if (I.display != NULL)
1388  vpDisplay::display(I);
1389  }
1390 
1391  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1392  x = o44c[2][0] + o44c[3][0];
1393  y = o44c[2][1] + o44c[3][1];
1394  z = o44c[2][2] + o44c[3][2];
1395  add_vwstack ("start","vrp", x,y,z);
1396  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1397  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1398  add_vwstack ("start","window", -u, u, -v, v);
1399  if (displayObject)
1400  display_scene(id,this->scene,I, curColor);
1401 
1402 
1403  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1404  x = o44cd[2][0] + o44cd[3][0];
1405  y = o44cd[2][1] + o44cd[3][1];
1406  z = o44cd[2][2] + o44cd[3][2];
1407  add_vwstack ("start","vrp", x,y,z);
1408  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1409  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1410  add_vwstack ("start","window", -u, u, -v, v);
1412  {
1414  else display_scene(id,desiredScene,I, desColor);
1415  }
1416 }
1417 
1418 
1427 void
1429 {
1430  bool changed = false;
1431  vpHomogeneousMatrix displacement = navigation(I,changed);
1432 
1433  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1434  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1435  camMf2 = camMf2*displacement;
1436 
1437  f2Mf = camMf2.inverse()*camMf;
1438 
1439  camMf = camMf2* displacement * f2Mf;
1440 
1441  double u;
1442  double v;
1443  //if(px_ext != 1 && py_ext != 1)
1444  // we assume px_ext and py_ext > 0
1445  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1446  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1447  {
1448  u = (double)I.getWidth()/(2*px_ext);
1449  v = (double)I.getHeight()/(2*py_ext);
1450  }
1451  else
1452  {
1453  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1454  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1455  }
1456 
1457  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1458 
1459  vp2jlc_matrix(camMf.inverse(),w44cext);
1460  vp2jlc_matrix(fMc,w44c);
1461  vp2jlc_matrix(fMo,w44o);
1462 
1463 
1464  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1465  x = w44cext[2][0] + w44cext[3][0];
1466  y = w44cext[2][1] + w44cext[3][1];
1467  z = w44cext[2][2] + w44cext[3][2];
1468  add_vwstack ("start","vrp", x,y,z);
1469  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1470  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1471  add_vwstack ("start","window", -u, u, -v, v);
1472  if ((object == CUBE) || (object == SPHERE))
1473  {
1474  add_vwstack ("start","type", PERSPECTIVE);
1475  }
1476 
1478  {
1479  I = 255;
1480  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1481  vpImageSimulator* imSim = &(*it);
1482  imSim->setCameraPosition(rotz*camMf*fMo);
1484  }
1485  if (I.display != NULL)
1486  vpDisplay::display(I);
1487  }
1488 
1489  if (displayObject)
1490  display_scene(w44o,this->scene,I, curColor);
1491 
1492  if (displayCamera)
1493  display_scene(w44c,camera, I, camColor);
1494 
1496  {
1497  vpImagePoint iP;
1498  vpImagePoint iP_1;
1499  poseList.push_back(cMo);
1500  fMoList.push_back(fMo);
1501 
1502  int iter = 0;
1503 
1504  if (changed || extCamChanged)
1505  {
1506  cameraTrajectory.clear();
1507  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1508  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1509 
1510  while ((iter_poseList!=poseList.end()) && (iter_fMoList!=fMoList.end()) )
1511  {
1512  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1513  cameraTrajectory.push_back(iP);
1514  //vpDisplay::displayPoint(I,cameraTrajectory.value(),vpColor::green);
1515  if (camTrajType == CT_LINE)
1516  {
1517  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor, thickness_);
1518  }
1519  else if (camTrajType == CT_POINT)
1521  ++iter_poseList;
1522  ++iter_fMoList;
1523  iter++;
1524  iP_1 = iP;
1525  }
1526  extCamChanged = false;
1527  }
1528  else
1529  {
1530  iP = projectCameraTrajectory(I, cMo,fMo);
1531  cameraTrajectory.push_back(iP);
1532 
1533  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1534  if (camTrajType == CT_LINE){
1535  if (iter != 0) vpDisplay::displayLine(I,iP_1,*it,camTrajColor, thickness_);
1536  }
1537  else if(camTrajType == CT_POINT)
1539  iter++;
1540  iP_1 = *it;
1541  }
1542  }
1543 
1544  if (poseList.size() > nbrPtLimit)
1545  {
1546  poseList.pop_front();
1547  }
1548  if (fMoList.size() > nbrPtLimit)
1549  {
1550  fMoList.pop_front();
1551  }
1552  if (cameraTrajectory.size() > nbrPtLimit)
1553  {
1554  cameraTrajectory.pop_front();
1555  }
1556  }
1557 }
1558 
1559 
1568 void
1570 {
1571  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1572 
1573  vpHomogeneousMatrix camMft = rotz * cam_Mf;
1574 
1575  double u;
1576  double v;
1577  //if(px_ext != 1 && py_ext != 1)
1578  // we assume px_ext and py_ext > 0
1579  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1580  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1581  {
1582  u = (double)I.getWidth()/(2*px_ext);
1583  v = (double)I.getHeight()/(2*py_ext);
1584  }
1585  else
1586  {
1587  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1588  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1589  }
1590 
1591  vp2jlc_matrix(camMft.inverse(),w44cext);
1592  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1593  vp2jlc_matrix(fMo,w44o);
1594 
1595  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1596  x = w44cext[2][0] + w44cext[3][0];
1597  y = w44cext[2][1] + w44cext[3][1];
1598  z = w44cext[2][2] + w44cext[3][2];
1599  add_vwstack ("start","vrp", x,y,z);
1600  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1601  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1602  add_vwstack ("start","window", -u, u, -v, v);
1603 
1605  {
1606  I = 255;
1607  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1608  vpImageSimulator* imSim = &(*it);
1609  imSim->setCameraPosition(rotz*cam_Mf*fMo);
1611  }
1612  if (I.display != NULL)
1613  vpDisplay::display(I);
1614  }
1615 
1616  if (displayObject)
1617  display_scene(w44o,this->scene,I, curColor);
1618  if (displayCamera)
1619  display_scene(w44c,camera, I, camColor);
1620 }
1621 
1622 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
1623 
1637 void
1639 {
1640  if (list_cMo.nbElements() != list_fMo.nbElements())
1641  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1642 
1643  list_cMo.front();
1644  list_fMo.front();
1645  vpImagePoint iP;
1646  vpImagePoint iP_1;
1647  int iter = 0;
1648 
1649  while (!list_cMo.outside() && !list_fMo.outside())
1650  {
1651  iP = projectCameraTrajectory(I, rotz * list_cMo.value(), list_fMo.value(), rotz * cMf);
1652  if (camTrajType == CT_LINE)
1653  {
1654  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor, thickness_);
1655  }
1656  else if (camTrajType == CT_POINT)
1658  list_cMo.next();
1659  list_fMo.next();
1660  iter++;
1661  iP_1 = iP;
1662  }
1663 }
1664 
1679 void
1681 {
1682  if (list_cMo.nbElements() != list_fMo.nbElements())
1683  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1684 
1685  list_cMo.front();
1686  list_fMo.front();
1687  vpImagePoint iP;
1688  vpImagePoint iP_1;
1689  int iter = 0;
1690 
1691  while (!list_cMo.outside() && !list_fMo.outside())
1692  {
1693  iP = projectCameraTrajectory(I, rotz * list_cMo.value(), list_fMo.value(), rotz * cMf);
1694  if (camTrajType == CT_LINE)
1695  {
1696  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor,thickness_);
1697  }
1698  else if (camTrajType == CT_POINT)
1700  list_cMo.next();
1701  list_fMo.next();
1702  iter++;
1703  iP_1 = iP;
1704  }
1705 }
1706 #endif
1707 
1708 
1719 void
1720 vpWireFrameSimulator::displayTrajectory (const vpImage<unsigned char> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1721  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1722 {
1723  if (list_cMo.size() != list_fMo.size())
1724  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1725 
1726  vpImagePoint iP;
1727  vpImagePoint iP_1;
1728  int iter = 0;
1729 
1730  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1731  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1732 
1733  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1734  {
1735  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1736  if (camTrajType == CT_LINE)
1737  {
1738  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor,thickness_);
1739  }
1740  else if (camTrajType == CT_POINT)
1742  ++it_cMo;
1743  ++it_fMo;
1744  iter++;
1745  iP_1 = iP;
1746  }
1747 }
1748 
1759 void
1760 vpWireFrameSimulator::displayTrajectory (const vpImage<vpRGBa> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1761  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1762 {
1763  if (list_cMo.size() != list_fMo.size())
1764  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1765 
1766  vpImagePoint iP;
1767  vpImagePoint iP_1;
1768  int iter = 0;
1769 
1770  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1771  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1772 
1773  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1774  {
1775  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1776  if (camTrajType == CT_LINE)
1777  {
1778  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor,thickness_);
1779  }
1780  else if (camTrajType == CT_POINT)
1782  ++it_cMo;
1783  ++it_fMo;
1784  iter++;
1785  iP_1 = iP;
1786  }
1787 }
1788 
1789 
1795 {
1796  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1797  vpImagePoint iP;
1798  vpImagePoint trash;
1799  bool clicked = false;
1800  bool clickedUp = false;
1802 
1803  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1804  changed = false;
1805 
1806  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1807 
1808  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1809 
1810  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1811 
1812  if(clicked)
1813  {
1814  if (b == vpMouseButton::button1) blockedr = true;
1815  if (b == vpMouseButton::button2) blockedz = true;
1816  if (b == vpMouseButton::button3) blockedt = true;
1817  blocked = true;
1818  }
1819  if(clickedUp)
1820  {
1821  if (b == vpMouseButton::button1)
1822  {
1823  old_iPr = vpImagePoint(-1,-1);
1824  blockedr = false;
1825  }
1826  if (b == vpMouseButton::button2)
1827  {
1828  old_iPz = vpImagePoint(-1,-1);
1829  blockedz = false;
1830  }
1831  if (b == vpMouseButton::button3)
1832  {
1833  old_iPt = vpImagePoint(-1,-1);
1834  blockedt = false;
1835  }
1836  if (!(blockedr || blockedz || blockedt))
1837  {
1838  blocked = false;
1839  while (vpDisplay::getClick(I,trash,b,false)) {};
1840  }
1841  }
1842 
1844 
1845  double anglei = 0;
1846  double anglej = 0;
1847 
1848  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1849  {
1850  double diffi = iP.get_i() - old_iPr.get_i();
1851  double diffj = iP.get_j() - old_iPr.get_j();
1852  //cout << "delta :" << diffj << endl;;
1853  anglei = diffi*360/width;
1854  anglej = diffj*360/width;
1855  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1856  changed = true;
1857  }
1858 
1859  if (blockedr) old_iPr = iP;
1860 
1861  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1862  {
1863  double diffi = iP.get_i() - old_iPz.get_i();
1864  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1865  changed = true;
1866  }
1867 
1868  if (blockedz) old_iPz = iP;
1869 
1870  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1871  {
1872  double diffi = iP.get_i() - old_iPt.get_i();
1873  double diffj = iP.get_j() - old_iPt.get_j();
1874  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1875  changed = true;
1876  }
1877 
1878  if (blockedt) old_iPt = iP;
1879 
1880  return mov;
1881 }
1882 
1883 
1889 {
1890  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1891  vpImagePoint iP;
1892  vpImagePoint trash;
1893  bool clicked = false;
1894  bool clickedUp = false;
1896 
1897  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1898  changed = false;
1899 
1900  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1901 
1902  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1903 
1904  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1905 
1906  if(clicked)
1907  {
1908  if (b == vpMouseButton::button1) blockedr = true;
1909  if (b == vpMouseButton::button2) blockedz = true;
1910  if (b == vpMouseButton::button3) blockedt = true;
1911  blocked = true;
1912  }
1913  if(clickedUp)
1914  {
1915  if (b == vpMouseButton::button1)
1916  {
1917  old_iPr = vpImagePoint(-1,-1);
1918  blockedr = false;
1919  }
1920  if (b == vpMouseButton::button2)
1921  {
1922  old_iPz = vpImagePoint(-1,-1);
1923  blockedz = false;
1924  }
1925  if (b == vpMouseButton::button3)
1926  {
1927  old_iPt = vpImagePoint(-1,-1);
1928  blockedt = false;
1929  }
1930  if (!(blockedr || blockedz || blockedt))
1931  {
1932  blocked = false;
1933  while (vpDisplay::getClick(I,trash,b,false)) {};
1934  }
1935  }
1936 
1938 
1939  //std::cout << "point : " << iP << std::endl;
1940 
1941  double anglei = 0;
1942  double anglej = 0;
1943 
1944  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1945  {
1946  double diffi = iP.get_i() - old_iPr.get_i();
1947  double diffj = iP.get_j() - old_iPr.get_j();
1948  //cout << "delta :" << diffj << endl;;
1949  anglei = diffi*360/width;
1950  anglej = diffj*360/width;
1951  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1952  changed = true;
1953  }
1954 
1955  if (blockedr) old_iPr = iP;
1956 
1957  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1958  {
1959  double diffi = iP.get_i() - old_iPz.get_i();
1960  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1961  changed = true;
1962  }
1963 
1964  if (blockedz) old_iPz = iP;
1965 
1966  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1967  {
1968  double diffi = iP.get_i() - old_iPt.get_i();
1969  double diffj = iP.get_j() - old_iPt.get_j();
1970  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1971  changed = true;
1972  }
1973 
1974  if (blockedt) old_iPt = iP;
1975 
1976  return mov;
1977 }
1978 
1984  const vpHomogeneousMatrix &fMo_)
1985 {
1986  vpPoint point;
1987  point.setWorldCoordinates(0,0,0);
1988 
1989  point.track(rotz*(camMf*fMo_*cMo_.inverse())) ;
1990 
1991  vpImagePoint iP;
1992 
1994 
1995  return iP;
1996 }
1997 
2003  const vpHomogeneousMatrix &cMo_,
2004  const vpHomogeneousMatrix &fMo_)
2005 {
2006  vpPoint point;
2007  point.setWorldCoordinates(0,0,0);
2008 
2009  point.track(rotz*(camMf*fMo_*cMo_.inverse())) ;
2010 
2011  vpImagePoint iP;
2012 
2014 
2015  return iP;
2016 }
2017 
2023  const vpHomogeneousMatrix &fMo_, const vpHomogeneousMatrix &cMf)
2024 {
2025  vpPoint point;
2026  point.setWorldCoordinates(0,0,0);
2027 
2028  point.track(rotz*(cMf*fMo_*cMo_.inverse())) ;
2029 
2030  vpImagePoint iP;
2031 
2033 
2034  return iP;
2035 }
2036 
2042  const vpHomogeneousMatrix &fMo_, const vpHomogeneousMatrix &cMf)
2043 {
2044  vpPoint point;
2045  point.setWorldCoordinates(0,0,0);
2046 
2047  point.track(rotz*(cMf*fMo_*cMo_.inverse())) ;
2048 
2049  vpImagePoint iP;
2050 
2052 
2053  return iP;
2054 }
2055 
The object displayed at the desired position is the same than the scene object defined in vpSceneObje...
vpDisplay * display
Definition: vpImage.h:121
A cylinder of 80cm length and 10cm radius.
unsigned int nbElements(void)
return the number of element in the list
Definition: vpList.h:259
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
A tire represented by 2 circles with radius 10cm and 15cm.
double get_i() const
Definition: vpImagePoint.h:194
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
Definition: vpList.h:429
unsigned int getWidth() const
Definition: vpImage.h:159
Three parallel lines with equation y=-5, y=0, y=5.
A plane represented by a 56cm by 56cm plate with a grid of 49 squares inside.
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:395
Provide simple list management.
Definition: vpList.h:113
A plate with 8 points at coordinates (0.05,0,0), (0.15,0.05,0), (0.2,0.2,0), (-0.05,0.2,0), (-0.15,-0.1,0), (-0.1,-0.1,0), (-0.05,0.05,0) and (0.5,0,0). ach point is represented by a circle with 2cm radius.
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
A 40cm by 40cm plate with 4 points at coordinates (-0.1,-0.1,0), (0.1,-0.1,0), (0.1,0.1,0), (0.1,0.1,0). Each point is represented by a circle with 2cm radius.
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Point coordinates conversion from normalized coordinates in meter to pixel coordinates ...
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
A cylindrical tool is attached to the camera.
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:208
virtual bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)=0
std::list< vpImageSimulator > objectImage
error that can be emited by ViSP classes.
Definition: vpException.h:76
std::list< vpImagePoint > cameraTrajectory
void track(const vpHomogeneousMatrix &cMo)
vpHomogeneousMatrix cdMo
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:138
A 40cm by 40cm plate with 4 points at coordinates (-0.05,0.05,0), (0.05,0.05,0), (0.05,-0.05,0), (-0.05,-0.05,0). Each point is represented by a circle with 2cm radius.
vpHomogeneousMatrix fMo
double get_j() const
Definition: vpImagePoint.h:205
std::list< vpHomogeneousMatrix > fMoList
void next(void)
position the current element on the next one
Definition: vpList.h:273
static const vpColor red
Definition: vpColor.h:167
Class that defines what is a point.
Definition: vpPoint.h:65
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
4 points at coordinates (-0.03,-0.03,0), (0.03,-0.03,0), (0.03,0.03,0), (0.03,0.03,0). Each point is represented by a circle with 1cm radius.
vpCameraParameters getExternalCameraParameters(const vpImage< unsigned char > &I) const
void setCameraPosition(const vpHomogeneousMatrix &cMt)
virtual bool getPointerPosition(vpImagePoint &ip)=0
The object displayed at the desired position is a circle.
vpHomogeneousMatrix f2Mf
A 40cm by 40cm plate with 4 points at coordinates (-0.025,-0.05,0), (-0.075,0.05,0), (0.075,0.05,0), (0.025,-0.05,0). Each point is represented by a circle with 2cm radius.
void front(void)
Position the current element on the first element of the list.
Definition: vpList.h:384
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
type & value(void)
return the value of the current element
Definition: vpList.h:301
Class which enables to project an image in the 3D space and get the view of a virtual camera...
void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject)
vpHomogeneousMatrix cMo
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.h:136
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:148
A 40cm by 40cm plate with 3 points at coordinates (0,0,0), (0.1,0,0), (0,0.1,0). Each point is repres...
void getExternalImage(vpImage< unsigned char > &I)
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
vpCameraParameters getInternalCameraParameters(const vpImage< unsigned char > &I) const
static double rad(double deg)
Definition: vpMath.h:100
void displayTrajectory(const vpImage< unsigned char > &I, const std::list< vpHomogeneousMatrix > &list_cMo, const std::list< vpHomogeneousMatrix > &list_fMo, const vpHomogeneousMatrix &camMf)
vpCameraTrajectoryDisplayType camTrajType
A 40cm by 40cm plate with 4 points at coordinates (-0.07,-0.05,0), (0.07,0.05,0), (0...
vpSceneDesiredObject desiredObject
A 40cm by 40cm plate with 4 points at coordinates (0,-0.1,0), (0.1,0,0), (0,0.1,0), (-0.1,0,0). Each point is represented by a circle with 2cm radius.
void getInternalImage(vpImage< unsigned char > &I)
void display_scene(Matrix mat, Bound_scene &sc, const vpImage< vpRGBa > &I, const vpColor &color)
vpHomogeneousMatrix camMf
vpHomogeneousMatrix navigation(const vpImage< vpRGBa > &I, bool &changed)
Three parallel lines representing a road.
vpHomogeneousMatrix fMc
vpImagePoint projectCameraTrajectory(const vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo, const vpHomogeneousMatrix &fMo)
vpHomogeneousMatrix inverse() const
vpHomogeneousMatrix rotz
A pipe represented by a cylinder of 25 cm length and 15cm radius.
unsigned int getHeight() const
Definition: vpImage.h:150
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
std::list< vpHomogeneousMatrix > poseList
vpHomogeneousMatrix camMf2
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74