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