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