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