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

Last change on this file since 219 was 217, checked in by Torben Dannhauer, 14 years ago
File size: 14.7 KB
RevLine 
[32]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
[55]29#include <osgGA/CameraManipulator>
[32]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
[202]38// XML Parser
39#include <stdio.h>
40#include <libxml/parser.h>
41#include <libxml/tree.h>
42
[32]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{
[88]75        #include <leakDetection.h>
[32]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
[202]92        static visual_object* createNodeFromXMLConfig(osg::CoordinateSystemNode* sceneRoot_, xmlNode* a_node);
[32]93
[202]94
[32]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
[217]279        /**
280         * \brief This function sets the trackingId to allow to identify the visual_obejct for tracking purposes.
281         *
282         * See also getTrackingId() and trackingId
283         *
284         * @param trackindID_ : trackingId to set.
285         */ 
286        void setTrackingId(int trackingId_) {trackingId = trackingId_;};
287
288        /**
289         * \brief This function returns the trackingId to allow to identify the visual_obejct for tracking purposes.
290         *
291         * @return : -1 If no trackingId is set, othwerwise the trackingId.
292         */ 
293        int getTrackingId() {return trackingId;};
294
[32]295/*@}*/
296/** @name Label management
297 *  These functions allow to display labels attached to visual_object.
298 */
299/*@{*/
300        /**
[217]301         * \brief This function removes all labels attached to this object.
[32]302         *
303         */ 
304        void clearLabels();
305
306        /**
307         * \brief This function adds a label to the object. 
308         *
309         * @param idString_ : Identifiy string, which is used to find and manipulate the label later.
310         * @param label_ : Message to display as label.
311         * @param color_ : Textcolor. Defaults to white.
312         * @param offset_ : Offset im meter in the objects local coordinate frame
313         */ 
314        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));
315       
316        /**
317         * \brief This function removes the specified label.
318         *
319         * @param idString_ : idString of the label to remove.
320         * @return : True if a label with the specified idString_ was found, false if no label with that idString was found.
321         */ 
322        bool removeLabel(std::string idString_);
323
324        /**
325         * \brief Update the message of the label specified with the idString_.
326         *
327         * @param idString_ : idString of the label to update.
328         * @param label_ : New message to set.
329         * @return : Return true if the specified label was found, return false if the specified label was not found.
330         */ 
331        bool updateLabelText(std::string idString_, std::string label_);
332
333        /**
334         * \brief returns the message string of the specified label.
335         *
336         * @param idString_ : idString of the label to return message.
337         * @return : Pointer to the Label (osgText::Text) if found, otherwise NULL
338         */ 
339        osgText::Text* getLabel(std::string idString_); 
340
341        /**
342         * \brief This functions sets the draw mode of the specified label.
343         *
344         * @param idString_ : Label to configure.
345         * @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)
346         * @return : Return true if the specified label was found, return false if the specified label was not found.
347         */ 
348        bool setDrawLabelAsOverlay(std::string idString_, bool drawAsOverlay);
349
350        /**
351         * \brief This function returns if the specified label is configured as overlay.
352         *
353         * @param idString_ : Label to configure.
354         * @return : True if configured as overlay, false if configured for simple draw
355         */ 
356        bool getDrawLabelAsOverlay(std::string idString_ );
357/*@}*/
358
359protected:
360        /**
361         * \brief Callback class for updating the visual_object while traversing the scenegraph in the event stage.
362         *
363         * @author Torben Dannhauer
364         * @date  Jul 2009
365         */ 
366        class visual_objectPositionCallback : public osg::NodeCallback
367        {
368        public:
369                /**
370                 * \brief Constructor: Empty
371                 *
372                 */ 
373                visual_objectPositionCallback()
374                {
375                   // nothing
376                }
377           
378                /**
379                 * \brief This function is executed by the callback during update traversal.
380                 *
381                 */ 
382                virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
383        };
384
385        osg::Vec3 upVector;
386       
387// Position
388        /**
389         * Latitude of the object.
390         */ 
391        double lat;
392
393        /**
394         * Longitude of the object.
395         */ 
396        double lon;
397
398        /**
399         * Altitude of the object over the ellipsoid.
400         */ 
401        double alt;
402
403// Attitude
404        /**
405         * Azimuth angle (Rotation along Z axis, "heading") of the object
406         */ 
407        double azimuthAngle_psi;
408
409        /**
410         * Pitch ("nose relative to horizon") angle of the object
411         */ 
412        double pitchAngle_theta;
413
414        /**
415         * Bank angle of the object.
416         */ 
417        double bankAngle_phi;
418
419// Scale
420        /**
421         * Object scale along X axis.
422         */ 
423        double scaleX;
424
425        /**
426         * Object scale along Y axis.
427         */ 
428        double scaleY;
429
430        /**
431         * Object scale along Z axis.
432         */ 
433        double scaleZ;
434
435// Camera offset
436        /**
437         * This is the matrix containing the camera position for the  manipulators
438         */ 
439        osg::Matrixd cameraMatrix;
440
441        /**
442         * This is the camera matrix containing offset translation
443         */ 
444        osg::Matrix cameraTranslationOffset;
445
446        /**
447         * This is the camera matrix containing offset rotation
448         */ 
449        osg::Matrix cameraRotationOffset;
450
451        /**
452         * Geometry offset (If a model's coordinate system has wrong orientation.)
453         */ 
454        osg::Quat geometry_offset_rotation;     
455
456        /**
457         * Pointer to the geometry, associated with this object.
458         */ 
459        osg::ref_ptr<osg::Group> geometry;
460
461        /**
462         * Pointer to the updater class which updates this visual_object.
463         */ 
464        osg::ref_ptr<object_updater> updater;
465
[217]466        /**
467         * Tracking ID of the visual_object. Used to identify which node should be tracked by tracking-manipulators.
468         */ 
469        int trackingId;
470
[32]471// Labels
472        /**
473         * Pointer to the labels group which holds all labels associated with this object.
474         */ 
475        osg::ref_ptr<osg::Geode> labels;
476
477        // Friend classes
478        friend class visual_objectPositionCallback; // To allow the callback access to all member variables.
479        friend class object_updater;    // To allow updater to modify all members.
480
481};
482
483} // END NAMESPACE
Note: See TracBrowser for help on using the repository browser.