iMSTK
Interactive Medical Simulation Toolkit
imstkDebugGeometryModel.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkDebugGeometryModel.h"
8 #include "imstkLineMesh.h"
9 #include "imstkRenderMaterial.h"
10 #include "imstkSurfaceMesh.h"
11 #include "imstkVecDataArray.h"
12 #include "imstkVisualModel.h"
13 #include "imstkEntity.h"
14 
15 namespace imstk
16 {
17 DebugGeometryModel::DebugGeometryModel(const std::string& name) : SceneBehaviour(name),
18  m_arrowScale(1.0),
19  m_arrowColor(Color(0.0, 1.0, 0.0)),
20  m_debugLineMesh(std::make_shared<LineMesh>()),
21  m_debugPointSet(std::make_shared<PointSet>()),
22  m_debugSurfMesh(std::make_shared<SurfaceMesh>()),
23  m_triVerticesPtr(m_debugSurfMesh->getVertexPositions()),
24  m_triIndicesPtr(m_debugSurfMesh->getCells()),
25  m_triColorsPtr(std::make_shared<VecDataArray<unsigned char, 3>>()),
26  m_trianglesChanged(false),
27  m_lineVerticesPtr(m_debugLineMesh->getVertexPositions()),
28  m_lineIndicesPtr(m_debugLineMesh->getCells()),
29  m_lineColorsPtr(std::make_shared<VecDataArray<unsigned char, 3>>()),
30  m_linesChanged(false),
31  m_pointVerticesPtr(m_debugPointSet->getVertexPositions()),
32  m_pointColorsPtr(std::make_shared<VecDataArray<unsigned char, 3>>()),
33  m_ptsChanged(false)
34 {
35  // Add color attributes to each mesh
36  m_debugPointSet->setVertexScalars("colors", m_pointColorsPtr);
37  m_debugLineMesh->setCellScalars("colors", m_lineColorsPtr);
38  m_debugSurfMesh->setCellScalars("colors", m_triColorsPtr);
39 
40  auto lineMaterial = std::make_shared<RenderMaterial>();
41  lineMaterial->setDisplayMode(RenderMaterial::DisplayMode::Wireframe);
42  lineMaterial->setRecomputeVertexNormals(false);
43  lineMaterial->setBackFaceCulling(false);
44  lineMaterial->setLineWidth(20.0);
45  lineMaterial->setColor(Color::Blue);
46 
47  m_debugLineModel = std::make_shared<VisualModel>();
48  m_debugLineModel->setGeometry(m_debugLineMesh);
49  m_debugLineModel->setRenderMaterial(lineMaterial);
50 
51  auto pointMaterial = std::make_shared<RenderMaterial>();
52  pointMaterial->setDisplayMode(RenderMaterial::DisplayMode::Points);
53  pointMaterial->setRecomputeVertexNormals(false);
54  pointMaterial->setBackFaceCulling(false);
55  pointMaterial->setPointSize(10.0);
56  pointMaterial->setColor(Color::Red);
57 
58  m_debugPointModel = std::make_shared<VisualModel>();
59  m_debugPointModel->setGeometry(m_debugPointSet);
60  m_debugPointModel->setRenderMaterial(pointMaterial);
61 
62  auto faceMaterial = std::make_shared<RenderMaterial>();
63  faceMaterial->setRecomputeVertexNormals(false);
64  faceMaterial->setBackFaceCulling(false);
65  faceMaterial->setColor(Color::Orange);
66 
67  m_debugSurfModel = std::make_shared<VisualModel>();
68  m_debugSurfModel->setGeometry(m_debugSurfMesh);
69  m_debugSurfModel->setRenderMaterial(faceMaterial);
70 }
71 
72 void
74 {
75  std::shared_ptr<Entity> entity = m_entity.lock();
76  CHECK(entity != nullptr) << "DebugGeometryModel must have entity to initialize";
77  if (!entity->containsComponent(m_debugPointModel))
78  {
79  m_debugPointModel->setName(entity->getName() + "_DebugPointModel");
80  entity->addComponent(m_debugPointModel);
81  }
82  if (!entity->containsComponent(m_debugLineModel))
83  {
84  m_debugLineModel->setName(entity->getName() + "_DebugLineModel");
85  entity->addComponent(m_debugLineModel);
86  }
87  if (!entity->containsComponent(m_debugSurfModel))
88  {
89  m_debugSurfModel->setName(entity->getName() + "_DebugSurfModel");
90  entity->addComponent(m_debugSurfModel);
91  }
92 }
93 
94 void
95 DebugGeometryModel::addLine(const Vec3d& a, const Vec3d& b)
96 {
97  addLine(a, b, m_debugLineModel->getRenderMaterial()->getColor());
98 }
99 
100 void
101 DebugGeometryModel::addLine(const Vec3d& a, const Vec3d& b, const Color& color)
102 {
103  const int startI = static_cast<int>(m_lineVerticesPtr->size());
104  m_lineVerticesPtr->push_back(a);
105  m_lineVerticesPtr->push_back(b);
106  m_lineIndicesPtr->push_back(Vec2i(startI, startI + 1));
107 
108  Eigen::Matrix<unsigned char, 3, 1> col(
109  static_cast<unsigned char>(color.r * 255.0),
110  static_cast<unsigned char>(color.g * 255.0),
111  static_cast<unsigned char>(color.b * 255.0));
112  m_lineColorsPtr->push_back(col);
113 
114  m_linesChanged = true;
115 }
116 
117 void
118 DebugGeometryModel::addTriangle(const Vec3d& a, const Vec3d& b, const Vec3d& c)
119 {
120  addTriangle(a, b, c, m_debugSurfModel->getRenderMaterial()->getColor());
121 }
122 
123 void
124 DebugGeometryModel::addTriangle(const Vec3d& a, const Vec3d& b, const Vec3d& c, const Color& color)
125 {
126  const int startI = static_cast<int>(m_triVerticesPtr->size());
127  m_triVerticesPtr->push_back(a);
128  m_triVerticesPtr->push_back(b);
129  m_triVerticesPtr->push_back(c);
130 
131  m_triIndicesPtr->push_back(Vec3i(startI, startI + 1, startI + 2));
132 
133  Eigen::Matrix<unsigned char, 3, 1> col(
134  static_cast<unsigned char>(color.r * 255.0),
135  static_cast<unsigned char>(color.g * 255.0),
136  static_cast<unsigned char>(color.b * 255.0));
137  m_triColorsPtr->push_back(col);
138 
139  m_trianglesChanged = true;
140 }
141 
142 void
144 {
145  addPoint(a, m_debugPointModel->getRenderMaterial()->getColor());
146 }
147 
148 void
149 DebugGeometryModel::addPoint(const Vec3d& a, const Color& color)
150 {
151  m_pointVerticesPtr->push_back(a);
152 
153  Eigen::Matrix<unsigned char, 3, 1> col(
154  static_cast<unsigned char>(color.r * 255.0),
155  static_cast<unsigned char>(color.g * 255.0),
156  static_cast<unsigned char>(color.b * 255.0));
157  m_pointColorsPtr->push_back(col);
158 
159  m_ptsChanged = true;
160 }
161 
162 void
163 DebugGeometryModel::addArrow(const Vec3d& start, const Vec3d& end)
164 {
165  addArrow(start, end, m_arrowColor);
166 }
167 
168 void
169 DebugGeometryModel::addArrow(const Vec3d& start, const Vec3d& end, const Color& color)
170 {
171  const Vec3d scaledEnd = start + (end - start) * m_arrowScale;
172 
173  const Vec3d diff = scaledEnd - start;
174  const double length = diff.norm();
175  const Vec3d tan = Vec3d(1.0, 0.0, 0.0).cross(diff).normalized();
176 
177  addLine(start, scaledEnd, color);
178  addLine(scaledEnd, scaledEnd - diff * 0.2 + tan * length * 0.2, color);
179  addLine(scaledEnd, scaledEnd - diff * 0.2 - tan * length * 0.2, color);
180 }
181 
182 void
184 {
185  // \todo: Resize 0 shouldn't reallocate
186  m_triVerticesPtr->resize(0);
187  m_triIndicesPtr->resize(0);
188  m_triColorsPtr->resize(0);
189 
190  m_lineIndicesPtr->resize(0);
191  m_lineVerticesPtr->resize(0);
192  m_lineColorsPtr->resize(0);
193 
194  m_pointVerticesPtr->resize(0);
195  m_pointColorsPtr->resize(0);
196 
197  m_triVerticesPtr->postModified();
198  m_triIndicesPtr->postModified();
199  m_triColorsPtr->postModified();
200  m_lineIndicesPtr->postModified();
201  m_lineVerticesPtr->postModified();
202  m_lineColorsPtr->postModified();
203  m_pointVerticesPtr->postModified();
204  m_pointColorsPtr->postModified();
205 }
206 
207 void
209 {
210  if (m_trianglesChanged)
211  {
212  m_trianglesChanged = false;
213  m_triVerticesPtr->postModified();
214  m_triIndicesPtr->postModified();
215  m_triColorsPtr->postModified();
216  }
217  if (m_linesChanged)
218  {
219  m_linesChanged = false;
220  m_lineVerticesPtr->postModified();
221  m_lineIndicesPtr->postModified();
222  m_lineColorsPtr->postModified();
223  }
224  if (m_ptsChanged)
225  {
226  m_ptsChanged = false;
227  m_pointVerticesPtr->postModified();
228  m_pointColorsPtr->postModified();
229  }
230 }
231 
232 std::shared_ptr<RenderMaterial>
234 {
235  return m_debugPointModel->getRenderMaterial();
236 }
237 
238 std::shared_ptr<RenderMaterial>
239 DebugGeometryModel::getLineMaterial() const
240 {
241  return m_debugLineModel->getRenderMaterial();
242 }
243 
244 std::shared_ptr<RenderMaterial>
245 DebugGeometryModel::getFaceMaterial() const
246 {
247  return m_debugSurfModel->getRenderMaterial();
248 }
249 
250 void
251 DebugGeometryModel::setLineWidth(const double width)
252 {
253  m_debugLineModel->getRenderMaterial()->setLineWidth(width);
254 }
255 
256 void
257 DebugGeometryModel::setTriColor(const Color& color)
258 {
259  m_debugSurfModel->getRenderMaterial()->setColor(color);
260 }
261 
262 void
263 DebugGeometryModel::setLineColor(const Color& color)
264 {
265  m_debugLineModel->getRenderMaterial()->setColor(color);
266 }
267 
268 void
269 DebugGeometryModel::setPointColor(const Color& color)
270 {
271  m_debugPointModel->getRenderMaterial()->setColor(color);
272 }
273 
274 void
275 DebugGeometryModel::setArrowColor(const Color& color)
276 {
277  m_arrowColor = color;
278 }
279 
280 void
281 DebugGeometryModel::setPointSize(const double size)
282 {
283  m_debugPointModel->getRenderMaterial()->setPointSize(size);
284 }
285 
286 int
287 DebugGeometryModel::getNumPoints() const
288 {
289  return m_debugPointSet->getNumVertices();
290 }
291 
292 int
293 DebugGeometryModel::getNumLines() const
294 {
295  return m_debugLineMesh->getNumCells();
296 }
297 
298 int
299 DebugGeometryModel::getNumTriangles() const
300 {
301  return m_debugSurfMesh->getNumCells();
302 }
303 } // namespace imstk
void visualUpdate(const double &dt) override
Update the primitives.
std::weak_ptr< Entity > m_entity
Parent entity this component exists on.
void addTriangle(const Vec3d &a, const Vec3d &b, const Vec3d &c)
Adds a triangle to the debug triangles with default color.
void addPoint(const Vec3d &a)
Adds a point to the debug points.
Compound Geometry.
void init() override
Initialize the component, called at a later time after all component construction is complete...
void addLine(const Vec3d &a, const Vec3d &b)
Adds a line to the debug lines with default color.
std::shared_ptr< RenderMaterial > getPointMaterial() const
Accessors.
void addArrow(const Vec3d &start, const Vec3d &end)
Adds an arrow to the debug arrows.
Color in RGB space.
Definition: imstkColor.h:24
void clear()
Clears all primitives.