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 | |
---|
45 | initialized = true; |
---|
46 | } |
---|
47 | else |
---|
48 | { |
---|
49 | OSG_NOTIFY( osg::FATAL ) << "ERROR: Could not find VCL Configuration file " << VCLConfigFilename << std::endl; |
---|
50 | exit(-1); |
---|
51 | } |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | void dataIO_extLinkVCL::shutdown() |
---|
56 | { |
---|
57 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL shutdown()" << std::endl; |
---|
58 | } |
---|
59 | |
---|
60 | bool dataIO_extLinkVCL::readTO_OBJvalues() |
---|
61 | { |
---|
62 | OSG_NOTIFY( osg::INFO ) << "extLinkVCL readTO_OBJvalues()" << std::endl; |
---|
63 | |
---|
64 | // perform external data exchange |
---|
65 | CVCLIO::GetInstance().DoDataExchange(); |
---|
66 | |
---|
67 | // read TO_OBJ values from VCL |
---|
68 | for(unsigned int i=0;i<extLinkChannels.size();i++) |
---|
69 | { |
---|
70 | if(extLinkSlots[i]->getdataDirection() == osgVisual::dataIO_slot::TO_OBJ) |
---|
71 | { |
---|
72 | //Copy data from VCL to slot. IMPORTANT: Due to VCL's string incapability only double slots are filled. |
---|
73 | osgVisual::visual_dataIO::getInstance()->setSlotData( extLinkSlots[i]->getVariableName(), osgVisual::dataIO_slot::TO_OBJ, extLinkChannels[i]->GetValue() ); |
---|
74 | } // IF (TO_OBJ) END |
---|
75 | } |
---|
76 | |
---|
77 | return true; |
---|
78 | } |
---|
79 | |
---|
80 | bool dataIO_extLinkVCL::writebackFROM_OBJvalues() |
---|
81 | { |
---|
82 | OSG_NOTIFY( osg::INFO ) << "extLinkVCL writebackFROM_OBJvalues()" << std::endl; |
---|
83 | |
---|
84 | // write FROM_OBJ values into VCL |
---|
85 | for(unsigned int i=0;i<extLinkChannels.size();i++) |
---|
86 | { |
---|
87 | if(extLinkSlots[i]->getdataDirection() == osgVisual::dataIO_slot::FROM_OBJ && extLinkSlots[i]->getvarType() == osgVisual::dataIO_slot::DOUBLE) |
---|
88 | { |
---|
89 | //Copy data from slot to VCL. IMPORTANT: Due to VCL's string incapability only double slots are filled. |
---|
90 | extLinkChannels[i]->SetValue( osgVisual::visual_dataIO::getInstance()->getSlotDataAsDouble( extLinkSlots[i]->getVariableName(), osgVisual::dataIO_slot::FROM_OBJ ) ); |
---|
91 | } // IF (FROM_OBJ) END |
---|
92 | } |
---|
93 | |
---|
94 | /* In VCL no VCL dataexchange is performed, |
---|
95 | it is postponed until the next frame beginning, |
---|
96 | where TO_OBJ exchange calls CVCLIO::GetInstance().DoDataExchange();. |
---|
97 | */ |
---|
98 | return true; |
---|
99 | } |
---|
100 | |
---|
101 | bool dataIO_extLinkVCL::parseVCLConfig() |
---|
102 | { |
---|
103 | configFileValid = false; |
---|
104 | xmlDoc *doc = NULL; |
---|
105 | xmlNode *root_element = NULL; |
---|
106 | |
---|
107 | doc = xmlReadFile(VCLConfigFilename.c_str(), NULL, 0); |
---|
108 | if (doc == NULL) |
---|
109 | { |
---|
110 | configFileValid = false; |
---|
111 | std::cout << "error: could not parse file" << VCLConfigFilename; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | // Get the root element node |
---|
116 | root_element = xmlDocGetRootElement(doc); |
---|
117 | |
---|
118 | // Parse the XML document. |
---|
119 | checkXMLNode(root_element); |
---|
120 | |
---|
121 | // free the document |
---|
122 | xmlFreeDoc(doc);; |
---|
123 | } |
---|
124 | // Free the global variables that may have been allocated by the parser. |
---|
125 | xmlCleanupParser(); |
---|
126 | |
---|
127 | if(!configFileValid) |
---|
128 | OSG_ALWAYS << "ERROR: XML file seems not to be a valid VCL configuration file!" << std::endl; |
---|
129 | |
---|
130 | return true; |
---|
131 | } |
---|
132 | |
---|
133 | void dataIO_extLinkVCL::checkXMLNode(xmlNode * a_node) |
---|
134 | { |
---|
135 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
136 | { |
---|
137 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
138 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "CONFIGURATION") |
---|
139 | { |
---|
140 | //OSG_DEBUG << "XML node CONFIGURATION found" << std::endl; |
---|
141 | configFileValid = true; |
---|
142 | } |
---|
143 | |
---|
144 | if (cur_node->type == XML_ELEMENT_NODE && node_name == "CHANNEL") |
---|
145 | { |
---|
146 | //OSG_DEBUG << "XML node CHANNEL found" << std::endl; |
---|
147 | |
---|
148 | // Extract channel infos like name, direction and extract channels |
---|
149 | std::string name; |
---|
150 | dataIO_slot::dataDirection direction; |
---|
151 | xmlAttr *attr = cur_node->properties; |
---|
152 | while ( attr ) |
---|
153 | { |
---|
154 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
155 | if( attr_name == "name" ) |
---|
156 | name = reinterpret_cast<const char*>(attr->children->content); |
---|
157 | if( attr_name == "multicast_in_group" ) |
---|
158 | direction = dataIO_slot::TO_OBJ; |
---|
159 | if( attr_name == "multicast_out_group" ) |
---|
160 | direction = dataIO_slot::FROM_OBJ; |
---|
161 | //std::cout << "Attribute name: " << attr->name << " value: " << attr->children->content << std::endl; |
---|
162 | attr = attr->next; |
---|
163 | } |
---|
164 | addChannels(cur_node->children, name, direction ); |
---|
165 | |
---|
166 | //OSG_DEBUG << "node type=Element, name:" << cur_node->name << std::endl; |
---|
167 | //OSG_DEBUG << "Processing children at " << cur_node->children << std::endl; |
---|
168 | } // IF(CHANNEL) END |
---|
169 | |
---|
170 | // Iterate to the next nodes to find channels. |
---|
171 | checkXMLNode(cur_node->children); |
---|
172 | } // FOR END |
---|
173 | } |
---|
174 | |
---|
175 | void dataIO_extLinkVCL::addChannels(xmlNode * a_node, std::string channelName_, dataIO_slot::dataDirection direction_ ) |
---|
176 | { |
---|
177 | if(!a_node) |
---|
178 | return; |
---|
179 | |
---|
180 | OSG_ALWAYS << "dataIO_extL) : Processing entries for channel " << channelName_ << " with the direction " << direction_ << std::endl; |
---|
181 | |
---|
182 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
183 | { |
---|
184 | 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. |
---|
185 | continue; |
---|
186 | |
---|
187 | // Extract ENTRY - name (from the Channel <ENTRY> in the XML file |
---|
188 | std::string entryName; |
---|
189 | xmlAttr *attr = cur_node->properties; |
---|
190 | while ( attr ) |
---|
191 | { |
---|
192 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
193 | if( attr_name == "name" ) |
---|
194 | entryName = reinterpret_cast<const char*>(attr->children->content); |
---|
195 | //OSG_DEBUG << "Attribute name: " << attr->name << " value: " << attr->children->content << std::endl; |
---|
196 | attr = attr->next; |
---|
197 | } |
---|
198 | |
---|
199 | // Store VCL variable |
---|
200 | CVCLVariable<double>* tmp = new CVCLVariable<double>; |
---|
201 | extLinkChannels.push_back(tmp); |
---|
202 | |
---|
203 | // Attach VCL variable to channel: |
---|
204 | //OSG_DEBUG << "attaching.... name: " << channelName_ << ", entryName: " << entryName << std::endl; |
---|
205 | if( !tmp->Attach(channelName_.c_str(), entryName.c_str() ) ) |
---|
206 | OSG_ALWAYS << "ERROR - dataIO_extLinkVCL::addChannels(): unable to attach VCL variable entryName: " << entryName << " to channel: " << channelName_ << std::endl; |
---|
207 | |
---|
208 | // Set SLOT data and store SLOT pointer |
---|
209 | osgVisual::dataIO_slot* tmpSlot = osgVisual::visual_dataIO::getInstance()->setSlotData( entryName, direction_, 0 ); |
---|
210 | extLinkSlots.push_back( tmpSlot ); |
---|
211 | } // FOR each ENTRY END |
---|
212 | } |
---|