1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
2 | * |
---|
3 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
5 | * (at your option) any later version. The full license is in LICENSE file |
---|
6 | * included with this distribution, and on the openscenegraph.org website. |
---|
7 | * |
---|
8 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
9 | * You have to aquire licenses for all used proprietary modules. |
---|
10 | * |
---|
11 | * This library is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * OpenSceneGraph Public License for more details. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <visual_util.h> |
---|
18 | |
---|
19 | using namespace osgVisual; |
---|
20 | |
---|
21 | util::util(void) |
---|
22 | { |
---|
23 | } |
---|
24 | |
---|
25 | util::~util(void) |
---|
26 | { |
---|
27 | } |
---|
28 | |
---|
29 | osg::Node* util::findNamedNode(const std::string& searchName_, osg::Node* currNode_) |
---|
30 | { |
---|
31 | osg::Group* currGroup; |
---|
32 | osg::Node* foundNode; |
---|
33 | |
---|
34 | // check to see if we have a valid (non-NULL) node. |
---|
35 | // if we do have a null node, return NULL. |
---|
36 | if ( !currNode_) |
---|
37 | { |
---|
38 | return NULL; |
---|
39 | } |
---|
40 | |
---|
41 | // We have a valid node, check to see if this is the node we |
---|
42 | // are looking for. If so, return the current node. |
---|
43 | if (currNode_->getName() == searchName_) |
---|
44 | { |
---|
45 | return currNode_; |
---|
46 | } |
---|
47 | |
---|
48 | // We have a valid node, but not the one we are looking for. |
---|
49 | // Check to see if it has children (non-leaf node). If the node |
---|
50 | // has children, check each of the child nodes by recursive call. |
---|
51 | // If one of the recursive calls returns a non-null value we have |
---|
52 | // found the correct node, so return this node. |
---|
53 | // If we check all of the children and have not found the node, |
---|
54 | // return NULL |
---|
55 | currGroup = currNode_->asGroup(); // returns NULL if not a group. |
---|
56 | if ( currGroup ) |
---|
57 | { |
---|
58 | for (unsigned int i = 0 ; i < currGroup->getNumChildren(); i ++) |
---|
59 | { |
---|
60 | foundNode = findNamedNode(searchName_, currGroup->getChild(i)); |
---|
61 | if (foundNode) |
---|
62 | { |
---|
63 | std::cout << "Node gefunden in Ebene: " << i << std::endl; |
---|
64 | return foundNode; // found a match! |
---|
65 | } |
---|
66 | } |
---|
67 | return NULL; // We have checked each child node - no match found. |
---|
68 | } |
---|
69 | else |
---|
70 | { |
---|
71 | return NULL; // leaf node, no match |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | osg::ref_ptr<osg::Geode> util::getDemoCylinder(double length_, double width_, osg::Vec3 offset_ ) |
---|
76 | { |
---|
77 | osg::ref_ptr<osg::Geode> cyl = new osg::Geode(); |
---|
78 | osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable(new osg::Cylinder( offset_, width_, length_ )); |
---|
79 | osg::Vec4 color = osg::Vec4(255.0, 0.0, 0.0, 1.0); |
---|
80 | shape->setColor( color ); |
---|
81 | cyl->addDrawable( shape ); |
---|
82 | |
---|
83 | return cyl; |
---|
84 | } |
---|
85 | |
---|
86 | osg::ref_ptr<osg::Geode> util::getDemoSphere(double radius_, osg::Vec3 offset_ ) |
---|
87 | { |
---|
88 | osg::ref_ptr<osg::Geode> sphere = new osg::Geode(); |
---|
89 | osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable(new osg::Sphere( offset_, radius_ ) ); |
---|
90 | osg::Vec4 color = osg::Vec4(255.0, 0.0, 0.0, 1.0); |
---|
91 | shape->setColor( color ); |
---|
92 | sphere->addDrawable( shape ); |
---|
93 | |
---|
94 | return sphere; |
---|
95 | } |
---|
96 | |
---|
97 | bool util::intersect(const osg::Vec3d& start_, const osg::Vec3d& end_, osg::Vec3d& intersection_, osg::Node* node_, osg::Node::NodeMask intersectTraversalMask_ ) |
---|
98 | { |
---|
99 | osg::ref_ptr<osgUtil::LineSegmentIntersector> lsi = new osgUtil::LineSegmentIntersector(start_,end_); |
---|
100 | |
---|
101 | osgUtil::IntersectionVisitor iv(lsi.get()); |
---|
102 | iv.setTraversalMask(intersectTraversalMask_); |
---|
103 | |
---|
104 | node_->accept(iv); |
---|
105 | |
---|
106 | if (lsi->containsIntersections()) |
---|
107 | { |
---|
108 | intersection_ = lsi->getIntersections().begin()->getWorldIntersectPoint(); |
---|
109 | return true; // Intersect found |
---|
110 | } |
---|
111 | return false; // No intersect found |
---|
112 | } |
---|
113 | |
---|
114 | bool util::queryHeightOfTerrain(double& hot_, osg::Node* rootNode_, double lat_, double lon_, osg::Node::NodeMask traversalMask_) |
---|
115 | { |
---|
116 | // Get ellipsoid model |
---|
117 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(rootNode_); |
---|
118 | if ( !csn ) |
---|
119 | { |
---|
120 | OSG_NOTIFY( osg::FATAL ) << "util::queryHeightOfTerrain() :: Invalid CSN!" << std::endl; |
---|
121 | return false; |
---|
122 | } |
---|
123 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
---|
124 | if ( !ellipsoid ) |
---|
125 | { |
---|
126 | OSG_NOTIFY( osg::FATAL ) << "util::queryHeightOfTerrain() :: Invalid ellipsoid!" << std::endl; |
---|
127 | return false; |
---|
128 | } |
---|
129 | |
---|
130 | // Setup both endpoints of intersect line |
---|
131 | double X,Y,Z; |
---|
132 | ellipsoid->convertLatLongHeightToXYZ(lat_, lon_, 30000, X, Y, Z); |
---|
133 | osg::Vec3d s = osg::Vec3d(X, Y, Z); |
---|
134 | ellipsoid->convertLatLongHeightToXYZ(lat_, lon_, -30000, X, Y, Z); |
---|
135 | osg::Vec3d e = osg::Vec3d(X, Y, Z); |
---|
136 | |
---|
137 | // Query intersection point |
---|
138 | osg::Vec3d ip; |
---|
139 | if ( util::intersect(s, e, ip, rootNode_, traversalMask_) ) |
---|
140 | { |
---|
141 | double lat2_, lon2_; |
---|
142 | ellipsoid->convertXYZToLatLongHeight( ip.x(), ip.y(), ip.z(), lat2_, lon2_, hot_ ); // Convert Intersection Point back to Lat Lon, HOT. |
---|
143 | //OSG_NOTIFY(osg::ALWAYS) << "lat: "<< osg::RadiansToDegrees(lat2_) <<", Lon: " << osg::RadiansToDegrees(lon2_) << ", Hot: " << hot_ << std::endl; |
---|
144 | return true; |
---|
145 | } |
---|
146 | |
---|
147 | // If no intersection point found: set HOT to zero and return false. |
---|
148 | hot_ = 0; |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | bool util::queryHeightAboveTerrainInWGS84(double& hat_, osg::Node* rootNode_, double lat_, double lon_, double height_, osg::Node::NodeMask traversalMask_) |
---|
153 | { |
---|
154 | // Get HOT by asking util::queryHeightOfTerrain() :) |
---|
155 | double HOT; |
---|
156 | if ( !util::queryHeightOfTerrain(HOT, rootNode_, lat_, lon_, traversalMask_) ) |
---|
157 | { |
---|
158 | OSG_NOTIFY( osg::INFO ) << "util::queryHeightAboveTerrainInWGS84() :: Unable to get HOT, will use 0 for HOT!" << std::endl; |
---|
159 | } |
---|
160 | |
---|
161 | // Calculate HAT |
---|
162 | hat_ = height_ - HOT; |
---|
163 | return true; |
---|
164 | } |
---|
165 | |
---|
166 | bool util::queryHeightAboveTerrainInWorld(double& hat_, osg::Node* rootNode_, double x_, double y_, double z_, osg::Node::NodeMask traversalMask_) |
---|
167 | { |
---|
168 | // Get ellipsoid model |
---|
169 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(rootNode_); |
---|
170 | if ( !csn ) |
---|
171 | { |
---|
172 | OSG_NOTIFY( osg::FATAL ) << "util::queryHeightAboveTerrainInWorld() :: Invalid CSN!" << std::endl; |
---|
173 | return false; |
---|
174 | } |
---|
175 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
---|
176 | if ( !ellipsoid ) |
---|
177 | { |
---|
178 | OSG_NOTIFY( osg::FATAL ) << "util::queryHeightAboveTerrainInWorld() :: Invalid ellipsoid!" << std::endl; |
---|
179 | return false; |
---|
180 | } |
---|
181 | |
---|
182 | // Transform XYZ into LatLonHeight |
---|
183 | double lat_, lon_, height_; |
---|
184 | ellipsoid->convertXYZToLatLongHeight(x_, y_, z_, lat_, lon_, height_); |
---|
185 | |
---|
186 | // ask util::queryHeightAboveTerrainInWGS84() to calc HAT :) |
---|
187 | if( !util::queryHeightAboveTerrainInWGS84(hat_, rootNode_, lat_, lon_, height_, traversalMask_ ) ) |
---|
188 | { |
---|
189 | OSG_NOTIFY( osg::FATAL ) << "util::queryHeightAboveTerrainInWorld() :: Unable to get HAT!" << std::endl; |
---|
190 | return false; |
---|
191 | } |
---|
192 | |
---|
193 | return true; |
---|
194 | } |
---|
195 | |
---|
196 | bool util::calculateEarthRadiusAtWGS84Coordinate(double lat_, double lon_, osg::Node* rootNode_, double& radius_) |
---|
197 | { |
---|
198 | // Calculate radius: |
---|
199 | double x, y, z; |
---|
200 | |
---|
201 | if ( util::calculateXYZAtWGS84Coordinate(lat_, lon_, 0.0, rootNode_, x, y, z) ) |
---|
202 | { |
---|
203 | radius_ = sqrt( pow(x, 2) + pow(y, 2) + pow(z, 2) ); |
---|
204 | return true; |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | OSG_NOTIFY( osg::FATAL ) << "util::calculateEarthRadiusAtWGS84Coordinate() :: Unablöe to calculate Earth Radius!" << std::endl; |
---|
209 | return false; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | bool util::calculateXYZAtWGS84Coordinate(double lat_, double lon_, double height_, osg::Node* rootNode_, double& x_, double& y_, double& z_) |
---|
214 | { |
---|
215 | // Get ellipsoid model |
---|
216 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(rootNode_); |
---|
217 | if ( !csn ) |
---|
218 | return false; |
---|
219 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
---|
220 | if ( !ellipsoid ) |
---|
221 | return false; |
---|
222 | |
---|
223 | // Calculate xyz: |
---|
224 | ellipsoid->convertLatLongHeightToXYZ( lat_, lon_, height_, x_, y_, z_); |
---|
225 | return true; |
---|
226 | } |
---|
227 | |
---|
228 | bool util::getWGS84ofCamera( osg::Camera* camera_, osg::Node* rootNode_, double& lat_, double& lon_, double& height_ ) |
---|
229 | { |
---|
230 | // Get ellipsoid model |
---|
231 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(rootNode_); |
---|
232 | if ( !csn ) |
---|
233 | return false; |
---|
234 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
---|
235 | if ( !ellipsoid ) |
---|
236 | return false; |
---|
237 | |
---|
238 | osg::Vec3d eye, dir, up; |
---|
239 | camera_->getViewMatrixAsLookAt(eye,dir,up); // Get XYZ from camera |
---|
240 | ellipsoid->convertXYZToLatLongHeight(eye.x(), eye.y(), eye.z(), lat_, lon_, height_); |
---|
241 | return true; |
---|
242 | } |
---|
243 | |
---|
244 | void util::getXYZofCamera( osg::Camera* camera_, double& x_, double& y_, double& z_ ) |
---|
245 | { |
---|
246 | osg::Vec3d eye, dir, up; |
---|
247 | camera_->getViewMatrixAsLookAt(eye,dir,up); // Get XYZ from camera |
---|
248 | x_ = eye.x(); |
---|
249 | y_ = eye.y(); |
---|
250 | z_ = eye.z(); |
---|
251 | } |
---|
252 | |
---|
253 | bool util::removeClosebuttonOnGLWindow(osgViewer::Viewer* viewer_) |
---|
254 | { |
---|
255 | #ifdef FUNFUNCTIONS_ENABLED |
---|
256 | #ifdef WIN32 |
---|
257 | osgViewer::ViewerBase::Windows wins; |
---|
258 | viewer_->getWindows(wins); |
---|
259 | osgViewer::GraphicsHandleWin32* hwnd = dynamic_cast<osgViewer::GraphicsHandleWin32*>(wins[0]); |
---|
260 | |
---|
261 | HMENU hMenu = GetSystemMenu(hwnd->getHWND(), FALSE); |
---|
262 | ::EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | (MF_DISABLED | MF_GRAYED)); |
---|
263 | #endif |
---|
264 | #endif |
---|
265 | return true; |
---|
266 | } |
---|
267 | |
---|
268 | bool util::setTransparentWindowBackground(osgViewer::Viewer* viewer_) |
---|
269 | { |
---|
270 | #ifdef FUNFUNCTIONS_ENABLED |
---|
271 | #ifdef WIN32 |
---|
272 | osgViewer::ViewerBase::Windows wins; |
---|
273 | viewer_->getWindows(wins); |
---|
274 | osgViewer::GraphicsHandleWin32* hwnd = dynamic_cast<osgViewer::GraphicsHandleWin32*>(wins[0]); |
---|
275 | HWND _hwnd = hwnd->getHWND(); |
---|
276 | viewer_->getDisplaySettings()->setMinimumNumAlphaBits(8); |
---|
277 | |
---|
278 | // Create and populate the Blur Behind structure |
---|
279 | DWM_BLURBEHIND bb = {0}; |
---|
280 | // Disable Blur Behind and Blur Region; |
---|
281 | bb.dwFlags = DWM_BB_ENABLE; |
---|
282 | bb.fEnable = true; |
---|
283 | bb.hRgnBlur = NULL; |
---|
284 | |
---|
285 | // Ensable Blur Behind |
---|
286 | HRESULT hr = DwmEnableBlurBehindWindow(_hwnd, &bb); |
---|
287 | if (SUCCEEDED(hr)) |
---|
288 | return true; |
---|
289 | else |
---|
290 | return false; |
---|
291 | |
---|
292 | #endif |
---|
293 | #endif |
---|
294 | return true; |
---|
295 | } |
---|