polyline
triwarp.polyline
Open and closed 3D polyline operations.
The polyline_ prefix. Where a name carries the module's own token, the token comes
first -- polyline_length, polyline_open, polyline_resample,
polyline_triangulate -- so the whole family sorts and completes together. The measures used to
take the prefix while the operations took it as a suffix, which put one convention in two places
for no reason.
A function that does not need the token does not gain one:
is_closed and
cumulative_arc_length have an unambiguous subject
already, and triangulate_polygon takes a 2D polygon
rather than a polyline, so the token there would be wrong rather than redundant.
polyline_point_distance keeps both nouns because the
prefix alone would lose "from what"; it is spelled to match
points.point_plane_distance.
The closed= convention. Ten of these functions -- length, centroid, radius, angles,
point-to-curve distance, and the five resampling operations -- take a keyword-only
closed: bool = False. Passing closed=True appends the closing edge back to the first point
if it is absent (polyline_close) and then does the open
computation on that, so the seam is treated like any other segment. Three of the ten additionally
drop the duplicated seam point on the way out, so their result is a clean cyclic ring with one entry
per original point rather than one extra:
polyline_resample,
polyline_angles and
polyline_smooth_upsample.
The keyword is not always needed. polyline_angles and
polyline_smooth_upsample already detect an
explicitly closed input -- one whose last point equals its first -- with
is_closed; closed=True is for the common case of a loop stored
without that duplicate, which is the form
boundary_loops returns.
is_closed
is_closed(polyline: array[vec3]) -> bool
Whether a polyline is closed (its first and last points coincide).
The endpoint comparison runs on-device via allclose, so no array
is copied to the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
See Also
Source code in triwarp/polyline.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
polyline_open
polyline_open(polyline: array[vec3]) -> wp.array[wp.vec3]
Open a polyline by dropping the last point when it duplicates the first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
Returns:
| Type | Description |
|---|---|
array[vec3]
|
The input unchanged when it has fewer than two points or is already open;
otherwise a length |
See Also
Source code in triwarp/polyline.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
polyline_close
polyline_close(polyline: array[vec3]) -> wp.array[wp.vec3]
Close a polyline by appending the first point when it is not already the last.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
Returns:
| Type | Description |
|---|---|
array[vec3]
|
The input unchanged when it has fewer than two points or is already closed;
otherwise a length |
See Also
Source code in triwarp/polyline.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
polyline_length
polyline_length(
polyline: array[vec3], *, closed: bool = False
) -> float
Total arc length of a polyline (sum of segment lengths).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
float
|
The summed segment length, |
See Also
cumulative_arc_length
The same lengths, unreduced.
Source code in triwarp/polyline.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
polyline_centroid
polyline_centroid(
polyline: array[vec3], *, closed: bool = False
) -> wp.vec3
Segment-length-weighted centroid of a polyline.
Each segment contributes its midpoint weighted by its length, so the result is invariant to how densely the polyline is sampled (unlike the plain mean of the vertices).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
vec3
|
The weighted centroid on the host. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the polyline has fewer than two points. |
See Also
polyline_normal
polyline_radius
Both default their plane to this centroid.
Source code in triwarp/polyline.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
polyline_normal
polyline_normal(polyline: array[vec3]) -> wp.vec3
Average unit normal of a 3D polyline via Newell's method.
The polyline is treated as a closed loop (its closing edge is added if absent), then the
cross products of consecutive vertices, cross(V_i, V_{i + 1}), are summed and normalized.
This is Newell's method: the summed cross products of consecutive position vectors around a
closed loop equal the area-weighted, translation-invariant plane normal. The open-chain
variant (omitting the closing edge) is origin-dependent and not computed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
Returns:
| Type | Description |
|---|---|
vec3
|
The unit normal on the host. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the polyline has fewer than three points. |
Source code in triwarp/polyline.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
polyline_point_distance
polyline_point_distance(
points: array[vec3],
polyline: array[vec3],
*,
closed: bool = False,
) -> wp.array[wp.float32]
Minimum distance from each query point to the nearest segment of a polyline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array[vec3]
|
|
required |
polyline
|
array[vec3]
|
|
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
array[float32]
|
Length |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in triwarp/polyline.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
polyline_upsample
polyline_upsample(
polyline: array[vec3],
step_size: float,
*,
closed: bool = False,
) -> wp.array[wp.vec3]
Upsample a polyline to an approximately uniform step size.
Each segment is split into max(floor(length / step_size), 1) equal pieces. The final
endpoint of the polyline is not emitted (matching the source implementation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
step_size
|
float
|
Target spacing between consecutive output points. |
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
The upsampled polyline. The input is returned unchanged for fewer than two points. |
See Also
polyline_smooth_upsample
The same subdivision with the new points placed on a fitted arc instead of the chord.
polyline_downsample
polyline_resample
Source code in triwarp/polyline.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
polyline_smooth_upsample
polyline_smooth_upsample(
polyline: array[vec3],
step_size: float,
*,
closed: bool = False,
) -> wp.array[wp.vec3]
Upsample a polyline to an approximately uniform step size, following local curvature.
Like polyline_upsample, each segment is split into
max(floor(length / step_size), 1) pieces and the final endpoint is not emitted. Unlike it,
the inserted points are placed on a circular arc fitted to the segment's endpoint tangents
(estimated from the two bracketing neighbour vertices) rather than on the straight chord, so a
coarsely sampled curve is refined smoothly. Curvature-aware placement is usually stated for the
edge midpoint alone; here it is generalised to every interpolation parameter.
The first and last segments of an open polyline have no bracketing neighbour and are subdivided linearly; collinear neighbours likewise reduce to the straight chord. Original vertices are preserved exactly, since each segment's first sample coincides with its start vertex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
step_size
|
float
|
Target spacing between consecutive output points. |
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
The curvature-aware upsampled polyline. The input is returned unchanged for fewer than two points. |
See Also
polyline_upsample
The straight-chord version, which is what this reduces to on the end segments.
Source code in triwarp/polyline.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
cumulative_arc_length
cumulative_arc_length(
polyline: array[vec3],
) -> wp.array[wp.float32]
Cumulative arc length from the first vertex to each vertex of an open polyline.
The underlying arc-length parametrization behind
polyline_downsample and
polyline_resample; exposed directly for callers
doing custom resampling along the polyline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
Returns:
| Type | Description |
|---|---|
array[float32]
|
Length- |
Source code in triwarp/polyline.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
polyline_downsample
polyline_downsample(
polyline: array[vec3],
step_size: float,
*,
closed: bool = False,
) -> wp.array[wp.vec3]
Downsample a polyline to a minimum arc-length spacing between kept points.
Greedily keeps the first point, then each subsequent point at least step_size of arc
length beyond the previously kept point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
step_size
|
float
|
Minimum arc-length distance between kept points. |
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
The downsampled polyline. The input is returned unchanged for fewer than two points. |
See Also
polyline_simplify
Drops points by shape error rather than by spacing.
polyline_upsample
Source code in triwarp/polyline.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
polyline_simplify
polyline_simplify(
polyline: array[vec3],
tol: float,
*,
closed: bool = False,
) -> tuple[wp.array[wp.vec3], wp.array[wp.int32]]
Simplify a polyline with the Ramer-Douglas-Peucker algorithm.
Drops interior vertices whose perpendicular distance to the chord spanning a kept sub-range is
at most tol; the first and last vertices are always retained. The recursion is evaluated
level-synchronously rather than depth-first -- one round of four dim=n launches per
level of the split tree, so the cost is the tree's depth (about log2(n) on a mesh
boundary loop) rather than one thread's walk of the whole tree. The accepted set is identical
either way, since breadth-first and depth-first evaluation of the same recursion accept the
same points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
tol
|
float
|
Maximum Euclidean distance allowed between a dropped vertex and the retained chord. |
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
tuple[array[vec3], array[int32]]
|
|
Notes
The round loop runs on device, driven by wp.capture_while exactly as
polyline_triangulate's ear rounds are, so however
many levels the split tree has, the loop itself costs one graph launch and no readback. The one
host synchronisation in the call is the compaction that follows it, where
flatnonzero reads back the kept count in order to size
indices. The accepted index set is identical to a single-thread recursive evaluation of the
same recursion, since breadth-first and depth-first evaluation accept the same points.
The depth is bounded by the accepted count, not by n, and that is why there is no round
cap and no serial fallback here. Every root-to-leaf path of the split tree accepts one point
per level, so rounds <= kept + 1 -- a deep tree is precisely an input that accepts most of
its points, which is also the input the recursion does the most work on. A spiral's depth tracks
its turn count rather than n, while a power curve, a geometric staircase and a decaying
sawtooth are all shallower than a boundary loop.
This is deliberately one algorithm on both devices, with no serial fallback and no
host-driven loop for small inputs: the fixed cost of graph capture, the keep-mask compaction
and the round loop itself all scale with the tree depth rather than with n, so a size-gated
fallback would add a second implementation (and the test that its accepted set agrees with this
one's) without a reliable win. On the CPU backend a dim=n launch runs as a single lane, so
each round costs O(n) sequential work rather than the host's O(n log n) recursive total
-- the device is the target, so this is recorded rather than branched on.
See Also
polyline_downsample
Drops points by spacing rather than by shape error.
Source code in triwarp/polyline.py
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | |
polyline_resample
polyline_resample(
polyline: array[vec3],
num_points: int,
*,
closed: bool = False,
) -> wp.array[wp.vec3]
Resample a polyline to a fixed number of points evenly spaced by arc length.
Points are sampled at num_points arc lengths evenly spanning [0, total_length] and
linearly interpolated between the bracketing vertices (matching numpy.interp semantics).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
num_points
|
int
|
Number of output points. |
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
array[vec3]
|
Length |
See Also
polyline_upsample
Targets a step size instead of a point count.
Source code in triwarp/polyline.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 | |
polyline_radius
polyline_radius(
polyline: array[vec3],
reduction: Literal[
"min", "max", "mean", "median"
] = "min",
center: vec3 | None = None,
normal: vec3 | None = None,
*,
closed: bool = False,
) -> float
Radius of a polyline projected onto the plane through center with the given normal.
Each segment is projected onto the plane and its closest point to center is found; the
per-segment distances to center are then reduced. With reduction="min" this is the
inner radius (nearest point, not vertex, on the polyline).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
reduction
|
Literal['min', 'max', 'mean', 'median']
|
Reduction over the per-segment radial distances: |
'min'
|
center
|
vec3 | None
|
Plane origin. Defaults to |
None
|
normal
|
vec3 | None
|
Plane normal (need not be unit). Defaults to
These two are the circle's frame, not a cutting plane, so they deliberately keep their own
names and their own order rather than the |
None
|
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
float
|
The reduced radius. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
See Also
polyline_centroid
polyline_normal
The two defaults for the plane.
median
Source code in triwarp/polyline.py
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 | |
polyline_angles
polyline_angles(
polyline: array[vec3], *, closed: bool = False
) -> wp.array[wp.float32]
Angles between consecutive segments at each vertex of a polyline.
Returns one angle per point. For an open polyline the two endpoints get an angle of 0;
for a closed polyline (first point equal to last) the turning angles wrap around.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
closed
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
array[float32]
|
Length |
See Also
is_closed
What decides the wrap-around when closed is left False.
Source code in triwarp/polyline.py
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 | |
polyline_triangulate
polyline_triangulate(
polyline: array[vec3],
) -> twt.Array2dInt32
Triangulate the simple planar polygon bounded by a closed 3D polyline (ear clipping).
The polyline is treated as the boundary of a simple polygon, which is filled with triangles
whose vertices are the polyline vertices themselves — no new (Steiner) points are introduced.
A simple n-gon yields n - 2 non-overlapping triangles that cover the polygon. The loop
is first projected onto its best-fit plane (via
polyline_normal) so any planar loop works, not only
ones lying in the xy plane.
The implementation is a GPU-parallel port of ear_clipping.cpp from libigl: convex polygons
use a single fan, while non-convex polygons clip a maximal independent set of ears per round
until the polygon is exhausted. Returned faces are consistently wound counter-clockwise with
respect to the loop's turning direction; the exact set of triangles may differ from a
sequential ear clip, but every triangulation of a simple polygon has n - 2 faces.
Every launch in the round loop is dim=n, so the cost is set by the round count, and the
round count by how many ears the independent-set rule can retire at once. Competing ears are
ranked by a bijective hash of their ring index rather than by the index itself, which is what
keeps that logarithmic: under the raw index an alternating star lets the ear at i - 2
suppress the ear at i for every i, so one ear is clipped per round and the loop runs its
full n-round cap, where the hash keeps the round count near log2(n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polyline
|
array[vec3]
|
|
required |
Returns:
| Type | Description |
|---|---|
Array2dInt32
|
|
Notes
The round loop runs on device, driven by wp.capture_while over a device-side condition
exactly as bfs drives its levels, so the whole clip costs one graph
launch and one readback (the final face count) rather than a readback per round.
The prologue is fused. The plane frame
(polyline_normal, its internal
polyline_close closure test, and
polyline_centroid) is built as one accumulation pass,
one single-thread finalize and one projection, living in device memory and never crossing to
the host -- rather than as three separate host-scope reductions each ending in its own
readback. Three readbacks are left in the whole function, every one of them structural:
polyline_open's is_closed, which decides n and
therefore every launch dimension; the reflex count that selects the convex fan fast path, which
is the last one a convex loop pays; and the face count above, which sizes the returned slice.
When conditional graph nodes are unavailable (CPU, or a CUDA driver below 12.4)
wp.capture_while executes the same loop directly with one pinned 4-byte readback per round,
which is the behaviour this loop had throughout. Verified equal: the CPU fallback and the
captured CUDA loop produce the same triangulation up to row order on a 64-point star, and that
row order was never stable on CUDA either — clip_selected appends through an atomic.
See Also
Source code in triwarp/polyline.py
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 | |
triangulate_polygon
triangulate_polygon(
polygon: array[vec2],
) -> tuple[wp.array[wp.vec2], wp.array[wp.int32]]
Triangulate a simple 2D polygon by ear clipping, adding no new vertices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygon
|
array[vec2]
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
ring |
array[vec2]
|
The input ring with any repeated closing point removed; the vertices |
faces |
array[int32]
|
Length- |
Notes
Differs from trimesh.creation.triangulate_polygon in three ways, all of which follow from
replacing the CPU polygon libraries (mapbox_earcut / manifold3d / triangle) with
triwarp's own GPU ear clipper, polyline_triangulate:
- The input is a
wp.vec2ring, not ashapely.geometry.Polygon, and interior rings (holes) are not supported. - No Steiner points are ever inserted, which is trimesh's
force_vertices=Truecontract rather than its default. - There is no
engineselection, andfacesis flat rather than(m, 3), matching the face layout used throughout triwarp.
The ring must be a simple (non self-intersecting) polygon. A degenerate ring yields a partial
triangulation with fewer than n - 2 triangles rather than raising.
Source code in triwarp/polyline.py
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 | |