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(nullptr), internalView(nullptr),
335 externalView(nullptr), mainThread(nullptr), internal_width(0), internal_height(0), external_width(0), external_height(0),
336 scene(nullptr), internalRoot(nullptr), externalRoot(nullptr), internalCamera(nullptr), externalCamera(nullptr),
337 internalCameraPosition(nullptr), extrenalCameraPosition(nullptr), internalCameraObject(nullptr), zoomFactor(0.),
338 cameraPositionInitialized(false), cMf(), internalCameraParameters(), externalCameraParameters(), realtime(nullptr),
339 offScreenRenderer(nullptr), bufferView(nullptr), 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);
420 SoScale *taille = (SoScale *)this->
scene->getChild(0);
445 SoScale *taille = (SoScale *)this->
scene->getChild(index);
481 bufferView =
new unsigned char[3 * width * height];
515 float px = (float)_cam.
get_px();
516 float py = (float)_cam.
get_py();
534 float px = (float)_cam.
get_px();
535 float py = (float)_cam.
get_py();
556 orientation.getValue(axis, angle);
557 SbRotation rotation(axis, angle);
561 t = position.getValue();
564 matrix.setRotate(rotation);
568 rotX.setRotate(SbRotation(SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
569 matrix.multLeft(rotX);
570 for (
unsigned int i = 0; i < 4; i++)
571 for (
unsigned int j = 0; j < 4; j++)
572 fMc[j][i] = matrix[(
int)i][(int)j];
591 rotX.setRotate(SbRotation(SbVec3f(1.0f, 0.0f, 0.0f), (
float)M_PI));
592 for (
unsigned int i = 0; i < 4; i++)
593 for (
unsigned int j = 0; j < 4; j++)
594 matrix[(
int)j][(int)i] = (
float)
cMf[i][j];
597 matrix.multLeft(rotX);
598 rotCam.setValue(matrix);
602 internalCamera->position.setValue(matrix[3][0], matrix[3][1], matrix[3][2]);
605 rotX.setRotate(SbRotation(SbVec3f(-1.0f, 0.0f, 0.0f), (
float)M_PI));
606 matrix.multLeft(rotX);
607 rotCam.setValue(matrix);
635 static void timerSensorCallback(
void *data, SoSensor *)
651 SoTimerSensor *timer =
new SoTimerSensor(timerSensorCallback, (
void *)
this);
652 timer->setInterval(0.01);
654 vpViewer::mainLoop();
666 if (!input.openFile(file_name)) {
670 SoSeparator *newscene = SoDB::readAll(&input);
672 if (newscene ==
nullptr) {
676 SoScale *taille =
new SoScale;
684 this->
scene->addChild(taille);
685 this->
scene->addChild(newscene);
693 output.openFile(name);
696 output.setBinary(TRUE);
698 SoWriteAction writeAction(&output);
699 writeAction.apply(
scene);
711 SoScale *taille =
new SoScale;
712 taille->scaleFactor.setValue(zoom, zoom, zoom);
714 SoSeparator *frame =
new SoSeparator;
716 frame->addChild(taille);
717 frame->addChild(createFrame(LONGUEUR_FLECHE * zoom, PROPORTION_FLECHE * zoom, RAYON_FLECHE * zoom));
729 scene->addChild(createFrame(LONGUEUR_FLECHE * zoom, PROPORTION_FLECHE * zoom, RAYON_FLECHE * zoom));
741 SoSeparator *newObject;
743 if (!in.openFile(iv_filename)) {
744 vpERROR_TRACE(
"Erreur lors de la lecture du fichier %s.", iv_filename);
747 newObject = SoDB::readAll(&in);
748 if (
nullptr == newObject) {
749 vpERROR_TRACE(
"Problem reading data for file <%s>.", iv_filename);
756 vpERROR_TRACE(
"Error adding object from file <%s> ", iv_filename);
787 bool identity =
true;
788 for (
unsigned int i = 0; i < 4; i++) {
789 for (
unsigned int j = 0; j < 4; j++) {
791 if (fabs(fMo[i][j] - 1) > 1e-6)
795 if (fabs(fMo[i][j]) > 1e-6)
801 if (identity ==
true) {
802 root->addChild(
object);
807 for (
unsigned int i = 0; i < 4; i++)
808 for (
unsigned int j = 0; j < 4; j++)
809 matrix[(
int)j][(int)i] = (
float)fMo[i][j];
812 rotation.setValue(matrix);
814 SoTransform *displacement =
new SoTransform;
815 SoSeparator *newNode =
new SoSeparator;
817 displacement->rotation.setValue(rotation);
818 displacement->translation.setValue(matrix[3][0], matrix[3][1], matrix[3][2]);
820 root->addChild(newNode);
821 newNode->addChild(displacement);
822 newNode->addChild(
object);
830 mainThread = SbThread::create(start_routine, (
void *)
this);
844 mainThread = SbThread::create(start_routine, (
void *)data);
859 vpViewer::exitMainLoop();
876 SbVec2s size(320, 200);
881 size = this->
internalView->getViewportRegion().getWindowSize();
882 thisroot = this->
internalView->getSceneManager()->getSceneGraph();
885 size = this->
externalView->getViewportRegion().getWindowSize();
886 thisroot = this->
externalView->getSceneManager()->getSceneGraph();
889 SbViewportRegion myViewPort(size);
924 if (
nullptr != width) {
927 if (
nullptr != height) {
941 #ifdef VISP_HAVE_MODULE_IO
957 unsigned char r, g, b;
974 SbVec2s size = this->
internalView->getViewportRegion().getWindowSize();
1006 #elif !defined(VISP_BUILD_SHARED_LIBS)
1008 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 offScreenRendering(vpSimulatorViewType view=vpSimulator::EXTERNAL, int *width=nullptr, int *height=nullptr)
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
Viewer used by the simulator.
void resize(int x, int y, bool fixed=false)
VISP_EXPORT int wait(double t0, double t)