8 #include <argos3/core/simulator/simulator.h>
9 #include <argos3/core/simulator/entity/composable_entity.h>
10 #include <argos3/plugins/robots/generic/simulator/camera_sensor_algorithm.h>
18 m_bShowFrustum(false),
19 m_pcEmbodiedEntity(nullptr),
20 m_pcControllableEntity(nullptr) {}
42 for(itCamera = itCamera.begin(&t_tree);
43 itCamera != itCamera.end();
49 std::string strAnchorId;
67 cProjectionMatrix(0,0) = cFocalLength.
GetX();
68 cProjectionMatrix(1,1) = cFocalLength.
GetY();
72 cProjectionMatrix(0,2) = cPrincipalPoint.
GetX();
73 cProjectionMatrix(1,2) = cPrincipalPoint.
GetY();
89 std::vector<CCameraSensorSimulatedAlgorithm*> vecSimulatedAlgorithms;
90 std::vector<CCI_CameraSensorAlgorithm*> vecAlgorithms;
92 for(itAlgorithm = itAlgorithm.begin(&(*itCamera));
93 itAlgorithm != itAlgorithm.end();
101 if(pcCIAlgorithm ==
nullptr) {
103 "\" does not inherit from CCI_CameraSensorAlgorithm");
106 pcCIAlgorithm->Init(*itAlgorithm);
108 vecSimulatedAlgorithms.push_back(pcAlgorithm);
109 vecAlgorithms.push_back(pcCIAlgorithm);
112 m_vecSensors.emplace_back(sAnchor, cOffset, cRange, cProjectionMatrix,
113 cResolution, vecSimulatedAlgorithms);
134 std::vector<std::pair<bool, CRay3> >& vecCheckedRays =
140 CVector3 cCameraLocation, cLookAt, cUp;
142 CVector3 cNearCenter, cNearTopLeft, cNearTopRight, cNearBottomLeft, cNearBottomRight;
143 CVector3 cFarCenter, cFarTopLeft, cFarTopRight, cFarBottomLeft, cFarBottomRight;
144 std::array<CPlane, 6> arrFrustumPlanes;
145 CVector3 cBoundingBoxMinCorner, cBoundingBoxMaxCorner;
146 CVector3 cBoundingBoxPosition, cBoundingBoxHalfExtents;
150 cWorldToAnchorTransform.
SetFromComponents(s_sensor.Anchor.Orientation, s_sensor.Anchor.Position);
151 cWorldToCameraTransform = cWorldToAnchorTransform * s_sensor.Offset;
152 cCameraToWorldTransform = cWorldToCameraTransform.
GetInverse();
159 cZ = cCameraLocation - cLookAt;
167 cNearCenter = cCameraLocation - cZ * s_sensor.Range.GetMin();
168 cFarCenter = cCameraLocation - cZ * s_sensor.Range.GetMax();
169 cNearTopLeft = cNearCenter + (cY * s_sensor.NearPlaneHeight) - (cX * s_sensor.NearPlaneWidth);
170 cNearTopRight = cNearCenter + (cY * s_sensor.NearPlaneHeight) + (cX * s_sensor.NearPlaneWidth);
171 cNearBottomLeft = cNearCenter - (cY * s_sensor.NearPlaneHeight) - (cX * s_sensor.NearPlaneWidth);
172 cNearBottomRight = cNearCenter - (cY * s_sensor.NearPlaneHeight) + (cX * s_sensor.NearPlaneWidth);
173 cFarTopLeft = cFarCenter + (cY * s_sensor.FarPlaneHeight) - (cX * s_sensor.FarPlaneWidth);
174 cFarTopRight = cFarCenter + (cY * s_sensor.FarPlaneHeight) + (cX * s_sensor.FarPlaneWidth);
175 cFarBottomLeft = cFarCenter - (cY * s_sensor.FarPlaneHeight) - (cX * s_sensor.FarPlaneWidth);
176 cFarBottomRight = cFarCenter - (cY * s_sensor.FarPlaneHeight) + (cX * s_sensor.FarPlaneWidth);
179 vecCheckedRays.emplace_back(
false,
CRay3(cNearTopLeft, cNearTopRight));
180 vecCheckedRays.emplace_back(
false,
CRay3(cNearTopRight, cNearBottomRight));
181 vecCheckedRays.emplace_back(
false,
CRay3(cNearBottomRight, cNearBottomLeft));
182 vecCheckedRays.emplace_back(
false,
CRay3(cNearBottomLeft, cNearTopLeft));
183 vecCheckedRays.emplace_back(
false,
CRay3(cFarTopLeft, cFarTopRight));
184 vecCheckedRays.emplace_back(
false,
CRay3(cFarTopRight, cFarBottomRight));
185 vecCheckedRays.emplace_back(
false,
CRay3(cFarBottomRight, cFarBottomLeft));
186 vecCheckedRays.emplace_back(
false,
CRay3(cFarBottomLeft, cFarTopLeft));
187 vecCheckedRays.emplace_back(
false,
CRay3(cNearTopLeft, cFarTopLeft));
188 vecCheckedRays.emplace_back(
false,
CRay3(cNearTopRight, cFarTopRight));
189 vecCheckedRays.emplace_back(
false,
CRay3(cNearBottomRight, cFarBottomRight));
190 vecCheckedRays.emplace_back(
false,
CRay3(cNearBottomLeft, cFarBottomLeft));
194 cBoundingBoxMinCorner = cNearCenter;
195 cBoundingBoxMaxCorner = cNearCenter;
197 cNearTopLeft, cNearTopRight, cNearBottomLeft, cNearBottomRight,
198 cFarTopLeft, cFarTopRight, cFarBottomLeft, cFarBottomRight
200 if(c_point.GetX() > cBoundingBoxMaxCorner.
GetX()) {
201 cBoundingBoxMaxCorner.
SetX(c_point.GetX());
203 if(c_point.GetX() < cBoundingBoxMinCorner.
GetX()) {
204 cBoundingBoxMinCorner.
SetX(c_point.GetX());
206 if(c_point.GetY() > cBoundingBoxMaxCorner.
GetY()) {
207 cBoundingBoxMaxCorner.
SetY(c_point.GetY());
209 if(c_point.GetY() < cBoundingBoxMinCorner.
GetY()) {
210 cBoundingBoxMinCorner.
SetY(c_point.GetY());
212 if(c_point.GetZ() > cBoundingBoxMaxCorner.
GetZ()) {
213 cBoundingBoxMaxCorner.
SetZ(c_point.GetZ());
215 if(c_point.GetZ() < cBoundingBoxMinCorner.
GetZ()) {
216 cBoundingBoxMinCorner.
SetZ(c_point.GetZ());
219 cBoundingBoxMaxCorner *= 0.5;
220 cBoundingBoxMinCorner *= 0.5;
221 cBoundingBoxPosition = (cBoundingBoxMaxCorner + cBoundingBoxMinCorner);
222 cBoundingBoxHalfExtents = (cBoundingBoxMaxCorner - cBoundingBoxMinCorner);
224 arrFrustumPlanes[0].SetFromThreePoints(cNearTopRight, cNearTopLeft, cFarTopLeft);
225 arrFrustumPlanes[1].SetFromThreePoints(cNearBottomLeft, cNearBottomRight, cFarBottomRight);
226 arrFrustumPlanes[2].SetFromThreePoints(cNearTopLeft, cNearBottomLeft, cFarBottomLeft);
227 arrFrustumPlanes[3].SetFromThreePoints(cNearBottomRight, cNearTopRight, cFarBottomRight);
228 arrFrustumPlanes[4].SetFromThreePoints(cNearTopLeft, cNearTopRight, cNearBottomRight);
229 arrFrustumPlanes[5].SetFromThreePoints(cFarTopRight, cFarTopLeft, cFarBottomLeft);
232 pc_algorithm->
Update(s_sensor.ProjectionMatrix,
234 cCameraToWorldTransform,
236 cBoundingBoxPosition,
237 cBoundingBoxHalfExtents);
239 vecCheckedRays.insert(std::end(vecCheckedRays),
250 "cameras",
"default",
251 "Michael Allwright [allsey87@gmail.com]",
254 "A generic multi-camera sensor capable of running various algorithms",
255 "The generic multi-camera sensor can be attached to any composable entity in\n"
256 "ARGoS that contains an embodied entity with at least one anchor. The sensor\n"
257 "can be initialized with a number of cameras each running different algorithms\n"
258 "for detecting different objects in the simulation. The sensor is designed so\n"
259 "that algorithms can project a feature in the simulation on to the virtual\n"
260 "sensor and store its 2D pixel coordinates as a reading. The implementation\n"
261 "of algorithms that behave differently, however, is also possible.\n\n"
263 "This sensor is disabled by default, and must be enabled before it can be\n"
266 "REQUIRED XML CONFIGURATION\n\n"
269 " <my_controller ...>\n"
273 " <cameras implementation=\"default\"/>\n"
277 " </my_controller>\n"
279 " </controllers>\n\n"
281 "OPTIONAL XML CONFIGURATION\n\n"
283 "It is possible to draw the frustum of each camera sensor in the OpenGL\n"
284 "visualization. This can be useful for sensor debugging but also to understand\n"
285 "what's wrong in your controller. To turn this functionality on, add the\n"
286 "attribute \"show_frustum\" as follows:\n\n"
290 " <my_controller ...>\n"
294 " <cameras implementation=\"default\" show_frustum=\"true\"/>\n"
298 " </my_controller>\n"
300 " </controllers>\n\n"
302 "To add a camera to the plugin, create a camera node as shown in the following\n"
303 "example. A camera is defined by its range (how close and how far the camera\n"
304 "can see), its anchor and its position and orientation offsets from that\n"
305 "that anchor, its focal length and principal point (which define the\n"
306 "projection matrix), and its resolution.\n\n"
310 " <my_controller ...>\n"
314 " <cameras implementation=\"default\" show_frustum=\"true\">\n"
315 " <camera id=\"camera0\" range=\"0.025:0.25\" anchor=\"origin\"\n"
316 " position=\"0.1,0,0.1\" orientation=\"90,-90,0\"\n"
317 " focal_length=\"800,800\" principal_point=\"320,240\"\n"
318 " resolution=\"640,480\"/>\n"
323 " </my_controller>\n"
325 " </controllers>\n\n"
327 "To run an algorithm on the camera sensor, simply add the algorithm as a node\n"
328 "under the camera node. At the time of writing, three algorithms are available\n"
329 "by default: led_detector, directional_led_detector, and tag_detector. Each of\n"
330 "algorithms requires a medium attribute that specifies the medium where the\n"
331 "target entities are indexed. By setting the show_rays attribute to true, you\n"
332 "can see whether or not a target was partially occluded by another object in\n"
333 "the simulation. For example:\n\n"
337 " <my_controller ...>\n"
341 " <cameras implementation=\"default\" show_frustum=\"true\">\n"
342 " <camera id=\"camera0\" range=\"0.025:0.25\" anchor=\"origin\"\n"
343 " position=\"0.1,0,0.1\" orientation=\"90,-90,0\"\n"
344 " focal_length=\"800,800\" principal_point=\"320,240\"\n"
345 " resolution=\"640,480\">\n"
346 " <led_detector medium=\"leds\" show_rays=\"true\"/>\n"
352 " </my_controller>\n"
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
The namespace containing all the ARGoS related code.
ticpp::Iterator< ticpp::Element > TConfigurationNodeIterator
The iterator for the ARGoS configuration XML node.
void GetNodeAttributeOrDefault(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer, const T &t_default)
Returns the value of a node's attribute, or the passed default value.
REGISTER_SENSOR(CEPuckProximityDefaultSensor, "epuck_proximity", "default", "Danesh Tarapore [daneshtarapore@gmail.com]", "1.0", "The E-Puck proximity sensor.", "This sensor accesses the epuck proximity sensor. For a complete description\n" "of its usage, refer to the ci_epuck_proximity_sensor.h interface. For the XML\n" "configuration, refer to the default proximity sensor.\n", "Usable")
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
virtual void Init(TConfigurationNode &t_node)
Initializes the sensor from the XML configuration tree.
virtual void Disable()
Disables updating of sensor information in the event loop.
Basic class for an entity that contains other entities.
CEntity & GetComponent(const std::string &str_component)
Returns the component with the passed string label.
An entity that contains a pointer to the user-defined controller.
std::vector< std::pair< bool, CRay3 > > & GetCheckedRays()
Returns the list of checked rays.
This entity is a link to a body in the physics engine.
const SAnchor & GetAnchor(const std::string &str_id) const
Returns the wanted anchor as a const reference.
An anchor related to the body of an entity.
The exception that wraps all errors in ARGoS.
CSquareMatrix< DIM > GetInverse() const
CVector3 GetTranslationVector() const
CRotationMatrix3 GetRotationMatrix() const
void SetFromComponents(const CRotationMatrix3 &c_rotation, const CVector3 &c_translation)
Real GetY() const
Returns the y coordinate of this vector.
Real GetX() const
Returns the x coordinate of this vector.
CVector3 & CrossProduct(const CVector3 &c_vector3)
Calculates the cross product between this vector and the passed one.
void SetY(const Real f_y)
Sets the y coordinate of this vector.
CVector3 & Rotate(const CQuaternion &c_quaternion)
Rotates this vector by the given quaternion.
Real GetX() const
Returns the x coordinate of this vector.
void SetX(const Real f_x)
Sets the x coordinate of this vector.
CVector3 & Normalize()
Normalizes this vector.
void SetZ(const Real f_z)
Sets the z coordinate of this vector.
Real GetY() const
Returns the y coordinate of this vector.
static const CVector3 Z
The z axis.
Real GetZ() const
Returns the z coordinate of this vector.
static TYPE * New(const std::string &str_label)
Creates a new object of type TYPE
SInterface::TVector m_vecInterfaces
virtual void Init(TConfigurationNode &t_tree)
Initializes the sensor from the XML configuration tree.
virtual void SetRobot(CComposableEntity &c_entity)
Sets the entity associated to this sensor.
virtual void Update()
Updates the state of the entity associated to this sensor, if the sensor is currently enabled.
CControllableEntity * m_pcControllableEntity
CEmbodiedEntity * m_pcEmbodiedEntity
std::vector< SSensor > m_vecSensors
const std::vector< std::pair< bool, CRay3 > > & GetCheckedRays() const
virtual void Update(const CSquareMatrix< 3 > &c_projection_matrix, const std::array< CPlane, 6 > &arr_frustum_planes, const CTransformationMatrix3 &c_world_to_camera_transform, const CVector3 &c_camera_location, const CVector3 &c_bounding_box_position, const CVector3 &c_bounding_box_half_extents)=0