VisualizationArrowCircle.h
Go to the documentation of this file.
1#pragma once
2
3#include <RobotAPI/interface/ArViz/Elements.h>
4
5#include "ElementVisualizer.h"
6#include <Inventor/nodes/SoCone.h>
7#include <Inventor/nodes/SoCoordinate3.h>
8#include <Inventor/nodes/SoCylinder.h>
9#include <Inventor/nodes/SoIndexedFaceSet.h>
10#include <Inventor/nodes/SoShapeHints.h>
11#include <Inventor/nodes/SoSphere.h>
12#include <Inventor/nodes/SoTransform.h>
13#include <Inventor/nodes/SoTranslation.h>
14
15namespace armarx::viz::coin
16{
18 {
19 using ElementType = data::ElementArrowCircle;
20
21 SoCoordinate3* coords;
22 SoIndexedFaceSet* torusFaceSet;
23 SoSeparator* torus;
24 SoSeparator* coneSep;
25 SoTransform* coneTransform;
26 SoTransform* coneSignRotation;
27 SoCone* cone;
28
29 std::vector<SbVec3f> vertexPositions;
30 std::vector<int32_t> faces;
31 std::vector<int32_t> matInx;
32
33 static const int RINGS = 32;
34 static const int SIDES = 8;
35
37 {
38 SoMaterialBinding* torusBinding = new SoMaterialBinding;
39 torusBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
40
41 coords = new SoCoordinate3;
42
43 SoShapeHints* torusHints = new SoShapeHints;
44 // Disable back culling and enable two-sided lighting
45 torusHints->vertexOrdering = SoShapeHints::VertexOrdering::COUNTERCLOCKWISE;
46 torusHints->shapeType = SoShapeHints::ShapeType::UNKNOWN_SHAPE_TYPE;
47
48 torusFaceSet = new SoIndexedFaceSet();
49 torus = new SoSeparator;
50
51 torus->addChild(torusBinding);
52 torus->addChild(coords);
53 torus->addChild(torusHints);
54 torus->addChild(torusFaceSet);
55
56 coneSep = new SoSeparator();
57 coneTransform = new SoTransform;
58
59 node->addChild(torus);
60 node->addChild(coneSep);
61
62 coneSignRotation = new SoTransform;
63 cone = new SoCone;
64
65 coneSep->addChild(coneTransform);
66 coneSep->addChild(coneSignRotation);
67 coneSep->addChild(cone);
68
69 int numVerticesPerRow = SIDES + 1;
70 int numVerticesPerColumn = RINGS + 1;
71 vertexPositions.resize(numVerticesPerRow * numVerticesPerColumn);
72
73 int numFaces = RINGS * SIDES * 2;
74 faces.resize(numFaces * 4);
75 matInx.resize(numFaces * 4);
76 }
77
78 bool
79 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 =
137 (short)((horizontalIt + 1) + (verticalIt + 1) * (numVerticesPerRow));
138
139
140 faces[faceIndex * 4 + 0] = lt;
141 faces[faceIndex * 4 + 1] = rt;
142 faces[faceIndex * 4 + 2] = lb;
143 faces[faceIndex * 4 + 3] = SO_END_FACE_INDEX;
144
145 matInx[faceIndex * 4 + 0] = 0;
146 matInx[faceIndex * 4 + 1] = 0;
147 matInx[faceIndex * 4 + 2] = 0;
148 matInx[faceIndex * 4 + 3] = SO_END_FACE_INDEX;
149
150 faceIndex += 1;
151
152 faces[faceIndex * 4 + 0] = rt;
153 faces[faceIndex * 4 + 1] = rb;
154 faces[faceIndex * 4 + 2] = lb;
155 faces[faceIndex * 4 + 3] = SO_END_FACE_INDEX;
156
157 matInx[faceIndex * 4 + 0] = 0;
158 matInx[faceIndex * 4 + 1] = 0;
159 matInx[faceIndex * 4 + 2] = 0;
160 matInx[faceIndex * 4 + 3] = SO_END_FACE_INDEX;
161 }
162 }
163
164 torusFaceSet->coordIndex.setValuesPointer(faces.size(), faces.data());
165 torusFaceSet->materialIndex.setValuesPointer(matInx.size(), matInx.data());
166 }
167
168 {
169 // Create a cone to make the arrow for the completion circle
170 float angle0 = (RINGS - 1.0f) / RINGS * TWO_PI * completion;
171 float x0 = element.radius * std::cos(angle0);
172 float y0 = element.radius * std::sin(angle0);
173 float angle1 = (RINGS - 0.5f) / RINGS * TWO_PI * completion;
174
175 coneTransform->translation.setValue(x0, y0, 0);
176
177 coneTransform->rotation.setValue(SbVec3f(0, 0, 1), angle1);
178
179 float coneHeight = element.width * 6.0f;
180 float coneBottomRadius = element.width * 2.5f;
181
182 SbVec3f axis(0.0f, 0.0f, 1.0f);
183 float angle = sign > 0.0f ? 0.0f : (float)M_PI;
184 coneSignRotation->rotation.setValue(axis, angle);
185
186 cone->bottomRadius.setValue(coneBottomRadius);
187 cone->height.setValue(coneHeight);
188 }
189
190 return true;
191 }
192 };
193} // namespace armarx::viz::coin
#define float
Definition 16_Level.h:22
#define M_PI
Definition MathTools.h:17
This file offers overloads of toIce() and fromIce() functions for STL container types.
T sign(T t)
Definition algorithm.h:214
double angle(const Point &a, const Point &b, const Point &c)
Definition point.hpp:109