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