3 #include <SimoxUtility/color/hsv.h>
4 #include <VirtualRobot/Visualization/TriMeshModel.h>
5 #include <VirtualRobot/math/Helpers.h>
24 layer(layer), name(name)
31 return VisuID(this->layer, newName);
37 os <<
"Visu '" << rhs.
name <<
"' on layer '" << rhs.
layer <<
"'";
41 const std::string DebugDrawerTopic::TOPIC_NAME =
"DebugDrawerUpdates";
42 const std::string DebugDrawerTopic::DEFAULT_LAYER =
"debug";
50 const std::string& layer) :
76 return _enabled && topic;
81 const std::string& topicNameOverride)
const
83 component.offeringTopic(topicNameOverride.empty() ? TOPIC_NAME : topicNameOverride);
90 topicNameOverride.empty() ? TOPIC_NAME : topicNameOverride));
102 this->_layer = layer;
114 this->_lengthScale = scale;
138 this->_poseScale = scale;
156 this->
sleepFor(_shortSleepDuration);
183 topic->clearLayer(layer);
193 const Eigen::Vector3f& position,
194 const std::string& text,
196 const DrawColor color,
197 bool ignoreLengthScale)
201 const float scale = lengthScale(ignoreLengthScale);
202 topic->setTextVisu(layer(
id),
id.name, text, scaled(scale, position), color, size);
208 const Eigen::Vector3f& position,
210 const Eigen::Vector3f& extents,
211 const DrawColor& color,
212 bool ignoreLengthScale)
216 const float scale = lengthScale(ignoreLengthScale);
217 topic->setBoxVisu(layer(
id),
220 scaled(scale, extents),
228 const Eigen::Vector3f& extents,
229 const DrawColor& color,
230 bool ignoreLengthScale)
242 const VirtualRobot::BoundingBox& boundingBox,
243 const DrawColor& color,
244 bool ignoreLengthScale)
251 const VirtualRobot::BoundingBox& boundingBox,
253 const DrawColor& color,
254 bool ignoreLengthScale)
256 const Eigen::Vector3f
center = .5 * (boundingBox.getMin() + boundingBox.getMax());
258 ::math::Helpers::TransformPosition(pose,
center),
260 boundingBox.getMax() - boundingBox.getMin(),
270 topic->removeBoxVisu(layer(
id),
id.name);
276 const Eigen::Vector3f& position,
278 const Eigen::Vector3f& extents,
280 const DrawColor& color,
281 bool ignoreLengthScale)
294 const Eigen::Vector3f& extents,
296 const DrawColor& color,
297 bool ignoreLengthScale)
304 std::vector<Eigen::Vector3f> points;
307 bb.col(0) = -extents / 2;
308 bb.col(1) = extents / 2;
310 auto addLine = [&](
int x1,
int y1,
int z1,
int x2,
int y2,
int z2)
312 Eigen::Vector3f start = {bb.col(x1).x(), bb.col(y1).y(), bb.col(z1).z()};
313 Eigen::Vector3f end = {bb.col(x2).x(), bb.col(y2).y(), bb.col(z2).z()};
315 start = ::math::Helpers::TransformPosition(pose, start);
316 end = ::math::Helpers::TransformPosition(pose, end);
318 points.push_back(start);
319 points.push_back(end);
331 addLine(0, 0, 0, 1, 0, 0);
332 addLine(0, 0, 0, 0, 1, 0);
333 addLine(0, 0, 0, 0, 0, 1);
336 addLine(1, 1, 1, 0, 1, 1);
337 addLine(1, 1, 1, 1, 0, 1);
338 addLine(1, 1, 1, 1, 1, 0);
341 addLine(1, 0, 0, 1, 0, 1);
342 addLine(1, 0, 0, 1, 1, 0);
345 addLine(0, 1, 0, 1, 1, 0);
346 addLine(0, 1, 0, 0, 1, 1);
349 addLine(0, 0, 1, 1, 0, 1);
350 addLine(0, 0, 1, 0, 1, 1);
352 drawLineSet(
id, points, width, color, ignoreLengthScale);
357 const VirtualRobot::BoundingBox& boundingBox,
359 const DrawColor& color,
360 bool ignoreLengthScale)
369 const DrawColor& color,
370 bool ignoreLengthScale)
377 const VirtualRobot::BoundingBox& boundingBox,
380 const DrawColor& color,
381 bool ignoreLengthScale)
383 const Eigen::Vector3f
center = .5 * (boundingBox.getMin() + boundingBox.getMax());
388 boundingBox.getMax() - boundingBox.getMin(),
399 const DrawColor& color,
400 bool ignoreLengthScale)
402 const Eigen::Vector3f
center = 0.5 * (aabb.col(0) + aabb.col(1));
407 aabb.col(1) - aabb.col(0),
421 const Eigen::Vector3f&
center,
422 const Eigen::Vector3f& direction,
425 const DrawColor& color,
426 bool ignoreLengthScale)
430 const float scale = lengthScale(ignoreLengthScale);
431 topic->setCylinderVisu(layer(
id),
435 scaled(scale, length),
436 scaled(scale, radius),
443 const Eigen::Vector3f&
center,
447 const DrawColor& color,
448 bool ignoreLengthScale)
452 orientation * Eigen::Vector3f::UnitY(),
461 const Eigen::Vector3f&
from,
462 const Eigen::Vector3f& to,
464 const DrawColor& color,
465 bool ignoreLengthScale)
469 const Eigen::Vector3f dir = (to -
from);
470 const float length = dir.norm();
472 id, .5 * (
from + to), dir / length, radius, length, color, ignoreLengthScale);
481 topic->removeCylinderVisu(layer(
id),
id.name);
487 const Eigen::Vector3f&
center,
489 const DrawColor& color,
490 bool ignoreLengthScale)
494 const float scale = lengthScale(ignoreLengthScale);
495 topic->setSphereVisu(
496 layer(
id),
id.name, scaled(scale,
center), color, scaled(scale, radius));
505 topic->removeSphereVisu(layer(
id),
id.name);
511 const Eigen::Vector3f& position,
512 const Eigen::Vector3f& direction,
515 const DrawColor& color,
516 bool ignoreLengthScale)
520 const float scale = lengthScale(ignoreLengthScale);
521 topic->setArrowVisu(layer(
id),
523 scaled(scale, position),
526 scaled(scale, length),
527 scaled(scale, width));
533 const Eigen::Vector3f&
from,
534 const Eigen::Vector3f& to,
536 const DrawColor& color,
537 bool ignoreLengthScale)
541 const Eigen::Vector3f dir = (to -
from);
542 const float length = dir.norm();
543 drawArrow(
id,
from, dir / length, length, width, color, ignoreLengthScale);
552 topic->removeArrowVisu(layer(
id),
id.name);
558 const std::vector<Eigen::Vector3f>& points,
559 const DrawColor& colorFace,
561 const DrawColor& colorEdge,
562 bool ignoreLengthScale)
566 const float scale = lengthScale(ignoreLengthScale);
568 PolygonPointList polyPoints;
569 polyPoints.reserve(points.size());
571 for (
const auto& point : points)
573 polyPoints.push_back(scaled(scale, point));
576 topic->setPolygonVisu(layer(
id),
id.name, polyPoints, colorFace, colorEdge, lineWidth);
585 topic->removePolygonVisu(layer(
id),
id.name);
591 const Eigen::Vector3f&
from,
592 const Eigen::Vector3f& to,
594 const DrawColor& color,
595 bool ignoreLengthScale)
599 const float scale = lengthScale(ignoreLengthScale);
602 layer(
id),
id.name, scaled(scale,
from), scaled(scale, to), width, color);
611 topic->removeLineVisu(layer(
id),
id.name);
617 const DebugDrawerLineSet& lineSet,
618 bool ignoreLengthScale)
622 const float scale = lengthScale(ignoreLengthScale);
624 if (1.f <= scale && scale <= 1.f)
627 topic->setLineSetVisu(layer(
id),
id.name, lineSet);
632 DebugDrawerLineSet scaledLineSet = lineSet;
633 for (
auto& point : scaledLineSet.points)
635 scaleXYZ(scale, point);
637 topic->setLineSetVisu(layer(
id),
id.name, scaledLineSet);
644 const std::vector<Eigen::Vector3f>& points,
646 const DrawColor& color,
647 bool ignoreLengthScale)
651 const float scale = lengthScale(ignoreLengthScale);
653 DebugDrawerLineSet lineSet;
654 for (
const auto& point : points)
656 lineSet.points.push_back(scaledT<DebugDrawerPointCloudElement>(scale, point));
659 lineSet.lineWidth = width;
660 lineSet.colorNoIntensity = lineSet.colorFullIntensity = color;
661 lineSet.intensities.assign(points.size() / 2, 0.);
663 topic->setLineSetVisu(layer(
id),
id.name, lineSet);
669 const std::vector<Eigen::Vector3f>& points,
671 const DrawColor& colorA,
672 const DrawColor& colorB,
673 const std::vector<float>& intensitiesB,
674 bool ignoreLengthScale)
678 const float scale = lengthScale(ignoreLengthScale);
680 DebugDrawerLineSet lineSet;
681 for (
const auto& point : points)
683 lineSet.points.push_back(scaledT<DebugDrawerPointCloudElement>(scale, point));
686 lineSet.lineWidth = width;
687 lineSet.colorNoIntensity = colorA;
688 lineSet.colorFullIntensity = colorB;
689 lineSet.intensities = intensitiesB;
691 topic->setLineSetVisu(layer(
id),
id.name, lineSet);
700 topic->removeLineSetVisu(layer(
id),
id.name);
707 bool ignoreLengthScale)
709 drawPose(
id, pose, _poseScale, ignoreLengthScale);
714 const Eigen::Vector3f& pos,
716 bool ignoreLengthScale)
725 bool ignoreLengthScale)
729 const float lenghtScale = lengthScale(ignoreLengthScale);
731 if (scale >= 1 && scale <= 1)
733 topic->setPoseVisu(layer(
id),
id.name, scaled(lenghtScale, pose));
737 topic->setScaledPoseVisu(layer(
id),
id.name, scaled(lenghtScale, pose), scale);
744 const Eigen::Vector3f& pos,
747 bool ignoreLengthScale)
757 topic->removePoseVisu(layer(
id),
id.name);
763 const std::string& robotFile,
764 const std::string& armarxProject,
769 topic->setRobotVisu(layer(
id),
id.name, robotFile, armarxProject, drawStyle);
780 topic->updateRobotPose(layer(
id),
id.name, scaled(lengthScale(ignoreScale), pose));
786 const Eigen::Vector3f& pos,
795 const std::map<std::string, float>& config)
799 topic->updateRobotConfig(layer(
id),
id.name, config);
808 topic->updateRobotColor(layer(
id),
id.name, color);
814 const std::string& nodeName,
815 const DrawColor& color)
819 topic->updateRobotNodeColor(layer(
id),
id.name, nodeName, color);
828 topic->removeRobotVisu(layer(
id),
id.name);
834 const VirtualRobot::TriMeshModel& triMesh,
835 const DrawColor& color,
836 bool ignoreLengthScale)
843 const float scale = lengthScale(ignoreLengthScale);
845 DebugDrawerTriMesh dd;
846 dd.colors.push_back(color);
848 for (
const auto& vertex : triMesh.vertices)
850 auto scaled = vertex * scale;
851 dd.vertices.push_back({scaled.x(), scaled.y(), scaled.z()});
854 const std::size_t normalBase = dd.vertices.size();
855 for (
const auto& normal : triMesh.normals)
857 dd.vertices.push_back({normal.x(), normal.y(), normal.z()});
860 for (
const auto& face : triMesh.faces)
863 ddf.vertex1.vertexID =
static_cast<Ice::Int>(face.id1);
864 ddf.vertex2.vertexID =
static_cast<Ice::Int>(face.id2);
865 ddf.vertex3.vertexID =
static_cast<Ice::Int>(face.id3);
867 ddf.vertex1.colorID = ddf.vertex2.colorID = ddf.vertex3.colorID = 0;
868 ddf.vertex1.normalID = ddf.vertex2.normalID = ddf.vertex3.normalID = -1;
870 bool validNormalIDs =
true;
871 for (
const auto&
id : {face.idNormal1, face.idNormal2, face.idNormal3})
873 validNormalIDs &=
id < triMesh.normals.size();
878 ddf.vertex1.normalID =
static_cast<Ice::Int>(normalBase + face.idNormal1);
879 ddf.vertex2.normalID =
static_cast<Ice::Int>(normalBase + face.idNormal2);
880 ddf.vertex3.normalID =
static_cast<Ice::Int>(normalBase + face.idNormal3);
884 const Eigen::Vector3f& normal = face.normal;
885 ddf.normal = {normal.x(), normal.y(), normal.z()};
888 dd.faces.push_back(ddf);
891 topic->setTriMeshVisu(layer(
id),
id.name, dd);
896 const VirtualRobot::TriMeshModel& trimesh,
897 const DrawColor& colorFace,
899 const DrawColor& colorEdge,
900 bool ignoreLengthScale)
913 const VirtualRobot::TriMeshModel& trimesh,
915 const DrawColor& colorFace,
917 const DrawColor& colorEdge,
918 bool ignoreLengthScale)
925 const float scale = lengthScale(ignoreLengthScale);
926 bool isIdentity = pose.isIdentity();
928 auto toVector3 = [&scale, &isIdentity, &pose](
const Eigen::Vector3f&
v)
929 {
return scaled(scale, isIdentity ?
v : ::math::Helpers::TransformPosition(pose,
v)); };
934 for (std::size_t i = 0; i < trimesh.faces.size(); ++i)
936 const auto& face = trimesh.faces[i];
937 PolygonPointList points{toVector3(trimesh.vertices.at(face.id1)),
938 toVector3(trimesh.vertices.at(face.id2)),
939 toVector3(trimesh.vertices.at(face.id3))};
941 topic->setPolygonVisu(layer(
id),
953 const VirtualRobot::TriMeshModel& trimesh,
954 const std::vector<DrawColor>& faceColorsInner,
956 const DrawColor& colorEdge,
957 bool ignoreLengthScale)
965 const float scale = lengthScale(ignoreLengthScale);
967 for (std::size_t i = 0; i < trimesh.faces.size(); ++i)
969 const auto& face = trimesh.faces[i];
970 PolygonPointList points{scaled(scale, trimesh.vertices[face.id1]),
971 scaled(scale, trimesh.vertices[face.id2]),
972 scaled(scale, trimesh.vertices[face.id3])};
974 topic->setPolygonVisu(layer(
id),
977 faceColorsInner.at(i),
985 const DebugDrawerPointCloud& pointCloud)
989 topic->setPointCloudVisu(
id.layer,
id.name, pointCloud);
995 const DebugDrawerColoredPointCloud& pointCloud)
999 topic->setColoredPointCloudVisu(
id.layer,
id.name, pointCloud);
1005 const DebugDrawer24BitColoredPointCloud& pointCloud)
1009 topic->set24BitColoredPointCloudVisu(
id.layer,
id.name, pointCloud);
1022 const Eigen::Vector3f& at,
1023 const Eigen::Vector3f&
up,
1025 const DrawColor& color,
1026 bool ignoreLengthScale)
1033 Eigen::Vector3f seed = seed.UnitX();
1036 seed = seed.UnitY();
1046 const float halfSize = size / 2;
1048 const Eigen::Vector3f
a = halfSize *
up.cross(seed).normalized();
1049 const Eigen::Vector3f b = halfSize *
up.cross(
a).normalized();
1051 std::vector<Eigen::Vector3f> points;
1052 points.push_back(at +
a + b);
1053 points.push_back(at +
a - b);
1054 points.push_back(at -
a - b);
1055 points.push_back(at -
a + b);
1057 drawPolygon(
id, points, color, ignoreLengthScale);
1061 DebugDrawerTopic::layer(
const std::string& passedLayer)
const
1063 return passedLayer.empty() ? _layer : passedLayer;
1067 DebugDrawerTopic::layer(
const VisuID&
id)
const
1069 return layer(
id.layer);
1073 DebugDrawerTopic::lengthScale(
bool ignore)
const
1075 return ignore ? 1 : _lengthScale;
1079 DebugDrawerTopic::scaled(
float scale,
float value)
1081 return scale *
value;
1085 DebugDrawerTopic::scaled(
float scale,
const Eigen::Vector3f& vector)
1087 if (scale >= 1 && scale <= 1)
1093 return new Vector3((vector * scale).eval());
1100 if (scale >= 1 && scale <= 1)
1102 return new Pose(pose);
1108 return new Pose(out);
1112 DebugDrawerTopic::operator bool()
const
1142 return simox::color::rgb_to_hsv(rgb);
1148 return simox::color::hsv_to_rgb(hsv);