Ir al contenido principal

raygeo.geo.algo.fitting

Curve and primitive fitting algorithms.

Provides functions for fitting arcs, lines, circles, and beziers to point sequences. Includes recursive fitting with primitives, polyline linearization, and evaluating fitting quality (line and arc deviation).

Functions

are_points_collinear_3d()

are_points_collinear_3d(
points: Sequence[types.Point3D],
tolerance: float = 1e-06,
) -> bool

Check if three or more points are collinear within tolerance.

ParameterTypeDescription
pointsSequence[types.Point3D]Sequence of 3D points.
tolerancefloat = 1e-06Collinearity tolerance.
ReturnsboolTrue if points are collinear.
ComplexityO(n) time, O(1) space

fit_circle_to_3_points()

fit_circle_to_3_points(
p1: types.Point2DOr3D,
p2: types.Point2DOr3D,
p3: types.Point2DOr3D,
) -> Optional[tuple[types.Point, float]]

Fit a circle to three points.

ParameterTypeDescription
p1types.Point2DOr3DFirst point (x, y) or (x, y, z).
p2types.Point2DOr3DSecond point (x, y) or (x, y, z).
p3types.Point2DOr3DThird point (x, y) or (x, y, z).
ReturnsOptional[tuple[types.Point, float]]Tuple of (center, radius) or None.
ComplexityO(1) time, O(1) space

Circle fitted to three points

Circle fitted to three points

fit_circle_to_points_3d()

fit_circle_to_points_3d(
points: Sequence[types.Point3D],
) -> Optional[tuple[types.Point, float, float]]

Fit a circle to a set of points.

ParameterTypeDescription
pointsSequence[types.Point3D]Sequence of 3D points to fit.
ReturnsOptional[tuple[types.Point, float, float]]Tuple of (center, radius, error) or None.
ComplexityO(n) time, O(1) space

Circle fitted to points

Circle fitted to points

fit_points_recursive()

fit_points_recursive(
points: Sequence[types.Point3D],
tolerance: float,
start_idx: int,
end_idx: int,
) -> geo.Geometry

Recursively fit points with line and arc primitives.

ParameterTypeDescription
pointsSequence[types.Point3D]Sequence of 3D points to fit.
tolerancefloatFitting tolerance.
start_idxintStart index in the points array.
end_idxintEnd index in the points array.
Returnsgeo.GeometryGeometry of fitted commands.
ComplexityO(n log n) average time, O(n) space

fit_points_with_primitives()

fit_points_with_primitives(
points: Sequence[types.Point3D],
tolerance: float,
) -> geo.Geometry

Fit a polyline of points with arc and line primitives.

ParameterTypeDescription
pointsSequence[types.Point3D]Sequence of 3D points to fit.
tolerancefloatFitting tolerance.
Returnsgeo.GeometryGeometry of fitted commands.
ComplexityO(n log n) average time, O(n) space

Fitted primitives

Fitted primitives

flatten_to_points_3d()

flatten_to_points_3d(
geometry: geo.Geometry,
tolerance: float,
) -> list[list[types.Point3D]]

Flatten curves into linear segments.

ParameterTypeDescription
geometrygeo.GeometryGeometry to flatten.
tolerancefloatFlattening tolerance.
Returnslist[list[types.Point3D]]List of flattened point segments.
ComplexityO(n + m) time, O(m) space where n is the number of commands and m the number of output points

Arc curve flattened to dense line segments

Arc curve flattened to dense line segments

generate_arc_between_two_points()

generate_arc_between_two_points(
p0: tuple[float, float],
p1: tuple[float, float],
offset: float,
min_radius: float,
z: float,
resolution: float,
) -> Optional[list[tuple[float, float, float]]]

Fit a circular arc through two points with a perpendicular offset.

Uses a third point offset perpendicularly from the chord midpoint to define the arc shape. Returns a linearized arc polyline, or None if the radius would be below min_radius or the geometry is degenerate.

ParameterTypeDescription
p0tuple[float, float]Start point (x, y).
p1tuple[float, float]End point (x, y).
offsetfloatPerpendicular offset from the chord midpoint.
min_radiusfloatMinimum allowed arc radius.
zfloatZ-coordinate for all output points.
resolutionfloatArc linearization resolution.
ReturnsOptional[list[tuple[float, float, float]]]List of 3D points forming the arc, or None.
ComplexityO(n) time, O(n) space where n depends on arc length and resolution

Arc through two points with + and - perpendicular offset

Arc through two points with + and - perpendicular offset

generate_linking_arc()

generate_linking_arc(
start: tuple[float, float, float],
end: tuple[float, float, float],
min_radius: float,
z: float,
) -> list[tuple[float, float, float]]

Generate a smooth linking arc between two points.

Uses generate_arc_between_two_points internally with an offset derived from min_radius. Falls back to a straight-line interpolation if no valid arc can be fit.

ParameterTypeDescription
starttuple[float, float, float]Start 3D point (x, y, z).
endtuple[float, float, float]End 3D point (x, y, z).
min_radiusfloatMinimum allowed arc radius.
zfloatZ-coordinate for all output points (overrides start.z / end.z).
Returnslist[tuple[float, float, float]]List of 3D points forming the linking arc.
ComplexityO(n) time, O(n) space where n scales with chord_length / min_radius

Linking arc with varying minimum radius constraints

Linking arc with varying minimum radius constraints

get_polyline_arc_deviation()

get_polyline_arc_deviation(
points: Sequence[types.Point3D],
center: types.Point,
radius: float,
) -> float

Get the maximum arc deviation for a set of points.

ParameterTypeDescription
pointsSequence[types.Point3D]Sequence of 3D points.
centertypes.PointArc center (x, y).
radiusfloatArc radius.
ReturnsfloatMaximum deviation from the arc.
ComplexityO(n) time, O(1) space

Maximum deviation from a reference arc

Maximum deviation from a reference arc

get_polyline_line_deviation()

get_polyline_line_deviation(
points: Sequence[types.Point3D],
start: int,
end: int,
) -> tuple[float, int]

Get the maximum line deviation for a segment of a polyline.

ParameterTypeDescription
pointsSequence[types.Point3D]Sequence of 3D points.
startintStart index.
endintEnd index.
Returnstuple[float, int]Tuple of (max_deviation, index_of_max).
ComplexityO(n) time, O(1) space

Maximum deviation from a chord

Maximum deviation from a chord

linearize_geometry()

linearize_geometry(geometry: geo.Geometry, tolerance: float) -> geo.Geometry

Linearize geometry data into line segments.

ParameterTypeDescription
geometrygeo.GeometryGeometry to linearize.
tolerancefloatLinearization tolerance.
Returnsgeo.GeometryLinearized Geometry.
ComplexityO(n + m) time, O(m) space where n is the number of commands and m the number of output segments

Arc curve linearized with RDP simplification

Arc curve linearized with RDP simplification

project_circle_center_to_bisector()

project_circle_center_to_bisector(
p1: types.Point2DOr3D,
p2: types.Point2DOr3D,
center: types.Point,
) -> types.Point

Project a circle center onto the perpendicular bisector of two points.

ParameterTypeDescription
p1types.Point2DOr3DFirst point (x, y) or (x, y, z).
p2types.Point2DOr3DSecond point (x, y) or (x, y, z).
centertypes.PointCircle center to project.
Returnstypes.PointProjected center point (x, y).
ComplexityO(1) time, O(1) space

Circle center projected onto the perpendicular bisector

Circle center projected onto the perpendicular bisector