ViSP  2.7.0
vpWireFrameSimulator.cpp
1 /****************************************************************************
2  *
3  * $Id: vpWireFrameSimulator.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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, const vpImage<vpRGBa> &I, const 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  //bcopy ((char *) mat, (char *) m, sizeof (Matrix));
417  memmove((char *) m, (char *) mat, sizeof (Matrix));
418  View_to_Matrix (get_vwstack (), *(get_tmstack ()));
419  postmult_matrix (m, *(get_tmstack ()));
420  bp = sc.bound.ptr;
421  bend = bp + sc.bound.nbr;
422  for (; bp < bend; bp++)
423  {
424  if ((clip = clipping_Bound (bp, m)) != NULL)
425  {
426  Face *fp = clip->face.ptr;
427  Face *fend = fp + clip->face.nbr;
428 
429  set_Bound_face_display (clip, b); //regarde si is_visible
430 
431  point_3D_2D (clip->point.ptr, clip->point.nbr,I.getWidth(),I.getHeight(),point2i);
432  for (; fp < fend; fp++)
433  {
434  if (fp->is_visible)
435  {
436  wireframe_Face (fp, point2i);
437  Point2i *pt = listpoint2i;
438  for (int i = 1; i < fp->vertex.nbr; i++)
439  {
440  vpDisplay::displayLine(I,vpImagePoint((pt)->y,(pt)->x),vpImagePoint((pt+1)->y,(pt+1)->x),color,1);
441  pt++;
442  }
443  if (fp->vertex.nbr > 2)
444  {
445  vpDisplay::displayLine(I,vpImagePoint((listpoint2i)->y,(listpoint2i)->x),vpImagePoint((pt)->y,(pt)->x),color,1);
446  }
447  }
448  }
449  }
450  }
451 }
452 
453 /*
454  Copy the scene corresponding to the registeresd parameters in the image.
455 */
456 void
457 vpWireFrameSimulator::display_scene(Matrix mat, Bound_scene &sc, const vpImage<unsigned char> &I, const vpColor &color)
458 {
459  //extern Bound *clipping_Bound ();
460 
461  Bound *bp, *bend;
462  Bound *clip; /* surface apres clipping */
463  Byte b = (Byte) *get_rfstack ();
464  Matrix m;
465 
466  bcopy ((char *) mat, (char *) m, 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,1);
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,1);
495  }
496  }
497  }
498  }
499  }
500 }
501 
502 /*************************************************************************************************************/
503 
513 {
514  // set scene_dir from #define VISP_SCENE_DIR if it exists
515  if (vpIoTools::checkDirectory(VISP_SCENES_DIR) == true) // directory exists
516  scene_dir = VISP_SCENES_DIR;
517  else {
518  try {
519  scene_dir = vpIoTools::getenv("VISP_SCENES_DIR");
520  std::cout << "The simulator uses data from VISP_SCENES_DIR=" << scene_dir << std::endl;
521  }
522  catch (...) {
523  std::cout << "Cannot get VISP_SCENES_DIR environment variable" << std::endl;
524  }
525  }
526 
527  open_display();
528  open_clipping();
529 
534 
535  sceneInitialized = false;
536 
538  cameraTrajectory.clear();
539  poseList.clear();
540  fMoList.clear();
541 
542  fMo.setIdentity();
543 
544  old_iPr = vpImagePoint(-1,-1);
545  old_iPz = vpImagePoint(-1,-1);
546  old_iPt = vpImagePoint(-1,-1);
547  blockedr = false;
548  blockedz = false;
549  blockedt = false;
550  blocked = false;
551 
552  nbrPtLimit = 1000;
553 
554  px_int = 1;
555  py_int = 1;
556  px_ext = 1;
557  py_ext = 1;
558 
559  displayObject = false;
560  displayDesiredObject = false;
561  displayCamera = false;
562 
563  cameraFactor = 1.0;
564 
566 
567  extCamChanged = false;
568 
569  rotz.buildFrom(0,0,0,0,0,vpMath::rad(180));
570 
571  displayImageSimulator = false;
572  objectImage.clear();
573 }
574 
575 
580 {
581  if(sceneInitialized)
582  {
583  if(displayObject)
584  free_Bound_scene (&(this->scene));
585  if(displayCamera){
586  free_Bound_scene (&(this->camera));
587  }
589  free_Bound_scene (&(this->desiredScene));
590  }
591  close_clipping();
592  close_display ();
593 
594  cameraTrajectory.clear();
595  poseList.clear();
596  fMoList.clear();
597 }
598 
599 
609 void
611 {
612  char name_cam[FILENAME_MAX];
613  char name[FILENAME_MAX];
614 
615  object = obj;
616  this->desiredObject = desiredObject;
617 
618  strcpy(name_cam, scene_dir.c_str());
619  if (desiredObject != D_TOOL)
620  {
621  strcat(name_cam,"/camera.bnd");
622  set_scene(name_cam,&camera,cameraFactor);
623  }
624  else
625  {
626  strcat(name_cam,"/tool.bnd");
627  set_scene(name_cam,&(this->camera),1.0);
628  }
629 
630  strcpy(name, scene_dir.c_str());
631  switch (obj)
632  {
633  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
634  case CUBE : { strcat(name, "/cube.bnd"); break; }
635  case PLATE : { strcat(name, "/plate.bnd"); break; }
636  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
637  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
638  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
639  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
640  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
641  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
642  case ROAD : { strcat(name, "/road.bnd"); break; }
643  case TIRE : { strcat(name, "/circles2.bnd"); break; }
644  case PIPE : { strcat(name, "/pipe.bnd"); break; }
645  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
646  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
647  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
648  case PLAN: { strcat(name, "/plan.bnd"); break; }
649  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
650  }
651  set_scene(name,&(this->scene),1.0);
652 
653  switch (desiredObject)
654  {
655  case D_STANDARD : { break; }
656  case D_CIRCLE : {
657  strcpy(name, scene_dir.c_str());
658  strcat(name, "/cercle_sq2.bnd");
659  break; }
660  case D_TOOL : {
661  strcpy(name, scene_dir.c_str());
662  strcat(name, "/tool.bnd");
663  break; }
664  }
665  set_scene(name,&(this->desiredScene),1.0);
666 
667  if (obj == PIPE) load_rfstack(IS_INSIDE);
668  else add_rfstack(IS_BACK);
669 
670  add_vwstack ("start","depth", 0.0, 100.0);
671  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
672  add_vwstack ("start","type", PERSPECTIVE);
673 
674  sceneInitialized = true;
675  displayObject = true;
676  displayDesiredObject = true;
677  displayCamera = true;
678  displayImageSimulator = true;
679 }
680 
681 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
682 
698 void
700 {
701  initScene(obj, desiredObject);
702  objectImage.clear();
703  for(imObj.front(); !imObj.outside(); imObj.next()){
704  objectImage.push_back(imObj.value());
705  }
706  displayImageSimulator = true;
707 }
708 #endif
709 
722 void
723 vpWireFrameSimulator::initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject, const std::list<vpImageSimulator> &imObj)
724 {
725  initScene(obj, desiredObject);
726  objectImage = imObj;
727  displayImageSimulator = true;
728 }
729 
730 
740 void
741 vpWireFrameSimulator::initScene(const char* obj, const char* desiredObject)
742 {
743  char name_cam[FILENAME_MAX];
744  char name[FILENAME_MAX];
745 
746  object = THREE_PTS;
747  this->desiredObject = D_STANDARD;
748 
749  strcpy(name_cam, scene_dir.c_str());
750  strcat(name_cam,"/camera.bnd");
751  set_scene(name_cam,&camera,cameraFactor);
752 
753  strcpy(name,obj);
754  Model_3D model;
755  model = getExtension(obj);
756  if (model == BND_MODEL)
757  set_scene(name,&(this->scene),1.0);
758  else if (model == WRL_MODEL)
759  set_scene_wrl(name,&(this->scene),1.0);
760  else if (model == UNKNOWN_MODEL)
761  {
762  vpERROR_TRACE("Unknown file extension for the 3D model");
763  }
764 
765  strcpy(name,desiredObject);
766  model = getExtension(desiredObject);
767  if (model == BND_MODEL)
768  set_scene(name,&(this->desiredScene),1.0);
769  else if (model == WRL_MODEL)
770  set_scene_wrl(name,&(this->desiredScene),1.0);
771  else if (model == UNKNOWN_MODEL)
772  {
773  vpERROR_TRACE("Unknown file extension for the 3D model");
774  }
775 
776  add_rfstack(IS_BACK);
777 
778  add_vwstack ("start","depth", 0.0, 100.0);
779  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
780  add_vwstack ("start","type", PERSPECTIVE);
781 
782  sceneInitialized = true;
783  displayObject = true;
784  displayDesiredObject = true;
785  displayCamera = true;
786 }
787 
788 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
789 
804 void
805 vpWireFrameSimulator::initScene(const char* obj, const char* desiredObject, vpList<vpImageSimulator> &imObj)
806 {
807  initScene(obj, desiredObject);
808  objectImage.clear();
809  for(imObj.front(); !imObj.outside(); imObj.next()){
810  objectImage.push_back(imObj.value());
811  }
812  displayImageSimulator = true;
813 }
814 #endif
815 
828 void
829 vpWireFrameSimulator::initScene(const char* obj, const char* desiredObject, const std::list<vpImageSimulator> &imObj)
830 {
831  initScene(obj, desiredObject);
832  objectImage = imObj;
833  displayImageSimulator = true;
834 }
835 
844 void
846 {
847  char name_cam[FILENAME_MAX];
848  char name[FILENAME_MAX];
849 
850  object = obj;
851 
852  strcpy(name_cam, scene_dir.c_str());
853 
854  strcpy(name, scene_dir.c_str());
855  switch (obj)
856  {
857  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
858  case CUBE : { strcat(name, "/cube.bnd"); break; }
859  case PLATE : { strcat(name, "/plate.bnd"); break; }
860  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
861  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
862  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
863  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
864  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
865  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
866  case ROAD : { strcat(name, "/road.bnd"); break; }
867  case TIRE : { strcat(name, "/circles2.bnd"); break; }
868  case PIPE : { strcat(name, "/pipe.bnd"); break; }
869  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
870  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
871  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
872  case PLAN: { strcat(name, "/plan.bnd"); break; }
873  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
874  }
875  set_scene(name,&(this->scene),1.0);
876 
877  if (obj == PIPE) load_rfstack(IS_INSIDE);
878  else add_rfstack(IS_BACK);
879 
880  add_vwstack ("start","depth", 0.0, 100.0);
881  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
882  add_vwstack ("start","type", PERSPECTIVE);
883 
884  sceneInitialized = true;
885  displayObject = true;
886  displayCamera = true;
887 }
888 
889 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
890 
904 void
906 {
907  initScene(obj);
908  objectImage.clear();
909  for(imObj.front(); !imObj.outside(); imObj.next()){
910  objectImage.push_back(imObj.value());
911  }
912  displayImageSimulator = true;
913 }
914 #endif
915 
927 void
928 vpWireFrameSimulator::initScene(const vpSceneObject &obj, const std::list<vpImageSimulator> &imObj)
929 {
930  initScene(obj);
931  objectImage = imObj;
932  displayImageSimulator = true;
933 }
934 
943 void
945 {
946  char name_cam[FILENAME_MAX];
947  char name[FILENAME_MAX];
948 
949  object = THREE_PTS;
950 
951  strcpy(name_cam, scene_dir.c_str());
952  strcat(name_cam,"/camera.bnd");
953  set_scene(name_cam,&camera,cameraFactor);
954 
955  strcpy(name,obj);
956  Model_3D model;
957  model = getExtension(obj);
958  if (model == BND_MODEL)
959  set_scene(name,&(this->scene),1.0);
960  else if (model == WRL_MODEL)
961  set_scene_wrl(name,&(this->scene),1.0);
962  else if (model == UNKNOWN_MODEL)
963  {
964  vpERROR_TRACE("Unknown file extension for the 3D model");
965  }
966 
967  add_rfstack(IS_BACK);
968 
969  add_vwstack ("start","depth", 0.0, 100.0);
970  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
971  add_vwstack ("start","type", PERSPECTIVE);
972 
973  sceneInitialized = true;
974  displayObject = true;
975  displayCamera = true;
976 }
977 
978 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
979 
993 void
995 {
996  initScene(obj);
997  objectImage.clear();
998  for(imObj.front(); !imObj.outside(); imObj.next()){
999  objectImage.push_back(imObj.value());
1000  }
1001  displayImageSimulator = true;
1002 }
1003 #endif
1004 
1016 void
1017 vpWireFrameSimulator::initScene(const char* obj, const std::list<vpImageSimulator> &imObj)
1018 {
1019  initScene(obj);
1020  objectImage = imObj;
1021  displayImageSimulator = true;
1022 }
1023 
1031 void
1033 {
1034  if (!sceneInitialized)
1035  throw(vpException(vpSimulatorException::notInitializedError,"The scene has to be initialized")) ;
1036 
1037  double u;
1038  double v;
1039  //if(px_int != 1 && py_int != 1)
1040  // we assume px_int and py_int > 0
1041  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1042  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1043  {
1044  u = (double)I.getWidth()/(2*px_int);
1045  v = (double)I.getHeight()/(2*py_int);
1046  }
1047  else
1048  {
1049  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1050  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1051  }
1052 
1053  float o44c[4][4],o44cd[4][4],x,y,z;
1054  Matrix id = IDENTITY_MATRIX;
1055 
1056  vp2jlc_matrix(cMo.inverse(),o44c);
1057  vp2jlc_matrix(cdMo.inverse(),o44cd);
1058 
1060  {
1061  I = 255;
1062 
1063  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1064  vpImageSimulator* imSim = &(*it);
1065  imSim->setCameraPosition(rotz*cMo);
1067  }
1068 
1069  if (I.display != NULL)
1070  vpDisplay::display(I);
1071  }
1072 
1073  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1074  x = o44c[2][0] + o44c[3][0];
1075  y = o44c[2][1] + o44c[3][1];
1076  z = o44c[2][2] + o44c[3][2];
1077  add_vwstack ("start","vrp", x,y,z);
1078  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1079  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1080  add_vwstack ("start","window", -u, u, -v, v);
1081  if (displayObject)
1082  display_scene(id,this->scene,I, curColor);
1083 
1084 
1085  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1086  x = o44cd[2][0] + o44cd[3][0];
1087  y = o44cd[2][1] + o44cd[3][1];
1088  z = o44cd[2][2] + o44cd[3][2];
1089  add_vwstack ("start","vrp", x,y,z);
1090  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1091  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1092  add_vwstack ("start","window", -u, u, -v, v);
1094  {
1096  else display_scene(id,desiredScene,I, desColor);
1097  }
1098 }
1099 
1100 
1109 void
1111 {
1112  bool changed = false;
1113  vpHomogeneousMatrix displacement = navigation(I,changed);
1114 
1115  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1116  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1117  camMf2 = camMf2*displacement;
1118 
1119  f2Mf = camMf2.inverse()*camMf;
1120 
1121  camMf = camMf2* displacement * f2Mf;
1122 
1123  double u;
1124  double v;
1125  //if(px_ext != 1 && py_ext != 1)
1126  // we assume px_ext and py_ext > 0
1127  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1128  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1129  {
1130  u = (double)I.getWidth()/(2*px_ext);
1131  v = (double)I.getHeight()/(2*py_ext);
1132  }
1133  else
1134  {
1135  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1136  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1137  }
1138 
1139  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1140 
1141  vp2jlc_matrix(camMf.inverse(),w44cext);
1142  vp2jlc_matrix(fMc,w44c);
1143  vp2jlc_matrix(fMo,w44o);
1144 
1145 
1146  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1147  x = w44cext[2][0] + w44cext[3][0];
1148  y = w44cext[2][1] + w44cext[3][1];
1149  z = w44cext[2][2] + w44cext[3][2];
1150  add_vwstack ("start","vrp", x,y,z);
1151  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1152  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1153  add_vwstack ("start","window", -u, u, -v, v);
1154  if ((object == CUBE) || (object == SPHERE))
1155  {
1156  add_vwstack ("start","type", PERSPECTIVE);
1157  }
1158 
1160  {
1161  I = 255;
1162 
1163  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1164  vpImageSimulator* imSim = &(*it);
1165  imSim->setCameraPosition(rotz*camMf*fMo);
1167  }
1168 
1169  if (I.display != NULL)
1170  vpDisplay::display(I);
1171  }
1172 
1173  if (displayObject)
1174  display_scene(w44o,this->scene,I, curColor);
1175 
1176  if (displayCamera)
1177  display_scene(w44c,camera, I, camColor);
1178 
1180  {
1181  vpImagePoint iP;
1182  vpImagePoint iP_1;
1183  poseList.push_back(cMo);
1184  fMoList.push_back(fMo);
1185 
1186  int iter = 0;
1187 
1188  if (changed || extCamChanged)
1189  {
1190  cameraTrajectory.clear();
1191  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1192  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1193 
1194  while ((iter_poseList != poseList.end()) && (iter_fMoList != fMoList.end()))
1195  {
1196  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1197  cameraTrajectory.push_back(iP);
1198  if (camTrajType == CT_LINE)
1199  {
1200  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1201  }
1202  else if (camTrajType == CT_POINT)
1204  ++iter_poseList;
1205  ++iter_fMoList;
1206  iter++;
1207  iP_1 = iP;
1208  }
1209  extCamChanged = false;
1210  }
1211  else
1212  {
1213  iP = projectCameraTrajectory(I, cMo, fMo);
1214  cameraTrajectory.push_back(iP);
1215 
1216  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1217  if (camTrajType == CT_LINE)
1218  {
1219  if (iter != 0) vpDisplay::displayLine(I, iP_1, *it, camTrajColor);
1220  }
1221  else if (camTrajType == CT_POINT)
1223  iter++;
1224  iP_1 = *it;
1225  }
1226  }
1227 
1228  if (poseList.size() > nbrPtLimit)
1229  {
1230  poseList.pop_front();
1231  }
1232  if (fMoList.size() > nbrPtLimit)
1233  {
1234  fMoList.pop_front();
1235  }
1236  if (cameraTrajectory.size() > nbrPtLimit)
1237  {
1238  cameraTrajectory.pop_front();
1239  }
1240  }
1241 }
1242 
1243 
1252 void
1254 {
1255  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1256 
1257  vpHomogeneousMatrix camMft = rotz * camMf;
1258 
1259  double u;
1260  double v;
1261  //if(px_ext != 1 && py_ext != 1)
1262  // we assume px_ext and py_ext > 0
1263  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1264  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1265  {
1266  u = (double)I.getWidth()/(2*px_ext);
1267  v = (double)I.getHeight()/(2*py_ext);
1268  }
1269  else
1270  {
1271  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1272  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1273  }
1274 
1275  vp2jlc_matrix(camMft.inverse(),w44cext);
1276  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1277  vp2jlc_matrix(fMo,w44o);
1278 
1279  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1280  x = w44cext[2][0] + w44cext[3][0];
1281  y = w44cext[2][1] + w44cext[3][1];
1282  z = w44cext[2][2] + w44cext[3][2];
1283  add_vwstack ("start","vrp", x,y,z);
1284  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1285  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1286  add_vwstack ("start","window", -u, u, -v, v);
1287 
1289  {
1290  I = 255;
1291 
1292  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1293  vpImageSimulator* imSim = &(*it);
1294  imSim->setCameraPosition(rotz*camMf*fMo);
1296  }
1297 
1298  if (I.display != NULL)
1299  vpDisplay::display(I);
1300  }
1301 
1302  if (displayObject)
1303  display_scene(w44o,this->scene,I, curColor);
1304  if (displayCamera)
1305  display_scene(w44c,camera, I, camColor);
1306 }
1307 
1308 
1316 void
1318 {
1319  if (!sceneInitialized)
1320  throw(vpException(vpSimulatorException::notInitializedError,"The scene has to be initialized")) ;
1321 
1322  double u;
1323  double v;
1324  //if(px_int != 1 && py_int != 1)
1325  // we assume px_int and py_int > 0
1326  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1327  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1328  {
1329  u = (double)I.getWidth()/(2*px_int);
1330  v = (double)I.getHeight()/(2*py_int);
1331  }
1332  else
1333  {
1334  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1335  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1336  }
1337 
1338  float o44c[4][4],o44cd[4][4],x,y,z;
1339  Matrix id = IDENTITY_MATRIX;
1340 
1341  vp2jlc_matrix(cMo.inverse(),o44c);
1342  vp2jlc_matrix(cdMo.inverse(),o44cd);
1343 
1345  {
1346  I = 255;
1347 
1348  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1349  vpImageSimulator* imSim = &(*it);
1350  imSim->setCameraPosition(rotz*camMf*fMo);
1352  }
1353 
1354  if (I.display != NULL)
1355  vpDisplay::display(I);
1356  }
1357 
1358  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1359  x = o44c[2][0] + o44c[3][0];
1360  y = o44c[2][1] + o44c[3][1];
1361  z = o44c[2][2] + o44c[3][2];
1362  add_vwstack ("start","vrp", x,y,z);
1363  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1364  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1365  add_vwstack ("start","window", -u, u, -v, v);
1366  if (displayObject)
1367  display_scene(id,this->scene,I, curColor);
1368 
1369 
1370  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1371  x = o44cd[2][0] + o44cd[3][0];
1372  y = o44cd[2][1] + o44cd[3][1];
1373  z = o44cd[2][2] + o44cd[3][2];
1374  add_vwstack ("start","vrp", x,y,z);
1375  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1376  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1377  add_vwstack ("start","window", -u, u, -v, v);
1379  {
1381  else display_scene(id,desiredScene,I, desColor);
1382  }
1383 }
1384 
1385 
1394 void
1396 {
1397  bool changed = false;
1398  vpHomogeneousMatrix displacement = navigation(I,changed);
1399 
1400  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1401  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1402  camMf2 = camMf2*displacement;
1403 
1404  f2Mf = camMf2.inverse()*camMf;
1405 
1406  camMf = camMf2* displacement * f2Mf;
1407 
1408  double u;
1409  double v;
1410  //if(px_ext != 1 && py_ext != 1)
1411  // we assume px_ext and py_ext > 0
1412  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1413  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1414  {
1415  u = (double)I.getWidth()/(2*px_ext);
1416  v = (double)I.getHeight()/(2*py_ext);
1417  }
1418  else
1419  {
1420  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1421  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1422  }
1423 
1424  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1425 
1426  vp2jlc_matrix(camMf.inverse(),w44cext);
1427  vp2jlc_matrix(fMc,w44c);
1428  vp2jlc_matrix(fMo,w44o);
1429 
1430 
1431  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1432  x = w44cext[2][0] + w44cext[3][0];
1433  y = w44cext[2][1] + w44cext[3][1];
1434  z = w44cext[2][2] + w44cext[3][2];
1435  add_vwstack ("start","vrp", x,y,z);
1436  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1437  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1438  add_vwstack ("start","window", -u, u, -v, v);
1439  if ((object == CUBE) || (object == SPHERE))
1440  {
1441  add_vwstack ("start","type", PERSPECTIVE);
1442  }
1443 
1445  {
1446  I = 255;
1447  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1448  vpImageSimulator* imSim = &(*it);
1449  imSim->setCameraPosition(rotz*camMf*fMo);
1451  }
1452  if (I.display != NULL)
1453  vpDisplay::display(I);
1454  }
1455 
1456  if (displayObject)
1457  display_scene(w44o,this->scene,I, curColor);
1458 
1459  if (displayCamera)
1460  display_scene(w44c,camera, I, camColor);
1461 
1463  {
1464  vpImagePoint iP;
1465  vpImagePoint iP_1;
1466  poseList.push_back(cMo);
1467  fMoList.push_back(fMo);
1468 
1469  int iter = 0;
1470 
1471  if (changed || extCamChanged)
1472  {
1473  cameraTrajectory.clear();
1474  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1475  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1476 
1477  while ((iter_poseList!=poseList.end()) && (iter_fMoList!=fMoList.end()) )
1478  {
1479  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1480  cameraTrajectory.push_back(iP);
1481  //vpDisplay::displayPoint(I,cameraTrajectory.value(),vpColor::green);
1482  if (camTrajType == CT_LINE)
1483  {
1484  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1485  }
1486  else if (camTrajType == CT_POINT)
1488  ++iter_poseList;
1489  ++iter_fMoList;
1490  iter++;
1491  iP_1 = iP;
1492  }
1493  extCamChanged = false;
1494  }
1495  else
1496  {
1497  iP = projectCameraTrajectory(I, cMo,fMo);
1498  cameraTrajectory.push_back(iP);
1499 
1500  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1501  if (camTrajType == CT_LINE){
1502  if (iter != 0) vpDisplay::displayLine(I,iP_1,*it,camTrajColor);
1503  }
1504  else if(camTrajType == CT_POINT)
1506  iter++;
1507  iP_1 = *it;
1508  }
1509  }
1510 
1511  if (poseList.size() > nbrPtLimit)
1512  {
1513  poseList.pop_front();
1514  }
1515  if (fMoList.size() > nbrPtLimit)
1516  {
1517  fMoList.pop_front();
1518  }
1519  if (cameraTrajectory.size() > nbrPtLimit)
1520  {
1521  cameraTrajectory.pop_front();
1522  }
1523  }
1524 }
1525 
1526 
1535 void
1537 {
1538  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1539 
1540  vpHomogeneousMatrix camMft = rotz * camMf;
1541 
1542  double u;
1543  double v;
1544  //if(px_ext != 1 && py_ext != 1)
1545  // we assume px_ext and py_ext > 0
1546  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1547  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1548  {
1549  u = (double)I.getWidth()/(2*px_ext);
1550  v = (double)I.getHeight()/(2*py_ext);
1551  }
1552  else
1553  {
1554  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1555  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1556  }
1557 
1558  vp2jlc_matrix(camMft.inverse(),w44cext);
1559  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1560  vp2jlc_matrix(fMo,w44o);
1561 
1562  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1563  x = w44cext[2][0] + w44cext[3][0];
1564  y = w44cext[2][1] + w44cext[3][1];
1565  z = w44cext[2][2] + w44cext[3][2];
1566  add_vwstack ("start","vrp", x,y,z);
1567  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1568  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1569  add_vwstack ("start","window", -u, u, -v, v);
1570 
1572  {
1573  I = 255;
1574  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1575  vpImageSimulator* imSim = &(*it);
1576  imSim->setCameraPosition(rotz*camMf*fMo);
1578  }
1579  if (I.display != NULL)
1580  vpDisplay::display(I);
1581  }
1582 
1583  if (displayObject)
1584  display_scene(w44o,this->scene,I, curColor);
1585  if (displayCamera)
1586  display_scene(w44c,camera, I, camColor);
1587 }
1588 
1589 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
1590 
1604 void
1606 {
1607  if (list_cMo.nbElements() != list_fMo.nbElements())
1608  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1609 
1610  list_cMo.front();
1611  list_fMo.front();
1612  vpImagePoint iP;
1613  vpImagePoint iP_1;
1614  int iter = 0;
1615 
1616  while (!list_cMo.outside() && !list_fMo.outside())
1617  {
1618  iP = projectCameraTrajectory(I, rotz * list_cMo.value(), list_fMo.value(), rotz * cMf);
1619  if (camTrajType == CT_LINE)
1620  {
1621  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1622  }
1623  else if (camTrajType == CT_POINT)
1625  list_cMo.next();
1626  list_fMo.next();
1627  iter++;
1628  iP_1 = iP;
1629  }
1630 }
1631 
1646 void
1648 {
1649  if (list_cMo.nbElements() != list_fMo.nbElements())
1650  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1651 
1652  list_cMo.front();
1653  list_fMo.front();
1654  vpImagePoint iP;
1655  vpImagePoint iP_1;
1656  int iter = 0;
1657 
1658  while (!list_cMo.outside() && !list_fMo.outside())
1659  {
1660  iP = projectCameraTrajectory(I, rotz * list_cMo.value(), list_fMo.value(), rotz * cMf);
1661  if (camTrajType == CT_LINE)
1662  {
1663  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1664  }
1665  else if (camTrajType == CT_POINT)
1667  list_cMo.next();
1668  list_fMo.next();
1669  iter++;
1670  iP_1 = iP;
1671  }
1672 }
1673 #endif
1674 
1675 
1686 void
1687 vpWireFrameSimulator::displayTrajectory (const vpImage<unsigned char> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1688  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1689 {
1690  if (list_cMo.size() != list_fMo.size())
1691  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1692 
1693  vpImagePoint iP;
1694  vpImagePoint iP_1;
1695  int iter = 0;
1696 
1697  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1698  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1699 
1700  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1701  {
1702  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1703  if (camTrajType == CT_LINE)
1704  {
1705  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1706  }
1707  else if (camTrajType == CT_POINT)
1709  ++it_cMo;
1710  ++it_fMo;
1711  iter++;
1712  iP_1 = iP;
1713  }
1714 }
1715 
1726 void
1727 vpWireFrameSimulator::displayTrajectory (const vpImage<vpRGBa> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1728  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1729 {
1730  if (list_cMo.size() != list_fMo.size())
1731  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1732 
1733  vpImagePoint iP;
1734  vpImagePoint iP_1;
1735  int iter = 0;
1736 
1737  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1738  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1739 
1740  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1741  {
1742  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1743  if (camTrajType == CT_LINE)
1744  {
1745  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor);
1746  }
1747  else if (camTrajType == CT_POINT)
1749  ++it_cMo;
1750  ++it_fMo;
1751  iter++;
1752  iP_1 = iP;
1753  }
1754 }
1755 
1756 
1762 {
1763  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1764  vpImagePoint iP;
1765  vpImagePoint trash;
1766  bool clicked = false;
1767  bool clickedUp = false;
1769 
1770  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1771  changed = false;
1772 
1773  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1774 
1775  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1776 
1777  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1778 
1779  if(clicked)
1780  {
1781  if (b == vpMouseButton::button1) blockedr = true;
1782  if (b == vpMouseButton::button2) blockedz = true;
1783  if (b == vpMouseButton::button3) blockedt = true;
1784  blocked = true;
1785  }
1786  if(clickedUp)
1787  {
1788  if (b == vpMouseButton::button1)
1789  {
1790  old_iPr = vpImagePoint(-1,-1);
1791  blockedr = false;
1792  }
1793  if (b == vpMouseButton::button2)
1794  {
1795  old_iPz = vpImagePoint(-1,-1);
1796  blockedz = false;
1797  }
1798  if (b == vpMouseButton::button3)
1799  {
1800  old_iPt = vpImagePoint(-1,-1);
1801  blockedt = false;
1802  }
1803  if (!(blockedr || blockedz || blockedt))
1804  {
1805  blocked = false;
1806  while (vpDisplay::getClick(I,trash,b,false)) {};
1807  }
1808  }
1809 
1811 
1812  double anglei = 0;
1813  double anglej = 0;
1814 
1815  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1816  {
1817  double diffi = iP.get_i() - old_iPr.get_i();
1818  double diffj = iP.get_j() - old_iPr.get_j();
1819  //cout << "delta :" << diffj << endl;;
1820  anglei = diffi*360/width;
1821  anglej = diffj*360/width;
1822  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1823  changed = true;
1824  }
1825 
1826  if (blockedr) old_iPr = iP;
1827 
1828  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1829  {
1830  double diffi = iP.get_i() - old_iPz.get_i();
1831  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1832  changed = true;
1833  }
1834 
1835  if (blockedz) old_iPz = iP;
1836 
1837  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1838  {
1839  double diffi = iP.get_i() - old_iPt.get_i();
1840  double diffj = iP.get_j() - old_iPt.get_j();
1841  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1842  changed = true;
1843  }
1844 
1845  if (blockedt) old_iPt = iP;
1846 
1847  return mov;
1848 }
1849 
1850 
1856 {
1857  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1858  vpImagePoint iP;
1859  vpImagePoint trash;
1860  bool clicked = false;
1861  bool clickedUp = false;
1863 
1864  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1865  changed = false;
1866 
1867  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1868 
1869  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1870 
1871  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1872 
1873  if(clicked)
1874  {
1875  if (b == vpMouseButton::button1) blockedr = true;
1876  if (b == vpMouseButton::button2) blockedz = true;
1877  if (b == vpMouseButton::button3) blockedt = true;
1878  blocked = true;
1879  }
1880  if(clickedUp)
1881  {
1882  if (b == vpMouseButton::button1)
1883  {
1884  old_iPr = vpImagePoint(-1,-1);
1885  blockedr = false;
1886  }
1887  if (b == vpMouseButton::button2)
1888  {
1889  old_iPz = vpImagePoint(-1,-1);
1890  blockedz = false;
1891  }
1892  if (b == vpMouseButton::button3)
1893  {
1894  old_iPt = vpImagePoint(-1,-1);
1895  blockedt = false;
1896  }
1897  if (!(blockedr || blockedz || blockedt))
1898  {
1899  blocked = false;
1900  while (vpDisplay::getClick(I,trash,b,false)) {};
1901  }
1902  }
1903 
1905 
1906  //std::cout << "point : " << iP << std::endl;
1907 
1908  double anglei = 0;
1909  double anglej = 0;
1910 
1911  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1912  {
1913  double diffi = iP.get_i() - old_iPr.get_i();
1914  double diffj = iP.get_j() - old_iPr.get_j();
1915  //cout << "delta :" << diffj << endl;;
1916  anglei = diffi*360/width;
1917  anglej = diffj*360/width;
1918  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1919  changed = true;
1920  }
1921 
1922  if (blockedr) old_iPr = iP;
1923 
1924  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1925  {
1926  double diffi = iP.get_i() - old_iPz.get_i();
1927  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1928  changed = true;
1929  }
1930 
1931  if (blockedz) old_iPz = iP;
1932 
1933  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1934  {
1935  double diffi = iP.get_i() - old_iPt.get_i();
1936  double diffj = iP.get_j() - old_iPt.get_j();
1937  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1938  changed = true;
1939  }
1940 
1941  if (blockedt) old_iPt = iP;
1942 
1943  return mov;
1944 }
1945 
1951 {
1952  vpPoint point;
1953  point.setWorldCoordinates(0,0,0);
1954 
1955  point.track(rotz*(camMf*fMo*cMo.inverse())) ;
1956 
1957  vpImagePoint iP;
1958 
1960 
1961  return iP;
1962 }
1963 
1969 {
1970  vpPoint point;
1971  point.setWorldCoordinates(0,0,0);
1972 
1973  point.track(rotz*(camMf*fMo*cMo.inverse())) ;
1974 
1975  vpImagePoint iP;
1976 
1978 
1979  return iP;
1980 }
1981 
1987 {
1988  vpPoint point;
1989  point.setWorldCoordinates(0,0,0);
1990 
1991  point.track(rotz*(cMf*fMo*cMo.inverse())) ;
1992 
1993  vpImagePoint iP;
1994 
1996 
1997  return iP;
1998 }
1999 
2005 {
2006  vpPoint point;
2007  point.setWorldCoordinates(0,0,0);
2008 
2009  point.track(rotz*(cMf*fMo*cMo.inverse())) ;
2010 
2011  vpImagePoint iP;
2012 
2014 
2015  return iP;
2016 }
2017 
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:335
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
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:75
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
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
vpCameraParameters getExternalCameraParameters(const vpImage< unsigned char > &I) const
virtual bool getPointerPosition(vpImagePoint &ip)=0
vpHomogeneousMatrix f2Mf
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...
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
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
vpSceneDesiredObject desiredObject
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)
vpHomogeneousMatrix fMc
vpImagePoint projectCameraTrajectory(const vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo, const vpHomogeneousMatrix &fMo)
vpHomogeneousMatrix inverse() const
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)
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
std::list< vpHomogeneousMatrix > poseList
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