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" 17 DebugGeometryModel::DebugGeometryModel(
const std::string& name) :
SceneBehaviour(name),
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>>()),
36 m_debugPointSet->setVertexScalars(
"colors", m_pointColorsPtr);
37 m_debugLineMesh->setCellScalars(
"colors", m_lineColorsPtr);
38 m_debugSurfMesh->setCellScalars(
"colors", m_triColorsPtr);
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);
47 m_debugLineModel = std::make_shared<VisualModel>();
48 m_debugLineModel->setGeometry(m_debugLineMesh);
49 m_debugLineModel->setRenderMaterial(lineMaterial);
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);
58 m_debugPointModel = std::make_shared<VisualModel>();
59 m_debugPointModel->setGeometry(m_debugPointSet);
60 m_debugPointModel->setRenderMaterial(pointMaterial);
62 auto faceMaterial = std::make_shared<RenderMaterial>();
63 faceMaterial->setRecomputeVertexNormals(
false);
64 faceMaterial->setBackFaceCulling(
false);
65 faceMaterial->setColor(Color::Orange);
67 m_debugSurfModel = std::make_shared<VisualModel>();
68 m_debugSurfModel->setGeometry(m_debugSurfMesh);
69 m_debugSurfModel->setRenderMaterial(faceMaterial);
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))
79 m_debugPointModel->setName(entity->getName() +
"_DebugPointModel");
80 entity->addComponent(m_debugPointModel);
82 if (!entity->containsComponent(m_debugLineModel))
84 m_debugLineModel->setName(entity->getName() +
"_DebugLineModel");
85 entity->addComponent(m_debugLineModel);
87 if (!entity->containsComponent(m_debugSurfModel))
89 m_debugSurfModel->setName(entity->getName() +
"_DebugSurfModel");
90 entity->addComponent(m_debugSurfModel);
97 addLine(a, b, m_debugLineModel->getRenderMaterial()->getColor());
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));
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);
114 m_linesChanged =
true;
120 addTriangle(a, b, c, m_debugSurfModel->getRenderMaterial()->getColor());
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);
131 m_triIndicesPtr->push_back(Vec3i(startI, startI + 1, startI + 2));
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);
139 m_trianglesChanged =
true;
145 addPoint(a, m_debugPointModel->getRenderMaterial()->getColor());
151 m_pointVerticesPtr->push_back(a);
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);
171 const Vec3d scaledEnd = start + (end - start) * m_arrowScale;
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();
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);
186 m_triVerticesPtr->resize(0);
187 m_triIndicesPtr->resize(0);
188 m_triColorsPtr->resize(0);
190 m_lineIndicesPtr->resize(0);
191 m_lineVerticesPtr->resize(0);
192 m_lineColorsPtr->resize(0);
194 m_pointVerticesPtr->resize(0);
195 m_pointColorsPtr->resize(0);
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();
210 if (m_trianglesChanged)
212 m_trianglesChanged =
false;
213 m_triVerticesPtr->postModified();
214 m_triIndicesPtr->postModified();
215 m_triColorsPtr->postModified();
219 m_linesChanged =
false;
220 m_lineVerticesPtr->postModified();
221 m_lineIndicesPtr->postModified();
222 m_lineColorsPtr->postModified();
226 m_ptsChanged =
false;
227 m_pointVerticesPtr->postModified();
228 m_pointColorsPtr->postModified();
232 std::shared_ptr<RenderMaterial>
235 return m_debugPointModel->getRenderMaterial();
238 std::shared_ptr<RenderMaterial>
239 DebugGeometryModel::getLineMaterial()
const 241 return m_debugLineModel->getRenderMaterial();
244 std::shared_ptr<RenderMaterial>
245 DebugGeometryModel::getFaceMaterial()
const 247 return m_debugSurfModel->getRenderMaterial();
251 DebugGeometryModel::setLineWidth(
const double width)
253 m_debugLineModel->getRenderMaterial()->setLineWidth(width);
257 DebugGeometryModel::setTriColor(
const Color& color)
259 m_debugSurfModel->getRenderMaterial()->setColor(color);
263 DebugGeometryModel::setLineColor(
const Color& color)
265 m_debugLineModel->getRenderMaterial()->setColor(color);
269 DebugGeometryModel::setPointColor(
const Color& color)
271 m_debugPointModel->getRenderMaterial()->setColor(color);
275 DebugGeometryModel::setArrowColor(
const Color& color)
277 m_arrowColor = color;
281 DebugGeometryModel::setPointSize(
const double size)
283 m_debugPointModel->getRenderMaterial()->setPointSize(size);
287 DebugGeometryModel::getNumPoints()
const 289 return m_debugPointSet->getNumVertices();
293 DebugGeometryModel::getNumLines()
const 295 return m_debugLineMesh->getNumCells();
299 DebugGeometryModel::getNumTriangles()
const 301 return m_debugSurfMesh->getNumCells();
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.
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.
void clear()
Clears all primitives.