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/SoCoordinate3.h>
60 #include <Inventor/nodes/SoCylinder.h>
61 #include <Inventor/nodes/SoIndexedFaceSet.h>
62 #include <Inventor/nodes/SoPointLight.h>
63 #include <Inventor/nodes/SoRotationXYZ.h>
64 #include <Inventor/nodes/SoScale.h>
65 #include <Inventor/nodes/SoTranslation.h>
67 #include <Inventor/actions/SoWriteAction.h>
68 #include <Inventor/nodes/SoDirectionalLight.h>
69 #include <Inventor/nodes/SoDrawStyle.h>
70 #include <Inventor/nodes/SoEnvironment.h>
71 #include <Inventor/nodes/SoGroup.h>
72 #include <Inventor/nodes/SoMaterial.h>
76 static float pyramidVertexes[5][3] = {{0.33f, 0.33f, 0.f},
78 {-0.33f, -0.33f, 0.f},
83 static int32_t pyramidFaces[] = {
109 SoSeparator *makePyramide()
111 SoSeparator *result =
new SoSeparator;
115 SoCoordinate3 *myCoords =
new SoCoordinate3;
116 myCoords->point.setValues(0, 5, pyramidVertexes);
117 result->addChild(myCoords);
120 SoIndexedFaceSet *myFaceSet =
new SoIndexedFaceSet;
121 myFaceSet->coordIndex.setValues(0, 21, (
const int32_t *)pyramidFaces);
122 result->addChild(myFaceSet);
124 result->unrefNoDelete();
134 static SoSeparator *createArrow(
float longueur,
float proportionFleche,
float radius)
136 SoSeparator *fleche =
new SoSeparator;
139 SoTranslation *poseCylindre =
new SoTranslation;
140 SoCylinder *line =
new SoCylinder;
141 SoTranslation *posePointe =
new SoTranslation;
142 SoCone *pointe =
new SoCone;
144 float l_cylindre = longueur * (1 - proportionFleche);
145 float l_cone = longueur * proportionFleche;
146 float radius_cylindre = radius;
147 float radius_cone = radius * 5;
149 line->radius.setValue(radius_cylindre);
150 line->height.setValue(l_cylindre);
152 poseCylindre->translation.setValue(0, l_cylindre / 2, 0);
153 posePointe->translation.setValue(0.0, l_cylindre / 2 + l_cone / 2, 0);
155 pointe->bottomRadius.setValue(radius_cone);
156 pointe->height.setValue(l_cone);
158 fleche->addChild(poseCylindre);
159 fleche->addChild(line);
160 fleche->addChild(posePointe);
161 fleche->addChild(pointe);
170 #define LONGUEUR_FLECHE 1.0f
171 #define RAYON_FLECHE 0.002f
172 #define PROPORTION_FLECHE 0.1f
174 SoSeparator *createFrame(
float longueurFleche = LONGUEUR_FLECHE,
float proportionFleche = PROPORTION_FLECHE,
175 float radiusFleche = RAYON_FLECHE)
179 SoSeparator *frame =
new SoSeparator;
182 SoRotationXYZ *rotationY_X =
new SoRotationXYZ;
183 rotationY_X->axis = SoRotationXYZ::Z;
184 rotationY_X->angle.setValue((
float)(-M_PI / 2));
186 SoRotationXYZ *rotationX_Y =
new SoRotationXYZ;
187 rotationX_Y->axis = SoRotationXYZ::Z;
188 rotationX_Y->angle.setValue((
float)(M_PI / 2));
190 SoRotationXYZ *rotationY_Z =
new SoRotationXYZ;
191 rotationY_Z->axis = SoRotationXYZ::X;
192 rotationY_Z->angle.setValue((
float)(M_PI / 2));
194 SoMaterial *rouge =
new SoMaterial;
195 rouge->diffuseColor.setValue(1.0, 0.0, 0.0);
196 rouge->emissiveColor.setValue(0.5, 0.0, 0.0);
198 SoMaterial *vert =
new SoMaterial;
199 vert->diffuseColor.setValue(0.0, 1.0, 0.0);
200 vert->emissiveColor.setValue(0.0, 0.5, 0.0);
202 SoMaterial *bleu =
new SoMaterial;
203 bleu->diffuseColor.setValue(0.0, 0.0, 1.0);
204 bleu->emissiveColor.setValue(0.0, 0.0, 0.5);
206 SoSeparator *fleche = createArrow(longueurFleche, proportionFleche, radiusFleche);
208 frame->addChild(rouge);
209 frame->addChild(rotationY_X);
210 frame->addChild(fleche);
211 frame->addChild(vert);
212 frame->addChild(rotationX_Y);
213 frame->addChild(fleche);
214 frame->addChild(bleu);
215 frame->addChild(rotationY_Z);
216 frame->addChild(fleche);
218 frame->unrefNoDelete();
224 SoSeparator *createCameraObject(
float zoomFactor = 1.0)
228 SoSeparator *cam =
new SoSeparator;
231 SoMaterial *myMaterial =
new SoMaterial;
232 myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0);
233 myMaterial->emissiveColor.setValue(0.5, 0.0, 0.0);
235 SoScale *taille =
new SoScale;
237 float zoom = 0.1f * zoomFactor;
238 taille->scaleFactor.setValue(zoom, zoom, zoom);
241 SoMaterial *couleurBlanc =
new SoMaterial;
242 couleurBlanc->diffuseColor.setValue(1.0, 1.0, 1.0);
243 couleurBlanc->emissiveColor.setValue(1.0, 1.0, 1.0);
244 SoDrawStyle *filDeFer =
new SoDrawStyle;
245 filDeFer->style.setValue(SoDrawStyle::LINES);
246 filDeFer->lineWidth.setValue(1);
248 SoSeparator *cone =
new SoSeparator;
250 cone->addChild(makePyramide());
251 cone->addChild(couleurBlanc);
252 cone->addChild(filDeFer);
253 cone->addChild(makePyramide());
254 cone->unrefNoDelete();
256 cam->addChild(myMaterial);
257 cam->addChild(taille);
259 cam->addChild(createFrame(2.0f, 0.1f, 0.01f));
297 #if defined(VISP_HAVE_SOWIN)
299 #elif defined(VISP_HAVE_SOQT)
301 #elif defined(VISP_HAVE_SOXT)
327 #if defined(VISP_HAVE_SOWIN)
329 #elif defined(VISP_HAVE_SOQT)
331 #elif defined(VISP_HAVE_SOXT)
334 mainWindowInitialized(false), typeImage(
vpSimulator::grayImage), image_background(NULL), internalView(NULL),
335 externalView(NULL), mainThread(NULL), internal_width(0), internal_height(0), external_width(0), external_height(0),
336 scene(NULL), internalRoot(NULL), externalRoot(NULL), internalCamera(NULL), externalCamera(NULL),
337 internalCameraPosition(NULL), extrenalCameraPosition(NULL), internalCameraObject(NULL), zoomFactor(0.),
338 cameraPositionInitialized(false), cMf(), internalCameraParameters(), externalCameraParameters(), realtime(NULL),
339 offScreenRenderer(NULL), bufferView(NULL), get(0)
354 this->
scene =
new SoSeparator;
379 SoSeparator *camera =
new SoSeparator;
387 SoCube *cube =
new SoCube;
390 cube->height = 0.01f;
396 SoDB::enableRealTimeSensor(FALSE);
397 SoSceneManager::enableRealTimeUpdate(FALSE);
398 realtime = (SbTime *)SoDB::getGlobalField(
"realTime");
412 static bool firstTime =
true;
414 SoScale *taille =
new SoScale;
416 this->
scene->addChild(taille);
419 SoScale *taille = (SoScale *)this->
scene->getChild(0);
444 SoScale *taille = (SoScale *)this->
scene->getChild(index);
480 bufferView =
new unsigned char[3 * width * height];
514 float px = (float)_cam.
get_px();
515 float py = (float)_cam.
get_py();
533 float px = (float)_cam.
get_px();
534 float py = (float)_cam.
get_py();
555 orientation.getValue(axis, angle);
556 SbRotation rotation(axis, angle);
560 t = position.getValue();
563 matrix.setRotate(rotation);
567 rotX.setRotate(SbRotation(SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
568 matrix.multLeft(rotX);
569 for (
unsigned int i = 0; i < 4; i++)
570 for (
unsigned int j = 0; j < 4; j++)
571 fMc[j][i] = matrix[(
int)i][(int)j];
590 rotX.setRotate(SbRotation(SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
591 for (
unsigned int i = 0; i < 4; i++)
592 for (
unsigned int j = 0; j < 4; j++)
593 matrix[(
int)j][(int)i] = (
float)
cMf[i][j];
596 matrix.multLeft(rotX);
597 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);
634 static void timerSensorCallback(
void *data, SoSensor *)
650 SoTimerSensor *timer =
new SoTimerSensor(timerSensorCallback, (
void *)
this);
651 timer->setInterval(0.01);
653 vpViewer::mainLoop();
665 if (!input.openFile(file_name)) {
669 SoSeparator *newscene = SoDB::readAll(&input);
671 if (newscene == NULL) {
675 SoScale *taille =
new SoScale;
683 this->
scene->addChild(taille);
684 this->
scene->addChild(newscene);
692 output.openFile(name);
695 output.setBinary(TRUE);
697 SoWriteAction writeAction(&output);
698 writeAction.apply(
scene);
710 SoScale *taille =
new SoScale;
711 taille->scaleFactor.setValue(zoom, zoom, zoom);
713 SoSeparator *frame =
new SoSeparator;
715 frame->addChild(taille);
716 frame->addChild(createFrame(LONGUEUR_FLECHE * zoom, PROPORTION_FLECHE * zoom, RAYON_FLECHE * zoom));
728 scene->addChild(createFrame(LONGUEUR_FLECHE * zoom, PROPORTION_FLECHE * zoom, RAYON_FLECHE * zoom));
740 SoSeparator *newObject;
742 if (!in.openFile(iv_filename)) {
743 vpERROR_TRACE(
"Erreur lors de la lecture du fichier %s.", iv_filename);
746 newObject = SoDB::readAll(&in);
747 if (NULL == newObject) {
748 vpERROR_TRACE(
"Problem reading data for file <%s>.", iv_filename);
754 vpERROR_TRACE(
"Error adding object from file <%s> ", iv_filename);
784 bool identity =
true;
785 for (
unsigned int i = 0; i < 4; i++) {
786 for (
unsigned int j = 0; j < 4; j++) {
788 if (fabs(fMo[i][j] - 1) > 1e-6)
791 if (fabs(fMo[i][j]) > 1e-6)
797 if (identity ==
true) {
798 root->addChild(
object);
802 for (
unsigned int i = 0; i < 4; i++)
803 for (
unsigned int j = 0; j < 4; j++)
804 matrix[(
int)j][(int)i] = (
float)fMo[i][j];
807 rotation.setValue(matrix);
809 SoTransform *displacement =
new SoTransform;
810 SoSeparator *newNode =
new SoSeparator;
812 displacement->rotation.setValue(rotation);
813 displacement->translation.setValue(matrix[3][0], matrix[3][1], matrix[3][2]);
815 root->addChild(newNode);
816 newNode->addChild(displacement);
817 newNode->addChild(
object);
825 mainThread = SbThread::create(start_routine, (
void *)
this);
839 mainThread = SbThread::create(start_routine, (
void *)data);
854 vpViewer::exitMainLoop();
871 SbVec2s size(320, 200);
876 size = this->
internalView->getViewportRegion().getWindowSize();
877 thisroot = this->
internalView->getSceneManager()->getSceneGraph();
879 size = this->
externalView->getViewportRegion().getWindowSize();
880 thisroot = this->
externalView->getSceneManager()->getSceneGraph();
883 SbViewportRegion myViewPort(size);
919 if (NULL != height) {
933 #ifdef VISP_HAVE_MODULE_IO
949 unsigned char r, g, b;
966 SbVec2s size = this->
internalView->getViewportRegion().getWindowSize();
998 #elif !defined(VISP_BUILD_SHARED_LIBS)
1001 void dummy_vpSimulator(){};
Generic class defining intrinsic camera parameters.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int width, unsigned int height, bool flip=false)
static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Type * bitmap
points toward the bitmap
Implementation of a simulator based on Coin3d (www.coin3d.org).
SoPerspectiveCamera * internalCamera
internal camera
void save(const char *name, bool binary=false)
save the scene in an iv file
unsigned int internal_height
GLubyte * image_background
vpCameraParameters internalCameraParameters
internal camera parameters
void load(const char *file_name)
load an iv file
unsigned char * bufferView
image of the internal view
SoTransform * extrenalCameraPosition
external camera position
void addFrame(const vpHomogeneousMatrix &fMo, float zoom=1)
Add the representation of a frame.
void setInternalCameraParameters(vpCameraParameters &cam)
set internal camera parameters
void changeZoomFactor(float zoom, int index)
Change the zoom factor associated to the child given by index. In order to create multiple zoom facto...
void kill()
perform some destruction
virtual void mainLoop()
activate the mainloop
unsigned int external_height
void setExternalCameraParameters(vpCameraParameters &cam)
set external camera parameters
unsigned int internal_width
void getInternalImage(vpImage< unsigned char > &I)
get an Image of the internal view
void initMainApplication()
perform some initialization in the main program thread
bool cameraPositionInitialized
void getSizeInternalView(int &width, int &height)
get the size of the internal view
void moveInternalCamera(vpHomogeneousMatrix &cMf)
modify the position of the camera in the scene graph
void initSoApplication()
open the SoGui application
void getExternalCameraPosition(vpHomogeneousMatrix &cMf)
get the external camera position
void redraw()
display the scene (handle with care)
void initApplication(void *(*start_routine)(void *))
begin the main program
vpViewer * internalView
view from the camera
void init()
perform some initialization
vpHomogeneousMatrix cMf
internal camera position
SoSeparator * internalCameraObject
representation of the camera in the external view
SbThread * mainThread
thread with the main program
vpViewer * externalView
view from an external camera
void setZoomFactor(float zoom)
set the size of the camera/frame
SoPerspectiveCamera * externalCamera
external camera
void setCameraPosition(vpHomogeneousMatrix &cMf)
set the camera position (from an homogeneous matrix)
HWND mainWindow
main Widget
void initExternalViewer(unsigned int nlig, unsigned int ncol)
initialize the external view
SoSeparator * externalRoot
root node of the external view
void write(const char *fileName)
virtual void initInternalViewer(unsigned int nlig, unsigned int ncol)
initialize the camera view
bool mainWindowInitialized
void addObject(SoSeparator *object, const vpHomogeneousMatrix &fMo, SoSeparator *root)
Add a new object in the scene graph ad a given location.
vpCameraParameters externalCameraParameters
internal camera parameters
void initSceneGraph()
initialize the scene graph
SoOffscreenRenderer * offScreenRenderer
SoTransform * internalCameraPosition
internal camera position
SoSeparator * internalRoot
root node of the internal view
void closeMainApplication()
void addAbsoluteFrame(float zoom=1)
Add the representation of the absolute frame.
unsigned int external_width
void offScreenRendering(vpSimulatorViewType view=vpSimulator::EXTERNAL, int *width=NULL, int *height=NULL)
Viewer used by the simulator.
void resize(int x, int y, bool fixed=false)
VISP_EXPORT int wait(double t0, double t)