Skip to content

getPolygon with 0 points #1122

Closed
Closed
@TauTheLepton

Description

@TauTheLepton

Describe the bug
The CatmullRomSpline member function getPolygon when called with num_points = 0 raises an error with a description failed to calculate curve index.
This description does not say where the real error is in this case. It is thrown deep from the call stack by member function getCurveIndexAndS in the call order presented below:
getPolygon -> getLeftBounds -> getNormalVector -> getCurveIndexAndS
This happens because the num_points (= 0) is used in getLeftBounds to calculate step_size (which is then equal to infinity).

double step_size = getLength() / static_cast<double>(num_points);

This leads to a NaN s value which then is not matched to any component curve and a mentioned exception is being thrown by getCurveIndexAndS.
double s = step_size * static_cast<double>(i);

In my opinion there are three ways to improve the behavior in this situation:

  • To catch the exception in the getPolygon function and thrown another exception with more informative description
  • To just throw an exception in getPolygon when num_points == 0
  • To add an early return in getPolygon to return an empty vector when num_points == 0

Context:
I am adding unit tests for the geometry package and wanted to add tests for the getPolygon function.

To Reproduce
Steps to reproduce the behavior:

  1. Edit any existing source code file or create a new one
  2. Create any CatmullRomSpline
  3. Call the getPolygon member function with num_points argument set to 0
  4. Compile and execute
  5. See the error

Expected behavior
Either an exception with an appropriate description or the return of an empty vector.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]: Ubuntu
  • Browser [e.g. chrome, safari]: Firefox
  • Version [e.g. 22]: 22
  • ROS 2 version: Humble
  • DDS: CycloneDDS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions