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