Skip to content

Release: Docstrings + IMC #2417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 11, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added vector_angle_with sign
  • Loading branch information
yajushikhurana committed Jun 10, 2025
commit 248b5e8bc10a428436e49dc84f427f82820d6b7e
52 changes: 52 additions & 0 deletions trimesh/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,58 @@ def vector_angle(pairs):

return angles

def vector_angle_with_sign(mesh):
"""
Find the signed angles between between adjacent face normals in a mesh.

Parameters
----------
mesh : Trimesh
Source geometry to alter in-place.

Returns
----------
angles : (n,) float
Angles with sign between vectors in radians
"""
# Get adjacent face indices and shared edges
face_adjacency = mesh.face_adjacency

# Normals of adjacent triangles
normals = mesh.face_normals
pairs = normals[face_adjacency]
n1 = normals[face_adjacency[:, 0]]
n2 = normals[face_adjacency[:, 1]]

# COMs (centroids) of adjacent triangles
centers = mesh.triangles_center
c1 = centers[face_adjacency[:, 0]]
c2 = centers[face_adjacency[:, 1]]

pairs = np.asanyarray(pairs, dtype=np.float64)
if len(pairs) == 0:
return np.array([])
elif util.is_shape(pairs, (2, 3)):
pairs = pairs.reshape((-1, 2, 3))
elif not util.is_shape(pairs, (-1, 2, (2, 3))):
raise ValueError("pairs must be (n,2,(2|3))!")

# do the dot product between vectors
dots = util.diagonal_dot(pairs[:, 0], pairs[:, 1])
# clip for floating point error
dots = np.clip(dots, -1.0, 1.0)
# do cos and remove arbitrary sign
angles = np.abs(np.arccos(dots))

# Use geometric heuristic to determine sign
convex_vec = (c1 + n1) - (c2 + n2)
concave_vec = (c1 - n1) - (c2 - n2)

is_concave = np.linalg.norm(convex_vec, axis=1) < np.linalg.norm(concave_vec, axis=1)
angles[is_concave] *= -1 # Make angle negative for concave cases

return angles


def triangulate_quads(quads, dtype=np.int64) -> NDArray:
"""
Expand Down