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 | |
---|
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 | |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | namespace osgVisual |
---|
40 | { |
---|
41 | |
---|
42 | /** |
---|
43 | * \brief This class provides utility functions. |
---|
44 | * |
---|
45 | * The utility functions are implemented as static functions if possible for easy use in other classes. |
---|
46 | * |
---|
47 | * \todo check why lineIntersector is unstable, check if newer and better system is available. |
---|
48 | * |
---|
49 | * @author Torben Dannhauer |
---|
50 | * @date Jul 2009 |
---|
51 | */ |
---|
52 | class util |
---|
53 | { |
---|
54 | |
---|
55 | public: |
---|
56 | /** |
---|
57 | * \brief Construtor |
---|
58 | * |
---|
59 | */ |
---|
60 | util(); |
---|
61 | |
---|
62 | /** |
---|
63 | * \brief Destructor |
---|
64 | * |
---|
65 | */ |
---|
66 | ~util(); |
---|
67 | |
---|
68 | /** |
---|
69 | * \brief This functions searches in the scene graph for a node with a name |
---|
70 | * |
---|
71 | * This function searches in the scene graph unter the node "currNode" for a node named like the given search name. |
---|
72 | * The functions returns the first node with that name. |
---|
73 | * |
---|
74 | * @param searchName_ : Nodename to search for |
---|
75 | * @param currNode_ : Scene graph to search in. |
---|
76 | * @return : if Node found: Pointer to the node. If not found: NULL |
---|
77 | */ |
---|
78 | static osg::Node* findNamedNode(const std::string& searchName_, osg::Node* currNode_); |
---|
79 | |
---|
80 | /** |
---|
81 | * \brief This function creates a red cylinder of the specified size tor testing purposes. |
---|
82 | * |
---|
83 | * @param length_ : Length of the cylinder in meter. |
---|
84 | * @param width_ : Diameter of the cylinder in meter. |
---|
85 | * @param offset_ : Offset of the cylinder's origin in meter. |
---|
86 | * @return : Referenced Pointer off the Geode with the created cylinder. |
---|
87 | */ |
---|
88 | static osg::ref_ptr<osg::Geode> getDemoCylinder(double length_, double width_, osg::Vec3 offset_ = osg::Vec3(0.0, 0.0, 0.0) ); |
---|
89 | |
---|
90 | /** |
---|
91 | * \brief This function creates a red sphere of the specified size tor testing purposes. |
---|
92 | * |
---|
93 | * @param radius_ : Radius of the sphere in meter. |
---|
94 | * @param offset_ : Offset of the sphere's origin in meter. |
---|
95 | * @return : Referenced Pointer off the Geode with the created cylinder. |
---|
96 | */ |
---|
97 | static osg::ref_ptr<osg::Geode> getDemoSphere(double radius_, osg::Vec3 offset_ = osg::Vec3(0.0, 0.0, 0.0) ); |
---|
98 | |
---|
99 | /** |
---|
100 | * \brief This functions searches the scene graph for intersections of a line between two points and the model. |
---|
101 | * |
---|
102 | * @param start_ : Point 1 for the intersection line |
---|
103 | * @param end_ : Point 2 for the intersection line |
---|
104 | * @param intersection_ : vektor to the intersection point if any is found. |
---|
105 | * @param node_ : Node which is the root of the scene graph to check for intersections |
---|
106 | * @param intersectTraversalMask_ : Bitwise mask wich controls which nodes should be checked for intersections (e.g. to check only terrain but not clouds or sky) |
---|
107 | * @return returns : True if an intersection is found. |
---|
108 | */ |
---|
109 | static bool intersect(const osg::Vec3d& start_, const osg::Vec3d& end_, osg::Vec3d& intersection_, osg::Node* node_, osg::Node::NodeMask intersectTraversalMask_=0xffffffff ); |
---|
110 | |
---|
111 | /** |
---|
112 | * \brief This function queries the height of terrain (hot) at a specified location. |
---|
113 | * |
---|
114 | * @param hot_ : Reference to write the calculated hot into. |
---|
115 | * @param rootNode_ : Node which is the root of the scene graph to calculate the hot. |
---|
116 | * @param lat_ : Latitude of the position to calculate the hot. |
---|
117 | * @param lon_ : Longitude of the position to calculate the hot. |
---|
118 | * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hot (e.g. to check only terrain but not clouds or sky) |
---|
119 | * @return : True if hot calculation successful. |
---|
120 | */ |
---|
121 | static bool queryHeightOfTerrain(double& hot_, osg::Node* rootNode_, double lat_, double lon_, osg::Node::NodeMask traversalMask_=0xffffffff); |
---|
122 | |
---|
123 | /** |
---|
124 | * \brief This function queries the height above terrain (hat) at a specified location in WGS84 coordinates. |
---|
125 | * |
---|
126 | * @param hat_ : Reference to write the calculated hat into. |
---|
127 | * @param rootNode_ : Node which is the root of the scene graph to calculate the hat. |
---|
128 | * @param lat_ : Latitude of the position to calculate the hat. (e.g. lat of camera) |
---|
129 | * @param lon_ : Longitude of the position to calculate the hat.(e.g. lon of camera) |
---|
130 | * @param height_ : Height of the position to calculate the hat.(e.g. height of camera). |
---|
131 | * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hat (e.g. to check only terrain but not clouds or sky) |
---|
132 | * @return : True if hat calculation successful. |
---|
133 | */ |
---|
134 | static bool queryHeightAboveTerrainInWGS84(double& hat_, osg::Node* rootNode_, double lat_, double lon_, double height_, osg::Node::NodeMask traversalMask_=0xffffffff); |
---|
135 | |
---|
136 | /** |
---|
137 | * \brief This function queries the height above terrain (hat) at a specified location in world coordinates. |
---|
138 | * |
---|
139 | * @param hat_ : Reference to write the calculated hat into. |
---|
140 | * @param rootNode_ : Node which is the root of the scene graph to calculate the hat. |
---|
141 | * @param x_ : First value of the pointer which points to the position to calculate hat for. |
---|
142 | * @param y_ : Second value of the pointer which points to the position to calculate hat for. |
---|
143 | * @param z_ : Third value of the pointer which points to the position to calculate hat for. |
---|
144 | * @param traversalMask_ : Bitwise mask wich controls which nodes should be checked for hat (e.g. to check only terrain but not clouds or sky) |
---|
145 | * @return : True if hat calculation successful. |
---|
146 | */ |
---|
147 | static bool queryHeightAboveTerrainInWorld(double& hat_, osg::Node* rootNode_, double x_, double y_, double z_, osg::Node::NodeMask traversalMask_=0xffffffff); |
---|
148 | |
---|
149 | /** |
---|
150 | * \brief This function calculates the earth readius (vector length in world coordiantes from origin to location) at the specified location. |
---|
151 | * |
---|
152 | * @param lat_ : Latitude in radians. |
---|
153 | * @param lon_ : Longitude in radians. |
---|
154 | * @param rootNode_ : Root node of the scene which coordinate system node should be used. |
---|
155 | * @param radius_ : Calculated earth readius in meter. |
---|
156 | * @return : True if calculation successful. |
---|
157 | */ |
---|
158 | static bool calculateEarthRadiusAtWGS84Coordinate(double lat_, double lon_, osg::Node* rootNode_, double& radius_); |
---|
159 | |
---|
160 | /** |
---|
161 | * \brief This function converts a WGS84 position into global OpenGL XYZ coordinates. |
---|
162 | * |
---|
163 | * @param lat_ : Latitude of the position to calculate XYZ. |
---|
164 | * @param lon_ : Longitude of the position to calculate XYZ. |
---|
165 | * @param height_ : Height of the position to calculate XYZ. |
---|
166 | * @param rootNode_ : Node which is the root of the scene graph to calculate XYZ. |
---|
167 | * @param x_ : X Coordinate in global OpenGL coordinates. |
---|
168 | * @param y_ : Y Coordinate in global OpenGL coordinates. |
---|
169 | * @param z_ : Z Coordinate in global OpenGL coordinates. |
---|
170 | * @return : True if calculation successful. |
---|
171 | */ |
---|
172 | static bool calculateXYZAtWGS84Coordinate(double lat_, double lon_, double height_, osg::Node* rootNode_, double& x_, double& y_, double& z_); |
---|
173 | |
---|
174 | /** |
---|
175 | * \brief This function calculates lat, lon and height of a specified camera by a specified coordinate system node. |
---|
176 | * |
---|
177 | * @param camera_ : Camera node which position should be calculated. |
---|
178 | * @param rootNode_ : rootNode of the scene. |
---|
179 | * @param lat_ : Latitude variable to save value in. |
---|
180 | * @param lon_ : Longitude variable to save value in. |
---|
181 | * @param height_ : Height: variable to save value in. |
---|
182 | * @return : True if calculation was successful. |
---|
183 | */ |
---|
184 | static bool getWGS84ofCamera( osg::Camera* camera_, osg::Node* rootNode_, double& lat_, double& lon_, double& height_ ); |
---|
185 | |
---|
186 | /** |
---|
187 | * \brief This function returns the global XYZ coordinates of the specified camera. |
---|
188 | * |
---|
189 | * @param camera_ : Camera to return the position. |
---|
190 | * @param x_ : X coordinate. |
---|
191 | * @param y_ : Y coordinate. |
---|
192 | * @param z_ : Z coordinate. |
---|
193 | */ |
---|
194 | static void getXYZofCamera( osg::Camera* camera_, double& x_, double& y_, double& z_ ); |
---|
195 | |
---|
196 | |
---|
197 | /** |
---|
198 | * \brief This function converts a string into a double. |
---|
199 | * |
---|
200 | * @param s : String to convert. |
---|
201 | * @return : double representation of the string. |
---|
202 | */ |
---|
203 | static double strToDouble(std::string s); |
---|
204 | |
---|
205 | /** |
---|
206 | * \brief This function converts a string into an Integer. |
---|
207 | * |
---|
208 | * @param s : String to convert. |
---|
209 | * @return : Integer representation of the string. |
---|
210 | */ |
---|
211 | static int strToInt(std::string s); |
---|
212 | |
---|
213 | /** |
---|
214 | * \brief This function converts a string into a bool. |
---|
215 | * |
---|
216 | * @param s : String to convert. |
---|
217 | * @return : Bool representation of the string. Returns 'false' on error. |
---|
218 | */ |
---|
219 | static bool strToBool(std::string s); |
---|
220 | |
---|
221 | /** |
---|
222 | * \brief \todo : Kommentieren |
---|
223 | * |
---|
224 | * @param StartPoint |
---|
225 | * @param EndPoint |
---|
226 | * @param radius |
---|
227 | * @param CylinderColor |
---|
228 | * @param pAddToThisGroup |
---|
229 | */ |
---|
230 | static void AddCylinderBetweenPoints(osg::Vec3d StartPoint, osg::Vec3d EndPoint, float radius, osg::Vec4d CylinderColor, osg::Group *pAddToThisGroup); |
---|
231 | |
---|
232 | template<class T> |
---|
233 | class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor |
---|
234 | { |
---|
235 | public: |
---|
236 | FindTopMostNodeOfTypeVisitor(): |
---|
237 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
---|
238 | _foundNode(0) |
---|
239 | {} |
---|
240 | |
---|
241 | void apply(osg::Node& node) |
---|
242 | { |
---|
243 | T* result = dynamic_cast<T*>(&node); |
---|
244 | if (result) |
---|
245 | { |
---|
246 | _foundNode = result; |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | traverse(node); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | T* _foundNode; |
---|
255 | }; |
---|
256 | |
---|
257 | template<class T> |
---|
258 | static T* findTopMostNodeOfType(osg::Node* node) |
---|
259 | { |
---|
260 | if (!node) return 0; |
---|
261 | |
---|
262 | FindTopMostNodeOfTypeVisitor<T> fnotv; |
---|
263 | node->accept(fnotv); |
---|
264 | |
---|
265 | return fnotv._foundNode; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | }; |
---|
270 | |
---|
271 | } //END NAMESPACE |
---|