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 <dataIO_extLinkVCL.h> |
---|
18 | |
---|
19 | #include <visual_dataIO.h> // include in.cpp to avoid circular inclusion (visual_dataIO <-> extLinkVCL) |
---|
20 | |
---|
21 | using namespace osgVisual; |
---|
22 | |
---|
23 | dataIO_extLinkVCL::dataIO_extLinkVCL(std::vector<dataIO_slot>& dataSlots_) : dataIO_extLink(dataSlots_) |
---|
24 | { |
---|
25 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL constructed" << std::endl; |
---|
26 | } |
---|
27 | |
---|
28 | dataIO_extLinkVCL::~dataIO_extLinkVCL(void) |
---|
29 | { |
---|
30 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL destroyed" << std::endl; |
---|
31 | } |
---|
32 | |
---|
33 | void dataIO_extLinkVCL::init() |
---|
34 | { |
---|
35 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL init()" << std::endl; |
---|
36 | |
---|
37 | VCLConfigFilename = "osgVisual.xml"; |
---|
38 | |
---|
39 | if ( osgDB::fileExists( VCLConfigFilename ) ) |
---|
40 | { |
---|
41 | CVCLIO::GetInstance().LoadProject(VCLConfigFilename.c_str()); |
---|
42 | |
---|
43 | parseVCLConfig(); |
---|
44 | bool error = false; |
---|
45 | |
---|
46 | if( !SAENGER1_POS_LAT.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER1_POS_LAT") ) |
---|
47 | error = true; |
---|
48 | if( !SAENGER1_POS_LON.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER1_POS_LON") ) |
---|
49 | error = true; |
---|
50 | if( !SAENGER1_POS_ALT.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER1_POS_ALT") ) |
---|
51 | error = true; |
---|
52 | if( !SAENGER1_ROT_X.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER1_ROT_X") ) |
---|
53 | error = true; |
---|
54 | if( !SAENGER1_ROT_Y.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER1_ROT_Y") ) |
---|
55 | error = true; |
---|
56 | if( !SAENGER1_ROT_Z.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER1_ROT_Z") ) |
---|
57 | error = true; |
---|
58 | |
---|
59 | if( !SAENGER2_POS_LAT.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER2_POS_LAT") ) |
---|
60 | error = true; |
---|
61 | if( !SAENGER2_POS_LON.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER2_POS_LON") ) |
---|
62 | error = true; |
---|
63 | if( !SAENGER2_POS_ALT.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER2_POS_ALT") ) |
---|
64 | error = true; |
---|
65 | if( !SAENGER2_ROT_X.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER2_ROT_X") ) |
---|
66 | error = true; |
---|
67 | if( !SAENGER2_ROT_Y.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER2_ROT_Y") ) |
---|
68 | error = true; |
---|
69 | if( !SAENGER2_ROT_Z.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER2_ROT_Z") ) |
---|
70 | error = true; |
---|
71 | |
---|
72 | if( !SAENGER_POS_LAT.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER_POS_LAT") ) |
---|
73 | error = true; |
---|
74 | if( !SAENGER_POS_LON.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER_POS_LON") ) |
---|
75 | error = true; |
---|
76 | if( !SAENGER_POS_ALT.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER_POS_ALT") ) |
---|
77 | error = true; |
---|
78 | if( !SAENGER_ROT_X.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER_ROT_X") ) |
---|
79 | error = true; |
---|
80 | if( !SAENGER_ROT_Y.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER_ROT_Y") ) |
---|
81 | error = true; |
---|
82 | if( !SAENGER_ROT_Z.Attach("VISENGINE_CAMERA_CHANNEL", "SAENGER_ROT_Z") ) |
---|
83 | error = true; |
---|
84 | |
---|
85 | if (error) |
---|
86 | OSG_NOTIFY( osg::FATAL ) << "ERROR: An error occured while attaching VCL." << std::endl; |
---|
87 | initialized = true; |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | OSG_NOTIFY( osg::FATAL ) << "ERROR: Could not find VCL Configuration file " << VCLConfigFilename << std::endl; |
---|
92 | exit(-1); |
---|
93 | } |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | void dataIO_extLinkVCL::shutdown() |
---|
98 | { |
---|
99 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL shutdown()" << std::endl; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | bool dataIO_extLinkVCL::readTO_OBJvalues() |
---|
104 | { |
---|
105 | OSG_NOTIFY( osg::INFO ) << "extLinkVCL readTO_OBJvalues()" << std::endl; |
---|
106 | |
---|
107 | // perform external data exchange |
---|
108 | CVCLIO::GetInstance().DoDataExchange(); |
---|
109 | |
---|
110 | // Copy data from VCL variables to slot variables: |
---|
111 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER1_POS_LAT", osgVisual::dataIO_slot::TO_OBJ, SAENGER1_POS_LAT.GetValue() ); |
---|
112 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER1_POS_LON", osgVisual::dataIO_slot::TO_OBJ, SAENGER1_POS_LON.GetValue() ); |
---|
113 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER1_POS_ALT", osgVisual::dataIO_slot::TO_OBJ, -SAENGER1_POS_ALT.GetValue() ); // Negatives Vorzeichen da Z-Koordinaten anders herum. |
---|
114 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER1_ROT_X", osgVisual::dataIO_slot::TO_OBJ, SAENGER1_ROT_X.GetValue() ); |
---|
115 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER1_ROT_Y", osgVisual::dataIO_slot::TO_OBJ, SAENGER1_ROT_Y.GetValue() ); |
---|
116 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER1_ROT_Z", osgVisual::dataIO_slot::TO_OBJ, SAENGER1_ROT_Z.GetValue() ); |
---|
117 | |
---|
118 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER2_POS_LAT", osgVisual::dataIO_slot::TO_OBJ, SAENGER2_POS_LAT.GetValue() ); |
---|
119 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER2_POS_LON", osgVisual::dataIO_slot::TO_OBJ, SAENGER2_POS_LON.GetValue() ); |
---|
120 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER2_POS_ALT", osgVisual::dataIO_slot::TO_OBJ, -SAENGER2_POS_ALT.GetValue() ); // Negatives Vorzeichen da Z-Koordinaten anders herum. |
---|
121 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER2_ROT_X", osgVisual::dataIO_slot::TO_OBJ, SAENGER2_ROT_X.GetValue() ); |
---|
122 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER2_ROT_Y", osgVisual::dataIO_slot::TO_OBJ, SAENGER2_ROT_Y.GetValue() ); |
---|
123 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER2_ROT_Z", osgVisual::dataIO_slot::TO_OBJ, SAENGER2_ROT_Z.GetValue() ); |
---|
124 | |
---|
125 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER_POS_LAT", osgVisual::dataIO_slot::TO_OBJ, SAENGER_POS_LAT.GetValue() ); |
---|
126 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER_POS_LON", osgVisual::dataIO_slot::TO_OBJ, SAENGER_POS_LON.GetValue() ); |
---|
127 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER_POS_ALT", osgVisual::dataIO_slot::TO_OBJ, -SAENGER_POS_ALT.GetValue() ); // Negatives Vorzeichen da Z-Koordinaten anders herum. |
---|
128 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER_ROT_X", osgVisual::dataIO_slot::TO_OBJ, SAENGER_ROT_X.GetValue() ); |
---|
129 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER_ROT_Y", osgVisual::dataIO_slot::TO_OBJ, SAENGER_ROT_Y.GetValue() ); |
---|
130 | osgVisual::visual_dataIO::getInstance()->setSlotData( "SAENGER_ROT_Z", osgVisual::dataIO_slot::TO_OBJ, SAENGER_ROT_Z.GetValue() ); |
---|
131 | |
---|
132 | //OSG_NOTIFY( osg::ALWAYS ) << "LAT: " << osgVisual::visual_dataIO::getInstance()->getSlotDataAsDouble("SAENGER1_POS_LAT", osgVisual::dataIO_slot::TO_OBJ ) << std::endl; |
---|
133 | return true; |
---|
134 | } |
---|
135 | |
---|
136 | bool dataIO_extLinkVCL::writebackFROM_OBJvalues() |
---|
137 | { |
---|
138 | OSG_NOTIFY( osg::INFO ) << "extLinkVCL writebackFROM_OBJvalues()" << std::endl; |
---|
139 | |
---|
140 | // Copy slot data to variables |
---|
141 | // Nothing to do |
---|
142 | // Example: MY_CHANNEL.setValue( myValue ); |
---|
143 | |
---|
144 | /* In VCL no writeback transaction is done, |
---|
145 | it is postponed until the next frame beginning, |
---|
146 | where TO_OBJ exchange calls CVCLIO::GetInstance().DoDataExchange();. |
---|
147 | */ |
---|
148 | return true; |
---|
149 | } |
---|
150 | |
---|
151 | bool dataIO_extLinkVCL::parseVCLConfig() |
---|
152 | { |
---|
153 | configFileValid = false; |
---|
154 | xmlDoc *doc = NULL; |
---|
155 | xmlNode *root_element = NULL; |
---|
156 | |
---|
157 | doc = xmlReadFile(VCLConfigFilename.c_str(), NULL, 0); |
---|
158 | if (doc == NULL) |
---|
159 | { |
---|
160 | configFileValid = false; |
---|
161 | std::cout << "error: could not parse file" << VCLConfigFilename; |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | // Get the root element node |
---|
166 | root_element = xmlDocGetRootElement(doc); |
---|
167 | |
---|
168 | checkXMLNode(root_element); |
---|
169 | |
---|
170 | // free the document |
---|
171 | xmlFreeDoc(doc);; |
---|
172 | } |
---|
173 | // Free the global variables that may have been allocated by the parser. |
---|
174 | xmlCleanupParser(); |
---|
175 | |
---|
176 | if(!configFileValid) |
---|
177 | OSG_ALWAYS << "ERROR: XML file seems not to be a valid VCL configuration file!" << std::endl; |
---|
178 | |
---|
179 | return true; |
---|
180 | } |
---|
181 | |
---|
182 | void dataIO_extLinkVCL::checkXMLNode(xmlNode * a_node) |
---|
183 | { |
---|
184 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
185 | { |
---|
186 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
187 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "CONFIGURATION") |
---|
188 | { |
---|
189 | //OSG_DEBUG << "XML node CONFIGURATION found" << std::endl; |
---|
190 | configFileValid = true; |
---|
191 | } |
---|
192 | |
---|
193 | if (cur_node->type == XML_ELEMENT_NODE && node_name == "CHANNEL") |
---|
194 | { |
---|
195 | //OSG_DEBUG << "XML node CHANNEL found" << std::endl; |
---|
196 | |
---|
197 | // Extract channel infos like name, direction and extract channels |
---|
198 | std::string name; |
---|
199 | dataIO_slot::dataDirection direction; |
---|
200 | xmlAttr *attr = cur_node->properties; |
---|
201 | while ( attr ) |
---|
202 | { |
---|
203 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
204 | if( attr_name == "name" ) |
---|
205 | name = reinterpret_cast<const char*>(attr->children->content); |
---|
206 | if( attr_name == "multicast_in_group" ) |
---|
207 | direction = dataIO_slot::TO_OBJ; |
---|
208 | if( attr_name == "multicast_out_group" ) |
---|
209 | direction = dataIO_slot::FROM_OBJ; |
---|
210 | //std::cout << "Attribute name: " << attr->name << " value: " << attr->children->content << std::endl; |
---|
211 | attr = attr->next; |
---|
212 | } |
---|
213 | addChannels(cur_node->children, name, direction ); |
---|
214 | |
---|
215 | //OSG_DEBUG << "node type=Element, name:" << cur_node->name << std::endl; |
---|
216 | //OSG_DEBUG << "Processing children at " << cur_node->children << std::endl; |
---|
217 | } // IF(CHANNEL) END |
---|
218 | |
---|
219 | // Iterate to the next nodes to find channels. |
---|
220 | checkXMLNode(cur_node->children); |
---|
221 | } // FOR END |
---|
222 | } |
---|
223 | |
---|
224 | void dataIO_extLinkVCL::addChannels(xmlNode * a_node, std::string channelName_, dataIO_slot::dataDirection direction_ ) |
---|
225 | { |
---|
226 | if(!a_node) |
---|
227 | return; |
---|
228 | |
---|
229 | OSG_ALWAYS << "Processing channels for channel " << channelName_ << " with the direction " << direction_ << std::endl; |
---|
230 | |
---|
231 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
232 | { |
---|
233 | if (cur_node->type != XML_ELEMENT_NODE) // only emelent nodes are relevant entries. otherwise skip this iteration. VCL files only contain text nodes in comments or as untrimmed element nodes. |
---|
234 | continue; |
---|
235 | |
---|
236 | // Extract ENTRY - name (from the Channel <ENTRY> in the XML file |
---|
237 | std::string entryName; |
---|
238 | xmlAttr *attr = cur_node->properties; |
---|
239 | while ( attr ) |
---|
240 | { |
---|
241 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
242 | if( attr_name == "name" ) |
---|
243 | entryName = reinterpret_cast<const char*>(attr->children->content); |
---|
244 | //OSG_DEBUG << "Attribute name: " << attr->name << " value: " << attr->children->content << std::endl; |
---|
245 | attr = attr->next; |
---|
246 | } |
---|
247 | |
---|
248 | // Store VCL variable |
---|
249 | CVCLVariable<double>* tmp = new CVCLVariable<double>; |
---|
250 | extLinkChannels.push_back(tmp); |
---|
251 | |
---|
252 | // Attach VCL variable to channel: |
---|
253 | //OSG_DEBUG << "attaching.... name: " << channelName_ << ", entryName: " << entryName << std::endl; |
---|
254 | if( !tmp->Attach(channelName_.c_str(), entryName.c_str() ) ) |
---|
255 | OSG_ALWAYS << "ERROR - dataIO_extLinkVCL::addChannels(): unable to attach VCL variable entryName: " << entryName << " to channel: " << channelName_ << std::endl; |
---|
256 | |
---|
257 | // Set SLOT data and store SLOT pointer |
---|
258 | extLinkSlots.push_back( osgVisual::visual_dataIO::getInstance()->setSlotData( entryName, direction_, 0 ) ); |
---|
259 | } // FOR each ENTRY END |
---|
260 | } |
---|