Skip to content

Commit

Permalink
linter
Browse files Browse the repository at this point in the history
  • Loading branch information
meshvaD committed Jun 10, 2024
1 parent 417ae6c commit 6f23ade
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 243 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
camera_object_detection_node:
dets_2d_3d_node:
ros__parameters:
camera_info_topic: /CAM_FRONT/camera_info
lidar_topic: /LIDAR_TOP
Expand Down
88 changes: 68 additions & 20 deletions src/perception/tracking/dets_2d_3d/include/cluster.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,81 @@
#pragma once

#include <rclcpp/rclcpp.hpp>

#include <geometry_msgs/msg/transform_stamped.hpp>
#include <sensor_msgs/msg/camera_info.hpp>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <vision_msgs/msg/detection2_d_array.hpp>
#include <vision_msgs/msg/detection3_d_array.hpp>
#include <visualization_msgs/msg/marker_array.hpp>

#include <tf2_ros/buffer.h>
#include <tf2_ros/static_transform_broadcaster.h>
#include <tf2_ros/transform_listener.h>

#include <pcl/point_cloud.h>
#include <pcl/point_types.h>

#include <vision_msgs/msg/detection3_d_array.hpp>
#include "cluster.hpp"
#include "projection_utils.hpp"

#include <map>

struct ClusteringParams;

class TrackingNode : public rclcpp::Node {
public:
TrackingNode();

private:
std::map<std::string, ClusteringParams> clusteringParams;

// lidar and camera frame names
std::string lidarFrame_;
std::string cameraFrame_;

// synchronization utils
std::mutex lidarCloud_mutex_;

// camera information cached
bool transformInited_;
geometry_msgs::msg::TransformStamped transform_;
sensor_msgs::msg::CameraInfo::SharedPtr camInfo_;
pcl::PointCloud<pcl::PointXYZ>::Ptr lidarCloud_;

#include <vector>
// subscribers to camera intrinsics, lidar pts, camera feed, 2d detection
// boxes
rclcpp::Subscription<sensor_msgs::msg::CameraInfo>::SharedPtr camInfo_subscriber_;
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr lidar_subscriber_;
rclcpp::Subscription<vision_msgs::msg::Detection2DArray>::SharedPtr det_subscriber_;

class Cluster {
public:
// makes a cloud from all points `in_cloud_ptr` that are indexed by
// `in_cluster_indices`
Cluster(const pcl::PointCloud<pcl::PointXYZ>::Ptr &in_cloud_ptr,
const std::vector<int> &in_cluster_indices);
// publish the 3d detections
rclcpp::Publisher<vision_msgs::msg::Detection3DArray>::SharedPtr det3d_publisher_;
rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr marker_publisher_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pc_publisher_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pc_publisher2_;

pcl::PointXYZ getCentroid() { return centroid_; }
pcl::PointCloud<pcl::PointXYZRGB>::Ptr getCloud() { return cloud_; }
vision_msgs::msg::BoundingBox3D getBoundingBox();
std::unique_ptr<tf2_ros::Buffer> tf_buffer_;
std::shared_ptr<tf2_ros::TransformListener> tf_listener_;

bool isValid();
int size() { return cloud_->size(); }
void readCameraInfo(const sensor_msgs::msg::CameraInfo::SharedPtr msg);
void receiveLidar(const sensor_msgs::msg::PointCloud2::SharedPtr msg);
void receiveDetections(const vision_msgs::msg::Detection2DArray::SharedPtr msg);

private:
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_;
pcl::PointXYZ centroid_;
int highestIOUScoredBBox(const std::vector<vision_msgs::msg::BoundingBox3D> bboxes,
const vision_msgs::msg::BoundingBox2D &detBBox,
const std::vector<std::shared_ptr<Cluster>> &clusters);
double overlapBoundingBox(const vision_msgs::msg::BoundingBox2D &bboxA,
const vision_msgs::msg::BoundingBox2D &bboxB);
double iouScore(const vision_msgs::msg::BoundingBox2D &bboxA,
const vision_msgs::msg::BoundingBox2D &bboxB);

pcl::PointXYZ max_point_;
pcl::PointXYZ min_point_;
std::map<std::string, ClusteringParams> initializeClusteringParams(
const std::map<std::string, rclcpp::Parameter> &clustering_params_map);

static int color_index;
static std::vector<std::vector<int>> colors;
template <typename T>
T getDefaultOrValue(std::map<std::string, T> m, std::string key) {
if (m.find(key) == m.end()) return m[key];
return m["default"];
}
};
32 changes: 12 additions & 20 deletions src/perception/tracking/dets_2d_3d/include/dets_2d_3d_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
struct ClusteringParams;

class TrackingNode : public rclcpp::Node {
public:
public:
TrackingNode();

private:
private:
std::map<std::string, ClusteringParams> clusteringParams;

// lidar and camera frame names
Expand All @@ -45,18 +45,13 @@ class TrackingNode : public rclcpp::Node {

// subscribers to camera intrinsics, lidar pts, camera feed, 2d detection
// boxes
rclcpp::Subscription<sensor_msgs::msg::CameraInfo>::SharedPtr
camInfo_subscriber_;
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr
lidar_subscriber_;
rclcpp::Subscription<vision_msgs::msg::Detection2DArray>::SharedPtr
det_subscriber_;
rclcpp::Subscription<sensor_msgs::msg::CameraInfo>::SharedPtr camInfo_subscriber_;
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr lidar_subscriber_;
rclcpp::Subscription<vision_msgs::msg::Detection2DArray>::SharedPtr det_subscriber_;

// publish the 3d detections
rclcpp::Publisher<vision_msgs::msg::Detection3DArray>::SharedPtr
det3d_publisher_;
rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr
marker_publisher_;
rclcpp::Publisher<vision_msgs::msg::Detection3DArray>::SharedPtr det3d_publisher_;
rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr marker_publisher_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pc_publisher_;
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr pc_publisher2_;

Expand All @@ -65,13 +60,11 @@ class TrackingNode : public rclcpp::Node {

void readCameraInfo(const sensor_msgs::msg::CameraInfo::SharedPtr msg);
void receiveLidar(const sensor_msgs::msg::PointCloud2::SharedPtr msg);
void
receiveDetections(const vision_msgs::msg::Detection2DArray::SharedPtr msg);
void receiveDetections(const vision_msgs::msg::Detection2DArray::SharedPtr msg);

int highestIOUScoredBBox(
const std::vector<vision_msgs::msg::BoundingBox3D> bboxes,
const vision_msgs::msg::BoundingBox2D &detBBox,
const std::vector<std::shared_ptr<Cluster>> &clusters);
int highestIOUScoredBBox(const std::vector<vision_msgs::msg::BoundingBox3D> bboxes,
const vision_msgs::msg::BoundingBox2D &detBBox,
const std::vector<std::shared_ptr<Cluster>> &clusters);
double overlapBoundingBox(const vision_msgs::msg::BoundingBox2D &bboxA,
const vision_msgs::msg::BoundingBox2D &bboxB);
double iouScore(const vision_msgs::msg::BoundingBox2D &bboxA,
Expand All @@ -82,8 +75,7 @@ class TrackingNode : public rclcpp::Node {

template <typename T>
T getDefaultOrValue(std::map<std::string, T> m, std::string key) {
if (m.find(key) == m.end())
return m[key];
if (m.find(key) == m.end()) return m[key];
return m["default"];
}
};
58 changes: 25 additions & 33 deletions src/perception/tracking/dets_2d_3d/include/projection_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#include "cluster.hpp"

#include <geometry_msgs/msg/point.hpp>
#include <geometry_msgs/msg/transform_stamped.hpp>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <geometry_msgs/msg/point.hpp>
#include <geometry_msgs/msg/transform_stamped.hpp>
#include <vision_msgs/msg/bounding_box2_d.hpp>
#include <vision_msgs/msg/bounding_box3_d.hpp>

Expand All @@ -26,51 +26,43 @@ struct ClusteringParams {

// main helper functions used by the node -- should all be static
class ProjectionUtils {
public:
static void
pointsInBbox(const pcl::PointCloud<pcl::PointXYZ>::Ptr &inlierCloud,
const pcl::PointCloud<pcl::PointXYZ>::Ptr &lidarCloud,
const std::vector<geometry_msgs::msg::Point> &projs2d,
const vision_msgs::msg::BoundingBox2D &bbox);
public:
static void pointsInBbox(const pcl::PointCloud<pcl::PointXYZ>::Ptr &inlierCloud,
const pcl::PointCloud<pcl::PointXYZ>::Ptr &lidarCloud,
const std::vector<geometry_msgs::msg::Point> &projs2d,
const vision_msgs::msg::BoundingBox2D &bbox);

// P : projection matrix, pt : 3D lidar pt, transform: transform from lidar to
// camera frame
static std::optional<geometry_msgs::msg::Point>
projectLidarToCamera(const geometry_msgs::msg::TransformStamped &transform,
const std::array<double, 12> &P,
const pcl::PointXYZ &pt);
static std::optional<geometry_msgs::msg::Point> projectLidarToCamera(
const geometry_msgs::msg::TransformStamped &transform, const std::array<double, 12> &P,
const pcl::PointXYZ &pt);

static bool isPointInBbox(const geometry_msgs::msg::Point &pt,
const vision_msgs::msg::BoundingBox2D &bbox);

static void
removeFloor(const pcl::PointCloud<pcl::PointXYZ>::Ptr &lidarCloud,
const pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud_filtered);
static void removeFloor(const pcl::PointCloud<pcl::PointXYZ>::Ptr &lidarCloud,
const pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud_filtered);

static std::pair<std::vector<std::shared_ptr<Cluster>>,
std::vector<vision_msgs::msg::BoundingBox3D>>
getClusteredBBoxes(const pcl::PointCloud<pcl::PointXYZ>::Ptr &lidarCloud,
const ClusteringParams &clusteringParams);

private:
static std::vector<std::shared_ptr<Cluster>>
clusterAndColor(const pcl::PointCloud<pcl::PointXYZ>::Ptr &in_cloud_ptr,
double in_max_cluster_distance, double cluster_size_min,
double cluster_size_max);
private:
static std::vector<std::shared_ptr<Cluster>> clusterAndColor(
const pcl::PointCloud<pcl::PointXYZ>::Ptr &in_cloud_ptr, double in_max_cluster_distance,
double cluster_size_min, double cluster_size_max);

static void
checkClusterMerge(size_t in_cluster_id,
const std::vector<std::shared_ptr<Cluster>> &in_clusters,
std::vector<bool> &in_out_visited_clusters,
std::vector<size_t> &out_merge_indices,
double in_merge_threshold);
static void checkClusterMerge(size_t in_cluster_id,
const std::vector<std::shared_ptr<Cluster>> &in_clusters,
std::vector<bool> &in_out_visited_clusters,
std::vector<size_t> &out_merge_indices, double in_merge_threshold);

static std::shared_ptr<Cluster>
mergeClusters(const std::vector<std::shared_ptr<Cluster>> &in_clusters,
const std::vector<size_t> &in_merge_indices,
std::vector<bool> &in_out_merged_clusters);
static std::shared_ptr<Cluster> mergeClusters(
const std::vector<std::shared_ptr<Cluster>> &in_clusters,
const std::vector<size_t> &in_merge_indices, std::vector<bool> &in_out_merged_clusters);

static std::vector<std::shared_ptr<Cluster>>
checkAllForMerge(const std::vector<std::shared_ptr<Cluster>> &in_clusters,
float in_merge_threshold);
static std::vector<std::shared_ptr<Cluster>> checkAllForMerge(
const std::vector<std::shared_ptr<Cluster>> &in_clusters, float in_merge_threshold);
};
31 changes: 11 additions & 20 deletions src/perception/tracking/dets_2d_3d/src/cluster.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "cluster.hpp"

#include <geometry_msgs/msg/point.hpp>
#include <pcl/common/centroid.h>
#include <pcl/common/common.h>
#include <pcl/common/transforms.h>
#include <tf2/LinearMath/Quaternion.h>
#include <geometry_msgs/msg/point.hpp>
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>

#include <opencv2/core/core.hpp>
Expand All @@ -18,17 +18,15 @@ typedef pcl::PointCloud<pcl::PointXYZRGB> ClusterCloud;
typedef vision_msgs::msg::BoundingBox3D BBox3D;

int Cluster::color_index = 0;
std::vector<std::vector<int>> Cluster::colors = {{0, 0, 255}, {0, 255, 0},
{255, 0, 0}, {255, 255, 0},
{0, 255, 255}, {255, 0, 255}};
std::vector<std::vector<int>> Cluster::colors = {{0, 0, 255}, {0, 255, 0}, {255, 0, 0},
{255, 255, 0}, {0, 255, 255}, {255, 0, 255}};

Cluster::Cluster(const pcl::PointCloud<pcl::PointXYZ>::Ptr &in_cloud_ptr,
const std::vector<int> &in_cluster_indices)
: cloud_{new ClusterCloud()} {
bool indexesGiven = in_cluster_indices.size() > 0;

size_t max_index =
(indexesGiven) ? in_cluster_indices.size() : in_cloud_ptr->points.size();
size_t max_index = (indexesGiven) ? in_cluster_indices.size() : in_cloud_ptr->points.size();

min_point_.x = std::numeric_limits<double>::max();
min_point_.y = std::numeric_limits<double>::max();
Expand All @@ -49,19 +47,13 @@ Cluster::Cluster(const pcl::PointCloud<pcl::PointXYZ>::Ptr &in_cloud_ptr,
pt.g = colors[color_index % colors.size()][1];
pt.b = colors[color_index % colors.size()][2];

if (pt.x < min_point_.x)
min_point_.x = pt.x;
if (pt.y < min_point_.y)
min_point_.y = pt.y;
if (pt.z < min_point_.z)
min_point_.z = pt.z;
if (pt.x < min_point_.x) min_point_.x = pt.x;
if (pt.y < min_point_.y) min_point_.y = pt.y;
if (pt.z < min_point_.z) min_point_.z = pt.z;

if (pt.x > max_point_.x)
max_point_.x = pt.x;
if (pt.y > max_point_.y)
max_point_.y = pt.y;
if (pt.z > max_point_.z)
max_point_.z = pt.z;
if (pt.x > max_point_.x) max_point_.x = pt.x;
if (pt.y > max_point_.y) max_point_.y = pt.y;
if (pt.z > max_point_.z) max_point_.z = pt.z;

cloud_->emplace_back(pt);
}
Expand All @@ -72,8 +64,7 @@ Cluster::Cluster(const pcl::PointCloud<pcl::PointXYZ>::Ptr &in_cloud_ptr,
BBox3D Cluster::getBoundingBox() {
BBox3D bounding_box_;

if (cloud_->size() == 0)
return bounding_box_;
if (cloud_->size() == 0) return bounding_box_;

double length_ = max_point_.x - min_point_.x;
double width_ = max_point_.y - min_point_.y;
Expand Down
Loading

0 comments on commit 6f23ade

Please sign in to comment.