[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_dataIO.h" |
---|
| 18 | |
---|
| 19 | using namespace osgVisual; |
---|
| 20 | |
---|
| 21 | visual_dataIO::visual_dataIO() |
---|
| 22 | { |
---|
| 23 | OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO constructed" << std::endl; |
---|
[88] | 24 | |
---|
[31] | 25 | initialized = false; |
---|
[157] | 26 | clusterMode = osgVisual::dataIO_cluster::STANDALONE; |
---|
| 27 | // Create Transport-Container: |
---|
| 28 | slotContainer = new osgVisual::dataIO_transportContainer(); |
---|
[31] | 29 | } |
---|
| 30 | |
---|
| 31 | visual_dataIO::~visual_dataIO() |
---|
| 32 | { |
---|
[121] | 33 | // Delete all slots: |
---|
| 34 | for(unsigned int i=0;i<dataSlots.size();i++) |
---|
| 35 | { |
---|
| 36 | delete dataSlots[i]; |
---|
| 37 | } |
---|
| 38 | dataSlots.clear(); |
---|
| 39 | |
---|
[31] | 40 | OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO destructed" << std::endl; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | visual_dataIO* visual_dataIO::getInstance() |
---|
| 44 | { |
---|
| 45 | static visual_dataIO instance; |
---|
| 46 | return &instance; |
---|
| 47 | }; |
---|
| 48 | |
---|
[144] | 49 | void visual_dataIO::init(osgViewer::Viewer* viewer_, osg::ArgumentParser& arguments_, std::string configFileName) |
---|
[31] | 50 | { |
---|
[160] | 51 | OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO initialize.." << std::endl; |
---|
[31] | 52 | |
---|
| 53 | // Init variables |
---|
| 54 | viewer = viewer_; |
---|
| 55 | |
---|
[157] | 56 | // Process XML configuration |
---|
[160] | 57 | this->configFileName = configFileName; |
---|
[157] | 58 | if(!processXMLConfiguration()) |
---|
[160] | 59 | OSG_FATAL << "ERROR: visual_dataIO::init() - Failed to initialize dataIO via XML configuration!"; |
---|
[157] | 60 | |
---|
[31] | 61 | |
---|
| 62 | // Create Cluster. |
---|
[58] | 63 | #ifdef USE_CLUSTER_ASIO_TCP_IOSTREAM |
---|
| 64 | cluster = new dataIO_clusterAsioTcpIostream(); |
---|
| 65 | #endif |
---|
[65] | 66 | #ifdef USE_CLUSTER_ENET |
---|
| 67 | cluster = new dataIO_clusterENet(); |
---|
[69] | 68 | cluster->enableHardSync( false ); /** \todo : rebuild this structure in cluster.h and move it this way to a general implementation. */ |
---|
[65] | 69 | #endif |
---|
[84] | 70 | #ifdef USE_CLUSTER_DUMMY |
---|
| 71 | cluster = new dataIO_clusterDummy(); |
---|
| 72 | #endif |
---|
[65] | 73 | if(cluster.valid()) |
---|
[69] | 74 | //cluster->init(arguments_, clusterMode, slotContainer, true, false); |
---|
[75] | 75 | cluster->init(arguments_, viewer_, clusterMode, slotContainer, false, false); |
---|
[31] | 76 | |
---|
| 77 | // Create extLink. |
---|
| 78 | #ifdef USE_EXTLINK_DUMMY |
---|
| 79 | extLink = new dataIO_extLinkDummy( dataSlots ); |
---|
| 80 | #endif |
---|
| 81 | #ifdef USE_EXTLINK_VCL |
---|
| 82 | extLink = new dataIO_extLinkVCL( dataSlots ); |
---|
| 83 | #endif |
---|
| 84 | extLink->init(); |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | // Install callbacks to perform DataIO activities every frame: |
---|
| 89 | //// EventCallback at the absolute beginning of the frame |
---|
| 90 | eventCallback = new dataIO_eventCallback(this); |
---|
| 91 | viewer->getCamera()->setEventCallback( eventCallback ); |
---|
| 92 | //// FinalDrawCallback at the end of event and update handling, but BEFORE rendering the frame |
---|
| 93 | finalDrawCallback = new dataIO_finalDrawCallback(this); |
---|
| 94 | viewer->getCamera()->setFinalDrawCallback( finalDrawCallback ); |
---|
| 95 | |
---|
| 96 | initialized = true; |
---|
| 97 | } |
---|
| 98 | |
---|
[157] | 99 | bool visual_dataIO::processXMLConfiguration() |
---|
| 100 | { |
---|
[160] | 101 | // Init XML |
---|
| 102 | xmlDoc* tmpDoc; |
---|
| 103 | bool disabled; |
---|
| 104 | xmlNode* config = util::getModuleXMLConfig( configFileName, "dataio", tmpDoc, disabled ); |
---|
[157] | 105 | |
---|
[160] | 106 | if( disabled) |
---|
| 107 | OSG_NOTIFY( osg::ALWAYS ) << "..disabled by XML configuration file. dataIO can't be disabled. Ignoring." << std::endl; |
---|
| 108 | |
---|
| 109 | // extract configuration values |
---|
| 110 | if(config) |
---|
| 111 | { |
---|
| 112 | xmlNode* a_node = config->children; |
---|
| 113 | |
---|
| 114 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
| 115 | { |
---|
| 116 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
| 117 | //OSG_ALWAYS << "----visual_distortion::processXMLConfiguration() - node type="<< cur_node->type <<", name=" << cur_node->name << std::endl; |
---|
| 118 | |
---|
| 119 | // Check for dataio node |
---|
| 120 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "dataio") |
---|
| 121 | { |
---|
| 122 | // Extract cluster role |
---|
| 123 | xmlAttr *attr = cur_node->properties; |
---|
| 124 | while ( attr ) |
---|
| 125 | { |
---|
| 126 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 127 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 128 | if( attr_name == "clusterrole" ) |
---|
| 129 | { |
---|
| 130 | if(attr_value == "master") |
---|
| 131 | { |
---|
| 132 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as MASTER" << std::endl; |
---|
| 133 | clusterMode = osgVisual::dataIO_cluster::MASTER; |
---|
| 134 | } |
---|
| 135 | else if(attr_value == "slave") |
---|
| 136 | { |
---|
| 137 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as SLAVE" << std::endl; |
---|
| 138 | clusterMode = osgVisual::dataIO_cluster::SLAVE; |
---|
| 139 | slotContainer = NULL; // Slave only recieves container, therefor set this Pointer NULL (created instance will be deleted because it is an auto pointer). |
---|
| 140 | } |
---|
| 141 | else if(attr_value == "standalone") |
---|
| 142 | { |
---|
| 143 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as STANDALONE" << std::endl; |
---|
| 144 | clusterMode = osgVisual::dataIO_cluster::STANDALONE; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | attr = attr->next; |
---|
| 148 | } // WHILE attrib END |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // Check for cluster node |
---|
| 152 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "cluster") |
---|
| 153 | { |
---|
| 154 | // Check Attributes to determine if the dummy implementation or any other implementation must be instantiated |
---|
| 155 | |
---|
| 156 | // Pass XML attributes to cluster to analyse and configure it by cluster itself |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | // Check for extLink node |
---|
| 160 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "extlink") |
---|
| 161 | { |
---|
| 162 | // Check Attributes to determine if the dummy implementation or any other implementation must be instantiated |
---|
| 163 | |
---|
| 164 | // Pass XML attributes to extlink to analyse and configure it by extLink itself |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | } // FOR all nodes END |
---|
| 168 | |
---|
| 169 | // clean up |
---|
| 170 | xmlFreeDoc(tmpDoc); xmlCleanupParser(); |
---|
| 171 | return true; |
---|
| 172 | } // IF Config valid END |
---|
| 173 | else |
---|
| 174 | { |
---|
| 175 | OSG_WARN << "ERROR: visual_data::processXMLConfiguration() - Module configuration not found" << std::endl; |
---|
| 176 | return false; |
---|
| 177 | } |
---|
| 178 | |
---|
[157] | 179 | return true; |
---|
| 180 | } |
---|
| 181 | |
---|
[31] | 182 | void visual_dataIO::shutdown() |
---|
| 183 | { |
---|
| 184 | if(initialized) |
---|
| 185 | { |
---|
| 186 | OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_dataIO..." << std::endl; |
---|
| 187 | |
---|
[70] | 188 | viewer->getCamera()->removeEventCallback( eventCallback ); |
---|
[31] | 189 | eventCallback = NULL; |
---|
| 190 | viewer->getCamera()->setFinalDrawCallback( NULL ); |
---|
| 191 | finalDrawCallback = NULL; |
---|
[70] | 192 | |
---|
[31] | 193 | viewer = NULL; |
---|
[70] | 194 | |
---|
[31] | 195 | |
---|
[65] | 196 | if(cluster.valid()) |
---|
| 197 | cluster->shutdown(); |
---|
| 198 | if(extLink.valid()) |
---|
[31] | 199 | extLink->shutdown(); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void visual_dataIO::dataIO_eventCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
| 204 | { |
---|
| 205 | // perform all actions for the eventDrawCallback. |
---|
| 206 | OSG_NOTIFY( osg::INFO ) << "---- Executing EventCallback.." << std::endl; |
---|
| 207 | |
---|
| 208 | switch( dataIO->clusterMode ) |
---|
| 209 | { |
---|
| 210 | case osgVisual::dataIO_cluster::MASTER : |
---|
| 211 | { |
---|
| 212 | dataIO->extLink->readTO_OBJvalues(); |
---|
[82] | 213 | dataIO->cluster->sendTO_OBJvaluesToSlaves(dataIO->calcViewMatrix()); |
---|
[31] | 214 | } |
---|
| 215 | break; |
---|
| 216 | case osgVisual::dataIO_cluster::SLAVE : |
---|
| 217 | { |
---|
| 218 | dataIO->cluster->readTO_OBJvaluesFromMaster(); |
---|
| 219 | } |
---|
| 220 | break; |
---|
| 221 | case osgVisual::dataIO_cluster::STANDALONE : |
---|
| 222 | { |
---|
| 223 | dataIO->extLink->readTO_OBJvalues(); |
---|
| 224 | } |
---|
| 225 | break; |
---|
| 226 | default: |
---|
| 227 | OSG_NOTIFY( osg::FATAL ) << "ERROR: Unkown clustermode!" << std::endl; |
---|
| 228 | break; |
---|
| 229 | }; |
---|
[73] | 230 | traverse(node, nv); |
---|
[31] | 231 | } |
---|
| 232 | |
---|
| 233 | void visual_dataIO::dataIO_finalDrawCallback::operator() (const osg::Camera& camera) const |
---|
| 234 | { |
---|
| 235 | // perform all actions for the initialDrawCallback. |
---|
| 236 | OSG_NOTIFY( osg::INFO ) << "---- Executing InitialDrawCallback.." << std::endl; |
---|
| 237 | |
---|
| 238 | switch( dataIO->clusterMode ) |
---|
| 239 | { |
---|
| 240 | case osgVisual::dataIO_cluster::MASTER : |
---|
| 241 | { |
---|
| 242 | dataIO->extLink->writebackFROM_OBJvalues(); |
---|
| 243 | dataIO->cluster->waitForAllReadyToSwap(); |
---|
| 244 | dataIO->cluster->sendSwapCommand(); |
---|
| 245 | } |
---|
| 246 | break; |
---|
| 247 | case osgVisual::dataIO_cluster::SLAVE : |
---|
| 248 | { |
---|
| 249 | dataIO->cluster->reportAsReadyToSwap(); |
---|
| 250 | dataIO->cluster->waitForSwap(); |
---|
| 251 | } |
---|
| 252 | break; |
---|
| 253 | case osgVisual::dataIO_cluster::STANDALONE : |
---|
| 254 | { |
---|
| 255 | dataIO->extLink->writebackFROM_OBJvalues(); |
---|
| 256 | } |
---|
| 257 | break; |
---|
| 258 | default: |
---|
| 259 | OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_dataIO::dataIO_finalDrawCallback::operator() - Unkown clustermode!" << std::endl; |
---|
| 260 | break; |
---|
| 261 | }; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | void* visual_dataIO::getSlotPointer(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, osgVisual::dataIO_slot::varType variableTyp_ ) |
---|
| 265 | { |
---|
| 266 | // iterate through slotlist. If found, return pointer, else add slot to list and return pointer |
---|
| 267 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 268 | { |
---|
| 269 | // Check if this variable name&-type already exists |
---|
[118] | 270 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == variableTyp_) |
---|
[31] | 271 | { |
---|
| 272 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot found at position " << i << std::endl; |
---|
| 273 | // Return pointer to the value |
---|
[118] | 274 | return dataSlots[i]; |
---|
[31] | 275 | } |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | // Slot does not exist -> add it to slot list |
---|
| 279 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot not found, will add as new slot " << std::endl; |
---|
[118] | 280 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
| 281 | newSlot->variableName = variableName_; |
---|
| 282 | newSlot->variableType = variableTyp_; |
---|
| 283 | newSlot->value = 0; |
---|
| 284 | newSlot->sValue = ""; |
---|
[31] | 285 | dataSlots.push_back( newSlot ); |
---|
[118] | 286 | return dataSlots.back(); |
---|
[31] | 287 | } |
---|
| 288 | |
---|
| 289 | double visual_dataIO::getSlotDataAsDouble(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) |
---|
| 290 | { |
---|
| 291 | // iterate through slotlist. If found, return value |
---|
| 292 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 293 | { |
---|
| 294 | // Check if this variable name&-type already exists |
---|
[118] | 295 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::DOUBLE ) |
---|
[31] | 296 | { |
---|
| 297 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl; |
---|
[118] | 298 | return dataSlots[i]->value; |
---|
[31] | 299 | } |
---|
| 300 | } |
---|
| 301 | return 0; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | std::string visual_dataIO::getSlotDataAsString(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) |
---|
| 305 | { |
---|
| 306 | // iterate through slotlist. If found, return value |
---|
| 307 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 308 | { |
---|
| 309 | // Check if this variable name&-type already exists |
---|
[118] | 310 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::STRING ) |
---|
[31] | 311 | { |
---|
| 312 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl; |
---|
[118] | 313 | return dataSlots[i]->sValue; |
---|
[31] | 314 | } |
---|
| 315 | } |
---|
| 316 | return ""; |
---|
| 317 | } |
---|
| 318 | |
---|
[116] | 319 | osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, std::string sValue_ ) |
---|
[31] | 320 | { |
---|
| 321 | bool slotFound = false; |
---|
| 322 | // iterate through slotlist. If found, return pointer, else add slot to list |
---|
| 323 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 324 | { |
---|
| 325 | // Check if this variable name&-type already exists |
---|
[118] | 326 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::STRING) |
---|
[31] | 327 | { |
---|
| 328 | // Update value |
---|
[118] | 329 | dataSlots[i]->sValue = sValue_; |
---|
[31] | 330 | slotFound = true; |
---|
[118] | 331 | return dataSlots[i]; |
---|
[31] | 332 | } |
---|
| 333 | |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | if (!slotFound) |
---|
| 337 | { |
---|
| 338 | // Slot does not exist -> add it to slot list |
---|
[118] | 339 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
| 340 | newSlot->variableName = variableName_; |
---|
| 341 | newSlot->direction = direction_; |
---|
| 342 | newSlot->variableType = osgVisual::dataIO_slot::STRING; |
---|
| 343 | newSlot->value = 0; |
---|
| 344 | newSlot->sValue = sValue_; |
---|
[31] | 345 | dataSlots.push_back( newSlot ); |
---|
[118] | 346 | return dataSlots.back(); |
---|
[31] | 347 | } |
---|
[116] | 348 | |
---|
| 349 | return NULL; |
---|
[31] | 350 | } |
---|
| 351 | |
---|
[116] | 352 | osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, double value_ ) |
---|
[31] | 353 | { |
---|
| 354 | // iterate through slotlist. If found, return pointer, else add slot to list |
---|
| 355 | bool slotFound = false; |
---|
| 356 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 357 | { |
---|
| 358 | // Check if this variableName & -type already exists |
---|
[118] | 359 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::DOUBLE) |
---|
[31] | 360 | { |
---|
| 361 | // Update value |
---|
| 362 | //OSG_NOTIFY( osg::ALWAYS ) << "setSlotData: " << variableName_ << " - value: " << value_ << std::endl; |
---|
[118] | 363 | dataSlots[i]->value = value_; |
---|
[31] | 364 | slotFound = true; |
---|
[118] | 365 | return dataSlots[i]; |
---|
[31] | 366 | } |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | if (!slotFound) |
---|
| 370 | { |
---|
| 371 | // Slot does not exist -> add it to slot list |
---|
[118] | 372 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
| 373 | newSlot->variableName = variableName_; |
---|
| 374 | newSlot->direction = direction_; |
---|
| 375 | newSlot->variableType = osgVisual::dataIO_slot::DOUBLE; |
---|
| 376 | newSlot->value = value_; |
---|
| 377 | newSlot->sValue = ""; |
---|
[31] | 378 | dataSlots.push_back( newSlot ); |
---|
[118] | 379 | return dataSlots.back(); |
---|
[31] | 380 | } |
---|
[116] | 381 | |
---|
| 382 | return NULL; |
---|
[82] | 383 | } |
---|
| 384 | |
---|
| 385 | osg::Matrixd visual_dataIO::calcViewMatrix() |
---|
| 386 | { |
---|
| 387 | return viewer->getCameraManipulator()->getInverseMatrix(); |
---|
[31] | 388 | } |
---|