Skip to content

Commit 88fa2eb

Browse files
KhalilSelyanM. Fatih Cırıt
and
M. Fatih Cırıt
authoredOct 7, 2024
feat(tier4_planning_rviz_plugin): set path colors from rviz and add fade_out feature (autowarefoundation#8972)
Signed-off-by: KhalilSelyan <khalil@leodrive.ai> Co-authored-by: M. Fatih Cırıt <mfc@leodrive.ai>
1 parent e9ecd7f commit 88fa2eb

File tree

1 file changed

+61
-21
lines changed
  • common/tier4_planning_rviz_plugin/include/tier4_planning_rviz_plugin/path

1 file changed

+61
-21
lines changed
 

‎common/tier4_planning_rviz_plugin/include/tier4_planning_rviz_plugin/path/display_base.hpp

+61-21
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <OgreSceneNode.h>
3737

3838
#include <deque>
39+
#include <limits>
3940
#include <memory>
4041
#include <vector>
4142

@@ -60,24 +61,28 @@ std::unique_ptr<Ogre::ColourValue> gradation(
6061
}
6162

6263
std::unique_ptr<Ogre::ColourValue> setColorDependsOnVelocity(
63-
const double vel_max, const double cmd_vel)
64+
const double vel_max, const double cmd_vel,
65+
const rviz_common::properties::ColorProperty & color_min,
66+
const rviz_common::properties::ColorProperty & color_mid,
67+
const rviz_common::properties::ColorProperty & color_max)
6468
{
6569
const double cmd_vel_abs = std::fabs(cmd_vel);
6670
const double vel_min = 0.0;
6771

6872
std::unique_ptr<Ogre::ColourValue> color_ptr(new Ogre::ColourValue());
6973
if (vel_min < cmd_vel_abs && cmd_vel_abs <= (vel_max / 2.0)) {
7074
double ratio = (cmd_vel_abs - vel_min) / (vel_max / 2.0 - vel_min);
71-
color_ptr = gradation(Qt::red, Qt::yellow, ratio);
75+
color_ptr = gradation(color_min.getColor(), color_mid.getColor(), ratio);
7276
} else if ((vel_max / 2.0) < cmd_vel_abs && cmd_vel_abs <= vel_max) {
7377
double ratio = (cmd_vel_abs - vel_max / 2.0) / (vel_max - vel_max / 2.0);
74-
color_ptr = gradation(Qt::yellow, Qt::green, ratio);
78+
color_ptr = gradation(color_mid.getColor(), color_max.getColor(), ratio);
7579
} else if (vel_max < cmd_vel_abs) {
76-
*color_ptr = Ogre::ColourValue::Green;
80+
// Use max color when velocity exceeds max
81+
*color_ptr = rviz_common::properties::qtToOgre(color_max.getColor());
7782
} else {
78-
*color_ptr = Ogre::ColourValue::Red;
83+
// Use min color when velocity is below min
84+
*color_ptr = rviz_common::properties::qtToOgre(color_min.getColor());
7985
}
80-
8186
return color_ptr;
8287
}
8388

@@ -109,8 +114,10 @@ class AutowarePathBaseDisplay : public rviz_common::MessageFilterDisplay<T>
109114
property_path_width_view_{"Constant Width", false, "", &property_path_view_},
110115
property_path_width_{"Width", 2.0, "", &property_path_view_},
111116
property_path_alpha_{"Alpha", 1.0, "", &property_path_view_},
112-
property_path_color_view_{"Constant Color", false, "", &property_path_view_},
113-
property_path_color_{"Color", Qt::black, "", &property_path_view_},
117+
property_min_color_("Min Velocity Color", QColor("#3F2EE3"), "", &property_path_view_),
118+
property_mid_color_("Mid Velocity Color", QColor("#208AAE"), "", &property_path_view_),
119+
property_max_color_("Max Velocity Color", QColor("#00E678"), "", &property_path_view_),
120+
property_fade_out_distance_{"Fade Out Distance", 0.0, "[m]", &property_path_view_},
114121
property_vel_max_{"Color Border Vel Max", 3.0, "[m/s]", this},
115122
// velocity
116123
property_velocity_view_{"View Velocity", true, "", this},
@@ -356,6 +363,34 @@ class AutowarePathBaseDisplay : public rviz_common::MessageFilterDisplay<T>
356363
const float right = property_path_width_view_.getBool() ? property_path_width_.getFloat() / 2.0
357364
: info->width / 2.0;
358365

366+
// Initialize alphas with the default alpha value
367+
std::vector<float> alphas(msg_ptr->points.size(), property_path_alpha_.getFloat());
368+
369+
// Backward iteration to adjust alpha values for the last x meters
370+
if (property_fade_out_distance_.getFloat() > std::numeric_limits<float>::epsilon()) {
371+
alphas.back() = 0.0f;
372+
float cumulative_distance = 0.0f;
373+
for (size_t point_idx = msg_ptr->points.size() - 1; point_idx > 0; point_idx--) {
374+
const auto & curr_point = autoware::universe_utils::getPose(msg_ptr->points.at(point_idx));
375+
const auto & prev_point =
376+
autoware::universe_utils::getPose(msg_ptr->points.at(point_idx - 1));
377+
float distance = std::sqrt(autoware::universe_utils::calcSquaredDistance2d(
378+
prev_point.position, curr_point.position));
379+
cumulative_distance += distance;
380+
381+
if (cumulative_distance <= property_fade_out_distance_.getFloat()) {
382+
auto ratio =
383+
static_cast<float>(cumulative_distance / property_fade_out_distance_.getFloat());
384+
float alpha = property_path_alpha_.getFloat() * ratio;
385+
alphas.at(point_idx - 1) = alpha;
386+
} else {
387+
// If the distance exceeds the fade out distance, break the loop
388+
break;
389+
}
390+
}
391+
}
392+
393+
// Forward iteration to visualize path
359394
for (size_t point_idx = 0; point_idx < msg_ptr->points.size(); point_idx++) {
360395
const auto & path_point = msg_ptr->points.at(point_idx);
361396
const auto & pose = autoware::universe_utils::getPose(path_point);
@@ -364,15 +399,14 @@ class AutowarePathBaseDisplay : public rviz_common::MessageFilterDisplay<T>
364399
// path
365400
if (property_path_view_.getBool()) {
366401
Ogre::ColourValue color;
367-
if (property_path_color_view_.getBool()) {
368-
color = rviz_common::properties::qtToOgre(property_path_color_.getColor());
369-
} else {
370-
// color change depending on velocity
371-
std::unique_ptr<Ogre::ColourValue> dynamic_color_ptr =
372-
setColorDependsOnVelocity(property_vel_max_.getFloat(), velocity);
373-
color = *dynamic_color_ptr;
374-
}
375-
color.a = property_path_alpha_.getFloat();
402+
403+
// color change depending on velocity
404+
std::unique_ptr<Ogre::ColourValue> dynamic_color_ptr = setColorDependsOnVelocity(
405+
property_vel_max_.getFloat(), velocity, property_min_color_, property_mid_color_,
406+
property_max_color_);
407+
color = *dynamic_color_ptr;
408+
color.a = alphas.at(point_idx);
409+
376410
Eigen::Vector3f vec_in;
377411
Eigen::Vector3f vec_out;
378412
Eigen::Quaternionf quat_yaw_reverse(0, 0, 0, 1);
@@ -413,8 +447,9 @@ class AutowarePathBaseDisplay : public rviz_common::MessageFilterDisplay<T>
413447
color = rviz_common::properties::qtToOgre(property_velocity_color_.getColor());
414448
} else {
415449
/* color change depending on velocity */
416-
std::unique_ptr<Ogre::ColourValue> dynamic_color_ptr =
417-
setColorDependsOnVelocity(property_vel_max_.getFloat(), velocity);
450+
std::unique_ptr<Ogre::ColourValue> dynamic_color_ptr = setColorDependsOnVelocity(
451+
property_vel_max_.getFloat(), velocity, property_min_color_, property_mid_color_,
452+
property_max_color_);
418453
color = *dynamic_color_ptr;
419454
}
420455
color.a = property_velocity_alpha_.getFloat();
@@ -616,8 +651,13 @@ class AutowarePathBaseDisplay : public rviz_common::MessageFilterDisplay<T>
616651
rviz_common::properties::BoolProperty property_path_width_view_;
617652
rviz_common::properties::FloatProperty property_path_width_;
618653
rviz_common::properties::FloatProperty property_path_alpha_;
619-
rviz_common::properties::BoolProperty property_path_color_view_;
620-
rviz_common::properties::ColorProperty property_path_color_;
654+
// Gradient points for velocity color
655+
rviz_common::properties::ColorProperty property_min_color_;
656+
rviz_common::properties::ColorProperty property_mid_color_;
657+
rviz_common::properties::ColorProperty property_max_color_;
658+
// Last x meters of the path will fade out to transparent
659+
rviz_common::properties::FloatProperty property_fade_out_distance_;
660+
621661
rviz_common::properties::FloatProperty property_vel_max_;
622662
rviz_common::properties::BoolProperty property_velocity_view_;
623663
rviz_common::properties::FloatProperty property_velocity_alpha_;

0 commit comments

Comments
 (0)