source: osgVisual/include/util/visual_util.h @ 146

Last change on this file since 146 was 146, checked in by Torben Dannhauer, 14 years ago
File size: 12.1 KB
Line 
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 <string.h>
19#include <iostream>
20#include <libxml/parser.h>
21#include <libxml/tree.h>
22
23#include <osg/Node>
24#include <osg/Group>
25#include <osg/Geode>
26#include <osg/CoordinateSystemNode>
27#include <osg/ShapeDrawable>
28
29#include <osgViewer/Viewer>
30
31#include <OpenThreads/ReentrantMutex>
32
33#include <osgUtil/LineSegmentIntersector>
34
35#ifdef FUNFUNCTIONS_ENABLED
36#ifdef WIN32
37#include <osgViewer/api/Win32/GraphicsHandleWin32>
38#include "C:\\Program Files/Microsoft SDKs/Windows/v6.1/Include/dwmapi.h"
39#pragma comment ( lib, "C:\\Program Files/Microsoft SDKs/Windows/v6.1/Lib/dwmapi.lib" )
40#endif
41#endif
42
43
44
45namespace osgVisual
46{ 
47
48/**
49 * \brief This class provides utility functions.
50 *
51 * The utility functions are implemented as static functions if possible for easy use in other classes.
52 *
53 * \todo check why lineIntersector is unstable, check if newer and better system is available.
54 *
55 * @author Torben Dannhauer
56 * @date  Jul 2009
57 */ 
58class util
59{
60        #include <leakDetection.h>
61public:
62        /**
63         * \brief Construtor
64         *
65         */ 
66        util();
67
68        /**
69         * \brief Destructor
70         *
71         */ 
72        ~util();
73
74        /**
75         * \brief This functions searches in the scene graph for a node with a name
76         *
77         * This function searches in the scene graph unter the node "currNode" for a node named like the given search name.
78         * The functions returns the first node with that name.
79         *
80         * @param searchName_ : Nodename to search for
81         * @param currNode_ : Scene graph to search in.
82         * @return : if Node found: Pointer to the node. If not found: NULL
83         */ 
84        static osg::Node* findNamedNode(const std::string& searchName_, osg::Node* currNode_);
85
86        /**
87         * \brief This function creates a red cylinder of the specified size tor testing purposes.
88         *
89         * @param length_ : Length of the cylinder in meter.
90         * @param width_ : Diameter of the cylinder in meter.
91         * @param offset_ : Offset of the cylinder's origin in meter.
92         * @return : Referenced Pointer off the Geode with the created cylinder.
93         */ 
94        static osg::ref_ptr<osg::Geode> getDemoCylinder(double length_, double width_, osg::Vec3 offset_ = osg::Vec3(0.0, 0.0, 0.0) );
95       
96        /**
97         * \brief This function creates a red sphere of the specified size tor testing purposes.
98         *
99         * @param radius_ : Radius of the sphere in meter.
100         * @param offset_ : Offset of the sphere's origin in meter.
101         * @return : Referenced Pointer off the Geode with the created cylinder.
102         */ 
103        static osg::ref_ptr<osg::Geode> getDemoSphere(double radius_, osg::Vec3 offset_ = osg::Vec3(0.0, 0.0, 0.0) );
104
105        /**
106         * \brief This functions searches the scene graph for intersections of a line between two points and the model.
107         *
108         * @param start_ : Point 1 for the intersection line
109         * @param end_ : Point 2 for the intersection line
110         * @param intersection_ : vektor to the intersection point if any is found.
111         * @param node_ : Node which is the root of the scene graph to check for intersections
112         * @param intersectTraversalMask_ : Bitwise mask wich controls which nodes should be checked for intersections (e.g. to check only terrain but not clouds or sky)
113         * @return returns : True if an intersection is found.
114         */ 
115        static bool intersect(const osg::Vec3d& start_, const osg::Vec3d& end_, osg::Vec3d& intersection_, osg::Node* node_, osg::Node::NodeMask intersectTraversalMask_=0xffffffff );
116
117        /**
118         * \brief This function queries the height of terrain (hot) at a specified location.
119         *
120         * @param hot_ : Reference to write the calculated hot into.
121         * @param rootNode_ : Node which is the root of the scene graph to calculate the hot.
122         * @param lat_ : Latitude of the position to calculate the hot.
123         * @param lon_ : Longitude of the position to calculate the hot.
124         * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hot (e.g. to check only terrain but not clouds or sky)
125         * @return : True if hot calculation successful.
126         */ 
127        static bool queryHeightOfTerrain(double& hot_, osg::Node* rootNode_, double lat_, double lon_, osg::Node::NodeMask traversalMask_=0xffffffff);
128
129        /**
130         * \brief This function queries the height above terrain (hat) at a specified location in WGS84 coordinates.
131         *
132         * @param hat_ : Reference to write the calculated hat into.
133         * @param rootNode_ : Node which is the root of the scene graph to calculate the hat.
134         * @param lat_ : Latitude of the position to calculate the hat. (e.g. lat of camera)
135         * @param lon_ : Longitude of the position to calculate the hat.(e.g. lon of camera)
136         * @param height_ : Height of the position to calculate the hat.(e.g. height of camera).
137         * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hat (e.g. to check only terrain but not clouds or sky)
138         * @return :  True if hat calculation successful.
139         */ 
140        static bool queryHeightAboveTerrainInWGS84(double& hat_, osg::Node* rootNode_, double lat_, double lon_, double height_, osg::Node::NodeMask traversalMask_=0xffffffff);
141
142        /**
143         * \brief This function queries the height above terrain (hat) at a specified location in world coordinates.
144         *
145         * @param hat_ : Reference to write the calculated hat into.
146         * @param rootNode_ : Node which is the root of the scene graph to calculate the hat.
147         * @param x_ : First value of the pointer which points to the position to calculate hat for.
148         * @param y_ : Second value of the pointer which points to the position to calculate hat for.
149         * @param z_ : Third value of the pointer which points to the position to calculate hat for.
150         * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hat (e.g. to check only terrain but not clouds or sky)
151         * @return :  True if hat calculation successful.
152         */ 
153        static bool queryHeightAboveTerrainInWorld(double& hat_, osg::Node* rootNode_, double x_, double y_, double z_, osg::Node::NodeMask traversalMask_=0xffffffff);
154
155        /**
156         * \brief This function calculates the earth readius (vector length in world coordiantes from origin to location) at the specified location.
157         *
158         * @param lat_ : Latitude in radians.
159         * @param lon_ : Longitude in radians.
160         * @param rootNode_ : Root node of the scene which coordinate system node should be used.
161         * @param radius_ : Calculated earth readius in meter.
162         * @return : True if calculation successful.
163         */ 
164        static bool calculateEarthRadiusAtWGS84Coordinate(double lat_, double lon_, osg::Node* rootNode_, double& radius_);
165
166        /**
167         * \brief This function converts a WGS84 position into global OpenGL XYZ coordinates.
168         *
169         * @param lat_ : Latitude of the position to calculate XYZ.
170         * @param lon_ : Longitude of the position to calculate XYZ.
171         * @param height_ : Height of the position to calculate XYZ.
172         * @param rootNode_ : Node which is the root of the scene graph to calculate XYZ.
173         * @param x_ : X Coordinate in global OpenGL coordinates.
174         * @param y_ : Y Coordinate in global OpenGL coordinates.
175         * @param z_ : Z Coordinate in global OpenGL coordinates.
176         * @return : True if calculation successful.
177         */ 
178        static bool calculateXYZAtWGS84Coordinate(double lat_, double lon_, double height_, osg::Node* rootNode_, double& x_, double& y_, double& z_);
179
180        /**
181         * \brief This function calculates lat, lon and height of a specified camera by a specified coordinate system node.
182         *
183         * @param camera_ : Camera node which position should be calculated.
184         * @param rootNode_ : rootNode of the scene.
185         * @param lat_ : Latitude variable to save value in.
186         * @param lon_ : Longitude variable to save value in.
187         * @param height_ : Height: variable to save value in.
188         * @return : True if calculation was successful.
189         */ 
190        static bool getWGS84ofCamera( osg::Camera* camera_, osg::Node* rootNode_, double& lat_, double& lon_, double& height_ ); 
191
192        /**
193         * \brief This function returns the global XYZ coordinates of the specified camera.
194         *
195         * @param camera_ : Camera to return the position.
196         * @param x_ : X coordinate.
197         * @param y_ : Y coordinate.
198         * @param z_ : Z coordinate.
199         */ 
200        static void getXYZofCamera( osg::Camera* camera_, double& x_, double& y_, double& z_ ); 
201
202        /**
203         * \brief This function disables the close button on the openGL's windowdecoration
204         *
205         * 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 ;)
206         *
207         * @param viewer_ : Pointer to the applications viewer
208         * @return : true if successful
209         */ 
210        static bool removeClosebuttonOnGLWindow(osgViewer::Viewer* viewer_);
211
212        /**
213         * \brief This function make the backgroud of the GL window blurred transparent, if an backgroud with alpha is set.
214         *
215         * 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 ;)
216         *
217         * @param viewer_ : Pointer to the applications viewer
218         * @return : true if successful
219         */ 
220        static bool setTransparentWindowBackground(osgViewer::Viewer* viewer_);
221
222        /**
223         * \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.
224         *
225         * To clean up, call this two functions:
226         *  xmlFreeDoc(doc);
227         *  xmlCleanupParser();
228         *
229         * Example to use this function:
230         *      xmlDoc* tmpDoc;
231         *  xmlNode* yourNode = util::getModuleXMLConfig( configFilename, "core", tmpDoc );
232         *  // use yourNode
233         *  if(tmpDoc)
234         *  {
235         *     xmlFreeDoc(tmpDoc); xmlCleanupParser();
236         *  }
237         *
238         * @param configFilename : Config Filename to parse.
239         * @param moduleName : Module name to search for.
240         * @param doc : xmlDoc to use. Must be created outside that the caller can clean it up.
241         * @return : NULL on error, otherwise pointer to the xmlNode of the queried module.
242         */ 
243        static xmlNode* getModuleXMLConfig(std::string configFilename, std::string moduleName, xmlDoc*& doc);
244
245        /**
246         * \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.
247         *
248         * @param configFilename : Config Filename to parse.
249         * @param doc : xmlDoc to use. Must be created outside that the caller can clean it up.
250         * @return : NULL on error, otherwise pointer to the xmlNode of the scenery configuration.
251         */ 
252        static xmlNode* getSceneryXMLConfig(std::string configFilename, xmlDoc*& doc);
253
254private: 
255        /**
256         * \brief This functions checks a list of nodes and all of its children for the specified module configuration.
257         *
258         * This function is used by getModuleXMLConfig() and works recursive
259         *
260         * @param node : Node to search in
261         * @param moduleName :Module name to search for.
262         * @return : NULL if the module configuration was not found, otherwise pointer to the XML Node with the configuration for the specified module.
263         */ 
264        static xmlNode* checkXMLNodeChildrenForModule(xmlNode* node, std::string moduleName);
265
266        /**
267         * \brief This functions checks a list of nodes and all of its children for the scenery configuration.
268         *
269         * @param node : Node to search in
270         * @return : NULL if the module configuration was not found, otherwise pointer to the XML Node with the configuration for the specified module.
271         */ 
272        static xmlNode* checkXMLNodeChildrenForScenery(xmlNode* node);
273};
274
275} //END NAMESPACE
Note: See TracBrowser for help on using the repository browser.