src/CoreGeometry/include/CoreGeometry/line_geometry.hpp
src/CoreGeometry/include/CoreGeometry/line_geometry.hpp
Types
| Name | |
|---|---|
| enum class | ClipSide { KeepBase, KeepDistal} |
| enum class | ExtendEndpoint { Base, Distal} |
Functions
| Name | |
|---|---|
| float | calc_length(Line2D const & line) Calculate the total calc_length of a line using Euclidean distance. |
| float | calc_length2(Line2D const & line) |
| std::vector< float > | calc_cumulative_length_vector(Line2D const & line) Calculate cumulative distances along a line. |
| std::optional< Point2D< float > > | point_at_distance(Line2D const & line, float target_distance, bool use_interpolation =true) Find the point at a specific distance along a line. |
| std::optional< Point2D< float > > | point_at_fractional_position(Line2D const & line, float position, bool use_interpolation =true) Find the point at a fractional position along a line (0.0 to 1.0). |
| std::vector< Point2D< float > > | extract_line_subsegment_by_distance(Line2D const & line, float start_position, float end_position, bool preserve_original_spacing =true) Extract a subsegment of a line between two fractional positions. |
| Point2D< float > | get_position_at_percentage(Line2D const & line, float percentage) Calculate the position along a line at a given percentage of cumulative length. |
| Line2D | get_segment_between_percentages(Line2D const & line, float start_percentage, float end_percentage) Extract a segment of a line between two percentage points along its cumulative distance. |
| Point2D< float > | calculate_perpendicular_direction(Line2D const & line, size_t vertex_index) Calculate the perpendicular direction at a specific vertex of a line. |
| float | point_to_line_segment_distance2(Point2D< float > const & point, Point2D< float > const & line_start, Point2D< float > const & line_end) Calculate the squared distance from a point to a line segment. |
| std::optional< Point2D< float > > | line_segment_intersection(Point2D< float > const & p1, Point2D< float > const & p2, Point2D< float > const & p3, Point2D< float > const & p4) Find the intersection point between two line segments. |
| std::optional< std::pair< Point2D< float >, size_t > > | find_line_intersection(Line2D const & line, Line2D const & reference_line) Find the first intersection between a polyline and a reference polyline. |
| Line2D | clip_line_at_intersection(Line2D const & line, Line2D const & reference_line, ClipSide clip_side) Clip a line at the intersection with a reference line. |
| std::optional< Point2D< float > > | ray_segment_intersection(Point2D< float > const & origin, Point2D< float > const & direction, Point2D< float > const & seg_a, Point2D< float > const & seg_b) Find the intersection of a ray with a line segment. |
| Point2D< float > | calculate_endpoint_extension_direction(Line2D const & line, ExtendEndpoint extend_end, float tangent_distance_pixels) Outward unit tangent at a polyline endpoint for extension. |
| Line2D | extend_line_at_reference(Line2D const & line, Line2D const & reference_line, ExtendEndpoint extend_end, float tangent_distance_pixels) Extend a polyline from base or distal along a smoothed tangent until a reference polyline is hit. |
| float | point_to_line_min_distance2(Point2D< float > const & point, Line2D const & line) |
| Line2D | reverse_line(Line2D const & line) Reverse the order of points in a line. |
| bool | is_distal_end_closer(Line2D const & line, Point2D< float > const & reference_point) Determine if the distal (last) point of a line is closer to a reference point. |
Types Documentation
enum ClipSide
| Enumerator | Value | Description |
|---|---|---|
| KeepBase | ||
| KeepDistal |
enum ExtendEndpoint
| Enumerator | Value | Description |
|---|---|---|
| Base | Extend from the first point of the polyline. | |
| Distal | Extend from the last point of the polyline. |
Functions Documentation
function calc_length
float calc_length(
Line2D const & line
)Calculate the total calc_length of a line using Euclidean distance.
Parameters:
- line The line to calculate calc_length for
Return: Total calc_length of the line, or 0.0 if line has fewer than 2 points
function calc_length2
float calc_length2(
Line2D const & line
)function calc_cumulative_length_vector
std::vector< float > calc_cumulative_length_vector(
Line2D const & line
)Calculate cumulative distances along a line.
Parameters:
- line The line to calculate distances for
Return: Vector of cumulative distances from start of line to each point
function point_at_distance
std::optional< Point2D< float > > point_at_distance(
Line2D const & line,
float target_distance,
bool use_interpolation =true
)Find the point at a specific distance along a line.
Parameters:
- line The line to search
- target_distance The distance from start of line
- use_interpolation Whether to interpolate between points or use nearest
Return: The point at the specified distance, or nullopt if line is empty
function point_at_fractional_position
std::optional< Point2D< float > > point_at_fractional_position(
Line2D const & line,
float position,
bool use_interpolation =true
)Find the point at a fractional position along a line (0.0 to 1.0).
Parameters:
- line The line to search
- position Fractional position (0.0 = start, 1.0 = end)
- use_interpolation Whether to interpolate between points or use nearest
Return: The point at the specified position, or nullopt if line is empty
function extract_line_subsegment_by_distance
std::vector< Point2D< float > > extract_line_subsegment_by_distance(
Line2D const & line,
float start_position,
float end_position,
bool preserve_original_spacing =true
)Extract a subsegment of a line between two fractional positions.
Parameters:
- line The line to extract from
- start_position Start position as fraction (0.0 to 1.0)
- end_position End position as fraction (0.0 to 1.0)
- preserve_original_spacing Whether to keep original points or resample
Return: Vector of points forming the subsegment
function get_position_at_percentage
Point2D< float > get_position_at_percentage(
Line2D const & line,
float percentage
)Calculate the position along a line at a given percentage of cumulative length.
Parameters:
- line The line to calculate position on
- percentage Percentage along the line (0.0 = start, 1.0 = end)
Return: Point2D
function get_segment_between_percentages
Line2D get_segment_between_percentages(
Line2D const & line,
float start_percentage,
float end_percentage
)Extract a segment of a line between two percentage points along its cumulative distance.
Parameters:
- line The input line (vector of Point2D)
- start_percentage Start percentage (0.0 to 1.0, where 0.0 is beginning, 1.0 is end)
- end_percentage End percentage (0.0 to 1.0, where 0.0 is beginning, 1.0 is end)
Return: Line2D containing the extracted segment
Note:
- If start_percentage >= end_percentage, returns empty line
- If line is empty or has only one point, returns empty line
- Percentages are clamped to [0.0, 1.0] range
This function calculates the cumulative distance along the line and extracts a continuous segment between the specified start and end percentages.
function calculate_perpendicular_direction
Point2D< float > calculate_perpendicular_direction(
Line2D const & line,
size_t vertex_index
)Calculate the perpendicular direction at a specific vertex of a line.
Parameters:
- line The line to calculate perpendicular direction for
- vertex_index The index of the vertex (0 to line.size()-1)
Return: Point2D
Note:
- Returns (0,0) if line has fewer than 2 points or vertex_index is invalid
- For edge vertices, uses the single adjacent segment
- For internal vertices, averages perpendiculars from both adjacent segments
For internal vertices (1 to n-1), calculates the average of perpendicular directions from adjacent segments. For edge vertices (0 and n-1), calculates the perpendicular of the single adjacent segment.
function point_to_line_segment_distance2
float point_to_line_segment_distance2(
Point2D< float > const & point,
Point2D< float > const & line_start,
Point2D< float > const & line_end
)Calculate the squared distance from a point to a line segment.
Parameters:
- point The point to measure distance from
- line_start Start point of the line segment
- line_end End point of the line segment
Return: Squared distance from the point to the nearest point on the segment
function line_segment_intersection
std::optional< Point2D< float > > line_segment_intersection(
Point2D< float > const & p1,
Point2D< float > const & p2,
Point2D< float > const & p3,
Point2D< float > const & p4
)Find the intersection point between two line segments.
Parameters:
- p1 First point of first segment
- p2 Second point of first segment
- p3 First point of second segment
- p4 Second point of second segment
Return: The intersection point if it exists, or std::nullopt if segments don’t intersect
function find_line_intersection
std::optional< std::pair< Point2D< float >, size_t > > find_line_intersection(
Line2D const & line,
Line2D const & reference_line
)Find the first intersection between a polyline and a reference polyline.
Parameters:
- line The line to check for intersections (tested segment by segment)
- reference_line Reference polyline; intersections require crossing a reference segment
Return: Intersection point on line and the segment index of line if found, nullopt otherwise
function clip_line_at_intersection
Line2D clip_line_at_intersection(
Line2D const & line,
Line2D const & reference_line,
ClipSide clip_side
)Clip a line at the intersection with a reference line.
Parameters:
- line The line to clip
- reference_line The reference line to clip against
- clip_side Which side of the intersection to keep
Return: Clipped line including a vertex at the intersection when it falls between existing points, or original line if no intersection found
function ray_segment_intersection
std::optional< Point2D< float > > ray_segment_intersection(
Point2D< float > const & origin,
Point2D< float > const & direction,
Point2D< float > const & seg_a,
Point2D< float > const & seg_b
)Find the intersection of a ray with a line segment.
Parameters:
- origin Ray origin
- direction Ray direction (need not be unit length)
- seg_a First endpoint of the segment
- seg_b Second endpoint of the segment
Return: Intersection point if the ray hits the segment at positive parameter along direction
function calculate_endpoint_extension_direction
Point2D< float > calculate_endpoint_extension_direction(
Line2D const & line,
ExtendEndpoint extend_end,
float tangent_distance_pixels
)Outward unit tangent at a polyline endpoint for extension.
Parameters:
- line Input polyline
- extend_end Which endpoint to compute the direction at
- tangent_distance_pixels Arc length from the endpoint into the line used to define the tangent (interpolated along the polyline). Values larger than the line length use the full polyline.
Return: Normalized outward direction, or (0,0) if the line is too short or the lookback distance is invalid
function extend_line_at_reference
Line2D extend_line_at_reference(
Line2D const & line,
Line2D const & reference_line,
ExtendEndpoint extend_end,
float tangent_distance_pixels
)Extend a polyline from base or distal along a smoothed tangent until a reference polyline is hit.
Parameters:
- line Line to extend
- reference_line Reference polyline to intersect
- extend_end Endpoint from which to extend
- tangent_distance_pixels Arc length from the endpoint used to define the extension direction (pixels)
Return: Extended line with an added vertex at the intersection, or line if no valid hit
function point_to_line_min_distance2
float point_to_line_min_distance2(
Point2D< float > const & point,
Line2D const & line
)function reverse_line
Line2D reverse_line(
Line2D const & line
)Reverse the order of points in a line.
Parameters:
- line The line to reverse
Return: A new Line2D with points in reversed order, or empty if input is empty
Creates a new line with points in reverse order, so the first point becomes the last and vice versa. Useful for normalizing line orientation.
function is_distal_end_closer
bool is_distal_end_closer(
Line2D const & line,
Point2D< float > const & reference_point
)Determine if the distal (last) point of a line is closer to a reference point.
Parameters:
- line The line to evaluate
- reference_point The point to measure distance to
Return:
- true if the last point is closer to reference_point than the first point
- false if the first point is closer or equal, or if line has fewer than 2 points
Compares the squared distance from both endpoints to the reference point. Returns true if the last point is strictly closer than the first point.
Source code
#ifndef LINE_GEOMETRY_HPP
#define LINE_GEOMETRY_HPP
#include "CoreGeometry/lines.hpp"
#include <optional>
enum class ClipSide {
KeepBase, // Keep the portion from line start to intersection
KeepDistal// Keep the portion from intersection to line end
};
enum class ExtendEndpoint {
Base,
Distal
};
float calc_length(Line2D const & line);
float calc_length2(Line2D const & line);
std::vector<float> calc_cumulative_length_vector(Line2D const & line);
std::optional<Point2D<float>> point_at_distance(
Line2D const & line,
float target_distance,
bool use_interpolation = true);
std::optional<Point2D<float>> point_at_fractional_position(
Line2D const & line,
float position,
bool use_interpolation = true);
std::vector<Point2D<float>> extract_line_subsegment_by_distance(
Line2D const & line,
float start_position,
float end_position,
bool preserve_original_spacing = true);
Point2D<float> get_position_at_percentage(Line2D const & line, float percentage);
Line2D get_segment_between_percentages(Line2D const & line, float start_percentage, float end_percentage);
Point2D<float> calculate_perpendicular_direction(Line2D const & line, size_t vertex_index);
float point_to_line_segment_distance2(
Point2D<float> const & point,
Point2D<float> const & line_start,
Point2D<float> const & line_end);
std::optional<Point2D<float>> line_segment_intersection(
Point2D<float> const & p1, Point2D<float> const & p2,
Point2D<float> const & p3, Point2D<float> const & p4);
std::optional<std::pair<Point2D<float>, size_t>> find_line_intersection(
Line2D const & line, Line2D const & reference_line);
Line2D clip_line_at_intersection(
Line2D const & line,
Line2D const & reference_line,
ClipSide clip_side);
std::optional<Point2D<float>> ray_segment_intersection(
Point2D<float> const & origin,
Point2D<float> const & direction,
Point2D<float> const & seg_a,
Point2D<float> const & seg_b);
Point2D<float> calculate_endpoint_extension_direction(
Line2D const & line,
ExtendEndpoint extend_end,
float tangent_distance_pixels);
Line2D extend_line_at_reference(
Line2D const & line,
Line2D const & reference_line,
ExtendEndpoint extend_end,
float tangent_distance_pixels);
float point_to_line_min_distance2(Point2D<float> const & point, Line2D const & line);
Line2D reverse_line(Line2D const & line);
bool is_distal_end_closer(Line2D const & line, Point2D<float> const & reference_point);
#endif// LINE_GEOMETRY_HPPUpdated on 2026-11-13 at 20:25:42 +0000