45 #include <visp3/core/vpConfig.h> 47 #ifdef VISP_HAVE_COIN3D_AND_GUI 49 #include <visp3/ar/vpSimulator.h> 50 #include <visp3/core/vpTime.h> 52 #include <visp3/core/vpImage.h> 54 #ifdef VISP_HAVE_MODULE_IO 55 #include <visp3/io/vpImageIo.h> 59 #include <Inventor/nodes/SoCone.h> 60 #include <Inventor/nodes/SoCoordinate3.h> 61 #include <Inventor/nodes/SoCylinder.h> 62 #include <Inventor/nodes/SoIndexedFaceSet.h> 63 #include <Inventor/nodes/SoPointLight.h> 64 #include <Inventor/nodes/SoRotationXYZ.h> 65 #include <Inventor/nodes/SoScale.h> 66 #include <Inventor/nodes/SoTranslation.h> 68 #include <Inventor/actions/SoWriteAction.h> 69 #include <Inventor/nodes/SoDirectionalLight.h> 70 #include <Inventor/nodes/SoDrawStyle.h> 71 #include <Inventor/nodes/SoEnvironment.h> 72 #include <Inventor/nodes/SoGroup.h> 73 #include <Inventor/nodes/SoMaterial.h> 77 static float pyramidVertexes[5][3] = {{0.33f, 0.33f, 0.f},
79 {-0.33f, -0.33f, 0.f},
84 static int32_t pyramidFaces[] = {
110 SoSeparator *makePyramide()
112 SoSeparator *result =
new SoSeparator;
116 SoCoordinate3 *myCoords =
new SoCoordinate3;
117 myCoords->point.setValues(0, 5, pyramidVertexes);
118 result->addChild(myCoords);
121 SoIndexedFaceSet *myFaceSet =
new SoIndexedFaceSet;
122 myFaceSet->coordIndex.setValues(0, 21, (
const int32_t *)pyramidFaces);
123 result->addChild(myFaceSet);
125 result->unrefNoDelete();
135 static SoSeparator *createArrow(
float longueur,
float proportionFleche,
float radius)
137 SoSeparator *fleche =
new SoSeparator;
140 SoTranslation *poseCylindre =
new SoTranslation;
141 SoCylinder *line =
new SoCylinder;
142 SoTranslation *posePointe =
new SoTranslation;
143 SoCone *pointe =
new SoCone;
145 float l_cylindre = longueur * (1 - proportionFleche);
146 float l_cone = longueur * proportionFleche;
147 float radius_cylindre = radius;
148 float radius_cone = radius * 5;
150 line->radius.setValue(radius_cylindre);
151 line->height.setValue(l_cylindre);
153 poseCylindre->translation.setValue(0, l_cylindre / 2, 0);
154 posePointe->translation.setValue(0.0, l_cylindre / 2 + l_cone / 2, 0);
156 pointe->bottomRadius.setValue(radius_cone);
157 pointe->height.setValue(l_cone);
159 fleche->addChild(poseCylindre);
160 fleche->addChild(line);
161 fleche->addChild(posePointe);
162 fleche->addChild(pointe);
171 #define LONGUEUR_FLECHE 1.0f 172 #define RAYON_FLECHE 0.002f 173 #define PROPORTION_FLECHE 0.1f 175 SoSeparator *createFrame(
float longueurFleche = LONGUEUR_FLECHE,
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, proportionFleche, radiusFleche);
209 frame->addChild(rouge);
210 frame->addChild(rotationY_X);
211 frame->addChild(fleche);
212 frame->addChild(vert);
213 frame->addChild(rotationX_Y);
214 frame->addChild(fleche);
215 frame->addChild(bleu);
216 frame->addChild(rotationY_Z);
217 frame->addChild(fleche);
219 frame->unrefNoDelete();
225 SoSeparator *createCameraObject(
const float zoomFactor = 1.0)
229 SoSeparator *cam =
new SoSeparator;
232 SoMaterial *myMaterial =
new SoMaterial;
233 myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0);
234 myMaterial->emissiveColor.setValue(0.5, 0.0, 0.0);
236 SoScale *taille =
new SoScale;
239 taille->scaleFactor.setValue(zoom, zoom, zoom);
242 SoMaterial *couleurBlanc =
new SoMaterial;
243 couleurBlanc->diffuseColor.setValue(1.0, 1.0, 1.0);
244 couleurBlanc->emissiveColor.setValue(1.0, 1.0, 1.0);
245 SoDrawStyle *filDeFer =
new SoDrawStyle;
246 filDeFer->style.setValue(SoDrawStyle::LINES);
247 filDeFer->lineWidth.setValue(1);
249 SoSeparator *cone =
new SoSeparator;
251 cone->addChild(makePyramide());
252 cone->addChild(couleurBlanc);
253 cone->addChild(filDeFer);
254 cone->addChild(makePyramide());
255 cone->unrefNoDelete();
257 cam->addChild(myMaterial);
258 cam->addChild(taille);
260 cam->addChild(createFrame(2.0f, 0.1f, 0.01f));
298 #if defined(VISP_HAVE_SOWIN) 300 #elif defined(VISP_HAVE_SOQT) 302 #elif defined(VISP_HAVE_SOXT) 328 #if defined(VISP_HAVE_SOWIN)
330 #elif defined(VISP_HAVE_SOQT)
332 #elif defined(VISP_HAVE_SOXT)
355 this->
scene =
new SoSeparator;
380 SoSeparator *camera =
new SoSeparator;
388 SoCube *cube =
new SoCube;
391 cube->height = 0.01f;
397 SoDB::enableRealTimeSensor(FALSE);
398 SoSceneManager::enableRealTimeUpdate(FALSE);
399 realtime = (SbTime *)SoDB::getGlobalField(
"realTime");
413 static bool firstTime =
true;
415 SoScale *taille =
new SoScale;
417 this->
scene->addChild(taille);
420 SoScale *taille = (SoScale *)this->
scene->getChild(0);
445 SoScale *taille = (SoScale *)this->
scene->getChild(index);
446 taille->scaleFactor.setValue(zoomFactor, zoomFactor, zoomFactor);
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 == NULL) {
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 (NULL == newObject) {
749 vpERROR_TRACE(
"Problem reading data for file <%s>.", iv_filename);
755 vpERROR_TRACE(
"Error adding object from file <%s> ", iv_filename);
785 bool identity =
true;
786 for (
unsigned int i = 0; i < 4; i++) {
787 for (
unsigned int j = 0; j < 4; j++) {
789 if (fabs(fMo[i][j] - 1) > 1e-6)
792 if (fabs(fMo[i][j]) > 1e-6)
798 if (identity ==
true) {
799 root->addChild(
object);
803 for (
unsigned int i = 0; i < 4; i++)
804 for (
unsigned int j = 0; j < 4; j++)
805 matrix[(
int)j][(int)i] = (
float)fMo[i][j];
808 rotation.setValue(matrix);
810 SoTransform *displacement =
new SoTransform;
811 SoSeparator *newNode =
new SoSeparator;
813 displacement->rotation.setValue(rotation);
814 displacement->translation.setValue(matrix[3][0], matrix[3][1], matrix[3][2]);
816 root->addChild(newNode);
817 newNode->addChild(displacement);
818 newNode->addChild(
object);
826 mainThread = SbThread::create(start_routine, (
void *)
this);
840 mainThread = SbThread::create(start_routine, (
void *)data);
855 vpViewer::exitMainLoop();
872 SbVec2s size(320, 200);
877 size = this->
internalView->getViewportRegion().getWindowSize();
878 thisroot = this->
internalView->getSceneManager()->getSceneGraph();
880 size = this->
externalView->getViewportRegion().getWindowSize();
881 thisroot = this->
externalView->getSceneManager()->getSceneGraph();
884 SbViewportRegion myViewPort(size);
920 if (NULL != height) {
934 #ifdef VISP_HAVE_MODULE_IO 950 unsigned char r, g, b;
951 unsigned int index = 3 * ((internal_height - i - 1) * internal_width + j);
967 SbVec2s size = this->
internalView->getViewportRegion().getWindowSize();
999 #elif !defined(VISP_BUILD_SHARED_LIBS) 1002 void dummy_vpSimulator(){};
HWND mainWindow
main Widget
SoSeparator * externalRoot
root node of the external view
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
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
vpHomogeneousMatrix inverse() const
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
static void write(const vpImage< unsigned char > &I, const std::string &filename)
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)
resize the image : Image initialization
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
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