summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.cpp
blob: 08fc1f3cb5242e871c1593564ace9ebe0a598a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "OSGInvoker.h"
#include "uscxml/URL.h"
#include <glog/logging.h>

#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif

namespace uscxml {
  
#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool connect(pluma::Host& host) {
	host.add( new OSGInvokerProvider() );
	return true;
}
#endif
  
OSGInvoker::OSGInvoker() {
}

OSGInvoker::~OSGInvoker() {
};

boost::shared_ptr<IOProcessorImpl> OSGInvoker::create(Interpreter* interpreter) {
	boost::shared_ptr<OSGInvoker> invoker = boost::shared_ptr<OSGInvoker> (new OSGInvoker());
	invoker->_interpreter = interpreter;
	return invoker;
}

Data OSGInvoker::getDataModelVariables() {
	Data data;
	return data;
}

void OSGInvoker::send(const SendRequest& req) {
}

void OSGInvoker::cancel(const std::string sendId) {
}

void OSGInvoker::sendToParent(const SendRequest& req) {
}

void OSGInvoker::invoke(const InvokeRequest& req) {
  tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
  
  // register default event handlers
  Arabica::DOM::Events::EventTarget<std::string> evTarget = Arabica::DOM::Events::EventTarget<std::string>(req.dom);
  evTarget.addEventListener("DOMSubtreeModified", *this, false);
  evTarget.addEventListener("DOMNodeInserted", *this, false);
  evTarget.addEventListener("DOMNodeRemoved", *this, false);
  evTarget.addEventListener("DOMAttrModified", *this, false);
  
  Arabica::XPath::NodeSet<std::string> content = Interpreter::filterChildElements("content", req.dom);

  std::set<std::string> validChilds;
  validChilds.insert("display");
  processChildren(validChilds, content[0]);
}

void OSGInvoker::runOnMainThread() {
  _displays_t::iterator dispIter = _displays.begin();
  if (_mutex.try_lock()) {
    while(dispIter != _displays.end()) {
      dispIter->second->osgViewer::ViewerBase::frame();
      dispIter++;
    }
    _mutex.unlock();
  }
}

void OSGInvoker::handleEvent(Arabica::DOM::Events::Event<std::string>& event) {
//  std::cout << "Handling Event!" << std::endl;
  Arabica::DOM::Node<std::string> node(event.getTarget());
  if (_nodes.find(node) != _nodes.end()) {
    osg::Node* osgNode = _nodes[node];
    if (false) {
    } else if (boost::iequals(LOCALNAME(node), "rotation")) {
      updateRotation(osgNode, event);
    }
  }
}

void OSGInvoker::processDisplay(const Arabica::DOM::Node<std::string>& element) {
//  std::cout << element << std::endl;

  if (_displays.find(element) == _displays.end()) {
    
    int screenId = 0;
    unsigned int actualX = 0;
    unsigned int actualY = 0;
    unsigned int actualWidth = 0;
    unsigned int actualHeight = 0;
    getViewport(element, actualX, actualY, actualWidth, actualHeight, screenId);
    
    CompositeDisplay* compDisp = new CompositeDisplay(actualX, actualY, actualWidth, actualHeight, screenId);
    _displays[element] = compDisp;
    
    std::set<std::string> validChilds;
    validChilds.insert("viewport");
    processChildren(validChilds, element);
  }
}

void OSGInvoker::processViewport(const Arabica::DOM::Node<std::string>& element) {
  if (_displays.find(element.getParentNode()) == _displays.end())
    return;

  CompositeDisplay* compDisp = _displays[element.getParentNode()];
  osgViewer::View* sceneView = new osgViewer::View();
  _views[element] = sceneView;

  osg::Group* group = new osg::Group();
  _nodes[element] = group;
  sceneView->setSceneData(group);
  
  std::string name = (HAS_ATTR(element, "id") ? ATTR(element, "id") : Interpreter::getUUID());

  unsigned int actualX = 0;
  unsigned int actualY = 0;
  unsigned int actualWidth = 0;
  unsigned int actualHeight = 0;
  getViewport(element, actualX, actualY, actualWidth, actualHeight, compDisp);

  osg::Viewport* viewPort = new osg::Viewport(actualX, actualY, actualWidth, actualHeight);
  compDisp->addView(name, viewPort, sceneView);
  
  std::set<std::string> validChilds;
  validChilds.insert("camera");
  validChilds.insert("translation");
  validChilds.insert("rotation");
  validChilds.insert("scale");
  validChilds.insert("node");
  processChildren(validChilds, element);  
}

void OSGInvoker::processCamera(const Arabica::DOM::Node<std::string>& element) {}
void OSGInvoker::updateCamera(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event) {}

void OSGInvoker::processTranslation(const Arabica::DOM::Node<std::string>& element) {
  assert(_nodes.find(element.getParentNode()) != _nodes.end());
  osg::Node* node = _nodes[element.getParentNode()];

  double x = 0, y = 0, z = 0;
  if (HAS_ATTR(element, "x"))
    x = strTo<float>(ATTR(element, "x"));
  if (HAS_ATTR(element, "y"))
    y = strTo<float>(ATTR(element, "y"));
  if (HAS_ATTR(element, "z"))
    z = strTo<float>(ATTR(element, "z"));
  
  osg::Matrix translate;
  translate.makeTranslate(x, y, z);

  osg::MatrixTransform* transform = new osg::MatrixTransform();
  transform->setMatrix(translate);
  node->asGroup()->addChild(transform);
  _nodes[element] = transform;

  std::set<std::string> validChilds;
  validChilds.insert("translation");
  validChilds.insert("rotation");
  validChilds.insert("scale");
  validChilds.insert("node");
  processChildren(validChilds, element);
}

void OSGInvoker::processRotation(const Arabica::DOM::Node<std::string>& element) {
  assert(_nodes.find(element.getParentNode()) != _nodes.end());
  osg::Node* node = _nodes[element.getParentNode()];

  osg::Matrix rotation = rotationFromElement(element);
  osg::MatrixTransform* transform = new osg::MatrixTransform();
  transform->setMatrix(rotation);
  node->asGroup()->addChild(transform);
  _nodes[element] = transform;

  std::set<std::string> validChilds;
  validChilds.insert("translation");
  validChilds.insert("rotation");
  validChilds.insert("scale");
  validChilds.insert("node");
  processChildren(validChilds, element);
}

void OSGInvoker::updateRotation(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event) {
  osg::MatrixTransform* transform = static_cast<osg::MatrixTransform*>(node);
  if (false) {
  } else if (boost::iequals(event.getType(), "DOMAttrModified")) {
    osg::Matrix rotation = rotationFromElement(Arabica::DOM::Node<std::string>(event.getTarget()));
    transform->setMatrix(rotation);
  }
}

osg::Matrix OSGInvoker::rotationFromElement(const Arabica::DOM::Node<std::string>& element) {
  double pitch = 0, roll = 0, yaw = 0;
  if (HAS_ATTR(element, "pitch")) {
    NumAttr pitchAttr = NumAttr(ATTR(element, "pitch"));
    if (boost::iequals(pitchAttr.unit, "deg")) {
      pitch = osg::DegreesToRadians(strTo<float>(pitchAttr.value));
    } else if (boost::iequals(pitchAttr.unit, "%")) {
      pitch = osg::DegreesToRadians((strTo<float>(pitchAttr.value) * 360) / 100);
    } else {
      pitch = strTo<float>(pitchAttr.value);
    }
  }
  if (HAS_ATTR(element, "roll")) {
    NumAttr rollAttr = NumAttr(ATTR(element, "roll"));
    if (boost::iequals(rollAttr.unit, "deg")) {
      roll = osg::DegreesToRadians(strTo<float>(rollAttr.value));
    } else if (boost::iequals(rollAttr.unit, "%")) {
      roll = osg::DegreesToRadians((strTo<float>(rollAttr.value) * 360) / 100);
    } else {
      roll = strTo<float>(rollAttr.value);
    }
  }
  if (HAS_ATTR(element, "yaw")) {
    NumAttr yawAttr = NumAttr(ATTR(element, "yaw"));
    if (boost::iequals(yawAttr.unit, "deg")) {
      yaw = osg::DegreesToRadians(strTo<float>(yawAttr.value));
    } else if (boost::iequals(yawAttr.unit, "%")) {
      yaw = osg::DegreesToRadians((strTo<float>(yawAttr.value) * 360) / 100);
    } else {
      yaw = strTo<float>(yawAttr.value);
    }
  }
  
  osg::Matrix rotation;
  rotation.makeRotate(roll, osg::Vec3(0,1,0), // roll
                      pitch, osg::Vec3(1,0,0) , // pitch
                      yaw, osg::Vec3(0,0,1) ); // heading

  return rotation;
}

void OSGInvoker::processScale(const Arabica::DOM::Node<std::string>& element) {
  assert(_nodes.find(element.getParentNode()) != _nodes.end());
  osg::Node* node = _nodes[element.getParentNode()];
  
  double x = 1, y = 1, z = 1;
  if (HAS_ATTR(element, "x"))
    x = strTo<float>(ATTR(element, "x"));
  if (HAS_ATTR(element, "y"))
    y = strTo<float>(ATTR(element, "y"));
  if (HAS_ATTR(element, "z"))
    z = strTo<float>(ATTR(element, "z"));
  
  osg::Matrix scale;
  scale.makeScale(x, y, z);
  
  osg::MatrixTransform* transform = new osg::MatrixTransform();
  transform->setMatrix(scale);
  node->asGroup()->addChild(transform);
  _nodes[element] = transform;

  std::set<std::string> validChilds;
  validChilds.insert("translation");
  validChilds.insert("rotation");
  validChilds.insert("scale");
  validChilds.insert("node");
  processChildren(validChilds, element);
}

void OSGInvoker::processNode(const Arabica::DOM::Node<std::string>& element) {
  _nodes_t::iterator nodeIter = _nodes.find(element.getParentNode());
  assert(nodeIter != _nodes.end());
  
  assert(_nodes.find(element.getParentNode()) != _nodes.end());
  osg::Node* parent = _nodes[element.getParentNode()];

  std::string filename;
  if (HAS_ATTR(element, "src")) {
    filename = ATTR(element, "src");
    
    if (filename.length() > 0) {
      std::string extension;
      size_t extensionStart = filename.find_last_of(".");
      if (extensionStart != std::string::npos) {
        extension = filename.substr(extensionStart);
      }
      
      URL srcURI(filename);
      if (!srcURI.toAbsolute(_interpreter->getBaseURI())) {
        LOG(ERROR) << "invoke element has relative src URI with no baseURI set.";
        return;
      }
      filename = srcURI.asLocalFile(extension);
      osg::ref_ptr<osg::Node> model = osgDB::readNodeFile(filename);
      if (model.get())
        parent->asGroup()->addChild(model);

    }
  }
}

void OSGInvoker::processChildren(const std::set<std::string>& validChildren, const Arabica::DOM::Node<std::string>& element) {
  Arabica::DOM::NodeList<std::string> childs = element.getChildNodes();
  for (int i = 0; i < childs.getLength(); ++i) {
    if (childs.item(i).getNodeType() != Arabica::DOM::Node_base::ELEMENT_NODE)
      continue;
    if (false) {
    } else if (boost::iequals(LOCALNAME(childs.item(i)), "node") &&
               validChildren.find("node") != validChildren.end()) {
      processNode(childs.item(i));
    } else if (boost::iequals(LOCALNAME(childs.item(i)), "translation") &&
               validChildren.find("translation") != validChildren.end()) {
      processTranslation(childs.item(i));
    } else if (boost::iequals(LOCALNAME(childs.item(i)), "rotation") &&
               validChildren.find("rotation") != validChildren.end()) {
      processRotation(childs.item(i));
    } else if (boost::iequals(LOCALNAME(childs.item(i)), "scale") &&
               validChildren.find("scale") != validChildren.end()) {
      processScale(childs.item(i));
    } else if (boost::iequals(LOCALNAME(childs.item(i)), "viewport") &&
               validChildren.find("viewport") != validChildren.end()) {
      processViewport(childs.item(i));
    } else if (boost::iequals(LOCALNAME(childs.item(i)), "camera") &&
               validChildren.find("camera") != validChildren.end()) {
      processCamera(childs.item(i));
    } else if (boost::iequals(LOCALNAME(childs.item(i)), "display") &&
               validChildren.find("display") != validChildren.end()) {
      processDisplay(childs.item(i));
    } else {
      LOG(INFO) << "Unknown XML element " << TAGNAME(childs.item(i));
    }
  }
}

void OSGInvoker::getViewport(const Arabica::DOM::Node<std::string>& element,
                             unsigned int& x,
                             unsigned int& y,
                             unsigned int& width,
                             unsigned int& height,
                             CompositeDisplay* display) {
  getViewport(element, x, y, width, height, display->getWidth(), display->getHeight());

}

void OSGInvoker::getViewport(const Arabica::DOM::Node<std::string>& element,
                             unsigned int& x,
                             unsigned int& y,
                             unsigned int& width,
                             unsigned int& height,
                             int& screenId)
{
  
  screenId = (HAS_ATTR(element, "screenId") ? strTo<int>(ATTR(element, "screenId")) : 0);
  
  unsigned int fullWidth = 0;
  unsigned int fullHeight = 0;
  CompositeDisplay::getResolution(fullWidth, fullHeight, screenId);
  getViewport(element, x, y, width, height, fullWidth, fullHeight);
}

void OSGInvoker::getViewport(const Arabica::DOM::Node<std::string>& element,
                             unsigned int& x,
                             unsigned int& y,
                             unsigned int& width,
                             unsigned int& height,
                             unsigned int fullWidth,
                             unsigned int fullHeight)
{
  if (HAS_ATTR(element, "x")) {
    NumAttr xAttr = NumAttr(ATTR(element, "x"));
    x = strTo<float>(xAttr.value);
    if (boost::iequals(xAttr.unit, "%"))
      x = (x * fullWidth) / 100;
  }
  if (HAS_ATTR(element, "y")) {
    NumAttr yAttr = NumAttr(ATTR(element, "y"));
    y = strTo<float>(yAttr.value);
    if (boost::iequals(yAttr.unit, "%"))
      y = (y * fullHeight) / 100;
  }
  if (HAS_ATTR(element, "width")) {
    NumAttr widthAttr = NumAttr(ATTR(element, "width"));
    width = strTo<float>(widthAttr.value);
    if (boost::iequals(widthAttr.unit, "%"))
      width = (width * fullWidth) / 100;
  }
  if (HAS_ATTR(element, "height")) {
    NumAttr heightAttr = NumAttr(ATTR(element, "height"));
    height = strTo<float>(heightAttr.value);
    if (boost::iequals(heightAttr.unit, "%"))
      height = (height * fullHeight) / 100;
  }
}

osgViewer::View* OSGInvoker::getView(const Arabica::DOM::Node<std::string>& element) {
  Arabica::DOM::Node<std::string> curr = element;
  while(curr && !boost::iequals(LOCALNAME(curr), "viewport")) {
    curr = curr.getParentNode();
  }
  if (curr && _views.find(curr) != _views.end())
    return _views[curr];
  return NULL;
}

  
}