VisualizationMesh.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/SoCoordinate3.h>
8 #include <Inventor/nodes/SoIndexedFaceSet.h>
9 #include <Inventor/nodes/SoMaterialBinding.h>
10 #include <Inventor/nodes/SoNormal.h>
11 #include <Inventor/nodes/SoShapeHints.h>
12 
13 namespace armarx::viz::coin
14 {
16  {
17  using ElementType = data::ElementMesh;
18 
20  {
21  SoMaterialBinding* myBinding = new SoMaterialBinding;
22  myBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
23 
24  materials = new SoMaterial;
25  coords = new SoCoordinate3;
26 
27  SoShapeHints* hints = new SoShapeHints;
28  // Disable back culling and enable two-sided lighting
29  hints->vertexOrdering = SoShapeHints::VertexOrdering::COUNTERCLOCKWISE;
30  hints->shapeType = SoShapeHints::ShapeType::UNKNOWN_SHAPE_TYPE;
31 
32  faceSet = new SoIndexedFaceSet;
33 
34  node->addChild(myBinding);
35  node->addChild(materials);
36  node->addChild(coords);
37  node->addChild(hints);
38  node->addChild(faceSet);
39  }
40 
41  bool update(ElementType const& element)
42  {
43  int colorSize = (int)element.colors.size();
44  bool noColorsArray = colorSize == 0;
45  if (colorSize == 0)
46  {
47  colorSize = 1;
48  }
49  matColor.resize(colorSize);
50  transp.resize(colorSize);
51 
52  const float conv = 1.0f / 255.0f;
53  if (noColorsArray)
54  {
55  auto color = element.color;
56  float r = color.r * conv;
57  float g = color.g * conv;
58  float b = color.b * conv;
59  float a = color.a * conv;
60  matColor[0].setValue(r, g, b);
61  transp[0] = 1.0f - a;
62  }
63  else
64  {
65  for (int i = 0; i < colorSize; i++)
66  {
67  auto color = element.colors[i];
68  float r = color.r * conv;
69  float g = color.g * conv;
70  float b = color.b * conv;
71  float a = color.a * conv;
72  matColor[i].setValue(r, g, b);
73  transp[i] = 1.0f - a;
74  }
75  }
76 
77  // Define colors for the faces
78  materials->diffuseColor.setValuesPointer(colorSize, matColor.data());
79  materials->ambientColor.setValuesPointer(colorSize, matColor.data());
80  materials->transparency.setValuesPointer(colorSize, transp.data());
81 
82  // define vertex array
83  int vertexSize = (int)element.vertices.size();
84  vertexPositions.resize(vertexSize);
85  for (int i = 0; i < vertexSize; i++)
86  {
87  auto v = element.vertices[i];
88  vertexPositions[i].setValue(v.e0, v.e1, v.e2);
89  }
90 
91  // Define coordinates for vertices
92  coords->point.setValuesPointer(vertexSize, vertexPositions.data());
93 
94  int facesSize = (int)element.faces.size();
95  faces.resize(facesSize * 4);
96  matInx.resize(facesSize * 4);
97 
98  for (int i = 0; i < facesSize; i++)
99  {
100  auto& face = element.faces[i];
101 
102  faces[i * 4 + 0] = face.v0;
103  faces[i * 4 + 1] = face.v1;
104  faces[i * 4 + 2] = face.v2;
105  faces[i * 4 + 3] = SO_END_FACE_INDEX;
106 
107  matInx[i * 4 + 0] = face.c0;
108  matInx[i * 4 + 1] = face.c1;
109  matInx[i * 4 + 2] = face.c2;
110  matInx[i * 4 + 3] = SO_END_FACE_INDEX;
111  }
112 
113  faceSet->coordIndex.setValuesPointer(faces.size(), faces.data());
114  faceSet->materialIndex.setValuesPointer(matInx.size(), matInx.data());
115 
116  return true;
117  }
118 
119  SoMaterial* materials;
120  SoCoordinate3* coords;
121  SoIndexedFaceSet* faceSet;
122 
123  std::vector<SbColor> matColor;
124  std::vector<float> transp;
125  std::vector<SbVec3f> vertexPositions;
126  std::vector<int32_t> faces;
127  std::vector<int32_t> matInx;
128  };
129 }
ElementVisualizer.h
armarx::viz::coin::VisualizationMesh::faces
std::vector< int32_t > faces
Definition: VisualizationMesh.h:126
armarx::viz::coin::TypedElementVisualization
Definition: ElementVisualizer.h:53
armarx::viz::coin::VisualizationMesh::update
bool update(ElementType const &element)
Definition: VisualizationMesh.h:41
armarx::viz::coin::VisualizationMesh::materials
SoMaterial * materials
Definition: VisualizationMesh.h:119
armarx::viz::coin::VisualizationMesh::coords
SoCoordinate3 * coords
Definition: VisualizationMesh.h:120
armarx::viz::coin::VisualizationMesh::matInx
std::vector< int32_t > matInx
Definition: VisualizationMesh.h:127
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::VisualizationMesh
Definition: VisualizationMesh.h:15
armarx::viz::coin::VisualizationMesh::transp
std::vector< float > transp
Definition: VisualizationMesh.h:124
armarx::viz::coin::VisualizationMesh::matColor
std::vector< SbColor > matColor
Definition: VisualizationMesh.h:123
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::viz::coin
Definition: ElementVisualizer.cpp:11
armarx::viz::coin::VisualizationMesh::ElementType
data::ElementMesh ElementType
Definition: VisualizationMesh.h:17
armarx::viz::coin::VisualizationMesh::VisualizationMesh
VisualizationMesh()
Definition: VisualizationMesh.h:19
armarx::viz::coin::VisualizationMesh::vertexPositions
std::vector< SbVec3f > vertexPositions
Definition: VisualizationMesh.h:125
armarx::viz::coin::VisualizationMesh::faceSet
SoIndexedFaceSet * faceSet
Definition: VisualizationMesh.h:121