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)
346 this->
scene =
new SoSeparator;
375 SoSeparator * camera =
new SoSeparator;
384 SoCube *cube =
new SoCube ;
394 SoDB::enableRealTimeSensor(FALSE);
395 SoSceneManager::enableRealTimeUpdate(FALSE);
396 realtime = (SbTime *) SoDB::getGlobalField(
"realTime");
416 static bool firstTime =
true;
418 SoScale *taille =
new SoScale;
420 this->
scene->addChild(taille);
424 SoScale * taille = (SoScale*)this->
scene->getChild(0);
449 SoScale * taille = (SoScale*)this->
scene->getChild(index);
450 taille->scaleFactor.setValue (zoomFactor, zoomFactor, zoomFactor);
487 bufferView =
new unsigned char[3*width*height] ;
526 float px = (float)_cam.
get_px();
527 float py = (float)_cam.
get_py();
546 float px = (float)_cam.
get_px();
547 float py = (float)_cam.
get_py();
568 SbVec3f axis ;
float angle ;
569 orientation.getValue(axis,angle) ;
570 SbRotation rotation(axis,angle) ;
574 t = position.getValue() ;
577 matrix.setRotate(rotation) ;
581 rotX.setRotate (SbRotation (SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
582 matrix.multLeft (rotX);
583 for(
unsigned int i=0;i<4;i++)
584 for(
unsigned int j=0;j<4;j++)
585 fMc[j][i]=matrix[(
int)i][(int)j];
607 rotX.setRotate (SbRotation (SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
608 for(
unsigned int i=0;i<4;i++)
609 for(
unsigned int j=0;j<4;j++)
610 matrix[(
int)j][(int)i]=(
float)cMf[i][j];
613 matrix.multLeft (rotX);
614 rotCam.setValue(matrix);
619 internalCamera->position.setValue(matrix[3][0],matrix[3][1],matrix[3][2]);
622 rotX.setRotate (SbRotation (SbVec3f(-1.0f, 0.0f, 0.0f), (
float)M_PI));
623 matrix.multLeft (rotX);
624 rotCam.setValue(matrix);
657 timerSensorCallback(
void *data , SoSensor *)
677 SoTimerSensor * timer =
new SoTimerSensor(timerSensorCallback, (
void *)
this);
678 timer->setInterval(0.01);
680 vpViewer::mainLoop() ;
694 if (!input.openFile(file_name))
699 SoSeparator *newscene=SoDB::readAll(&input);
706 SoScale *taille =
new SoScale;
713 this->
scene->addChild(taille);
714 this->
scene->addChild(newscene);
725 output.openFile(name) ;
727 if (binary==
true) output.setBinary(TRUE) ;
729 SoWriteAction writeAction(&output) ;
730 writeAction.apply(
scene) ;
744 SoScale *taille =
new SoScale;
745 taille->scaleFactor.setValue (zoom, zoom, zoom);
747 SoSeparator * frame =
new SoSeparator;
749 frame->addChild(taille);
750 frame->addChild(createFrame (LONGUEUR_FLECHE*zoom, PROPORTION_FLECHE*zoom, RAYON_FLECHE*zoom));
763 scene->addChild(createFrame (LONGUEUR_FLECHE*zoom, PROPORTION_FLECHE*zoom, RAYON_FLECHE*zoom)) ;
776 SoSeparator * newObject;
778 if (! in.openFile (iv_filename))
780 vpERROR_TRACE (
"Erreur lors de la lecture du fichier %s.", iv_filename);
783 newObject = SoDB::readAll (&in);
784 if (NULL == newObject)
786 vpERROR_TRACE (
"Problem reading data for file <%s>.", iv_filename);
795 vpERROR_TRACE(
"Error adding object from file <%s> ",iv_filename) ;
838 bool identity = true ;
839 for (
unsigned int i=0 ; i <4 ;i++){
840 for (
unsigned int j=0 ; j < 4 ; j++){
842 if (fabs(fMo[i][j] -1) > 1e-6) identity=false ;
845 if (fabs(fMo[i][j]) > 1e-6) identity=false ;
853 root->addChild (
object);
859 for(
unsigned int i=0;i<4;i++)
860 for(
unsigned int j=0;j<4;j++)
861 matrix[(
int)j][(int)i]=(
float)fMo[i][j];
864 rotation.setValue(matrix);
866 SoTransform *displacement =
new SoTransform;
867 SoSeparator *newNode =
new SoSeparator;
869 displacement->rotation.setValue(rotation);
870 displacement->translation.setValue(matrix[3][0],
874 root->addChild (newNode);
875 newNode->addChild (displacement);
876 newNode->addChild (
object);
887 mainThread = SbThread::create (start_routine, (
void *)
this);
902 mainThread = SbThread::create (start_routine, (
void *)data);
919 vpViewer::exitMainLoop() ;
941 SbVec2s size(320,200);
947 size =
this ->internalView ->getViewportRegion().getWindowSize();
948 thisroot =
this ->internalView->getSceneManager()->getSceneGraph() ;
952 size =
this ->externalView ->getViewportRegion().getWindowSize();
953 thisroot =
this ->externalView->getSceneManager()->getSceneGraph() ;
956 SbViewportRegion myViewPort(size);
962 this ->offScreenRenderer =
new SoOffscreenRenderer(myViewPort);
967 this ->offScreenRenderer ->setViewportRegion (myViewPort);
974 delete this ->offScreenRenderer;
975 this ->offScreenRenderer = NULL;
997 if (NULL != width) { * width = size [0]; }
998 if (NULL != height) { * height = size [1]; }
1017 while (
get==0) {
vpTRACE(
"%d ",
get); }
1028 unsigned char r,g,b ;
1029 unsigned int index = 3*((internal_height-i-1)* internal_width + j );
1045 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
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