ViSP  2.6.2
vpWireFrameSimulator.cpp
1 /****************************************************************************
2  *
3  * $Id: vpWireFrameSimulator.cpp 3599 2012-03-07 11:06:36Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 #if defined(WIN32)
61 #define bcopy(b1,b2,len) (memmove((b2), (b1), (len)), (void) 0)
62 #endif
63 
64 
65 //Inventor includes
66 #if defined(VISP_HAVE_COIN)
67 #include <Inventor/nodes/SoSeparator.h>
68 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
69 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
70 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
71 #include <Inventor/actions/SoWriteAction.h>
72 #include <Inventor/actions/SoSearchAction.h>
73 #include <Inventor/misc/SoChildList.h>
74 #include <Inventor/actions/SoGetMatrixAction.h>
75 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
76 #include <Inventor/actions/SoToVRML2Action.h>
77 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
78 #include <Inventor/VRMLnodes/SoVRMLShape.h>
79 #endif
80 
81 extern "C"{extern Point2i *point2i;}
82 extern "C"{extern Point2i *listpoint2i;}
83 
84 typedef enum
85 {
86  BND_MODEL,
87  WRL_MODEL,
88  UNKNOWN_MODEL
89 } Model_3D;
90 
91 /*
92  Get the extension of the file and return it
93 */
94 Model_3D
95 getExtension(const char* file)
96 {
97  std::string sfilename(file);
98 
99  size_t bnd = sfilename.find("bnd");
100  size_t BND = sfilename.find("BND");
101  size_t wrl = sfilename.find("wrl");
102  size_t WRL = sfilename.find("WRL");
103 
104  size_t size = sfilename.size();
105 
106  if ((bnd>0 && bnd<size ) || (BND>0 && BND<size))
107  return BND_MODEL;
108  else if ((wrl>0 && wrl<size) || ( WRL>0 && WRL<size))
109  {
110 #if defined(VISP_HAVE_COIN)
111  return WRL_MODEL;
112 #else
113  std::cout << "Coin not installed, cannot read VRML files" << std::endl;
114  throw std::string("Coin not installed, cannot read VRML files");
115 #endif
116  }
117  return UNKNOWN_MODEL;
118 }
119 
120 /*
121  Enable to initialize the scene
122 */
123 void set_scene (const char* str, Bound_scene *sc, float factor)
124 {
125  FILE *fd;
126 
127  //if ((fd = fopen (str, 0)) == -1)
128  if ((fd = fopen (str, "r")) == NULL)
129  {
130  char strerr[80];
131  strcpy (strerr,"The file ");
132  strcat (strerr,str);
133  strcat (strerr," can not be opened");
135  }
136  open_keyword (keyword_tbl);
137  open_lex ();
138  open_source (fd, str);
139  malloc_Bound_scene (sc, str,(Index)BOUND_NBR);
140  parser (sc);
141 
142  //if (factor != 1)
143  if (std::fabs(factor) > std::numeric_limits<double>::epsilon())
144  {
145  for (int i = 0; i < sc->bound.nbr; i++)
146  {
147  for (int j = 0; j < sc->bound.ptr[i].point.nbr; j++)
148  {
149  sc->bound.ptr[i].point.ptr[j].x = sc->bound.ptr[i].point.ptr[j].x*factor;
150  sc->bound.ptr[i].point.ptr[j].y = sc->bound.ptr[i].point.ptr[j].y*factor;
151  sc->bound.ptr[i].point.ptr[j].z = sc->bound.ptr[i].point.ptr[j].z*factor;
152  }
153  }
154  }
155 
156  close_source ();
157  close_lex ();
158  close_keyword ();
159  fclose(fd);
160 }
161 
162 #if defined(VISP_HAVE_COIN)
163 
164 #ifndef DOXYGEN_SHOULD_SKIP_THIS
165 typedef struct
166 {
167  int nbPt;
168  std::vector<vpPoint> pt;
169  int nbIndex;
170  std::vector<int> index;
171 } indexFaceSet;
172 #endif // DOXYGEN_SHOULD_SKIP_THIS
173 
174 void extractFaces(SoVRMLIndexedFaceSet*, indexFaceSet *ifs);
175 void ifsToBound (Bound*, std::list<indexFaceSet*> &);
176 void destroyIfs(std::list<indexFaceSet*> &);
177 
178 
179 void set_scene_wrl (const char* str, Bound_scene *sc, float factor)
180 {
181  //Load the sceneGraph
182  SoDB::init();
183  SoInput in;
184  SbBool ok = in.openFile(str);
185  SoSeparator *sceneGraph;
186  SoVRMLGroup *sceneGraphVRML2;
187 
188  if (!ok) {
189  vpERROR_TRACE("can't open file \"%s\" \n Please check the Marker_Less.ini file", str);
190  exit(1);
191  }
192 
193  if(!in.isFileVRML2())
194  {
195  sceneGraph = SoDB::readAll(&in);
196  if (sceneGraph == NULL) { /*return -1;*/ }
197  sceneGraph->ref();
198 
199  SoToVRML2Action tovrml2;
200  tovrml2.apply(sceneGraph);
201  sceneGraphVRML2 =tovrml2.getVRML2SceneGraph();
202  sceneGraphVRML2->ref();
203  sceneGraph->unref();
204  }
205  else
206  {
207  sceneGraphVRML2 = SoDB::readAllVRML(&in);
208  if (sceneGraphVRML2 == NULL) { /*return -1;*/ }
209  sceneGraphVRML2->ref();
210  }
211 
212  in.closeFile();
213 
214  int nbShapes = sceneGraphVRML2->getNumChildren();
215 
216  SoNode * child;
217 
218  malloc_Bound_scene (sc, str,(Index)BOUND_NBR);
219 
220  int iterShapes = 0;
221  for (int i = 0; i < nbShapes; i++)
222  {
223  int nbFaces = 0;
224  child = sceneGraphVRML2->getChild(i);
225  if (child->getTypeId() == SoVRMLShape::getClassTypeId())
226  {
227  std::list<indexFaceSet*> ifs_list;
228  SoChildList * child2list = child->getChildren();
229  for (int j = 0; j < child2list->getLength(); j++)
230  {
231  if (((SoNode*)child2list->get(j))->getTypeId() == SoVRMLIndexedFaceSet::getClassTypeId())
232  {
233  indexFaceSet *ifs = new indexFaceSet;
234  SoVRMLIndexedFaceSet * face_set;
235  face_set = (SoVRMLIndexedFaceSet*)child2list->get(j);
236  extractFaces(face_set,ifs);
237  ifs_list.push_back(ifs);
238  nbFaces++;
239  }
240 // if (((SoNode*)child2list->get(j))->getTypeId() == SoVRMLIndexedLineSet::getClassTypeId())
241 // {
242 // std::cout << "> We found a line" << std::endl;
243 // SoVRMLIndexedLineSet * line_set;
244 // line_set = (SoVRMLIndexedLineSet*)child2list->get(j);
245 // extractLines(line_set);
246 // }
247  }
248  sc->bound.nbr++;
249  ifsToBound (&(sc->bound.ptr[iterShapes]), ifs_list);
250  destroyIfs(ifs_list);
251  iterShapes++;
252  }
253  }
254 
255  //if (factor != 1)
256  if (std::fabs(factor) > std::numeric_limits<double>::epsilon())
257  {
258  for (int i = 0; i < sc->bound.nbr; i++)
259  {
260  for (int j = 0; j < sc->bound.ptr[i].point.nbr; j++)
261  {
262  sc->bound.ptr[i].point.ptr[j].x = sc->bound.ptr[i].point.ptr[j].x*factor;
263  sc->bound.ptr[i].point.ptr[j].y = sc->bound.ptr[i].point.ptr[j].y*factor;
264  sc->bound.ptr[i].point.ptr[j].z = sc->bound.ptr[i].point.ptr[j].z*factor;
265  }
266  }
267  }
268 }
269 
270 
271 void
272 extractFaces(SoVRMLIndexedFaceSet* face_set, indexFaceSet *ifs)
273 {
274 // vpList<vpPoint> pointList;
275 // pointList.kill();
276  SoVRMLCoordinate *coord = (SoVRMLCoordinate *)(face_set->coord.getValue());
277  int coordSize = coord->point.getNum();
278 
279  ifs->nbPt = coordSize;
280  for (int i = 0; i < coordSize; i++)
281  {
282  SbVec3f point(0,0,0);
283  point[0]=coord->point[i].getValue()[0];
284  point[1]=coord->point[i].getValue()[1];
285  point[2]=coord->point[i].getValue()[2];
286  vpPoint pt;
287  pt.setWorldCoordinates(point[0],point[1],point[2]);
288  ifs->pt.push_back(pt);
289  }
290 
291  SoMFInt32 indexList = face_set->coordIndex;
292  int indexListSize = indexList.getNum();
293 
294  ifs->nbIndex = indexListSize;
295  for (int i = 0; i < indexListSize; i++)
296  {
297  int index = face_set->coordIndex[i];
298  ifs->index.push_back(index);
299  }
300 }
301 
302 void ifsToBound (Bound* bptr, std::list<indexFaceSet*> &ifs_list)
303 {
304  int nbPt = 0;
305  for(std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it){
306  nbPt += (*it)->nbPt;
307  }
308  bptr->point.nbr = (Index)nbPt;
309  bptr->point.ptr = (Point3f *) malloc ((unsigned int)nbPt * sizeof (Point3f));
310 
311  ifs_list.front();
312  unsigned int iter = 0;
313  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
314  {
315  indexFaceSet* ifs = *it;
316  for (unsigned int j = 0; j < (unsigned int)ifs->nbPt; j++)
317  {
318  bptr->point.ptr[iter].x = (float)ifs->pt[j].get_oX();
319  bptr->point.ptr[iter].y = (float)ifs->pt[j].get_oY();
320  bptr->point.ptr[iter].z = (float)ifs->pt[j].get_oZ();
321  iter++;
322  }
323  }
324 
325  unsigned int nbFace = 0;
326  ifs_list.front();
327  std::list<int> indSize;
328  int indice = 0;
329  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
330  {
331  indexFaceSet* ifs = *it;
332  for (unsigned int j = 0; j < (unsigned int)ifs->nbIndex; j++)
333  {
334  if(ifs->index[j] == -1)
335  {
336  nbFace++;
337  indSize.push_back(indice);
338  indice = 0;
339  }
340  else indice++;
341  }
342  }
343 
344  bptr->face.nbr = (Index)nbFace;
345  bptr->face.ptr = (Face *) malloc (nbFace * sizeof (Face));
346 
347 
348  std::list<int>::const_iterator iter_indSize = indSize.begin();
349  for (unsigned int i = 0; i < indSize.size(); i++)
350  {
351  bptr->face.ptr[i].vertex.nbr = (Index)*iter_indSize;
352  bptr->face.ptr[i].vertex.ptr = (Index *) malloc ((unsigned int)*iter_indSize * sizeof (Index));
353  ++iter_indSize;
354  }
355 
356  int offset = 0;
357  indice = 0;
358  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
359  {
360  indexFaceSet* ifs = *it;
361  iter = 0;
362  for (unsigned int j = 0; j < (unsigned int)ifs->nbIndex; j++)
363  {
364  if(ifs->index[j] != -1)
365  {
366  bptr->face.ptr[indice].vertex.ptr[iter] = (Index)(ifs->index[j] + offset);
367  iter++;
368  }
369  else
370  {
371  iter = 0;
372  indice++;
373  }
374  }
375  offset += ifs->nbPt;
376  }
377 }
378 
379 void destroyIfs(std::list<indexFaceSet*> &ifs_list)
380 {
381  for(std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it){
382  delete *it;
383  }
384  ifs_list.clear();
385 }
386 #else
387 void set_scene_wrl (const char* /*str*/, Bound_scene* /*sc*/, float /*factor*/)
388 {
389 }
390 #endif
391 
392 
393 /*
394  Convert the matrix format to deal with the one in the simulator
395 */
396 void vp2jlc_matrix (const vpHomogeneousMatrix vpM, Matrix &jlcM)
397 {
398  for (unsigned int i = 0; i < 4; i++) {
399  for (unsigned int j = 0; j < 4; j++)
400  jlcM[j][i] = (float)vpM[i][j];
401  }
402 }
403 
404 /*
405  Copy the scene corresponding to the registeresd parameters in the image.
406 */
407 void
408 vpWireFrameSimulator::display_scene(Matrix mat, Bound_scene &sc, vpImage<vpRGBa> &I, vpColor color)
409 {
410  // extern Bound *clipping_Bound ();
411  Bound *bp, *bend;
412  Bound *clip; /* surface apres clipping */
413  Byte b = (Byte) *get_rfstack ();
414  Matrix m;
415 
416 
417  //bcopy ((char *) mat, (char *) m, sizeof (Matrix));
418  memmove((char *) m, (char *) mat, sizeof (Matrix));
419  View_to_Matrix (get_vwstack (), *(get_tmstack ()));
420  postmult_matrix (m, *(get_tmstack ()));
421  bp = sc.bound.ptr;
422  bend = bp + sc.bound.nbr;
423  for (; bp < bend; bp++)
424  {
425  if ((clip = clipping_Bound (bp, m)) != NULL)
426  {
427  Face *fp = clip->face.ptr;
428  Face *fend = fp + clip->face.nbr;
429 
430  set_Bound_face_display (clip, b); //regarde si is_visible
431 
432  point_3D_2D (clip->point.ptr, clip->point.nbr,I.getWidth(),I.getHeight(),point2i);
433  for (; fp < fend; fp++)
434  {
435  if (fp->is_visible)
436  {
437  wireframe_Face (fp, point2i);
438  Point2i *pt = listpoint2i;
439  for (int i = 1; i < fp->vertex.nbr; i++)
440  {
441  vpDisplay::displayLine(I,vpImagePoint((pt)->y,(pt)->x),vpImagePoint((pt+1)->y,(pt+1)->x),color,1);
442  pt++;
443  }
444  if (fp->vertex.nbr > 2)
445  {
446  vpDisplay::displayLine(I,vpImagePoint((listpoint2i)->y,(listpoint2i)->x),vpImagePoint((pt)->y,(pt)->x),color,1);
447  }
448  }
449  }
450  }
451  }
452 }
453 
454 /*
455  Copy the scene corresponding to the registeresd parameters in the image.
456 */
457 void
459 {
460  //extern Bound *clipping_Bound ();
461 
462  Bound *bp, *bend;
463  Bound *clip; /* surface apres clipping */
464  Byte b = (Byte) *get_rfstack ();
465  Matrix m;
466 
467 
468  bcopy ((char *) mat, (char *) m, sizeof (Matrix));
469  View_to_Matrix (get_vwstack (), *(get_tmstack ()));
470  postmult_matrix (m, *(get_tmstack ()));
471  bp = sc.bound.ptr;
472  bend = bp + sc.bound.nbr;
473  for (; bp < bend; bp++)
474  {
475  if ((clip = clipping_Bound (bp, m)) != NULL)
476  {
477  Face *fp = clip->face.ptr;
478  Face *fend = fp + clip->face.nbr;
479 
480  set_Bound_face_display (clip, b); //regarde si is_visible
481 
482  point_3D_2D (clip->point.ptr, clip->point.nbr,I.getWidth(),I.getHeight(),point2i);
483  for (; fp < fend; fp++)
484  {
485  if (fp->is_visible)
486  {
487  wireframe_Face (fp, point2i);
488  Point2i *pt = listpoint2i;
489  for (int i = 1; i < fp->vertex.nbr; i++)
490  {
491  vpDisplay::displayLine(I,vpImagePoint((pt)->y,(pt)->x),vpImagePoint((pt+1)->y,(pt+1)->x),color,1);
492  pt++;
493  }
494  if (fp->vertex.nbr > 2)
495  {
496  vpDisplay::displayLine(I,vpImagePoint((listpoint2i)->y,(listpoint2i)->x),vpImagePoint((pt)->y,(pt)->x),color,1);
497  }
498  }
499  }
500  }
501  }
502 }
503 
504 /*************************************************************************************************************/
505 
515 {
516  // set scene_dir from #define VISP_SCENE_DIR if it exists
517  if (vpIoTools::checkDirectory(VISP_SCENES_DIR) == true) // directory exists
518  scene_dir = VISP_SCENES_DIR;
519  else {
520  try {
521  scene_dir = vpIoTools::getenv("VISP_SCENES_DIR");
522  std::cout << "The simulator uses data from VISP_SCENES_DIR=" << scene_dir << std::endl;
523  }
524  catch (...) {
525  std::cout << "Cannot get VISP_SCENES_DIR environment variable" << std::endl;
526  }
527  }
528 
529  open_display();
530  open_clipping();
531 
536 
537  sceneInitialized = false;
538 
540  cameraTrajectory.clear();
541  poseList.clear();
542  fMoList.clear();
543 
544  fMo.setIdentity();
545 
546  old_iPr = vpImagePoint(-1,-1);
547  old_iPz = vpImagePoint(-1,-1);
548  old_iPt = vpImagePoint(-1,-1);
549  blockedr = false;
550  blockedz = false;
551  blockedt = false;
552  blocked = false;
553 
554  nbrPtLimit = 1000;
555 
556  px_int = 1;
557  py_int = 1;
558  px_ext = 1;
559  py_ext = 1;
560 
561  displayObject = false;
562  displayDesiredObject = false;
563  displayCamera = false;
564 
565  cameraFactor = 1.0;
566 
568 
569  extCamChanged = false;
570 
571  rotz.buildFrom(0,0,0,0,0,vpMath::rad(180));
572 
573  displayImageSimulator = false;
574  objectImage.clear();
575 }
576 
577 
582 {
583  if(sceneInitialized)
584  {
585  if(displayObject)
586  free_Bound_scene (&(this->scene));
587  if(displayCamera){
588  free_Bound_scene (&(this->camera));
589  }
591  free_Bound_scene (&(this->desiredScene));
592  }
593  close_clipping();
594  close_display ();
595 
596  cameraTrajectory.clear();
597  poseList.clear();
598  fMoList.clear();
599 }
600 
601 
611 void
613 {
614  char name_cam[FILENAME_MAX];
615  char name[FILENAME_MAX];
616 
617  object = obj;
618  this->desiredObject = desiredObject;
619 
620  strcpy(name_cam, scene_dir.c_str());
621  if (desiredObject != D_TOOL)
622  {
623  strcat(name_cam,"/camera.bnd");
624  set_scene(name_cam,&camera,cameraFactor);
625  }
626  else
627  {
628  strcat(name_cam,"/tool.bnd");
629  set_scene(name_cam,&(this->camera),1.0);
630  }
631 
632  strcpy(name, scene_dir.c_str());
633  switch (obj)
634  {
635  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
636  case CUBE : { strcat(name, "/cube.bnd"); break; }
637  case PLATE : { strcat(name, "/plate.bnd"); break; }
638  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
639  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
640  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
641  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
642  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
643  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
644  case ROAD : { strcat(name, "/road.bnd"); break; }
645  case TIRE : { strcat(name, "/circles2.bnd"); break; }
646  case PIPE : { strcat(name, "/pipe.bnd"); break; }
647  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
648  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
649  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
650  case PLAN: { strcat(name, "/plan.bnd"); break; }
651  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
652  }
653  set_scene(name,&(this->scene),1.0);
654 
655  switch (desiredObject)
656  {
657  case D_STANDARD : { break; }
658  case D_CIRCLE : {
659  strcpy(name, scene_dir.c_str());
660  strcat(name, "/cercle_sq2.bnd");
661  break; }
662  case D_TOOL : {
663  strcpy(name, scene_dir.c_str());
664  strcat(name, "/tool.bnd");
665  break; }
666  }
667  set_scene(name,&(this->desiredScene),1.0);
668 
669  if (obj == PIPE) load_rfstack(IS_INSIDE);
670  else add_rfstack(IS_BACK);
671 
672  add_vwstack ("start","depth", 0.0, 100.0);
673  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
674  add_vwstack ("start","type", PERSPECTIVE);
675 
676  sceneInitialized = true;
677  displayObject = true;
678  displayDesiredObject = true;
679  displayCamera = true;
680  displayImageSimulator = true;
681 }
682 
683 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
684 
700 void
702 {
703  initScene(obj, desiredObject);
704  objectImage.clear();
705  for(imObj.front(); !imObj.outside(); imObj.next()){
706  objectImage.push_back(imObj.value());
707  }
708  displayImageSimulator = true;
709 }
710 #endif
711 
724 void
725 vpWireFrameSimulator::initScene(vpSceneObject obj, vpSceneDesiredObject desiredObject, const std::list<vpImageSimulator> &imObj)
726 {
727  initScene(obj, desiredObject);
728  objectImage = imObj;
729  displayImageSimulator = true;
730 }
731 
732 
742 void
743 vpWireFrameSimulator::initScene(const char* obj, const char* desiredObject)
744 {
745  char name_cam[FILENAME_MAX];
746  char name[FILENAME_MAX];
747 
748  object = THREE_PTS;
749  this->desiredObject = D_STANDARD;
750 
751  strcpy(name_cam, scene_dir.c_str());
752  strcat(name_cam,"/camera.bnd");
753  set_scene(name_cam,&camera,cameraFactor);
754 
755  strcpy(name,obj);
756  Model_3D model;
757  model = getExtension(obj);
758  if (model == BND_MODEL)
759  set_scene(name,&(this->scene),1.0);
760  else if (model == WRL_MODEL)
761  set_scene_wrl(name,&(this->scene),1.0);
762  else if (model == UNKNOWN_MODEL)
763  {
764  vpERROR_TRACE("Unknown file extension for the 3D model");
765  }
766 
767  strcpy(name,desiredObject);
768  model = getExtension(desiredObject);
769  if (model == BND_MODEL)
770  set_scene(name,&(this->desiredScene),1.0);
771  else if (model == WRL_MODEL)
772  set_scene_wrl(name,&(this->desiredScene),1.0);
773  else if (model == UNKNOWN_MODEL)
774  {
775  vpERROR_TRACE("Unknown file extension for the 3D model");
776  }
777 
778  add_rfstack(IS_BACK);
779 
780  add_vwstack ("start","depth", 0.0, 100.0);
781  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
782  add_vwstack ("start","type", PERSPECTIVE);
783 
784  sceneInitialized = true;
785  displayObject = true;
786  displayDesiredObject = true;
787  displayCamera = true;
788 }
789 
790 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
791 
806 void
807 vpWireFrameSimulator::initScene(const char* obj, const char* desiredObject, vpList<vpImageSimulator> &imObj)
808 {
809  initScene(obj, desiredObject);
810  objectImage.clear();
811  for(imObj.front(); !imObj.outside(); imObj.next()){
812  objectImage.push_back(imObj.value());
813  }
814  displayImageSimulator = true;
815 }
816 #endif
817 
830 void
831 vpWireFrameSimulator::initScene(const char* obj, const char* desiredObject, const std::list<vpImageSimulator> &imObj)
832 {
833  initScene(obj, desiredObject);
834  objectImage = imObj;
835  displayImageSimulator = true;
836 }
837 
846 void
848 {
849  char name_cam[FILENAME_MAX];
850  char name[FILENAME_MAX];
851 
852  object = obj;
853 
854  strcpy(name_cam, scene_dir.c_str());
855 
856  strcpy(name, scene_dir.c_str());
857  switch (obj)
858  {
859  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
860  case CUBE : { strcat(name, "/cube.bnd"); break; }
861  case PLATE : { strcat(name, "/plate.bnd"); break; }
862  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
863  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
864  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
865  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
866  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
867  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
868  case ROAD : { strcat(name, "/road.bnd"); break; }
869  case TIRE : { strcat(name, "/circles2.bnd"); break; }
870  case PIPE : { strcat(name, "/pipe.bnd"); break; }
871  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
872  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
873  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
874  case PLAN: { strcat(name, "/plan.bnd"); break; }
875  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
876  }
877  set_scene(name,&(this->scene),1.0);
878 
879  if (obj == PIPE) load_rfstack(IS_INSIDE);
880  else add_rfstack(IS_BACK);
881 
882  add_vwstack ("start","depth", 0.0, 100.0);
883  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
884  add_vwstack ("start","type", PERSPECTIVE);
885 
886  sceneInitialized = true;
887  displayObject = true;
888  displayCamera = true;
889 }
890 
891 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
892 
906 void
908 {
909  initScene(obj);
910  objectImage.clear();
911  for(imObj.front(); !imObj.outside(); imObj.next()){
912  objectImage.push_back(imObj.value());
913  }
914  displayImageSimulator = true;
915 }
916 #endif
917 
929 void
930 vpWireFrameSimulator::initScene(vpSceneObject obj, const std::list<vpImageSimulator> &imObj)
931 {
932  initScene(obj);
933  objectImage = imObj;
934  displayImageSimulator = true;
935 }
936 
945 void
947 {
948  char name_cam[FILENAME_MAX];
949  char name[FILENAME_MAX];
950 
951  object = THREE_PTS;
952 
953  strcpy(name_cam, scene_dir.c_str());
954  strcat(name_cam,"/camera.bnd");
955  set_scene(name_cam,&camera,cameraFactor);
956 
957  strcpy(name,obj);
958  Model_3D model;
959  model = getExtension(obj);
960  if (model == BND_MODEL)
961  set_scene(name,&(this->scene),1.0);
962  else if (model == WRL_MODEL)
963  set_scene_wrl(name,&(this->scene),1.0);
964  else if (model == UNKNOWN_MODEL)
965  {
966  vpERROR_TRACE("Unknown file extension for the 3D model");
967  }
968 
969  add_rfstack(IS_BACK);
970 
971  add_vwstack ("start","depth", 0.0, 100.0);
972  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
973  add_vwstack ("start","type", PERSPECTIVE);
974 
975  sceneInitialized = true;
976  displayObject = true;
977  displayCamera = true;
978 }
979 
980 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
981 
995 void
997 {
998  initScene(obj);
999  objectImage.clear();
1000  for(imObj.front(); !imObj.outside(); imObj.next()){
1001  objectImage.push_back(imObj.value());
1002  }
1003  displayImageSimulator = true;
1004 }
1005 #endif
1006 
1018 void
1019 vpWireFrameSimulator::initScene(const char* obj, const std::list<vpImageSimulator> &imObj)
1020 {
1021  initScene(obj);
1022  objectImage = imObj;
1023  displayImageSimulator = true;
1024 }
1025 
1033 void
1035 {
1036  if (!sceneInitialized)
1037  throw(vpException(vpSimulatorException::notInitializedError,"The scene has to be initialized")) ;
1038 
1039  double u;
1040  double v;
1041  //if(px_int != 1 && py_int != 1)
1042  // we assume px_int and py_int > 0
1043  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1044  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1045  {
1046  u = (double)I.getWidth()/(2*px_int);
1047  v = (double)I.getHeight()/(2*py_int);
1048  }
1049  else
1050  {
1051  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1052  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1053  }
1054 
1055  float o44c[4][4],o44cd[4][4],x,y,z;
1056  Matrix id = IDENTITY_MATRIX;
1057 
1058  vp2jlc_matrix(cMo.inverse(),o44c);
1059  vp2jlc_matrix(cdMo.inverse(),o44cd);
1060 
1062  {
1063  I = 255;
1064 
1065  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1066  vpImageSimulator* imSim = &(*it);
1067  imSim->setCameraPosition(rotz*cMo);
1069  }
1070 
1071  if (I.display != NULL)
1072  vpDisplay::display(I);
1073  }
1074 
1075  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1076  x = o44c[2][0] + o44c[3][0];
1077  y = o44c[2][1] + o44c[3][1];
1078  z = o44c[2][2] + o44c[3][2];
1079  add_vwstack ("start","vrp", x,y,z);
1080  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1081  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1082  add_vwstack ("start","window", -u, u, -v, v);
1083  if (displayObject)
1084  display_scene(id,this->scene,I, curColor);
1085 
1086 
1087  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1088  x = o44cd[2][0] + o44cd[3][0];
1089  y = o44cd[2][1] + o44cd[3][1];
1090  z = o44cd[2][2] + o44cd[3][2];
1091  add_vwstack ("start","vrp", x,y,z);
1092  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1093  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1094  add_vwstack ("start","window", -u, u, -v, v);
1096  {
1098  else display_scene(id,desiredScene,I, desColor);
1099  }
1100 }
1101 
1102 
1111 void
1113 {
1114  bool changed = false;
1115  vpHomogeneousMatrix displacement = navigation(I,changed);
1116 
1117  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1118  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1119  camMf2 = camMf2*displacement;
1120 
1121  f2Mf = camMf2.inverse()*camMf;
1122 
1123  camMf = camMf2* displacement * f2Mf;
1124 
1125  double u;
1126  double v;
1127  //if(px_ext != 1 && py_ext != 1)
1128  // we assume px_ext and py_ext > 0
1129  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1130  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1131  {
1132  u = (double)I.getWidth()/(2*px_ext);
1133  v = (double)I.getHeight()/(2*py_ext);
1134  }
1135  else
1136  {
1137  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1138  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1139  }
1140 
1141  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1142 
1143  vp2jlc_matrix(camMf.inverse(),w44cext);
1144  vp2jlc_matrix(fMc,w44c);
1145  vp2jlc_matrix(fMo,w44o);
1146 
1147 
1148  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1149  x = w44cext[2][0] + w44cext[3][0];
1150  y = w44cext[2][1] + w44cext[3][1];
1151  z = w44cext[2][2] + w44cext[3][2];
1152  add_vwstack ("start","vrp", x,y,z);
1153  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1154  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1155  add_vwstack ("start","window", -u, u, -v, v);
1156  if ((object == CUBE) || (object == SPHERE))
1157  {
1158  add_vwstack ("start","type", PERSPECTIVE);
1159  }
1160 
1162  {
1163  I = 255;
1164 
1165  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1166  vpImageSimulator* imSim = &(*it);
1167  imSim->setCameraPosition(rotz*camMf*fMo);
1169  }
1170 
1171  if (I.display != NULL)
1172  vpDisplay::display(I);
1173  }
1174 
1175  if (displayObject)
1176  display_scene(w44o,this->scene,I, curColor);
1177 
1178  if (displayCamera)
1179  display_scene(w44c,camera, I, camColor);
1180 
1182  {
1183  vpImagePoint iP;
1184  vpImagePoint iP_1;
1185  poseList.push_back(cMo);
1186  fMoList.push_back(fMo);
1187 
1188  int iter = 0;
1189 
1190  if (changed || extCamChanged)
1191  {
1192  cameraTrajectory.clear();
1193  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1194  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1195 
1196  while ((iter_poseList != poseList.end()) && (iter_fMoList != fMoList.end()))
1197  {
1198  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1199  cameraTrajectory.push_back(iP);
1200  if (camTrajType == CT_LINE)
1201  {
1202  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1203  }
1204  else if (camTrajType == CT_POINT)
1206  ++iter_poseList;
1207  ++iter_fMoList;
1208  iter++;
1209  iP_1 = iP;
1210  }
1211  extCamChanged = false;
1212  }
1213  else
1214  {
1215  iP = projectCameraTrajectory(I, cMo, fMo);
1216  cameraTrajectory.push_back(iP);
1217 
1218  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1219  if (camTrajType == CT_LINE)
1220  {
1221  if (iter != 0) vpDisplay::displayLine(I, iP_1, *it, camTrajColor);
1222  }
1223  else if (camTrajType == CT_POINT)
1225  iter++;
1226  iP_1 = *it;
1227  }
1228  }
1229 
1230  if (poseList.size() > nbrPtLimit)
1231  {
1232  poseList.pop_front();
1233  }
1234  if (fMoList.size() > nbrPtLimit)
1235  {
1236  fMoList.pop_front();
1237  }
1238  if (cameraTrajectory.size() > nbrPtLimit)
1239  {
1240  cameraTrajectory.pop_front();
1241  }
1242  }
1243 }
1244 
1245 
1254 void
1256 {
1257  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1258 
1259  vpHomogeneousMatrix camMft = rotz * camMf;
1260 
1261  double u;
1262  double v;
1263  //if(px_ext != 1 && py_ext != 1)
1264  // we assume px_ext and py_ext > 0
1265  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1266  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1267  {
1268  u = (double)I.getWidth()/(2*px_ext);
1269  v = (double)I.getHeight()/(2*py_ext);
1270  }
1271  else
1272  {
1273  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1274  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1275  }
1276 
1277  vp2jlc_matrix(camMft.inverse(),w44cext);
1278  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1279  vp2jlc_matrix(fMo,w44o);
1280 
1281  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1282  x = w44cext[2][0] + w44cext[3][0];
1283  y = w44cext[2][1] + w44cext[3][1];
1284  z = w44cext[2][2] + w44cext[3][2];
1285  add_vwstack ("start","vrp", x,y,z);
1286  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1287  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1288  add_vwstack ("start","window", -u, u, -v, v);
1289 
1291  {
1292  I = 255;
1293 
1294  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1295  vpImageSimulator* imSim = &(*it);
1296  imSim->setCameraPosition(rotz*camMf*fMo);
1298  }
1299 
1300  if (I.display != NULL)
1301  vpDisplay::display(I);
1302  }
1303 
1304  if (displayObject)
1305  display_scene(w44o,this->scene,I, curColor);
1306  if (displayCamera)
1307  display_scene(w44c,camera, I, camColor);
1308 }
1309 
1310 
1318 void
1320 {
1321  if (!sceneInitialized)
1322  throw(vpException(vpSimulatorException::notInitializedError,"The scene has to be initialized")) ;
1323 
1324  double u;
1325  double v;
1326  //if(px_int != 1 && py_int != 1)
1327  // we assume px_int and py_int > 0
1328  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1329  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1330  {
1331  u = (double)I.getWidth()/(2*px_int);
1332  v = (double)I.getHeight()/(2*py_int);
1333  }
1334  else
1335  {
1336  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1337  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1338  }
1339 
1340  float o44c[4][4],o44cd[4][4],x,y,z;
1341  Matrix id = IDENTITY_MATRIX;
1342 
1343  vp2jlc_matrix(cMo.inverse(),o44c);
1344  vp2jlc_matrix(cdMo.inverse(),o44cd);
1345 
1347  {
1348  I = 255;
1349 
1350  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1351  vpImageSimulator* imSim = &(*it);
1352  imSim->setCameraPosition(rotz*camMf*fMo);
1354  }
1355 
1356  if (I.display != NULL)
1357  vpDisplay::display(I);
1358  }
1359 
1360  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1361  x = o44c[2][0] + o44c[3][0];
1362  y = o44c[2][1] + o44c[3][1];
1363  z = o44c[2][2] + o44c[3][2];
1364  add_vwstack ("start","vrp", x,y,z);
1365  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1366  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1367  add_vwstack ("start","window", -u, u, -v, v);
1368  if (displayObject)
1369  display_scene(id,this->scene,I, curColor);
1370 
1371 
1372  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1373  x = o44cd[2][0] + o44cd[3][0];
1374  y = o44cd[2][1] + o44cd[3][1];
1375  z = o44cd[2][2] + o44cd[3][2];
1376  add_vwstack ("start","vrp", x,y,z);
1377  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1378  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1379  add_vwstack ("start","window", -u, u, -v, v);
1381  {
1383  else display_scene(id,desiredScene,I, desColor);
1384  }
1385 }
1386 
1387 
1396 void
1398 {
1399  bool changed = false;
1400  vpHomogeneousMatrix displacement = navigation(I,changed);
1401 
1402  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1403  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1404  camMf2 = camMf2*displacement;
1405 
1406  f2Mf = camMf2.inverse()*camMf;
1407 
1408  camMf = camMf2* displacement * f2Mf;
1409 
1410  double u;
1411  double v;
1412  //if(px_ext != 1 && py_ext != 1)
1413  // we assume px_ext and py_ext > 0
1414  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1415  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1416  {
1417  u = (double)I.getWidth()/(2*px_ext);
1418  v = (double)I.getHeight()/(2*py_ext);
1419  }
1420  else
1421  {
1422  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1423  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1424  }
1425 
1426  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1427 
1428  vp2jlc_matrix(camMf.inverse(),w44cext);
1429  vp2jlc_matrix(fMc,w44c);
1430  vp2jlc_matrix(fMo,w44o);
1431 
1432 
1433  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1434  x = w44cext[2][0] + w44cext[3][0];
1435  y = w44cext[2][1] + w44cext[3][1];
1436  z = w44cext[2][2] + w44cext[3][2];
1437  add_vwstack ("start","vrp", x,y,z);
1438  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1439  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1440  add_vwstack ("start","window", -u, u, -v, v);
1441  if ((object == CUBE) || (object == SPHERE))
1442  {
1443  add_vwstack ("start","type", PERSPECTIVE);
1444  }
1445 
1447  {
1448  I = 255;
1449  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1450  vpImageSimulator* imSim = &(*it);
1451  imSim->setCameraPosition(rotz*camMf*fMo);
1453  }
1454  if (I.display != NULL)
1455  vpDisplay::display(I);
1456  }
1457 
1458  if (displayObject)
1459  display_scene(w44o,this->scene,I, curColor);
1460 
1461  if (displayCamera)
1462  display_scene(w44c,camera, I, camColor);
1463 
1465  {
1466  vpImagePoint iP;
1467  vpImagePoint iP_1;
1468  poseList.push_back(cMo);
1469  fMoList.push_back(fMo);
1470 
1471  int iter = 0;
1472 
1473  if (changed || extCamChanged)
1474  {
1475  cameraTrajectory.clear();
1476  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1477  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1478 
1479  while ((iter_poseList!=poseList.end()) && (iter_fMoList!=fMoList.end()) )
1480  {
1481  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1482  cameraTrajectory.push_back(iP);
1483  //vpDisplay::displayPoint(I,cameraTrajectory.value(),vpColor::green);
1484  if (camTrajType == CT_LINE)
1485  {
1486  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1487  }
1488  else if (camTrajType == CT_POINT)
1490  ++iter_poseList;
1491  ++iter_fMoList;
1492  iter++;
1493  iP_1 = iP;
1494  }
1495  extCamChanged = false;
1496  }
1497  else
1498  {
1499  iP = projectCameraTrajectory(I, cMo,fMo);
1500  cameraTrajectory.push_back(iP);
1501 
1502  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1503  if (camTrajType == CT_LINE){
1504  if (iter != 0) vpDisplay::displayLine(I,iP_1,*it,camTrajColor);
1505  }
1506  else if(camTrajType == CT_POINT)
1508  iter++;
1509  iP_1 = *it;
1510  }
1511  }
1512 
1513  if (poseList.size() > nbrPtLimit)
1514  {
1515  poseList.pop_front();
1516  }
1517  if (fMoList.size() > nbrPtLimit)
1518  {
1519  fMoList.pop_front();
1520  }
1521  if (cameraTrajectory.size() > nbrPtLimit)
1522  {
1523  cameraTrajectory.pop_front();
1524  }
1525  }
1526 }
1527 
1528 
1537 void
1539 {
1540  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1541 
1542  vpHomogeneousMatrix camMft = rotz * camMf;
1543 
1544  double u;
1545  double v;
1546  //if(px_ext != 1 && py_ext != 1)
1547  // we assume px_ext and py_ext > 0
1548  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1549  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1550  {
1551  u = (double)I.getWidth()/(2*px_ext);
1552  v = (double)I.getHeight()/(2*py_ext);
1553  }
1554  else
1555  {
1556  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1557  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1558  }
1559 
1560  vp2jlc_matrix(camMft.inverse(),w44cext);
1561  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1562  vp2jlc_matrix(fMo,w44o);
1563 
1564  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1565  x = w44cext[2][0] + w44cext[3][0];
1566  y = w44cext[2][1] + w44cext[3][1];
1567  z = w44cext[2][2] + w44cext[3][2];
1568  add_vwstack ("start","vrp", x,y,z);
1569  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1570  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1571  add_vwstack ("start","window", -u, u, -v, v);
1572 
1574  {
1575  I = 255;
1576  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1577  vpImageSimulator* imSim = &(*it);
1578  imSim->setCameraPosition(rotz*camMf*fMo);
1580  }
1581  if (I.display != NULL)
1582  vpDisplay::display(I);
1583  }
1584 
1585  if (displayObject)
1586  display_scene(w44o,this->scene,I, curColor);
1587  if (displayCamera)
1588  display_scene(w44c,camera, I, camColor);
1589 }
1590 
1591 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
1592 
1606 void
1608 {
1609  if (list_cMo.nbElements() != list_fMo.nbElements())
1610  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1611 
1612  list_cMo.front();
1613  list_fMo.front();
1614  vpImagePoint iP;
1615  vpImagePoint iP_1;
1616  int iter = 0;
1617 
1618  while (!list_cMo.outside() && !list_fMo.outside())
1619  {
1620  iP = projectCameraTrajectory(I, rotz * list_cMo.value(), list_fMo.value(), rotz * cMf);
1621  if (camTrajType == CT_LINE)
1622  {
1623  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1624  }
1625  else if (camTrajType == CT_POINT)
1627  list_cMo.next();
1628  list_fMo.next();
1629  iter++;
1630  iP_1 = iP;
1631  }
1632 }
1633 
1648 void
1650 {
1651  if (list_cMo.nbElements() != list_fMo.nbElements())
1652  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1653 
1654  list_cMo.front();
1655  list_fMo.front();
1656  vpImagePoint iP;
1657  vpImagePoint iP_1;
1658  int iter = 0;
1659 
1660  while (!list_cMo.outside() && !list_fMo.outside())
1661  {
1662  iP = projectCameraTrajectory(I, rotz * list_cMo.value(), list_fMo.value(), rotz * cMf);
1663  if (camTrajType == CT_LINE)
1664  {
1665  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1666  }
1667  else if (camTrajType == CT_POINT)
1669  list_cMo.next();
1670  list_fMo.next();
1671  iter++;
1672  iP_1 = iP;
1673  }
1674 }
1675 #endif
1676 
1677 
1688 void
1689 vpWireFrameSimulator::displayTrajectory (vpImage<unsigned char> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1690  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1691 {
1692  if (list_cMo.size() != list_fMo.size())
1693  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1694 
1695  vpImagePoint iP;
1696  vpImagePoint iP_1;
1697  int iter = 0;
1698 
1699  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1700  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1701 
1702  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1703  {
1704  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1705  if (camTrajType == CT_LINE)
1706  {
1707  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1708  }
1709  else if (camTrajType == CT_POINT)
1711  ++it_cMo;
1712  ++it_fMo;
1713  iter++;
1714  iP_1 = iP;
1715  }
1716 }
1717 
1728 void
1729 vpWireFrameSimulator::displayTrajectory (vpImage<vpRGBa> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1730  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1731 {
1732  if (list_cMo.size() != list_fMo.size())
1733  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1734 
1735  vpImagePoint iP;
1736  vpImagePoint iP_1;
1737  int iter = 0;
1738 
1739  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1740  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1741 
1742  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1743  {
1744  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1745  if (camTrajType == CT_LINE)
1746  {
1747  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1748  }
1749  else if (camTrajType == CT_POINT)
1751  ++it_cMo;
1752  ++it_fMo;
1753  iter++;
1754  iP_1 = iP;
1755  }
1756 }
1757 
1758 
1764 {
1765  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1766  vpImagePoint iP;
1767  vpImagePoint trash;
1768  bool clicked = false;
1769  bool clickedUp = false;
1771 
1772  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1773  changed = false;
1774 
1775  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1776 
1777  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1778 
1779  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1780 
1781  if(clicked)
1782  {
1783  if (b == vpMouseButton::button1) blockedr = true;
1784  if (b == vpMouseButton::button2) blockedz = true;
1785  if (b == vpMouseButton::button3) blockedt = true;
1786  blocked = true;
1787  }
1788  if(clickedUp)
1789  {
1790  if (b == vpMouseButton::button1)
1791  {
1792  old_iPr = vpImagePoint(-1,-1);
1793  blockedr = false;
1794  }
1795  if (b == vpMouseButton::button2)
1796  {
1797  old_iPz = vpImagePoint(-1,-1);
1798  blockedz = false;
1799  }
1800  if (b == vpMouseButton::button3)
1801  {
1802  old_iPt = vpImagePoint(-1,-1);
1803  blockedt = false;
1804  }
1805  if (!(blockedr || blockedz || blockedt))
1806  {
1807  blocked = false;
1808  while (vpDisplay::getClick(I,trash,b,false)) {};
1809  }
1810  }
1811 
1813 
1814  double anglei = 0;
1815  double anglej = 0;
1816 
1817  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1818  {
1819  double diffi = iP.get_i() - old_iPr.get_i();
1820  double diffj = iP.get_j() - old_iPr.get_j();
1821  //cout << "delta :" << diffj << endl;;
1822  anglei = diffi*360/width;
1823  anglej = diffj*360/width;
1824  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1825  changed = true;
1826  }
1827 
1828  if (blockedr) old_iPr = iP;
1829 
1830  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1831  {
1832  double diffi = iP.get_i() - old_iPz.get_i();
1833  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1834  changed = true;
1835  }
1836 
1837  if (blockedz) old_iPz = iP;
1838 
1839  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1840  {
1841  double diffi = iP.get_i() - old_iPt.get_i();
1842  double diffj = iP.get_j() - old_iPt.get_j();
1843  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1844  changed = true;
1845  }
1846 
1847  if (blockedt) old_iPt = iP;
1848 
1849  return mov;
1850 }
1851 
1852 
1858 {
1859  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1860  vpImagePoint iP;
1861  vpImagePoint trash;
1862  bool clicked = false;
1863  bool clickedUp = false;
1865 
1866  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1867  changed = false;
1868 
1869  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1870 
1871  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1872 
1873  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1874 
1875  if(clicked)
1876  {
1877  if (b == vpMouseButton::button1) blockedr = true;
1878  if (b == vpMouseButton::button2) blockedz = true;
1879  if (b == vpMouseButton::button3) blockedt = true;
1880  blocked = true;
1881  }
1882  if(clickedUp)
1883  {
1884  if (b == vpMouseButton::button1)
1885  {
1886  old_iPr = vpImagePoint(-1,-1);
1887  blockedr = false;
1888  }
1889  if (b == vpMouseButton::button2)
1890  {
1891  old_iPz = vpImagePoint(-1,-1);
1892  blockedz = false;
1893  }
1894  if (b == vpMouseButton::button3)
1895  {
1896  old_iPt = vpImagePoint(-1,-1);
1897  blockedt = false;
1898  }
1899  if (!(blockedr || blockedz || blockedt))
1900  {
1901  blocked = false;
1902  while (vpDisplay::getClick(I,trash,b,false)) {};
1903  }
1904  }
1905 
1907 
1908  //std::cout << "point : " << iP << std::endl;
1909 
1910  double anglei = 0;
1911  double anglej = 0;
1912 
1913  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1914  {
1915  double diffi = iP.get_i() - old_iPr.get_i();
1916  double diffj = iP.get_j() - old_iPr.get_j();
1917  //cout << "delta :" << diffj << endl;;
1918  anglei = diffi*360/width;
1919  anglej = diffj*360/width;
1920  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1921  changed = true;
1922  }
1923 
1924  if (blockedr) old_iPr = iP;
1925 
1926  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1927  {
1928  double diffi = iP.get_i() - old_iPz.get_i();
1929  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1930  changed = true;
1931  }
1932 
1933  if (blockedz) old_iPz = iP;
1934 
1935  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1936  {
1937  double diffi = iP.get_i() - old_iPt.get_i();
1938  double diffj = iP.get_j() - old_iPt.get_j();
1939  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1940  changed = true;
1941  }
1942 
1943  if (blockedt) old_iPt = iP;
1944 
1945  return mov;
1946 }
1947 
1953 {
1954  vpPoint point;
1955  point.setWorldCoordinates(0,0,0);
1956 
1957  point.track(rotz*(camMf*fMo*cMo.inverse())) ;
1958 
1959  vpImagePoint iP;
1960 
1962 
1963  return iP;
1964 }
1965 
1971 {
1972  vpPoint point;
1973  point.setWorldCoordinates(0,0,0);
1974 
1975  point.track(rotz*(camMf*fMo*cMo.inverse())) ;
1976 
1977  vpImagePoint iP;
1978 
1980 
1981  return iP;
1982 }
1983 
1989 {
1990  vpPoint point;
1991  point.setWorldCoordinates(0,0,0);
1992 
1993  point.track(rotz*(cMf*fMo*cMo.inverse())) ;
1994 
1995  vpImagePoint iP;
1996 
1998 
1999  return iP;
2000 }
2001 
2007 {
2008  vpPoint point;
2009  point.setWorldCoordinates(0,0,0);
2010 
2011  point.track(rotz*(cMf*fMo*cMo.inverse())) ;
2012 
2013  vpImagePoint iP;
2014 
2016 
2017  return iP;
2018 }
2019 
vpDisplay * display
Definition: vpImage.h:116
unsigned int nbElements(void)
return the number of element in the list
Definition: vpList.h:261
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:289
vpCameraParameters getInternalCameraParameters(const vpImage< vpRGBa > &I) const
double get_i() const
Definition: vpImagePoint.h:181
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
Definition: vpList.h:431
unsigned int getWidth() const
Definition: vpImage.h:154
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:379
Provide simple list management.
Definition: vpList.h:112
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
void setIdentity()
Basic initialisation (identity)
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:123
vpHomogeneousMatrix navigation(vpImage< vpRGBa > &I, bool &changed)
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:204
virtual bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)=0
std::list< vpImageSimulator > objectImage
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:145
static const vpColor green
Definition: vpColor.h:168
vpHomogeneousMatrix fMo
double get_j() const
Definition: vpImagePoint.h:192
std::list< vpHomogeneousMatrix > fMoList
void next(void)
position the current element on the next one
Definition: vpList.h:275
static const vpColor red
Definition: vpColor.h:165
Class that defines what is a point.
Definition: vpPoint.h:65
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
virtual bool getPointerPosition(vpImagePoint &ip)=0
vpHomogeneousMatrix f2Mf
void getInternalImage(vpImage< vpRGBa > &I)
void front(void)
Position the current element on the first element of the list.
Definition: vpList.h:386
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
type & value(void)
return the value of the current element
Definition: vpList.h:303
Class which enables to project an image in the 3D space and get the view of a virtual camera...
vpHomogeneousMatrix cMo
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.h:143
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:148
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
static double rad(double deg)
Definition: vpMath.h:100
vpCameraTrajectoryDisplayType camTrajType
void display_scene(Matrix mat, Bound_scene &sc, vpImage< vpRGBa > &I, vpColor color)
vpCameraParameters getExternalCameraParameters(const vpImage< vpRGBa > &I) const
vpSceneDesiredObject desiredObject
vpImagePoint projectCameraTrajectory(vpImage< vpRGBa > &I, vpHomogeneousMatrix cMo, vpHomogeneousMatrix fMo)
vpHomogeneousMatrix camMf
vpHomogeneousMatrix fMc
vpHomogeneousMatrix inverse() const
void initScene(vpSceneObject obj, vpSceneDesiredObject desiredObject)
vpHomogeneousMatrix rotz
unsigned int getHeight() const
Definition: vpImage.h:145
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
void setCameraPosition(const vpHomogeneousMatrix &_cMt)
void getExternalImage(vpImage< vpRGBa > &I)
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
std::list< vpHomogeneousMatrix > poseList
void displayTrajectory(vpImage< unsigned char > &I, const std::list< vpHomogeneousMatrix > &list_cMo, const std::list< vpHomogeneousMatrix > &list_fMo, const vpHomogeneousMatrix &camMf)
vpHomogeneousMatrix camMf2
static const vpColor blue
Definition: vpColor.h:171
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