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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
/**
* @file
* @author 2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
* @copyright Simplified BSD
*
* @cond
* This program is free software: you can redistribute it and/or modify
* it under the terms of the FreeBSD license as published by the FreeBSD
* project.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the FreeBSD license along with this
* program. If not, see <http://www.opensource.org/licenses/bsd-license>.
* @endcond
*/
#include "OSGInvoker.h"
#include "uscxml/URL.h"
#include "uscxml/UUID.h"
#include <glog/logging.h>
#include <osg/Shape>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/Depth>
#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif
#define OSG_SET_MATERIAL \
osg::ref_ptr<osg::Material> mat = getMaterial(element); \
if (mat) { \
osg::ref_ptr<osg::StateSet> nodeSS = geode->getOrCreateStateSet(); \
\
nodeSS->setAttribute(mat.get()); \
nodeSS->setMode( GL_BLEND, osg::StateAttribute::ON ); \
/* nodeSS->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); \
nodeSS->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON ); \
osg::Depth* depth = new osg::Depth; \
depth->setWriteMask( false ); \
nodeSS->setAttributeAndModes( depth, osg::StateAttribute::ON ); \
nodeSS->setMode( GL_LIGHTING, osg::StateAttribute::OFF );*/ \
}
#define OSG_SET_COLOR \
bool validColor = true; \
osg::Vec4 color = getColor(element, "color", validColor); \
if (validColor) \
drawable->setColor(color);
namespace uscxml {
#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool pluginConnect(pluma::Host& host) {
host.add( new OSGInvokerProvider() );
return true;
}
#endif
#define OSG_TAG_HANDLE(tagName, procFunc) \
} else if (boost::iequals(LOCALNAME(childs.item(i)), tagName) && \
validChildren.find(tagName) != validChildren.end()) { \
procFunc(childs.item(i));\
OSGInvoker::OSGInvoker() {
}
OSGInvoker::~OSGInvoker() {
};
boost::shared_ptr<InvokerImpl> OSGInvoker::create(InterpreterImpl* 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) {
if (boost::iequals(req.name, "intersect")) {
}
}
void OSGInvoker::cancel(const std::string sendId) {
}
void OSGInvoker::invoke(const InvokeRequest& req) {
tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
setupColors();
std::cout << req.dom;
// 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);
std::set<std::string> validChilds;
validChilds.insert("display");
// this is somewhat unfortunate, if content contains a single child, we will get that, otherwise its parent (<content>)
if (boost::iequals(LOCALNAME(req.dom), "display")) {
processChildren(validChilds, req.dom.getParentNode());
} else {
processChildren(validChilds, req.dom);
}
}
void OSGInvoker::setupColors() {
_colors["red"] = osg::Vec4(1.0, 0.0, 0.0, 1.0);
_colors["cyan"] = osg::Vec4(0.0, 1.0, 1.0, 1.0);
_colors["blue"] = osg::Vec4(0.0, 0.0, 1.0, 1.0);
_colors["darkblue"] = osg::Vec4(0.0, 0.0, 0.625, 1.0);
_colors["lightblue"] = osg::Vec4(0.675, 0.84375,0.89844,1.0);
_colors["purple"] = osg::Vec4(0.5, 0.0, 0.5, 1.0);
_colors["yellow"] = osg::Vec4(1.0, 1.0, 0.0, 1.0);
_colors["lime"] = osg::Vec4(0.0, 1.0, 0.0, 1.0);
_colors["magenta"] = osg::Vec4(1.0, 0.0, 1.0, 1.0);
_colors["white"] = osg::Vec4(1.0, 1.0, 1.0, 1.0);
_colors["silver"] = osg::Vec4(0.75, 0.75, 0.75, 1.0);
_colors["grey"] = osg::Vec4(0.5, 0.5, 0.5, 1.0);
_colors["gray"] = osg::Vec4(0.5, 0.5, 0.5, 1.0);
_colors["black"] = osg::Vec4(0.0, 0.0, 0.0, 1.0);
_colors["orange"] = osg::Vec4(1.0, 0.644, 0.0, 1.0);
_colors["brown"] = osg::Vec4(0.644, 0.164, 0.164, 1.0);
_colors["maroon"] = osg::Vec4(0.5, 0.0, 0.0, 1.0);
_colors["green"] = osg::Vec4(0.0, 0.5, 0.0, 1.0);
_colors["olive"] = osg::Vec4(0.5, 0.5, 0.0, 1.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::ref_ptr<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") : UUID::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);
bool hasBGColor;
osg::Vec4 bgColor = getColor(element, "bgcolor", hasBGColor);
if (hasBGColor) {
sceneView->getCamera()->setClearColor(bgColor);
} else {
sceneView->getCamera()->setClearColor(_colors["white"]);
}
std::set<std::string> validChilds;
validChilds.insert("camera");
validChilds.insert("translation");
validChilds.insert("rotation");
validChilds.insert("scale");
validChilds.insert("node");
validChilds.insert("sphere");
validChilds.insert("box");
validChilds.insert("capsule");
validChilds.insert("cone");
validChilds.insert("cylinder");
processChildren(validChilds, element);
}
void OSGInvoker::processCamera(const Arabica::DOM::Node<std::string>& element) {}
void OSGInvoker::updateCamera(osg::ref_ptr<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::ref_ptr<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");
validChilds.insert("sphere");
validChilds.insert("box");
validChilds.insert("capsule");
validChilds.insert("cone");
validChilds.insert("cylinder");
processChildren(validChilds, element);
}
void OSGInvoker::processRotation(const Arabica::DOM::Node<std::string>& element) {
assert(_nodes.find(element.getParentNode()) != _nodes.end());
osg::ref_ptr<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");
validChilds.insert("sphere");
validChilds.insert("box");
validChilds.insert("capsule");
validChilds.insert("cone");
validChilds.insert("cylinder");
processChildren(validChilds, element);
}
void OSGInvoker::updateRotation(osg::ref_ptr<osg::Node> node, Arabica::DOM::Events::Event<std::string>& event) {
osg::ref_ptr<osg::MatrixTransform> transform = static_cast<osg::MatrixTransform*>(node->asTransform());
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::ref_ptr<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");
validChilds.insert("sphere");
validChilds.insert("box");
validChilds.insert("capsule");
validChilds.insert("cone");
validChilds.insert("cylinder");
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::ref_ptr<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::processSphere(const Arabica::DOM::Node<std::string>& element) {
assert(_nodes.find(element.getParentNode()) != _nodes.end());
osg::ref_ptr<osg::Node> parent = _nodes[element.getParentNode()];
float radius = 1;
osg::Vec3 center(0,0,0);
if (HAS_ATTR(element, "radius")) {
radius = strTo<float>(ATTR(element, "radius"));
}
osg::ref_ptr<osg::Sphere> sphere = new osg::Sphere(center, radius);
osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(sphere);
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(drawable);
OSG_SET_COLOR;
OSG_SET_MATERIAL;
_nodes[element] = geode;
parent->asGroup()->addChild(geode);
}
void OSGInvoker::updateSphere(osg::ref_ptr<osg::Node> node, Arabica::DOM::Events::Event<std::string>& event) {
}
void OSGInvoker::processBox(const Arabica::DOM::Node<std::string>& element) {
assert(_nodes.find(element.getParentNode()) != _nodes.end());
osg::ref_ptr<osg::Node> parent = _nodes[element.getParentNode()];
float x = 1;
float y = 1;
float z = 1;
osg::Vec3 center(0,0,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::ref_ptr<osg::Box> box = new osg::Box(center, x, y, z);
osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(box);
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(drawable);
OSG_SET_COLOR;
OSG_SET_MATERIAL;
_nodes[element] = geode;
parent->asGroup()->addChild(geode);
}
void OSGInvoker::updateBox(osg::ref_ptr<osg::Node> node, Arabica::DOM::Events::Event<std::string>& event) {
}
void OSGInvoker::processCapsule(const Arabica::DOM::Node<std::string>& element) {
assert(_nodes.find(element.getParentNode()) != _nodes.end());
osg::ref_ptr<osg::Node> parent = _nodes[element.getParentNode()];
float radius = 1;
float height = 1;
osg::Vec3 center(0,0,0);
if (HAS_ATTR(element, "radius")) radius = strTo<float>(ATTR(element, "radius"));
if (HAS_ATTR(element, "height")) height = strTo<float>(ATTR(element, "height"));
osg::ref_ptr<osg::Capsule> capsule = new osg::Capsule(center, radius, height);
osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(capsule);
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(drawable);
OSG_SET_COLOR;
OSG_SET_MATERIAL;
_nodes[element] = geode;
parent->asGroup()->addChild(geode);
}
void OSGInvoker::updateCapsule(osg::ref_ptr<osg::Node> node, Arabica::DOM::Events::Event<std::string>& event) {
}
void OSGInvoker::processCone(const Arabica::DOM::Node<std::string>& element) {
assert(_nodes.find(element.getParentNode()) != _nodes.end());
osg::ref_ptr<osg::Node> parent = _nodes[element.getParentNode()];
float radius = 1;
float height = 1;
osg::Vec3 center(0,0,0);
if (HAS_ATTR(element, "radius")) radius = strTo<float>(ATTR(element, "radius"));
if (HAS_ATTR(element, "height")) height = strTo<float>(ATTR(element, "height"));
osg::ref_ptr<osg::Cone> cone = new osg::Cone(center, radius, height);
osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(cone);
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(drawable);
OSG_SET_COLOR;
OSG_SET_MATERIAL;
_nodes[element] = geode;
parent->asGroup()->addChild(geode);
}
void OSGInvoker::updateCone(osg::ref_ptr<osg::Node> node, Arabica::DOM::Events::Event<std::string>& event) {
}
void OSGInvoker::processCylinder(const Arabica::DOM::Node<std::string>& element) {
assert(_nodes.find(element.getParentNode()) != _nodes.end());
osg::ref_ptr<osg::Node> parent = _nodes[element.getParentNode()];
float radius = 1;
float height = 1;
osg::Vec3 center(0,0,0);
if (HAS_ATTR(element, "radius")) radius = strTo<float>(ATTR(element, "radius"));
if (HAS_ATTR(element, "height")) height = strTo<float>(ATTR(element, "height"));
osg::ref_ptr<osg::Cylinder> cylinder = new osg::Cylinder(center, radius, height);
osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(cylinder);
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(drawable);
OSG_SET_COLOR;
OSG_SET_MATERIAL;
_nodes[element] = geode;
parent->asGroup()->addChild(geode);
}
void OSGInvoker::updateCylinder(osg::ref_ptr<osg::Node> node, Arabica::DOM::Events::Event<std::string>& event) {
}
osg::Vec4 OSGInvoker::getColor(const Arabica::DOM::Node<std::string>& element, const std::string& attr, bool& valid) {
if (HAS_ATTR(element, attr)) {
std::string color = ATTR(element, attr);
// is this one of the predefined colors?
if (_colors.find(color) != _colors.end()) {
valid = true;
return _colors[color];
}
// otherwise try to parse as rgba values
int i;
osg::Vec4 colorVec = parseVec4(color, i);
if (i == 1) {
// only a single value was given, interpret as grey value
colorVec[1] = colorVec[2] = colorVec[0];
colorVec[3] = 1.0;
valid = true;
return colorVec;
}
if (i == 3) {
// three values were given, set opacity to max
colorVec[3] = 1.0;
valid = true;
return colorVec;
}
}
// return empty reference
valid = false;
return osg::Vec4();
}
osg::ref_ptr<osg::Material> OSGInvoker::getMaterial(const Arabica::DOM::Node<std::string>& element) {
osg::ref_ptr<osg::Material> nodeMat;
// material color
bool hasMatColor;
osg::Vec4 matColor = getColor(element, "materialcolor", hasMatColor);
if (hasMatColor) {
if (!nodeMat)
nodeMat = new osg::Material;
nodeMat->setDiffuse(osg::Material::FRONT, matColor);
nodeMat->setDiffuse(osg::Material::BACK, matColor);
}
// translucency
if (HAS_ATTR(element, "transparency")) {
std::string transparency = ATTR(element, "transparency");
float trans = strTo<float>(transparency);
if (!nodeMat)
nodeMat = new osg::Material;
nodeMat->setTransparency(osg::Material::FRONT, trans);
nodeMat->setTransparency(osg::Material::BACK, trans);
}
return nodeMat;
}
osg::Vec4 OSGInvoker::parseVec4(const std::string& coeffs, int& i) {
// otherwise try to parse as rgba values
std::string coeff;
std::stringstream coeffSS(coeffs);
osg::Vec4 vec;
i = 0;
while(std::getline(coeffSS, coeff, ',')) {
boost::trim(coeff);
if (coeff.length() == 0)
continue;
if (!isNumeric(coeff.c_str(), 10))
continue;
vec[i] = strTo<float>(coeff);
i++;
}
return vec;
}
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) {
OSG_TAG_HANDLE("node", processNode);
OSG_TAG_HANDLE("translation", processTranslation);
OSG_TAG_HANDLE("rotation", processRotation);
OSG_TAG_HANDLE("scale", processScale);
OSG_TAG_HANDLE("viewport", processViewport);
OSG_TAG_HANDLE("camera", processCamera);
OSG_TAG_HANDLE("display", processDisplay);
OSG_TAG_HANDLE("sphere", processSphere);
OSG_TAG_HANDLE("box", processBox);
OSG_TAG_HANDLE("cone", processCone);
OSG_TAG_HANDLE("capsule", processCapsule);
OSG_TAG_HANDLE("cylinder", processCylinder);
} 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;
}
}
|