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));
328 this->
scene =
new SoSeparator;
357 SoSeparator * camera =
new SoSeparator;
366 SoCube *cube =
new SoCube ;
376 SoDB::enableRealTimeSensor(FALSE);
377 SoSceneManager::enableRealTimeUpdate(FALSE);
378 realtime = (SbTime *) SoDB::getGlobalField(
"realTime");
398 static bool firstTime =
true;
400 SoScale *taille =
new SoScale;
402 this->
scene->addChild(taille);
406 SoScale * taille = (SoScale*)this->
scene->getChild(0);
431 SoScale * taille = (SoScale*)this->
scene->getChild(index);
432 taille->scaleFactor.setValue (zoomFactor, zoomFactor, zoomFactor);
469 bufferView =
new unsigned char[3*width*height] ;
508 float px = (float)_cam.
get_px();
509 float py = (float)_cam.
get_py();
528 float px = (float)_cam.
get_px();
529 float py = (float)_cam.
get_py();
550 SbVec3f axis ;
float angle ;
551 orientation.getValue(axis,angle) ;
552 SbRotation rotation(axis,angle) ;
556 t = position.getValue() ;
559 matrix.setRotate(rotation) ;
563 rotX.setRotate (SbRotation (SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
564 matrix.multLeft (rotX);
565 for(
unsigned int i=0;i<4;i++)
566 for(
unsigned int j=0;j<4;j++)
567 fMc[j][i]=matrix[(
int)i][(int)j];
589 rotX.setRotate (SbRotation (SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
590 for(
unsigned int i=0;i<4;i++)
591 for(
unsigned int j=0;j<4;j++)
592 matrix[(
int)j][(int)i]=(
float)cMf[i][j];
595 matrix.multLeft (rotX);
596 rotCam.setValue(matrix);
601 internalCamera->position.setValue(matrix[3][0],matrix[3][1],matrix[3][2]);
604 rotX.setRotate (SbRotation (SbVec3f(-1.0f, 0.0f, 0.0f), (
float)M_PI));
605 matrix.multLeft (rotX);
606 rotCam.setValue(matrix);
639 timerSensorCallback(
void *data , SoSensor *)
659 SoTimerSensor * timer =
new SoTimerSensor(timerSensorCallback, (
void *)
this);
660 timer->setInterval(0.01);
662 vpViewer::mainLoop() ;
676 if (!input.openFile(file_name))
681 SoSeparator *newscene=SoDB::readAll(&input);
688 SoScale *taille =
new SoScale;
695 this->
scene->addChild(taille);
696 this->
scene->addChild(newscene);
707 output.openFile(name) ;
709 if (binary==
true) output.setBinary(TRUE) ;
711 SoWriteAction writeAction(&output) ;
712 writeAction.apply(
scene) ;
726 SoScale *taille =
new SoScale;
727 taille->scaleFactor.setValue (zoom, zoom, zoom);
729 SoSeparator * frame =
new SoSeparator;
731 frame->addChild(taille);
732 frame->addChild(createFrame (LONGUEUR_FLECHE*zoom, PROPORTION_FLECHE*zoom, RAYON_FLECHE*zoom));
745 scene->addChild(createFrame (LONGUEUR_FLECHE*zoom, PROPORTION_FLECHE*zoom, RAYON_FLECHE*zoom)) ;
758 SoSeparator * newObject;
760 if (! in.openFile (iv_filename))
762 vpERROR_TRACE (
"Erreur lors de la lecture du fichier %s.", iv_filename);
765 newObject = SoDB::readAll (&in);
766 if (NULL == newObject)
768 vpERROR_TRACE (
"Problem reading data for file <%s>.", iv_filename);
777 vpERROR_TRACE(
"Error adding object from file <%s> ",iv_filename) ;
820 bool identity = true ;
821 for (
unsigned int i=0 ; i <4 ;i++){
822 for (
unsigned int j=0 ; j < 4 ; j++){
824 if (fabs(fMo[i][j] -1) > 1e-6) identity=false ;
827 if (fabs(fMo[i][j]) > 1e-6) identity=false ;
835 root->addChild (
object);
841 for(
unsigned int i=0;i<4;i++)
842 for(
unsigned int j=0;j<4;j++)
843 matrix[(
int)j][(int)i]=(
float)fMo[i][j];
846 rotation.setValue(matrix);
848 SoTransform *displacement =
new SoTransform;
849 SoSeparator *newNode =
new SoSeparator;
851 displacement->rotation.setValue(rotation);
852 displacement->translation.setValue(matrix[3][0],
856 root->addChild (newNode);
857 newNode->addChild (displacement);
858 newNode->addChild (
object);
869 mainThread = SbThread::create (start_routine, (
void *)
this);
884 mainThread = SbThread::create (start_routine, (
void *)data);
901 vpViewer::exitMainLoop() ;
923 SbVec2s size(320,200);
929 size =
this ->internalView ->getViewportRegion().getWindowSize();
930 thisroot =
this ->internalView->getSceneManager()->getSceneGraph() ;
934 size =
this ->externalView ->getViewportRegion().getWindowSize();
935 thisroot =
this ->externalView->getSceneManager()->getSceneGraph() ;
938 SbViewportRegion myViewPort(size);
944 this ->offScreenRenderer =
new SoOffscreenRenderer(myViewPort);
949 this ->offScreenRenderer ->setViewportRegion (myViewPort);
956 delete this ->offScreenRenderer;
957 this ->offScreenRenderer = NULL;
979 if (NULL != width) { * width = size [0]; }
980 if (NULL != height) { * height = size [1]; }
999 while (
get==0) {
vpTRACE(
"%d ",
get); }
1010 unsigned char r,g,b ;
1011 unsigned int index = 3*((internal_height-i-1)* internal_width + j );
1027 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 resize(const unsigned int height, const unsigned int width)
set the size of the image
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
GLubyte * image_background
void setInternalCameraParameters(vpCameraParameters &cam)
set internal camera parameters
Viewer used by the simulator.
Generic class defining intrinsic camera parameters.
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