DebugDrawerHelper.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5 * Karlsruhe Institute of Technology (KIT), all rights reserved.
6 *
7 * ArmarX is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * ArmarX is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24#include "DebugDrawerHelper.h"
25
26#include <VirtualRobot/Nodes/RobotNode.h>
27#include <VirtualRobot/Robot.h>
28#include <VirtualRobot/math/Helpers.h>
29
32
34
35using namespace math;
36
38{
39 FrameView::FrameView(class DebugDrawerHelper& helper, const Eigen::Matrix4f frame) :
40 _helper{&helper}, _fallbackFrame{frame}
41 {
42 }
43
44 FrameView::FrameView(class DebugDrawerHelper& helper, const VirtualRobot::RobotNodePtr& rn) :
45 _helper{&helper}, _rn{rn}
46 {
47 }
48
49 //make global
50 Eigen::Matrix4f
51 FrameView::makeGlobalEigen(const Eigen::Matrix4f& pose) const
52 {
54 if (_rn)
55 {
56 return _rn->getGlobalPose(pose);
57 }
58 return _fallbackFrame * pose;
59 }
60
61 Eigen::Vector3f
62 FrameView::makeGlobalEigen(const Eigen::Vector3f& position) const
63 {
65 if (_rn)
66 {
67 return _rn->getGlobalPosition(position);
68 }
69 return (_fallbackFrame * position.homogeneous()).eval().hnormalized();
70 }
71
72 Eigen::Vector3f
73 FrameView::makeGlobalDirectionEigen(const Eigen::Vector3f& direction) const
74 {
76 if (_rn)
77 {
78 return ::math::Helpers::TransformDirection(_rn->getGlobalPose(), direction);
79 }
80 return ::math::Helpers::TransformDirection(_fallbackFrame, direction);
81 }
82
84 FrameView::makeGlobal(const Eigen::Matrix4f& pose) const
85 {
87 return new Pose(makeGlobalEigen(pose));
88 }
89
91 FrameView::makeGlobal(const Eigen::Vector3f& position) const
92 {
94 return new Vector3(makeGlobalEigen(position));
95 }
96
98 FrameView::makeGlobalDirection(const Eigen::Vector3f& direction) const
99 {
101 return new Vector3(makeGlobalDirectionEigen(direction));
102 }
103
104 //1st order
106#define CHECK_AND_ADD(name, type) \
107 if (!_helper->enableVisu) \
108 { \
109 return; \
110 } \
111 _helper->addNewElement(name, type);
112
113 void
114 FrameView::drawPose(const std::string& name, const Eigen::Matrix4f& pose)
115 {
117 CHECK_AND_ADD(name, DrawElementType::Pose)
118 _helper->debugDrawerPrx->setPoseVisu(_helper->layerName, name, makeGlobal(pose));
119 }
120
121 void
122 FrameView::drawPose(const std::string& name, const Eigen::Matrix4f& pose, float scale)
123 {
125 CHECK_AND_ADD(name, DrawElementType::Pose)
126 _helper->debugDrawerPrx->setScaledPoseVisu(
127 _helper->layerName, name, makeGlobal(pose), scale);
128 }
129
130 void
131 FrameView::drawBox(const std::string& name,
132 const Eigen::Matrix4f& pose,
133 const Eigen::Vector3f& size,
134 const DrawColor& color)
135 {
137 CHECK_AND_ADD(name, DrawElementType::Box)
138 _helper->debugDrawerPrx->setBoxVisu(
139 _helper->layerName, name, makeGlobal(pose), new Vector3(size), color);
140 }
141
142 void
143 FrameView::drawLine(const std::string& name,
144 const Eigen::Vector3f& p1,
145 const Eigen::Vector3f& p2,
146 float width,
147 const DrawColor& color)
148 {
150 CHECK_AND_ADD(name, DrawElementType::Line)
151 _helper->debugDrawerPrx->setLineVisu(
152 _helper->layerName, name, makeGlobal(p1), makeGlobal(p2), width, color);
153 }
154
155 void
156 FrameView::drawText(const std::string& name,
157 const Eigen::Vector3f& p1,
158 const std::string& text,
159 const DrawColor& color,
160 int size)
161 {
163 CHECK_AND_ADD(name, DrawElementType::Text)
164 _helper->debugDrawerPrx->setTextVisu(
165 _helper->layerName, name, text, makeGlobal(p1), color, size);
166 }
167
168 void
169 FrameView::drawArrow(const std::string& name,
170 const Eigen::Vector3f& pos,
171 const Eigen::Vector3f& direction,
172 const DrawColor& color,
173 float length,
174 float width)
175 {
177 CHECK_AND_ADD(name, DrawElementType::Arrow)
178 _helper->debugDrawerPrx->setArrowVisu(_helper->layerName,
179 name,
180 makeGlobal(pos),
181 makeGlobalDirection(direction),
182 color,
183 length,
184 width);
185 }
186
187 void
188 FrameView::drawSphere(const std::string& name,
189 const Eigen::Vector3f& position,
190 float size,
191 const DrawColor& color)
192 {
194 _helper->debugDrawerPrx->setSphereVisu(
195 _helper->layerName, name, makeGlobal(position), color, size);
196 }
197
198 void
199 FrameView::drawPointCloud(const std::string& name,
200 const std::vector<Eigen::Vector3f>& points,
201 float pointSize,
202 const DrawColor& color)
203 {
205 CHECK_AND_ADD(name, DrawElementType::ColoredPointCloud)
206 DebugDrawerColoredPointCloud pc;
207 pc.pointSize = pointSize;
208 for (const Eigen::Vector3f& p : points)
209 {
210 Eigen::Vector3f pg = makeGlobalEigen(p);
211 DebugDrawerColoredPointCloudElement e;
212 e.x = pg(0);
213 e.y = pg(1);
214 e.z = pg(2);
215 e.color =
216 color; //DrawColor24Bit {(Ice::Byte)(color.r * 255), (Ice::Byte)(color.g * 255), (Ice::Byte)(color.b * 255)};
217 pc.points.push_back(e);
218 }
219 _helper->debugDrawerPrx->setColoredPointCloudVisu(_helper->layerName, name, pc);
220 }
221
222 void
223 FrameView::drawRobot(const std::string& name,
224 const std::string& robotFile,
225 const std::string& armarxProject,
226 const Eigen::Matrix4f& pose,
227 const DrawColor& color)
228 {
230 CHECK_AND_ADD(name, DrawElementType::Robot)
231 _helper->debugDrawerPrx->setRobotVisu(
232 _helper->layerName, name, robotFile, armarxProject, DrawStyle::FullModel);
233 _helper->debugDrawerPrx->updateRobotPose(_helper->layerName, name, makeGlobal(pose));
234 _helper->debugDrawerPrx->updateRobotColor(_helper->layerName, name, color);
235 }
236
237 void
238 FrameView::setRobotConfig(const std::string& name, const std::map<std::string, float>& config)
239 {
241 _helper->debugDrawerPrx->updateRobotConfig(_helper->layerName, name, config);
242 }
243
244 void
245 FrameView::setRobotColor(const std::string& name, const DrawColor& color)
246 {
248 _helper->debugDrawerPrx->updateRobotColor(_helper->layerName, name, color);
249 }
250
251 void
252 FrameView::setRobotPose(const std::string& name, const Eigen::Matrix4f& pose)
253 {
255 _helper->debugDrawerPrx->updateRobotPose(_helper->layerName, name, makeGlobal(pose));
256 }
257
258#undef CHECK_AND_ADD
259
260 //2nd order
261 void
262 FrameView::drawPoses(const std::string& prefix, const std::vector<Eigen::Matrix4f>& poses)
263 {
265 for (std::size_t idx = 0; idx < poses.size(); ++idx)
266 {
267 Eigen::Matrix4f const& pose = poses[idx];
268 drawPose(prefix + std::to_string(idx), pose);
269 }
270 }
271
272 void
273 FrameView::drawPoses(const std::string& prefix,
274 const std::vector<Eigen::Matrix4f>& poses,
275 float scale)
276 {
278 for (std::size_t idx = 0; idx < poses.size(); ++idx)
279 {
280 Eigen::Matrix4f const& pose = poses[idx];
281 drawPose(prefix + std::to_string(idx), pose, scale);
282 }
283 }
284
285 void
286 FrameView::drawBox(const std::string& name,
287 const Eigen::Vector3f& position,
288 float size,
289 const DrawColor& color)
290 {
292 drawBox(name,
293 Helpers::CreatePose(position, Eigen::Matrix3f::Identity()),
294 Eigen::Vector3f(size, size, size),
295 color);
296 }
297
298 void
299 FrameView::drawBox(const std::string& name,
300 const Eigen::Matrix4f& pose,
301 float size,
302 const DrawColor& color)
303 {
305 drawBox(name, pose, Eigen::Vector3f(size, size, size), color);
306 }
307
308 void
309 FrameView::drawLine(const std::string& name,
310 const Eigen::Vector3f& p1,
311 const Eigen::Vector3f& p2)
312 {
314 drawLine(name, p1, p2, _helper->defaults.lineWidth, _helper->defaults.lineColor);
315 }
316
317 void
318 FrameView::drawLines(const std::string& prefix,
319 const std::vector<Eigen::Vector3f>& ps,
320 float width,
321 const DrawColor& color)
322 {
324 for (std::size_t idx = 1; idx < ps.size(); ++idx)
325 {
326 drawLine(prefix + std::to_string(idx), ps.at(idx - 1), ps.at(idx), width, color);
327 }
328 }
329
330 void
331 FrameView::drawLines(const std::string& prefix, const std::vector<Eigen::Vector3f>& ps)
332 {
334 for (std::size_t idx = 1; idx < ps.size(); ++idx)
335 {
336 drawLine(prefix + std::to_string(idx), ps.at(idx - 1), ps.at(idx));
337 }
338 }
339
340 void
341 FrameView::drawLines(const std::string& prefix,
342 const std::vector<Eigen::Matrix4f>& ps,
343 float width,
344 const DrawColor& color)
345 {
347 for (std::size_t idx = 1; idx < ps.size(); ++idx)
348 {
349 drawLine(prefix + std::to_string(idx),
350 ps.at(idx - 1).topRightCorner<3, 1>(),
351 ps.at(idx).topRightCorner<3, 1>(),
352 width,
353 color);
354 }
355 }
356
357 void
358 FrameView::drawLines(const std::string& prefix, const std::vector<Eigen::Matrix4f>& ps)
359 {
361 for (std::size_t idx = 1; idx < ps.size(); ++idx)
362 {
363 drawLine(prefix + std::to_string(idx),
364 ps.at(idx - 1).topRightCorner<3, 1>(),
365 ps.at(idx).topRightCorner<3, 1>());
366 }
367 }
368} // namespace armarx::detail::DebugDrawerHelper
369
370namespace armarx
371{
373 const std::string& layerName,
374 const VirtualRobot::RobotPtr& robot) :
375 FrameView(*this, robot ? robot->getRootNode() : nullptr),
376 debugDrawerPrx(debugDrawerPrx),
377 layerName(layerName),
378 robot(robot)
379 {
380 ARMARX_CHECK_NOT_NULL(debugDrawerPrx);
381 }
382
383 DebugDrawerHelper::DebugDrawerHelper(const std::string& layerName) :
384 FrameView(*this, nullptr), layerName{layerName}
385 {
386 }
387
388 //utility
389 void
391 {
393 debugDrawerPrx->clearLayer(layerName);
394 oldElements.clear();
395 newElements.clear();
396 }
397
398 void
400 {
402 /*
403 // debug code...
404 std::stringstream ss;
405 ss << "OLD: ";
406 for (const DrawElement& e : oldElements)
407 {
408 ss << e.name << ",";
409 }
410 ss << " NEW: ";
411 for (const DrawElement& e : newElements)
412 {
413 ss << e.name << ",";
414 }
415 ARMARX_IMPORTANT << ss.str();*/
416
417 for (const DrawElement& old : oldElements)
418 {
419 if (newElements.find(old) == newElements.end())
420 {
421 //ARMARX_IMPORTANT << "removing " << old.name;
422 removeElement(old.name, old.type);
423 }
424 }
425 oldElements = newElements;
426 newElements.clear();
427 }
428
429 void
431 {
432 this->enableVisu = enableVisu;
433 }
434
435 void
436 DebugDrawerHelper::removeElement(const std::string& name,
438 {
440 switch (type)
441 {
443 debugDrawerPrx->removePoseVisu(layerName, name);
444 return;
446 debugDrawerPrx->removeBoxVisu(layerName, name);
447 return;
449 debugDrawerPrx->removeLineVisu(layerName, name);
450 return;
452 debugDrawerPrx->removeTextVisu(layerName, name);
453 return;
455 debugDrawerPrx->removeArrowVisu(layerName, name);
456 return;
458 debugDrawerPrx->removeColoredPointCloudVisu(layerName, name);
459 return;
461 debugDrawerPrx->removeRobotVisu(layerName, name);
462 return;
463 }
464 }
465
468 {
469 return robot;
470 }
471
474 {
475 return debugDrawerPrx;
476 }
477
478 void
480 {
481 ARMARX_CHECK_NOT_NULL(drawer);
482 debugDrawerPrx = drawer;
483 }
484
485 void
487 {
488 robot = rob;
489 }
490
491 DebugDrawerHelper::FrameView
492 DebugDrawerHelper::inFrame(const Eigen::Matrix4f& frame)
493 {
494 return {*this, frame};
495 }
496
497 DebugDrawerHelper::FrameView
499 {
500 return {*this, Eigen::Matrix4f::Identity()};
501 }
502
503 DebugDrawerHelper::FrameView
504 DebugDrawerHelper::inFrame(const std::string& nodeName)
505 {
508 ARMARX_CHECK_EXPRESSION(robot->hasRobotNode(nodeName));
509 return {*this, robot->getRobotNode(nodeName)};
510 }
511
512 DebugDrawerHelper::FrameView
513 DebugDrawerHelper::inFrame(const VirtualRobot::RobotNodePtr& node)
514 {
517 return {*this, node};
518 }
519
520 void
521 DebugDrawerHelper::setDefaultFrame(const Eigen::Matrix4f& frame)
522 {
524 _rn = nullptr;
525 _fallbackFrame = frame;
526 }
527
528 void
530 {
532 _rn = nullptr;
533 _fallbackFrame = Eigen::Matrix4f::Identity();
534 }
535
536 void
537 DebugDrawerHelper::setDefaultFrame(const std::string& nodeName)
538 {
541 ARMARX_CHECK_EXPRESSION(robot->hasRobotNode(nodeName));
542 _rn = robot->getRobotNode(nodeName);
543 }
544
545 void
546 DebugDrawerHelper::setDefaultFrame(const VirtualRobot::RobotNodePtr& node)
547 {
550 ARMARX_CHECK_EXPRESSION(robot == node->getRobot());
551 _rn = node;
552 }
553
554 void
555 DebugDrawerHelper::addNewElement(const std::string& name,
557 {
559 newElements.insert(DrawElement{name, type});
560 }
561
562 const DrawColor DebugDrawerHelper::Colors::Red = DrawColor{1, 0, 0, 1};
563 const DrawColor DebugDrawerHelper::Colors::Green = DrawColor{0, 1, 0, 1};
564 const DrawColor DebugDrawerHelper::Colors::Blue = DrawColor{0, 0, 1, 1};
565 const DrawColor DebugDrawerHelper::Colors::Yellow = DrawColor{1, 1, 0, 1};
566 const DrawColor DebugDrawerHelper::Colors::Orange = DrawColor{1, 0.5f, 0, 1};
567
568 DrawColor
569 DebugDrawerHelper::Colors::Lerp(const DrawColor& a, const DrawColor& b, float f)
570 {
571 return DrawColor{::math::Helpers::Lerp(a.r, b.r, f),
572 ::math::Helpers::Lerp(a.g, b.g, f),
573 ::math::Helpers::Lerp(a.b, b.b, f),
574 ::math::Helpers::Lerp(a.a, b.a, f)};
575 }
576} // namespace armarx
#define CHECK_AND_ADD(name, type)
static DrawColor Lerp(const DrawColor &a, const DrawColor &b, float f)
DebugDrawerHelper(const DebugDrawerInterfacePrx &debugDrawerPrx, const std::string &layerName, const VirtualRobot::RobotPtr &robot)
void removeElement(const std::string &name, DrawElementType type)
const DebugDrawerInterfacePrx & getDebugDrawer() const
void setVisuEnabled(bool enableVisu)
FrameView inFrame(const Eigen::Matrix4f &frame=Eigen::Matrix4f::Identity())
void setDebugDrawer(const DebugDrawerInterfacePrx &drawer)
void setRobot(const VirtualRobot::RobotPtr &rob)
const VirtualRobot::RobotPtr & getRobot() const
void setDefaultFrame(const Eigen::Matrix4f &frame=Eigen::Matrix4f::Identity())
The Pose class.
Definition Pose.h:243
The Vector3 class.
Definition Pose.h:113
void drawLine(const std::string &name, const Eigen::Vector3f &p1, const Eigen::Vector3f &p2, float width, const DrawColor &color)
FrameView(class DebugDrawerHelper &helper, const Eigen::Matrix4f frame=Eigen::Matrix4f::Identity())
void setRobotConfig(const std::string &name, const std::map< std::string, float > &config)
void drawText(const std::string &name, const Eigen::Vector3f &p1, const std::string &text, const DrawColor &color, int size)
void drawLines(const std::string &prefix, const std::vector< Eigen::Vector3f > &ps, float width, const DrawColor &color)
void drawBox(const std::string &name, const Eigen::Matrix4f &pose, const Eigen::Vector3f &size, const DrawColor &color)
void drawArrow(const std::string &name, const Eigen::Vector3f &pos, const Eigen::Vector3f &direction, const DrawColor &color, float length, float width)
Eigen::Matrix4f makeGlobalEigen(const Eigen::Matrix4f &pose) const
void setRobotPose(const std::string &name, const Eigen::Matrix4f &pose)
void drawRobot(const std::string &name, const std::string &robotFile, const std::string &armarxProject, const Eigen::Matrix4f &pose, const DrawColor &color)
void drawPointCloud(const std::string &name, const std::vector< Eigen::Vector3f > &points, float pointSize, const DrawColor &color)
void drawPoses(const std::string &prefix, const std::vector< Eigen::Matrix4f > &poses)
void drawPose(const std::string &name, const Eigen::Matrix4f &pose)
Vector3Ptr makeGlobalDirection(const Eigen::Vector3f &direction) const
void drawSphere(const std::string &name, const Eigen::Vector3f &position, float size, const DrawColor &color)
PosePtr makeGlobal(const Eigen::Matrix4f &pose) const
void setRobotColor(const std::string &name, const DrawColor &color)
Eigen::Vector3f makeGlobalDirectionEigen(const Eigen::Vector3f &direction) const
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
::armarx::DebugDrawerHelper::DrawElementType DrawElementType
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< Vector3 > Vector3Ptr
Definition Pose.h:165
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
#define ARMARX_TRACE
Definition trace.h:77