1 | #pragma once |
---|
2 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer |
---|
3 | * |
---|
4 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
5 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
6 | * (at your option) any later version. The full license is in LICENSE file |
---|
7 | * included with this distribution, and on the openscenegraph.org website. |
---|
8 | * |
---|
9 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
10 | * You have to aquire licenses for all used proprietary modules. |
---|
11 | * |
---|
12 | * This library is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * OpenSceneGraph Public License for more details. |
---|
16 | */ |
---|
17 | |
---|
18 | #include <osg/MatrixTransform> |
---|
19 | #include <osg/Matrix> |
---|
20 | #include <osg/NodeCallback> |
---|
21 | #include <osg/CoordinateSystemNode> |
---|
22 | #include <osg/Notify> |
---|
23 | |
---|
24 | #include <osgDB/ReadFile> |
---|
25 | #include <osgDB/FileUtils> |
---|
26 | |
---|
27 | #include <osgText/Text> |
---|
28 | |
---|
29 | #include <osgGA/CameraManipulator> |
---|
30 | |
---|
31 | #include <object_updater.h> |
---|
32 | #include <visual_dataIO.h> |
---|
33 | #include <visual_util.h> |
---|
34 | |
---|
35 | #include <string.h> |
---|
36 | #include <iostream> |
---|
37 | |
---|
38 | // XML Parser |
---|
39 | #include <stdio.h> |
---|
40 | #include <libxml/parser.h> |
---|
41 | #include <libxml/tree.h> |
---|
42 | |
---|
43 | namespace osgVisual |
---|
44 | { |
---|
45 | class visual_objectPositionCallback; |
---|
46 | class object_updater; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * \brief Standard namespace of osgVisual |
---|
51 | * |
---|
52 | */ |
---|
53 | namespace osgVisual |
---|
54 | { |
---|
55 | |
---|
56 | /** |
---|
57 | * \brief This class provides object management for displaying objects in the 3D scene. |
---|
58 | * |
---|
59 | * It allows to control an object in position, attitude and size. To control this object automatically, use object_updaters. |
---|
60 | * To object itself is invisible, you have to connect a visible geometry to this object. |
---|
61 | * To display geometries, it provides function to load geometries by geometry pointer or filename. |
---|
62 | * To correct wrong coordinate frames in the geometries to display, |
---|
63 | * you can configure a geometry off set in translation in translation and rotation |
---|
64 | * For object mounted camera, you can configure the camera offset in rotation and translation. |
---|
65 | * |
---|
66 | * All angles are defined in degree. |
---|
67 | * |
---|
68 | * Derive from this class to implement your custom visual_object. |
---|
69 | * |
---|
70 | * \todo: Labelmanagement to allow to display a label attached to this object: set/unset label, size, color, offset to object, LOD for label display. |
---|
71 | * |
---|
72 | * @author Torben Dannhauer |
---|
73 | * @date Apr 2010 |
---|
74 | */ |
---|
75 | class visual_object : public osg::MatrixTransform |
---|
76 | { |
---|
77 | #include <leakDetection.h> |
---|
78 | public: |
---|
79 | /** |
---|
80 | * \brief Constuctor: Adds this object to the scenegraph, |
---|
81 | * initializes this object and installs the callback to calculate its local to world matrix |
---|
82 | * |
---|
83 | * @param sceneRoot_ : Scenegraph to add this object to. |
---|
84 | * @param nodeName_ : Name of this object, is used for further identification. |
---|
85 | */ |
---|
86 | visual_object( osg::CoordinateSystemNode* sceneRoot_, std::string nodeName_ ); |
---|
87 | |
---|
88 | /** |
---|
89 | * \brief Empty destructor. |
---|
90 | * |
---|
91 | */ |
---|
92 | ~visual_object(); |
---|
93 | |
---|
94 | static visual_object* createNodeFromXMLConfig(osg::CoordinateSystemNode* sceneRoot_, xmlNode* a_node); |
---|
95 | |
---|
96 | |
---|
97 | /** @name Position and attitude |
---|
98 | * These functions control objects position and attitude |
---|
99 | */ |
---|
100 | /*@{*/ |
---|
101 | |
---|
102 | /** |
---|
103 | * \brief This function updates the objects position and attitude. |
---|
104 | * |
---|
105 | * @param lat_ : Latitude to set. |
---|
106 | * @param lon_ : Longitude to set. |
---|
107 | * @param alt_ : Altitude over ellipsoid to set. |
---|
108 | * @param azimuthAngle_psi_ : Psi angle () to set. |
---|
109 | * @param pitchAngle_theta_ : Theta angle () to set. |
---|
110 | * @param bankAngle_phi_ : Phi angle () to set. |
---|
111 | * |
---|
112 | * All angles are rad! |
---|
113 | * |
---|
114 | * \todo: Erklären welche Wirkung die drei Winkel haben. |
---|
115 | */ |
---|
116 | void setNewPositionAttitude( double lat_, double lon_, double alt_, double azimuthAngle_psi_, double pitchAngle_theta_, double bankAngle_phi_ ); |
---|
117 | |
---|
118 | /** |
---|
119 | * \brief This function updates the objects position. |
---|
120 | * |
---|
121 | * @param lat_ : Latitude to set. |
---|
122 | * @param lon_ : Longitude to set. |
---|
123 | * @param alt_ : Altitude over ellipsoid to set. |
---|
124 | */ |
---|
125 | void setNewPosition( double lat_, double lon_, double alt_ ); |
---|
126 | |
---|
127 | /** |
---|
128 | * \brief This function updates the objects attitude. |
---|
129 | * |
---|
130 | * @param azimuthAngle_psi_ : Psi angle () to set. |
---|
131 | * @param pitchAngle_theta_ : Theta angle () to set. |
---|
132 | * @param bankAngle_phi_ : Phi angle () to set. |
---|
133 | * |
---|
134 | * All angles are rad! |
---|
135 | * |
---|
136 | * \todo: Erklären welche Wirkung die drei Winkel haben. |
---|
137 | */ |
---|
138 | void setNewAttitude( double azimuthAngle_psi_, double pitchAngle_theta_, double bankAngle_phi_ ); |
---|
139 | /*@}*/ |
---|
140 | /** @name Geometry management |
---|
141 | * These functions control which geometry visual_object should display. |
---|
142 | */ |
---|
143 | /*@{*/ |
---|
144 | /** |
---|
145 | * \brief This function configures the geometry offset |
---|
146 | * |
---|
147 | * @param rotX_ : Geometry rotation along the X axis. |
---|
148 | * @param rotY_ : Geometry rotation along the Y axis. |
---|
149 | * @param rotZ_ : Geometry rotation along the Z axis. |
---|
150 | * |
---|
151 | * It rotates along the axis of the visual_object, not along the geometry axis! |
---|
152 | * |
---|
153 | * \todo: Erklären, welche Achsen welche Richtung sind. |
---|
154 | */ |
---|
155 | void setGeometryOffset( double rotX_, double rotY_, double rotZ_ ); |
---|
156 | |
---|
157 | /** |
---|
158 | * \brief This function set the scale to all 3 axis. Use this function to scale objects without distortion. |
---|
159 | * |
---|
160 | * @param scale_ : Scale to set for every axis. |
---|
161 | */ |
---|
162 | void setScale( double scale_ ); |
---|
163 | |
---|
164 | /** |
---|
165 | * \brief This function sets the scale factor for all 3 axis independently. |
---|
166 | * |
---|
167 | * @param scaleX_ : Scalefactor along X axis. |
---|
168 | * @param scaleY_ : Scalefactor along Y axis. |
---|
169 | * @param scaleZ_ : Scalefactor along Z axis. |
---|
170 | */ |
---|
171 | void setScale( double scaleX_, double scaleY_, double scaleZ_ ); |
---|
172 | |
---|
173 | /** |
---|
174 | * \brief this function loads a geometry from a file and connects it to this visual_object. All filetypes OSG is aware of can be used. |
---|
175 | * |
---|
176 | * @param filename_ : File to load and connect. |
---|
177 | * @return : True if loading was successfully |
---|
178 | */ |
---|
179 | bool loadGeometry( std::string filename_); |
---|
180 | |
---|
181 | /** |
---|
182 | * \brief This function connects a geometry to this visual_object. |
---|
183 | * |
---|
184 | * @param geometry_ : Geometry to connect. |
---|
185 | * @return : True if successful. |
---|
186 | */ |
---|
187 | bool setGeometry(osg::Node* geometry_); |
---|
188 | |
---|
189 | /** |
---|
190 | * \brief This function disconnects the connected object. |
---|
191 | * |
---|
192 | */ |
---|
193 | void unsetGeometry(); |
---|
194 | |
---|
195 | /** |
---|
196 | * \brief This function returns the pointer to the loaded geometry. |
---|
197 | * |
---|
198 | * It returns NULL if no geometry is connected. |
---|
199 | * |
---|
200 | * @return : Pointer to the geometry if loaded, otherwise NULL. |
---|
201 | */ |
---|
202 | osg::Node* getGeometry(){return geometry;} |
---|
203 | |
---|
204 | /*@}*/ |
---|
205 | /** @name Object updater |
---|
206 | * These functions control objects updater. |
---|
207 | */ |
---|
208 | /*@{*/ |
---|
209 | /** |
---|
210 | * \brief This function adds an Updater to this object. |
---|
211 | * |
---|
212 | * The updaters postUpdate und preUpdate functions are executed during eventTraversal traversal. |
---|
213 | * The preUdate is executed before the objects matrix is calculated from position, the postUpdate is executed after the matrix calculation. |
---|
214 | * |
---|
215 | * @param updater_ : Updater to add. |
---|
216 | */ |
---|
217 | void addUpdater( object_updater* updater_ ); |
---|
218 | |
---|
219 | /** |
---|
220 | * \brief Removes all updater from the visual_object. |
---|
221 | * |
---|
222 | */ |
---|
223 | void clearAllUpdater(); |
---|
224 | |
---|
225 | /** |
---|
226 | * \brief This function returns a vector containing pointer to all updater of the object. |
---|
227 | * |
---|
228 | * @return : vector with pointers to all object_updater. |
---|
229 | */ |
---|
230 | std::vector<object_updater*> getUpdaterList(); |
---|
231 | |
---|
232 | /*@}*/ |
---|
233 | /** @name Camera management |
---|
234 | * These functions control the camera attitude and position. |
---|
235 | */ |
---|
236 | /*@{*/ |
---|
237 | /** |
---|
238 | * \brief This function returns the camera matrix of the scene camera. |
---|
239 | * |
---|
240 | * @return : Camera matrix |
---|
241 | */ |
---|
242 | osg::Matrixd& getCameraMatrix() {return cameraMatrix;}; |
---|
243 | |
---|
244 | /** |
---|
245 | * \brief This function rotates and translate the objects camera position. |
---|
246 | * |
---|
247 | * The translation coordinate frame is object fixed, the rotation coordinate frame is camera fixed. |
---|
248 | * |
---|
249 | * @param x_ : Translation along object's x axis. |
---|
250 | * @param y_ : Translation along object's y axis. |
---|
251 | * @param z_ : Translation along object's z axis (positive axis points downwards). |
---|
252 | * @param rotX_ : Rotation around camera's x axis in [rad]. |
---|
253 | * @param rotY_ : Rotation around camera's y axis in [rad]. |
---|
254 | * @param rotZ_ : Rotation around camera's z axis in [rad]. |
---|
255 | */ |
---|
256 | void setCameraOffset(double x_, double y_, double z_, double rotX_, double rotY_, double rotZ_); |
---|
257 | |
---|
258 | /** |
---|
259 | * \brief This function configures the camera offset rotation in that object. |
---|
260 | * |
---|
261 | * The coordinate frame for this rotation is camera fixed, NOT object fixed |
---|
262 | * (e.g. rotation around x axis (bank) rotatates always around the cameras visual axis, |
---|
263 | * independent from pitch or azimuth) |
---|
264 | * |
---|
265 | * @param rotX_ : Rotation around camera's x axis in [rad]. |
---|
266 | * @param rotY_ : Rotation around camera's y axis in [rad]. |
---|
267 | * @param rotZ_ : Rotation around camera's z axis in [rad]. |
---|
268 | */ |
---|
269 | void setCameraOffsetRotation(double rotX_, double rotY_, double rotZ_); |
---|
270 | |
---|
271 | /** |
---|
272 | * \brief This function configures the camera offset translation in that object. |
---|
273 | * |
---|
274 | * @param x_ : Translate in longitudinal direction. x>0: Camera is in front if the ob |
---|
275 | * ject, x<0 behind the object. |
---|
276 | * @param y_ : Translate in transversal direction. x>0: Camera is right of the object, x<0 left of the object. |
---|
277 | * @param z_ : Translate in vertical direction. x>0: Camera is below the Object, x<0 above the object. (Positiv Z axis points downwards!) |
---|
278 | */ |
---|
279 | void setCameraOffsetTranslation( double x_, double y_, double z_); |
---|
280 | |
---|
281 | /** |
---|
282 | * \brief This function sets the trackingId to allow to identify the visual_obejct for tracking purposes. |
---|
283 | * |
---|
284 | * See also getTrackingId() and trackingId |
---|
285 | * |
---|
286 | * @param trackindID_ : trackingId to set. |
---|
287 | */ |
---|
288 | void setTrackingId(int trackingId_) {trackingId = trackingId_;}; |
---|
289 | |
---|
290 | /** |
---|
291 | * \brief This function returns the trackingId to allow to identify the visual_obejct for tracking purposes. |
---|
292 | * |
---|
293 | * @return : -1 If no trackingId is set, othwerwise the trackingId. |
---|
294 | */ |
---|
295 | int getTrackingId() {return trackingId;}; |
---|
296 | |
---|
297 | /*@}*/ |
---|
298 | /** @name Label management |
---|
299 | * These functions allow to display labels attached to visual_object. |
---|
300 | */ |
---|
301 | /*@{*/ |
---|
302 | /** |
---|
303 | * \brief This function removes all labels attached to this object. |
---|
304 | * |
---|
305 | */ |
---|
306 | void clearLabels(); |
---|
307 | |
---|
308 | /** |
---|
309 | * \brief This function adds a label to the object. |
---|
310 | * |
---|
311 | * @param idString_ : Identifiy string, which is used to find and manipulate the label later. |
---|
312 | * @param label_ : Message to display as label. |
---|
313 | * @param color_ : Textcolor. Defaults to white. |
---|
314 | * @param offset_ : Offset im meter in the objects local coordinate frame |
---|
315 | */ |
---|
316 | void addLabel(std::string idString_, std::string label_, osg::Vec4 color_ = osg::Vec4(1.0f,1.0f,1.0f,1.0f), osg::Vec3 offset_ = osg::Vec3(0,0,0)); |
---|
317 | |
---|
318 | /** |
---|
319 | * \brief This function removes the specified label. |
---|
320 | * |
---|
321 | * @param idString_ : idString of the label to remove. |
---|
322 | * @return : True if a label with the specified idString_ was found, false if no label with that idString was found. |
---|
323 | */ |
---|
324 | bool removeLabel(std::string idString_); |
---|
325 | |
---|
326 | /** |
---|
327 | * \brief Update the message of the label specified with the idString_. |
---|
328 | * |
---|
329 | * @param idString_ : idString of the label to update. |
---|
330 | * @param label_ : New message to set. |
---|
331 | * @return : Return true if the specified label was found, return false if the specified label was not found. |
---|
332 | */ |
---|
333 | bool updateLabelText(std::string idString_, std::string label_); |
---|
334 | |
---|
335 | /** |
---|
336 | * \brief returns the message string of the specified label. |
---|
337 | * |
---|
338 | * @param idString_ : idString of the label to return message. |
---|
339 | * @return : Pointer to the Label (osgText::Text) if found, otherwise NULL |
---|
340 | */ |
---|
341 | osgText::Text* getLabel(std::string idString_); |
---|
342 | |
---|
343 | /** |
---|
344 | * \brief This functions sets the draw mode of the specified label. |
---|
345 | * |
---|
346 | * @param idString_ : Label to configure. |
---|
347 | * @param drawAsOverlay : Set true to draw the label always over the scene. Set false to draw it 3D into the scene (maybe occluded party by objects) |
---|
348 | * @return : Return true if the specified label was found, return false if the specified label was not found. |
---|
349 | */ |
---|
350 | bool setDrawLabelAsOverlay(std::string idString_, bool drawAsOverlay); |
---|
351 | |
---|
352 | /** |
---|
353 | * \brief This function returns if the specified label is configured as overlay. |
---|
354 | * |
---|
355 | * @param idString_ : Label to configure. |
---|
356 | * @return : True if configured as overlay, false if configured for simple draw |
---|
357 | */ |
---|
358 | bool getDrawLabelAsOverlay(std::string idString_ ); |
---|
359 | /*@}*/ |
---|
360 | |
---|
361 | protected: |
---|
362 | /** |
---|
363 | * \brief Callback class for updating the visual_object while traversing the scenegraph in the event stage. |
---|
364 | * |
---|
365 | * @author Torben Dannhauer |
---|
366 | * @date Jul 2009 |
---|
367 | */ |
---|
368 | class visual_objectPositionCallback : public osg::NodeCallback |
---|
369 | { |
---|
370 | public: |
---|
371 | /** |
---|
372 | * \brief Constructor: Empty |
---|
373 | * |
---|
374 | */ |
---|
375 | visual_objectPositionCallback() |
---|
376 | { |
---|
377 | // nothing |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * \brief This function is executed by the callback during update traversal. |
---|
382 | * |
---|
383 | */ |
---|
384 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv); |
---|
385 | }; |
---|
386 | |
---|
387 | osg::Vec3 upVector; |
---|
388 | |
---|
389 | // Position |
---|
390 | /** |
---|
391 | * Latitude of the object in RAD. |
---|
392 | */ |
---|
393 | double lat; |
---|
394 | |
---|
395 | /** |
---|
396 | * Longitude of the object in RAD. |
---|
397 | */ |
---|
398 | double lon; |
---|
399 | |
---|
400 | /** |
---|
401 | * Altitude of the object over the ellipsoid in meter. |
---|
402 | */ |
---|
403 | double alt; |
---|
404 | |
---|
405 | // Attitude |
---|
406 | /** |
---|
407 | * Azimuth angle (Rotation along Z axis, "heading") of the object in RAD. |
---|
408 | */ |
---|
409 | double azimuthAngle_psi; |
---|
410 | |
---|
411 | /** |
---|
412 | * Pitch ("nose relative to horizon") angle of the object in RAD. |
---|
413 | */ |
---|
414 | double pitchAngle_theta; |
---|
415 | |
---|
416 | /** |
---|
417 | * Bank angle of the object in RAD. |
---|
418 | */ |
---|
419 | double bankAngle_phi; |
---|
420 | |
---|
421 | // Scale |
---|
422 | /** |
---|
423 | * Object scale along X axis. |
---|
424 | */ |
---|
425 | double scaleX; |
---|
426 | |
---|
427 | /** |
---|
428 | * Object scale along Y axis. |
---|
429 | */ |
---|
430 | double scaleY; |
---|
431 | |
---|
432 | /** |
---|
433 | * Object scale along Z axis. |
---|
434 | */ |
---|
435 | double scaleZ; |
---|
436 | |
---|
437 | // Camera offset |
---|
438 | /** |
---|
439 | * This is the matrix containing the camera position for the manipulators |
---|
440 | */ |
---|
441 | osg::Matrixd cameraMatrix; |
---|
442 | |
---|
443 | /** |
---|
444 | * This is the camera matrix containing offset translation |
---|
445 | */ |
---|
446 | osg::Matrix cameraTranslationOffset; |
---|
447 | |
---|
448 | /** |
---|
449 | * This is the camera matrix containing offset rotation |
---|
450 | */ |
---|
451 | osg::Matrix cameraRotationOffset; |
---|
452 | |
---|
453 | /** |
---|
454 | * Geometry offset (If a model's coordinate system has wrong orientation.) |
---|
455 | */ |
---|
456 | osg::Quat geometry_offset_rotation; |
---|
457 | |
---|
458 | /** |
---|
459 | * Pointer to the geometry, associated with this object. |
---|
460 | */ |
---|
461 | osg::ref_ptr<osg::Group> geometry; |
---|
462 | |
---|
463 | /** |
---|
464 | * Pointer to the updater class which updates this visual_object. |
---|
465 | */ |
---|
466 | osg::ref_ptr<object_updater> updater; |
---|
467 | |
---|
468 | /** |
---|
469 | * Tracking ID of the visual_object. Used to identify which node should be tracked by tracking-manipulators. |
---|
470 | */ |
---|
471 | int trackingId; |
---|
472 | |
---|
473 | // Labels |
---|
474 | /** |
---|
475 | * Pointer to the labels group which holds all labels associated with this object. |
---|
476 | */ |
---|
477 | osg::ref_ptr<osg::Geode> labels; |
---|
478 | |
---|
479 | // Friend classes |
---|
480 | friend class visual_objectPositionCallback; // To allow the callback access to all member variables. |
---|
481 | friend class object_updater; // To allow updater to modify all members. |
---|
482 | |
---|
483 | }; |
---|
484 | |
---|
485 | } // END NAMESPACE |
---|