Skip to content

Commit

Permalink
kind of working #2299 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Oct 15, 2024
1 parent e072537 commit 120dda9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
20 changes: 14 additions & 6 deletions trimesh/exchange/threemf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@ def _read_mesh(mesh):
# this is very sensitive as it is large, i.e. it is
# much faster with the full list comprehension before
# the `.join` as the giant string can be fully allocated
vs = ' '.join([f'{i.attrib["x"]} {i.attrib["y"]} {i.attrib["z"]}'
for i in vertices.iter("{*}vertex")])
vs = " ".join(
[
f'{i.attrib["x"]} {i.attrib["y"]} {i.attrib["z"]}'
for i in vertices.iter("{*}vertex")
]
)
# convert every value to floating point in one-shot rather than in a loop
v_array = np.fromstring(vs, dtype=np.float64, sep=' ').reshape((-1, 3))
v_array = np.fromstring(vs, dtype=np.float64, sep=" ").reshape((-1, 3))

# do the same behavior for faces but as an integer
fs = ' '.join([f'{i.attrib["v1"]} {i.attrib["v2"]} {i.attrib["v3"]}'
for i in faces.iter("{*}triangle")])
f_array = np.fromstring(fs, dtype=np.int64, sep=' ').reshape((-1, 3))
fs = " ".join(
[
f'{i.attrib["v1"]} {i.attrib["v2"]} {i.attrib["v3"]}'
for i in faces.iter("{*}triangle")
]
)
f_array = np.fromstring(fs, dtype=np.int64, sep=" ").reshape((-1, 3))

return v_array, f_array

Expand Down
45 changes: 38 additions & 7 deletions trimesh/intersections.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,11 @@ def slice_faces_plane(
faces = faces[face_index]

if cached_dots is not None:
dots = cached_dots
else:
# dot product of each vertex with the plane normal indexed by face
# so for each face the dot product of each vertex is a row
# shape is the same as faces (n,3)
dots = np.dot(vertices - plane_origin, plane_normal)
log.debug("`cached_dots` is deprecated!")
# dot product of each vertex with the plane normal indexed by face
# so for each face the dot product of each vertex is a row
# shape is the same as faces (n,3)
dots = np.dot(vertices - plane_origin, plane_normal)

# Find vertex orientations w.r.t. faces for all triangles:
# -1 -> vertex "inside" plane (positive normal direction)
Expand Down Expand Up @@ -748,6 +747,7 @@ def slice_mesh_plane(
plane_origin=origin,
face_index=face_index,
)

# check if cap arg specified
if cap:
if face_index:
Expand All @@ -773,6 +773,7 @@ def slice_mesh_plane(
on_plane = np.abs(vertices_2D[:, 2]) < 1e-8
edges = edges[on_plane[edges].all(axis=1)]
edges = edges[edges[:, 0] != edges[:, 1]]
edges.sort(axis=1)

unique_edge = grouping.group_rows(edges, require_count=1)
if len(unique) < 3:
Expand All @@ -781,14 +782,44 @@ def slice_mesh_plane(
tree = cKDTree(vertices)
# collect new faces
faces = [f]
for p in polygons.edges_to_polygons(edges[unique_edge], vertices_2D[:, :2]):

ee = edges[unique_edge]

broken = np.bincount(ee.ravel()) == 1
if broken.any():
bid = np.nonzero(broken)[0]
v2 = vertices_2D[:, :2][bid]
t2d = cKDTree(v2)

_, pairs = t2d.query(v2, k=2)

pairs.sort(axis=1)

"""
pc = np.vstack((pairs[:,[0,1]],
pairs[:,[0,2]],
pairs[:,[0,3]]))
pc.sort(axis=1)
import trimesh
infill = trimesh.grouping.boolean_rows(edges, pc)
"""

pairs = pairs[grouping.unique_rows(pairs)[0]]

infill = bid[pairs]

if len(infill) > 0:
ee = np.vstack((ee, infill))

for p in polygons.edges_to_polygons(ee, vertices_2D[:, :2]):
# triangulate cap and raise an error if any new vertices were inserted
vn, fn = triangulate_polygon(p, engine=engine, force_vertices=True)
# collect the original index for the new vertices
vn3 = tf.transform_points(util.stack_3D(vn), to_3D)
distance, vid = tree.query(vn3)
if distance.max() > 1e-8:
util.log.debug("triangulate may have inserted vertex!")

# triangulation should not have inserted vertices
nf = vid[fn]
# hmm but it may have returned faces that are now degenerate
Expand Down

0 comments on commit 120dda9

Please sign in to comment.