source: osgVisual/trunk/include/object/visual_object.h @ 202

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