ViSP  2.8.0
vpWireFrameSimulator.cpp
1 /****************************************************************************
2  *
3  * $Id: vpWireFrameSimulator.cpp 4227 2013-04-19 07:55:22Z 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,thickness_);
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,thickness_);
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,thickness_);
490  pt++;
491  }
492  if (fp->vertex.nbr > 2)
493  {
494  vpDisplay::displayLine(I,vpImagePoint((listpoint2i)->y,(listpoint2i)->x),vpImagePoint((pt)->y,(pt)->x),color,thickness_);
495  }
496  }
497  }
498  }
499  }
500 }
501 
502 /*************************************************************************************************************/
503 
513 {
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  thickness_ = 1;
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, "/circle_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(const vpSceneObject &obj, const 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  strcat(name_cam,"/camera.bnd");
856  set_scene(name_cam,&camera,cameraFactor);
857 
858  strcpy(name, scene_dir.c_str());
859  switch (obj)
860  {
861  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
862  case CUBE : { strcat(name, "/cube.bnd"); break; }
863  case PLATE : { strcat(name, "/plate.bnd"); break; }
864  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
865  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
866  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
867  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
868  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
869  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
870  case ROAD : { strcat(name, "/road.bnd"); break; }
871  case TIRE : { strcat(name, "/circles2.bnd"); break; }
872  case PIPE : { strcat(name, "/pipe.bnd"); break; }
873  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
874  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
875  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
876  case PLAN: { strcat(name, "/plan.bnd"); break; }
877  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
878  }
879  set_scene(name,&(this->scene),1.0);
880 
881  if (obj == PIPE) load_rfstack(IS_INSIDE);
882  else add_rfstack(IS_BACK);
883 
884  add_vwstack ("start","depth", 0.0, 100.0);
885  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
886  add_vwstack ("start","type", PERSPECTIVE);
887 
888  sceneInitialized = true;
889  displayObject = true;
890  displayCamera = true;
891 
892  displayDesiredObject = false;
893  displayImageSimulator = false;
894 }
895 
896 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
897 
911 void
913 {
914  initScene(obj);
915  objectImage.clear();
916  for(imObj.front(); !imObj.outside(); imObj.next()){
917  objectImage.push_back(imObj.value());
918  }
919  displayImageSimulator = true;
920 }
921 #endif
922 
934 void
935 vpWireFrameSimulator::initScene(const vpSceneObject &obj, const std::list<vpImageSimulator> &imObj)
936 {
937  initScene(obj);
938  objectImage = imObj;
939  displayImageSimulator = true;
940 }
941 
950 void
952 {
953  char name_cam[FILENAME_MAX];
954  char name[FILENAME_MAX];
955 
956  object = THREE_PTS;
957 
958  strcpy(name_cam, scene_dir.c_str());
959  strcat(name_cam,"/camera.bnd");
960  set_scene(name_cam,&camera,cameraFactor);
961 
962  strcpy(name,obj);
963  Model_3D model;
964  model = getExtension(obj);
965  if (model == BND_MODEL)
966  set_scene(name,&(this->scene),1.0);
967  else if (model == WRL_MODEL)
968  set_scene_wrl(name,&(this->scene),1.0);
969  else if (model == UNKNOWN_MODEL)
970  {
971  vpERROR_TRACE("Unknown file extension for the 3D model");
972  }
973 
974  add_rfstack(IS_BACK);
975 
976  add_vwstack ("start","depth", 0.0, 100.0);
977  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
978  add_vwstack ("start","type", PERSPECTIVE);
979 
980  sceneInitialized = true;
981  displayObject = true;
982  displayCamera = true;
983 }
984 
985 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
986 
1000 void
1002 {
1003  initScene(obj);
1004  objectImage.clear();
1005  for(imObj.front(); !imObj.outside(); imObj.next()){
1006  objectImage.push_back(imObj.value());
1007  }
1008  displayImageSimulator = true;
1009 }
1010 #endif
1011 
1023 void
1024 vpWireFrameSimulator::initScene(const char* obj, const std::list<vpImageSimulator> &imObj)
1025 {
1026  initScene(obj);
1027  objectImage = imObj;
1028  displayImageSimulator = true;
1029 }
1030 
1038 void
1040 {
1041  if (!sceneInitialized)
1042  throw(vpException(vpSimulatorException::notInitializedError,"The scene has to be initialized")) ;
1043 
1044  double u;
1045  double v;
1046  //if(px_int != 1 && py_int != 1)
1047  // we assume px_int and py_int > 0
1048  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1049  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1050  {
1051  u = (double)I.getWidth()/(2*px_int);
1052  v = (double)I.getHeight()/(2*py_int);
1053  }
1054  else
1055  {
1056  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1057  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1058  }
1059 
1060  float o44c[4][4],o44cd[4][4],x,y,z;
1061  Matrix id = IDENTITY_MATRIX;
1062 
1063  vp2jlc_matrix(cMo.inverse(),o44c);
1064  vp2jlc_matrix(cdMo.inverse(),o44cd);
1065 
1067  {
1068  I = 255;
1069 
1070  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1071  vpImageSimulator* imSim = &(*it);
1072  imSim->setCameraPosition(rotz*cMo);
1074  }
1075 
1076  if (I.display != NULL)
1077  vpDisplay::display(I);
1078  }
1079 
1080  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1081  x = o44c[2][0] + o44c[3][0];
1082  y = o44c[2][1] + o44c[3][1];
1083  z = o44c[2][2] + o44c[3][2];
1084  add_vwstack ("start","vrp", x,y,z);
1085  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1086  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1087  add_vwstack ("start","window", -u, u, -v, v);
1088  if (displayObject)
1089  display_scene(id,this->scene,I, curColor);
1090 
1091 
1092  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1093  x = o44cd[2][0] + o44cd[3][0];
1094  y = o44cd[2][1] + o44cd[3][1];
1095  z = o44cd[2][2] + o44cd[3][2];
1096  add_vwstack ("start","vrp", x,y,z);
1097  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1098  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1099  add_vwstack ("start","window", -u, u, -v, v);
1101  {
1103  else display_scene(id,desiredScene,I, desColor);
1104  }
1105 }
1106 
1107 
1116 void
1118 {
1119  bool changed = false;
1120  vpHomogeneousMatrix displacement = navigation(I,changed);
1121 
1122  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1123  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1124  camMf2 = camMf2*displacement;
1125 
1126  f2Mf = camMf2.inverse()*camMf;
1127 
1128  camMf = camMf2* displacement * f2Mf;
1129 
1130  double u;
1131  double v;
1132  //if(px_ext != 1 && py_ext != 1)
1133  // we assume px_ext and py_ext > 0
1134  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1135  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1136  {
1137  u = (double)I.getWidth()/(2*px_ext);
1138  v = (double)I.getHeight()/(2*py_ext);
1139  }
1140  else
1141  {
1142  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1143  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1144  }
1145 
1146  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1147 
1148  vp2jlc_matrix(camMf.inverse(),w44cext);
1149  vp2jlc_matrix(fMc,w44c);
1150  vp2jlc_matrix(fMo,w44o);
1151 
1152 
1153  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1154  x = w44cext[2][0] + w44cext[3][0];
1155  y = w44cext[2][1] + w44cext[3][1];
1156  z = w44cext[2][2] + w44cext[3][2];
1157  add_vwstack ("start","vrp", x,y,z);
1158  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1159  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1160  add_vwstack ("start","window", -u, u, -v, v);
1161  if ((object == CUBE) || (object == SPHERE))
1162  {
1163  add_vwstack ("start","type", PERSPECTIVE);
1164  }
1165 
1167  {
1168  I = 255;
1169 
1170  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1171  vpImageSimulator* imSim = &(*it);
1172  imSim->setCameraPosition(rotz*camMf*fMo);
1174  }
1175 
1176  if (I.display != NULL)
1177  vpDisplay::display(I);
1178  }
1179 
1180  if (displayObject)
1181  display_scene(w44o,this->scene,I, curColor);
1182 
1183  if (displayCamera)
1184  display_scene(w44c,camera, I, camColor);
1185 
1187  {
1188  vpImagePoint iP;
1189  vpImagePoint iP_1;
1190  poseList.push_back(cMo);
1191  fMoList.push_back(fMo);
1192 
1193  int iter = 0;
1194 
1195  if (changed || extCamChanged)
1196  {
1197  cameraTrajectory.clear();
1198  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1199  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1200 
1201  while ((iter_poseList != poseList.end()) && (iter_fMoList != fMoList.end()))
1202  {
1203  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1204  cameraTrajectory.push_back(iP);
1205  if (camTrajType == CT_LINE)
1206  {
1207  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor, thickness_);
1208  }
1209  else if (camTrajType == CT_POINT)
1211  ++iter_poseList;
1212  ++iter_fMoList;
1213  iter++;
1214  iP_1 = iP;
1215  }
1216  extCamChanged = false;
1217  }
1218  else
1219  {
1220  iP = projectCameraTrajectory(I, cMo, fMo);
1221  cameraTrajectory.push_back(iP);
1222 
1223  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1224  if (camTrajType == CT_LINE)
1225  {
1226  if (iter != 0) vpDisplay::displayLine(I, iP_1, *it, camTrajColor, thickness_);
1227  }
1228  else if (camTrajType == CT_POINT)
1230  iter++;
1231  iP_1 = *it;
1232  }
1233  }
1234 
1235  if (poseList.size() > nbrPtLimit)
1236  {
1237  poseList.pop_front();
1238  }
1239  if (fMoList.size() > nbrPtLimit)
1240  {
1241  fMoList.pop_front();
1242  }
1243  if (cameraTrajectory.size() > nbrPtLimit)
1244  {
1245  cameraTrajectory.pop_front();
1246  }
1247  }
1248 }
1249 
1250 
1259 void
1261 {
1262  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1263 
1264  vpHomogeneousMatrix camMft = rotz * camMf;
1265 
1266  double u;
1267  double v;
1268  //if(px_ext != 1 && py_ext != 1)
1269  // we assume px_ext and py_ext > 0
1270  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1271  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1272  {
1273  u = (double)I.getWidth()/(2*px_ext);
1274  v = (double)I.getHeight()/(2*py_ext);
1275  }
1276  else
1277  {
1278  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1279  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1280  }
1281 
1282  vp2jlc_matrix(camMft.inverse(),w44cext);
1283  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1284  vp2jlc_matrix(fMo,w44o);
1285 
1286  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1287  x = w44cext[2][0] + w44cext[3][0];
1288  y = w44cext[2][1] + w44cext[3][1];
1289  z = w44cext[2][2] + w44cext[3][2];
1290  add_vwstack ("start","vrp", x,y,z);
1291  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1292  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1293  add_vwstack ("start","window", -u, u, -v, v);
1294 
1296  {
1297  I = 255;
1298 
1299  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1300  vpImageSimulator* imSim = &(*it);
1301  imSim->setCameraPosition(rotz*camMf*fMo);
1303  }
1304 
1305  if (I.display != NULL)
1306  vpDisplay::display(I);
1307  }
1308 
1309  if (displayObject)
1310  display_scene(w44o,this->scene,I, curColor);
1311  if (displayCamera)
1312  display_scene(w44c,camera, I, camColor);
1313 }
1314 
1315 
1323 void
1325 {
1326  if (!sceneInitialized)
1327  throw(vpException(vpSimulatorException::notInitializedError,"The scene has to be initialized")) ;
1328 
1329  double u;
1330  double v;
1331  //if(px_int != 1 && py_int != 1)
1332  // we assume px_int and py_int > 0
1333  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1334  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1335  {
1336  u = (double)I.getWidth()/(2*px_int);
1337  v = (double)I.getHeight()/(2*py_int);
1338  }
1339  else
1340  {
1341  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1342  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1343  }
1344 
1345  float o44c[4][4],o44cd[4][4],x,y,z;
1346  Matrix id = IDENTITY_MATRIX;
1347 
1348  vp2jlc_matrix(cMo.inverse(),o44c);
1349  vp2jlc_matrix(cdMo.inverse(),o44cd);
1350 
1352  {
1353  I = 255;
1354 
1355  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1356  vpImageSimulator* imSim = &(*it);
1357  imSim->setCameraPosition(rotz*camMf*fMo);
1359  }
1360 
1361  if (I.display != NULL)
1362  vpDisplay::display(I);
1363  }
1364 
1365  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1366  x = o44c[2][0] + o44c[3][0];
1367  y = o44c[2][1] + o44c[3][1];
1368  z = o44c[2][2] + o44c[3][2];
1369  add_vwstack ("start","vrp", x,y,z);
1370  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1371  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1372  add_vwstack ("start","window", -u, u, -v, v);
1373  if (displayObject)
1374  display_scene(id,this->scene,I, curColor);
1375 
1376 
1377  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1378  x = o44cd[2][0] + o44cd[3][0];
1379  y = o44cd[2][1] + o44cd[3][1];
1380  z = o44cd[2][2] + o44cd[3][2];
1381  add_vwstack ("start","vrp", x,y,z);
1382  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1383  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1384  add_vwstack ("start","window", -u, u, -v, v);
1386  {
1388  else display_scene(id,desiredScene,I, desColor);
1389  }
1390 }
1391 
1392 
1401 void
1403 {
1404  bool changed = false;
1405  vpHomogeneousMatrix displacement = navigation(I,changed);
1406 
1407  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1408  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1409  camMf2 = camMf2*displacement;
1410 
1411  f2Mf = camMf2.inverse()*camMf;
1412 
1413  camMf = camMf2* displacement * f2Mf;
1414 
1415  double u;
1416  double v;
1417  //if(px_ext != 1 && py_ext != 1)
1418  // we assume px_ext and py_ext > 0
1419  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1420  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1421  {
1422  u = (double)I.getWidth()/(2*px_ext);
1423  v = (double)I.getHeight()/(2*py_ext);
1424  }
1425  else
1426  {
1427  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1428  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1429  }
1430 
1431  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1432 
1433  vp2jlc_matrix(camMf.inverse(),w44cext);
1434  vp2jlc_matrix(fMc,w44c);
1435  vp2jlc_matrix(fMo,w44o);
1436 
1437 
1438  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1439  x = w44cext[2][0] + w44cext[3][0];
1440  y = w44cext[2][1] + w44cext[3][1];
1441  z = w44cext[2][2] + w44cext[3][2];
1442  add_vwstack ("start","vrp", x,y,z);
1443  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1444  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1445  add_vwstack ("start","window", -u, u, -v, v);
1446  if ((object == CUBE) || (object == SPHERE))
1447  {
1448  add_vwstack ("start","type", PERSPECTIVE);
1449  }
1450 
1452  {
1453  I = 255;
1454  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1455  vpImageSimulator* imSim = &(*it);
1456  imSim->setCameraPosition(rotz*camMf*fMo);
1458  }
1459  if (I.display != NULL)
1460  vpDisplay::display(I);
1461  }
1462 
1463  if (displayObject)
1464  display_scene(w44o,this->scene,I, curColor);
1465 
1466  if (displayCamera)
1467  display_scene(w44c,camera, I, camColor);
1468 
1470  {
1471  vpImagePoint iP;
1472  vpImagePoint iP_1;
1473  poseList.push_back(cMo);
1474  fMoList.push_back(fMo);
1475 
1476  int iter = 0;
1477 
1478  if (changed || extCamChanged)
1479  {
1480  cameraTrajectory.clear();
1481  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1482  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1483 
1484  while ((iter_poseList!=poseList.end()) && (iter_fMoList!=fMoList.end()) )
1485  {
1486  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1487  cameraTrajectory.push_back(iP);
1488  //vpDisplay::displayPoint(I,cameraTrajectory.value(),vpColor::green);
1489  if (camTrajType == CT_LINE)
1490  {
1491  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor, thickness_);
1492  }
1493  else if (camTrajType == CT_POINT)
1495  ++iter_poseList;
1496  ++iter_fMoList;
1497  iter++;
1498  iP_1 = iP;
1499  }
1500  extCamChanged = false;
1501  }
1502  else
1503  {
1504  iP = projectCameraTrajectory(I, cMo,fMo);
1505  cameraTrajectory.push_back(iP);
1506 
1507  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1508  if (camTrajType == CT_LINE){
1509  if (iter != 0) vpDisplay::displayLine(I,iP_1,*it,camTrajColor, thickness_);
1510  }
1511  else if(camTrajType == CT_POINT)
1513  iter++;
1514  iP_1 = *it;
1515  }
1516  }
1517 
1518  if (poseList.size() > nbrPtLimit)
1519  {
1520  poseList.pop_front();
1521  }
1522  if (fMoList.size() > nbrPtLimit)
1523  {
1524  fMoList.pop_front();
1525  }
1526  if (cameraTrajectory.size() > nbrPtLimit)
1527  {
1528  cameraTrajectory.pop_front();
1529  }
1530  }
1531 }
1532 
1533 
1542 void
1544 {
1545  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1546 
1547  vpHomogeneousMatrix camMft = rotz * camMf;
1548 
1549  double u;
1550  double v;
1551  //if(px_ext != 1 && py_ext != 1)
1552  // we assume px_ext and py_ext > 0
1553  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1554  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1555  {
1556  u = (double)I.getWidth()/(2*px_ext);
1557  v = (double)I.getHeight()/(2*py_ext);
1558  }
1559  else
1560  {
1561  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1562  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1563  }
1564 
1565  vp2jlc_matrix(camMft.inverse(),w44cext);
1566  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1567  vp2jlc_matrix(fMo,w44o);
1568 
1569  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1570  x = w44cext[2][0] + w44cext[3][0];
1571  y = w44cext[2][1] + w44cext[3][1];
1572  z = w44cext[2][2] + w44cext[3][2];
1573  add_vwstack ("start","vrp", x,y,z);
1574  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1575  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1576  add_vwstack ("start","window", -u, u, -v, v);
1577 
1579  {
1580  I = 255;
1581  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1582  vpImageSimulator* imSim = &(*it);
1583  imSim->setCameraPosition(rotz*camMf*fMo);
1585  }
1586  if (I.display != NULL)
1587  vpDisplay::display(I);
1588  }
1589 
1590  if (displayObject)
1591  display_scene(w44o,this->scene,I, curColor);
1592  if (displayCamera)
1593  display_scene(w44c,camera, I, camColor);
1594 }
1595 
1596 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
1597 
1611 void
1613 {
1614  if (list_cMo.nbElements() != list_fMo.nbElements())
1615  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1616 
1617  list_cMo.front();
1618  list_fMo.front();
1619  vpImagePoint iP;
1620  vpImagePoint iP_1;
1621  int iter = 0;
1622 
1623  while (!list_cMo.outside() && !list_fMo.outside())
1624  {
1625  iP = projectCameraTrajectory(I, rotz * list_cMo.value(), list_fMo.value(), rotz * cMf);
1626  if (camTrajType == CT_LINE)
1627  {
1628  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor, thickness_);
1629  }
1630  else if (camTrajType == CT_POINT)
1632  list_cMo.next();
1633  list_fMo.next();
1634  iter++;
1635  iP_1 = iP;
1636  }
1637 }
1638 
1653 void
1655 {
1656  if (list_cMo.nbElements() != list_fMo.nbElements())
1657  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1658 
1659  list_cMo.front();
1660  list_fMo.front();
1661  vpImagePoint iP;
1662  vpImagePoint iP_1;
1663  int iter = 0;
1664 
1665  while (!list_cMo.outside() && !list_fMo.outside())
1666  {
1667  iP = projectCameraTrajectory(I, rotz * list_cMo.value(), list_fMo.value(), rotz * cMf);
1668  if (camTrajType == CT_LINE)
1669  {
1670  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor,thickness_);
1671  }
1672  else if (camTrajType == CT_POINT)
1674  list_cMo.next();
1675  list_fMo.next();
1676  iter++;
1677  iP_1 = iP;
1678  }
1679 }
1680 #endif
1681 
1682 
1693 void
1694 vpWireFrameSimulator::displayTrajectory (const vpImage<unsigned char> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1695  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1696 {
1697  if (list_cMo.size() != list_fMo.size())
1698  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1699 
1700  vpImagePoint iP;
1701  vpImagePoint iP_1;
1702  int iter = 0;
1703 
1704  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1705  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1706 
1707  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1708  {
1709  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1710  if (camTrajType == CT_LINE)
1711  {
1712  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor,thickness_);
1713  }
1714  else if (camTrajType == CT_POINT)
1716  ++it_cMo;
1717  ++it_fMo;
1718  iter++;
1719  iP_1 = iP;
1720  }
1721 }
1722 
1733 void
1734 vpWireFrameSimulator::displayTrajectory (const vpImage<vpRGBa> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1735  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1736 {
1737  if (list_cMo.size() != list_fMo.size())
1738  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1739 
1740  vpImagePoint iP;
1741  vpImagePoint iP_1;
1742  int iter = 0;
1743 
1744  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1745  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1746 
1747  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1748  {
1749  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1750  if (camTrajType == CT_LINE)
1751  {
1752  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor,thickness_);
1753  }
1754  else if (camTrajType == CT_POINT)
1756  ++it_cMo;
1757  ++it_fMo;
1758  iter++;
1759  iP_1 = iP;
1760  }
1761 }
1762 
1763 
1769 {
1770  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1771  vpImagePoint iP;
1772  vpImagePoint trash;
1773  bool clicked = false;
1774  bool clickedUp = false;
1776 
1777  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1778  changed = false;
1779 
1780  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1781 
1782  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1783 
1784  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1785 
1786  if(clicked)
1787  {
1788  if (b == vpMouseButton::button1) blockedr = true;
1789  if (b == vpMouseButton::button2) blockedz = true;
1790  if (b == vpMouseButton::button3) blockedt = true;
1791  blocked = true;
1792  }
1793  if(clickedUp)
1794  {
1795  if (b == vpMouseButton::button1)
1796  {
1797  old_iPr = vpImagePoint(-1,-1);
1798  blockedr = false;
1799  }
1800  if (b == vpMouseButton::button2)
1801  {
1802  old_iPz = vpImagePoint(-1,-1);
1803  blockedz = false;
1804  }
1805  if (b == vpMouseButton::button3)
1806  {
1807  old_iPt = vpImagePoint(-1,-1);
1808  blockedt = false;
1809  }
1810  if (!(blockedr || blockedz || blockedt))
1811  {
1812  blocked = false;
1813  while (vpDisplay::getClick(I,trash,b,false)) {};
1814  }
1815  }
1816 
1818 
1819  double anglei = 0;
1820  double anglej = 0;
1821 
1822  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1823  {
1824  double diffi = iP.get_i() - old_iPr.get_i();
1825  double diffj = iP.get_j() - old_iPr.get_j();
1826  //cout << "delta :" << diffj << endl;;
1827  anglei = diffi*360/width;
1828  anglej = diffj*360/width;
1829  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1830  changed = true;
1831  }
1832 
1833  if (blockedr) old_iPr = iP;
1834 
1835  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1836  {
1837  double diffi = iP.get_i() - old_iPz.get_i();
1838  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1839  changed = true;
1840  }
1841 
1842  if (blockedz) old_iPz = iP;
1843 
1844  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1845  {
1846  double diffi = iP.get_i() - old_iPt.get_i();
1847  double diffj = iP.get_j() - old_iPt.get_j();
1848  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1849  changed = true;
1850  }
1851 
1852  if (blockedt) old_iPt = iP;
1853 
1854  return mov;
1855 }
1856 
1857 
1863 {
1864  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1865  vpImagePoint iP;
1866  vpImagePoint trash;
1867  bool clicked = false;
1868  bool clickedUp = false;
1870 
1871  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1872  changed = false;
1873 
1874  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1875 
1876  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1877 
1878  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1879 
1880  if(clicked)
1881  {
1882  if (b == vpMouseButton::button1) blockedr = true;
1883  if (b == vpMouseButton::button2) blockedz = true;
1884  if (b == vpMouseButton::button3) blockedt = true;
1885  blocked = true;
1886  }
1887  if(clickedUp)
1888  {
1889  if (b == vpMouseButton::button1)
1890  {
1891  old_iPr = vpImagePoint(-1,-1);
1892  blockedr = false;
1893  }
1894  if (b == vpMouseButton::button2)
1895  {
1896  old_iPz = vpImagePoint(-1,-1);
1897  blockedz = false;
1898  }
1899  if (b == vpMouseButton::button3)
1900  {
1901  old_iPt = vpImagePoint(-1,-1);
1902  blockedt = false;
1903  }
1904  if (!(blockedr || blockedz || blockedt))
1905  {
1906  blocked = false;
1907  while (vpDisplay::getClick(I,trash,b,false)) {};
1908  }
1909  }
1910 
1912 
1913  //std::cout << "point : " << iP << std::endl;
1914 
1915  double anglei = 0;
1916  double anglej = 0;
1917 
1918  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1919  {
1920  double diffi = iP.get_i() - old_iPr.get_i();
1921  double diffj = iP.get_j() - old_iPr.get_j();
1922  //cout << "delta :" << diffj << endl;;
1923  anglei = diffi*360/width;
1924  anglej = diffj*360/width;
1925  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1926  changed = true;
1927  }
1928 
1929  if (blockedr) old_iPr = iP;
1930 
1931  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1932  {
1933  double diffi = iP.get_i() - old_iPz.get_i();
1934  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1935  changed = true;
1936  }
1937 
1938  if (blockedz) old_iPz = iP;
1939 
1940  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1941  {
1942  double diffi = iP.get_i() - old_iPt.get_i();
1943  double diffj = iP.get_j() - old_iPt.get_j();
1944  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1945  changed = true;
1946  }
1947 
1948  if (blockedt) old_iPt = iP;
1949 
1950  return mov;
1951 }
1952 
1958 {
1959  vpPoint point;
1960  point.setWorldCoordinates(0,0,0);
1961 
1962  point.track(rotz*(camMf*fMo*cMo.inverse())) ;
1963 
1964  vpImagePoint iP;
1965 
1967 
1968  return iP;
1969 }
1970 
1976 {
1977  vpPoint point;
1978  point.setWorldCoordinates(0,0,0);
1979 
1980  point.track(rotz*(camMf*fMo*cMo.inverse())) ;
1981 
1982  vpImagePoint iP;
1983 
1985 
1986  return iP;
1987 }
1988 
1994 {
1995  vpPoint point;
1996  point.setWorldCoordinates(0,0,0);
1997 
1998  point.track(rotz*(cMf*fMo*cMo.inverse())) ;
1999 
2000  vpImagePoint iP;
2001 
2003 
2004  return iP;
2005 }
2006 
2012 {
2013  vpPoint point;
2014  point.setWorldCoordinates(0,0,0);
2015 
2016  point.track(rotz*(cMf*fMo*cMo.inverse())) ;
2017 
2018  vpImagePoint iP;
2019 
2021 
2022  return iP;
2023 }
2024 
The object displayed at the desired position is the same than the scene object defined in vpSceneObje...
vpDisplay * display
Definition: vpImage.h:121
A cylinder of 80cm length and 10cm radius.
unsigned int nbElements(void)
return the number of element in the list
Definition: vpList.h:261
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
A tire represented by 2 circles with radius 10cm and 15cm.
double get_i() const
Definition: vpImagePoint.h: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:159
Three parallel lines with equation y=-5, y=0, y=5.
A plane represented by a 56cm by 56cm plate with a grid of 49 squares inside.
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:379
Provide simple list management.
Definition: vpList.h:112
A plate with 8 points at coordinates (0.05,0,0), (0.15,0.05,0), (0.2,0.2,0), (-0.05,0.2,0), (-0.15,-0.1,0), (-0.1,-0.1,0), (-0.05,0.05,0) and (0.5,0,0). ach point is represented by a circle with 2cm radius.
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
A 40cm by 40cm plate with 4 points at coordinates (-0.1,-0.1,0), (0.1,-0.1,0), (0.1,0.1,0), (0.1,0.1,0). Each point is represented by a circle with 2cm radius.
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:125
A cylindrical tool is attached to the camera.
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:208
virtual bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)=0
std::list< vpImageSimulator > objectImage
error that can be emited by ViSP classes.
Definition: vpException.h: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
A 40cm by 40cm plate with 4 points at coordinates (-0.05,0.05,0), (0.05,0.05,0), (0.05,-0.05,0), (-0.05,-0.05,0). Each point is represented by a circle with 2cm radius.
static const vpColor green
Definition: vpColor.h:170
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:167
Class that defines what is a point.
Definition: vpPoint.h:65
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:137
4 points at coordinates (-0.03,-0.03,0), (0.03,-0.03,0), (0.03,0.03,0), (0.03,0.03,0). Each point is represented by a circle with 1cm radius.
vpCameraParameters getExternalCameraParameters(const vpImage< unsigned char > &I) const
virtual bool getPointerPosition(vpImagePoint &ip)=0
The object displayed at the desired position is a circle.
vpHomogeneousMatrix f2Mf
A 40cm by 40cm plate with 4 points at coordinates (-0.025,-0.05,0), (-0.075,0.05,0), (0.075,0.05,0), (0.025,-0.05,0). Each point is represented by a circle with 2cm radius.
void front(void)
Position the current element on the first element of the list.
Definition: vpList.h:386
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
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
A 40cm by 40cm plate with 3 points at coordinates (0,0,0), (0.1,0,0), (0,0.1,0). Each point is repres...
void getExternalImage(vpImage< unsigned char > &I)
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
vpCameraParameters getInternalCameraParameters(const vpImage< unsigned char > &I) const
static double rad(double deg)
Definition: vpMath.h:100
void displayTrajectory(const vpImage< unsigned char > &I, const std::list< vpHomogeneousMatrix > &list_cMo, const std::list< vpHomogeneousMatrix > &list_fMo, const vpHomogeneousMatrix &camMf)
vpCameraTrajectoryDisplayType camTrajType
A 40cm by 40cm plate with 4 points at coordinates (-0.07,-0.05,0), (0.07,0.05,0), (0...
vpSceneDesiredObject desiredObject
A 40cm by 40cm plate with 4 points at coordinates (0,-0.1,0), (0.1,0,0), (0,0.1,0), (-0.1,0,0). Each point is represented by a circle with 2cm radius.
void getInternalImage(vpImage< unsigned char > &I)
void display_scene(Matrix mat, Bound_scene &sc, const vpImage< vpRGBa > &I, const vpColor &color)
vpHomogeneousMatrix camMf
vpHomogeneousMatrix navigation(const vpImage< vpRGBa > &I, bool &changed)
Three parallel lines representing a road.
vpHomogeneousMatrix fMc
vpImagePoint projectCameraTrajectory(const vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo, const vpHomogeneousMatrix &fMo)
vpHomogeneousMatrix inverse() const
vpHomogeneousMatrix rotz
A pipe represented by a cylinder of 25 cm length and 15cm radius.
unsigned int getHeight() const
Definition: vpImage.h:150
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
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:173
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