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

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