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

Last change on this file since 31 was 31, checked in by Torben Dannhauer, 14 years ago

Adding first version of osgVisual!!

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