3 #include <VirtualRobot/math/Helpers.h>
4 #include <VirtualRobot/Visualization/TriMeshModel.h>
12 #include <SimoxUtility/color/hsv.h>
25 layer(layer), name(name)
30 return VisuID(this->layer, newName);
35 os <<
"Visu '" << rhs.
name <<
"' on layer '" << rhs.
layer <<
"'";
40 const std::string DebugDrawerTopic::TOPIC_NAME =
"DebugDrawerUpdates";
41 const std::string DebugDrawerTopic::DEFAULT_LAYER =
"debug";
70 return _enabled && topic;
75 component.offeringTopic(topicNameOverride.empty() ? TOPIC_NAME : topicNameOverride);
100 this->_lengthScale = scale;
120 this->_poseScale = scale;
135 this->
sleepFor(_shortSleepDuration);
159 topic->clearLayer(layer);
168 const VisuID&
id,
const Eigen::Vector3f& position,
const std::string& text,
169 int size,
const DrawColor color,
170 bool ignoreLengthScale)
174 const float scale = lengthScale(ignoreLengthScale);
175 topic->setTextVisu(layer(
id),
id.name, text, scaled(scale, position), color, size);
181 const Eigen::Vector3f& extents,
const DrawColor& color,
182 bool ignoreLengthScale)
186 const float scale = lengthScale(ignoreLengthScale);
187 topic->setBoxVisu(layer(
id),
id.name,
189 scaled(scale, extents), color);
194 const DrawColor& color,
bool ignoreLengthScale)
197 extents, color, ignoreLengthScale);
201 const DrawColor& color,
bool ignoreLengthScale)
208 const VirtualRobot::BoundingBox& boundingBox,
const Eigen::Matrix4f& pose,
209 const DrawColor& color,
bool ignoreLengthScale)
211 const Eigen::Vector3f center = .5 * (boundingBox.getMin() + boundingBox.getMax());
212 drawBox(
id, ::math::Helpers::TransformPosition(pose, center),
214 boundingBox.getMax() - boundingBox.getMin(), color, ignoreLengthScale);
221 topic->removeBoxVisu(layer(
id),
id.name);
228 const Eigen::Vector3f& extents,
229 float width,
const DrawColor& color,
bool ignoreLengthScale)
237 float width,
const DrawColor& color,
bool ignoreLengthScale)
244 std::vector<Eigen::Vector3f> points;
247 bb.col(0) = -extents / 2;
248 bb.col(1) = extents / 2;
250 auto addLine = [&](
int x1,
int y1,
int z1,
int x2,
int y2,
int z2)
252 Eigen::Vector3f start = { bb.col(x1).x(), bb.col(y1).y(), bb.col(z1).z() };
253 Eigen::Vector3f end = { bb.col(x2).x(), bb.col(y2).y(), bb.col(z2).z() };
255 start = ::math::Helpers::TransformPosition(pose, start);
256 end = ::math::Helpers::TransformPosition(pose, end);
258 points.push_back(start);
259 points.push_back(end);
271 addLine(0, 0, 0, 1, 0, 0);
272 addLine(0, 0, 0, 0, 1, 0);
273 addLine(0, 0, 0, 0, 0, 1);
276 addLine(1, 1, 1, 0, 1, 1);
277 addLine(1, 1, 1, 1, 0, 1);
278 addLine(1, 1, 1, 1, 1, 0);
281 addLine(1, 0, 0, 1, 0, 1);
282 addLine(1, 0, 0, 1, 1, 0);
285 addLine(0, 1, 0, 1, 1, 0);
286 addLine(0, 1, 0, 0, 1, 1);
289 addLine(0, 0, 1, 1, 0, 1);
290 addLine(0, 0, 1, 0, 1, 1);
292 drawLineSet(
id, points, width, color, ignoreLengthScale);
297 float width,
const DrawColor& color,
bool ignoreLengthScale)
304 float width,
const DrawColor& color,
bool ignoreLengthScale)
311 const VirtualRobot::BoundingBox& boundingBox,
const Eigen::Matrix4f& pose,
312 float width,
const DrawColor& color,
bool ignoreLengthScale)
314 const Eigen::Vector3f center = .5 * (boundingBox.getMin() + boundingBox.getMax());
318 boundingBox.getMax() - boundingBox.getMin(),
319 width, color, ignoreLengthScale);
325 float width,
const DrawColor& color,
bool ignoreLengthScale)
327 const Eigen::Vector3f center = 0.5 * (aabb.col(0) + aabb.col(1));
331 aabb.col(1) - aabb.col(0),
332 width, color, ignoreLengthScale);
343 const Eigen::Vector3f& center,
const Eigen::Vector3f& direction,
344 float length,
float radius,
345 const DrawColor& color,
bool ignoreLengthScale)
349 const float scale = lengthScale(ignoreLengthScale);
350 topic->setCylinderVisu(layer(
id),
id.name, scaled(scale, center),
new Vector3(direction),
351 scaled(scale, length), scaled(scale, radius), color);
358 float length,
float radius,
359 const DrawColor& color,
bool ignoreLengthScale)
361 drawCylinder(
id, center, orientation * Eigen::Vector3f::UnitY(), radius, length, color,
367 const Eigen::Vector3f& from,
const Eigen::Vector3f& to,
float radius,
368 const DrawColor& color,
bool ignoreLengthScale)
372 const Eigen::Vector3f dir = (to - from);
373 const float length = dir.norm();
374 drawCylinder(
id, .5 * (from + to), dir / length, radius, length, color, ignoreLengthScale);
383 topic->removeCylinderVisu(layer(
id),
id.name);
390 const Eigen::Vector3f& center,
float radius,
391 const DrawColor& color,
bool ignoreLengthScale)
395 const float scale = lengthScale(ignoreLengthScale);
396 topic->setSphereVisu(layer(
id),
id.name, scaled(scale, center), color,
397 scaled(scale, radius));
406 topic->removeSphereVisu(layer(
id),
id.name);
413 const Eigen::Vector3f& position,
const Eigen::Vector3f& direction,
float length,
414 float width,
const DrawColor& color,
bool ignoreLengthScale)
418 const float scale = lengthScale(ignoreLengthScale);
419 topic->setArrowVisu(layer(
id),
id.name, scaled(scale, position),
new Vector3(direction),
420 color, scaled(scale, length), scaled(scale, width));
427 const Eigen::Vector3f& from,
const Eigen::Vector3f& to,
428 float width,
const DrawColor& color,
bool ignoreLengthScale)
432 const Eigen::Vector3f dir = (to - from);
433 const float length = dir.norm();
434 drawArrow(
id, from, dir / length, length, width, color, ignoreLengthScale);
443 topic->removeArrowVisu(layer(
id),
id.name);
449 const std::vector<Eigen::Vector3f>& points,
450 const DrawColor& colorFace,
float lineWidth,
const DrawColor& colorEdge,
451 bool ignoreLengthScale)
455 const float scale = lengthScale(ignoreLengthScale);
457 PolygonPointList polyPoints;
458 polyPoints.reserve(points.size());
460 for (
const auto& point : points)
462 polyPoints.push_back(scaled(scale, point));
465 topic->setPolygonVisu(layer(
id),
id.name, polyPoints,
466 colorFace, colorEdge, lineWidth);
475 topic->removePolygonVisu(layer(
id),
id.name);
481 const VisuID&
id,
const Eigen::Vector3f& from,
const Eigen::Vector3f& to,
482 float width,
const DrawColor& color,
bool ignoreLengthScale)
486 const float scale = lengthScale(ignoreLengthScale);
488 topic->setLineVisu(layer(
id),
id.name, scaled(scale, from), scaled(scale, to),
498 topic->removeLineVisu(layer(
id),
id.name);
504 const VisuID&
id,
const DebugDrawerLineSet& lineSet,
bool ignoreLengthScale)
508 const float scale = lengthScale(ignoreLengthScale);
510 if (1.f <= scale && scale <= 1.f)
513 topic->setLineSetVisu(layer(
id),
id.name, lineSet);
518 DebugDrawerLineSet scaledLineSet = lineSet;
519 for (
auto& point : scaledLineSet.points)
521 scaleXYZ(scale, point);
523 topic->setLineSetVisu(layer(
id),
id.name, scaledLineSet);
530 const VisuID&
id,
const std::vector<Eigen::Vector3f>& points,
531 float width,
const DrawColor& color,
bool ignoreLengthScale)
535 const float scale = lengthScale(ignoreLengthScale);
537 DebugDrawerLineSet lineSet;
538 for (
const auto& point : points)
540 lineSet.points.push_back(scaledT<DebugDrawerPointCloudElement>(scale, point));
543 lineSet.lineWidth = width;
544 lineSet.colorNoIntensity = lineSet.colorFullIntensity = color;
545 lineSet.intensities.assign(points.size() / 2, 0.);
547 topic->setLineSetVisu(layer(
id),
id.name, lineSet);
553 const VisuID&
id,
const std::vector<Eigen::Vector3f>& points,
float width,
554 const DrawColor& colorA,
const DrawColor& colorB,
const std::vector<float>& intensitiesB,
555 bool ignoreLengthScale)
559 const float scale = lengthScale(ignoreLengthScale);
561 DebugDrawerLineSet lineSet;
562 for (
const auto& point : points)
564 lineSet.points.push_back(scaledT<DebugDrawerPointCloudElement>(scale, point));
567 lineSet.lineWidth = width;
568 lineSet.colorNoIntensity = colorA;
569 lineSet.colorFullIntensity = colorB;
570 lineSet.intensities = intensitiesB;
572 topic->setLineSetVisu(layer(
id),
id.name, lineSet);
581 topic->removeLineSetVisu(layer(
id),
id.name);
589 drawPose(
id, pose, _poseScale, ignoreLengthScale);
596 bool ignoreLengthScale)
604 bool ignoreLengthScale)
608 const float lenghtScale = lengthScale(ignoreLengthScale);
610 if (scale >= 1 && scale <= 1)
612 topic->setPoseVisu(layer(
id),
id.name, scaled(lenghtScale, pose));
616 topic->setScaledPoseVisu(layer(
id),
id.name, scaled(lenghtScale, pose), scale);
625 float scale,
bool ignoreLengthScale)
635 topic->removePoseVisu(layer(
id),
id.name);
641 const std::string& robotFile,
const std::string& armarxProject,
646 topic->setRobotVisu(layer(
id),
id.name, robotFile, armarxProject, drawStyle);
657 topic->updateRobotPose(layer(
id),
id.name, scaled(lengthScale(ignoreScale), pose));
675 topic->updateRobotConfig(layer(
id),
id.name, config);
685 topic->updateRobotColor(layer(
id),
id.name, color);
692 const std::string& nodeName,
const DrawColor& color)
696 topic->updateRobotNodeColor(layer(
id),
id.name, nodeName, color);
705 topic->removeRobotVisu(layer(
id),
id.name);
711 const VisuID&
id,
const VirtualRobot::TriMeshModel& triMesh,
712 const DrawColor& color,
bool ignoreLengthScale)
719 const float scale = lengthScale(ignoreLengthScale);
721 DebugDrawerTriMesh dd;
722 dd.colors.push_back(color);
724 for (
const auto& vertex : triMesh.vertices)
726 auto scaled = vertex * scale;
727 dd.vertices.push_back({ scaled.x(), scaled.y(), scaled.z() });
730 const std::size_t normalBase = dd.vertices.size();
731 for (
const auto& normal : triMesh.normals)
733 dd.vertices.push_back({ normal.x(), normal.y(), normal.z() });
736 for (
const auto& face : triMesh.faces)
739 ddf.vertex1.vertexID =
static_cast<Ice::Int>(face.id1);
740 ddf.vertex2.vertexID =
static_cast<Ice::Int>(face.id2);
741 ddf.vertex3.vertexID =
static_cast<Ice::Int>(face.id3);
743 ddf.vertex1.colorID = ddf.vertex2.colorID = ddf.vertex3.colorID = 0;
744 ddf.vertex1.normalID = ddf.vertex2.normalID = ddf.vertex3.normalID = -1;
746 bool validNormalIDs =
true;
747 for (
const auto&
id :
749 face.idNormal1, face.idNormal2, face.idNormal3
752 validNormalIDs &=
id < triMesh.normals.size();
757 ddf.vertex1.normalID =
static_cast<Ice::Int>(normalBase + face.idNormal1);
758 ddf.vertex2.normalID =
static_cast<Ice::Int>(normalBase + face.idNormal2);
759 ddf.vertex3.normalID =
static_cast<Ice::Int>(normalBase + face.idNormal3);
763 const Eigen::Vector3f& normal = face.normal;
764 ddf.normal = { normal.x(), normal.y(), normal.z() };
767 dd.faces.push_back(ddf);
770 topic->setTriMeshVisu(layer(
id),
id.name, dd);
775 const VirtualRobot::TriMeshModel& trimesh,
776 const DrawColor& colorFace,
float lineWidth,
const DrawColor& colorEdge,
777 bool ignoreLengthScale)
780 colorFace, lineWidth, colorEdge, ignoreLengthScale);
785 const VirtualRobot::TriMeshModel& trimesh,
const Eigen::Matrix4f& pose,
786 const DrawColor& colorFace,
float lineWidth,
const DrawColor& colorEdge,
787 bool ignoreLengthScale)
794 const float scale = lengthScale(ignoreLengthScale);
795 bool isIdentity = pose.isIdentity();
797 auto toVector3 = [&scale, &isIdentity, &pose](
const Eigen::Vector3f &
v)
799 return scaled(scale, isIdentity ?
v : ::math::Helpers::TransformPosition(pose,
v));
805 for (std::size_t i = 0; i < trimesh.faces.size(); ++i)
807 const auto& face = trimesh.faces[i];
808 PolygonPointList points
810 toVector3(trimesh.vertices.at(face.id1)),
811 toVector3(trimesh.vertices.at(face.id2)),
812 toVector3(trimesh.vertices.at(face.id3))
815 topic->setPolygonVisu(layer(
id),
id.name +
"_" +
std::to_string(counter), points,
816 colorFace, colorEdge, lineWidth);
824 const VirtualRobot::TriMeshModel& trimesh,
825 const std::vector<DrawColor>& faceColorsInner,
float lineWidth,
826 const DrawColor& colorEdge,
bool ignoreLengthScale)
834 const float scale = lengthScale(ignoreLengthScale);
836 for (std::size_t i = 0; i < trimesh.faces.size(); ++i)
838 const auto& face = trimesh.faces[i];
839 PolygonPointList points
841 scaled(scale, trimesh.vertices[face.id1]),
842 scaled(scale, trimesh.vertices[face.id2]),
843 scaled(scale, trimesh.vertices[face.id3])
846 topic->setPolygonVisu(layer(
id),
id.name +
"_" +
std::to_string(i), points,
847 faceColorsInner.at(i), colorEdge, lineWidth);
853 const DebugDrawerPointCloud& pointCloud)
857 topic->setPointCloudVisu(
id.layer,
id.name, pointCloud);
863 const DebugDrawerColoredPointCloud& pointCloud)
867 topic->setColoredPointCloudVisu(
id.layer,
id.name, pointCloud);
873 const DebugDrawer24BitColoredPointCloud& pointCloud)
877 topic->set24BitColoredPointCloudVisu(
id.layer,
id.name, pointCloud);
889 const Eigen::Vector3f& at,
const Eigen::Vector3f& up,
float size,
890 const DrawColor& color,
bool ignoreLengthScale)
897 Eigen::Vector3f seed = seed.UnitX();
898 if (
std::abs(up.dot(seed)) <= 1e-6f)
910 const float halfSize = size / 2;
912 const Eigen::Vector3f
a = halfSize * up.cross(seed).normalized();
913 const Eigen::Vector3f b = halfSize * up.cross(
a).normalized();
915 std::vector<Eigen::Vector3f> points;
916 points.push_back(at +
a + b);
917 points.push_back(at +
a - b);
918 points.push_back(at -
a - b);
919 points.push_back(at -
a + b);
925 const std::string& DebugDrawerTopic::layer(
const std::string& passedLayer)
const
927 return passedLayer.empty() ? _layer : passedLayer;
930 const std::string& DebugDrawerTopic::layer(
const VisuID&
id)
const
932 return layer(
id.layer);
935 float DebugDrawerTopic::lengthScale(
bool ignore)
const
937 return ignore ? 1 : _lengthScale;
940 float DebugDrawerTopic::scaled(
float scale,
float value)
942 return scale *
value;
945 Vector3BasePtr DebugDrawerTopic::scaled(
float scale,
const Eigen::Vector3f& vector)
947 if (scale >= 1 && scale <= 1)
953 return new Vector3((vector * scale).eval());
957 PoseBasePtr DebugDrawerTopic::scaled(
float scale,
const Eigen::Matrix4f& pose)
959 if (scale >= 1 && scale <= 1)
961 return new Pose(pose);
967 return new Pose(out);
972 DebugDrawerTopic::operator bool()
const
999 return simox::color::rgb_to_hsv(rgb);
1004 return simox::color::hsv_to_rgb(hsv);