source: osgVisual/src/dataIO/visual_dataIO.cpp @ 121

Last change on this file since 121 was 121, checked in by Torben Dannhauer, 14 years ago
File size: 10.2 KB
Line 
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
19using namespace osgVisual;
20
21visual_dataIO::visual_dataIO()
22{
23        OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO constructed" << std::endl;
24
25        initialized = false;
26}
27
28visual_dataIO::~visual_dataIO()
29{
30        // Delete all slots:
31        for(unsigned int i=0;i<dataSlots.size();i++)
32        {
33                delete dataSlots[i];
34        }
35        dataSlots.clear();
36       
37        OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO destructed" << std::endl;
38}
39
40visual_dataIO* visual_dataIO::getInstance()
41{
42        static visual_dataIO instance; 
43        return &instance; 
44};
45
46void 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;
65                slotContainer = NULL;   // Slave only recieves container, therefor set this Pointer null.
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.
76        #ifdef USE_CLUSTER_ASIO_TCP_IOSTREAM
77                cluster = new dataIO_clusterAsioTcpIostream();
78        #endif
79        #ifdef USE_CLUSTER_ENET
80                cluster = new dataIO_clusterENet();
81                cluster->enableHardSync( false );       /** \todo : rebuild this structure in cluster.h and move it this way to a general implementation. */
82        #endif
83        #ifdef USE_CLUSTER_DUMMY
84                cluster = new dataIO_clusterDummy();
85        #endif
86        if(cluster.valid())
87                //cluster->init(arguments_, clusterMode, slotContainer, true, false);
88                cluster->init(arguments_, viewer_, clusterMode, slotContainer, false, false);
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
112void visual_dataIO::shutdown()
113{
114        if(initialized)
115        {
116                OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_dataIO..." << std::endl;
117
118                viewer->getCamera()->removeEventCallback( eventCallback );
119                eventCallback = NULL;
120                viewer->getCamera()->setFinalDrawCallback( NULL );
121                finalDrawCallback = NULL;
122               
123                viewer = NULL;
124               
125
126                if(cluster.valid())
127                        cluster->shutdown();
128                if(extLink.valid())
129                extLink->shutdown();
130        }
131}
132
133void 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();
143                                dataIO->cluster->sendTO_OBJvaluesToSlaves(dataIO->calcViewMatrix());
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        };
160        traverse(node, nv);
161}
162
163void 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
194void* 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
200                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_  && dataSlots[i]->variableType ==  variableTyp_)
201                {
202                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot found at position " << i << std::endl;
203                        // Return pointer to the value
204                        return dataSlots[i];
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;
210        dataIO_slot* newSlot = new dataIO_slot();
211        newSlot->variableName = variableName_;
212        newSlot->variableType = variableTyp_;
213        newSlot->value = 0;
214        newSlot->sValue = "";
215        dataSlots.push_back( newSlot );
216        return dataSlots.back();
217}
218
219double 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
225                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_  && dataSlots[i]->variableType == osgVisual::dataIO_slot::DOUBLE )
226                {
227                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl;
228                        return dataSlots[i]->value;
229                }
230        }
231        return 0;
232}
233
234std::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
240                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_  && dataSlots[i]->variableType == osgVisual::dataIO_slot::STRING )
241                {
242                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl;
243                        return dataSlots[i]->sValue;
244                }
245        }
246        return "";
247}
248
249osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, std::string sValue_ )
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
256                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType ==  osgVisual::dataIO_slot::STRING)
257                {
258                        // Update value
259                        dataSlots[i]->sValue = sValue_;
260                        slotFound = true;
261                        return dataSlots[i];
262                }
263               
264        }
265
266        if (!slotFound)
267        {
268                // Slot does not exist -> add it to slot list
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_;
275                dataSlots.push_back( newSlot );
276                return dataSlots.back();
277        }
278
279        return NULL;
280}
281
282osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, double value_ )
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
289                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType ==  osgVisual::dataIO_slot::DOUBLE)
290                {
291                        // Update value
292                        //OSG_NOTIFY( osg::ALWAYS ) << "setSlotData: " << variableName_ << " - value: " << value_ << std::endl;
293                        dataSlots[i]->value = value_;
294                        slotFound = true;
295                        return dataSlots[i];
296                }       
297        }
298
299        if (!slotFound)
300        {
301                // Slot does not exist -> add it to slot list
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 = "";
308                dataSlots.push_back( newSlot );
309                return dataSlots.back();
310        }
311
312        return NULL;
313}
314
315osg::Matrixd visual_dataIO::calcViewMatrix()
316{
317        return viewer->getCameraManipulator()->getInverseMatrix();
318}
Note: See TracBrowser for help on using the repository browser.