|
| 1 | +"""Polyline utilities using CGAL.""" |
| 2 | + |
| 3 | +from typing import List |
| 4 | +from typing import Union |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +from numpy.typing import NDArray |
| 8 | + |
| 9 | +from compas_cgal import _types_std # noqa: F401 # Load vector type bindings |
| 10 | +from compas_cgal._polylines import closest_points_on_polyline as _closest_points |
| 11 | +from compas_cgal._polylines import simplify_polylines as _simplify |
| 12 | + |
| 13 | +PointsList = Union[List[List[float]], NDArray] |
| 14 | + |
| 15 | + |
| 16 | +__all__ = ["simplify_polylines", "simplify_polyline", "closest_points_on_polyline"] |
| 17 | + |
| 18 | + |
| 19 | +def simplify_polylines(polylines: List[PointsList], threshold: float) -> List[NDArray]: |
| 20 | + """Simplify multiple polylines using Douglas-Peucker algorithm. |
| 21 | +
|
| 22 | + Simplification is performed in the XY plane only. For 3D polylines, |
| 23 | + Z coordinates are preserved but not considered in distance calculations. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + polylines : list of array-like |
| 28 | + List of polylines. Each polyline is a sequence of 2D or 3D points. |
| 29 | + threshold : float |
| 30 | + Distance threshold for simplification. Higher values remove more points. |
| 31 | +
|
| 32 | + Returns |
| 33 | + ------- |
| 34 | + list of ndarray |
| 35 | + Simplified polylines as numpy arrays. |
| 36 | +
|
| 37 | + Examples |
| 38 | + -------- |
| 39 | + >>> polylines = [[[0, 0], [1, 0.01], [2, 0]], [[0, 0], [0, 1], [1, 1]]] |
| 40 | + >>> simplified = simplify_polylines(polylines, threshold=0.1) |
| 41 | + >>> len(simplified[0]) # First polyline simplified |
| 42 | + 2 |
| 43 | + >>> len(simplified[1]) # Second has corner, preserved |
| 44 | + 3 |
| 45 | +
|
| 46 | + """ |
| 47 | + if threshold < 0: |
| 48 | + raise ValueError("threshold must be non-negative") |
| 49 | + arrays = [np.asarray(p, dtype=np.float64) for p in polylines] |
| 50 | + return _simplify(arrays, threshold) |
| 51 | + |
| 52 | + |
| 53 | +def simplify_polyline(polyline: PointsList, threshold: float) -> NDArray: |
| 54 | + """Simplify a single polyline using Douglas-Peucker algorithm. |
| 55 | +
|
| 56 | + Simplification is performed in the XY plane only. For 3D polylines, |
| 57 | + Z coordinates are preserved but not considered in distance calculations. |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + polyline : array-like |
| 62 | + Sequence of 2D or 3D points. |
| 63 | + threshold : float |
| 64 | + Distance threshold for simplification. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + ndarray |
| 69 | + Simplified polyline as numpy array. |
| 70 | +
|
| 71 | + """ |
| 72 | + result = simplify_polylines([polyline], threshold) |
| 73 | + return result[0] |
| 74 | + |
| 75 | + |
| 76 | +def closest_points_on_polyline(query_points: PointsList, polyline: PointsList) -> NDArray: |
| 77 | + """Find closest points on a polyline for a batch of query points. |
| 78 | +
|
| 79 | + Uses CGAL's AABB tree for efficient batch queries. |
| 80 | +
|
| 81 | + Parameters |
| 82 | + ---------- |
| 83 | + query_points : array-like |
| 84 | + Query points as Mx2 or Mx3 array. |
| 85 | + polyline : array-like |
| 86 | + Polyline as Nx2 or Nx3 array. |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + ndarray |
| 91 | + Closest points on the polyline (same shape as query_points). |
| 92 | +
|
| 93 | + Examples |
| 94 | + -------- |
| 95 | + >>> polyline = [[0, 0], [10, 0]] |
| 96 | + >>> queries = [[5, 5], [3, -2]] |
| 97 | + >>> closest = closest_points_on_polyline(queries, polyline) |
| 98 | + >>> closest[0] # Closest to (5, 5) on horizontal line |
| 99 | + array([5., 0.]) |
| 100 | +
|
| 101 | + """ |
| 102 | + queries = np.asarray(query_points, dtype=np.float64) |
| 103 | + poly = np.asarray(polyline, dtype=np.float64) |
| 104 | + return _closest_points(queries, poly) |
0 commit comments