44 #include <visp3/core/vpConfig.h>
46 #ifdef VISP_HAVE_COIN3D_AND_GUI
48 #include <visp3/ar/vpSimulator.h>
49 #include <visp3/core/vpTime.h>
51 #include <visp3/core/vpImage.h>
53 #ifdef VISP_HAVE_MODULE_IO
54 # include <visp3/io/vpImageIo.h>
58 #include <Inventor/nodes/SoCone.h>
59 #include <Inventor/nodes/SoCylinder.h>
60 #include <Inventor/nodes/SoPointLight.h>
61 #include <Inventor/nodes/SoCoordinate3.h>
62 #include <Inventor/nodes/SoIndexedFaceSet.h>
63 #include <Inventor/nodes/SoTranslation.h>
64 #include <Inventor/nodes/SoScale.h>
65 #include <Inventor/nodes/SoRotationXYZ.h>
67 #include <Inventor/nodes/SoDirectionalLight.h>
68 #include <Inventor/nodes/SoMaterial.h>
69 #include <Inventor/nodes/SoDrawStyle.h>
70 #include <Inventor/nodes/SoEnvironment.h>
71 #include <Inventor/nodes/SoGroup.h>
72 #include <Inventor/actions/SoWriteAction.h>
79 static float pyramidVertexes [5][3] =
83 {-0.33f, -0.33f, 0.f},
90 static int32_t pyramidFaces[] =
92 0, 1, 2, 3, SO_END_FACE_INDEX,
94 0, 1, 4, SO_END_FACE_INDEX,
95 1, 2, 4, SO_END_FACE_INDEX,
96 2, 3, 4, SO_END_FACE_INDEX,
97 3, 0, 4, SO_END_FACE_INDEX,
105 SoSeparator *result =
new SoSeparator;
109 SoCoordinate3 *myCoords =
new SoCoordinate3;
110 myCoords->point.setValues(0, 5, pyramidVertexes);
111 result->addChild(myCoords);
114 SoIndexedFaceSet *myFaceSet =
new SoIndexedFaceSet;
115 myFaceSet->coordIndex.setValues (0, 21, (
const int32_t*)pyramidFaces);
116 result->addChild (myFaceSet);
118 result->unrefNoDelete();
129 createArrow (
float longueur,
130 float proportionFleche,
133 SoSeparator *fleche =
new SoSeparator;
136 SoTranslation *poseCylindre =
new SoTranslation;
137 SoCylinder *line =
new SoCylinder;
138 SoTranslation *posePointe =
new SoTranslation;
139 SoCone *pointe =
new SoCone;
141 float l_cylindre = longueur * ( 1 - proportionFleche);
142 float l_cone = longueur * proportionFleche;
143 float radius_cylindre = radius;
144 float radius_cone = radius * 5;
146 line->radius.setValue (radius_cylindre);
147 line->height.setValue (l_cylindre);
149 poseCylindre->translation.setValue (0, l_cylindre / 2, 0);
150 posePointe->translation.setValue (0.0, l_cylindre / 2 + l_cone / 2, 0);
152 pointe->bottomRadius.setValue (radius_cone);
153 pointe->height.setValue (l_cone);
156 fleche->addChild (poseCylindre);
157 fleche->addChild (line);
158 fleche->addChild (posePointe);
159 fleche->addChild (pointe);
169 #define LONGUEUR_FLECHE 1.0f
170 #define RAYON_FLECHE 0.002f
171 #define PROPORTION_FLECHE 0.1f
174 createFrame (
float longueurFleche = LONGUEUR_FLECHE ,
175 float proportionFleche = PROPORTION_FLECHE,
176 float radiusFleche = RAYON_FLECHE)
180 SoSeparator *frame =
new SoSeparator;
183 SoRotationXYZ *rotationY_X =
new SoRotationXYZ;
184 rotationY_X->axis = SoRotationXYZ::Z;
185 rotationY_X->angle.setValue ((
float)(- M_PI / 2));
187 SoRotationXYZ *rotationX_Y =
new SoRotationXYZ;
188 rotationX_Y->axis = SoRotationXYZ::Z;
189 rotationX_Y->angle.setValue ((
float)(M_PI / 2));
191 SoRotationXYZ *rotationY_Z =
new SoRotationXYZ;
192 rotationY_Z->axis = SoRotationXYZ::X;
193 rotationY_Z->angle.setValue ((
float)(M_PI / 2));
195 SoMaterial *rouge =
new SoMaterial;
196 rouge->diffuseColor.setValue(1.0, 0.0, 0.0);
197 rouge->emissiveColor.setValue(0.5, 0.0, 0.0);
199 SoMaterial *vert =
new SoMaterial;
200 vert->diffuseColor.setValue(0.0, 1.0, 0.0);
201 vert->emissiveColor.setValue(0.0, 0.5, 0.0);
203 SoMaterial *bleu =
new SoMaterial;
204 bleu->diffuseColor.setValue(0.0, 0.0, 1.0);
205 bleu->emissiveColor.setValue(0.0, 0.0, 0.5);
207 SoSeparator *fleche = createArrow(longueurFleche,
211 frame->addChild (rouge);
212 frame->addChild (rotationY_X);
213 frame->addChild (fleche);
214 frame->addChild (vert);
215 frame->addChild (rotationX_Y);
216 frame->addChild (fleche);
217 frame->addChild (bleu);
218 frame->addChild (rotationY_Z);
219 frame->addChild (fleche);
221 frame-> unrefNoDelete ();
228 createCameraObject (
const float zoomFactor = 1.0)
232 SoSeparator * cam =
new SoSeparator;
235 SoMaterial *myMaterial =
new SoMaterial;
236 myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0);
237 myMaterial->emissiveColor.setValue(0.5, 0.0, 0.0);
239 SoScale *taille =
new SoScale;
242 taille->scaleFactor.setValue (zoom, zoom, zoom);
245 SoMaterial *couleurBlanc =
new SoMaterial;
246 couleurBlanc->diffuseColor.setValue(1.0, 1.0, 1.0);
247 couleurBlanc->emissiveColor.setValue(1.0, 1.0, 1.0);
248 SoDrawStyle * filDeFer =
new SoDrawStyle;
249 filDeFer->style.setValue (SoDrawStyle::LINES);
250 filDeFer->lineWidth.setValue (1);
252 SoSeparator * cone =
new SoSeparator;
254 cone->addChild (makePyramide());
255 cone->addChild (couleurBlanc);
256 cone->addChild (filDeFer);
257 cone->addChild (makePyramide());
258 cone->unrefNoDelete();
260 cam->addChild(myMaterial);
261 cam->addChild(taille);
263 cam->addChild(createFrame(2.0f,0.1f,0.01f));
303 #if defined(VISP_HAVE_SOWIN)
305 #elif defined(VISP_HAVE_SOQT)
307 #elif defined(VISP_HAVE_SOXT)
327 #if defined(VISP_HAVE_SOWIN)
329 #elif defined(VISP_HAVE_SOQT)
331 #elif defined(VISP_HAVE_SOXT)
362 this->
scene =
new SoSeparator;
391 SoSeparator * camera =
new SoSeparator;
400 SoCube *cube =
new SoCube ;
410 SoDB::enableRealTimeSensor(FALSE);
411 SoSceneManager::enableRealTimeUpdate(FALSE);
412 realtime = (SbTime *) SoDB::getGlobalField(
"realTime");
432 static bool firstTime =
true;
434 SoScale *taille =
new SoScale;
436 this->
scene->addChild(taille);
440 SoScale * taille = (SoScale*)this->
scene->getChild(0);
465 SoScale * taille = (SoScale*)this->
scene->getChild(index);
466 taille->scaleFactor.setValue (zoomFactor, zoomFactor, zoomFactor);
503 bufferView =
new unsigned char[3*width*height] ;
542 float px = (float)_cam.
get_px();
543 float py = (float)_cam.
get_py();
562 float px = (float)_cam.
get_px();
563 float py = (float)_cam.
get_py();
584 SbVec3f axis ;
float angle ;
585 orientation.getValue(axis,angle) ;
586 SbRotation rotation(axis,angle) ;
590 t = position.getValue() ;
593 matrix.setRotate(rotation) ;
597 rotX.setRotate (SbRotation (SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
598 matrix.multLeft (rotX);
599 for(
unsigned int i=0;i<4;i++)
600 for(
unsigned int j=0;j<4;j++)
601 fMc[j][i]=matrix[(
int)i][(int)j];
623 rotX.setRotate (SbRotation (SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
624 for(
unsigned int i=0;i<4;i++)
625 for(
unsigned int j=0;j<4;j++)
626 matrix[(
int)j][(int)i]=(
float)cMf[i][j];
629 matrix.multLeft (rotX);
630 rotCam.setValue(matrix);
635 internalCamera->position.setValue(matrix[3][0],matrix[3][1],matrix[3][2]);
638 rotX.setRotate (SbRotation (SbVec3f(-1.0f, 0.0f, 0.0f), (
float)M_PI));
639 matrix.multLeft (rotX);
640 rotCam.setValue(matrix);
673 timerSensorCallback(
void *data , SoSensor *)
693 SoTimerSensor * timer =
new SoTimerSensor(timerSensorCallback, (
void *)
this);
694 timer->setInterval(0.01);
696 vpViewer::mainLoop() ;
710 if (!input.openFile(file_name))
715 SoSeparator *newscene=SoDB::readAll(&input);
722 SoScale *taille =
new SoScale;
729 this->
scene->addChild(taille);
730 this->
scene->addChild(newscene);
741 output.openFile(name) ;
743 if (binary==
true) output.setBinary(TRUE) ;
745 SoWriteAction writeAction(&output) ;
746 writeAction.apply(
scene) ;
760 SoScale *taille =
new SoScale;
761 taille->scaleFactor.setValue (zoom, zoom, zoom);
763 SoSeparator * frame =
new SoSeparator;
765 frame->addChild(taille);
766 frame->addChild(createFrame (LONGUEUR_FLECHE*zoom, PROPORTION_FLECHE*zoom, RAYON_FLECHE*zoom));
779 scene->addChild(createFrame (LONGUEUR_FLECHE*zoom, PROPORTION_FLECHE*zoom, RAYON_FLECHE*zoom)) ;
792 SoSeparator * newObject;
794 if (! in.openFile (iv_filename))
796 vpERROR_TRACE (
"Erreur lors de la lecture du fichier %s.", iv_filename);
799 newObject = SoDB::readAll (&in);
800 if (NULL == newObject)
802 vpERROR_TRACE (
"Problem reading data for file <%s>.", iv_filename);
811 vpERROR_TRACE(
"Error adding object from file <%s> ",iv_filename) ;
854 bool identity = true ;
855 for (
unsigned int i=0 ; i <4 ;i++){
856 for (
unsigned int j=0 ; j < 4 ; j++){
858 if (fabs(fMo[i][j] -1) > 1e-6) identity=false ;
861 if (fabs(fMo[i][j]) > 1e-6) identity=false ;
868 root->addChild (
object);
874 for(
unsigned int i=0;i<4;i++)
875 for(
unsigned int j=0;j<4;j++)
876 matrix[(
int)j][(int)i]=(
float)fMo[i][j];
879 rotation.setValue(matrix);
881 SoTransform *displacement =
new SoTransform;
882 SoSeparator *newNode =
new SoSeparator;
884 displacement->rotation.setValue(rotation);
885 displacement->translation.setValue(matrix[3][0],
889 root->addChild (newNode);
890 newNode->addChild (displacement);
891 newNode->addChild (
object);
902 mainThread = SbThread::create (start_routine, (
void *)
this);
917 mainThread = SbThread::create (start_routine, (
void *)data);
934 vpViewer::exitMainLoop() ;
956 SbVec2s size(320,200);
962 size =
this ->internalView ->getViewportRegion().getWindowSize();
963 thisroot =
this ->internalView->getSceneManager()->getSceneGraph() ;
967 size =
this ->externalView ->getViewportRegion().getWindowSize();
968 thisroot =
this ->externalView->getSceneManager()->getSceneGraph() ;
971 SbViewportRegion myViewPort(size);
977 this ->offScreenRenderer =
new SoOffscreenRenderer(myViewPort);
982 this ->offScreenRenderer ->setViewportRegion (myViewPort);
989 delete this ->offScreenRenderer;
990 this ->offScreenRenderer = NULL;
1012 if (NULL != width) { * width = size [0]; }
1013 if (NULL != height) { * height = size [1]; }
1028 #ifdef VISP_HAVE_MODULE_IO
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 );
1062 SbVec2s size =
this ->internalView ->getViewportRegion().getWindowSize();
1096 #elif !defined(VISP_BUILD_SHARED_LIBS)
1098 void dummy_vpSimulator() {};
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
VISP_EXPORT int wait(double t0, double t)
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
int get
Flag to protect the read and write of the framebuffer (between the simulator and the viewer)...
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)
Implementation of an homogeneous matrix and operations on such kind of matrices.
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
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