VisualizationPolygon.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <RobotAPI/interface/ArViz/Elements.h>
6
7#include "ElementVisualizer.h"
8#include <Inventor/nodes/SoCoordinate3.h>
9#include <Inventor/nodes/SoDrawStyle.h>
10#include <Inventor/nodes/SoFaceSet.h>
11#include <Inventor/nodes/SoLineSet.h>
12#include <Inventor/nodes/SoMaterial.h>
13#include <Inventor/nodes/SoShapeHints.h>
14
15namespace armarx::viz::coin
16{
18 {
19 using ElementType = data::ElementPolygon;
20
22 {
23 coordinate3 = new SoCoordinate3;
24
25 faceSet = new SoFaceSet;
26
27 SoShapeHints* shapeHint = new SoShapeHints;
28 shapeHint->faceType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
29
30 // create line around polygon
31 SoSeparator* lineSep = new SoSeparator;
32
33 lineMaterial = new SoMaterial;
34 lineSep->addChild(lineMaterial);
35 lineSep->addChild(coordinate3);
36
37 lineStyle = new SoDrawStyle();
38 lineSep->addChild(lineStyle);
39
40 lineSet = new SoLineSet;
41 lineSep->addChild(lineSet);
42
43 node->addChild(coordinate3);
44 node->addChild(shapeHint);
45 node->addChild(faceSet);
46 node->addChild(lineSep);
47 }
48
49 bool
50 update(ElementType const& element)
51 {
52 if (element.points.size() < 3)
53 {
54 // A polygon with < 3 points is invalid.
55 ARMARX_WARNING << deactivateSpam(1000) << "Ignoring polygon '" << element.id << "' "
56 << "with " << element.points.size() << " points "
57 << "(a polygon requires >= 3 points).";
58 return true;
59 }
60
61 int pointSize = (int)element.points.size();
62
63 int i = 0;
64 coordinate3->point.setNum(pointSize + 1);
65 for (auto& point : element.points)
66 {
67 SbVec3f pt(point.e0, point.e1, point.e2);
68 coordinate3->point.set1Value(i, pt);
69 i += 1;
70 }
71
72 if (pointSize > 0)
73 {
74 // Add the first element as last element to close the loop
75 auto& first = element.points.front();
76 SbVec3f pt0(first.e0, first.e1, first.e2);
77 coordinate3->point.set1Value(pointSize, pt0);
78 }
79
80 faceSet->numVertices.set1Value(0, pointSize);
81
82 // Line around polygon
83 const float conv = 1.0f / 255.0f;
84 float r = element.lineColor.r * conv;
85 float g = element.lineColor.g * conv;
86 float b = element.lineColor.b * conv;
87 float a = element.lineColor.a * conv;
88 lineMaterial->diffuseColor.setValue(r, g, b);
89 lineMaterial->ambientColor.setValue(r, g, b);
90 lineMaterial->transparency.setValue(1.0f - a);
91
92 if (element.lineWidth > 0.0f)
93 {
94 lineStyle->lineWidth.setValue(element.lineWidth);
95 }
96 else
97 {
98 lineStyle->style = SoDrawStyleElement::INVISIBLE;
99 }
100
101 lineSet->numVertices.set1Value(0, pointSize + 1);
102
103 return true;
104 }
105
106 SoCoordinate3* coordinate3;
107 SoFaceSet* faceSet;
108 SoDrawStyle* lineStyle;
109 SoLineSet* lineSet;
110 SoMaterial* lineMaterial;
111 };
112} // namespace armarx::viz::coin
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
bool update(ElementType const &element)