source: osgVisual/trunk/include/util/visual_util.h @ 326

Last change on this file since 326 was 295, checked in by Torben Dannhauer, 13 years ago
File size: 14.7 KB
RevLine 
[32]1#pragma once
[221]2/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer
[32]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 <string.h>
19#include <iostream>
[218]20#include <sstream>
[144]21#include <libxml/parser.h>
22#include <libxml/tree.h>
[32]23
24#include <osg/Node>
25#include <osg/Group>
26#include <osg/Geode>
27#include <osg/CoordinateSystemNode>
28#include <osg/ShapeDrawable>
29
30#include <osgViewer/Viewer>
31
[144]32#include <OpenThreads/ReentrantMutex>
33
34#include <osgUtil/LineSegmentIntersector>
35
[226]36
37
[32]38#ifdef FUNFUNCTIONS_ENABLED
39#ifdef WIN32
40#include <osgViewer/api/Win32/GraphicsHandleWin32>
41#include "C:\\Program Files/Microsoft SDKs/Windows/v6.1/Include/dwmapi.h"
42#pragma comment ( lib, "C:\\Program Files/Microsoft SDKs/Windows/v6.1/Lib/dwmapi.lib" )
43#endif
44#endif
45
46
47
48namespace osgVisual
49{ 
50
51/**
52 * \brief This class provides utility functions.
53 *
54 * The utility functions are implemented as static functions if possible for easy use in other classes.
55 *
56 * \todo check why lineIntersector is unstable, check if newer and better system is available.
57 *
58 * @author Torben Dannhauer
59 * @date  Jul 2009
60 */ 
61class util
62{
[88]63        #include <leakDetection.h>
[32]64public:
65        /**
66         * \brief Construtor
67         *
68         */ 
69        util();
70
71        /**
72         * \brief Destructor
73         *
74         */ 
75        ~util();
76
77        /**
78         * \brief This functions searches in the scene graph for a node with a name
79         *
80         * This function searches in the scene graph unter the node "currNode" for a node named like the given search name.
81         * The functions returns the first node with that name.
82         *
83         * @param searchName_ : Nodename to search for
84         * @param currNode_ : Scene graph to search in.
85         * @return : if Node found: Pointer to the node. If not found: NULL
86         */ 
87        static osg::Node* findNamedNode(const std::string& searchName_, osg::Node* currNode_);
88
89        /**
90         * \brief This function creates a red cylinder of the specified size tor testing purposes.
91         *
92         * @param length_ : Length of the cylinder in meter.
93         * @param width_ : Diameter of the cylinder in meter.
94         * @param offset_ : Offset of the cylinder's origin in meter.
95         * @return : Referenced Pointer off the Geode with the created cylinder.
96         */ 
97        static osg::ref_ptr<osg::Geode> getDemoCylinder(double length_, double width_, osg::Vec3 offset_ = osg::Vec3(0.0, 0.0, 0.0) );
98       
99        /**
100         * \brief This function creates a red sphere of the specified size tor testing purposes.
101         *
102         * @param radius_ : Radius of the sphere in meter.
103         * @param offset_ : Offset of the sphere's origin in meter.
104         * @return : Referenced Pointer off the Geode with the created cylinder.
105         */ 
106        static osg::ref_ptr<osg::Geode> getDemoSphere(double radius_, osg::Vec3 offset_ = osg::Vec3(0.0, 0.0, 0.0) );
107
108        /**
109         * \brief This functions searches the scene graph for intersections of a line between two points and the model.
110         *
111         * @param start_ : Point 1 for the intersection line
112         * @param end_ : Point 2 for the intersection line
113         * @param intersection_ : vektor to the intersection point if any is found.
114         * @param node_ : Node which is the root of the scene graph to check for intersections
115         * @param intersectTraversalMask_ : Bitwise mask wich controls which nodes should be checked for intersections (e.g. to check only terrain but not clouds or sky)
116         * @return returns : True if an intersection is found.
117         */ 
118        static bool intersect(const osg::Vec3d& start_, const osg::Vec3d& end_, osg::Vec3d& intersection_, osg::Node* node_, osg::Node::NodeMask intersectTraversalMask_=0xffffffff );
119
120        /**
121         * \brief This function queries the height of terrain (hot) at a specified location.
122         *
123         * @param hot_ : Reference to write the calculated hot into.
124         * @param rootNode_ : Node which is the root of the scene graph to calculate the hot.
125         * @param lat_ : Latitude of the position to calculate the hot.
126         * @param lon_ : Longitude of the position to calculate the hot.
127         * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hot (e.g. to check only terrain but not clouds or sky)
128         * @return : True if hot calculation successful.
129         */ 
130        static bool queryHeightOfTerrain(double& hot_, osg::Node* rootNode_, double lat_, double lon_, osg::Node::NodeMask traversalMask_=0xffffffff);
131
132        /**
133         * \brief This function queries the height above terrain (hat) at a specified location in WGS84 coordinates.
134         *
135         * @param hat_ : Reference to write the calculated hat into.
136         * @param rootNode_ : Node which is the root of the scene graph to calculate the hat.
137         * @param lat_ : Latitude of the position to calculate the hat. (e.g. lat of camera)
138         * @param lon_ : Longitude of the position to calculate the hat.(e.g. lon of camera)
139         * @param height_ : Height of the position to calculate the hat.(e.g. height of camera).
140         * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hat (e.g. to check only terrain but not clouds or sky)
141         * @return :  True if hat calculation successful.
142         */ 
143        static bool queryHeightAboveTerrainInWGS84(double& hat_, osg::Node* rootNode_, double lat_, double lon_, double height_, osg::Node::NodeMask traversalMask_=0xffffffff);
144
145        /**
146         * \brief This function queries the height above terrain (hat) at a specified location in world coordinates.
147         *
148         * @param hat_ : Reference to write the calculated hat into.
149         * @param rootNode_ : Node which is the root of the scene graph to calculate the hat.
150         * @param x_ : First value of the pointer which points to the position to calculate hat for.
151         * @param y_ : Second value of the pointer which points to the position to calculate hat for.
152         * @param z_ : Third value of the pointer which points to the position to calculate hat for.
153         * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hat (e.g. to check only terrain but not clouds or sky)
154         * @return :  True if hat calculation successful.
155         */ 
156        static bool queryHeightAboveTerrainInWorld(double& hat_, osg::Node* rootNode_, double x_, double y_, double z_, osg::Node::NodeMask traversalMask_=0xffffffff);
157
158        /**
159         * \brief This function calculates the earth readius (vector length in world coordiantes from origin to location) at the specified location.
160         *
161         * @param lat_ : Latitude in radians.
162         * @param lon_ : Longitude in radians.
163         * @param rootNode_ : Root node of the scene which coordinate system node should be used.
164         * @param radius_ : Calculated earth readius in meter.
165         * @return : True if calculation successful.
166         */ 
167        static bool calculateEarthRadiusAtWGS84Coordinate(double lat_, double lon_, osg::Node* rootNode_, double& radius_);
168
169        /**
170         * \brief This function converts a WGS84 position into global OpenGL XYZ coordinates.
171         *
172         * @param lat_ : Latitude of the position to calculate XYZ.
173         * @param lon_ : Longitude of the position to calculate XYZ.
174         * @param height_ : Height of the position to calculate XYZ.
175         * @param rootNode_ : Node which is the root of the scene graph to calculate XYZ.
176         * @param x_ : X Coordinate in global OpenGL coordinates.
177         * @param y_ : Y Coordinate in global OpenGL coordinates.
178         * @param z_ : Z Coordinate in global OpenGL coordinates.
179         * @return : True if calculation successful.
180         */ 
181        static bool calculateXYZAtWGS84Coordinate(double lat_, double lon_, double height_, osg::Node* rootNode_, double& x_, double& y_, double& z_);
182
183        /**
184         * \brief This function calculates lat, lon and height of a specified camera by a specified coordinate system node.
185         *
186         * @param camera_ : Camera node which position should be calculated.
187         * @param rootNode_ : rootNode of the scene.
188         * @param lat_ : Latitude variable to save value in.
189         * @param lon_ : Longitude variable to save value in.
190         * @param height_ : Height: variable to save value in.
191         * @return : True if calculation was successful.
192         */ 
193        static bool getWGS84ofCamera( osg::Camera* camera_, osg::Node* rootNode_, double& lat_, double& lon_, double& height_ ); 
194
195        /**
196         * \brief This function returns the global XYZ coordinates of the specified camera.
197         *
198         * @param camera_ : Camera to return the position.
199         * @param x_ : X coordinate.
200         * @param y_ : Y coordinate.
201         * @param z_ : Z coordinate.
202         */ 
203        static void getXYZofCamera( osg::Camera* camera_, double& x_, double& y_, double& z_ ); 
204
205        /**
206         * \brief This function disables the close button on the openGL's windowdecoration
207         *
208         * To be honest, this function is only to play, I have found it on http://forum.openscenegraph.org and found it interesting enought to play along ;)
209         *
210         * @param viewer_ : Pointer to the applications viewer
211         * @return : true if successful
212         */ 
213        static bool removeClosebuttonOnGLWindow(osgViewer::Viewer* viewer_);
214
215        /**
216         * \brief This function make the backgroud of the GL window blurred transparent, if an backgroud with alpha is set.
217         *
218         * To be honest, this function is only to play, I have found it on http://forum.openscenegraph.org and found it interesting enought to play along ;)
219         *
220         * @param viewer_ : Pointer to the applications viewer
221         * @return : true if successful
222         */ 
223        static bool setTransparentWindowBackground(osgViewer::Viewer* viewer_);
[144]224
[145]225        /**
[146]226         * \brief Parses for the XML node of the specified module. The caller has to clean up the xmlDoc and the parser, beside it returns NULL because the queried modules is not configured.
[145]227         *
228         * To clean up, call this two functions:
229         *  xmlFreeDoc(doc);
230         *  xmlCleanupParser();
231         *
232         * Example to use this function:
233         *      xmlDoc* tmpDoc;
234         *  xmlNode* yourNode = util::getModuleXMLConfig( configFilename, "core", tmpDoc );
235         *  // use yourNode
[151]236         *  if(yourNode)
[145]237         *  {
238         *     xmlFreeDoc(tmpDoc); xmlCleanupParser();
239         *  }
240         *
241         * @param configFilename : Config Filename to parse.
242         * @param moduleName : Module name to search for.
243         * @param doc : xmlDoc to use. Must be created outside that the caller can clean it up.
[152]244         * @param disabled : Contains after return if the module was disabled. Only true if valid configuration and definitely disabled.
[145]245         * @return : NULL on error, otherwise pointer to the xmlNode of the queried module.
246         */ 
[152]247        static xmlNode* getModuleXMLConfig(std::string configFilename, std::string moduleName, xmlDoc*& doc, bool& disabled);
[144]248
[146]249        /**
250         * \brief Parses for the XML node of the scenery configuration. The caller has to clean up the xmlDoc and the parser, beside it returns NULL because the queried modules is not configured.
251         *
252         * @param configFilename : Config Filename to parse.
253         * @param doc : xmlDoc to use. Must be created outside that the caller can clean it up.
254         * @return : NULL on error, otherwise pointer to the xmlNode of the scenery configuration.
255         */ 
256        static xmlNode* getSceneryXMLConfig(std::string configFilename, xmlDoc*& doc);
257
[191]258        /**
259         * \brief This function returns the path of the terrainfile specified in the configuration file.
260         *
261         * @param configFilename : Filename of the XML configuration file.
262         * @return : On error an empty string, otherwise the terrain path specified in the configuration file.
263         */ 
[193]264        static std::vector<std::string> getTerrainFromXMLConfig(std::string configFilename);
[190]265
[191]266        /**
267         * \brief This function returns the path of the animationpath file specified in the configuration file.
268         *
269         * @param configFilename : Filename of the XML configuration file.
270         * @return : On error an empty string, otherwise the path of the animationpath specified in the configuration file.
271         */ 
[190]272        static std::string getAnimationPathFromXMLConfig(std::string configFilename);
273
[219]274        /**
275         * \brief This function converts a string into a double.
276         *
277         * @param s : String to convert.
278         * @return : double representation of the string.
279         */ 
[220]280        static double strToDouble(std::string s);
[219]281
282        /**
283         * \brief This function converts a string into an Integer.
284         *
285         * @param s : String to convert.
286         * @return : Integer representation of the string.
287         */ 
[220]288        static int strToInt(std::string s);
[219]289
290        /**
291         * \brief This function converts a string into a bool.
292         *
293         * @param s : String to convert.
294         * @return : Bool representation of the string. Returns 'false' on error.
295         */ 
[218]296        static bool strToBool(std::string s);
297
[293]298        /**
299         * \brief \todo : Kommentieren
300         *
301         * @param StartPoint
302         * @param EndPoint
303         * @param radius
304         * @param CylinderColor
305         * @param pAddToThisGroup
306         */ 
[295]307        static void AddCylinderBetweenPoints(osg::Vec3d StartPoint, osg::Vec3d EndPoint, float radius, osg::Vec4d CylinderColor, osg::Group *pAddToThisGroup);
[293]308
[273]309        template<class T>
310        class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor
311        {
312        public:
313                FindTopMostNodeOfTypeVisitor():
314                        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
315                        _foundNode(0)
316                {}
317
318                void apply(osg::Node& node)
319                {
320                        T* result = dynamic_cast<T*>(&node);
321                        if (result)
322                        {
323                                _foundNode = result;
324                        }
325                        else
326                        {
327                                traverse(node);
328                        }
329                }
330
331                T* _foundNode;
332        };
333
334        template<class T>
335        static T* findTopMostNodeOfType(osg::Node* node)
336        {
337                if (!node) return 0;
338
339                FindTopMostNodeOfTypeVisitor<T> fnotv;
340                node->accept(fnotv);
341
342                return fnotv._foundNode;
343        }
344
[144]345private: 
[145]346        /**
347         * \brief This functions checks a list of nodes and all of its children for the specified module configuration.
348         *
349         * This function is used by getModuleXMLConfig() and works recursive
350         *
351         * @param node : Node to search in
352         * @param moduleName :Module name to search for.
[152]353         * @param disabled : Contains after return if the module was disabled. Only true if valid configuration and definitely disabled.
[145]354         * @return : NULL if the module configuration was not found, otherwise pointer to the XML Node with the configuration for the specified module.
355         */ 
[152]356        static xmlNode* checkXMLNodeChildrenForModule(xmlNode* node, std::string moduleName, bool& disabled);
[146]357
358        /**
359         * \brief This functions checks a list of nodes and all of its children for the scenery configuration.
360         *
361         * @param node : Node to search in
362         * @return : NULL if the module configuration was not found, otherwise pointer to the XML Node with the configuration for the specified module.
363         */ 
364        static xmlNode* checkXMLNodeChildrenForScenery(xmlNode* node);
[293]365
[32]366};
367
368} //END NAMESPACE
Note: See TracBrowser for help on using the repository browser.