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