Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
carma_wm::geometry Namespace Reference

geometry namespace contains utility geometry functions which do not require the map or route provided by an active world model More...

Functions

TrackPos trackPos (const lanelet::ConstLanelet &lanelet, const lanelet::BasicPoint2d &point)
 Returns the TrackPos, computed in 2d, of the provided point relative to the centerline of the provided lanelet. Positive crosstrack will be to the right. Points occuring before the segment will have negative downtrack. See the matchSegment function for a description of the edge cases associated with the max_crosstrack parameter. More...
 
TrackPos trackPos (const lanelet::BasicPoint2d &p, const lanelet::BasicPoint2d &seg_start, const lanelet::BasicPoint2d &seg_end)
 Return the track position of a point relative to a line segment defined by two other points. Positive crosstrack will be to the right. Points occuring before the segment will have negative downtrack. More...
 
std::tuple< TrackPos, lanelet::BasicSegment2d > matchSegment (const lanelet::BasicPoint2d &p, const lanelet::BasicLineString2d &line_string)
 Get the TrackPos of the provided point along the linestring and the segment of the line string which the provided point should be considered in. More...
 
std::vector< double > local_curvatures (const lanelet::BasicLineString2d &centerline_points)
 Returns a list of local (computed by discrete derivative) curvatures for the input centerlines. The list of returned curvatures matches 1-to-1 with with list of points in the input centerlines. More...
 
std::vector< double > local_curvatures (const std::vector< lanelet::BasicPoint2d > &centerline_points)
 
std::vector< double > local_curvatures (const std::vector< lanelet::ConstLanelet > &lanelets)
 Specialized overload of the centerline local_curvatures method but for lanelets. More...
 
lanelet::BasicLineString2d concatenate_line_strings (const lanelet::BasicLineString2d &l1, const lanelet::BasicLineString2d &l2)
 Helper function to concatenate 2 linestrings together and return the result. Neither LineString is modified in this function. More...
 
lanelet::BasicLineString2d concatenate_lanelets (const std::vector< lanelet::ConstLanelet > &lanelets)
 Helper function to a list of lanelets together and return the result. Neither LineString is modified in this function. More...
 
std::vector< Eigen::Vector2d > compute_finite_differences (const lanelet::BasicLineString2d &data)
 Use finite differences methods to compute the derivative of the input data set with respect to index Compute the finite differences using the forward finite difference for the first point, centered finite differences for the middle points and backwards finite difference for the final point. This will result in the middle points being a better approximation of the actual derivative than the endpoints. More...
 
std::vector< Eigen::Vector2d > compute_finite_differences (const std::vector< lanelet::BasicPoint2d > &data)
 
std::vector< double > compute_finite_differences (const std::vector< double > &data)
 Use finite differences methods to compute the derivative of the input data set with respect to index Compute the finite differences using the forward finite difference for the first point, centered finite differences for the middle points and backwards finite difference for the final point. This will result in the middle points being a better approximation of the actual derivative than the endpoints. More...
 
std::vector< Eigen::Vector2d > compute_finite_differences (const std::vector< Eigen::Vector2d > &x, const std::vector< double > &y)
 Use finite differences methods to compute the derivative of the input data set with respect to the second paramter. More...
 
std::vector< double > compute_arc_lengths (const std::vector< lanelet::BasicPoint2d > &data)
 Compute the arc length at each point around the curve. More...
 
std::vector< double > compute_arc_lengths (const lanelet::BasicLineString2d &data)
 Compute the arc length at each point around the curve. More...
 
double compute_euclidean_distance (const Eigen::Vector2d &a, const Eigen::Vector2d &b)
 Compute the Euclidean distance between the two points. More...
 
std::vector< Eigen::Vector2d > normalize_vectors (const std::vector< Eigen::Vector2d > &vectors)
 Normalize the vectors in the input list such that their magnitudes = 1. More...
 
std::vector< double > compute_magnitude_of_vectors (const std::vector< Eigen::Vector2d > &vectors)
 Compute the magnitude of each vector in the input list. More...
 
double computeCurvature (const lanelet::BasicPoint2d &p1, const lanelet::BasicPoint2d &p2, const lanelet::BasicPoint2d &p3)
 Function for computing curvature from 3 points. More...
 
double safeAcos (double x)
 acos method that caps the input x value for 1 or -1 to avoid undefined output. This is meant to prevent issues with floating point approximations. The user should still make sure the input data is expected to be within the [-1, 1] range for this method to give accurate results. More...
 
double safeAsin (double x)
 asin method that caps the input x value for 1 or -1 to avoid undefined output. This is meant to prevent issues with floating point approximations. The user should still make sure the input data is expected to be within the [-1, 1] range for this method to give accurate results. More...
 
double getAngleBetweenVectors (const Eigen::Vector2d &vec1, const Eigen::Vector2d &vec2)
 Calculates the angle between two vectors. More...
 
lanelet::BasicPolygon2d objectToMapPolygon (const geometry_msgs::msg::Pose &pose, const geometry_msgs::msg::Vector3 &size)
 Uses the provided pose and size vector of an ExternalObject to compute what the polygon would be of that object would be if viewed from the same frame as the pose is defined relative to. More...
 
void rpyFromQuaternion (const tf2::Quaternion &q, double &roll, double &pitch, double &yaw)
 Extract extrinsic roll-pitch-yaw from quaternion. More...
 
void rpyFromQuaternion (const geometry_msgs::msg::Quaternion &q_msg, double &roll, double &pitch, double &yaw)
 Extract extrinsic roll-pitch-yaw from quaternion. More...
 
std::vector< double > compute_tangent_orientations (const lanelet::BasicLineString2d &centerline)
 Compute an approximate orientation for the vehicle at each point along the provided centerline. More...
 
std::vector< double > compute_tangent_orientations (const std::vector< lanelet::BasicPoint2d > &centerline)
 
Eigen::Isometry2d build2dEigenTransform (const Eigen::Vector2d &position, const Eigen::Rotation2Dd &rotation)
 Builds a 2D Eigen coordinate frame transform with not applied scaling (only translation and rotation) based on the provided position and rotation parameters. More...
 
Eigen::Isometry3d build3dEigenTransform (const Eigen::Vector3d &position, const Eigen::Quaterniond &rotation)
 Builds a 3D Eigen coordinate frame transform with not applied scaling (only translation and rotation) based on the provided position and rotation parameters. More...
 
Eigen::Isometry3d build3dEigenTransform (const Eigen::Vector3d &position, const Eigen::AngleAxisd &rotation)
 
double point_to_point_yaw (const lanelet::BasicPoint2d &cur_point, const lanelet::BasicPoint2d &next_point)
 Computes the 2d orientation of the vector from the provided start point to end point. More...
 
double circular_arc_curvature (const lanelet::BasicPoint2d &cur_point, const lanelet::BasicPoint2d &next_point)
 Computes the curvature of a circular arc that passes through the two provided points. If the two points lie on a curve then larger distances between them will result in less accurate, but also less noisy curvature calculations. More...
 
std::vector< double > local_circular_arc_curvatures (const std::vector< lanelet::BasicPoint2d > &points, int lookahead)
 Computes the curvature of each point in the provided list by calling circular_arc_curvature using that point and the point at index current point index + lookahead. In this way the curvature of a path is effectively low pass filtered by the size of the lookahead value. More...
 
bool selectFirstSegment (const TrackPos &first_seg_trackPos, const TrackPos &second_seg_trackPos, double first_seg_length, double second_seg_length)
 Helper function to identify whether to select the preceeding or succeeding segment of a linstring point that is nearest an external point when trying to find the TrackPos of the external point. Of the 3 points comprising the 2 segment linestirng the external point must be closest to the midpoint for this function to be valid. More...
 
template<class P , class A >
std::vector< double > templated_local_curvatures (const std::vector< P, A > &centerline_points)
 
template<class P , class A >
std::vector< Eigen::Vector2d > compute_templated_finite_differences (const std::vector< P, A > &data)
 
template<class P , class A >
std::vector< double > compute_templated_arc_lengths (const std::vector< P, A > &data)
 
template<class P , class A >
std::vector< double > compute_templated_tangent_orientations (const std::vector< P, A > &centerline)
 

Variables

constexpr double SPATIAL_EPSILON_M = 0.05
 

Detailed Description

geometry namespace contains utility geometry functions which do not require the map or route provided by an active world model

Function Documentation

◆ build2dEigenTransform()

Eigen::Isometry2d carma_wm::geometry::build2dEigenTransform ( const Eigen::Vector2d &  position,
const Eigen::Rotation2Dd &  rotation 
)

Builds a 2D Eigen coordinate frame transform with not applied scaling (only translation and rotation) based on the provided position and rotation parameters.

Parameters
positionThe origin of the child coordinate frame in the parent frame
rotationThe rotation of the child coordinate frame in the parent frame
Returns
A 2D transformation which is defined such that given a parent frame F and a child frame C, A point P_c in child frame C can be converted to the parent frame using the returned transform T_f_c Therefore P_f = T_f_c * P_c

Definition at line 570 of file Geometry.cpp.

570 {
571 Eigen::Vector2d scale(1.0, 1.0);
572 Eigen::Isometry2d tf;
573 return tf.fromPositionOrientationScale(position, rotation, scale);
574}

Referenced by basic_autonomy::waypoint_generation::compute_heading_frame().

Here is the caller graph for this function:

◆ build3dEigenTransform() [1/2]

Eigen::Isometry3d carma_wm::geometry::build3dEigenTransform ( const Eigen::Vector3d &  position,
const Eigen::AngleAxisd &  rotation 
)

Definition at line 582 of file Geometry.cpp.

582 {
583 Eigen::Vector3d scale(1.0, 1.0, 1.0);
584 Eigen::Isometry3d tf;
585 return tf.fromPositionOrientationScale(position, rotation, scale);
586}

◆ build3dEigenTransform() [2/2]

Eigen::Isometry3d carma_wm::geometry::build3dEigenTransform ( const Eigen::Vector3d &  position,
const Eigen::Quaterniond &  rotation 
)

Builds a 3D Eigen coordinate frame transform with not applied scaling (only translation and rotation) based on the provided position and rotation parameters.

Parameters
positionThe origin of the child coordinate frame in the parent frame
rotationThe rotation of the child coordinate frame in the parent frame
Returns
A 3D transformation which is defined such that given a parent frame F and a child frame C, A point P_c in child frame C can be converted to the parent frame using the returned transform T_f_c Therefore P_f = T_f_c * P_c

Definition at line 576 of file Geometry.cpp.

576 {
577 Eigen::Vector3d scale(1.0, 1.0, 1.0);
578 Eigen::Isometry3d tf;
579 return tf.fromPositionOrientationScale(position, rotation, scale);
580}

◆ circular_arc_curvature()

double carma_wm::geometry::circular_arc_curvature ( const lanelet::BasicPoint2d &  cur_point,
const lanelet::BasicPoint2d &  next_point 
)

Computes the curvature of a circular arc that passes through the two provided points. If the two points lie on a curve then larger distances between them will result in less accurate, but also less noisy curvature calculations.

Parameters
cur_pointThe first point
next_pointThe second point
Returns
The curvature of the circular arc that passes through both points. Curvature is always positive.

Definition at line 596 of file Geometry.cpp.

597{
598 double dist = sqrt(pow(cur_point[0] - next_point[0], 2) + pow(cur_point[1] - next_point[1], 2));
599
600 double angle = point_to_point_yaw(cur_point, next_point);
601
602 double r = 0.5 * (dist / std::sin(angle));
603
604 double max_curvature = 100000;
605 double curvature = std::min(1 / r, max_curvature);
606
607 return curvature;
608}
double point_to_point_yaw(const lanelet::BasicPoint2d &cur_point, const lanelet::BasicPoint2d &next_point)
Computes the 2d orientation of the vector from the provided start point to end point.
Definition: Geometry.cpp:588

References point_to_point_yaw().

Referenced by local_circular_arc_curvatures().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_arc_lengths() [1/2]

std::vector< double > carma_wm::geometry::compute_arc_lengths ( const lanelet::BasicLineString2d &  data)

Compute the arc length at each point around the curve.

Definition at line 493 of file Geometry.cpp.

493 {
495}
std::vector< double > compute_templated_arc_lengths(const std::vector< P, A > &data)
Definition: Geometry.cpp:475

References compute_templated_arc_lengths(), and process_traj_logs::data.

Here is the call graph for this function:

◆ compute_arc_lengths() [2/2]

◆ compute_euclidean_distance()

double carma_wm::geometry::compute_euclidean_distance ( const Eigen::Vector2d &  a,
const Eigen::Vector2d &  b 
)

Compute the Euclidean distance between the two points.

Definition at line 504 of file Geometry.cpp.

505{
506 return std::sqrt(std::pow(b[0] - a[0], 2) + std::pow(b[1] - a[1], 2));
507}

Referenced by compute_templated_arc_lengths(), and concatenate_line_strings().

Here is the caller graph for this function:

◆ compute_finite_differences() [1/4]

std::vector< Eigen::Vector2d > carma_wm::geometry::compute_finite_differences ( const lanelet::BasicLineString2d &  data)

Use finite differences methods to compute the derivative of the input data set with respect to index Compute the finite differences using the forward finite difference for the first point, centered finite differences for the middle points and backwards finite difference for the final point. This will result in the middle points being a better approximation of the actual derivative than the endpoints.

Parameters
dataThe data to differentiate over
Returns
A vector containing the point-by-point derivatives in the same indices as the input data

Definition at line 419 of file Geometry.cpp.

420{
422}
std::vector< Eigen::Vector2d > compute_templated_finite_differences(const std::vector< P, A > &data)
Definition: Geometry.cpp:391

References compute_templated_finite_differences(), and process_traj_logs::data.

Referenced by templated_local_curvatures().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_finite_differences() [2/4]

std::vector< double > carma_wm::geometry::compute_finite_differences ( const std::vector< double > &  data)

Use finite differences methods to compute the derivative of the input data set with respect to index Compute the finite differences using the forward finite difference for the first point, centered finite differences for the middle points and backwards finite difference for the final point. This will result in the middle points being a better approximation of the actual derivative than the endpoints.

Parameters
dataThe data to differentiate over
Returns
A vector containing the point-by-point derivatives in the same indices as the input data

Definition at line 429 of file Geometry.cpp.

430{
431 std::vector<double> out;
432 double diff;
433 for (size_t i = 0; i < data.size(); i++) {
434
435 if (i == 0) {
436 diff = data[i + 1] - data[i];
437 } else if (i == data.size() - 1) {
438 diff = data[i] - data[i - 1];
439 } else {
440 diff = (data[i + 1] - data[i - 1])/2.0;
441 }
442
443 out.push_back(diff);
444 }
445
446 return out;
447}

References process_traj_logs::data, and process_bag::i.

◆ compute_finite_differences() [3/4]

std::vector< Eigen::Vector2d > carma_wm::geometry::compute_finite_differences ( const std::vector< Eigen::Vector2d > &  x,
const std::vector< double > &  y 
)

Use finite differences methods to compute the derivative of the input data set with respect to the second paramter.

Input x and y must be the same length. Compute the finite differences using the forward finite difference for the first point, centered finite differences for the middle points and backwards finite difference for the final point. This will result in the middle points being a better approximation of the actual derivative than the endpoints.

Parameters
xThe x value of the derivative dx/dy
yThe y value of the derivative dx/dy
Returns
A vector containing the point-by-point derivatives in the same indices as the input data

Definition at line 450 of file Geometry.cpp.

451{
452 if (x.size() != y.size()) {
453 throw std::invalid_argument("Attempting to differentiate two unequal sized lists!");
454 }
455
456 std::vector<Eigen::Vector2d> out;
457 Eigen::Vector2d diff;
458 for (size_t i = 0; i < x.size(); i++) {
459 if (i == 0) {
460 diff = (x[i + 1] - x[i])/(y[i + 1] - y[i]);
461 } else if (i == x.size() - 1) {
462 diff = (x[i] - x[i - 1])/(y[i] - y[i - 1]);
463 } else {
464 diff = (x[i + 1] - x[i - 1])/(y[i + 1] - y[i - 1]);
465 }
466
467 out.push_back(diff);
468 }
469
470 return out;
471}

References process_bag::i, process_traj_logs::x, and process_traj_logs::y.

◆ compute_finite_differences() [4/4]

std::vector< Eigen::Vector2d > carma_wm::geometry::compute_finite_differences ( const std::vector< lanelet::BasicPoint2d > &  data)

Definition at line 424 of file Geometry.cpp.

424 {
426}

References compute_templated_finite_differences(), and process_traj_logs::data.

Here is the call graph for this function:

◆ compute_magnitude_of_vectors()

std::vector< double > carma_wm::geometry::compute_magnitude_of_vectors ( const std::vector< Eigen::Vector2d > &  vectors)

Compute the magnitude of each vector in the input list.

Definition at line 520 of file Geometry.cpp.

521{
522 std::vector<double> out;
523 for (auto vec : vectors) {
524 out.push_back(vec.norm());
525 }
526
527 return out;
528}

Referenced by templated_local_curvatures().

Here is the caller graph for this function:

◆ compute_tangent_orientations() [1/2]

std::vector< double > carma_wm::geometry::compute_tangent_orientations ( const lanelet::BasicLineString2d &  centerline)

Compute an approximate orientation for the vehicle at each point along the provided centerline.

!

Uses the tangent vectors along the centerline to estimate vehicle pose in the yaw dimension. Roll and pitch are not considered.

Parameters
centerlinecenterline represented as BasicLinestring2d to compute the orientation
Returns
A vector of doubles representing the yaw value in radians the same frame as the provided centerline.

Definition at line 559 of file Geometry.cpp.

560{
562}
std::vector< double > compute_templated_tangent_orientations(const std::vector< P, A > &centerline)
Definition: Geometry.cpp:532

References compute_templated_tangent_orientations().

Referenced by basic_autonomy::waypoint_generation::compose_lanechange_trajectory_from_path(), basic_autonomy::waypoint_generation::compose_lanefollow_trajectory_from_path(), stop_controlled_intersection_tactical_plugin::StopControlledIntersectionTacticalPlugin::compose_trajectory_from_centerline(), and stop_and_wait_plugin::StopandWait::compose_trajectory_from_centerline().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_tangent_orientations() [2/2]

std::vector< double > carma_wm::geometry::compute_tangent_orientations ( const std::vector< lanelet::BasicPoint2d > &  centerline)

Definition at line 565 of file Geometry.cpp.

566{
568}

References compute_templated_tangent_orientations().

Here is the call graph for this function:

◆ compute_templated_arc_lengths()

template<class P , class A >
std::vector< double > carma_wm::geometry::compute_templated_arc_lengths ( const std::vector< P, A > &  data)

Definition at line 475 of file Geometry.cpp.

475 {
476 std::vector<double> out;
477 double total = 0;
478 double diff = 0;
479 for (size_t i = 0; i < data.size(); i++) {
480 if (i == 0) {
481 out.push_back(0);
482 } else {
483 diff = compute_euclidean_distance(data[i - 1], data[i]);
484 total += diff;
485 out.push_back(total);
486 }
487 }
488
489 return out;
490}
double compute_euclidean_distance(const Eigen::Vector2d &a, const Eigen::Vector2d &b)
Compute the Euclidean distance between the two points.
Definition: Geometry.cpp:504

References compute_euclidean_distance(), process_traj_logs::data, and process_bag::i.

Referenced by compute_arc_lengths().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_templated_finite_differences()

template<class P , class A >
std::vector< Eigen::Vector2d > carma_wm::geometry::compute_templated_finite_differences ( const std::vector< P, A > &  data)

Definition at line 391 of file Geometry.cpp.

392{
393 std::vector<Eigen::Vector2d> out;
394 Eigen::Vector2d diff;
395 Eigen::Vector2d vec_i_plus_1;
396 Eigen::Vector2d vec_i_minus_1;
397
398 for (size_t i = 0; i < data.size(); i++) {
399 if (i == 0) {
400 // Compute forward derivative
401 diff = (data[i + 1] - data[i]);
402 } else if (i == data.size() - 1) {
403 // Compute backward derivative
404 diff = (data[i] - data[i - 1]);
405 } else {
406 // Else, compute centered derivative
407 vec_i_plus_1 = data[i + 1];
408 vec_i_minus_1 = data[i - 1];
409 diff = (vec_i_plus_1 - vec_i_minus_1)/2.0;
410 }
411
412 out.push_back(diff);
413 }
414
415 return out;
416}

References process_traj_logs::data, and process_bag::i.

Referenced by compute_finite_differences(), and compute_templated_tangent_orientations().

Here is the caller graph for this function:

◆ compute_templated_tangent_orientations()

template<class P , class A >
std::vector< double > carma_wm::geometry::compute_templated_tangent_orientations ( const std::vector< P, A > &  centerline)

Definition at line 532 of file Geometry.cpp.

533{
534 std::vector<double> out;
535 if (centerline.empty())
536 return out;
537
538 std::vector<Eigen::Vector2d> tangents = carma_wm::geometry::compute_templated_finite_differences(centerline);
539
540 for (auto tangent : tangents)
541 {
542 geometry_msgs::msg::Quaternion q;
543
544 // Derive angle by cos theta = (u . v)/(||u| * ||v||)
545 double yaw = 0;
546 double norm = tangent.norm();
547 if (norm != 0.0) {
548 auto normalized_tanged = tangent / norm;
549 yaw = atan2(normalized_tanged[1], normalized_tanged[0]);
550 }
551
552 out.push_back(yaw);
553 }
554
555 return out;
556}

References compute_templated_finite_differences().

Referenced by compute_tangent_orientations().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ computeCurvature()

double carma_wm::geometry::computeCurvature ( const lanelet::BasicPoint2d &  p1,
const lanelet::BasicPoint2d &  p2,
const lanelet::BasicPoint2d &  p3 
)

Function for computing curvature from 3 points.

This function is a direct copy of the function by the same name found in the lanelet2_validation package which was not exposed for use The original function can be found here https://github.com/fzi-forschungszentrum-informatik/Lanelet2/blob/master/lanelet2_validation/src/validators/CurvatureTooBig.cpp The source function is copyrighted under BSD 3-Clause "New" or "Revised" License a copy of that notice has been included with this package

Parameters
p1The first point
p2The second point
p3The third point
Returns
The computed curvature in 1/m units.

Definition at line 276 of file Geometry.cpp.

278{
279 auto dp = 0.5 * (p3 - p1);
280 auto ddp = p3 - 2.0 * p2 + p1;
281 auto denom = std::pow(dp.x() * dp.x() + dp.y() * dp.y(), 3.0 / 2.0);
282 if (std::fabs(denom) < 1e-20)
283 {
284 denom = 1e-20;
285 }
286 return static_cast<double>((ddp.y() * dp.x() - dp.y() * ddp.x()) / denom);
287}

◆ concatenate_lanelets()

lanelet::BasicLineString2d carma_wm::geometry::concatenate_lanelets ( const std::vector< lanelet::ConstLanelet > &  lanelets)

Helper function to a list of lanelets together and return the result. Neither LineString is modified in this function.

Definition at line 316 of file Geometry.cpp.

317{
318 if (lanelets.empty()) {
319 return lanelet::BasicLineString2d();
320 }
321 lanelet::BasicLineString2d centerline_points = lanelets[0].centerline2d().basicLineString();
322 lanelet::BasicLineString2d new_points;
323 for (size_t i = 1; i < lanelets.size(); i++) {
324 new_points = lanelets[i].centerline2d().basicLineString();
325 centerline_points = concatenate_line_strings(centerline_points, new_points);
326 }
327
328 return centerline_points;
329}
lanelet::BasicLineString2d concatenate_line_strings(const lanelet::BasicLineString2d &l1, const lanelet::BasicLineString2d &l2)
Helper function to concatenate 2 linestrings together and return the result. Neither LineString is mo...
Definition: Geometry.cpp:373

References concatenate_line_strings(), and process_bag::i.

Referenced by local_curvatures().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ concatenate_line_strings()

lanelet::BasicLineString2d carma_wm::geometry::concatenate_line_strings ( const lanelet::BasicLineString2d &  l1,
const lanelet::BasicLineString2d &  l2 
)

Helper function to concatenate 2 linestrings together and return the result. Neither LineString is modified in this function.

Definition at line 373 of file Geometry.cpp.

375{
376 lanelet::BasicLineString2d out;
377
378 int start_offset = 0;
379 if (!a.empty() && !b.empty() && compute_euclidean_distance(a.back(), b.front()) < SPATIAL_EPSILON_M) {
380 start_offset = 1;
381 }
382
383 out.insert(out.end(), a.begin(), a.end());
384 out.insert(out.end(), b.begin() + start_offset, b.end());
385
386 return out;
387}
constexpr double SPATIAL_EPSILON_M
Definition: Geometry.cpp:28

References compute_euclidean_distance(), and SPATIAL_EPSILON_M.

Referenced by concatenate_lanelets(), and basic_autonomy::waypoint_generation::create_lanefollow_geometry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAngleBetweenVectors()

double carma_wm::geometry::getAngleBetweenVectors ( const Eigen::Vector2d &  vec1,
const Eigen::Vector2d &  vec2 
)

Calculates the angle between two vectors.

Parameters
vec1the first vector
vec2the second vector
Returns
The angle in rad between the two vectors

Definition at line 65 of file Geometry.cpp.

66{
67 double vec1Mag = vec1.norm();
68 double vec2Mag = vec2.norm();
69 if (vec1Mag == 0.0 || vec2Mag == 0.0)
70 {
71 return 0;
72 }
73 return safeAcos(vec1.dot(vec2) / (vec1Mag * vec2Mag));
74}
double safeAcos(double x)
acos method that caps the input x value for 1 or -1 to avoid undefined output. This is meant to preve...
Definition: Geometry.cpp:34

References safeAcos().

Referenced by carma_wm::query::getAffectedLaneletOrAreas(), and trackPos().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ local_circular_arc_curvatures()

std::vector< double > carma_wm::geometry::local_circular_arc_curvatures ( const std::vector< lanelet::BasicPoint2d > &  points,
int  lookahead 
)

Computes the curvature of each point in the provided list by calling circular_arc_curvature using that point and the point at index current point index + lookahead. In this way the curvature of a path is effectively low pass filtered by the size of the lookahead value.

Parameters
pointsThe points to compute the curvature for
lookaheadThe lookahead index distance to use for computing the curvature at each point
Returns
A vector of computed curvatures that has size equal to points.size()

Definition at line 610 of file Geometry.cpp.

610 {
611 std::vector<double> curvatures;
612 curvatures.reserve(points.size());
613
614 if (lookahead <= 0) {
615 throw std::invalid_argument("local_circular_arc_curvatures lookahead must be greater than 0");
616 }
617
618 if (points.empty()) {
619 return curvatures;
620 }
621 else if (points.size() == 1) {
622 curvatures.push_back(0.0);
623 return curvatures;
624 }
625
626 for (size_t i = 0; i < points.size() - 1; i++)
627 {
628 size_t next_point_index = i + lookahead;
629 if (next_point_index >= points.size()) {
630 next_point_index = points.size() - 1;
631 }
632 double cur = circular_arc_curvature(points[i], points[next_point_index]);
633 curvatures.push_back(fabs(cur));
634 }
635 curvatures.push_back(curvatures.back());
636 return curvatures;
637}
double circular_arc_curvature(const lanelet::BasicPoint2d &cur_point, const lanelet::BasicPoint2d &next_point)
Computes the curvature of a circular arc that passes through the two provided points....
Definition: Geometry.cpp:596

References circular_arc_curvature(), and process_bag::i.

Here is the call graph for this function:

◆ local_curvatures() [1/3]

std::vector< double > carma_wm::geometry::local_curvatures ( const lanelet::BasicLineString2d &  centerline_points)

Returns a list of local (computed by discrete derivative) curvatures for the input centerlines. The list of returned curvatures matches 1-to-1 with with list of points in the input centerlines.

The numerical accuracy of this method is greatly increased by using larger collections of points as inputs as the first 2 and final 2 points in the list must be computed using alternative differentiation methods from all the others resulting in greater error. It is also important for this function to yield useful results that all points in the input lanelet's centerline are actually on the centerline of the lanelet (i.e. none to minimal linear interpolation of points) as this results in "flat" spots on an otherwise smooth curve that causes 0 curvature to be computed.

NOTE: The accuracy of this method is very susceptable to input noise. Consider using local_circular_arc_curvatures for calculations using noisy data

Parameters
centerline_pointsThe list of points to compute curvatures for
Exceptions
std::invalid_argumentIf one of the provided lanelets cannot have its centerline computed
Returns
A list of continuous centerline segments and their respective curvatures

Definition at line 356 of file Geometry.cpp.

357{
358 return templated_local_curvatures(centerline_points);
359}
std::vector< double > templated_local_curvatures(const std::vector< P, A > &centerline_points)
Definition: Geometry.cpp:333

References templated_local_curvatures().

Referenced by local_curvatures().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ local_curvatures() [2/3]

std::vector< double > carma_wm::geometry::local_curvatures ( const std::vector< lanelet::BasicPoint2d > &  centerline_points)

Definition at line 362 of file Geometry.cpp.

363{
364 return templated_local_curvatures(centerline_points);
365}

References templated_local_curvatures().

Here is the call graph for this function:

◆ local_curvatures() [3/3]

std::vector< double > carma_wm::geometry::local_curvatures ( const std::vector< lanelet::ConstLanelet > &  lanelets)

Specialized overload of the centerline local_curvatures method but for lanelets.

Parameters
laneletsThe list of points to compute curvatures for
Exceptions
std::invalid_argumentIf one of the provided lanelets cannot have its centerline computed

Definition at line 368 of file Geometry.cpp.

368 {
369 return local_curvatures(concatenate_lanelets(lanelets));
370}
std::vector< double > local_curvatures(const lanelet::BasicLineString2d &centerline_points)
Returns a list of local (computed by discrete derivative) curvatures for the input centerlines....
Definition: Geometry.cpp:356
lanelet::BasicLineString2d concatenate_lanelets(const std::vector< lanelet::ConstLanelet > &lanelets)
Helper function to a list of lanelets together and return the result. Neither LineString is modified ...
Definition: Geometry.cpp:316

References concatenate_lanelets(), and local_curvatures().

Here is the call graph for this function:

◆ matchSegment()

std::tuple< TrackPos, lanelet::BasicSegment2d > carma_wm::geometry::matchSegment ( const lanelet::BasicPoint2d &  p,
const lanelet::BasicLineString2d &  line_string 
)

Get the TrackPos of the provided point along the linestring and the segment of the line string which the provided point should be considered in.

This function iterates over the provided linestring from front to back. First the nearest vertex point on the linestring to the external point is found Once the nearest point has been found next step is to determine which segment it should go with using the following rules. If the minimum point is the first point then use the first segment If the minimum point is the last point then use the last segment If the minimum point is within the downtrack bounds of one segment but not the other then use the one it is within If the minimum point is within the downtrack bounds of both segments then use the one with the smallest crosstrack distance If the minimum point is within the downtrack bounds of both segments and has exactly equal crosstrack bounds with each segment then use the first one

Parameters
pointThe 2d point to match with a segment
line_stringThe line_string to match against
Exceptions
std::invalid_argumentif line string contains only one point
Returns
An std::tuple where the first element is the TrackPos of the point and the second element is the matched segment

Definition at line 187 of file Geometry.cpp.

189{
190 if (line_string.size() < 2)
191 {
192 throw std::invalid_argument("Provided with linestring containing fewer than 2 points");
193 }
194
195 lanelet::BasicSegment2d best_segment =
196 std::make_pair(line_string[0], line_string[1]); // Default to starting segment if no match is found
197 auto point_2d = lanelet::utils::to2D(p);
198
199 double min_distance = lanelet::geometry::distance2d(point_2d, line_string[0]);
200 size_t best_point_index = 0;
201 double best_accumulated_length = 0;
202 double best_last_accumulated_length = 0;
203 double best_seg_length = 0;
204 double best_last_seg_length = 0;
205 double last_seg_length = 0;
206
207 double accumulated_length = 0;
208 double last_accumulated_length = 0;
209 for (size_t i = 0; i < line_string.size(); i++)
210 { // Iterate over line string to find nearest point
211 double seg_length = 0;
212 if (i < line_string.size() - 1)
213 {
214 seg_length = lanelet::geometry::distance2d(line_string[i], line_string[i + 1]); // Compute segment length
215 }
216
217 double distance = lanelet::geometry::distance2d(p, line_string[i]); // Compute from current point to external point
218 if (distance < min_distance)
219 { // If this distance is below minimum discovered so far then update minimum
220 min_distance = distance;
221 best_point_index = i;
222 best_accumulated_length = accumulated_length;
223 best_last_accumulated_length = last_accumulated_length; // Record accumulated lengths to each segment
224 best_seg_length = seg_length;
225 best_last_seg_length = last_seg_length;
226 }
227
228 last_accumulated_length = accumulated_length; // Update accumulated lenths
229 accumulated_length += seg_length;
230 last_seg_length = seg_length;
231 }
232
233 // Minimum point has been found next step is to determine which segment it should go with using the following rules.
234 // If the minimum point is the first point then use the first segment
235 // If the minimum point is the last point then use the last segment
236 // If the minimum point is within the downtrack bounds of one segment but not the other then use the one it is within
237 // If the minimum point is within the downtrack bounds of both segments then use the one with the smallest crosstrack
238 // distance If the minimum point is within the downtrack bounds of both segments and has exactly equal crosstrack
239 // bounds with each segment then use the first one
240 TrackPos best_pos(0, 0);
241 if (best_point_index == 0)
242 {
243 best_pos = trackPos(p, line_string[0], line_string[1]);
244 best_segment = std::make_pair(line_string[0], line_string[1]);
245 }
246 else if (best_point_index == line_string.size() - 1)
247 {
248 best_pos = trackPos(p, line_string[line_string.size() - 2], line_string[line_string.size() - 1]);
249 best_pos.downtrack += best_last_accumulated_length;
250 best_segment = std::make_pair(line_string[line_string.size() - 2], line_string[line_string.size() - 1]);
251 }
252 else
253 {
254 TrackPos first_seg_trackPos = trackPos(p, line_string[best_point_index - 1], line_string[best_point_index]);
255 TrackPos second_seg_trackPos = trackPos(p, line_string[best_point_index], line_string[best_point_index + 1]);
256 if (selectFirstSegment(first_seg_trackPos, second_seg_trackPos, best_last_seg_length, best_seg_length))
257 {
258 best_pos = first_seg_trackPos;
259 best_pos.downtrack += best_last_accumulated_length;
260 best_segment = std::make_pair(line_string[best_point_index - 1], line_string[best_point_index]);
261 }
262 else
263 {
264 best_pos = second_seg_trackPos;
265 best_pos.downtrack += best_accumulated_length;
266 best_segment = std::make_pair(line_string[best_point_index], line_string[best_point_index + 1]);
267 }
268 }
269
270 // couldn't find a matching segment, so use the first segment within the downtrack range of.
271 // Or the starting segment assuming we are before the route
272 return std::make_tuple(best_pos, best_segment);
273}
bool selectFirstSegment(const TrackPos &first_seg_trackPos, const TrackPos &second_seg_trackPos, double first_seg_length, double second_seg_length)
Helper function to identify whether to select the preceeding or succeeding segment of a linstring poi...
Definition: Geometry.cpp:156
TrackPos trackPos(const lanelet::ConstLanelet &lanelet, const lanelet::BasicPoint2d &point)
Returns the TrackPos, computed in 2d, of the provided point relative to the centerline of the provide...
Definition: Geometry.cpp:120

References carma_wm::TrackPos::downtrack, process_bag::i, selectFirstSegment(), and trackPos().

Referenced by carma_wm::CARMAWorldModel::routeTrackPos(), and trackPos().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ normalize_vectors()

std::vector< Eigen::Vector2d > carma_wm::geometry::normalize_vectors ( const std::vector< Eigen::Vector2d > &  vectors)

Normalize the vectors in the input list such that their magnitudes = 1.

Definition at line 510 of file Geometry.cpp.

511{
512 std::vector<Eigen::Vector2d> out;
513 for (auto vec : vectors) {
514 out.push_back(vec.normalized());
515 }
516 return out;
517}

Referenced by templated_local_curvatures().

Here is the caller graph for this function:

◆ objectToMapPolygon()

lanelet::BasicPolygon2d carma_wm::geometry::objectToMapPolygon ( const geometry_msgs::msg::Pose &  pose,
const geometry_msgs::msg::Vector3 &  size 
)

Uses the provided pose and size vector of an ExternalObject to compute what the polygon would be of that object would be if viewed from the same frame as the pose is defined relative to.

Parameters
posethe pose of an external object
sizeThe size vector of an external object
Returns
A polygon of 4 points describing the object aligned bounds starting with the upper left point in the object frame and moving clockwise.

Definition at line 289 of file Geometry.cpp.

290{
291 tf2::Transform object_tf;
292 tf2::fromMsg(pose, object_tf);
293
294 double half_x_bound = size.x / 2;
295 double half_y_bound = size.y / 2;
296
297 // 4 corners of the object starting with upper left and moving in clockwise direction in pose frame
298 tf2::Vector3 obj_p1(half_x_bound, half_y_bound, 0);
299 tf2::Vector3 obj_p2(half_x_bound, -half_y_bound, 0);
300 tf2::Vector3 obj_p3(-half_x_bound, -half_y_bound, 0);
301 tf2::Vector3 obj_p4(-half_x_bound, half_y_bound, 0);
302
303 tf2::Vector3 obj_p1_map = object_tf * obj_p1;
304 tf2::Vector3 obj_p2_map = object_tf * obj_p2;
305 tf2::Vector3 obj_p3_map = object_tf * obj_p3;
306 tf2::Vector3 obj_p4_map = object_tf * obj_p4;
307
308 lanelet::BasicPoint2d p1(obj_p1_map.getX(), obj_p1_map.getY());
309 lanelet::BasicPoint2d p2(obj_p2_map.getX(), obj_p2_map.getY());
310 lanelet::BasicPoint2d p3(obj_p3_map.getX(), obj_p3_map.getY());
311 lanelet::BasicPoint2d p4(obj_p4_map.getX(), obj_p4_map.getY());
312
313 return { p1, p2, p3, p4 };
314}

Referenced by carma_wm::CARMAWorldModel::distToNearestObjInLane(), carma_wm::CARMAWorldModel::getInLaneObjects(), carma_wm::CARMAWorldModel::getIntersectingLanelet(), and carma_wm::CARMAWorldModel::toRoadwayObstacle().

Here is the caller graph for this function:

◆ point_to_point_yaw()

double carma_wm::geometry::point_to_point_yaw ( const lanelet::BasicPoint2d &  cur_point,
const lanelet::BasicPoint2d &  next_point 
)

Computes the 2d orientation of the vector from the provided start point to end point.

Parameters
cur_pointThe start point of the orientation vector. Units m
next_pointThe end point of the orientation vector. Units m
Returns
The rotation of the vector around the +Z axis in radians

Definition at line 588 of file Geometry.cpp.

589{
590 double dx = next_point[0] - cur_point[0];
591 double dy = next_point[1] - cur_point[1];
592 double yaw = atan2(dy, dx);
593 return yaw;
594}

Referenced by circular_arc_curvature(), and carma_wm::CARMAWorldModel::pointFromRouteTrackPos().

Here is the caller graph for this function:

◆ rpyFromQuaternion() [1/2]

void carma_wm::geometry::rpyFromQuaternion ( const geometry_msgs::msg::Quaternion &  q_msg,
double &  roll,
double &  pitch,
double &  yaw 
)

Extract extrinsic roll-pitch-yaw from quaternion.

Parameters
qThe quaternion message to convert
rollThe output variable for roll units will be radians
pitchThe output variable for pitch units will be radians
yawThe output variable for yaw units will be radians

Definition at line 58 of file Geometry.cpp.

59{
60 tf2::Quaternion quat;
61 tf2::convert(q_msg, quat);
62 rpyFromQuaternion(quat, roll, pitch, yaw);
63}
void rpyFromQuaternion(const tf2::Quaternion &q, double &roll, double &pitch, double &yaw)
Extract extrinsic roll-pitch-yaw from quaternion.
Definition: Geometry.cpp:52
void convert(const carma_v2x_msgs::msg::PSM &in_msg, carma_perception_msgs::msg::ExternalObject &out_msg, const std::string &map_frame_id, double pred_period, double pred_step_size, const lanelet::projection::LocalFrameProjector &map_projector, const tf2::Quaternion &ned_in_map_rotation, rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock)

References motion_computation::conversion::convert(), and rpyFromQuaternion().

Here is the call graph for this function:

◆ rpyFromQuaternion() [2/2]

void carma_wm::geometry::rpyFromQuaternion ( const tf2::Quaternion &  q,
double &  roll,
double &  pitch,
double &  yaw 
)

Extract extrinsic roll-pitch-yaw from quaternion.

Parameters
qThe quaternion to convert
rollThe output variable for roll units will be radians
pitchThe output variable for pitch units will be radians
yawThe output variable for yaw units will be radians

Definition at line 52 of file Geometry.cpp.

53{
54 tf2::Matrix3x3 mat(q);
55 mat.getRPY(roll, pitch, yaw);
56}

Referenced by plan_delegator::PlanDelegator::composePlanTrajectoryRequest(), and rpyFromQuaternion().

Here is the caller graph for this function:

◆ safeAcos()

double carma_wm::geometry::safeAcos ( double  x)

acos method that caps the input x value for 1 or -1 to avoid undefined output. This is meant to prevent issues with floating point approximations. The user should still make sure the input data is expected to be within the [-1, 1] range for this method to give accurate results.

Parameters
xThe angle in radians
Returns
The arc cosine of x

Definition at line 34 of file Geometry.cpp.

35{
36 if (x < -1.0) x = -1.0 ;
37 else if (x > 1.0) x = 1.0 ;
38 return std::acos(x) ;
39}

References process_traj_logs::x.

Referenced by getAngleBetweenVectors().

Here is the caller graph for this function:

◆ safeAsin()

double carma_wm::geometry::safeAsin ( double  x)

asin method that caps the input x value for 1 or -1 to avoid undefined output. This is meant to prevent issues with floating point approximations. The user should still make sure the input data is expected to be within the [-1, 1] range for this method to give accurate results.

Parameters
xThe angle in radians
Returns
The arc sine of x

Definition at line 45 of file Geometry.cpp.

46{
47 if (x < -1.0) x = -1.0 ;
48 else if (x > 1.0) x = 1.0 ;
49 return std::asin(x) ;
50}

References process_traj_logs::x.

◆ selectFirstSegment()

bool carma_wm::geometry::selectFirstSegment ( const TrackPos first_seg_trackPos,
const TrackPos second_seg_trackPos,
double  first_seg_length,
double  second_seg_length 
)

Helper function to identify whether to select the preceeding or succeeding segment of a linstring point that is nearest an external point when trying to find the TrackPos of the external point. Of the 3 points comprising the 2 segment linestirng the external point must be closest to the midpoint for this function to be valid.

ASSUMPTION: Of the 3 points comprising the 2 segment linestirng the external point is closest to the midpoint

Meant to be called in the matchSegment method. Logic is as follows If the external point is within the downtrack bounds of one segment but not the other then use the one it is within If the external point is within the downtrack bounds of both segments then use the one with the smallest crosstrack distance If the external point is within the downtrack bounds of both segments and has exactly equal crosstrack bounds with each segment then use the preceeding segment If the external point is outside the downtrack bounds of both segments then assign to the first segment as it will always have positive downtrack in this case while the second segment will always have negative downtrack

Parameters
first_seg_trackPosThe TrackPos of the external point relative to the preceeding segment
second_seg_trackPosThe TrackPos of the succeeding segment
first_seg_lengthThe length of the preceeding segment
second_seg_lengthThe length of the succeeding segment
Returns
True if the preceeding segment is the better choice. False if the succeeding segment is the better choice.

Definition at line 156 of file Geometry.cpp.

158{
159 const bool first_seg_in_downtrack =
160 (0 <= first_seg_trackPos.downtrack && first_seg_trackPos.downtrack < first_seg_length);
161 const bool second_seg_in_downtrack =
162 (0 <= second_seg_trackPos.downtrack && second_seg_trackPos.downtrack < second_seg_length);
163
164 if (first_seg_in_downtrack && !second_seg_in_downtrack)
165 { // If in the first segment but not the last segment
166
167 return true; // First segment is better
168 }
169 else if (second_seg_in_downtrack && !first_seg_in_downtrack)
170 {
171 return false; // Second segment is better
172 }
173 else if (first_seg_in_downtrack && second_seg_in_downtrack)
174 {
175 return first_seg_trackPos.crosstrack <=
176 second_seg_trackPos.crosstrack; // Pick the first segment if the crosstrack values are equal or the first
177 // segment is closer
178 }
179 else
180 { // Point lies outside the downtrack bounds of both segments (Remember According to function contract the nearest
181 // point to external point is the midpoint of the two segments)
182 return true; // Choose first segment as it will always have positive downtrack in this case while the second
183 // segment will always have negative downtrack
184 }
185}

References carma_wm::TrackPos::crosstrack, and carma_wm::TrackPos::downtrack.

Referenced by matchSegment().

Here is the caller graph for this function:

◆ templated_local_curvatures()

template<class P , class A >
std::vector< double > carma_wm::geometry::templated_local_curvatures ( const std::vector< P, A > &  centerline_points)

Definition at line 333 of file Geometry.cpp.

334{
335
336 if (centerline_points.empty()) {
337 throw std::invalid_argument("No points in centerline for curvature calculation");
338 }
339
340 std::vector<Eigen::Vector2d> spatial_derivative = compute_finite_differences(centerline_points);
341 std::vector<Eigen::Vector2d> normalized_tangent_vectors = normalize_vectors(spatial_derivative);
342
343 std::vector<double> arc_lengths = compute_arc_lengths(centerline_points);
344
345 std::vector<Eigen::Vector2d> tangent_derivative =
347 normalized_tangent_vectors,
348 arc_lengths);
349
350 std::vector<double> curvature = compute_magnitude_of_vectors(tangent_derivative);
351
352 return curvature;
353}
std::vector< Eigen::Vector2d > normalize_vectors(const std::vector< Eigen::Vector2d > &vectors)
Normalize the vectors in the input list such that their magnitudes = 1.
Definition: Geometry.cpp:510
std::vector< double > compute_arc_lengths(const std::vector< lanelet::BasicPoint2d > &data)
Compute the arc length at each point around the curve.
Definition: Geometry.cpp:498
std::vector< Eigen::Vector2d > compute_finite_differences(const lanelet::BasicLineString2d &data)
Use finite differences methods to compute the derivative of the input data set with respect to index ...
Definition: Geometry.cpp:419
std::vector< double > compute_magnitude_of_vectors(const std::vector< Eigen::Vector2d > &vectors)
Compute the magnitude of each vector in the input list.
Definition: Geometry.cpp:520

References compute_arc_lengths(), compute_finite_differences(), compute_magnitude_of_vectors(), and normalize_vectors().

Referenced by local_curvatures().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ trackPos() [1/2]

TrackPos carma_wm::geometry::trackPos ( const lanelet::BasicPoint2d &  p,
const lanelet::BasicPoint2d &  seg_start,
const lanelet::BasicPoint2d &  seg_end 
)

Return the track position of a point relative to a line segment defined by two other points. Positive crosstrack will be to the right. Points occuring before the segment will have negative downtrack.

Parameters
pThe point to find the TrackPos of
seg_startThe starting point of a line segment
seg_endThe ending point of a line segment
Returns
The TrackPos of point p relative to the line defined by seg_start and seg_end

Calculate the sign of the crosstrack distance by projecting the points to 2d d = (p_x - s_x)(e_y - s_y) - (p_y - s_y)(e_x - s_x) Equivalent to d = (start_to_p.x * start_to_end.y) - (start_to_p.y * start_to_end.x)

Code below based on math equation described at https://math.stackexchange.com/questions/274712/calculate-on-which-side-of-a-straight-line-is-a-given-point-located Question asked by user Ritvars (https://math.stackexchange.com/users/56723/ritvars) and answered by user Shard (https://math.stackexchange.com/users/55608/shard) Attribution here is in line with Stack Overflow's Attribution policy cc-by-sa found here: https://stackoverflow.blog/2009/06/25/attribution-required/

Calculate the sign of the crosstrack distance by projecting the points to 2d d = (p_x - s_x)(e_y - s_y) - (p_y - s_y)(e_x - s_x) Equivalent to d = (start_to_p.x * start_to_end.y) - (start_to_p.y * start_to_end.x)

Code below based on math equation described at https://math.stackexchange.com/questions/274712/calculate-on-which-side-of-a-straight-line-is-a-given-point-located Question asked by user Ritvars (https://math.stackexchange.com/users/56723/ritvars) and answered by user Shard (https://math.stackexchange.com/users/55608/shard) Attribution here is in line with Stack Overflow's Attribution policy cc-by-sa found here: https://stackoverflow.blog/2009/06/25/attribution-required/

Definition at line 76 of file Geometry.cpp.

78{
79 Eigen::Vector2d vec_to_p(p);
80 Eigen::Vector2d vec_to_start(seg_start);
81 Eigen::Vector2d vec_to_end(seg_end);
82
83 // Get vector from start to external point
84 Eigen::Vector2d start_to_p = vec_to_p - vec_to_start;
85
86 // Get vector from start to end point
87 Eigen::Vector2d start_to_end = vec_to_end - vec_to_start;
88
89 // Get angle between both vectors
90 double interior_angle = getAngleBetweenVectors(start_to_p, start_to_end);
91
92 // Calculate downtrack distance
93 double start_to_p_mag = start_to_p.norm();
94 double downtrack_dist = start_to_p_mag * std::cos(interior_angle);
95
111 double d = (start_to_p[0] * start_to_end[1]) - (start_to_p[1] * start_to_end[0]);
112 double sign = d >= 0 ? 1.0 : -1.0; // If d is positive then the point is to the right if it is negative the point is
113 // to the left
114
115 double crosstrack = start_to_p_mag * std::sin(interior_angle) * sign;
116
117 return TrackPos(downtrack_dist, crosstrack);
118}
Position in a track based coordinate system where the axis are downtrack and crosstrack....
Definition: TrackPos.hpp:35
double getAngleBetweenVectors(const Eigen::Vector2d &vec1, const Eigen::Vector2d &vec2)
Calculates the angle between two vectors.
Definition: Geometry.cpp:65

References getAngleBetweenVectors().

Here is the call graph for this function:

◆ trackPos() [2/2]

TrackPos carma_wm::geometry::trackPos ( const lanelet::ConstLanelet &  lanelet,
const lanelet::BasicPoint2d &  point 
)

Returns the TrackPos, computed in 2d, of the provided point relative to the centerline of the provided lanelet. Positive crosstrack will be to the right. Points occuring before the segment will have negative downtrack. See the matchSegment function for a description of the edge cases associated with the max_crosstrack parameter.

Parameters
laneletThe lanelet whose centerline will serve as the TrackPos reference line
pointThe lanelet2 point which will have its distance computed
max_crosstrackThe maximum crosstrack distance allowed for a perfect centerline segment match. See the matchSegment function for details
Exceptions
std::invalid_argumentIf the lanelet centerline cannot be found
Returns
The TrackPos of the point relative to the lanelet centerline

Definition at line 120 of file Geometry.cpp.

121{
122 auto center_line = lanelet::utils::to2D(lanelet.centerline());
123
124 if (center_line.numSegments() < 1)
125 {
126 throw std::invalid_argument("Provided lanelet has invalid centerline containing no points");
127 }
128 auto tuple = matchSegment(point, center_line.basicLineString());
129
130 return std::get<0>(tuple);
131}
std::tuple< TrackPos, lanelet::BasicSegment2d > matchSegment(const lanelet::BasicPoint2d &p, const lanelet::BasicLineString2d &line_string)
Get the TrackPos of the provided point along the linestring and the segment of the line string which ...
Definition: Geometry.cpp:187

References matchSegment(), and process_traj_logs::point.

Referenced by route::RouteGeneratorWorker::bumperPoseCb(), route::RouteGeneratorWorker::composeRouteMarkerMsg(), carma_wm_ctrl::WMBroadcaster::distToNearestActiveGeofence(), carma_wm::query::getAffectedLaneletOrAreas(), carma_wm::CARMAWorldModel::getNearestObjInLane(), matchSegment(), carma_wm::query::nonConnectedAdjacentLeft(), carma_wm::CARMAWorldModel::routeTrackPos(), carma_wm_ctrl::WMBroadcaster::splitLaneletWithPoint(), carma_wm_ctrl::WMBroadcaster::splitOppositeLaneletWithPoint(), and carma_wm::CARMAWorldModel::toRoadwayObstacle().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ SPATIAL_EPSILON_M

constexpr double carma_wm::geometry::SPATIAL_EPSILON_M = 0.05
constexpr

Definition at line 28 of file Geometry.cpp.

Referenced by concatenate_line_strings().