-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from gergondet/topic/AddVisual
Add support for Visual element
- Loading branch information
Showing
6 changed files
with
211 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include "VisualWidget.h" | ||
|
||
namespace mc_rtc_rviz | ||
{ | ||
|
||
VisualWidget::VisualWidget(const ClientWidgetParam & params, visualization_msgs::MarkerArray & markers) | ||
: ClientWidget(params), markers_(markers), visible_(visible()), was_visible_(visible_) | ||
{ | ||
auto layout = new QHBoxLayout(this); | ||
if(!secret()) | ||
{ | ||
layout->addWidget(new QLabel(id().name.c_str())); | ||
} | ||
button_ = new QPushButton(this); | ||
button_->setCheckable(true); | ||
button_->setChecked(!visible()); | ||
toggled(!visible_); | ||
layout->addWidget(button_); | ||
connect(button_, SIGNAL(toggled(bool)), this, SLOT(toggled(bool))); | ||
marker_.ns = id2name(id()); | ||
marker_.header.frame_id = "robot_map"; | ||
marker_.header.seq = 0; | ||
} | ||
|
||
VisualWidget::~VisualWidget() | ||
{ | ||
marker_.action = visualization_msgs::Marker::DELETE; | ||
markers_.markers.push_back(marker_); | ||
} | ||
|
||
void VisualWidget::update(const rbd::parsers::Visual & visual, const sva::PTransformd & pose) | ||
{ | ||
{ | ||
// Marker general settings | ||
marker_.header.seq++; | ||
marker_.header.stamp = ros::Time::now(); | ||
marker_.action = visualization_msgs::Marker::ADD; | ||
} | ||
{ | ||
// Convert the position | ||
auto X_0_marker = visual.origin * pose; | ||
geometry_msgs::Pose p; | ||
Eigen::Quaterniond q{X_0_marker.rotation().transpose()}; | ||
p.orientation.w = q.w(); | ||
p.orientation.x = q.x(); | ||
p.orientation.y = q.y(); | ||
p.orientation.z = q.z(); | ||
const auto & t = X_0_marker.translation(); | ||
p.position.x = t.x(); | ||
p.position.y = t.y(); | ||
p.position.z = t.z(); | ||
marker_.pose = p; | ||
} | ||
{ | ||
// Color from material, defaults to white | ||
if(visual.material.type == rbd::parsers::Material::Type::COLOR) | ||
{ | ||
auto & c = boost::get<rbd::parsers::Material::Color>(visual.material.data); | ||
marker_.color.r = c.r; | ||
marker_.color.g = c.g; | ||
marker_.color.b = c.b; | ||
marker_.color.a = c.a; | ||
} | ||
else | ||
{ | ||
marker_.color.r = 1.0; | ||
marker_.color.g = 1.0; | ||
marker_.color.b = 1.0; | ||
marker_.color.a = 1.0; | ||
} | ||
} | ||
{ | ||
// Convert geometry | ||
using Geometry = rbd::parsers::Geometry; | ||
using Type = Geometry::Type; | ||
auto & type = visual.geometry.type; | ||
auto & data = visual.geometry.data; | ||
if(type == Type::BOX) | ||
{ | ||
auto & b = boost::get<Geometry::Box>(data); | ||
marker_.type = visualization_msgs::Marker::CUBE; | ||
marker_.scale.x = b.size.x(); | ||
marker_.scale.y = b.size.y(); | ||
marker_.scale.z = b.size.z(); | ||
} | ||
else if(type == Type::CYLINDER) | ||
{ | ||
auto & c = boost::get<Geometry::Cylinder>(data); | ||
marker_.type = visualization_msgs::Marker::CYLINDER; | ||
marker_.scale.x = 2 * c.radius; | ||
marker_.scale.y = 2 * c.radius; | ||
marker_.scale.z = c.length; | ||
} | ||
else if(type == Type::SPHERE) | ||
{ | ||
auto & s = boost::get<Geometry::Sphere>(data); | ||
marker_.type = visualization_msgs::Marker::SPHERE; | ||
marker_.scale.x = 2 * s.radius; | ||
marker_.scale.y = 2 * s.radius; | ||
marker_.scale.z = 2 * s.radius; | ||
} | ||
else if(type == Type::MESH) | ||
{ | ||
auto & m = boost::get<Geometry::Mesh>(data); | ||
marker_.type = visualization_msgs::Marker::MESH_RESOURCE; | ||
marker_.scale.x = m.scale; | ||
marker_.scale.y = m.scale; | ||
marker_.scale.z = m.scale; | ||
marker_.mesh_resource = m.filename; | ||
marker_.mesh_use_embedded_materials = true; | ||
} | ||
} | ||
if(visible_ || was_visible_) | ||
{ | ||
if(!visible_) | ||
{ | ||
marker_.action = visualization_msgs::Marker::DELETE; | ||
} | ||
markers_.markers.push_back(marker_); | ||
} | ||
was_visible_ = visible_; | ||
} | ||
|
||
void VisualWidget::toggled(bool hide) | ||
{ | ||
visible_ = !hide; | ||
button_->setText(hide ? "Show" : "Hide"); | ||
visible(!hide); | ||
} | ||
|
||
} // namespace mc_rtc_rviz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2021 CNRS-UM LIRMM, CNRS-AIST JRL | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "ClientWidget.h" | ||
#include "utils.h" | ||
|
||
#include <visualization_msgs/MarkerArray.h> | ||
|
||
namespace mc_rtc_rviz | ||
{ | ||
|
||
struct VisualWidget : public ClientWidget | ||
{ | ||
Q_OBJECT | ||
public: | ||
VisualWidget(const ClientWidgetParam & params, visualization_msgs::MarkerArray & markers); | ||
|
||
~VisualWidget() override; | ||
|
||
void update(const rbd::parsers::Visual & visual, const sva::PTransformd & pose); | ||
|
||
private: | ||
visualization_msgs::MarkerArray & markers_; | ||
visualization_msgs::Marker marker_; | ||
bool visible_; | ||
bool was_visible_; | ||
QPushButton * button_; | ||
private slots: | ||
void toggled(bool); | ||
}; | ||
|
||
} // namespace mc_rtc_rviz |