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