Skip to content

Commit d032cba

Browse files
committed
feat(projection): cleanup precise projection computation
- round vertices to precision - remove empty faces - add precise eps parameter - update docstrings
1 parent 621a5dd commit d032cba

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

trimesh/base.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,20 +2700,28 @@ def projected(self, normal: ArrayLike, **kwargs) -> Path2D:
27002700
27012701
Parameters
27022702
----------
2703-
check : bool
2704-
If True make sure is flat
27052703
normal : (3,) float
27062704
Normal to extract flat pattern along
27072705
origin : None or (3,) float
27082706
Origin of plane to project mesh onto
2709-
pad : float
2707+
ignore_sign : bool
2708+
Allow a projection from the normal vector in
2709+
either direction: this provides a substantial speedup
2710+
on watertight meshes where the direction is irrelevant
2711+
but if you have a triangle soup and want to discard
2712+
backfaces you should set this to False.
2713+
rpad : float
27102714
Proportion to pad polygons by before unioning
27112715
and then de-padding result by to avoid zero-width gaps.
2716+
apad : float
2717+
Absolute padding to pad polygons by before unioning
2718+
and then de-padding result by to avoid zero-width gaps.
27122719
tol_dot : float
27132720
Tolerance for discarding on-edge triangles.
2714-
max_regions : int
2715-
Raise an exception if the mesh has more than this
2716-
number of disconnected regions to fail quickly before unioning.
2721+
precise : bool
2722+
Use the precise projection computation using shapely.
2723+
precise_eps : float
2724+
Tolerance for precise triangle checks.
27172725
27182726
Returns
27192727
----------

trimesh/path/polygons.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ def projected(
735735
apad=None,
736736
tol_dot=1e-10,
737737
precise: bool = False,
738+
precise_eps: float = 1e-10,
738739
):
739740
"""
740741
Project a mesh onto a plane and then extract the polygon
@@ -753,8 +754,6 @@ def projected(
753754
----------
754755
mesh : trimesh.Trimesh
755756
Source geometry
756-
check : bool
757-
If True make sure is flat
758757
normal : (3,) float
759758
Normal to extract flat pattern along
760759
origin : None or (3,) float
@@ -773,10 +772,10 @@ def projected(
773772
and then de-padding result by to avoid zero-width gaps.
774773
tol_dot : float
775774
Tolerance for discarding on-edge triangles.
776-
max_regions : int
777-
Raise an exception if the mesh has more than this
778-
number of disconnected regions to fail quickly before
779-
unioning.
775+
precise : bool
776+
Use the precise projection computation using shapely.
777+
precise_eps : float
778+
Tolerance for precise triangle checks.
780779
781780
Returns
782781
----------
@@ -829,16 +828,14 @@ def projected(
829828
vertices_2D = transform_points(mesh.vertices, to_2D)[:, :2]
830829

831830
if precise:
832-
eps = 1e-10
833831
faces = mesh.faces[side]
834-
# just union all the polygons
835-
return (
836-
ops.unary_union(
837-
[Polygon(f) for f in vertices_2D[np.column_stack((faces, faces[:, :1]))]]
838-
)
839-
.buffer(eps)
840-
.buffer(-eps)
841-
)
832+
# Cleanup vertices
833+
vertices_2D_rounded = np.round(vertices_2D, int(np.abs(np.round(np.log10(precise_eps))) + 1))
834+
triangles = vertices_2D_rounded[np.column_stack((faces, faces[:, :1]))]
835+
valid = ~(triangles[:,[0,0,2]] == triangles[:,[1,2,1]]).all(axis=2).any(axis=1)
836+
triangles = triangles[valid]
837+
# Union all the polygons
838+
return ops.unary_union([Polygon(f) for f in triangles]).buffer(precise_eps).buffer(-precise_eps)
842839

843840
# a sequence of face indexes that are connected
844841
face_groups = graph.connected_components(adjacency, nodes=np.nonzero(side)[0])

0 commit comments

Comments
 (0)