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

Last change on this file since 226 was 226, checked in by Torben Dannhauer, 13 years ago

TRacking nodes by XML config should work now

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