Skip to content

Commit 0d4ca6d

Browse files
a-maumaupre-commit-ci[bot]
authored and
KhalilSelyan
committed
refactor(yabloc_common): apply static analysis (#7481)
* refactor based on linter Signed-off-by: a-maumau <maumaumaumaumaumaumaumaumaumau@gmail.com> * restore unwanted change Signed-off-by: a-maumau <maumaumaumaumaumaumaumaumaumau@gmail.com> * remove unnecessary comment Signed-off-by: a-maumau <maumaumaumaumaumaumaumaumaumau@gmail.com> * style(pre-commit): autofix * change to const double Signed-off-by: a-maumau <maumaumaumaumaumaumaumaumaumau@gmail.com> * add static cast Signed-off-by: a-maumau <maumaumaumaumaumaumaumaumaumau@gmail.com> --------- Signed-off-by: a-maumau <maumaumaumaumaumaumaumaumaumau@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent cbb1fe4 commit 0d4ca6d

18 files changed

+131
-119
lines changed

localization/yabloc/yabloc_common/include/yabloc_common/camera_info_subscriber.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ class CameraInfoSubscriber
3131

3232
explicit CameraInfoSubscriber(rclcpp::Node * node);
3333

34-
bool is_camera_info_ready() const;
34+
[[nodiscard]] bool is_camera_info_ready() const;
3535

36-
bool is_camera_info_nullopt() const;
36+
[[nodiscard]] bool is_camera_info_nullopt() const;
3737

38-
Eigen::Vector2i size() const;
38+
[[nodiscard]] Eigen::Vector2i size() const;
3939

40-
Eigen::Matrix3f intrinsic() const;
40+
[[nodiscard]] Eigen::Matrix3f intrinsic() const;
4141

4242
// This member function DOES NOT check isCameraInfoReady()
4343
// If it it not ready, throw bad optional access
44-
std::string get_frame_id() const;
44+
[[nodiscard]] std::string get_frame_id() const;
4545

4646
private:
4747
rclcpp::Subscription<CameraInfo>::SharedPtr sub_info_;

localization/yabloc/yabloc_common/include/yabloc_common/color.hpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,28 @@ struct Color
3131
}
3232

3333
explicit Color(const cv::Scalar & rgb, float a = 1.0f)
34-
: r(rgb[2] / 255.f), g(rgb[1] / 255.f), b(rgb[0] / 255.f), a(a)
34+
: r(static_cast<float>(rgb[2]) / 255.f),
35+
g(static_cast<float>(rgb[1]) / 255.f),
36+
b(static_cast<float>(rgb[0]) / 255.f),
37+
a(a)
3538
{
3639
}
3740

38-
operator uint32_t() const
41+
explicit operator uint32_t() const
3942
{
4043
union uchar4_uint32 {
4144
uint8_t rgba[4];
4245
uint32_t u32;
4346
};
44-
uchar4_uint32 tmp;
47+
uchar4_uint32 tmp{};
4548
tmp.rgba[0] = static_cast<uint8_t>(r * 255);
4649
tmp.rgba[1] = static_cast<uint8_t>(g * 255);
4750
tmp.rgba[2] = static_cast<uint8_t>(b * 255);
4851
tmp.rgba[3] = static_cast<uint8_t>(a * 255);
4952
return tmp.u32;
5053
}
51-
operator const cv::Scalar() const { return cv::Scalar(255 * b, 255 * g, 255 * r); }
52-
operator const std_msgs::msg::ColorRGBA() const
54+
explicit operator cv::Scalar() const { return {255 * b, 255 * g, 255 * r}; }
55+
explicit operator std_msgs::msg::ColorRGBA() const
5356
{
5457
std_msgs::msg::ColorRGBA rgba;
5558
rgba.a = a;

localization/yabloc/yabloc_common/include/yabloc_common/gamma_converter.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class GammaConverter
2727
{
2828
lut_ = cv::Mat(1, 256, CV_8UC1);
2929
for (int i = 0; i < 256; i++) {
30-
lut_.at<uchar>(0, i) = 256 * std::pow(i / 256.f, gamma);
30+
lut_.at<uchar>(0, i) =
31+
static_cast<unsigned char>(256 * std::pow(static_cast<float>(i) / 256.f, gamma));
3132
}
3233
}
3334

localization/yabloc/yabloc_common/include/yabloc_common/ground_plane.hpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,39 @@ struct GroundPlane
4646
}
4747
}
4848

49-
float height() const { return xyz.z(); }
49+
[[nodiscard]] float height() const { return xyz.z(); }
5050

51-
Eigen::Quaternionf align_with_slope(const Eigen::Quaternionf & q) const
51+
[[nodiscard]] Eigen::Quaternionf align_with_slope(const Eigen::Quaternionf & q) const
5252
{
5353
return Eigen::Quaternionf{align_with_slope(q.toRotationMatrix())};
5454
}
5555

56-
Eigen::Matrix3f align_with_slope(const Eigen::Matrix3f & R) const
56+
[[nodiscard]] Eigen::Matrix3f align_with_slope(const Eigen::Matrix3f & R) const
5757
{
58-
Eigen::Matrix3f R_;
58+
Eigen::Matrix3f r;
5959
Eigen::Vector3f rz = this->normal;
6060
Eigen::Vector3f azimuth = R * Eigen::Vector3f::UnitX();
6161
Eigen::Vector3f ry = (rz.cross(azimuth)).normalized();
6262
Eigen::Vector3f rx = ry.cross(rz);
63-
R_.col(0) = rx;
64-
R_.col(1) = ry;
65-
R_.col(2) = rz;
66-
return R_;
63+
r.col(0) = rx;
64+
r.col(1) = ry;
65+
r.col(2) = rz;
66+
return r;
6767
}
6868

69-
Sophus::SE3f align_with_slope(const Sophus::SE3f & pose) const
69+
[[nodiscard]] Sophus::SE3f align_with_slope(const Sophus::SE3f & pose) const
7070
{
7171
return {align_with_slope(pose.rotationMatrix()), pose.translation()};
7272
}
7373

74-
Eigen::Affine3f align_with_slope(const Eigen::Affine3f & pose) const
74+
[[nodiscard]] Eigen::Affine3f align_with_slope(const Eigen::Affine3f & pose) const
7575
{
76-
Eigen::Matrix3f R = pose.rotation();
76+
Eigen::Matrix3f r = pose.rotation();
7777
Eigen::Vector3f t = pose.translation();
78-
return Eigen::Translation3f(t) * align_with_slope(R);
78+
return Eigen::Translation3f(t) * align_with_slope(r);
7979
}
8080

81-
Float32Array msg() const
81+
[[nodiscard]] Float32Array msg() const
8282
{
8383
Float32Array array;
8484
for (int i = 0; i < 3; i++) array.data.push_back(xyz(i));

localization/yabloc/yabloc_common/include/yabloc_common/ground_server/filter/moving_averaging.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class MovingAveraging
3232

3333
Eigen::Vector3f mean = Eigen::Vector3f::Zero();
3434
for (const Eigen::Vector3f & v : buffer_) mean += v;
35-
mean /= buffer_.size();
35+
mean /= static_cast<float>(buffer_.size());
3636

3737
return mean.normalized();
3838
}

localization/yabloc/yabloc_common/include/yabloc_common/ground_server/ground_server.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class GroundServer : public rclcpp::Node
6161

6262
private:
6363
const bool force_zero_tilt_;
64-
const float R;
65-
const int K;
64+
const float R_;
65+
const int K_;
6666

6767
// Subscriber
6868
rclcpp::Subscription<LaneletMapBin>::SharedPtr sub_map_;

localization/yabloc/yabloc_common/include/yabloc_common/ground_server/util.hpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626

2727
namespace yabloc::ground_server
2828
{
29-
void upsample_line_string(
29+
void inline upsample_line_string(
3030
const lanelet::ConstPoint3d & from, const lanelet::ConstPoint3d & to,
3131
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
3232
{
33-
Eigen::Vector3f f(from.x(), from.y(), from.z());
34-
Eigen::Vector3f t(to.x(), to.y(), to.z());
33+
Eigen::Vector3f f(
34+
static_cast<float>(from.x()), static_cast<float>(from.y()), static_cast<float>(from.z()));
35+
Eigen::Vector3f t(
36+
static_cast<float>(to.x()), static_cast<float>(to.y()), static_cast<float>(to.z()));
3537
float length = (t - f).norm();
3638
Eigen::Vector3f d = (t - f).normalized();
3739
for (float l = 0; l < length; l += 0.5f) {
@@ -41,7 +43,8 @@ void upsample_line_string(
4143
}
4244
};
4345

44-
std::vector<int> merge_indices(const std::vector<int> & indices1, const std::vector<int> & indices2)
46+
std::vector<int> inline merge_indices(
47+
const std::vector<int> & indices1, const std::vector<int> & indices2)
4548
{
4649
std::unordered_set<int> set;
4750
for (int i : indices1) set.insert(i);

localization/yabloc/yabloc_common/include/yabloc_common/ll2_decomposer/ll2_decomposer.hpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@ class Ll2Decomposer : public rclcpp::Node
5454

5555
void on_map(const LaneletMapBin & msg);
5656

57-
pcl::PointNormal to_point_normal(
58-
const lanelet::ConstPoint3d & from, const lanelet::ConstPoint3d & to) const;
57+
static pcl::PointNormal to_point_normal(
58+
const lanelet::ConstPoint3d & from, const lanelet::ConstPoint3d & to);
5959

60-
pcl::PointCloud<pcl::PointNormal> split_line_strings(
60+
static pcl::PointCloud<pcl::PointNormal> split_line_strings(
6161
const lanelet::ConstLineStrings3d & line_strings);
6262

6363
pcl::PointCloud<pcl::PointXYZL> load_bounding_boxes(const lanelet::PolygonLayer & polygons) const;
6464

65-
lanelet::ConstLineStrings3d extract_specified_line_string(
66-
const lanelet::LineStringLayer & line_strings, const std::set<std::string> & visible_labels);
67-
lanelet::ConstPolygons3d extract_specified_polygon(
65+
static lanelet::ConstLineStrings3d extract_specified_line_string(
66+
const lanelet::LineStringLayer & line_string_layer,
67+
const std::set<std::string> & visible_labels);
68+
static lanelet::ConstPolygons3d extract_specified_polygon(
6869
const lanelet::PolygonLayer & polygon_layer, const std::set<std::string> & visible_labels);
6970

7071
MarkerArray make_sign_marker_msg(

localization/yabloc/yabloc_common/src/camera_info_subscriber.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Eigen::Matrix3f CameraInfoSubscriber::intrinsic() const
5151
if (!opt_info_.has_value()) {
5252
throw std::runtime_error("camera_info is not ready but it's accessed");
5353
}
54-
const Eigen::Matrix3d Kd_t = Eigen::Map<const Eigen::Matrix<double, 3, 3>>(opt_info_->k.data());
55-
return Kd_t.cast<float>().transpose();
54+
const Eigen::Matrix3d kd_t = Eigen::Map<const Eigen::Matrix<double, 3, 3>>(opt_info_->k.data());
55+
return kd_t.cast<float>().transpose();
5656
}
5757

5858
} // namespace yabloc::common

localization/yabloc/yabloc_common/src/color.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ namespace yabloc::common::color_scale
1919
Color rainbow(float value)
2020
{
2121
// clang-format off
22-
float r = 1.0f, g = 1.0f, b = 1.0f;
22+
float r = 1.0f;
23+
float g = 1.0f;
24+
float b = 1.0f;
2325
value = std::clamp(value, 0.0f, 1.0f);
2426
if (value < 0.25f) {
2527
r = 0; g = 4 * (value);
@@ -40,18 +42,12 @@ Color hsv_to_rgb(float h_, float s_, float v_)
4042
const float max = v_;
4143
const float min = max * (1.0f - s_);
4244

43-
if (h < 60)
44-
return {max, h / 60 * (max - min) + min, min};
45-
else if (h < 120)
46-
return {(120 - h) / 60 * (max - min) + min, max, min};
47-
else if (h < 180)
48-
return {min, max, (h - 120) / 60 * (max - min) + min};
49-
else if (h < 240)
50-
return {min, (240 - h) / 60 * (max - min) + min, max};
51-
else if (h < 300)
52-
return {(h - 240) / 60 * (max - min) + min, min, max};
53-
else
54-
return {max, min, (360 - h) / 60 * (max - min) + min};
45+
if (h < 60) return {max, h / 60 * (max - min) + min, min};
46+
if (h < 120) return {(120 - h) / 60 * (max - min) + min, max, min};
47+
if (h < 180) return {min, max, (h - 120) / 60 * (max - min) + min};
48+
if (h < 240) return {min, (240 - h) / 60 * (max - min) + min, max};
49+
if (h < 300) return {(h - 240) / 60 * (max - min) + min, min, max};
50+
return {max, min, (360 - h) / 60 * (max - min) + min};
5551
}
5652

5753
Color blue_red(float value)

localization/yabloc/yabloc_common/src/cv_decompress.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ cv::Mat decompress_image(const sensor_msgs::msg::CompressedImage & compressed_im
2828
cv::Mat raw_image;
2929

3030
const std::string & format = compressed_img.format;
31-
const std::string encoding = format.substr(0, format.find(";"));
31+
const std::string encoding = format.substr(0, format.find(';'));
3232

33-
constexpr int DECODE_GRAY = 0;
34-
constexpr int DECODE_RGB = 1;
33+
constexpr int decode_gray = 0;
34+
constexpr int decode_rgb = 1;
3535

3636
bool encoding_is_bayer = encoding.find("bayer") != std::string::npos;
3737
if (!encoding_is_bayer) {
38-
return cv::imdecode(cv::Mat(compressed_img.data), DECODE_RGB);
38+
return cv::imdecode(cv::Mat(compressed_img.data), decode_rgb);
3939
}
40-
raw_image = cv::imdecode(cv::Mat(compressed_img.data), DECODE_GRAY);
40+
raw_image = cv::imdecode(cv::Mat(compressed_img.data), decode_gray);
4141

4242
// TODO(KYabuuchi) integrate with implementation in the sensing/perception component
4343
if (encoding == "bayer_rggb8") {

localization/yabloc/yabloc_common/src/extract_line_segments.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pcl::PointCloud<pcl::PointNormal> extract_near_line_segments(
2020
const pcl::PointCloud<pcl::PointNormal> & line_segments, const Sophus::SE3f & transform,
2121
const float max_range)
2222
{
23-
constexpr double sqrt_two = 1.41421356237309504880;
24-
const Eigen::Vector3f pose_vector = transform.translation();
23+
const double sqrt_two = 1.41421356237309504880;
24+
const Eigen::Vector3f & pose_vector = transform.translation();
2525

2626
// All line segments contained in a square with max_range on one side must be taken out,
2727
// so pick up those that are closer than the **diagonals** of the square.
@@ -42,11 +42,9 @@ pcl::PointCloud<pcl::PointNormal> extract_near_line_segments(
4242
};
4343

4444
pcl::PointCloud<pcl::PointNormal> dst;
45-
for (const pcl::PointNormal & pn : line_segments) {
46-
if (check_intersection(pn)) {
47-
dst.push_back(pn);
48-
}
49-
}
45+
std::copy_if(
46+
line_segments.begin(), line_segments.end(), std::back_inserter(dst),
47+
[check_intersection](const auto & pn) { return check_intersection(pn); });
5048
return dst;
5149
}
5250

localization/yabloc/yabloc_common/src/ground_server/ground_server_core.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
#include <pcl/sample_consensus/model_types.h>
2929
#include <pcl/segmentation/sac_segmentation.h>
3030

31+
#include <cmath>
32+
3133
namespace yabloc::ground_server
3234
{
3335
GroundServer::GroundServer(const rclcpp::NodeOptions & options)
3436
: Node("ground_server", options),
3537
force_zero_tilt_(declare_parameter<bool>("force_zero_tilt")),
36-
R(declare_parameter<int>("R")),
37-
K(declare_parameter<int>("K"))
38+
R_(static_cast<float>(declare_parameter<int>("R"))),
39+
K_(static_cast<int>(declare_parameter<int>("K")))
3840
{
3941
using std::placeholders::_1;
4042
using std::placeholders::_2;
@@ -147,8 +149,8 @@ float GroundServer::estimate_height_simply(const geometry_msgs::msg::Point & poi
147149
{
148150
// NOTE: Sometimes it might give not-accurate height
149151
constexpr float sq_radius = 3.0 * 3.0;
150-
const float x = point.x;
151-
const float y = point.y;
152+
const auto x = static_cast<float>(point.x);
153+
const auto y = static_cast<float>(point.y);
152154

153155
float height = std::numeric_limits<float>::infinity();
154156
for (const auto & p : cloud_->points) {
@@ -186,12 +188,12 @@ std::vector<int> GroundServer::estimate_inliers_by_ransac(const std::vector<int>
186188
GroundServer::GroundPlane GroundServer::estimate_ground(const Point & point)
187189
{
188190
// Because height_filter_ is always initialized, getValue does not return nullopt
189-
const float predicted_z = height_filter_.getValue().value();
190-
const pcl::PointXYZ xyz(point.x, point.y, predicted_z);
191+
const float predicted_z = static_cast<float>(height_filter_.getValue().value());
192+
const pcl::PointXYZ xyz(static_cast<float>(point.x), static_cast<float>(point.y), predicted_z);
191193

192194
std::vector<int> raw_indices;
193195
std::vector<float> distances;
194-
kdtree_->nearestKSearch(xyz, K, raw_indices, distances);
196+
kdtree_->nearestKSearch(xyz, K_, raw_indices, distances);
195197

196198
std::vector<int> indices = estimate_inliers_by_ransac(raw_indices);
197199

@@ -206,7 +208,7 @@ GroundServer::GroundPlane GroundServer::estimate_ground(const Point & point)
206208

207209
// NOTE: I forgot why I don't use coefficients computed by SACSegmentation
208210
Eigen::Vector4f plane_parameter;
209-
float curvature;
211+
float curvature = NAN;
210212
pcl::solvePlaneParameters(covariance, centroid, plane_parameter, curvature);
211213
Eigen::Vector3f normal = plane_parameter.topRows(3).normalized();
212214

@@ -229,15 +231,16 @@ GroundServer::GroundPlane GroundServer::estimate_ground(const Point & point)
229231
const Eigen::Vector3f filt_normal = normal_filter_.update(normal);
230232

231233
GroundPlane plane;
232-
plane.xyz = Eigen::Vector3f(point.x, point.y, predicted_z);
234+
plane.xyz =
235+
Eigen::Vector3f(static_cast<float>(point.x), static_cast<float>(point.y), predicted_z);
233236
plane.normal = filt_normal;
234237

235238
// Compute z value by intersection of estimated plane and orthogonal line
236239
{
237240
Eigen::Vector3f center = centroid.topRows(3);
238241
float inner = center.dot(plane.normal);
239-
float px_nx = point.x * plane.normal.x();
240-
float py_ny = point.y * plane.normal.y();
242+
float px_nx = static_cast<float>(point.x) * plane.normal.x();
243+
float py_ny = static_cast<float>(point.y) * plane.normal.y();
241244
plane.xyz.z() = (inner - px_nx - py_ny) / plane.normal.z();
242245
}
243246

0 commit comments

Comments
 (0)