Skip to content

Commit 9e8b0ee

Browse files
committed
Replace implementation of createInertia that relies on gz_math with an implementation that uses Eigen and thus removes the gz_math dependency
1 parent 29ce976 commit 9e8b0ee

File tree

3 files changed

+55
-50
lines changed

3 files changed

+55
-50
lines changed

rviz_default_plugins/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ if(${QT_VERSION} VERSION_LESS 5.15.0)
6666
endfunction()
6767
endif()
6868

69-
find_package(geometry_msgs REQUIRED)
70-
71-
find_package(gz_math_vendor REQUIRED)
72-
find_package(gz-math REQUIRED)
69+
find_package(eigen3_cmake_module REQUIRED)
70+
find_package(Eigen3 REQUIRED)
7371

72+
find_package(geometry_msgs REQUIRED)
7473
find_package(image_transport REQUIRED)
7574
find_package(interactive_markers REQUIRED)
7675
find_package(laser_geometry REQUIRED)
@@ -278,7 +277,7 @@ target_link_libraries(rviz_default_plugins PUBLIC
278277
)
279278

280279
target_link_libraries(rviz_default_plugins PRIVATE
281-
gz-math::core
280+
Eigen3::Eigen
282281
)
283282

284283
# Causes the visibility macros to use dllexport rather than dllimport,

rviz_default_plugins/package.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
<author email="[email protected]">William Woodall</author>
2626

2727
<buildtool_depend>ament_cmake_ros</buildtool_depend>
28+
<buildtool_depend>eigen3_cmake_module</buildtool_depend>
2829

2930
<build_depend>qtbase5-dev</build_depend>
3031
<build_depend>rviz_ogre_vendor</build_depend>
32+
<build_depend>eigen</build_depend>
3133

3234
<build_export_depend>rviz_ogre_vendor</build_export_depend>
3335

@@ -38,7 +40,6 @@
3840
<exec_depend>rviz_ogre_vendor</exec_depend>
3941

4042
<depend>geometry_msgs</depend>
41-
<depend>gz_math_vendor</depend>
4243
<depend>image_transport</depend>
4344
<depend>interactive_markers</depend>
4445
<depend>laser_geometry</depend>

rviz_default_plugins/src/rviz_default_plugins/robot/robot_link.cpp

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#include <string>
3939
#include <vector>
4040

41+
#include <Eigen/Eigenvalues>
42+
#include <Eigen/Geometry>
43+
4144
#include <OgreEntity.h>
4245
#include <OgreMaterial.h>
4346
#include <OgreMaterialManager.h>
@@ -52,12 +55,6 @@
5255
#include <QFileInfo> // NOLINT cpplint cannot handle include order here
5356
#include <QString> // NOLINT: cpplint is unable to handle the include order here
5457

55-
#include <gz/math/Inertial.hh>
56-
#include <gz/math/MassMatrix3.hh>
57-
#include <gz/math/Pose3.hh>
58-
#include <gz/math/Quaternion.hh>
59-
#include <gz/math/Vector3.hh>
60-
6158
#include "resource_retriever/retriever.hpp"
6259

6360
#include "rviz_default_plugins/robot/robot_joint.hpp"
@@ -880,46 +877,54 @@ void RobotLink::createMass(const urdf::LinkConstSharedPtr & link)
880877

881878
void RobotLink::createInertia(const urdf::LinkConstSharedPtr & link)
882879
{
883-
if (link->inertial) {
884-
const gz::math::Vector3d i_xx_yy_zz(
885-
link->inertial->ixx,
886-
link->inertial->iyy,
887-
link->inertial->izz);
888-
const gz::math::Vector3d Ixyxzyz(
889-
link->inertial->ixy,
880+
if (!link->inertial) {
881+
return;
882+
}
883+
884+
const Eigen::Matrix3d inertia{(Eigen::Matrix3d{} << link->inertial->ixx, link->inertial->ixy,
890885
link->inertial->ixz,
891-
link->inertial->iyz);
892-
gz::math::MassMatrix3d mass_matrix(link->inertial->mass, i_xx_yy_zz, Ixyxzyz);
893-
894-
gz::math::Vector3d box_scale;
895-
gz::math::Quaterniond box_rot;
896-
if (!mass_matrix.EquivalentBox(box_scale, box_rot)) {
897-
// Invalid inertia, load with default scale
898-
if (link->parent_joint && link->parent_joint->type != urdf::Joint::FIXED) {
899-
// Do not show error message for base link or static links
900-
RVIZ_COMMON_LOG_ERROR_STREAM(
901-
"The link " << link->name << " has unrealistic "
902-
"inertia, so the equivalent inertia box will not be shown.\n");
903-
}
904-
return;
886+
link->inertial->ixy, link->inertial->iyy, link->inertial->iyz,
887+
link->inertial->ixz, link->inertial->iyz, link->inertial->izz).finished()};
888+
const Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver{inertia};
889+
if (link->inertial->mass <= 0 || link->inertial->ixx <= 0 ||
890+
link->inertial->ixx * link->inertial->iyy - std::pow(link->inertial->ixy, 2) <= 0 ||
891+
inertia.determinant() <= 0 || eigen_solver.info() != Eigen::ComputationInfo::Success)
892+
{
893+
// Invalid inertia, load with default scale
894+
if (link->parent_joint && link->parent_joint->type != urdf::Joint::FIXED) {
895+
// Do not show error message for base link or static links
896+
RVIZ_COMMON_LOG_ERROR_STREAM(
897+
"The link " << link->name << " has unrealistic "
898+
"inertia, so the equivalent inertia box will not be shown.\n");
905899
}
906-
Ogre::Vector3 translate(
907-
link->inertial->origin.position.x,
908-
link->inertial->origin.position.y,
909-
link->inertial->origin.position.z);
910-
911-
double x, y, z, w;
912-
link->inertial->origin.rotation.getQuaternion(x, y, z, w);
913-
Ogre::Quaternion originRotate(w, x, y, z);
914-
915-
Ogre::Quaternion rotate(box_rot.W(), box_rot.X(), box_rot.Y(), box_rot.Z());
916-
Ogre::SceneNode * offset_node = inertia_node_->createChildSceneNode(
917-
translate, originRotate * rotate);
918-
inertia_shape_ = new Shape(Shape::Cube, scene_manager_, offset_node);
919-
920-
inertia_shape_->setColor(1, 0, 0, 1);
921-
inertia_shape_->setScale(Ogre::Vector3(box_scale.X(), box_scale.Y(), box_scale.Z()));
922-
}
900+
return;
901+
}
902+
903+
const Eigen::Vector3d & eigen_values{eigen_solver.eigenvalues()};
904+
Eigen::Vector3d box_size{(eigen_values.y() + eigen_values.z() - eigen_values.x()),
905+
(eigen_values.x() + eigen_values.z() - eigen_values.y()),
906+
(eigen_values.x() + eigen_values.y() - eigen_values.z())};
907+
box_size = (6 / link->inertial->mass * box_size).cwiseSqrt();
908+
const Eigen::Vector3f box_size_float{box_size.cast<float>()};
909+
910+
const Eigen::Quaternionf box_rotation_eigen{Eigen::Quaterniond{eigen_solver.eigenvectors()}.cast<float>()};
911+
const Ogre::Quaternion box_rotation{box_rotation_eigen.w(), box_rotation_eigen.x(),
912+
box_rotation_eigen.y(), box_rotation_eigen.z()};
913+
914+
const urdf::Pose & pose{link->inertial->origin};
915+
const Ogre::Vector3 translation{static_cast<float>(pose.position.x),
916+
static_cast<float>(pose.position.y),
917+
static_cast<float>(pose.position.z)};
918+
const Ogre::Quaternion origin_rotation{static_cast<float>(pose.rotation.w),
919+
static_cast<float>(pose.rotation.x), static_cast<float>(pose.rotation.y),
920+
static_cast<float>(pose.rotation.z)};
921+
const Ogre::Quaternion rotation{origin_rotation * box_rotation};
922+
const Ogre::Vector3 scale{box_size_float.x(), box_size_float.y(), box_size_float.z()};
923+
924+
Ogre::SceneNode * offset_node = inertia_node_->createChildSceneNode(translation, rotation);
925+
inertia_shape_ = new Shape(Shape::Cube, scene_manager_, offset_node);
926+
inertia_shape_->setColor(1, 0, 0, 1);
927+
inertia_shape_->setScale(scale);
923928
}
924929

925930
void RobotLink::createSelection()

0 commit comments

Comments
 (0)