Visual Servoing Platform  version 3.6.1 under development (2025-02-19)
tutorial-ibvs-4pts-ogre.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #ifdef VISP_HAVE_MODULE_AR
4 #include <visp3/ar/vpAROgre.h>
5 #endif
6 #include <visp3/robot/vpSimulatorCamera.h>
7 #include <visp3/visual_features/vpFeatureBuilder.h>
8 #include <visp3/vs/vpServo.h>
9 
10 int main()
11 {
12 
13 #if defined(ENABLE_VISP_NAMESPACE)
14  using namespace VISP_NAMESPACE_NAME;
15 #endif
16 
17  try {
18  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
19  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
20 
21  // Define the target as 4 points
22  vpPoint point[4];
23  point[0].setWorldCoordinates(-0.1, -0.1, 0);
24  point[1].setWorldCoordinates(0.1, -0.1, 0);
25  point[2].setWorldCoordinates(0.1, 0.1, 0);
26  point[3].setWorldCoordinates(-0.1, 0.1, 0);
27 
28 #if defined(VISP_HAVE_OGRE)
29  // Color image used as background texture.
30  vpImage<unsigned char> background(480, 640, 255);
31 
32  // Parameters of our camera
33  vpCameraParameters cam(840, 840, background.getWidth() / 2, background.getHeight() / 2);
34 
35  // Our object
36  // A simulator with the camera parameters defined above,
37  // and the background image size
38  vpAROgre ogre;
39  ogre.setShowConfigDialog(false);
40  ogre.setCameraParameters(cam);
41  ogre.addResource("./"); // Add the path to the Sphere.mesh resource
42  ogre.init(background, false, true);
43 
44  // Create the scene that contains 4 spheres
45  // Sphere.mesh contains a sphere with 1 meter radius
46  std::vector<std::string> name(4);
47  for (unsigned int i = 0; i < 4; i++) {
48  std::ostringstream s;
49  s << "Sphere" << i;
50  name[i] = s.str();
51  ogre.load(name[i], "Sphere.mesh");
52  ogre.setScale(name[i], 0.02f, 0.02f,
53  0.02f); // Rescale the sphere to 2 cm radius
54  // Set the position of each sphere in the object frame
55  ogre.setPosition(name[i], vpTranslationVector(point[i].get_oX(), point[i].get_oY(), point[i].get_oZ()));
56  }
57 
58  // Add an optional point light source
59  Ogre::Light *light = ogre.getSceneManager()->createLight();
60  light->setDiffuseColour(1, 1, 1); // scaled RGB values
61  light->setType(Ogre::Light::LT_POINT);
62 #if (VISP_HAVE_OGRE_VERSION < (1 << 16 | 10 << 8 | 0))
63  light->setPosition((Ogre::Real)cdMo[0][3], (Ogre::Real)cdMo[1][3], (Ogre::Real)(-cdMo[2][3]));
64 #else
65  Ogre::SceneNode *spotLightNode = ogre.getSceneManager()->getRootSceneNode()->createChildSceneNode();
66  spotLightNode->attachObject(light);
67  spotLightNode->setPosition((Ogre::Real)cdMo[0][3], (Ogre::Real)cdMo[1][3], (Ogre::Real)(-cdMo[2][3]));
68 #endif
69 #endif
70 
71  vpServo task;
74  task.setLambda(0.5);
75 
76  vpFeaturePoint p[4], pd[4];
77  for (int i = 0; i < 4; i++) {
78  point[i].track(cdMo);
79  vpFeatureBuilder::create(pd[i], point[i]);
80  point[i].track(cMo);
81  vpFeatureBuilder::create(p[i], point[i]);
82  task.addFeature(p[i], pd[i]);
83  }
84 
85  vpHomogeneousMatrix wMc, wMo;
86  vpSimulatorCamera robot;
87  robot.setSamplingTime(0.040);
88  robot.getPosition(wMc);
89  wMo = wMc * cMo;
90 
91  for (unsigned int iter = 0; iter < 150; iter++) {
92  robot.getPosition(wMc);
93  cMo = wMc.inverse() * wMo;
94  for (int i = 0; i < 4; i++) {
95  point[i].track(cMo);
96  vpFeatureBuilder::create(p[i], point[i]);
97  }
98 #if defined(VISP_HAVE_OGRE)
99  // Update the scene from the new camera position
100  ogre.display(background, cMo);
101 #endif
102  vpColVector v = task.computeControlLaw();
104  vpTime::wait(robot.getSamplingTime() * 1000);
105  }
106  }
107  catch (const vpException &e) {
108  std::cout << "Catch an exception: " << e << std::endl;
109  }
110  catch (...) {
111  std::cout << "Catch an exception " << std::endl;
112  }
113 }
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:110
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:852
void setShowConfigDialog(bool showConfigDialog)
Definition: vpAROgre.h:274
void addResource(const std::string &resourceLocation)
Definition: vpAROgre.h:140
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:177
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:180
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:814
void load(const std::string &entityName, const std::string &model)
Definition: vpAROgre.cpp:859
void setPosition(const std::string &sceneName, const vpTranslationVector &wTo)
Definition: vpAROgre.cpp:884
void setScale(const std::string &sceneName, float factorx, float factory, float factorz)
Definition: vpAROgre.cpp:971
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:129
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void setLambda(double c)
Definition: vpServo.h:991
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
Class that defines the simplest robot: a free flying camera.
Class that consider the case of a translation vector.
VISP_EXPORT int wait(double t0, double t)