VisualizationArrowCircle.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 
7 #include <Inventor/nodes/SoCone.h>
8 #include <Inventor/nodes/SoCoordinate3.h>
9 #include <Inventor/nodes/SoCylinder.h>
10 #include <Inventor/nodes/SoIndexedFaceSet.h>
11 #include <Inventor/nodes/SoShapeHints.h>
12 #include <Inventor/nodes/SoSphere.h>
13 #include <Inventor/nodes/SoTranslation.h>
14 
15 
16 namespace armarx::viz::coin
17 {
19  {
20  using ElementType = data::ElementArrowCircle;
21 
22  SoCoordinate3* coords;
23  SoIndexedFaceSet* torusFaceSet;
24  SoSeparator* torus;
25  SoSeparator* coneSep;
26  SoTransform* coneTransform;
27  SoTransform* coneSignRotation;
28  SoCone* cone;
29 
30  std::vector<SbVec3f> vertexPositions;
31  std::vector<int32_t> faces;
32  std::vector<int32_t> matInx;
33 
34  static const int RINGS = 32;
35  static const int SIDES = 8;
36 
38  {
39  SoMaterialBinding* torusBinding = new SoMaterialBinding;
40  torusBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
41 
42  coords = new SoCoordinate3;
43 
44  SoShapeHints* torusHints = new SoShapeHints;
45  // Disable back culling and enable two-sided lighting
46  torusHints->vertexOrdering = SoShapeHints::VertexOrdering::COUNTERCLOCKWISE;
47  torusHints->shapeType = SoShapeHints::ShapeType::UNKNOWN_SHAPE_TYPE;
48 
49  torusFaceSet = new SoIndexedFaceSet();
50  torus = new SoSeparator;
51 
52  torus->addChild(torusBinding);
53  torus->addChild(coords);
54  torus->addChild(torusHints);
55  torus->addChild(torusFaceSet);
56 
57  coneSep = new SoSeparator();
58  coneTransform = new SoTransform;
59 
60  node->addChild(torus);
61  node->addChild(coneSep);
62 
63  coneSignRotation = new SoTransform;
64  cone = new SoCone;
65 
66  coneSep->addChild(coneTransform);
67  coneSep->addChild(coneSignRotation);
68  coneSep->addChild(cone);
69 
70  int numVerticesPerRow = SIDES + 1;
71  int numVerticesPerColumn = RINGS + 1;
72  vertexPositions.resize(numVerticesPerRow * numVerticesPerColumn);
73 
74  int numFaces = RINGS * SIDES * 2;
75  faces.resize(numFaces * 4);
76  matInx.resize(numFaces * 4);
77  }
78 
79  bool update(ElementType const& element)
80  {
81  float completion = std::min<float>(1.0f, std::max(-1.0f, element.completion));
82  int sign = completion >= 0 ? 1 : -1;
83  float torusCompletion = completion - 1.0f / RINGS * sign;
84  if (torusCompletion * sign < 0)
85  {
86  torusCompletion = 0;
87  }
88 
89  const float TWO_PI = 2.0f * (float)M_PI;
90 
91  {
92  // Create a torus mesh (for the completion circle
93  float radius = element.radius;
94  float tubeRadius = element.width;
95  float completion = torusCompletion;
96 
97  int numVerticesPerRow = SIDES + 1;
98  int numVerticesPerColumn = RINGS + 1;
99 
100 
101  float theta = 0.0f;
102  float phi = 0.0f;
103  float verticalAngularStride = TWO_PI / RINGS;
104  float horizontalAngularStride = TWO_PI / SIDES;
105 
106  int numVertices = numVerticesPerColumn * numVerticesPerRow;
107  for (int verticalIt = 0; verticalIt < numVerticesPerColumn; verticalIt++)
108  {
109  theta = verticalAngularStride * verticalIt * completion;
110 
111  for (int horizontalIt = 0; horizontalIt < numVerticesPerRow; horizontalIt++)
112  {
113  phi = horizontalAngularStride * horizontalIt;
114 
115  // position
116  float x = std::cos(theta) * (radius + tubeRadius * std::cos(phi));
117  float y = std::sin(theta) * (radius + tubeRadius * std::cos(phi));
118  float z = tubeRadius * std::sin(phi);
119 
120  int vertexIndex = verticalIt * numVerticesPerRow + horizontalIt;
121  vertexPositions[vertexIndex].setValue(x, y, z);
122  }
123  }
124  coords->point.setValuesPointer(numVertices, vertexPositions.data());
125 
126  for (int verticalIt = 0; verticalIt < RINGS; verticalIt++)
127  {
128  for (int horizontalIt = 0; horizontalIt < SIDES; horizontalIt++)
129  {
130  int faceIndex = (verticalIt * SIDES + horizontalIt) * 2;
131 
132  short lt = (short)(horizontalIt + verticalIt * (numVerticesPerRow));
133  short rt = (short)((horizontalIt + 1) + verticalIt * (numVerticesPerRow));
134 
135  short lb = (short)(horizontalIt + (verticalIt + 1) * (numVerticesPerRow));
136  short rb = (short)((horizontalIt + 1) + (verticalIt + 1) * (numVerticesPerRow));
137 
138 
139  faces[faceIndex * 4 + 0] = lt;
140  faces[faceIndex * 4 + 1] = rt;
141  faces[faceIndex * 4 + 2] = lb;
142  faces[faceIndex * 4 + 3] = SO_END_FACE_INDEX;
143 
144  matInx[faceIndex * 4 + 0] = 0;
145  matInx[faceIndex * 4 + 1] = 0;
146  matInx[faceIndex * 4 + 2] = 0;
147  matInx[faceIndex * 4 + 3] = SO_END_FACE_INDEX;
148 
149  faceIndex += 1;
150 
151  faces[faceIndex * 4 + 0] = rt;
152  faces[faceIndex * 4 + 1] = rb;
153  faces[faceIndex * 4 + 2] = lb;
154  faces[faceIndex * 4 + 3] = SO_END_FACE_INDEX;
155 
156  matInx[faceIndex * 4 + 0] = 0;
157  matInx[faceIndex * 4 + 1] = 0;
158  matInx[faceIndex * 4 + 2] = 0;
159  matInx[faceIndex * 4 + 3] = SO_END_FACE_INDEX;
160  }
161  }
162 
163  torusFaceSet->coordIndex.setValuesPointer(faces.size(), faces.data());
164  torusFaceSet->materialIndex.setValuesPointer(matInx.size(), matInx.data());
165  }
166 
167  {
168  // Create a cone to make the arrow for the completion circle
169  float angle0 = (RINGS - 1.0f) / RINGS * TWO_PI * completion;
170  float x0 = element.radius * std::cos(angle0);
171  float y0 = element.radius * std::sin(angle0);
172  float angle1 = (RINGS - 0.5f) / RINGS * TWO_PI * completion;
173 
174  coneTransform->translation.setValue(x0, y0, 0);
175 
176  coneTransform->rotation.setValue(SbVec3f(0, 0, 1), angle1);
177 
178  float coneHeight = element.width * 6.0f;
179  float coneBottomRadius = element.width * 2.5f;
180 
181  SbVec3f axis(0.0f, 0.0f, 1.0f);
182  float angle = sign > 0.0f ? 0.0f : (float)M_PI;
183  coneSignRotation->rotation.setValue(axis, angle);
184 
185  cone->bottomRadius.setValue(coneBottomRadius);
186  cone->height.setValue(coneHeight);
187  }
188 
189  return true;
190  }
191  };
192 }
armarx::viz::coin::VisualizationArrowCircle::torusFaceSet
SoIndexedFaceSet * torusFaceSet
Definition: VisualizationArrowCircle.h:23
armarx::viz::coin::VisualizationArrowCircle::coneTransform
SoTransform * coneTransform
Definition: VisualizationArrowCircle.h:26
armarx::viz::coin::VisualizationArrowCircle::ElementType
data::ElementArrowCircle ElementType
Definition: VisualizationArrowCircle.h:20
ElementVisualizer.h
armarx::viz::coin::TypedElementVisualization
Definition: ElementVisualizer.h:53
armarx::viz::coin::VisualizationArrowCircle::coneSignRotation
SoTransform * coneSignRotation
Definition: VisualizationArrowCircle.h:27
armarx::viz::coin::VisualizationArrowCircle::torus
SoSeparator * torus
Definition: VisualizationArrowCircle.h:24
armarx::viz::coin::VisualizationArrowCircle::coords
SoCoordinate3 * coords
Definition: VisualizationArrowCircle.h:22
armarx::sign
T sign(T t)
Definition: algorithm.h:194
armarx::viz::coin::VisualizationArrowCircle::coneSep
SoSeparator * coneSep
Definition: VisualizationArrowCircle.h:25
armarx::viz::coin::VisualizationArrowCircle::vertexPositions
std::vector< SbVec3f > vertexPositions
Definition: VisualizationArrowCircle.h:30
armarx::viz::coin::VisualizationArrowCircle::update
bool update(ElementType const &element)
Definition: VisualizationArrowCircle.h:79
armarx::viz::coin::VisualizationArrowCircle
Definition: VisualizationArrowCircle.h:18
armarx::viz::coin::VisualizationArrowCircle::cone
SoCone * cone
Definition: VisualizationArrowCircle.h:28
armarx::viz::coin::VisualizationArrowCircle::SIDES
static const int SIDES
Definition: VisualizationArrowCircle.h:35
armarx::viz::coin::TypedElementVisualization< SoSeparator >::node
NodeType * node
Definition: ElementVisualizer.h:68
M_PI
#define M_PI
Definition: MathTools.h:17
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::viz::coin::VisualizationArrowCircle::faces
std::vector< int32_t > faces
Definition: VisualizationArrowCircle.h:31
armarx::viz::coin::VisualizationArrowCircle::matInx
std::vector< int32_t > matInx
Definition: VisualizationArrowCircle.h:32
armarx::viz::coin
Definition: ElementVisualizer.cpp:11
float
#define float
Definition: 16_Level.h:22
armarx::viz::coin::VisualizationArrowCircle::RINGS
static const int RINGS
Definition: VisualizationArrowCircle.h:34
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:100
armarx::viz::coin::VisualizationArrowCircle::VisualizationArrowCircle
VisualizationArrowCircle()
Definition: VisualizationArrowCircle.h:37