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

Last change on this file since 273 was 273, checked in by Torben Dannhauer, 13 years ago
File size: 14.4 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 <string.h>
19#include <iostream>
20#include <sstream>
21#include <libxml/parser.h>
22#include <libxml/tree.h>
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
32#include <OpenThreads/ReentrantMutex>
33
34#include <osgUtil/LineSegmentIntersector>
35
36
37
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{
63        #include <leakDetection.h>
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_);
224
225        /**
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.
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
236         *  if(yourNode)
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.
244         * @param disabled : Contains after return if the module was disabled. Only true if valid configuration and definitely disabled.
245         * @return : NULL on error, otherwise pointer to the xmlNode of the queried module.
246         */ 
247        static xmlNode* getModuleXMLConfig(std::string configFilename, std::string moduleName, xmlDoc*& doc, bool& disabled);
248
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
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         */ 
264        static std::vector<std::string> getTerrainFromXMLConfig(std::string configFilename);
265
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         */ 
272        static std::string getAnimationPathFromXMLConfig(std::string configFilename);
273
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         */ 
280        static double strToDouble(std::string s);
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         */ 
288        static int strToInt(std::string s);
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         */ 
296        static bool strToBool(std::string s);
297
298        template<class T>
299        class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor
300        {
301        public:
302                FindTopMostNodeOfTypeVisitor():
303                        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
304                        _foundNode(0)
305                {}
306
307                void apply(osg::Node& node)
308                {
309                        T* result = dynamic_cast<T*>(&node);
310                        if (result)
311                        {
312                                _foundNode = result;
313                        }
314                        else
315                        {
316                                traverse(node);
317                        }
318                }
319
320                T* _foundNode;
321        };
322
323        template<class T>
324        static T* findTopMostNodeOfType(osg::Node* node)
325        {
326                if (!node) return 0;
327
328                FindTopMostNodeOfTypeVisitor<T> fnotv;
329                node->accept(fnotv);
330
331                return fnotv._foundNode;
332        }
333
334private: 
335        /**
336         * \brief This functions checks a list of nodes and all of its children for the specified module configuration.
337         *
338         * This function is used by getModuleXMLConfig() and works recursive
339         *
340         * @param node : Node to search in
341         * @param moduleName :Module name to search for.
342         * @param disabled : Contains after return if the module was disabled. Only true if valid configuration and definitely disabled.
343         * @return : NULL if the module configuration was not found, otherwise pointer to the XML Node with the configuration for the specified module.
344         */ 
345        static xmlNode* checkXMLNodeChildrenForModule(xmlNode* node, std::string moduleName, bool& disabled);
346
347        /**
348         * \brief This functions checks a list of nodes and all of its children for the scenery configuration.
349         *
350         * @param node : Node to search in
351         * @return : NULL if the module configuration was not found, otherwise pointer to the XML Node with the configuration for the specified module.
352         */ 
353        static xmlNode* checkXMLNodeChildrenForScenery(xmlNode* node);
354};
355
356} //END NAMESPACE
Note: See TracBrowser for help on using the repository browser.