3d-base 0.3.2__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: 3d-base
3
- Version: 0.3.2
3
+ Version: 0.4.0
4
4
  Summary: Headless pictures of 3D meshes with correct occlusion and smooth shading, and a health check for exported STL files
5
5
  Author: Konstantinos Georgiou
6
6
  License-Expression: MIT
@@ -15,6 +15,8 @@ Requires-Dist: numpy>=1.22
15
15
  Requires-Dist: trimesh>=3.20
16
16
  Requires-Dist: scipy>=1.8
17
17
  Requires-Dist: rtree>=1.0
18
+ Requires-Dist: fast-simplification>=0.1
19
+ Requires-Dist: scikit-image>=0.21
18
20
  Provides-Extra: captions
19
21
  Requires-Dist: pillow>=10.1; extra == "captions"
20
22
  Provides-Extra: sections
@@ -4,13 +4,13 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "3d-base"
7
- version = "0.3.2"
7
+ version = "0.4.0"
8
8
  description = "Headless pictures of 3D meshes with correct occlusion and smooth shading, and a health check for exported STL files"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
11
11
  license = "MIT"
12
12
  authors = [{ name = "Konstantinos Georgiou" }]
13
- dependencies = ["numpy>=1.22", "trimesh>=3.20", "scipy>=1.8", "rtree>=1.0"]
13
+ dependencies = ["numpy>=1.22", "trimesh>=3.20", "scipy>=1.8", "rtree>=1.0", "fast-simplification>=0.1", "scikit-image>=0.21"]
14
14
  keywords = ["3d", "mesh", "stl", "render", "headless", "trimesh", "3d-printing", "cad"]
15
15
  classifiers = ["Programming Language :: Python :: 3", "Topic :: Multimedia :: Graphics :: 3D Rendering"]
16
16
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: 3d-base
3
- Version: 0.3.2
3
+ Version: 0.4.0
4
4
  Summary: Headless pictures of 3D meshes with correct occlusion and smooth shading, and a health check for exported STL files
5
5
  Author: Konstantinos Georgiou
6
6
  License-Expression: MIT
@@ -15,6 +15,8 @@ Requires-Dist: numpy>=1.22
15
15
  Requires-Dist: trimesh>=3.20
16
16
  Requires-Dist: scipy>=1.8
17
17
  Requires-Dist: rtree>=1.0
18
+ Requires-Dist: fast-simplification>=0.1
19
+ Requires-Dist: scikit-image>=0.21
18
20
  Provides-Extra: captions
19
21
  Requires-Dist: pillow>=10.1; extra == "captions"
20
22
  Provides-Extra: sections
@@ -2,6 +2,8 @@ numpy>=1.22
2
2
  trimesh>=3.20
3
3
  scipy>=1.8
4
4
  rtree>=1.0
5
+ fast-simplification>=0.1
6
+ scikit-image>=0.21
5
7
 
6
8
  [captions]
7
9
  pillow>=10.1
@@ -188,7 +188,7 @@ def as_written(mesh: trimesh.Trimesh, file_type: str = "stl", loose_mm3: float =
188
188
  "touches": touches, "loose_mm3": float(loose)}
189
189
 
190
190
 
191
- def solid_from_skin(mesh: trimesh.Trimesh, centre, lean: float = 0.3, keep=None) -> trimesh.Trimesh:
191
+ def solid_from_skin(mesh: trimesh.Trimesh, centre, lean: float = 0.3, keep=None, skin: float = 0.5) -> trimesh.Trimesh:
192
192
  """A hollow part made solid from its outer skin, by removing its inner surface.
193
193
 
194
194
  The inner surface is the largest connected region, by area, of faces that point toward `centre`
@@ -200,6 +200,11 @@ def solid_from_skin(mesh: trimesh.Trimesh, centre, lean: float = 0.3, keep=None)
200
200
  the same outside and no inside, so a cavity cut into it afterwards (a bore for a part it sits on)
201
201
  is the only inside it has.
202
202
 
203
+ A face is hidden only by a face more than `skin` nearer the centre along its ray. Nearer than
204
+ that is a fold or a second copy of the surface, the kind a boolean or a vertex displacement
205
+ leaves (a cap's seams, pressed in by half a millimetre, folded its inner skin over itself at
206
+ the front and hid a patch of it), not a wall.
207
+
203
208
  `keep` names faces that stay whatever they face, as a boolean mask over the faces: a part the
204
209
  centre sees through the opening, such as the underside of a cap's bill, which faces the centre
205
210
  from outside the shell and would otherwise be taken for inner surface and have the air under it
@@ -223,7 +228,7 @@ def solid_from_skin(mesh: trimesh.Trimesh, centre, lean: float = 0.3, keep=None)
223
228
  if not cand.any():
224
229
  raise ValueError("no face points toward the centre")
225
230
  which = np.flatnonzero(cand)
226
- cand[which[~_seen_from(mesh, centre, which)]] = False
231
+ cand[which[~_seen_from(mesh, centre, which, skin=skin)]] = False
227
232
  if not cand.any():
228
233
  raise ValueError("no face that points toward the centre is seen from it")
229
234
  adj = np.asarray(mesh.face_adjacency)
@@ -265,8 +270,50 @@ def solid_from_skin(mesh: trimesh.Trimesh, centre, lean: float = 0.3, keep=None)
265
270
  return solid
266
271
 
267
272
 
268
- def _seen_from(mesh: trimesh.Trimesh, centre, which, cells: int = 64, batch: int = 4_000_000):
269
- """Which of the faces `which` a ray from `centre` to their middles meets first.
273
+ def remanifold(mesh: trimesh.Trimesh, pitch: float = 0.3, close: int = 1, smooth: int = 25,
274
+ decimate_to: int = 25000) -> trimesh.Trimesh:
275
+ """A mesh rebuilt as one simple solid on a voxel grid.
276
+
277
+ A boolean operation can leave a mesh that is watertight but not a simple solid: a tunnel through a
278
+ seam where two parts join (a handle, so the Euler number is not 2), or a near-coincident sheet
279
+ where two surfaces meet at a grazing angle, which a thickness measure reads as a wall thinner than
280
+ any line and a slicer cannot print. This turns the mesh into filled voxels of side `pitch`, which
281
+ cannot carry a tunnel or a gap thinner than `pitch`; `close` passes of a morphological closing
282
+ fill the sub-pitch tunnels and the thin grooves a surface would otherwise read as handles; the
283
+ surface is taken from the voxels and, because it comes out stepped, smoothed with `smooth` Taubin
284
+ iterations, which keep the volume and the topology. The mesh is decimated to about `decimate_to`
285
+ faces first, because voxelising a high-resolution mesh is the memory cost and the grid resamples
286
+ the surface anyway. The result is one watertight simple solid; a crisp cut such as a bore or a
287
+ slot is re-cut by the caller afterwards, since the grid fills a gap thinner than its pitch.
288
+
289
+ A hole wider than `pitch` is kept, so a part that is meant to have a hole stays as it is; only the
290
+ tunnels and gaps narrower than the grid go. Needs `scipy`, `scikit-image` and `fast-simplification`.
291
+ """
292
+ from trimesh.voxel import ops as _vops
293
+ from scipy import ndimage as _nd
294
+ if decimate_to and len(mesh.faces) > decimate_to:
295
+ try:
296
+ mesh = mesh.simplify_quadric_decimation(percent=float(decimate_to) / len(mesh.faces))
297
+ except Exception:
298
+ pass
299
+ vg = mesh.voxelized(float(pitch)).fill()
300
+ mat = np.asarray(vg.matrix)
301
+ if close:
302
+ pad = int(close) + 1
303
+ mat = _nd.binary_closing(np.pad(mat, pad), iterations=int(close))[pad:-pad, pad:-pad, pad:-pad]
304
+ out = _vops.matrix_to_marching_cubes(mat)
305
+ out.apply_transform(vg.transform)
306
+ out.merge_vertices()
307
+ out = max(out.split(only_watertight=False), key=lambda b: abs(b.volume))
308
+ out.fix_normals()
309
+ if smooth:
310
+ trimesh.smoothing.filter_taubin(out, iterations=int(smooth))
311
+ return out
312
+
313
+
314
+ def _seen_from(mesh: trimesh.Trimesh, centre, which, cells: int = 64, batch: int = 4_000_000, skin: float = 0.0):
315
+ """Which of the faces `which` a ray from `centre` to their middles meets first, or within `skin`
316
+ of first: a face less than that nearer the centre is a fold or a second copy of the surface.
270
317
 
271
318
  Every ray starts at the one point, so the triangles are binned by their direction from it on a
272
319
  cube map (six faces of `cells` by `cells`), and each ray is tested only against the triangles in
@@ -313,7 +360,7 @@ def _seen_from(mesh: trimesh.Trimesh, centre, which, cells: int = 64, batch: int
313
360
  for i in range(0, len(pair_ray), batch):
314
361
  r, t_idx = pair_ray[i:i + batch], pair_tri[i:i + batch]
315
362
  t = _ray_t(mid[r] / dist[r][:, None], tri[t_idx])
316
- before = (t < dist[r] - 1e-7) & ~own[i:i + batch]
363
+ before = (t < dist[r] - max(float(skin), 1e-7)) & ~own[i:i + batch]
317
364
  hidden[np.unique(r[before])] = True
318
365
  return ~hidden
319
366
 
@@ -343,6 +343,9 @@ fixed each.
343
343
 
344
344
  ## trimesh calls that cost a build each to learn
345
345
 
346
+ - `mesh.remanifold(mesh)` rebuilds a mesh as one simple solid on a voxel grid, to shed the seam
347
+ tunnels and near-coincident-sheet slivers a boolean leaves (a handle, or a wall thinner than any
348
+ line where two surfaces graze). A hole wider than the pitch is kept; re-cut a bore or a slot after.
346
349
  - A cut half is `trimesh.intersections.slice_mesh_plane(mesh, plane_normal, plane_origin, cap=True)`
347
350
  (there is no `slice_plane`), and capping needs shapely and mapbox-earcut. A cut outline is
348
351
  `mesh.section(plane_origin=, plane_normal=)`, then `.to_planar()` for 2D polygons.
@@ -259,3 +259,55 @@ def test_a_bill_seen_through_the_opening_is_kept_when_named():
259
259
  assert told.is_watertight and told.is_winding_consistent and told.euler_number == 2
260
260
  assert not told.contains(under)[0]
261
261
  assert abs(told.bounds[1][0] - 20.0) < 1e-6
262
+
263
+
264
+ def test_a_sliver_just_inside_the_inner_surface_neither_hides_it_nor_stays():
265
+ from base3d.mesh import solid_from_skin
266
+ # a dome shell with a closed sliver, 0.2 mm thick, lying 0.05 mm under the top of its inner
267
+ # surface: the kind of second copy a boolean or a vertex displacement leaves. Its far side faces
268
+ # outward and stands in front of the dome's top as seen from the centre
269
+ t = np.linspace(0, np.pi / 2, 24)
270
+ outer = np.c_[10 * np.sin(t), 10 * np.cos(t)]
271
+ inner = np.c_[9 * np.sin(t), 9 * np.cos(t)][::-1]
272
+ dome = _revolved(np.vstack([outer, inner, outer[:1]]), sections=64)
273
+ sliver = trimesh.creation.box(extents=(2.0, 2.0, 0.2))
274
+ sliver.apply_translation([0, 0, 9.0 - 0.05 - 0.1])
275
+ both = trimesh.util.concatenate([dome, sliver])
276
+ # the dome's faces keep their indices in the joined mesh; the top of its inner surface lies in
277
+ # the sliver's shadow, hidden by 0.05 mm with no skin and seen through it with the default
278
+ from base3d.mesh import _seen_from
279
+ c = dome.triangles_center
280
+ top = np.flatnonzero((c[:, 2] > 8.5) & (c[:, 2] < 9.2) & (np.linalg.norm(c[:, :2], axis=1) < 0.6))
281
+ assert len(top) > 0
282
+ assert not _seen_from(both, (0, 0, 0), top, skin=0.0).any()
283
+ assert _seen_from(both, (0, 0, 0), top, skin=0.5).all()
284
+ solid = solid_from_skin(both, centre=(0, 0, 0))
285
+ assert solid.is_watertight and solid.is_winding_consistent and solid.euler_number == 2
286
+ assert solid.contains([[0.0, 0.0, 8.9], [0.0, 0.0, 5.0]]).all() # the top is solid through
287
+ assert abs(solid.volume - 2.0 / 3.0 * np.pi * 1000) < 0.03 * 2094
288
+
289
+
290
+ def _block_with_hole(side, hole, height=4.0):
291
+ # a square block with a square hole through it, built from a polygon with a hole (no boolean
292
+ # backend needed), so it is a genus-1 solid: watertight with a handle
293
+ from shapely.geometry import Polygon
294
+ h, s = hole / 2.0, side / 2.0
295
+ shell = [(-s, -s), (s, -s), (s, s), (-s, s)]
296
+ inner = [(-h, -h), (h, -h), (h, h), (-h, h)]
297
+ return trimesh.creation.extrude_polygon(Polygon(shell, [inner]), height=height)
298
+
299
+
300
+ def test_remanifold_closes_a_sub_pitch_tunnel_to_a_simple_solid():
301
+ from base3d.mesh import remanifold
302
+ holed = _block_with_hole(10.0, 0.3) # a 0.3 mm tunnel through a 10 mm block
303
+ assert holed.euler_number == 0 # genus 1: a handle, not a simple solid
304
+ fixed = remanifold(holed, pitch=0.6, close=1, smooth=0)
305
+ assert fixed.is_watertight and fixed.euler_number == 2 # the sub-pitch tunnel is gone
306
+ assert 400.0 < fixed.volume < 800.0 # voxelising inflates the surface; it never shrinks the solid
307
+
308
+
309
+ def test_remanifold_keeps_a_hole_wider_than_the_pitch():
310
+ from base3d.mesh import remanifold
311
+ frame = _block_with_hole(12.0, 3.0) # a 3 mm hole, far wider than the pitch
312
+ kept = remanifold(frame, pitch=0.6, close=1, smooth=0)
313
+ assert kept.is_watertight and kept.euler_number == 0 # the real hole is still a hole
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes