[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; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | visual_dataIO::~visual_dataIO() |
---|
| 29 | { |
---|
[121] | 30 | // Delete all slots: |
---|
| 31 | for(unsigned int i=0;i<dataSlots.size();i++) |
---|
| 32 | { |
---|
| 33 | delete dataSlots[i]; |
---|
| 34 | } |
---|
| 35 | dataSlots.clear(); |
---|
| 36 | |
---|
[31] | 37 | OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO destructed" << std::endl; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | visual_dataIO* visual_dataIO::getInstance() |
---|
| 41 | { |
---|
| 42 | static visual_dataIO instance; |
---|
| 43 | return &instance; |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | void visual_dataIO::init(osgViewer::Viewer* viewer_, osg::ArgumentParser& arguments_) |
---|
| 47 | { |
---|
| 48 | OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO init()" << std::endl; |
---|
| 49 | |
---|
| 50 | // Init variables |
---|
| 51 | viewer = viewer_; |
---|
| 52 | |
---|
| 53 | // Parse operating Mode |
---|
| 54 | if (arguments_.read("-m")) |
---|
| 55 | { |
---|
| 56 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as MASTER" << std::endl; |
---|
| 57 | clusterMode = osgVisual::dataIO_cluster::MASTER; |
---|
| 58 | // Create Container: |
---|
| 59 | slotContainer = new osgVisual::dataIO_transportContainer(); |
---|
| 60 | } |
---|
| 61 | else if (arguments_.read("-s")) |
---|
| 62 | { |
---|
| 63 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as SLAVE" << std::endl; |
---|
| 64 | clusterMode = osgVisual::dataIO_cluster::SLAVE; |
---|
[67] | 65 | slotContainer = NULL; // Slave only recieves container, therefor set this Pointer null. |
---|
[31] | 66 | } |
---|
| 67 | else |
---|
| 68 | { |
---|
| 69 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as STANDALONE" << std::endl; |
---|
| 70 | clusterMode = osgVisual::dataIO_cluster::STANDALONE; |
---|
| 71 | // Create Container: |
---|
| 72 | slotContainer = new osgVisual::dataIO_transportContainer(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | // Create Cluster. |
---|
[58] | 76 | #ifdef USE_CLUSTER_ASIO_TCP_IOSTREAM |
---|
| 77 | cluster = new dataIO_clusterAsioTcpIostream(); |
---|
| 78 | #endif |
---|
[65] | 79 | #ifdef USE_CLUSTER_ENET |
---|
| 80 | cluster = new dataIO_clusterENet(); |
---|
[69] | 81 | cluster->enableHardSync( false ); /** \todo : rebuild this structure in cluster.h and move it this way to a general implementation. */ |
---|
[65] | 82 | #endif |
---|
[84] | 83 | #ifdef USE_CLUSTER_DUMMY |
---|
| 84 | cluster = new dataIO_clusterDummy(); |
---|
| 85 | #endif |
---|
[65] | 86 | if(cluster.valid()) |
---|
[69] | 87 | //cluster->init(arguments_, clusterMode, slotContainer, true, false); |
---|
[75] | 88 | cluster->init(arguments_, viewer_, clusterMode, slotContainer, false, false); |
---|
[31] | 89 | |
---|
| 90 | // Create extLink. |
---|
| 91 | #ifdef USE_EXTLINK_DUMMY |
---|
| 92 | extLink = new dataIO_extLinkDummy( dataSlots ); |
---|
| 93 | #endif |
---|
| 94 | #ifdef USE_EXTLINK_VCL |
---|
| 95 | extLink = new dataIO_extLinkVCL( dataSlots ); |
---|
| 96 | #endif |
---|
| 97 | extLink->init(); |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | // Install callbacks to perform DataIO activities every frame: |
---|
| 102 | //// EventCallback at the absolute beginning of the frame |
---|
| 103 | eventCallback = new dataIO_eventCallback(this); |
---|
| 104 | viewer->getCamera()->setEventCallback( eventCallback ); |
---|
| 105 | //// FinalDrawCallback at the end of event and update handling, but BEFORE rendering the frame |
---|
| 106 | finalDrawCallback = new dataIO_finalDrawCallback(this); |
---|
| 107 | viewer->getCamera()->setFinalDrawCallback( finalDrawCallback ); |
---|
| 108 | |
---|
| 109 | initialized = true; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void visual_dataIO::shutdown() |
---|
| 113 | { |
---|
| 114 | if(initialized) |
---|
| 115 | { |
---|
| 116 | OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_dataIO..." << std::endl; |
---|
| 117 | |
---|
[70] | 118 | viewer->getCamera()->removeEventCallback( eventCallback ); |
---|
[31] | 119 | eventCallback = NULL; |
---|
| 120 | viewer->getCamera()->setFinalDrawCallback( NULL ); |
---|
| 121 | finalDrawCallback = NULL; |
---|
[70] | 122 | |
---|
[31] | 123 | viewer = NULL; |
---|
[70] | 124 | |
---|
[31] | 125 | |
---|
[65] | 126 | if(cluster.valid()) |
---|
| 127 | cluster->shutdown(); |
---|
| 128 | if(extLink.valid()) |
---|
[31] | 129 | extLink->shutdown(); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void visual_dataIO::dataIO_eventCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
| 134 | { |
---|
| 135 | // perform all actions for the eventDrawCallback. |
---|
| 136 | OSG_NOTIFY( osg::INFO ) << "---- Executing EventCallback.." << std::endl; |
---|
| 137 | |
---|
| 138 | switch( dataIO->clusterMode ) |
---|
| 139 | { |
---|
| 140 | case osgVisual::dataIO_cluster::MASTER : |
---|
| 141 | { |
---|
| 142 | dataIO->extLink->readTO_OBJvalues(); |
---|
[82] | 143 | dataIO->cluster->sendTO_OBJvaluesToSlaves(dataIO->calcViewMatrix()); |
---|
[31] | 144 | } |
---|
| 145 | break; |
---|
| 146 | case osgVisual::dataIO_cluster::SLAVE : |
---|
| 147 | { |
---|
| 148 | dataIO->cluster->readTO_OBJvaluesFromMaster(); |
---|
| 149 | } |
---|
| 150 | break; |
---|
| 151 | case osgVisual::dataIO_cluster::STANDALONE : |
---|
| 152 | { |
---|
| 153 | dataIO->extLink->readTO_OBJvalues(); |
---|
| 154 | } |
---|
| 155 | break; |
---|
| 156 | default: |
---|
| 157 | OSG_NOTIFY( osg::FATAL ) << "ERROR: Unkown clustermode!" << std::endl; |
---|
| 158 | break; |
---|
| 159 | }; |
---|
[73] | 160 | traverse(node, nv); |
---|
[31] | 161 | } |
---|
| 162 | |
---|
| 163 | void visual_dataIO::dataIO_finalDrawCallback::operator() (const osg::Camera& camera) const |
---|
| 164 | { |
---|
| 165 | // perform all actions for the initialDrawCallback. |
---|
| 166 | OSG_NOTIFY( osg::INFO ) << "---- Executing InitialDrawCallback.." << std::endl; |
---|
| 167 | |
---|
| 168 | switch( dataIO->clusterMode ) |
---|
| 169 | { |
---|
| 170 | case osgVisual::dataIO_cluster::MASTER : |
---|
| 171 | { |
---|
| 172 | dataIO->extLink->writebackFROM_OBJvalues(); |
---|
| 173 | dataIO->cluster->waitForAllReadyToSwap(); |
---|
| 174 | dataIO->cluster->sendSwapCommand(); |
---|
| 175 | } |
---|
| 176 | break; |
---|
| 177 | case osgVisual::dataIO_cluster::SLAVE : |
---|
| 178 | { |
---|
| 179 | dataIO->cluster->reportAsReadyToSwap(); |
---|
| 180 | dataIO->cluster->waitForSwap(); |
---|
| 181 | } |
---|
| 182 | break; |
---|
| 183 | case osgVisual::dataIO_cluster::STANDALONE : |
---|
| 184 | { |
---|
| 185 | dataIO->extLink->writebackFROM_OBJvalues(); |
---|
| 186 | } |
---|
| 187 | break; |
---|
| 188 | default: |
---|
| 189 | OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_dataIO::dataIO_finalDrawCallback::operator() - Unkown clustermode!" << std::endl; |
---|
| 190 | break; |
---|
| 191 | }; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | void* visual_dataIO::getSlotPointer(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, osgVisual::dataIO_slot::varType variableTyp_ ) |
---|
| 195 | { |
---|
| 196 | // iterate through slotlist. If found, return pointer, else add slot to list and return pointer |
---|
| 197 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 198 | { |
---|
| 199 | // Check if this variable name&-type already exists |
---|
[118] | 200 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == variableTyp_) |
---|
[31] | 201 | { |
---|
| 202 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot found at position " << i << std::endl; |
---|
| 203 | // Return pointer to the value |
---|
[118] | 204 | return dataSlots[i]; |
---|
[31] | 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | // Slot does not exist -> add it to slot list |
---|
| 209 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot not found, will add as new slot " << std::endl; |
---|
[118] | 210 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
| 211 | newSlot->variableName = variableName_; |
---|
| 212 | newSlot->variableType = variableTyp_; |
---|
| 213 | newSlot->value = 0; |
---|
| 214 | newSlot->sValue = ""; |
---|
[31] | 215 | dataSlots.push_back( newSlot ); |
---|
[118] | 216 | return dataSlots.back(); |
---|
[31] | 217 | } |
---|
| 218 | |
---|
| 219 | double visual_dataIO::getSlotDataAsDouble(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) |
---|
| 220 | { |
---|
| 221 | // iterate through slotlist. If found, return value |
---|
| 222 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 223 | { |
---|
| 224 | // Check if this variable name&-type already exists |
---|
[118] | 225 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::DOUBLE ) |
---|
[31] | 226 | { |
---|
| 227 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl; |
---|
[118] | 228 | return dataSlots[i]->value; |
---|
[31] | 229 | } |
---|
| 230 | } |
---|
| 231 | return 0; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | std::string visual_dataIO::getSlotDataAsString(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) |
---|
| 235 | { |
---|
| 236 | // iterate through slotlist. If found, return value |
---|
| 237 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 238 | { |
---|
| 239 | // Check if this variable name&-type already exists |
---|
[118] | 240 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::STRING ) |
---|
[31] | 241 | { |
---|
| 242 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl; |
---|
[118] | 243 | return dataSlots[i]->sValue; |
---|
[31] | 244 | } |
---|
| 245 | } |
---|
| 246 | return ""; |
---|
| 247 | } |
---|
| 248 | |
---|
[116] | 249 | osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, std::string sValue_ ) |
---|
[31] | 250 | { |
---|
| 251 | bool slotFound = false; |
---|
| 252 | // iterate through slotlist. If found, return pointer, else add slot to list |
---|
| 253 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 254 | { |
---|
| 255 | // Check if this variable name&-type already exists |
---|
[118] | 256 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::STRING) |
---|
[31] | 257 | { |
---|
| 258 | // Update value |
---|
[118] | 259 | dataSlots[i]->sValue = sValue_; |
---|
[31] | 260 | slotFound = true; |
---|
[118] | 261 | return dataSlots[i]; |
---|
[31] | 262 | } |
---|
| 263 | |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | if (!slotFound) |
---|
| 267 | { |
---|
| 268 | // Slot does not exist -> add it to slot list |
---|
[118] | 269 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
| 270 | newSlot->variableName = variableName_; |
---|
| 271 | newSlot->direction = direction_; |
---|
| 272 | newSlot->variableType = osgVisual::dataIO_slot::STRING; |
---|
| 273 | newSlot->value = 0; |
---|
| 274 | newSlot->sValue = sValue_; |
---|
[31] | 275 | dataSlots.push_back( newSlot ); |
---|
[118] | 276 | return dataSlots.back(); |
---|
[31] | 277 | } |
---|
[116] | 278 | |
---|
| 279 | return NULL; |
---|
[31] | 280 | } |
---|
| 281 | |
---|
[116] | 282 | osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, double value_ ) |
---|
[31] | 283 | { |
---|
| 284 | // iterate through slotlist. If found, return pointer, else add slot to list |
---|
| 285 | bool slotFound = false; |
---|
| 286 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
| 287 | { |
---|
| 288 | // Check if this variableName & -type already exists |
---|
[118] | 289 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::DOUBLE) |
---|
[31] | 290 | { |
---|
| 291 | // Update value |
---|
| 292 | //OSG_NOTIFY( osg::ALWAYS ) << "setSlotData: " << variableName_ << " - value: " << value_ << std::endl; |
---|
[118] | 293 | dataSlots[i]->value = value_; |
---|
[31] | 294 | slotFound = true; |
---|
[118] | 295 | return dataSlots[i]; |
---|
[31] | 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | if (!slotFound) |
---|
| 300 | { |
---|
| 301 | // Slot does not exist -> add it to slot list |
---|
[118] | 302 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
| 303 | newSlot->variableName = variableName_; |
---|
| 304 | newSlot->direction = direction_; |
---|
| 305 | newSlot->variableType = osgVisual::dataIO_slot::DOUBLE; |
---|
| 306 | newSlot->value = value_; |
---|
| 307 | newSlot->sValue = ""; |
---|
[31] | 308 | dataSlots.push_back( newSlot ); |
---|
[118] | 309 | return dataSlots.back(); |
---|
[31] | 310 | } |
---|
[116] | 311 | |
---|
| 312 | return NULL; |
---|
[82] | 313 | } |
---|
| 314 | |
---|
| 315 | osg::Matrixd visual_dataIO::calcViewMatrix() |
---|
| 316 | { |
---|
| 317 | return viewer->getCameraManipulator()->getInverseMatrix(); |
---|
[31] | 318 | } |
---|