Visual Servoing Platform  version 3.0.0
vpWireFrameSimulator.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Wire frame simulator
32  *
33  * Authors:
34  * Nicolas Melchior
35  *
36  *****************************************************************************/
37 
43 #include <visp3/robot/vpWireFrameSimulator.h>
44 #include <fcntl.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <vector>
48 
49 #include <visp3/core/vpException.h>
50 #include <visp3/core/vpPoint.h>
51 #include <visp3/core/vpCameraParameters.h>
52 #include <visp3/core/vpMeterPixelConversion.h>
53 #include <visp3/core/vpPoint.h>
54 #include <visp3/core/vpIoTools.h>
55 
56 //Inventor includes
57 #if defined(VISP_HAVE_COIN3D)
58 #include <Inventor/nodes/SoSeparator.h>
59 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
60 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
61 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
62 #include <Inventor/actions/SoWriteAction.h>
63 #include <Inventor/actions/SoSearchAction.h>
64 #include <Inventor/misc/SoChildList.h>
65 #include <Inventor/actions/SoGetMatrixAction.h>
66 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
67 #include <Inventor/actions/SoToVRML2Action.h>
68 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
69 #include <Inventor/VRMLnodes/SoVRMLShape.h>
70 #endif
71 
72 extern Point2i *point2i;
73 extern Point2i *listpoint2i;
74 
75 typedef enum
76 {
77  BND_MODEL,
78  WRL_MODEL,
79  UNKNOWN_MODEL
80 } Model_3D;
81 
82 Model_3D getExtension(const char* file);
83 void set_scene_wrl (const char* str, Bound_scene *sc, float factor);
84 
85 /*
86  Get the extension of the file and return it
87 */
88 Model_3D
89 getExtension(const char* file)
90 {
91  std::string sfilename(file);
92 
93  size_t bnd = sfilename.find("bnd");
94  size_t BND = sfilename.find("BND");
95  size_t wrl = sfilename.find("wrl");
96  size_t WRL = sfilename.find("WRL");
97 
98  size_t size = sfilename.size();
99 
100  if ((bnd>0 && bnd<size ) || (BND>0 && BND<size))
101  return BND_MODEL;
102  else if ((wrl>0 && wrl<size) || ( WRL>0 && WRL<size))
103  {
104 #if defined(VISP_HAVE_COIN3D)
105  return WRL_MODEL;
106 #else
107  std::cout << "Coin not installed, cannot read VRML files" << std::endl;
108  throw std::string("Coin not installed, cannot read VRML files");
109 #endif
110  }
111  return UNKNOWN_MODEL;
112 }
113 
114 /*
115  Enable to initialize the scene
116 */
117 void set_scene (const char* str, Bound_scene *sc, float factor)
118 {
119  FILE *fd;
120 
121  //if ((fd = fopen (str, 0)) == -1)
122  if ((fd = fopen (str, "r")) == NULL)
123  {
124  std::string error = "The file " + std::string(str) + " can not be opened";
125 
126  throw(vpException(vpException::ioError, error.c_str())) ;
127  }
128  open_keyword (keyword_tbl);
129  open_lex ();
130  open_source (fd, str);
131  malloc_Bound_scene (sc, str,(Index)BOUND_NBR);
132  parser (sc);
133 
134  //if (factor != 1)
135  if (std::fabs(factor) > std::numeric_limits<double>::epsilon())
136  {
137  for (int i = 0; i < sc->bound.nbr; i++)
138  {
139  for (int j = 0; j < sc->bound.ptr[i].point.nbr; j++)
140  {
141  sc->bound.ptr[i].point.ptr[j].x = sc->bound.ptr[i].point.ptr[j].x*factor;
142  sc->bound.ptr[i].point.ptr[j].y = sc->bound.ptr[i].point.ptr[j].y*factor;
143  sc->bound.ptr[i].point.ptr[j].z = sc->bound.ptr[i].point.ptr[j].z*factor;
144  }
145  }
146  }
147 
148  close_source ();
149  close_lex ();
150  close_keyword ();
151  fclose(fd);
152 }
153 
154 #if defined(VISP_HAVE_COIN3D)
155 
156 #ifndef DOXYGEN_SHOULD_SKIP_THIS
157 typedef struct indexFaceSet
158 {
159  indexFaceSet() : nbPt(0), pt(), nbIndex(0), index() {};
160  int nbPt;
161  std::vector<vpPoint> pt;
162  int nbIndex;
163  std::vector<int> index;
164 } indexFaceSet;
165 #endif // DOXYGEN_SHOULD_SKIP_THIS
166 
167 void extractFaces(SoVRMLIndexedFaceSet*, indexFaceSet *ifs);
168 void ifsToBound (Bound*, std::list<indexFaceSet*> &);
169 void destroyIfs(std::list<indexFaceSet*> &);
170 
171 
172 void set_scene_wrl (const char* str, Bound_scene *sc, float factor)
173 {
174  //Load the sceneGraph
175  SoDB::init();
176  SoInput in;
177  SbBool ok = in.openFile(str);
178  SoSeparator *sceneGraph;
179  SoVRMLGroup *sceneGraphVRML2;
180 
181  if (!ok) {
182  vpERROR_TRACE("can't open file \"%s\" \n Please check the Marker_Less.ini file", str);
183  exit(1);
184  }
185 
186  if(!in.isFileVRML2())
187  {
188  sceneGraph = SoDB::readAll(&in);
189  if (sceneGraph == NULL) { /*return -1;*/ }
190  sceneGraph->ref();
191 
192  SoToVRML2Action tovrml2;
193  tovrml2.apply(sceneGraph);
194  sceneGraphVRML2 =tovrml2.getVRML2SceneGraph();
195  sceneGraphVRML2->ref();
196  sceneGraph->unref();
197  }
198  else
199  {
200  sceneGraphVRML2 = SoDB::readAllVRML(&in);
201  if (sceneGraphVRML2 == NULL) {
202  /*return -1;*/
203  throw(vpException(vpException::notInitialized, "Cannot read VRML file"));
204  }
205  sceneGraphVRML2->ref();
206  }
207 
208  in.closeFile();
209 
210  int nbShapes = sceneGraphVRML2->getNumChildren();
211 
212  SoNode * child;
213 
214  malloc_Bound_scene (sc, str,(Index)BOUND_NBR);
215 
216  int iterShapes = 0;
217  for (int i = 0; i < nbShapes; i++)
218  {
219  int nbFaces = 0;
220  child = sceneGraphVRML2->getChild(i);
221  if (child->getTypeId() == SoVRMLShape::getClassTypeId())
222  {
223  std::list<indexFaceSet*> ifs_list;
224  SoChildList * child2list = child->getChildren();
225  for (int j = 0; j < child2list->getLength(); j++)
226  {
227  if (((SoNode*)child2list->get(j))->getTypeId() == SoVRMLIndexedFaceSet::getClassTypeId())
228  {
229  indexFaceSet *ifs = new indexFaceSet;
230  SoVRMLIndexedFaceSet * face_set;
231  face_set = (SoVRMLIndexedFaceSet*)child2list->get(j);
232  extractFaces(face_set,ifs);
233  ifs_list.push_back(ifs);
234  nbFaces++;
235  }
236 // if (((SoNode*)child2list->get(j))->getTypeId() == SoVRMLIndexedLineSet::getClassTypeId())
237 // {
238 // std::cout << "> We found a line" << std::endl;
239 // SoVRMLIndexedLineSet * line_set;
240 // line_set = (SoVRMLIndexedLineSet*)child2list->get(j);
241 // extractLines(line_set);
242 // }
243  }
244  sc->bound.nbr++;
245  ifsToBound (&(sc->bound.ptr[iterShapes]), ifs_list);
246  destroyIfs(ifs_list);
247  iterShapes++;
248  }
249  }
250 
251  //if (factor != 1)
252  if (std::fabs(factor) > std::numeric_limits<double>::epsilon())
253  {
254  for (int i = 0; i < sc->bound.nbr; i++)
255  {
256  for (int j = 0; j < sc->bound.ptr[i].point.nbr; j++)
257  {
258  sc->bound.ptr[i].point.ptr[j].x = sc->bound.ptr[i].point.ptr[j].x*factor;
259  sc->bound.ptr[i].point.ptr[j].y = sc->bound.ptr[i].point.ptr[j].y*factor;
260  sc->bound.ptr[i].point.ptr[j].z = sc->bound.ptr[i].point.ptr[j].z*factor;
261  }
262  }
263  }
264 }
265 
266 
267 void
268 extractFaces(SoVRMLIndexedFaceSet* face_set, indexFaceSet *ifs)
269 {
270 // vpList<vpPoint> pointList;
271 // pointList.kill();
272  SoVRMLCoordinate *coord = (SoVRMLCoordinate *)(face_set->coord.getValue());
273  int coordSize = coord->point.getNum();
274 
275  ifs->nbPt = coordSize;
276  for (int i = 0; i < coordSize; i++)
277  {
278  SbVec3f point(0,0,0);
279  point[0]=coord->point[i].getValue()[0];
280  point[1]=coord->point[i].getValue()[1];
281  point[2]=coord->point[i].getValue()[2];
282  vpPoint pt(point[0],point[1],point[2]);
283  ifs->pt.push_back(pt);
284  }
285 
286  SoMFInt32 indexList = face_set->coordIndex;
287  int indexListSize = indexList.getNum();
288 
289  ifs->nbIndex = indexListSize;
290  for (int i = 0; i < indexListSize; i++)
291  {
292  int index = face_set->coordIndex[i];
293  ifs->index.push_back(index);
294  }
295 }
296 
297 void ifsToBound (Bound* bptr, std::list<indexFaceSet*> &ifs_list)
298 {
299  int nbPt = 0;
300  for(std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it){
301  nbPt += (*it)->nbPt;
302  }
303  bptr->point.nbr = (Index)nbPt;
304  bptr->point.ptr = (Point3f *) malloc ((unsigned int)nbPt * sizeof (Point3f));
305 
306  ifs_list.front();
307  unsigned int iter = 0;
308  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
309  {
310  indexFaceSet* ifs = *it;
311  for (unsigned int j = 0; j < (unsigned int)ifs->nbPt; j++)
312  {
313  bptr->point.ptr[iter].x = (float)ifs->pt[j].get_oX();
314  bptr->point.ptr[iter].y = (float)ifs->pt[j].get_oY();
315  bptr->point.ptr[iter].z = (float)ifs->pt[j].get_oZ();
316  iter++;
317  }
318  }
319 
320  unsigned int nbFace = 0;
321  ifs_list.front();
322  std::list<int> indSize;
323  int indice = 0;
324  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
325  {
326  indexFaceSet* ifs = *it;
327  for (unsigned int j = 0; j < (unsigned int)ifs->nbIndex; j++)
328  {
329  if(ifs->index[j] == -1)
330  {
331  nbFace++;
332  indSize.push_back(indice);
333  indice = 0;
334  }
335  else indice++;
336  }
337  }
338 
339  bptr->face.nbr = (Index)nbFace;
340  bptr->face.ptr = (Face *) malloc (nbFace * sizeof (Face));
341 
342 
343  std::list<int>::const_iterator iter_indSize = indSize.begin();
344  for (unsigned int i = 0; i < indSize.size(); i++)
345  {
346  bptr->face.ptr[i].vertex.nbr = (Index)*iter_indSize;
347  bptr->face.ptr[i].vertex.ptr = (Index *) malloc ((unsigned int)*iter_indSize * sizeof (Index));
348  ++iter_indSize;
349  }
350 
351  int offset = 0;
352  indice = 0;
353  for (std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it)
354  {
355  indexFaceSet* ifs = *it;
356  iter = 0;
357  for (unsigned int j = 0; j < (unsigned int)ifs->nbIndex; j++)
358  {
359  if(ifs->index[j] != -1)
360  {
361  bptr->face.ptr[indice].vertex.ptr[iter] = (Index)(ifs->index[j] + offset);
362  iter++;
363  }
364  else
365  {
366  iter = 0;
367  indice++;
368  }
369  }
370  offset += ifs->nbPt;
371  }
372 }
373 
374 void destroyIfs(std::list<indexFaceSet*> &ifs_list)
375 {
376  for(std::list<indexFaceSet*>::const_iterator it=ifs_list.begin(); it!=ifs_list.end(); ++it){
377  delete *it;
378  }
379  ifs_list.clear();
380 }
381 #else
382 void set_scene_wrl (const char* /*str*/, Bound_scene* /*sc*/, float /*factor*/)
383 {
384 }
385 #endif
386 
387 
388 /*
389  Convert the matrix format to deal with the one in the simulator
390 */
391 void vp2jlc_matrix (const vpHomogeneousMatrix vpM, Matrix &jlcM)
392 {
393  for (unsigned int i = 0; i < 4; i++) {
394  for (unsigned int j = 0; j < 4; j++)
395  jlcM[j][i] = (float)vpM[i][j];
396  }
397 }
398 
399 /*
400  Copy the scene corresponding to the registeresd parameters in the image.
401 */
402 void
403 vpWireFrameSimulator::display_scene(Matrix mat, Bound_scene &sc, const vpImage<vpRGBa> &I, const vpColor &color)
404 {
405  // extern Bound *clipping_Bound ();
406  Bound *bp, *bend;
407  Bound *clip; /* surface apres clipping */
408  Byte b = (Byte) *get_rfstack ();
409  Matrix m;
410 
411  //bcopy ((char *) mat, (char *) m, sizeof (Matrix));
412  memmove((char *) m, (char *) mat, sizeof (Matrix));
413  View_to_Matrix (get_vwstack (), *(get_tmstack ()));
414  postmult_matrix (m, *(get_tmstack ()));
415  bp = sc.bound.ptr;
416  bend = bp + sc.bound.nbr;
417  for (; bp < bend; bp++)
418  {
419  if ((clip = clipping_Bound (bp, m)) != NULL)
420  {
421  Face *fp = clip->face.ptr;
422  Face *fend = fp + clip->face.nbr;
423 
424  set_Bound_face_display (clip, b); //regarde si is_visible
425 
426  point_3D_2D (clip->point.ptr, clip->point.nbr,(int)I.getWidth(),(int)I.getHeight(),point2i);
427  for (; fp < fend; fp++)
428  {
429  if (fp->is_visible)
430  {
431  wireframe_Face (fp, point2i);
432  Point2i *pt = listpoint2i;
433  for (int i = 1; i < fp->vertex.nbr; i++)
434  {
435  vpDisplay::displayLine(I,vpImagePoint((pt)->y,(pt)->x),vpImagePoint((pt+1)->y,(pt+1)->x),color,thickness_);
436  pt++;
437  }
438  if (fp->vertex.nbr > 2)
439  {
440  vpDisplay::displayLine(I,vpImagePoint((listpoint2i)->y,(listpoint2i)->x),vpImagePoint((pt)->y,(pt)->x),color,thickness_);
441  }
442  }
443  }
444  }
445  }
446 }
447 
448 /*
449  Copy the scene corresponding to the registeresd parameters in the image.
450 */
451 void
452 vpWireFrameSimulator::display_scene(Matrix mat, Bound_scene &sc, const vpImage<unsigned char> &I, const vpColor &color)
453 {
454  //extern Bound *clipping_Bound ();
455 
456  Bound *bp, *bend;
457  Bound *clip; /* surface apres clipping */
458  Byte b = (Byte) *get_rfstack ();
459  Matrix m;
460 
461  //bcopy ((char *) mat, (char *) m, sizeof (Matrix));
462  memmove((char *) m, (char *) mat, sizeof (Matrix));
463  View_to_Matrix (get_vwstack (), *(get_tmstack ()));
464  postmult_matrix (m, *(get_tmstack ()));
465  bp = sc.bound.ptr;
466  bend = bp + sc.bound.nbr;
467  for (; bp < bend; bp++)
468  {
469  if ((clip = clipping_Bound (bp, m)) != NULL)
470  {
471  Face *fp = clip->face.ptr;
472  Face *fend = fp + clip->face.nbr;
473 
474  set_Bound_face_display (clip, b); //regarde si is_visible
475 
476  point_3D_2D (clip->point.ptr, clip->point.nbr,(int)I.getWidth(),(int)I.getHeight(),point2i);
477  for (; fp < fend; fp++)
478  {
479  if (fp->is_visible)
480  {
481  wireframe_Face (fp, point2i);
482  Point2i *pt = listpoint2i;
483  for (int i = 1; i < fp->vertex.nbr; i++)
484  {
485  vpDisplay::displayLine(I,vpImagePoint((pt)->y,(pt)->x),vpImagePoint((pt+1)->y,(pt+1)->x),color,thickness_);
486  pt++;
487  }
488  if (fp->vertex.nbr > 2)
489  {
490  vpDisplay::displayLine(I,vpImagePoint((listpoint2i)->y,(listpoint2i)->x),vpImagePoint((pt)->y,(pt)->x),color,thickness_);
491  }
492  }
493  }
494  }
495  }
496 }
497 
498 /*************************************************************************************************************/
499 
509  : scene(), desiredScene(), camera(), objectImage(), fMo(), fMc(), camMf(),
510  refMo(), cMo(), cdMo(), object(PLATE), desiredObject(D_STANDARD),
511  camColor(vpColor::green), camTrajColor(vpColor::green), curColor(vpColor::blue),
512  desColor(vpColor::red), sceneInitialized(false), displayCameraTrajectory(true),
513  cameraTrajectory(), poseList(), fMoList(), nbrPtLimit(1000), old_iPr(), old_iPz(),
514  old_iPt(), blockedr(false), blockedz(false), blockedt(false), blocked(false),
515  camMf2(), f2Mf(), px_int(1), py_int(1), px_ext(1), py_ext(1), displayObject(false),
516  displayDesiredObject(false), displayCamera(false), displayImageSimulator(false),
517  cameraFactor(1.), camTrajType(CT_LINE), extCamChanged(false), rotz(), thickness_(1), scene_dir()
518 {
519  // set scene_dir from #define VISP_SCENE_DIR if it exists
520  // VISP_SCENES_DIR may contain multiple locations separated by ";"
521  std::vector<std::string> scene_dirs = vpIoTools::splitChain(std::string(VISP_SCENES_DIR), std::string(";"));
522  bool sceneDirExists = false;
523  for(size_t i=0; i < scene_dirs.size(); i++)
524  if (vpIoTools::checkDirectory(scene_dirs[i]) == true) { // directory exists
525  scene_dir = scene_dirs[i];
526  sceneDirExists = true;
527  break;
528  }
529  if (! sceneDirExists) {
530  try {
531  scene_dir = vpIoTools::getenv("VISP_SCENES_DIR");
532  std::cout << "The simulator uses data from VISP_SCENES_DIR=" << scene_dir << std::endl;
533  }
534  catch (...) {
535  std::cout << "Cannot get VISP_SCENES_DIR environment variable" << std::endl;
536  }
537  }
538 
539  open_display();
540  open_clipping();
541 
542  old_iPr = vpImagePoint(-1,-1);
543  old_iPz = vpImagePoint(-1,-1);
544  old_iPt = vpImagePoint(-1,-1);
545 
546  rotz.buildFrom(0,0,0,0,0,vpMath::rad(180));
547 
548  scene.name = NULL;
549  scene.bound.ptr = NULL;
550  scene.bound.nbr = 0;
551 
552  desiredScene.name = NULL;
553  desiredScene.bound.ptr = NULL;
554  desiredScene.bound.nbr = 0;
555 
556  camera.name = NULL;
557  camera.bound.ptr = NULL;
558  camera.bound.nbr = 0;
559 }
560 
561 
566 {
567  if(sceneInitialized)
568  {
569  if(displayObject)
570  free_Bound_scene (&(this->scene));
571  if(displayCamera){
572  free_Bound_scene (&(this->camera));
573  }
575  free_Bound_scene (&(this->desiredScene));
576  }
577  close_clipping();
578  close_display ();
579 
580  cameraTrajectory.clear();
581  poseList.clear();
582  fMoList.clear();
583 }
584 
585 
595 void
597 {
598  char name_cam[FILENAME_MAX];
599  char name[FILENAME_MAX];
600 
601  object = obj;
602  this->desiredObject = desired_object;
603 
604  const char *scene_dir_ = scene_dir.c_str();
605  if (strlen(scene_dir_) >= FILENAME_MAX) {
607  "Not enough memory to intialize the camera name"));
608  }
609 
610  strcpy(name_cam, scene_dir_);
611  if (desiredObject != D_TOOL)
612  {
613  strcat(name_cam,"/camera.bnd");
614  set_scene(name_cam,&camera,cameraFactor);
615  }
616  else
617  {
618  strcat(name_cam,"/tool.bnd");
619  set_scene(name_cam,&(this->camera),1.0);
620  }
621 
622  strcpy(name, scene_dir_);
623  switch (obj)
624  {
625  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
626  case CUBE : { strcat(name, "/cube.bnd"); break; }
627  case PLATE : { strcat(name, "/plate.bnd"); break; }
628  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
629  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
630  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
631  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
632  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
633  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
634  case ROAD : { strcat(name, "/road.bnd"); break; }
635  case TIRE : { strcat(name, "/circles2.bnd"); break; }
636  case PIPE : { strcat(name, "/pipe.bnd"); break; }
637  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
638  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
639  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
640  case PLAN: { strcat(name, "/plan.bnd"); break; }
641  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
642  }
643  set_scene(name,&(this->scene),1.0);
644 
645  scene_dir_ = scene_dir.c_str();
646  if (strlen(scene_dir_) >= FILENAME_MAX) {
648  "Not enough memory to intialize the desired object name"));
649  }
650 
651  switch (desiredObject)
652  {
653  case D_STANDARD : { break; }
654  case D_CIRCLE : {
655  strcpy(name, scene_dir_);
656  strcat(name, "/circle_sq2.bnd");
657  break; }
658  case D_TOOL : {
659  strcpy(name, scene_dir_);
660  strcat(name, "/tool.bnd");
661  break; }
662  }
663  set_scene(name, &(this->desiredScene), 1.0);
664 
665  if (obj == PIPE) load_rfstack(IS_INSIDE);
666  else add_rfstack(IS_BACK);
667 
668  add_vwstack ("start","depth", 0.0, 100.0);
669  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
670  add_vwstack ("start","type", PERSPECTIVE);
671 
672  sceneInitialized = true;
673  displayObject = true;
674  displayDesiredObject = true;
675  displayCamera = true;
676  displayImageSimulator = true;
677 }
678 
691 void
692 vpWireFrameSimulator::initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desired_object, const std::list<vpImageSimulator> &imObj)
693 {
694  initScene(obj, desired_object);
695  objectImage = imObj;
696  displayImageSimulator = true;
697 }
698 
699 
709 void
710 vpWireFrameSimulator::initScene(const char* obj, const char* desired_object)
711 {
712  char name_cam[FILENAME_MAX];
713  char name[FILENAME_MAX];
714 
715  object = THREE_PTS;
716  this->desiredObject = D_STANDARD;
717 
718  const char *scene_dir_ = scene_dir.c_str();
719  if (strlen(scene_dir_) >= FILENAME_MAX) {
721  "Not enough memory to intialize the camera name"));
722  }
723 
724  strcpy(name_cam, scene_dir_);
725  strcat(name_cam,"/camera.bnd");
726  set_scene(name_cam,&camera,cameraFactor);
727 
728  if (strlen(obj) >= FILENAME_MAX) {
730  "Not enough memory to intialize the name"));
731  }
732 
733  strcpy(name,obj);
734  Model_3D model;
735  model = getExtension(obj);
736  if (model == BND_MODEL)
737  set_scene(name,&(this->scene),1.0);
738  else if (model == WRL_MODEL)
739  set_scene_wrl(name,&(this->scene),1.0);
740  else if (model == UNKNOWN_MODEL)
741  {
742  vpERROR_TRACE("Unknown file extension for the 3D model");
743  }
744 
745  if (strlen(desired_object) >= FILENAME_MAX) {
747  "Not enough memory to intialize the camera name"));
748  }
749 
750  strcpy(name,desired_object);
751  model = getExtension(desired_object);
752  if (model == BND_MODEL)
753  set_scene(name,&(this->desiredScene),1.0);
754  else if (model == WRL_MODEL)
755  set_scene_wrl(name,&(this->desiredScene),1.0);
756  else if (model == UNKNOWN_MODEL)
757  {
758  vpERROR_TRACE("Unknown file extension for the 3D model");
759  }
760 
761  add_rfstack(IS_BACK);
762 
763  add_vwstack ("start","depth", 0.0, 100.0);
764  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
765  add_vwstack ("start","type", PERSPECTIVE);
766 
767  sceneInitialized = true;
768  displayObject = true;
769  displayDesiredObject = true;
770  displayCamera = true;
771 }
772 
785 void
786 vpWireFrameSimulator::initScene(const char* obj, const char* desired_object, const std::list<vpImageSimulator> &imObj)
787 {
788  initScene(obj, desired_object);
789  objectImage = imObj;
790  displayImageSimulator = true;
791 }
792 
801 void
803 {
804  char name_cam[FILENAME_MAX];
805  char name[FILENAME_MAX];
806 
807  object = obj;
808 
809  const char *scene_dir_ = scene_dir.c_str();
810  if (strlen(scene_dir_) >= FILENAME_MAX) {
812  "Not enough memory to intialize the camera name"));
813  }
814 
815  strcpy(name_cam, scene_dir_);
816  strcat(name_cam,"/camera.bnd");
817  set_scene(name_cam,&camera,cameraFactor);
818 
819  strcpy(name, scene_dir_);
820  switch (obj)
821  {
822  case THREE_PTS : {strcat(name,"/3pts.bnd"); break; }
823  case CUBE : { strcat(name, "/cube.bnd"); break; }
824  case PLATE : { strcat(name, "/plate.bnd"); break; }
825  case SMALL_PLATE : { strcat(name, "/plate_6cm.bnd"); break; }
826  case RECTANGLE : { strcat(name, "/rectangle.bnd"); break; }
827  case SQUARE_10CM : { strcat(name, "/square10cm.bnd"); break; }
828  case DIAMOND : { strcat(name, "/diamond.bnd"); break; }
829  case TRAPEZOID : { strcat(name, "/trapezoid.bnd"); break; }
830  case THREE_LINES : { strcat(name, "/line.bnd"); break; }
831  case ROAD : { strcat(name, "/road.bnd"); break; }
832  case TIRE : { strcat(name, "/circles2.bnd"); break; }
833  case PIPE : { strcat(name, "/pipe.bnd"); break; }
834  case CIRCLE : { strcat(name, "/circle.bnd"); break; }
835  case SPHERE : { strcat(name, "/sphere.bnd"); break; }
836  case CYLINDER : { strcat(name, "/cylinder.bnd"); break; }
837  case PLAN: { strcat(name, "/plan.bnd"); break; }
838  case POINT_CLOUD: { strcat(name, "/point_cloud.bnd"); break; }
839  }
840  set_scene(name,&(this->scene),1.0);
841 
842  if (obj == PIPE) load_rfstack(IS_INSIDE);
843  else add_rfstack(IS_BACK);
844 
845  add_vwstack ("start","depth", 0.0, 100.0);
846  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
847  add_vwstack ("start","type", PERSPECTIVE);
848 
849  sceneInitialized = true;
850  displayObject = true;
851  displayCamera = true;
852 
853  displayDesiredObject = false;
854  displayImageSimulator = false;
855 }
856 
868 void
869 vpWireFrameSimulator::initScene(const vpSceneObject &obj, const std::list<vpImageSimulator> &imObj)
870 {
871  initScene(obj);
872  objectImage = imObj;
873  displayImageSimulator = true;
874 }
875 
884 void
886 {
887  char name_cam[FILENAME_MAX];
888  char name[FILENAME_MAX];
889 
890  object = THREE_PTS;
891 
892  const char *scene_dir_ = scene_dir.c_str();
893  if (strlen(scene_dir_) >= FILENAME_MAX) {
895  "Not enough memory to intialize the camera name"));
896  }
897 
898  strcpy(name_cam, scene_dir_);
899  strcat(name_cam,"/camera.bnd");
900  set_scene(name_cam,&camera,cameraFactor);
901 
902  if (strlen(obj) >= FILENAME_MAX) {
904  "Not enough memory to intialize the name"));
905  }
906 
907  strcpy(name,obj);
908  Model_3D model;
909  model = getExtension(obj);
910  if (model == BND_MODEL)
911  set_scene(name,&(this->scene),1.0);
912  else if (model == WRL_MODEL)
913  set_scene_wrl(name,&(this->scene),1.0);
914  else if (model == UNKNOWN_MODEL)
915  {
916  vpERROR_TRACE("Unknown file extension for the 3D model");
917  }
918 
919  add_rfstack(IS_BACK);
920 
921  add_vwstack ("start","depth", 0.0, 100.0);
922  add_vwstack ("start","window", -0.1,0.1,-0.1,0.1);
923  add_vwstack ("start","type", PERSPECTIVE);
924 
925  sceneInitialized = true;
926  displayObject = true;
927  displayCamera = true;
928 }
929 
941 void
942 vpWireFrameSimulator::initScene(const char* obj, const std::list<vpImageSimulator> &imObj)
943 {
944  initScene(obj);
945  objectImage = imObj;
946  displayImageSimulator = true;
947 }
948 
956 void
958 {
959  if (!sceneInitialized)
960  throw(vpException(vpException::notInitialized,"The scene has to be initialized")) ;
961 
962  double u;
963  double v;
964  //if(px_int != 1 && py_int != 1)
965  // we assume px_int and py_int > 0
966  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
967  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
968  {
969  u = (double)I.getWidth()/(2*px_int);
970  v = (double)I.getHeight()/(2*py_int);
971  }
972  else
973  {
974  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
975  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
976  }
977 
978  float o44c[4][4],o44cd[4][4],x,y,z;
979  Matrix id = IDENTITY_MATRIX;
980 
981  vp2jlc_matrix(cMo.inverse(),o44c);
982  vp2jlc_matrix(cdMo.inverse(),o44cd);
983 
985  {
986  I = 255;
987 
988  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
989  vpImageSimulator* imSim = &(*it);
990  imSim->setCameraPosition(rotz*cMo);
992  }
993 
994  if (I.display != NULL)
996  }
997 
998  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
999  x = o44c[2][0] + o44c[3][0];
1000  y = o44c[2][1] + o44c[3][1];
1001  z = o44c[2][2] + o44c[3][2];
1002  add_vwstack ("start","vrp", x,y,z);
1003  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1004  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1005  add_vwstack ("start","window", -u, u, -v, v);
1006  if (displayObject)
1007  display_scene(id,this->scene,I, curColor);
1008 
1009 
1010  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1011  x = o44cd[2][0] + o44cd[3][0];
1012  y = o44cd[2][1] + o44cd[3][1];
1013  z = o44cd[2][2] + o44cd[3][2];
1014  add_vwstack ("start","vrp", x,y,z);
1015  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1016  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1017  add_vwstack ("start","window", -u, u, -v, v);
1019  {
1021  else display_scene(id,desiredScene,I, desColor);
1022  }
1023 }
1024 
1025 
1034 void
1036 {
1037  bool changed = false;
1038  vpHomogeneousMatrix displacement = navigation(I,changed);
1039 
1040  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1041  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1042  camMf2 = camMf2*displacement;
1043 
1044  f2Mf = camMf2.inverse()*camMf;
1045 
1046  camMf = camMf2* displacement * f2Mf;
1047 
1048  double u;
1049  double v;
1050  //if(px_ext != 1 && py_ext != 1)
1051  // we assume px_ext and py_ext > 0
1052  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1053  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1054  {
1055  u = (double)I.getWidth()/(2*px_ext);
1056  v = (double)I.getHeight()/(2*py_ext);
1057  }
1058  else
1059  {
1060  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1061  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1062  }
1063 
1064  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1065 
1066  vp2jlc_matrix(camMf.inverse(),w44cext);
1067  vp2jlc_matrix(fMc,w44c);
1068  vp2jlc_matrix(fMo,w44o);
1069 
1070 
1071  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1072  x = w44cext[2][0] + w44cext[3][0];
1073  y = w44cext[2][1] + w44cext[3][1];
1074  z = w44cext[2][2] + w44cext[3][2];
1075  add_vwstack ("start","vrp", x,y,z);
1076  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1077  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1078  add_vwstack ("start","window", -u, u, -v, v);
1079  if ((object == CUBE) || (object == SPHERE))
1080  {
1081  add_vwstack ("start","type", PERSPECTIVE);
1082  }
1083 
1085  {
1086  I = 255;
1087 
1088  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1089  vpImageSimulator* imSim = &(*it);
1090  imSim->setCameraPosition(rotz*camMf*fMo);
1092  }
1093 
1094  if (I.display != NULL)
1095  vpDisplay::display(I);
1096  }
1097 
1098  if (displayObject)
1099  display_scene(w44o,this->scene,I, curColor);
1100 
1101  if (displayCamera)
1102  display_scene(w44c,camera, I, camColor);
1103 
1105  {
1106  vpImagePoint iP;
1107  vpImagePoint iP_1;
1108  poseList.push_back(cMo);
1109  fMoList.push_back(fMo);
1110 
1111  int iter = 0;
1112 
1113  if (changed || extCamChanged)
1114  {
1115  cameraTrajectory.clear();
1116  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1117  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1118 
1119  while ((iter_poseList != poseList.end()) && (iter_fMoList != fMoList.end()))
1120  {
1121  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1122  cameraTrajectory.push_back(iP);
1123  if (camTrajType == CT_LINE)
1124  {
1125  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor, thickness_);
1126  }
1127  else if (camTrajType == CT_POINT)
1129  ++iter_poseList;
1130  ++iter_fMoList;
1131  iter++;
1132  iP_1 = iP;
1133  }
1134  extCamChanged = false;
1135  }
1136  else
1137  {
1138  iP = projectCameraTrajectory(I, cMo, fMo);
1139  cameraTrajectory.push_back(iP);
1140 
1141  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1142  if (camTrajType == CT_LINE)
1143  {
1144  if (iter != 0) vpDisplay::displayLine(I, iP_1, *it, camTrajColor, thickness_);
1145  }
1146  else if (camTrajType == CT_POINT)
1148  iter++;
1149  iP_1 = *it;
1150  }
1151  }
1152 
1153  if (poseList.size() > nbrPtLimit)
1154  {
1155  poseList.pop_front();
1156  }
1157  if (fMoList.size() > nbrPtLimit)
1158  {
1159  fMoList.pop_front();
1160  }
1161  if (cameraTrajectory.size() > nbrPtLimit)
1162  {
1163  cameraTrajectory.pop_front();
1164  }
1165  }
1166 }
1167 
1168 
1177 void
1179 {
1180  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1181 
1182  vpHomogeneousMatrix camMft = rotz * cam_Mf;
1183 
1184  double u;
1185  double v;
1186  //if(px_ext != 1 && py_ext != 1)
1187  // we assume px_ext and py_ext > 0
1188  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1189  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1190  {
1191  u = (double)I.getWidth()/(2*px_ext);
1192  v = (double)I.getHeight()/(2*py_ext);
1193  }
1194  else
1195  {
1196  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1197  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1198  }
1199 
1200  vp2jlc_matrix(camMft.inverse(),w44cext);
1201  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1202  vp2jlc_matrix(fMo,w44o);
1203 
1204  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1205  x = w44cext[2][0] + w44cext[3][0];
1206  y = w44cext[2][1] + w44cext[3][1];
1207  z = w44cext[2][2] + w44cext[3][2];
1208  add_vwstack ("start","vrp", x,y,z);
1209  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1210  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1211  add_vwstack ("start","window", -u, u, -v, v);
1212 
1214  {
1215  I = 255;
1216 
1217  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1218  vpImageSimulator* imSim = &(*it);
1219  imSim->setCameraPosition(rotz*cam_Mf*fMo);
1221  }
1222 
1223  if (I.display != NULL)
1224  vpDisplay::display(I);
1225  }
1226 
1227  if (displayObject)
1228  display_scene(w44o,this->scene,I, curColor);
1229  if (displayCamera)
1230  display_scene(w44c,camera, I, camColor);
1231 }
1232 
1233 
1241 void
1243 {
1244  if (!sceneInitialized)
1245  throw(vpException(vpException::notInitialized,"The scene has to be initialized")) ;
1246 
1247  double u;
1248  double v;
1249  //if(px_int != 1 && py_int != 1)
1250  // we assume px_int and py_int > 0
1251  if( (std::fabs(px_int-1.) > vpMath::maximum(px_int,1.)*std::numeric_limits<double>::epsilon())
1252  && (std::fabs(py_int-1) > vpMath::maximum(py_int,1.)*std::numeric_limits<double>::epsilon()))
1253  {
1254  u = (double)I.getWidth()/(2*px_int);
1255  v = (double)I.getHeight()/(2*py_int);
1256  }
1257  else
1258  {
1259  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1260  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1261  }
1262 
1263  float o44c[4][4],o44cd[4][4],x,y,z;
1264  Matrix id = IDENTITY_MATRIX;
1265 
1266  vp2jlc_matrix(cMo.inverse(),o44c);
1267  vp2jlc_matrix(cdMo.inverse(),o44cd);
1268 
1270  {
1271  I = 255;
1272 
1273  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1274  vpImageSimulator* imSim = &(*it);
1275  imSim->setCameraPosition(rotz*camMf*fMo);
1277  }
1278 
1279  if (I.display != NULL)
1280  vpDisplay::display(I);
1281  }
1282 
1283  add_vwstack ("start","cop", o44c[3][0],o44c[3][1],o44c[3][2]);
1284  x = o44c[2][0] + o44c[3][0];
1285  y = o44c[2][1] + o44c[3][1];
1286  z = o44c[2][2] + o44c[3][2];
1287  add_vwstack ("start","vrp", x,y,z);
1288  add_vwstack ("start","vpn", o44c[2][0],o44c[2][1],o44c[2][2]);
1289  add_vwstack ("start","vup", o44c[1][0],o44c[1][1],o44c[1][2]);
1290  add_vwstack ("start","window", -u, u, -v, v);
1291  if (displayObject)
1292  display_scene(id,this->scene,I, curColor);
1293 
1294 
1295  add_vwstack ("start","cop", o44cd[3][0],o44cd[3][1],o44cd[3][2]);
1296  x = o44cd[2][0] + o44cd[3][0];
1297  y = o44cd[2][1] + o44cd[3][1];
1298  z = o44cd[2][2] + o44cd[3][2];
1299  add_vwstack ("start","vrp", x,y,z);
1300  add_vwstack ("start","vpn", o44cd[2][0],o44cd[2][1],o44cd[2][2]);
1301  add_vwstack ("start","vup", o44cd[1][0],o44cd[1][1],o44cd[1][2]);
1302  add_vwstack ("start","window", -u, u, -v, v);
1304  {
1306  else display_scene(id,desiredScene,I, desColor);
1307  }
1308 }
1309 
1310 
1319 void
1321 {
1322  bool changed = false;
1323  vpHomogeneousMatrix displacement = navigation(I,changed);
1324 
1325  //if (displacement[2][3] != 0 /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1326  if (std::fabs(displacement[2][3]) > std::numeric_limits<double>::epsilon() /*|| rotation[0][3] != 0 || rotation[1][3] != 0*/)
1327  camMf2 = camMf2*displacement;
1328 
1329  f2Mf = camMf2.inverse()*camMf;
1330 
1331  camMf = camMf2* displacement * f2Mf;
1332 
1333  double u;
1334  double v;
1335  //if(px_ext != 1 && py_ext != 1)
1336  // we assume px_ext and py_ext > 0
1337  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1338  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1339  {
1340  u = (double)I.getWidth()/(2*px_ext);
1341  v = (double)I.getHeight()/(2*py_ext);
1342  }
1343  else
1344  {
1345  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1346  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1347  }
1348 
1349  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1350 
1351  vp2jlc_matrix(camMf.inverse(),w44cext);
1352  vp2jlc_matrix(fMc,w44c);
1353  vp2jlc_matrix(fMo,w44o);
1354 
1355 
1356  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1357  x = w44cext[2][0] + w44cext[3][0];
1358  y = w44cext[2][1] + w44cext[3][1];
1359  z = w44cext[2][2] + w44cext[3][2];
1360  add_vwstack ("start","vrp", x,y,z);
1361  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1362  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1363  add_vwstack ("start","window", -u, u, -v, v);
1364  if ((object == CUBE) || (object == SPHERE))
1365  {
1366  add_vwstack ("start","type", PERSPECTIVE);
1367  }
1368 
1370  {
1371  I = 255;
1372  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1373  vpImageSimulator* imSim = &(*it);
1374  imSim->setCameraPosition(rotz*camMf*fMo);
1376  }
1377  if (I.display != NULL)
1378  vpDisplay::display(I);
1379  }
1380 
1381  if (displayObject)
1382  display_scene(w44o,this->scene,I, curColor);
1383 
1384  if (displayCamera)
1385  display_scene(w44c,camera, I, camColor);
1386 
1388  {
1389  vpImagePoint iP;
1390  vpImagePoint iP_1;
1391  poseList.push_back(cMo);
1392  fMoList.push_back(fMo);
1393 
1394  int iter = 0;
1395 
1396  if (changed || extCamChanged)
1397  {
1398  cameraTrajectory.clear();
1399  std::list<vpHomogeneousMatrix>::const_iterator iter_poseList = poseList.begin();
1400  std::list<vpHomogeneousMatrix>::const_iterator iter_fMoList = fMoList.begin();
1401 
1402  while ((iter_poseList!=poseList.end()) && (iter_fMoList!=fMoList.end()) )
1403  {
1404  iP = projectCameraTrajectory(I, *iter_poseList, *iter_fMoList);
1405  cameraTrajectory.push_back(iP);
1406  //vpDisplay::displayPoint(I,cameraTrajectory.value(),vpColor::green);
1407  if (camTrajType == CT_LINE)
1408  {
1409  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor, thickness_);
1410  }
1411  else if (camTrajType == CT_POINT)
1413  ++iter_poseList;
1414  ++iter_fMoList;
1415  iter++;
1416  iP_1 = iP;
1417  }
1418  extCamChanged = false;
1419  }
1420  else
1421  {
1422  iP = projectCameraTrajectory(I, cMo,fMo);
1423  cameraTrajectory.push_back(iP);
1424 
1425  for(std::list<vpImagePoint>::const_iterator it=cameraTrajectory.begin(); it!=cameraTrajectory.end(); ++it){
1426  if (camTrajType == CT_LINE){
1427  if (iter != 0) vpDisplay::displayLine(I,iP_1,*it,camTrajColor, thickness_);
1428  }
1429  else if(camTrajType == CT_POINT)
1431  iter++;
1432  iP_1 = *it;
1433  }
1434  }
1435 
1436  if (poseList.size() > nbrPtLimit)
1437  {
1438  poseList.pop_front();
1439  }
1440  if (fMoList.size() > nbrPtLimit)
1441  {
1442  fMoList.pop_front();
1443  }
1444  if (cameraTrajectory.size() > nbrPtLimit)
1445  {
1446  cameraTrajectory.pop_front();
1447  }
1448  }
1449 }
1450 
1451 
1460 void
1462 {
1463  float w44o[4][4],w44cext[4][4],w44c[4][4],x,y,z;
1464 
1465  vpHomogeneousMatrix camMft = rotz * cam_Mf;
1466 
1467  double u;
1468  double v;
1469  //if(px_ext != 1 && py_ext != 1)
1470  // we assume px_ext and py_ext > 0
1471  if( (std::fabs(px_ext-1.) > vpMath::maximum(px_ext,1.)*std::numeric_limits<double>::epsilon())
1472  && (std::fabs(py_ext-1) > vpMath::maximum(py_ext,1.)*std::numeric_limits<double>::epsilon()))
1473  {
1474  u = (double)I.getWidth()/(2*px_ext);
1475  v = (double)I.getHeight()/(2*py_ext);
1476  }
1477  else
1478  {
1479  u = (double)I.getWidth()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1480  v = (double)I.getHeight()/(vpMath::minimum(I.getWidth(),I.getHeight()));
1481  }
1482 
1483  vp2jlc_matrix(camMft.inverse(),w44cext);
1484  vp2jlc_matrix(fMo*cMo.inverse(),w44c);
1485  vp2jlc_matrix(fMo,w44o);
1486 
1487  add_vwstack ("start","cop", w44cext[3][0],w44cext[3][1],w44cext[3][2]);
1488  x = w44cext[2][0] + w44cext[3][0];
1489  y = w44cext[2][1] + w44cext[3][1];
1490  z = w44cext[2][2] + w44cext[3][2];
1491  add_vwstack ("start","vrp", x,y,z);
1492  add_vwstack ("start","vpn", w44cext[2][0],w44cext[2][1],w44cext[2][2]);
1493  add_vwstack ("start","vup", w44cext[1][0],w44cext[1][1],w44cext[1][2]);
1494  add_vwstack ("start","window", -u, u, -v, v);
1495 
1497  {
1498  I = 255;
1499  for(std::list<vpImageSimulator>::iterator it=objectImage.begin(); it!=objectImage.end(); ++it){
1500  vpImageSimulator* imSim = &(*it);
1501  imSim->setCameraPosition(rotz*cam_Mf*fMo);
1503  }
1504  if (I.display != NULL)
1505  vpDisplay::display(I);
1506  }
1507 
1508  if (displayObject)
1509  display_scene(w44o,this->scene,I, curColor);
1510  if (displayCamera)
1511  display_scene(w44c,camera, I, camColor);
1512 }
1513 
1524 void
1525 vpWireFrameSimulator::displayTrajectory (const vpImage<unsigned char> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1526  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1527 {
1528  if (list_cMo.size() != list_fMo.size())
1529  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1530 
1531  vpImagePoint iP;
1532  vpImagePoint iP_1;
1533  int iter = 0;
1534 
1535  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1536  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1537 
1538  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1539  {
1540  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1541  if (camTrajType == CT_LINE)
1542  {
1543  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor,thickness_);
1544  }
1545  else if (camTrajType == CT_POINT)
1547  ++it_cMo;
1548  ++it_fMo;
1549  iter++;
1550  iP_1 = iP;
1551  }
1552 }
1553 
1564 void
1565 vpWireFrameSimulator::displayTrajectory (const vpImage<vpRGBa> &I, const std::list<vpHomogeneousMatrix> &list_cMo,
1566  const std::list<vpHomogeneousMatrix> &list_fMo, const vpHomogeneousMatrix &cMf)
1567 {
1568  if (list_cMo.size() != list_fMo.size())
1569  throw(vpException(vpException::dimensionError ,"The two lists must have the same size")) ;
1570 
1571  vpImagePoint iP;
1572  vpImagePoint iP_1;
1573  int iter = 0;
1574 
1575  std::list<vpHomogeneousMatrix>::const_iterator it_cMo = list_cMo.begin();
1576  std::list<vpHomogeneousMatrix>::const_iterator it_fMo = list_fMo.begin();
1577 
1578  while ((it_cMo != list_cMo.end()) && (it_fMo != list_fMo.end()))
1579  {
1580  iP = projectCameraTrajectory(I, rotz * (*it_cMo), (*it_fMo), rotz * cMf);
1581  if (camTrajType == CT_LINE)
1582  {
1583  if (iter != 0) vpDisplay::displayLine(I,iP_1,iP,camTrajColor,thickness_);
1584  }
1585  else if (camTrajType == CT_POINT)
1587  ++it_cMo;
1588  ++it_fMo;
1589  iter++;
1590  iP_1 = iP;
1591  }
1592 }
1593 
1594 
1600 {
1601  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1602  vpImagePoint iP;
1603  vpImagePoint trash;
1604  bool clicked = false;
1605  bool clickedUp = false;
1607 
1608  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1609  changed = false;
1610 
1611  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1612 
1613  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1614 
1615  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1616 
1617  if(clicked)
1618  {
1619  if (b == vpMouseButton::button1) blockedr = true;
1620  if (b == vpMouseButton::button2) blockedz = true;
1621  if (b == vpMouseButton::button3) blockedt = true;
1622  blocked = true;
1623  }
1624  if(clickedUp)
1625  {
1626  if (b == vpMouseButton::button1)
1627  {
1628  old_iPr = vpImagePoint(-1,-1);
1629  blockedr = false;
1630  }
1631  if (b == vpMouseButton::button2)
1632  {
1633  old_iPz = vpImagePoint(-1,-1);
1634  blockedz = false;
1635  }
1636  if (b == vpMouseButton::button3)
1637  {
1638  old_iPt = vpImagePoint(-1,-1);
1639  blockedt = false;
1640  }
1641  if (!(blockedr || blockedz || blockedt))
1642  {
1643  blocked = false;
1644  while (vpDisplay::getClick(I,trash,b,false)) {};
1645  }
1646  }
1647 
1649 
1650  double anglei = 0;
1651  double anglej = 0;
1652 
1653  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1654  {
1655  double diffi = iP.get_i() - old_iPr.get_i();
1656  double diffj = iP.get_j() - old_iPr.get_j();
1657  //cout << "delta :" << diffj << endl;;
1658  anglei = diffi*360/width;
1659  anglej = diffj*360/width;
1660  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1661  changed = true;
1662  }
1663 
1664  if (blockedr) old_iPr = iP;
1665 
1666  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1667  {
1668  double diffi = iP.get_i() - old_iPz.get_i();
1669  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1670  changed = true;
1671  }
1672 
1673  if (blockedz) old_iPz = iP;
1674 
1675  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1676  {
1677  double diffi = iP.get_i() - old_iPt.get_i();
1678  double diffj = iP.get_j() - old_iPt.get_j();
1679  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1680  changed = true;
1681  }
1682 
1683  if (blockedt) old_iPt = iP;
1684 
1685  return mov;
1686 }
1687 
1688 
1694 {
1695  double width = vpMath::minimum(I.getWidth(),I.getHeight());
1696  vpImagePoint iP;
1697  vpImagePoint trash;
1698  bool clicked = false;
1699  bool clickedUp = false;
1701 
1702  vpHomogeneousMatrix mov(0,0,0,0,0,0);
1703  changed = false;
1704 
1705  //if(!blocked) vpDisplay::getClickUp(I,trash, b,false);
1706 
1707  if(!blocked)clicked = vpDisplay::getClick(I,trash,b,false);
1708 
1709  if(blocked)clickedUp = vpDisplay::getClickUp(I,trash, b,false);
1710 
1711  if(clicked)
1712  {
1713  if (b == vpMouseButton::button1) blockedr = true;
1714  if (b == vpMouseButton::button2) blockedz = true;
1715  if (b == vpMouseButton::button3) blockedt = true;
1716  blocked = true;
1717  }
1718  if(clickedUp)
1719  {
1720  if (b == vpMouseButton::button1)
1721  {
1722  old_iPr = vpImagePoint(-1,-1);
1723  blockedr = false;
1724  }
1725  if (b == vpMouseButton::button2)
1726  {
1727  old_iPz = vpImagePoint(-1,-1);
1728  blockedz = false;
1729  }
1730  if (b == vpMouseButton::button3)
1731  {
1732  old_iPt = vpImagePoint(-1,-1);
1733  blockedt = false;
1734  }
1735  if (!(blockedr || blockedz || blockedt))
1736  {
1737  blocked = false;
1738  while (vpDisplay::getClick(I,trash,b,false)) {};
1739  }
1740  }
1741 
1743 
1744  //std::cout << "point : " << iP << std::endl;
1745 
1746  double anglei = 0;
1747  double anglej = 0;
1748 
1749  if (old_iPr != vpImagePoint(-1,-1) && blockedr)
1750  {
1751  double diffi = iP.get_i() - old_iPr.get_i();
1752  double diffj = iP.get_j() - old_iPr.get_j();
1753  //cout << "delta :" << diffj << endl;;
1754  anglei = diffi*360/width;
1755  anglej = diffj*360/width;
1756  mov.buildFrom(0,0,0,vpMath::rad(-anglei),vpMath::rad(anglej),0);
1757  changed = true;
1758  }
1759 
1760  if (blockedr) old_iPr = iP;
1761 
1762  if (old_iPz != vpImagePoint(-1,-1) && blockedz)
1763  {
1764  double diffi = iP.get_i() - old_iPz.get_i();
1765  mov.buildFrom(0,0,diffi*0.01,0,0,0);
1766  changed = true;
1767  }
1768 
1769  if (blockedz) old_iPz = iP;
1770 
1771  if (old_iPt != vpImagePoint(-1,-1) && blockedt)
1772  {
1773  double diffi = iP.get_i() - old_iPt.get_i();
1774  double diffj = iP.get_j() - old_iPt.get_j();
1775  mov.buildFrom(diffj*0.01,diffi*0.01,0,0,0,0);
1776  changed = true;
1777  }
1778 
1779  if (blockedt) old_iPt = iP;
1780 
1781  return mov;
1782 }
1783 
1789  const vpHomogeneousMatrix &fMo_)
1790 {
1791  vpPoint point;
1792  point.setWorldCoordinates(0,0,0);
1793 
1794  point.track(rotz*(camMf*fMo_*cMo_.inverse())) ;
1795 
1796  vpImagePoint iP;
1797 
1799 
1800  return iP;
1801 }
1802 
1808  const vpHomogeneousMatrix &cMo_,
1809  const vpHomogeneousMatrix &fMo_)
1810 {
1811  vpPoint point;
1812  point.setWorldCoordinates(0,0,0);
1813 
1814  point.track(rotz*(camMf*fMo_*cMo_.inverse())) ;
1815 
1816  vpImagePoint iP;
1817 
1819 
1820  return iP;
1821 }
1822 
1828  const vpHomogeneousMatrix &fMo_, const vpHomogeneousMatrix &cMf)
1829 {
1830  vpPoint point;
1831  point.setWorldCoordinates(0,0,0);
1832 
1833  point.track(rotz*(cMf*fMo_*cMo_.inverse())) ;
1834 
1835  vpImagePoint iP;
1836 
1838 
1839  return iP;
1840 }
1841 
1847  const vpHomogeneousMatrix &fMo_, const vpHomogeneousMatrix &cMf)
1848 {
1849  vpPoint point;
1850  point.setWorldCoordinates(0,0,0);
1851 
1852  point.track(rotz*(cMf*fMo_*cMo_.inverse())) ;
1853 
1854  vpImagePoint iP;
1855 
1857 
1858  return iP;
1859 }
1860 
The object displayed at the desired position is the same than the scene object defined in vpSceneObje...
vpDisplay * display
Definition: vpImage.h:117
A cylinder of 80cm length and 10cm radius.
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:190
unsigned int getWidth() const
Definition: vpImage.h:161
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.
Implementation of an homogeneous matrix and operations on such kind of matrices.
A plate with 8 points at coordinates (0.05,0,0), (0.15,0.05,0), (0.2,0.2,0), (-0.05,0.2,0), (-0.15,-0.1,0), (-0.1,-0.1,0), (-0.05,0.05,0) and (0.5,0,0). ach point is represented by a circle with 2cm radius.
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
A 40cm by 40cm plate with 4 points at coordinates (-0.1,-0.1,0), (0.1,-0.1,0), (0.1,0.1,0), (0.1,0.1,0). Each point is represented by a circle with 2cm radius.
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Point coordinates conversion from normalized coordinates in meter to pixel coordinates ...
#define vpERROR_TRACE
Definition: vpDebug.h:391
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
A cylindrical tool is attached to the camera.
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:224
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:73
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.cpp:458
A 40cm by 40cm plate with 4 points at coordinates (-0.05,0.05,0), (0.05,0.05,0), (0.05,-0.05,0), (-0.05,-0.05,0). Each point is represented by a circle with 2cm radius.
vpHomogeneousMatrix fMo
double get_j() const
Definition: vpImagePoint.h:201
std::list< vpHomogeneousMatrix > fMoList
static const vpColor red
Definition: vpColor.h:163
Class that defines what is a point.
Definition: vpPoint.h:59
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:141
4 points at coordinates (-0.03,-0.03,0), (0.03,-0.03,0), (0.03,0.03,0), (0.03,0.03,0). Each point is represented by a circle with 1cm radius.
vpCameraParameters getExternalCameraParameters(const vpImage< unsigned char > &I) const
void setCameraPosition(const vpHomogeneousMatrix &cMt)
virtual bool getPointerPosition(vpImagePoint &ip)=0
The object displayed at the desired position is a circle.
vpHomogeneousMatrix f2Mf
A 40cm by 40cm plate with 4 points at coordinates (-0.025,-0.05,0), (-0.075,0.05,0), (0.075,0.05,0), (0.025,-0.05,0). Each point is represented by a circle with 2cm radius.
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
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.cpp:456
static std::vector< std::string > splitChain(const std::string &chain, const std::string &sep)
Definition: vpIoTools.cpp:1477
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:152
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)
vpCameraParameters getInternalCameraParameters(const vpImage< unsigned char > &I) const
static double rad(double deg)
Definition: vpMath.h:104
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
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
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:152
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:88
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
std::list< vpHomogeneousMatrix > poseList
vpHomogeneousMatrix camMf2