Delta analysis
Reviewed on Mar 13, 01:33
UnknownTracker::UnknownTracker has 76 lines, threshold = 70
Overly long functions make the code harder to read. The recommended maximum function length for the C++ language is 70 lines of code. Severity: Brain Method - Complex Method - Long Method.
We recommend to be careful here -- just splitting long functions don't necessarily make the code easier to read. Instead, look for natural chunks inside the functions that expresses a specific task or concern. Often, such concerns are indicated by a Code Comment followed by an if-statement. Use the EXTRACT FUNCTION refactoring to encapsulate that concern.
The module contains 2 functions with similar structure: BicycleMotionModel::updateStatePoseHead,BicycleMotionModel::updateStatePoseHeadVel
Duplicated code often leads to code that's harder to change since the same logical change has to be done in multiple functions. More duplication gives lower code health.
A certain degree of duplicated code might be acceptable. The problems start when it is the same behavior that is duplicated across the functions in the module, ie. a violation of the Don't Repeat Yourself (DRY) principle. DRY violations lead to code that is changed together in predictable patterns, which is both expensive and risky. DRY violations can be identified using CodeScene's X-Ray analysis to detect clusters of change coupled functions with high code similarity. Read More
Once you have identified the similarities across functions, look to extract and encapsulate the concept that varies into its own function(s). These shared abstractions can then be re-used, which minimizes the amount of duplication and simplifies change.
In this module, 84.6% of all function arguments are primitive types, threshold = 30.0%
Code that uses a high degree of built-in, primitives such as integers, strings, floats, lacks a domain language that encapsulates the validation and semantics of function arguments. Primitive Obsession has several consequences: 1) In a statically typed language, the compiler will detect less erroneous assignments. 2) Security impact since the possible value range of a variable/argument isn't retricted.
In this module, 85 % of all functions have primitive types as arguments.
Primitive Obsession indicates a missing domain language. Introduce data types that encapsulate the details and constraints of your domain. For example, instead of int userId
, consider User clicked
.
To get a general understanding of what this code health issue looks like - and how it might be addressed - we have prepared some diffs for illustrative purposes.
@@ -1,8 +1,8 @@ |
// Problem: It's hard to know what this function does, and what values are valid as parameters. |
function getPopularRepositories(String baseURL, String query, Integer pages, Integer pageSize, String sortorder): Json { |
let pages == null ? 10 : pages |
let pageSize == null ? 10 : pageSize |
return httpClient.get(`${baseURL}?q=${query}&pages=${pages}&pageSize=${pageSize}&sortorder=${sortorder}`) |
// Refactoring: extract the pagination & API logic into a class, and it will |
// attract validation and other logic related to the specific query. It's now |
// easier to use and to maintain the getPopularRepositories function! |
function getPopularRepositories(query: PaginatedRepoQuery): Json { |
return httpClient.get(query.getURL()) |
.map(json => json.repositories) |
.filter(repository => repositry.stargazersCount > 1000) |
} |
BicycleMotionModel::setMotionParams has 11 arguments, threshold = 4
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the C++ language is 4 function arguments.
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
BicycleMotionModel::initialize has 10 arguments, threshold = 4
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the C++ language is 4 function arguments.
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
BicycleMotionModel::updateStatePoseHeadVel has 6 arguments, threshold = 4
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the C++ language is 4 function arguments.
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
The module contains 2 functions with similar structure: CTRVMotionModel::updateStatePoseHead,CTRVMotionModel::updateStatePoseHeadVel
Duplicated code often leads to code that's harder to change since the same logical change has to be done in multiple functions. More duplication gives lower code health.
A certain degree of duplicated code might be acceptable. The problems start when it is the same behavior that is duplicated across the functions in the module, ie. a violation of the Don't Repeat Yourself (DRY) principle. DRY violations lead to code that is changed together in predictable patterns, which is both expensive and risky. DRY violations can be identified using CodeScene's X-Ray analysis to detect clusters of change coupled functions with high code similarity. Read More
Once you have identified the similarities across functions, look to extract and encapsulate the concept that varies into its own function(s). These shared abstractions can then be re-used, which minimizes the amount of duplication and simplifies change.
In this module, 80.6% of all function arguments are primitive types, threshold = 30.0%
Code that uses a high degree of built-in, primitives such as integers, strings, floats, lacks a domain language that encapsulates the validation and semantics of function arguments. Primitive Obsession has several consequences: 1) In a statically typed language, the compiler will detect less erroneous assignments. 2) Security impact since the possible value range of a variable/argument isn't retricted.
In this module, 81 % of all functions have primitive types as arguments.
Primitive Obsession indicates a missing domain language. Introduce data types that encapsulate the details and constraints of your domain. For example, instead of int userId
, consider User clicked
.
To get a general understanding of what this code health issue looks like - and how it might be addressed - we have prepared some diffs for illustrative purposes.
@@ -1,8 +1,8 @@ |
// Problem: It's hard to know what this function does, and what values are valid as parameters. |
function getPopularRepositories(String baseURL, String query, Integer pages, Integer pageSize, String sortorder): Json { |
let pages == null ? 10 : pages |
let pageSize == null ? 10 : pageSize |
return httpClient.get(`${baseURL}?q=${query}&pages=${pages}&pageSize=${pageSize}&sortorder=${sortorder}`) |
// Refactoring: extract the pagination & API logic into a class, and it will |
// attract validation and other logic related to the specific query. It's now |
// easier to use and to maintain the getPopularRepositories function! |
function getPopularRepositories(query: PaginatedRepoQuery): Json { |
return httpClient.get(query.getURL()) |
.map(json => json.repositories) |
.filter(repository => repositry.stargazersCount > 1000) |
} |
CTRVMotionModel::setMotionParams has 5 arguments, threshold = 4
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the C++ language is 4 function arguments.
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
CTRVMotionModel::initialize has 9 arguments, threshold = 4
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the C++ language is 4 function arguments.
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
CTRVMotionModel::updateStatePoseHeadVel has 6 arguments, threshold = 4
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the C++ language is 4 function arguments.
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
In this module, 75.0% of all function arguments are primitive types, threshold = 30.0%
Code that uses a high degree of built-in, primitives such as integers, strings, floats, lacks a domain language that encapsulates the validation and semantics of function arguments. Primitive Obsession has several consequences: 1) In a statically typed language, the compiler will detect less erroneous assignments. 2) Security impact since the possible value range of a variable/argument isn't retricted.
In this module, 75 % of all functions have primitive types as arguments.
Primitive Obsession indicates a missing domain language. Introduce data types that encapsulate the details and constraints of your domain. For example, instead of int userId
, consider User clicked
.
To get a general understanding of what this code health issue looks like - and how it might be addressed - we have prepared some diffs for illustrative purposes.
@@ -1,8 +1,8 @@ |
// Problem: It's hard to know what this function does, and what values are valid as parameters. |
function getPopularRepositories(String baseURL, String query, Integer pages, Integer pageSize, String sortorder): Json { |
let pages == null ? 10 : pages |
let pageSize == null ? 10 : pageSize |
return httpClient.get(`${baseURL}?q=${query}&pages=${pages}&pageSize=${pageSize}&sortorder=${sortorder}`) |
// Refactoring: extract the pagination & API logic into a class, and it will |
// attract validation and other logic related to the specific query. It's now |
// easier to use and to maintain the getPopularRepositories function! |
function getPopularRepositories(query: PaginatedRepoQuery): Json { |
return httpClient.get(query.getURL()) |
.map(json => json.repositories) |
.filter(repository => repositry.stargazersCount > 1000) |
} |
CVMotionModel::initialize has 7 arguments, threshold = 4
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the C++ language is 4 function arguments.
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
CVMotionModel::updateStatePoseVel has 6 arguments, threshold = 4
Functions with many arguments indicate either a) low cohesion where the function has too many responsibilities, or b) a missing abstraction that encapsulates those arguments.
The threshold for the C++ language is 4 function arguments.
Start by investigating the responsibilities of the function. Make sure it doesn't do too many things, in which case it should be split into smaller and more cohesive functions. Consider the refactoring INTRODUCE PARAMETER OBJECT to encapsulate arguments that refer to the same logical concept.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
MultiObjectTracker::sanitizeTracker increases in cyclomatic complexity from 17 to 21, threshold = 9
A Complex Method has a high cyclomatic complexity. The recommended threshold for the C++ language is a cyclomatic complexity lower than 9.
There are many reasons for Complex Method. Sometimes, another design approach is beneficial such as a) modeling state using an explicit state machine rather than conditionals, or b) using table lookup rather than long chains of logic. In other scenarios, the function can be split using EXTRACT FUNCTION. Just make sure you extract natural and cohesive functions. Complex Methods can also be addressed by identifying complex conditional expressions and then using the DECOMPOSE CONDITIONAL refactoring.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
MultiObjectTracker::publish has a cyclomatic complexity of 9, threshold = 9
A Complex Method has a high cyclomatic complexity. The recommended threshold for the C++ language is a cyclomatic complexity lower than 9.
There are many reasons for Complex Method. Sometimes, another design approach is beneficial such as a) modeling state using an explicit state machine rather than conditionals, or b) using table lookup rather than long chains of logic. In other scenarios, the function can be split using EXTRACT FUNCTION. Just make sure you extract natural and cohesive functions. Complex Methods can also be addressed by identifying complex conditional expressions and then using the DECOMPOSE CONDITIONAL refactoring.
This code health issue has been solved before in this project. Here are some examples for inspiration:
@@ -13,5 +13,2 @@ |
// limitations under the License. |
// |
// |
// Author: v1.0 Yukihiro Saito, Taekjin Lee |
@@ -69,7 +66,5 @@ double get2dIoU( |
const auto source_polygon = autoware_utils::to_polygon2d( |
source_object.kinematics.pose_with_covariance.pose, source_object.shape); |
const auto source_polygon = autoware_utils::to_polygon2d(source_object.pose, source_object.shape); |
if (boost::geometry::area(source_polygon) < MIN_AREA) return 0.0; |
const auto target_polygon = autoware_utils::to_polygon2d( |
target_object.kinematics.pose_with_covariance.pose, target_object.shape); |
const auto target_polygon = autoware_utils::to_polygon2d(target_object.pose, target_object.shape); |
if (boost::geometry::area(target_polygon) < MIN_AREA) return 0.0; |
@@ -113,6 +108,4 @@ bool convertConvexHullToBoundingBox( |
// calc new center |
const Eigen::Vector2d center{ |
input_object.kinematics.pose_with_covariance.pose.position.x, |
input_object.kinematics.pose_with_covariance.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.kinematics.pose_with_covariance.pose.orientation); |
const Eigen::Vector2d center{input_object.pose.position.x, input_object.pose.position.y}; |
const auto yaw = tf2::getYaw(input_object.pose.orientation); |
const Eigen::Matrix2d R_inv = Eigen::Rotation2Dd(-yaw).toRotationMatrix(); |
@@ -123,4 +116,4 @@ bool convertConvexHullToBoundingBox( |
output_object = input_object; |
output_object.kinematics.pose_with_covariance.pose.position.x = new_center.x(); |
output_object.kinematics.pose_with_covariance.pose.position.y = new_center.y(); |
output_object.pose.position.x = new_center.x(); |
output_object.pose.position.y = new_center.y(); |
@@ -137,3 +130,3 @@ bool getMeasurementYaw( |
{ |
measurement_yaw = tf2::getYaw(object.kinematics.pose_with_covariance.pose.orientation); |
measurement_yaw = tf2::getYaw(object.pose.orientation); |
@@ -180,6 +173,11 @@ enum BBOX_IDX { |
*/ |
int getNearestCornerOrSurface( |
const double x, const double y, const double yaw, const double width, const double length, |
const geometry_msgs::msg::Transform & self_transform) |
void getNearestCornerOrSurface( |
const geometry_msgs::msg::Transform & self_transform, types::DynamicObject & object) |
{ |
const double x = object.pose.position.x; |
const double y = object.pose.position.y; |
const double yaw = tf2::getYaw(object.pose.orientation); |
const double width = object.shape.dimensions.y; |
const double length = object.shape.dimensions.x; |
// get local vehicle pose |
@@ -193,3 +191,3 @@ int getNearestCornerOrSurface( |
// Determine Index |
// Determine anchor point |
// x+ (front) |
@@ -200,104 +198,57 @@ int getNearestCornerOrSurface( |
// x- (rear) |
int xgrid = 0; |
int ygrid = 0; |
const int labels[3][3] = { |
{BBOX_IDX::FRONT_L_CORNER, BBOX_IDX::FRONT_SURFACE, BBOX_IDX::FRONT_R_CORNER}, |
{BBOX_IDX::LEFT_SURFACE, BBOX_IDX::INSIDE, BBOX_IDX::RIGHT_SURFACE}, |
{BBOX_IDX::REAR_L_CORNER, BBOX_IDX::REAR_SURFACE, BBOX_IDX::REAR_R_CORNER}}; |
double anchor_x = 0; |
double anchor_y = 0; |
if (xl > length / 2.0) { |
xgrid = 0; // front |
anchor_x = length / 2.0; |
} else if (xl > -length / 2.0) { |
xgrid = 1; // middle |
anchor_x = 0; |
} else { |
xgrid = 2; // rear |
anchor_x = -length / 2.0; |
} |
if (yl > width / 2.0) { |
ygrid = 0; // left |
anchor_y = width / 2.0; |
} else if (yl > -width / 2.0) { |
ygrid = 1; // middle |
anchor_y = 0; |
} else { |
ygrid = 2; // right |
anchor_y = -width / 2.0; |
} |
return labels[xgrid][ygrid]; // 0 to 7 + 1(null) value |
} |
/** |
* @brief Calc bounding box center offset caused by shape change |
* @param dw: width update [m] = w_new - w_old |
* @param dl: length update [m] = l_new - l_old |
* @param indx: nearest corner index |
* @return 2d offset vector caused by shape change |
*/ |
inline Eigen::Vector2d calcOffsetVectorFromShapeChange( |
const double dw, const double dl, const int indx) |
{ |
Eigen::Vector2d offset; |
// if surface |
if (indx == BBOX_IDX::FRONT_SURFACE) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::RIGHT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_SURFACE) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = 0; |
} else if (indx == BBOX_IDX::LEFT_SURFACE) { |
offset(0, 0) = 0; |
offset(1, 0) = dw / 2.0; // move left |
} |
// if corner |
if (indx == BBOX_IDX::FRONT_R_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_R_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = -dw / 2.0; // move right |
} else if (indx == BBOX_IDX::REAR_L_CORNER) { |
offset(0, 0) = -dl / 2.0; // move backward |
offset(1, 0) = dw / 2.0; // move left |
} else if (indx == BBOX_IDX::FRONT_L_CORNER) { |
offset(0, 0) = dl / 2.0; // move forward |
offset(1, 0) = dw / 2.0; // move left |
} |
return offset; // do nothing if indx == INVALID or INSIDE |
object.anchor_point.x = anchor_x; |
object.anchor_point.y = anchor_y; |
} |
/** |
* @brief Convert input object center to tracking point based on nearest corner information |
* 1. update anchor offset vector, 2. offset input bbox based on tracking_offset vector and |
* prediction yaw angle |
* @param w: last input bounding box width |
* @param l: last input bounding box length |
* @param indx: last input bounding box closest corner index |
* @param input_object: input object bounding box |
* @param yaw: current yaw estimation |
* @param offset_object: output tracking measurement to feed ekf |
* @return nearest corner index(int) |
*/ |
void calcAnchorPointOffset( |
const double w, const double l, const int indx, const types::DynamicObject & input_object, |
const double & yaw, types::DynamicObject & offset_object, Eigen::Vector2d & tracking_offset) |
const types::DynamicObject & this_object, Eigen::Vector2d & tracking_offset, |
types::DynamicObject & updating_object) |
{ |
// copy value |
offset_object = input_object; |
// invalid index |
if (indx == BBOX_IDX::INSIDE) { |
return; // do nothing |
const geometry_msgs::msg::Point anchor_vector = updating_object.anchor_point; |
// invalid anchor |
if (anchor_vector.x <= 1e-6 && anchor_vector.y <= 1e-6) { |
return; |
} |
double input_yaw = tf2::getYaw(updating_object.pose.orientation); |
// current object width and height |
const double w_n = input_object.shape.dimensions.y; |
const double l_n = input_object.shape.dimensions.x; |
const double length = this_object.shape.dimensions.x; |
const double width = this_object.shape.dimensions.y; |
// update offset |
const Eigen::Vector2d offset = calcOffsetVectorFromShapeChange(w_n - w, l_n - l, indx); |
tracking_offset = offset; |
tracking_offset = Eigen::Vector2d(anchor_vector.x, anchor_vector.y); |
if (tracking_offset.x() > 0) { |
tracking_offset.x() -= length / 2.0; |
} else if (tracking_offset.x() < 0) { |
tracking_offset.x() += length / 2.0; |
} |
if (tracking_offset.y() > 0) { |
tracking_offset.y() -= width / 2.0; |
} else if (tracking_offset.y() < 0) { |
tracking_offset.y() += width / 2.0; |
} |
// offset input object |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(yaw).toRotationMatrix(); |
const Eigen::Matrix2d R = Eigen::Rotation2Dd(input_yaw).toRotationMatrix(); |
const Eigen::Vector2d rotated_offset = R * tracking_offset; |
offset_object.kinematics.pose_with_covariance.pose.position.x += rotated_offset.x(); |
offset_object.kinematics.pose_with_covariance.pose.position.y += rotated_offset.y(); |
updating_object.pose.position.x += rotated_offset.x(); |
updating_object.pose.position.y += rotated_offset.y(); |
} |
MultiObjectTracker::sanitizeTracker increases from 3 to 4 logical blocks with deeply nested code, threshold is one single block per function
A Bumpy Road is a function that contains multiple chunks of nested conditional logic inside the same function. The deeper the nesting and the more bumps, the lower the code health.
A bumpy code road represents a lack of encapsulation which becomes an obstacle to comprehension. In imperative languages there’s also an increased risk for feature entanglement, which leads to complex state management. CodeScene considers the following rules for the code health impact: 1) The deeper the nested conditional logic of each bump, the higher the tax on our working memory. 2) The more bumps inside a function, the more expensive it is to refactor as each bump represents a missing abstraction. 3) The larger each bump – that is, the more lines of code it spans – the harder it is to build up a mental model of the function. The nesting depth for what is considered a bump is levels of conditionals.
Bumpy Road implementations indicate a lack of encapsulation. Check out the detailed description of the Bumpy Road code health issue.
A Bumpy Road often suggests that the function/method does too many things. The first refactoring step is to identify the different possible responsibilities of the function. Consider extracting those responsibilities into smaller, cohesive, and well-named functions. The EXTRACT FUNCTION refactoring is the primary response.
The mean cyclomatic complexity increases from 4.12 to 4.59, threshold = 4
Overall Code Complexity is measured by the mean cyclomatic complexity across all functions in the file. The lower the number, the better.
Cyclomatic complexity is a function level metric that measures the number of logical branches (if-else, loops, etc.). Cyclomatic complexity is a rough complexity measure, but useful as a way of estimating the minimum number of unit tests you would need. As such, prefer functions with low cyclomatic complexity (2-3 branches).
You address the overall cyclomatic complexity by a) modularizing the code, and b) abstract away the complexity. Let's look at some examples:
Modularizing the Code: Do an X-Ray and inspect the local hotspots. Are there any complex conditional expressions? If yes, then do a DECOMPOSE CONDITIONAL refactoring. Extract the conditional logic into a separate function and put a good name on that function. This clarifies the intent and makes the original function easier to read. Repeat until all complex conditional expressions have been simplified.
BicycleTracker::BicycleTracker decreases from 92 to 91 lines of code, threshold = 70
BicycleTracker::getTrackedObject is no longer above the threshold for lines of code
The median function size in this module is no longer above the threshold
BicycleTracker::measureWithPose is no longer above the threshold for cyclomatic complexity
BicycleTracker::measureWithPose is no longer above the threshold for logical blocks with deeply nested code
The mean cyclomatic complexity in this module is no longer above the threshold
BigVehicleTracker::BigVehicleTracker decreases from 103 to 102 lines of code, threshold = 70
BigVehicleTracker::getTrackedObject is no longer above the threshold for lines of code
BigVehicleTracker::measureWithPose is no longer above the threshold for cyclomatic complexity
BigVehicleTracker::measureWithPose is no longer above the threshold for logical blocks with deeply nested code
The mean cyclomatic complexity in this module is no longer above the threshold
NormalVehicleTracker::NormalVehicleTracker decreases from 103 to 102 lines of code, threshold = 70
NormalVehicleTracker::getTrackedObject is no longer above the threshold for lines of code
NormalVehicleTracker::measureWithPose is no longer above the threshold for cyclomatic complexity
NormalVehicleTracker::measureWithPose is no longer above the threshold for logical blocks with deeply nested code
The mean cyclomatic complexity in this module is no longer above the threshold
PedestrianTracker::PedestrianTracker decreases from 94 to 85 lines of code, threshold = 70
PedestrianTracker::getTrackedObject is no longer above the threshold for lines of code
PedestrianTracker::measureWithPose is no longer above the threshold for cyclomatic complexity
The mean cyclomatic complexity in this module is no longer above the threshold
UnknownTracker::measureWithPose is no longer above the threshold for cyclomatic complexity