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