1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2011 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_object.h> |
---|
18 | |
---|
19 | using namespace osgVisual; |
---|
20 | |
---|
21 | visual_object::visual_object( osg::CoordinateSystemNode* sceneRoot_, std::string nodeName_) |
---|
22 | { |
---|
23 | // Add this node to Scenegraph |
---|
24 | sceneRoot_->addChild( this ); |
---|
25 | |
---|
26 | // Set Nodename for further identification |
---|
27 | this->setName( nodeName_ ); |
---|
28 | |
---|
29 | // Set callback. |
---|
30 | /** \todo: welcher update ist der richtige? voraussichtlich event.) */ |
---|
31 | //this->setUpdateCallback( new visual_objectPositionCallback() ); |
---|
32 | this->setEventCallback( new visual_objectPositionCallback() ); |
---|
33 | |
---|
34 | // Init Position and Attitude |
---|
35 | lat = 0; |
---|
36 | lon = 0; |
---|
37 | alt = 0; |
---|
38 | |
---|
39 | azimuthAngle_psi = 0; |
---|
40 | pitchAngle_theta = 0; |
---|
41 | bankAngle_phi = 0; |
---|
42 | |
---|
43 | geometry_offset_rotation.makeRotate( 0.0, 1.0, 1.0, 1.0 ); |
---|
44 | |
---|
45 | // Init Scale factor |
---|
46 | scaleX = 1.0; |
---|
47 | scaleY = 1.0; |
---|
48 | scaleZ = 1.0; |
---|
49 | |
---|
50 | // Init cameraOffset |
---|
51 | cameraTranslationOffset.makeTranslate( osg::Vec3d(0.0, 0.0, 0.0) ); // Trans: (y, x, -z_down) |
---|
52 | cameraRotationOffset.makeRotate( osg::DegreesToRadians( 90.0 ), osg::Vec3(1, 0, 0) ); // Rot: (-y, +x , -z) |
---|
53 | |
---|
54 | //setCameraOffsetTranslation(0.0, -150.0, 50.0); // Trans: (rechts davon, longitudinal, vertikal) |
---|
55 | setCameraOffsetTranslation( 150.0, 0.0, 30.0); |
---|
56 | setCameraOffsetRotation( osg::DegreesToRadians(0.0), osg::DegreesToRadians(-15.0), osg::DegreesToRadians(-90.0) ); |
---|
57 | |
---|
58 | // Geometrynode hinzufügen |
---|
59 | geometry = new osg::Group(); |
---|
60 | this->addChild( geometry ); |
---|
61 | unsetGeometry(); // adds an osg::Node as geometry to make the visual_object trackable for node trackers. |
---|
62 | |
---|
63 | // Tracking ID |
---|
64 | trackingId = -1; |
---|
65 | |
---|
66 | // Labelnode hinzufügen |
---|
67 | labels = new osg::Geode(); |
---|
68 | this->addChild( labels ); |
---|
69 | } |
---|
70 | |
---|
71 | visual_object::~visual_object() |
---|
72 | { |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | visual_object* visual_object::createNodeFromXMLConfig(osg::CoordinateSystemNode* sceneRoot_, xmlNode* a_node) |
---|
77 | { |
---|
78 | if(a_node == NULL) |
---|
79 | return NULL; |
---|
80 | |
---|
81 | OSG_NOTIFY( osg::ALWAYS ) << __FUNCTION__ << " - Try to creating a new Model.." << std::endl; |
---|
82 | |
---|
83 | // Prepare Variables |
---|
84 | std::string objectname="", filename="", label=""; |
---|
85 | bool dynamic = false; |
---|
86 | int trackingID=-1; |
---|
87 | double lat=0.0, lon=0.0, alt=0.0, rot_x=0.0, rot_y=0.0, rot_z=0.0; |
---|
88 | double cam_trans_x=0.0, cam_trans_y=0.0, cam_trans_z=0.0, cam_rot_x=0.0, cam_rot_y=0.0, cam_rot_z=0.0; |
---|
89 | double geometry_rot_x=0.0, geometry_rot_y=0.0, geometry_rot_z=0.0; |
---|
90 | double geometry_scale_x=1.0, geometry_scale_y=1.0, geometry_scale_z=1.0; |
---|
91 | osg::ref_ptr<osgVisual::object_updater> updater = NULL; |
---|
92 | std::string updater_lat="", updater_lon="", updater_alt="", updater_rot_x="", updater_rot_y="", updater_rot_z="", updater_label=""; |
---|
93 | |
---|
94 | // extract model properties |
---|
95 | xmlAttr *attr = a_node->properties; |
---|
96 | while ( attr ) |
---|
97 | { |
---|
98 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
99 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
100 | if( attr_name == "objectname" ) objectname = attr_value; |
---|
101 | if( attr_name == "trackingid" ) trackingID = util::strToInt(attr_value); |
---|
102 | if( attr_name == "label" ) label = attr_value; |
---|
103 | if( attr_name == "dynamic" ) dynamic = util::strToBool(attr_value); |
---|
104 | |
---|
105 | attr = attr->next; |
---|
106 | } |
---|
107 | for (xmlNode *cur_node = a_node->children; cur_node; cur_node = cur_node->next) |
---|
108 | { |
---|
109 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
110 | |
---|
111 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "position") |
---|
112 | { |
---|
113 | xmlAttr *attr = cur_node->properties; |
---|
114 | while ( attr ) |
---|
115 | { |
---|
116 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
117 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
118 | if( attr_name == "lat" ) lat = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
119 | if( attr_name == "lon" ) lon = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
120 | if( attr_name == "alt" ) alt = util::strToDouble(attr_value); |
---|
121 | |
---|
122 | attr = attr->next; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "attitude") |
---|
127 | { |
---|
128 | xmlAttr *attr = cur_node->properties; |
---|
129 | while ( attr ) |
---|
130 | { |
---|
131 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
132 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
133 | if( attr_name == "rot_x" ) rot_x = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
134 | if( attr_name == "rot_y" ) rot_y = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
135 | if( attr_name == "rot_z" ) rot_z = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
136 | |
---|
137 | attr = attr->next; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "updater") |
---|
142 | { |
---|
143 | for (xmlNode *sub_cur_node = cur_node->children; sub_cur_node; sub_cur_node = sub_cur_node->next) |
---|
144 | { |
---|
145 | std::string sub_node_name=reinterpret_cast<const char*>(sub_cur_node->name); |
---|
146 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "position") |
---|
147 | { |
---|
148 | xmlAttr *attr = sub_cur_node->properties; |
---|
149 | while ( attr ) |
---|
150 | { |
---|
151 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
152 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
153 | if( attr_name == "lat" ) |
---|
154 | updater_lat = attr_value; |
---|
155 | if( attr_name == "lon" ) |
---|
156 | updater_lon = attr_value; |
---|
157 | if( attr_name == "alt" ) |
---|
158 | updater_alt = attr_value; |
---|
159 | attr = attr->next; |
---|
160 | } |
---|
161 | } |
---|
162 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "attitude") |
---|
163 | { |
---|
164 | xmlAttr *attr = sub_cur_node->properties; |
---|
165 | while ( attr ) |
---|
166 | { |
---|
167 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
168 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
169 | if( attr_name == "rot_x" ) |
---|
170 | updater_rot_x = attr_value; |
---|
171 | if( attr_name == "rot_y" ) |
---|
172 | updater_rot_y = attr_value; |
---|
173 | if( attr_name == "rot_z" ) |
---|
174 | updater_rot_z = attr_value; |
---|
175 | attr = attr->next; |
---|
176 | } |
---|
177 | } |
---|
178 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "label") |
---|
179 | { |
---|
180 | xmlAttr *attr = sub_cur_node->properties; |
---|
181 | while ( attr ) |
---|
182 | { |
---|
183 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
184 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
185 | if( attr_name == "text" ) |
---|
186 | updater_label = attr_value; |
---|
187 | attr = attr->next; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "cameraoffset") |
---|
194 | { |
---|
195 | for (xmlNode *sub_cur_node = cur_node->children; sub_cur_node; sub_cur_node = sub_cur_node->next) |
---|
196 | { |
---|
197 | std::string sub_node_name=reinterpret_cast<const char*>(sub_cur_node->children->name); |
---|
198 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "translation") |
---|
199 | { |
---|
200 | xmlAttr *attr = sub_cur_node->properties; |
---|
201 | while ( attr ) |
---|
202 | { |
---|
203 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
204 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
205 | if( attr_name == "trans_x" ) cam_trans_x = util::strToDouble(attr_value); |
---|
206 | if( attr_name == "trans_y" ) cam_trans_y = util::strToDouble(attr_value); |
---|
207 | if( attr_name == "trans_z" ) cam_trans_z = util::strToDouble(attr_value); |
---|
208 | |
---|
209 | attr = attr->next; |
---|
210 | } |
---|
211 | } |
---|
212 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "rotation") |
---|
213 | { |
---|
214 | xmlAttr *attr = sub_cur_node->properties; |
---|
215 | while ( attr ) |
---|
216 | { |
---|
217 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
218 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
219 | if( attr_name == "rot_x" ) cam_rot_x = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
220 | if( attr_name == "rot_y" ) cam_rot_y = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
221 | if( attr_name == "rot_z" ) cam_rot_z = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
222 | |
---|
223 | attr = attr->next; |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "geometry") |
---|
230 | { |
---|
231 | // extract filename |
---|
232 | xmlAttr *attr = cur_node->properties; |
---|
233 | while ( attr ) |
---|
234 | { |
---|
235 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
236 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
237 | if( attr_name == "filename" ) |
---|
238 | filename = attr_value; |
---|
239 | attr = attr->next; |
---|
240 | } |
---|
241 | |
---|
242 | // Extract optional settings |
---|
243 | for (xmlNode *sub_cur_node = cur_node->children; sub_cur_node; sub_cur_node = sub_cur_node->next) |
---|
244 | { |
---|
245 | std::string sub_node_name=reinterpret_cast<const char*>(sub_cur_node->name); |
---|
246 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "offset") |
---|
247 | { |
---|
248 | xmlAttr *attr = sub_cur_node->properties; |
---|
249 | while ( attr ) |
---|
250 | { |
---|
251 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
252 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
253 | if( attr_name == "rot_x" ) geometry_rot_x = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
254 | if( attr_name == "rot_y" ) geometry_rot_y = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
255 | if( attr_name == "rot_z" ) geometry_rot_z = osg::DegreesToRadians(util::strToDouble(attr_value)); |
---|
256 | |
---|
257 | attr = attr->next; |
---|
258 | } |
---|
259 | } |
---|
260 | if(sub_cur_node->type == XML_ELEMENT_NODE && sub_node_name == "scalefactor") |
---|
261 | { |
---|
262 | xmlAttr *attr = sub_cur_node->properties; |
---|
263 | while ( attr ) |
---|
264 | { |
---|
265 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
266 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
267 | if( attr_name == "scale_x" ) geometry_scale_x = util::strToDouble(attr_value); |
---|
268 | if( attr_name == "scale_y" ) geometry_scale_y = util::strToDouble(attr_value); |
---|
269 | if( attr_name == "scale_z" ) geometry_scale_z = util::strToDouble(attr_value); |
---|
270 | |
---|
271 | attr = attr->next; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | osgVisual::visual_object* object = new osgVisual::visual_object( sceneRoot_, objectname ); |
---|
280 | object->lat = lat; |
---|
281 | object->lon = lon; |
---|
282 | object->alt = alt; |
---|
283 | object->azimuthAngle_psi = rot_x; |
---|
284 | object->pitchAngle_theta = rot_y; |
---|
285 | object->bankAngle_phi = rot_z; |
---|
286 | object->trackingId = trackingID; |
---|
287 | if(label!="") |
---|
288 | object->addLabel("default", label); |
---|
289 | if(dynamic) |
---|
290 | { |
---|
291 | updater = new osgVisual::object_updater(object); |
---|
292 | object->addUpdater( updater ); |
---|
293 | } |
---|
294 | object->setCameraOffset( cam_trans_x, cam_trans_y, cam_trans_z, cam_rot_x, cam_rot_y, cam_rot_z); |
---|
295 | if(filename!="") |
---|
296 | { |
---|
297 | object->loadGeometry( filename ); |
---|
298 | object->setGeometryOffset( geometry_rot_x, geometry_rot_y, geometry_rot_z ); |
---|
299 | object->setScale( geometry_scale_x, geometry_scale_y, geometry_scale_z ); |
---|
300 | } |
---|
301 | |
---|
302 | if(updater.valid()) |
---|
303 | { |
---|
304 | updater->setUpdaterSlotNames( object, updater_lat, updater_lon, updater_alt, updater_rot_x, updater_rot_y, updater_rot_z, updater_label); |
---|
305 | } |
---|
306 | |
---|
307 | OSG_NOTIFY( osg::ALWAYS ) << "Done." << std::endl; |
---|
308 | return object; |
---|
309 | } |
---|
310 | |
---|
311 | osg::Node* visual_object::findNodeByTrackingID(int trackingID, osg::Node* currNode_) |
---|
312 | { |
---|
313 | osg::Group* currGroup; |
---|
314 | osg::Node* foundNode; |
---|
315 | |
---|
316 | // check to see if we have a valid (non-NULL) node. |
---|
317 | // if we do have a null node, return NULL. |
---|
318 | if ( !currNode_) |
---|
319 | { |
---|
320 | return NULL; |
---|
321 | } |
---|
322 | |
---|
323 | // We have a valid node, check to see if this is the node we |
---|
324 | // are looking for. If so, return the current node. |
---|
325 | osgVisual::visual_object* tmp = dynamic_cast<osgVisual::visual_object*>(currNode_); |
---|
326 | if(tmp && tmp->getTrackingId()==trackingID) |
---|
327 | return currNode_; |
---|
328 | |
---|
329 | |
---|
330 | // We have a valid node, but not the one we are looking for. |
---|
331 | // Check to see if it has children (non-leaf node). If the node |
---|
332 | // has children, check each of the child nodes by recursive call. |
---|
333 | // If one of the recursive calls returns a non-null value we have |
---|
334 | // found the correct node, so return this node. |
---|
335 | // If we check all of the children and have not found the node, |
---|
336 | // return NULL |
---|
337 | currGroup = currNode_->asGroup(); // returns NULL if not a group. |
---|
338 | if ( currGroup ) |
---|
339 | { |
---|
340 | for (unsigned int i = 0 ; i < currGroup->getNumChildren(); i ++) |
---|
341 | { |
---|
342 | foundNode = findNodeByTrackingID( trackingID, currGroup->getChild(i)); |
---|
343 | if (foundNode) |
---|
344 | { |
---|
345 | std::cout << "Node gefunden in Ebene: " << i << std::endl; |
---|
346 | return foundNode; // found a match! |
---|
347 | } |
---|
348 | } |
---|
349 | return NULL; // We have checked each child node - no match found. |
---|
350 | } |
---|
351 | else |
---|
352 | return NULL; // leaf node, no match |
---|
353 | } |
---|
354 | |
---|
355 | void visual_object::setNewPositionAttitude( double lat_, double lon_, double alt_, double azimuthAngle_psi_, double pitchAngle_theta_, double bankAngle_phi_ ) |
---|
356 | { |
---|
357 | lat = lat_; |
---|
358 | lon = lon_; |
---|
359 | alt = alt_; |
---|
360 | |
---|
361 | azimuthAngle_psi = azimuthAngle_psi_; |
---|
362 | pitchAngle_theta = pitchAngle_theta_; |
---|
363 | bankAngle_phi = bankAngle_phi_; |
---|
364 | } |
---|
365 | |
---|
366 | void visual_object::setNewPosition( double lat_, double lon_, double alt_ ) |
---|
367 | { |
---|
368 | lat = lat_; |
---|
369 | lon = lon_; |
---|
370 | alt = alt_; |
---|
371 | } |
---|
372 | |
---|
373 | void visual_object::setNewAttitude( double azimuthAngle_psi_, double pitchAngle_theta_, double bankAngle_phi_ ) |
---|
374 | { |
---|
375 | azimuthAngle_psi = azimuthAngle_psi_; |
---|
376 | pitchAngle_theta = pitchAngle_theta_; |
---|
377 | bankAngle_phi = bankAngle_phi_; |
---|
378 | } |
---|
379 | |
---|
380 | void visual_object::setGeometryOffset( double rotX_, double rotY_, double rotZ_ ) |
---|
381 | { |
---|
382 | geometry_offset_rotation.makeRotate( rotX_, osg::Vec3f(1.0, 0.0, 0.0), |
---|
383 | rotY_, osg::Vec3f(0.0, 1.0, 0.0), |
---|
384 | rotZ_, osg::Vec3f(0.0, 0.0, 1.0) ); |
---|
385 | } |
---|
386 | |
---|
387 | void visual_object::setScale( double scale_ ) |
---|
388 | { |
---|
389 | scaleX = scale_; |
---|
390 | scaleY = scale_; |
---|
391 | scaleZ = scale_; |
---|
392 | } |
---|
393 | |
---|
394 | void visual_object::setScale( double scaleX_, double scaleY_, double scaleZ_ ) |
---|
395 | { |
---|
396 | scaleX = scaleX_; |
---|
397 | scaleY = scaleY_; |
---|
398 | scaleZ = scaleZ_; |
---|
399 | } |
---|
400 | |
---|
401 | bool visual_object::loadGeometry(std::string filename_) |
---|
402 | { |
---|
403 | // Check if file exists |
---|
404 | if( !osgDB::fileExists(filename_) ) |
---|
405 | { |
---|
406 | OSG_NOTIFY(osg::FATAL) << "Error: Model not loaded. File '" << filename_ << "' does not exist." << std::endl; |
---|
407 | } |
---|
408 | |
---|
409 | osg::ref_ptr<osg::Node> tmpModel = osgDB::readNodeFile( filename_ ); |
---|
410 | |
---|
411 | if( tmpModel.valid() ) |
---|
412 | { |
---|
413 | // remove old geometry |
---|
414 | geometry->removeChildren(0, geometry->getNumChildren()); |
---|
415 | |
---|
416 | // add new geometry |
---|
417 | geometry->addChild( tmpModel.get() ); |
---|
418 | return true; |
---|
419 | } |
---|
420 | else |
---|
421 | { |
---|
422 | std::cout <<": No model loaded: " << filename_ << std::endl; |
---|
423 | return false; |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | bool visual_object::setGeometry(osg::Node* geometry_) |
---|
428 | { |
---|
429 | // remove old geometry |
---|
430 | geometry->removeChildren(0, geometry->getNumChildren()); |
---|
431 | |
---|
432 | // add new geometry |
---|
433 | geometry->addChild( geometry_ ); |
---|
434 | |
---|
435 | return true; |
---|
436 | } |
---|
437 | |
---|
438 | void visual_object::unsetGeometry() |
---|
439 | { |
---|
440 | // remove old geometry |
---|
441 | geometry->removeChildren(0, geometry->getNumChildren()); |
---|
442 | |
---|
443 | // Set std OSG Node to allow tracking of an osgVisual without |
---|
444 | geometry->addChild( new osg::Node() ); |
---|
445 | } |
---|
446 | |
---|
447 | void visual_object::addUpdater( object_updater* updater_ ) |
---|
448 | { |
---|
449 | if ( updater.valid() ) |
---|
450 | updater->addUpdater( updater_ ); |
---|
451 | else |
---|
452 | updater = updater_; |
---|
453 | } |
---|
454 | |
---|
455 | void visual_object::clearAllUpdater() |
---|
456 | { |
---|
457 | // release only first updater. Because smartpointer: Will be deleted if not referenced. |
---|
458 | if ( updater.valid() ) |
---|
459 | updater = NULL; |
---|
460 | } |
---|
461 | |
---|
462 | std::vector<object_updater*> visual_object::getUpdaterList() |
---|
463 | { |
---|
464 | // iterate through updater and add all pointer. |
---|
465 | std::vector<object_updater*> updaterList; |
---|
466 | osg::ref_ptr<object_updater> tmpUpdater = updater; |
---|
467 | |
---|
468 | while (tmpUpdater.valid()) |
---|
469 | { |
---|
470 | updaterList.push_back( tmpUpdater ); |
---|
471 | tmpUpdater = tmpUpdater->getPointer(); |
---|
472 | } |
---|
473 | |
---|
474 | // return list |
---|
475 | return updaterList; |
---|
476 | } |
---|
477 | |
---|
478 | void visual_object::visual_objectPositionCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
479 | { |
---|
480 | visual_object* object = dynamic_cast<visual_object*>(node); |
---|
481 | if ( !object ) |
---|
482 | { |
---|
483 | OSG_NOTIFY(osg::FATAL) << "ERROR : No object found. Unable to apply this callback!" << std::endl; |
---|
484 | return; |
---|
485 | } |
---|
486 | |
---|
487 | // execute preUpdater to get new data of this object. |
---|
488 | if ( object->updater.valid() ) |
---|
489 | object->updater->preUpdate(object); |
---|
490 | |
---|
491 | // Nodepath from this node to absolute parent (if no endnode specified) |
---|
492 | osg::NodePath nodePath = nv->getNodePath(); |
---|
493 | |
---|
494 | // If Nodepath != empty, then mt = last element of node path |
---|
495 | osg::MatrixTransform* mt = nodePath.empty() ? 0 : dynamic_cast<osg::MatrixTransform*>(nodePath.back()); |
---|
496 | if (mt) |
---|
497 | { |
---|
498 | osg::CoordinateSystemNode* csn = 0; |
---|
499 | |
---|
500 | // find coordinate system node from our parental chain: traverse chain and try to convert every node to a csn. |
---|
501 | unsigned int i; |
---|
502 | for(i=0; i<nodePath.size() && csn==0; ++i) // "&& csn" means: exit loop if csn found |
---|
503 | { |
---|
504 | csn = dynamic_cast<osg::CoordinateSystemNode*>(nodePath[i]); // dynamic_cast returns 0 if dynamic_cast fails. |
---|
505 | } |
---|
506 | |
---|
507 | // Wenn csn gefunden: |
---|
508 | if (csn) |
---|
509 | { |
---|
510 | // Ellipsoidmodel erfragen |
---|
511 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
---|
512 | if (ellipsoid) |
---|
513 | { |
---|
514 | osg::Matrix inheritedMatrix; |
---|
515 | |
---|
516 | // durch den _restlichen_ Nodepath durchgehen und alle anfallenden Transformationen durchführen. |
---|
517 | for(i+=1; i<nodePath.size()-1; ++i) |
---|
518 | { |
---|
519 | osg::Transform* transform = nodePath[i]->asTransform(); // Versuchen, den Node zu einer Transformation zu konvertieren |
---|
520 | |
---|
521 | // wenn Node wirklich Trafo, dann die Tranformationsmatrix von Nodekoordinaten nach Global auf inheritedMatrix draufschlagen. |
---|
522 | if (transform) transform->computeLocalToWorldMatrix(inheritedMatrix, nv); |
---|
523 | } |
---|
524 | |
---|
525 | osg::Matrixd matrix(inheritedMatrix); |
---|
526 | |
---|
527 | // Set position |
---|
528 | ellipsoid->computeLocalToWorldTransformFromLatLongHeight(object->lat, object->lon, object->alt, matrix); |
---|
529 | |
---|
530 | // Set Upvector for position |
---|
531 | double X,Y,Z; |
---|
532 | util::calculateXYZAtWGS84Coordinate(object->lat, object->lon, object->alt, csn, X, Y, Z ); |
---|
533 | object->upVector = ellipsoid->computeLocalUpVector(X,Y,Z); |
---|
534 | |
---|
535 | // Set scale |
---|
536 | osg::Matrixd scaleMatrix; |
---|
537 | scaleMatrix.makeScale( object->scaleX, object->scaleY, object->scaleZ ); |
---|
538 | matrix.preMult( scaleMatrix ); |
---|
539 | |
---|
540 | // Set rotation |
---|
541 | // rotation von links ranmultiplizieren, entspricht: matrix = rotation * matrix. Da rotation ein Quat ist, wäre die direkte Multiplikation nur über Umwege machbar. |
---|
542 | // Rotate Object to Attitude. |
---|
543 | osg::Matrixd rotationMatrix; |
---|
544 | // Move Model by Azimuth |
---|
545 | rotationMatrix.makeRotate( -object->azimuthAngle_psi, osg::Vec3d(0.0, 0.0, 1.0) ); |
---|
546 | matrix.preMult(rotationMatrix); |
---|
547 | // Move Model by Pitch |
---|
548 | rotationMatrix.makeRotate( object->pitchAngle_theta, osg::Vec3d(1.0, 0.0, 0.0) ); |
---|
549 | matrix.preMult(rotationMatrix); |
---|
550 | // Move Model by Bank |
---|
551 | rotationMatrix.makeRotate( object->bankAngle_phi, osg::Vec3d(0.0, 1.0, 0.0) ); |
---|
552 | matrix.preMult(rotationMatrix); |
---|
553 | |
---|
554 | // Also update camera matrix (without geometry offset, because camera is interested in the objects matrix, not in the model's matrix.) |
---|
555 | object->cameraMatrix = matrix; |
---|
556 | /** \todo : Clean up camera matrix management: try to solve it with a single matrix. (each frame two matrix mults less) */ |
---|
557 | // dont know, why this rotation is necessary - maybe manipulator and node MatrixTransform interpret a matrix in different way? |
---|
558 | object->cameraMatrix.preMult( object->cameraTranslationOffset ); |
---|
559 | object->cameraMatrix.preMult( object->cameraRotationOffset ); |
---|
560 | |
---|
561 | |
---|
562 | // Set geometry correction |
---|
563 | matrix.preMultRotate( object->geometry_offset_rotation ); |
---|
564 | |
---|
565 | // Set cumulated object matrix as the matrix of this matrix transform |
---|
566 | mt->setMatrix(matrix); |
---|
567 | } |
---|
568 | } |
---|
569 | } |
---|
570 | |
---|
571 | // Call any nested callbacks. |
---|
572 | traverse(node,nv); |
---|
573 | |
---|
574 | // If SLAVE: execute postUpdater to pass new data of this object to dataIO. |
---|
575 | if( visual_dataIO::getInstance()->isSlave() ) |
---|
576 | { |
---|
577 | if ( object->updater.valid() ) |
---|
578 | object->updater->postUpdate(object); |
---|
579 | } |
---|
580 | |
---|
581 | } // Callbackfunction [ Operater() ] END |
---|
582 | |
---|
583 | void visual_object::setCameraOffsetTranslation( double x_, double y_, double z_) |
---|
584 | { |
---|
585 | cameraTranslationOffset.makeTranslate( osg::Vec3d(x_, y_, z_) ); // Trans: (rechts davon, longitudinal, vertikal) |
---|
586 | } |
---|
587 | |
---|
588 | void visual_object::setCameraOffset(double x_, double y_, double z_, double rotX_, double rotY_, double rotZ_) |
---|
589 | { |
---|
590 | setCameraOffsetTranslation( x_, y_, z_); |
---|
591 | setCameraOffsetRotation( rotX_, rotY_, rotZ_); |
---|
592 | } |
---|
593 | |
---|
594 | void visual_object::setCameraOffsetRotation(double rotX_, double rotY_, double rotZ_) |
---|
595 | { |
---|
596 | osg::Matrix tmp; |
---|
597 | cameraRotationOffset.makeRotate( osg::DegreesToRadians( 90.0 ), osg::Vec3(1, 0, 0) ); |
---|
598 | tmp.makeRotate( -rotZ_, osg::Vec3d(0.0, 1.0, 0.0) ); |
---|
599 | cameraRotationOffset.preMult(tmp); |
---|
600 | tmp.makeRotate( rotY_, osg::Vec3d(1.0, 0.0, 0.0) ); |
---|
601 | cameraRotationOffset.preMult(tmp); |
---|
602 | tmp.makeRotate( -rotX_, osg::Vec3d(0.0, 0.0, 1.0) ); |
---|
603 | cameraRotationOffset.preMult(tmp); |
---|
604 | } |
---|
605 | |
---|
606 | void visual_object::clearLabels() |
---|
607 | { |
---|
608 | labels->removeDrawables(0, labels->getNumDrawables()); |
---|
609 | } |
---|
610 | |
---|
611 | void visual_object::addLabel(std::string idString_, std::string label_, osg::Vec4 color_, osg::Vec3 offset_) |
---|
612 | { |
---|
613 | osg::ref_ptr<osgText::Text> text = new osgText::Text(); |
---|
614 | |
---|
615 | text->setName(idString_); |
---|
616 | text->setText(label_); |
---|
617 | text->setColor(color_); |
---|
618 | text->setFont("fonts/arial.ttf"); |
---|
619 | text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); |
---|
620 | text->setAutoRotateToScreen(true); |
---|
621 | text->setPosition(offset_); |
---|
622 | |
---|
623 | text->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
---|
624 | labels->addDrawable( text ); |
---|
625 | } |
---|
626 | |
---|
627 | bool visual_object::removeLabel(std::string idString_) |
---|
628 | { |
---|
629 | osg::Node* labelToRemove = util::findNamedNode(idString_, this); |
---|
630 | |
---|
631 | if(labelToRemove) |
---|
632 | { |
---|
633 | removeChild( labelToRemove ); |
---|
634 | return true; |
---|
635 | } |
---|
636 | else |
---|
637 | return false; |
---|
638 | } |
---|
639 | |
---|
640 | bool visual_object::updateLabelText(std::string idString_, std::string label_) |
---|
641 | { |
---|
642 | osg::Node* labelToUpdate = util::findNamedNode(idString_, this); |
---|
643 | |
---|
644 | if(labelToUpdate) |
---|
645 | { |
---|
646 | osgText::Text* text = dynamic_cast<osgText::Text*>(labelToUpdate); |
---|
647 | if(text) |
---|
648 | { |
---|
649 | text->setText(label_); |
---|
650 | return true; |
---|
651 | } |
---|
652 | return false; |
---|
653 | } |
---|
654 | return false; |
---|
655 | } |
---|
656 | |
---|
657 | osgText::Text* visual_object::getLabel(std::string idString_) |
---|
658 | { |
---|
659 | osg::Node* labelToFind = util::findNamedNode(idString_, this); |
---|
660 | |
---|
661 | if(labelToFind) |
---|
662 | { |
---|
663 | osgText::Text* text = dynamic_cast<osgText::Text*>(labelToFind); |
---|
664 | if(text) |
---|
665 | return text; |
---|
666 | } |
---|
667 | return NULL; |
---|
668 | |
---|
669 | } |
---|
670 | |
---|
671 | bool visual_object::setDrawLabelAsOverlay(std::string idString_, bool drawAsOverlay) |
---|
672 | { |
---|
673 | osg::Node* labelToFind = util::findNamedNode(idString_, this); |
---|
674 | |
---|
675 | if(labelToFind) |
---|
676 | { |
---|
677 | if (drawAsOverlay) |
---|
678 | labelToFind->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
---|
679 | else |
---|
680 | labelToFind->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON); |
---|
681 | return true; |
---|
682 | } |
---|
683 | else |
---|
684 | return false; |
---|
685 | } |
---|
686 | |
---|
687 | bool visual_object::getDrawLabelAsOverlay(std::string idString_) |
---|
688 | { |
---|
689 | osg::Node* labelToFind = util::findNamedNode(idString_, this); |
---|
690 | |
---|
691 | if(labelToFind) |
---|
692 | { |
---|
693 | if(labelToFind->getOrCreateStateSet()->getMode(GL_DEPTH_TEST) == osg::StateAttribute::OFF) |
---|
694 | return false; |
---|
695 | else |
---|
696 | return true; |
---|
697 | } |
---|
698 | return false; |
---|
699 | } |
---|