48 #include <visp/vpConfig.h>
50 #ifdef VISP_HAVE_COIN_AND_GUI
52 #include <visp/vpSimulator.h>
53 #include <visp/vpTime.h>
55 #include <visp/vpImage.h>
56 #include <visp/vpImageIo.h>
59 #include <Inventor/nodes/SoCone.h>
60 #include <Inventor/nodes/SoCylinder.h>
61 #include <Inventor/nodes/SoPointLight.h>
62 #include <Inventor/nodes/SoCoordinate3.h>
63 #include <Inventor/nodes/SoIndexedFaceSet.h>
64 #include <Inventor/nodes/SoTranslation.h>
65 #include <Inventor/nodes/SoScale.h>
66 #include <Inventor/nodes/SoRotationXYZ.h>
68 #include <Inventor/nodes/SoDirectionalLight.h>
69 #include <Inventor/nodes/SoMaterial.h>
70 #include <Inventor/nodes/SoDrawStyle.h>
71 #include <Inventor/nodes/SoEnvironment.h>
72 #include <Inventor/nodes/SoGroup.h>
73 #include <Inventor/actions/SoWriteAction.h>
80 static float pyramidVertexes [5][3] =
84 {-0.33f, -0.33f, 0.f},
91 static int32_t pyramidFaces[] =
93 0, 1, 2, 3, SO_END_FACE_INDEX,
95 0, 1, 4, SO_END_FACE_INDEX,
96 1, 2, 4, SO_END_FACE_INDEX,
97 2, 3, 4, SO_END_FACE_INDEX,
98 3, 0, 4, SO_END_FACE_INDEX,
106 SoSeparator *result =
new SoSeparator;
110 SoCoordinate3 *myCoords =
new SoCoordinate3;
111 myCoords->point.setValues(0, 5, pyramidVertexes);
112 result->addChild(myCoords);
115 SoIndexedFaceSet *myFaceSet =
new SoIndexedFaceSet;
116 myFaceSet->coordIndex.setValues (0, 21, (
const int32_t*)pyramidFaces);
117 result->addChild (myFaceSet);
119 result->unrefNoDelete();
130 createArrow (
float longueur,
131 float proportionFleche,
134 SoSeparator *fleche =
new SoSeparator;
137 SoTranslation *poseCylindre =
new SoTranslation;
138 SoCylinder *line =
new SoCylinder;
139 SoTranslation *posePointe =
new SoTranslation;
140 SoCone *pointe =
new SoCone;
142 float l_cylindre = longueur * ( 1 - proportionFleche);
143 float l_cone = longueur * proportionFleche;
144 float radius_cylindre = radius;
145 float radius_cone = radius * 5;
147 line->radius.setValue (radius_cylindre);
148 line->height.setValue (l_cylindre);
150 poseCylindre->translation.setValue (0, l_cylindre / 2, 0);
151 posePointe->translation.setValue (0.0, l_cylindre / 2 + l_cone / 2, 0);
153 pointe->bottomRadius.setValue (radius_cone);
154 pointe->height.setValue (l_cone);
157 fleche->addChild (poseCylindre);
158 fleche->addChild (line);
159 fleche->addChild (posePointe);
160 fleche->addChild (pointe);
170 #define LONGUEUR_FLECHE 1.0f
171 #define RAYON_FLECHE 0.002f
172 #define PROPORTION_FLECHE 0.1f
175 createFrame (
float longueurFleche = LONGUEUR_FLECHE ,
176 float proportionFleche = PROPORTION_FLECHE,
177 float radiusFleche = RAYON_FLECHE)
181 SoSeparator *frame =
new SoSeparator;
184 SoRotationXYZ *rotationY_X =
new SoRotationXYZ;
185 rotationY_X->axis = SoRotationXYZ::Z;
186 rotationY_X->angle.setValue ((
float)(- M_PI / 2));
188 SoRotationXYZ *rotationX_Y =
new SoRotationXYZ;
189 rotationX_Y->axis = SoRotationXYZ::Z;
190 rotationX_Y->angle.setValue ((
float)(M_PI / 2));
192 SoRotationXYZ *rotationY_Z =
new SoRotationXYZ;
193 rotationY_Z->axis = SoRotationXYZ::X;
194 rotationY_Z->angle.setValue ((
float)(M_PI / 2));
196 SoMaterial *rouge =
new SoMaterial;
197 rouge->diffuseColor.setValue(1.0, 0.0, 0.0);
198 rouge->emissiveColor.setValue(0.5, 0.0, 0.0);
200 SoMaterial *vert =
new SoMaterial;
201 vert->diffuseColor.setValue(0.0, 1.0, 0.0);
202 vert->emissiveColor.setValue(0.0, 0.5, 0.0);
204 SoMaterial *bleu =
new SoMaterial;
205 bleu->diffuseColor.setValue(0.0, 0.0, 1.0);
206 bleu->emissiveColor.setValue(0.0, 0.0, 0.5);
208 SoSeparator *fleche = createArrow(longueurFleche,
212 frame->addChild (rouge);
213 frame->addChild (rotationY_X);
214 frame->addChild (fleche);
215 frame->addChild (vert);
216 frame->addChild (rotationX_Y);
217 frame->addChild (fleche);
218 frame->addChild (bleu);
219 frame->addChild (rotationY_Z);
220 frame->addChild (fleche);
222 frame-> unrefNoDelete ();
229 createCameraObject (
const float zoomFactor = 1.0)
233 SoSeparator * cam =
new SoSeparator;
236 SoMaterial *myMaterial =
new SoMaterial;
237 myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0);
238 myMaterial->emissiveColor.setValue(0.5, 0.0, 0.0);
240 SoScale *taille =
new SoScale;
242 float zoom = 0.1f * zoomFactor;
243 taille->scaleFactor.setValue (zoom, zoom, zoom);
246 SoMaterial *couleurBlanc =
new SoMaterial;
247 couleurBlanc->diffuseColor.setValue(1.0, 1.0, 1.0);
248 couleurBlanc->emissiveColor.setValue(1.0, 1.0, 1.0);
249 SoDrawStyle * filDeFer =
new SoDrawStyle;
250 filDeFer->style.setValue (SoDrawStyle::LINES);
251 filDeFer->lineWidth.setValue (1);
253 SoSeparator * cone =
new SoSeparator;
255 cone->addChild (makePyramide());
256 cone->addChild (couleurBlanc);
257 cone->addChild (filDeFer);
258 cone->addChild (makePyramide());
259 cone->unrefNoDelete();
261 cam->addChild(myMaterial);
262 cam->addChild(taille);
264 cam->addChild(createFrame(2.0f,0.1f,0.01f));
304 #if defined(VISP_HAVE_SOWIN)
306 #elif defined(VISP_HAVE_SOQT)
308 #elif defined(VISP_HAVE_SOXT)
328 #if defined(VISP_HAVE_SOWIN)
330 #elif defined(VISP_HAVE_SOQT)
332 #elif defined(VISP_HAVE_SOXT)
335 mainWindowInitialized(false), typeImage(
vpSimulator::grayImage),
336 image_background(NULL), internalView(NULL), externalView(NULL),
337 mainThread(NULL), internal_width(0), internal_height(0),
338 external_width(0), external_height(0), scene(NULL), internalRoot(NULL),
339 externalRoot(NULL), internalCamera(NULL), externalCamera(NULL),
340 internalCameraPosition(NULL), extrenalCameraPosition(NULL), internalCameraObject(NULL),
341 zoomFactor(0.), cameraPositionInitialized(false), cMf(), internalCameraParameters(),
342 externalCameraParameters(), realtime(NULL), offScreenRenderer(NULL), bufferView(NULL),
363 this->
scene =
new SoSeparator;
392 SoSeparator * camera =
new SoSeparator;
401 SoCube *cube =
new SoCube ;
411 SoDB::enableRealTimeSensor(FALSE);
412 SoSceneManager::enableRealTimeUpdate(FALSE);
413 realtime = (SbTime *) SoDB::getGlobalField(
"realTime");
433 static bool firstTime =
true;
435 SoScale *taille =
new SoScale;
437 this->
scene->addChild(taille);
441 SoScale * taille = (SoScale*)this->
scene->getChild(0);
466 SoScale * taille = (SoScale*)this->
scene->getChild(index);
467 taille->scaleFactor.setValue (zoomFactor, zoomFactor, zoomFactor);
504 bufferView =
new unsigned char[3*width*height] ;
543 float px = (float)_cam.
get_px();
544 float py = (float)_cam.
get_py();
563 float px = (float)_cam.
get_px();
564 float py = (float)_cam.
get_py();
585 SbVec3f axis ;
float angle ;
586 orientation.getValue(axis,angle) ;
587 SbRotation rotation(axis,angle) ;
591 t = position.getValue() ;
594 matrix.setRotate(rotation) ;
598 rotX.setRotate (SbRotation (SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
599 matrix.multLeft (rotX);
600 for(
unsigned int i=0;i<4;i++)
601 for(
unsigned int j=0;j<4;j++)
602 fMc[j][i]=matrix[(
int)i][(int)j];
624 rotX.setRotate (SbRotation (SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
625 for(
unsigned int i=0;i<4;i++)
626 for(
unsigned int j=0;j<4;j++)
627 matrix[(
int)j][(int)i]=(
float)cMf[i][j];
630 matrix.multLeft (rotX);
631 rotCam.setValue(matrix);
636 internalCamera->position.setValue(matrix[3][0],matrix[3][1],matrix[3][2]);
639 rotX.setRotate (SbRotation (SbVec3f(-1.0f, 0.0f, 0.0f), (
float)M_PI));
640 matrix.multLeft (rotX);
641 rotCam.setValue(matrix);
674 timerSensorCallback(
void *data , SoSensor *)
694 SoTimerSensor * timer =
new SoTimerSensor(timerSensorCallback, (
void *)
this);
695 timer->setInterval(0.01);
697 vpViewer::mainLoop() ;
711 if (!input.openFile(file_name))
716 SoSeparator *newscene=SoDB::readAll(&input);
723 SoScale *taille =
new SoScale;
730 this->
scene->addChild(taille);
731 this->
scene->addChild(newscene);
742 output.openFile(name) ;
744 if (binary==
true) output.setBinary(TRUE) ;
746 SoWriteAction writeAction(&output) ;
747 writeAction.apply(
scene) ;
761 SoScale *taille =
new SoScale;
762 taille->scaleFactor.setValue (zoom, zoom, zoom);
764 SoSeparator * frame =
new SoSeparator;
766 frame->addChild(taille);
767 frame->addChild(createFrame (LONGUEUR_FLECHE*zoom, PROPORTION_FLECHE*zoom, RAYON_FLECHE*zoom));
780 scene->addChild(createFrame (LONGUEUR_FLECHE*zoom, PROPORTION_FLECHE*zoom, RAYON_FLECHE*zoom)) ;
793 SoSeparator * newObject;
795 if (! in.openFile (iv_filename))
797 vpERROR_TRACE (
"Erreur lors de la lecture du fichier %s.", iv_filename);
800 newObject = SoDB::readAll (&in);
801 if (NULL == newObject)
803 vpERROR_TRACE (
"Problem reading data for file <%s>.", iv_filename);
812 vpERROR_TRACE(
"Error adding object from file <%s> ",iv_filename) ;
855 bool identity = true ;
856 for (
unsigned int i=0 ; i <4 ;i++){
857 for (
unsigned int j=0 ; j < 4 ; j++){
859 if (fabs(fMo[i][j] -1) > 1e-6) identity=false ;
862 if (fabs(fMo[i][j]) > 1e-6) identity=false ;
869 root->addChild (
object);
875 for(
unsigned int i=0;i<4;i++)
876 for(
unsigned int j=0;j<4;j++)
877 matrix[(
int)j][(int)i]=(
float)fMo[i][j];
880 rotation.setValue(matrix);
882 SoTransform *displacement =
new SoTransform;
883 SoSeparator *newNode =
new SoSeparator;
885 displacement->rotation.setValue(rotation);
886 displacement->translation.setValue(matrix[3][0],
890 root->addChild (newNode);
891 newNode->addChild (displacement);
892 newNode->addChild (
object);
903 mainThread = SbThread::create (start_routine, (
void *)
this);
918 mainThread = SbThread::create (start_routine, (
void *)data);
935 vpViewer::exitMainLoop() ;
957 SbVec2s size(320,200);
963 size =
this ->internalView ->getViewportRegion().getWindowSize();
964 thisroot =
this ->internalView->getSceneManager()->getSceneGraph() ;
968 size =
this ->externalView ->getViewportRegion().getWindowSize();
969 thisroot =
this ->externalView->getSceneManager()->getSceneGraph() ;
972 SbViewportRegion myViewPort(size);
978 this ->offScreenRenderer =
new SoOffscreenRenderer(myViewPort);
983 this ->offScreenRenderer ->setViewportRegion (myViewPort);
990 delete this ->offScreenRenderer;
991 this ->offScreenRenderer = NULL;
1013 if (NULL != width) { * width = size [0]; }
1014 if (NULL != height) { * height = size [1]; }
1033 while (
get==0) {
vpTRACE(
"%d ",
get); }
1044 unsigned char r,g,b ;
1045 unsigned int index = 3*((internal_height-i-1)* internal_width + j );
1061 SbVec2s size =
this ->internalView ->getViewportRegion().getWindowSize();
HWND mainWindow
main Widget
SoSeparator * externalRoot
root node of the external view
static void write(const vpImage< unsigned char > &I, const char *filename)
virtual void initInternalViewer(const unsigned int nlig, const unsigned int ncol)
initialize the camera view
void resize(int x, int y, bool fixed=false)
void changeZoomFactor(const float zoom, const int index)
Change the zoom factor associated to the child given by index. In order to create multiple zoom facto...
SoPerspectiveCamera * internalCamera
internal camera
bool cameraPositionInitialized
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size)
void write(const char *fileName)
SoTransform * internalCameraPosition
internal camera position
void kill()
perform some destruction
void setExternalCameraParameters(vpCameraParameters &cam)
set external camera parameters
void setCameraPosition(vpHomogeneousMatrix &cMf)
set the camera position (from an homogeneous matrix)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void addObject(SoSeparator *object, const vpHomogeneousMatrix &fMo, SoSeparator *root)
Add a new object in the scene graph ad a given location.
static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size)
Type * bitmap
points toward the bitmap
Implementation of a simulator based on Coin3d (www.coin3d.org).
vpCameraParameters internalCameraParameters
internal camera parameters
void closeMainApplication()
void getSizeInternalView(int &width, int &height)
get the size of the internal view
void addAbsoluteFrame(float zoom=1)
Add the representation of the absolute frame.
unsigned int external_height
void getExternalCameraPosition(vpHomogeneousMatrix &cMf)
get the external camera position
void init()
perform some initialization
virtual void mainLoop()
activate the mainloop
static int wait(double t0, double t)
bool mainWindowInitialized
vpViewer * internalView
view from the camera
void moveInternalCamera(vpHomogeneousMatrix &cMf)
modify the position of the camera in the scene graph
void getInternalImage(vpImage< unsigned char > &I)
get an Image of the internal view
SoSeparator * internalRoot
root node of the internal view
vpCameraParameters externalCameraParameters
internal camera parameters
void initApplication(void *(*start_routine)(void *))
begin the main program
SoTransform * extrenalCameraPosition
external camera position
GLubyte * image_background
void setInternalCameraParameters(vpCameraParameters &cam)
set internal camera parameters
Viewer used by the simulator.
Generic class defining intrinsic camera parameters.
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
void initSceneGraph()
initialize the scene graph
void load(const char *file_name)
load an iv file
void offScreenRendering(vpSimulatorViewType view=vpSimulator::EXTERNAL, int *width=NULL, int *height=NULL)
vpViewer * externalView
view from an external camera
void initSoApplication()
open the SoGui application
SoPerspectiveCamera * externalCamera
external camera
unsigned int external_width
vpHomogeneousMatrix cMf
internal camera position
void initMainApplication()
perform some initialization in the main program thread
void addFrame(const vpHomogeneousMatrix &fMo, float zoom=1)
Add the representation of a frame.
unsigned char * bufferView
image of the internal view
unsigned int internal_width
vpHomogeneousMatrix inverse() const
void redraw()
display the scene (handle with care)
void initExternalViewer(const unsigned int nlig, const unsigned int ncol)
initialize the external view
SoOffscreenRenderer * offScreenRenderer
void setZoomFactor(const float zoom)
set the size of the camera/frame
unsigned int internal_height
void save(const char *name, bool binary=false)
save the scene in an iv file
SoSeparator * internalCameraObject
representation of the camera in the external view
SbThread * mainThread
thread with the main program