fabriks 3.0.0__tar.gz → 3.0.2__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.
- {fabriks-3.0.0 → fabriks-3.0.2}/PKG-INFO +7 -8
- {fabriks-3.0.0 → fabriks-3.0.2}/README.md +6 -7
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/build.py +100 -31
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/codecs/blobs.py +20 -6
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/codecs/meshopt.py +34 -8
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/codecs/protocol.py +10 -2
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/codecs/raw.py +17 -4
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/contrib/__init__.py +8 -1
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/contrib/catalog.py +10 -4
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/contrib/fit.py +15 -5
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/contrib/gltf.py +4 -1
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/contrib/meshes.py +21 -6
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/contrib/scene.py +36 -12
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/frames.py +99 -51
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/geometry.py +46 -13
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/manifest.py +57 -18
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/octree.py +8 -2
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/planner.py +22 -6
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/reader.py +96 -32
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/simplifiers/measure.py +20 -6
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/simplifiers/meshopt.py +6 -3
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/simplifiers/quadric.py +3 -1
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/simplifiers/registry.py +4 -1
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/sizing.py +47 -17
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/sources.py +9 -3
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/stores/access.py +9 -3
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/stores/file.py +9 -2
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/stores/memory.py +5 -1
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/stores/protocol.py +9 -3
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/verify.py +135 -37
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/writer.py +39 -12
- {fabriks-3.0.0 → fabriks-3.0.2}/pyproject.toml +11 -66
- {fabriks-3.0.0 → fabriks-3.0.2}/.gitignore +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/__init__.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/codecs/__init__.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/codecs/compression.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/contrib/obj.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/errors.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/py.typed +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/simplifiers/__init__.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/simplifiers/greedy.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/simplifiers/protocol.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/stores/__init__.py +0 -0
- {fabriks-3.0.0 → fabriks-3.0.2}/fabriks/stores/directory.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: fabriks
|
|
3
|
-
Version: 3.0.
|
|
3
|
+
Version: 3.0.2
|
|
4
4
|
Summary: a level-of-detail mesh wire format: octree-partitioned surfaces written to any object store
|
|
5
5
|
Author-email: jhnnsrs <jhnnsrs@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -89,7 +89,6 @@ manifest = fabriks.write_meshes(
|
|
|
89
89
|
axes=("x", "y", "z"),
|
|
90
90
|
**grid_plan.as_kwargs(),
|
|
91
91
|
)
|
|
92
|
-
|
|
93
92
|
```
|
|
94
93
|
|
|
95
94
|
---
|
|
@@ -113,7 +112,6 @@ for cell in collection.read_cells([(e.level, e.cell) for e in plan]):
|
|
|
113
112
|
# Reassemble single object across cells
|
|
114
113
|
mesh = collection.object_mesh(7)
|
|
115
114
|
collection.release()
|
|
116
|
-
|
|
117
115
|
```
|
|
118
116
|
|
|
119
117
|
**Async Reading**
|
|
@@ -122,7 +120,6 @@ collection.release()
|
|
|
122
120
|
collection = await fabriks.aopen_collection(S3Store(...), "my-collection")
|
|
123
121
|
plan = collection.plan(camera=camera)
|
|
124
122
|
cells = await collection.aread_cells([(e.level, e.cell) for e in plan], concurrency=16)
|
|
125
|
-
|
|
126
123
|
```
|
|
127
124
|
|
|
128
125
|
---
|
|
@@ -138,8 +135,9 @@ import fabriks
|
|
|
138
135
|
imported = read_glb("bunny.glb", store, "bunny", levels=4)
|
|
139
136
|
|
|
140
137
|
collection = fabriks.open_collection(store, "bunny")
|
|
141
|
-
blob = write_obj(
|
|
142
|
-
|
|
138
|
+
blob = write_obj(
|
|
139
|
+
collection, level=2
|
|
140
|
+
) # or write to a path: write_obj(collection, "out.obj")
|
|
143
141
|
```
|
|
144
142
|
|
|
145
143
|
| Format | Identity | Notes |
|
|
@@ -176,10 +174,11 @@ blob = write_obj(collection, level=2) # or write to a path: write_obj(c
|
|
|
176
174
|
**Verification**
|
|
177
175
|
|
|
178
176
|
```python
|
|
179
|
-
report = fabriks.verify(
|
|
177
|
+
report = fabriks.verify(
|
|
178
|
+
collection, tier="geometry"
|
|
179
|
+
) # "structure" | "blobs" | "geometry"
|
|
180
180
|
if not report:
|
|
181
181
|
print(report)
|
|
182
|
-
|
|
183
182
|
```
|
|
184
183
|
|
|
185
184
|
---
|
|
@@ -67,7 +67,6 @@ manifest = fabriks.write_meshes(
|
|
|
67
67
|
axes=("x", "y", "z"),
|
|
68
68
|
**grid_plan.as_kwargs(),
|
|
69
69
|
)
|
|
70
|
-
|
|
71
70
|
```
|
|
72
71
|
|
|
73
72
|
---
|
|
@@ -91,7 +90,6 @@ for cell in collection.read_cells([(e.level, e.cell) for e in plan]):
|
|
|
91
90
|
# Reassemble single object across cells
|
|
92
91
|
mesh = collection.object_mesh(7)
|
|
93
92
|
collection.release()
|
|
94
|
-
|
|
95
93
|
```
|
|
96
94
|
|
|
97
95
|
**Async Reading**
|
|
@@ -100,7 +98,6 @@ collection.release()
|
|
|
100
98
|
collection = await fabriks.aopen_collection(S3Store(...), "my-collection")
|
|
101
99
|
plan = collection.plan(camera=camera)
|
|
102
100
|
cells = await collection.aread_cells([(e.level, e.cell) for e in plan], concurrency=16)
|
|
103
|
-
|
|
104
101
|
```
|
|
105
102
|
|
|
106
103
|
---
|
|
@@ -116,8 +113,9 @@ import fabriks
|
|
|
116
113
|
imported = read_glb("bunny.glb", store, "bunny", levels=4)
|
|
117
114
|
|
|
118
115
|
collection = fabriks.open_collection(store, "bunny")
|
|
119
|
-
blob = write_obj(
|
|
120
|
-
|
|
116
|
+
blob = write_obj(
|
|
117
|
+
collection, level=2
|
|
118
|
+
) # or write to a path: write_obj(collection, "out.obj")
|
|
121
119
|
```
|
|
122
120
|
|
|
123
121
|
| Format | Identity | Notes |
|
|
@@ -154,10 +152,11 @@ blob = write_obj(collection, level=2) # or write to a path: write_obj(c
|
|
|
154
152
|
**Verification**
|
|
155
153
|
|
|
156
154
|
```python
|
|
157
|
-
report = fabriks.verify(
|
|
155
|
+
report = fabriks.verify(
|
|
156
|
+
collection, tier="geometry"
|
|
157
|
+
) # "structure" | "blobs" | "geometry"
|
|
158
158
|
if not report:
|
|
159
159
|
print(report)
|
|
160
|
-
|
|
161
160
|
```
|
|
162
161
|
|
|
163
162
|
---
|
|
@@ -53,8 +53,8 @@ from fabriks.manifest import (
|
|
|
53
53
|
Encoding,
|
|
54
54
|
Grid,
|
|
55
55
|
Manifest,
|
|
56
|
-
_read_axes,
|
|
57
56
|
level_part_path,
|
|
57
|
+
read_axes,
|
|
58
58
|
)
|
|
59
59
|
from fabriks.octree import cell_box, morton_decode, morton_encode_one
|
|
60
60
|
from fabriks.simplifiers import Simplifier, resolve_simplifier, simplify_to_target
|
|
@@ -98,7 +98,9 @@ class MeshCollection:
|
|
|
98
98
|
return write_collection(self, store, prefix)
|
|
99
99
|
|
|
100
100
|
|
|
101
|
-
def choose_cell_size(
|
|
101
|
+
def choose_cell_size(
|
|
102
|
+
objects: Mapping[int, Any], *, levels: int = 3
|
|
103
|
+
) -> tuple[int, int, int]:
|
|
102
104
|
"""Pick a level-0 cell, in voxels, that most objects fit inside whole.
|
|
103
105
|
|
|
104
106
|
**Prefer passing the source array's chunk shape when you know it.** ``cell_size`` is in
|
|
@@ -120,7 +122,9 @@ def choose_cell_size(objects: Mapping[int, Any], *, levels: int = 3) -> tuple[in
|
|
|
120
122
|
Powers of two because a coarser cell is exactly two finer ones, so the level-0 planes stay a
|
|
121
123
|
superset of every coarser level's -- which is what the boundary argument rests on.
|
|
122
124
|
"""
|
|
123
|
-
del
|
|
125
|
+
del (
|
|
126
|
+
levels
|
|
127
|
+
) # part of the signature so a caller can pass it; the heuristic is level-free
|
|
124
128
|
if not objects:
|
|
125
129
|
raise FormatError(
|
|
126
130
|
"Cannot choose a cell size for an empty collection; pass `cell_size` explicitly."
|
|
@@ -142,7 +146,9 @@ def choose_cell_size(objects: Mapping[int, Any], *, levels: int = 3) -> tuple[in
|
|
|
142
146
|
chosen = np.minimum(chosen, np.exp2(np.ceil(np.log2(np.maximum(extent, 1.0)))))
|
|
143
147
|
|
|
144
148
|
def level0_cells(size: npt.NDArray[np.float64]) -> float:
|
|
145
|
-
return float(
|
|
149
|
+
return float(
|
|
150
|
+
np.prod(np.maximum(np.ceil((upper - np.minimum(lower, 0.0)) / size), 1.0))
|
|
151
|
+
)
|
|
146
152
|
|
|
147
153
|
# Halve while the octree is doing too little work, but never below the object-fit size --
|
|
148
154
|
# that is the constraint whose violation is silent.
|
|
@@ -215,8 +221,10 @@ def build_collection(
|
|
|
215
221
|
# `axes`, which the manifest would otherwise not check until every mesh had been cut.
|
|
216
222
|
Encoding(codec=codec, compression=compression)
|
|
217
223
|
if axes is None: # pyright: ignore[reportUnnecessaryComparison] -- untyped callers still pass it
|
|
218
|
-
raise TypeError(
|
|
219
|
-
|
|
224
|
+
raise TypeError(
|
|
225
|
+
"build_collection needs `axes=`: the names of the three vertex slots, in slot order. Without them the manifest could only be read by guessing which slot is which."
|
|
226
|
+
)
|
|
227
|
+
read_axes(axes) # unlaundered: `list("xyz")` would pass a string off as three names
|
|
220
228
|
if codec == CODEC_MESHOPT:
|
|
221
229
|
from fabriks.codecs import require_meshoptimizer
|
|
222
230
|
|
|
@@ -251,15 +259,32 @@ def build_collection(
|
|
|
251
259
|
# for why an odd 65535 makes that a separate step.
|
|
252
260
|
coarse_extent = cell_size_array.astype(np.float64) * (2 ** (levels - 1))
|
|
253
261
|
fragments_by_object: dict[
|
|
254
|
-
int,
|
|
262
|
+
int,
|
|
263
|
+
dict[
|
|
264
|
+
tuple[int, int, int],
|
|
265
|
+
tuple[
|
|
266
|
+
npt.NDArray[np.float64], npt.NDArray[np.int64], npt.NDArray[np.bool_]
|
|
267
|
+
],
|
|
268
|
+
],
|
|
255
269
|
] = {}
|
|
256
270
|
for object_id in object_ids:
|
|
257
|
-
fragments: dict[
|
|
258
|
-
|
|
271
|
+
fragments: dict[
|
|
272
|
+
tuple[int, int, int],
|
|
273
|
+
tuple[
|
|
274
|
+
npt.NDArray[np.float64], npt.NDArray[np.int64], npt.NDArray[np.bool_]
|
|
275
|
+
],
|
|
276
|
+
] = {}
|
|
277
|
+
for triple, fragment in clip_to_cells(
|
|
278
|
+
meshes[object_id], cell_size_array
|
|
279
|
+
).items():
|
|
259
280
|
vertices, boundary = snap_boundary(
|
|
260
|
-
np.asarray(fragment.vertices, dtype=np.float64),
|
|
281
|
+
np.asarray(fragment.vertices, dtype=np.float64),
|
|
282
|
+
cell_size_array,
|
|
283
|
+
coarse_extent,
|
|
284
|
+
)
|
|
285
|
+
faces = drop_degenerate(
|
|
286
|
+
vertices, np.asarray(fragment.faces, dtype=np.int64)
|
|
261
287
|
)
|
|
262
|
-
faces = drop_degenerate(vertices, np.asarray(fragment.faces, dtype=np.int64))
|
|
263
288
|
if len(faces) == 0:
|
|
264
289
|
continue
|
|
265
290
|
fragments[triple] = (vertices, faces, boundary)
|
|
@@ -284,7 +309,9 @@ def build_collection(
|
|
|
284
309
|
#
|
|
285
310
|
# A level's representation is standalone: the whole object is decimated to that level's
|
|
286
311
|
# budget and then split by the fragments' cells, never expressed as a delta on a finer one.
|
|
287
|
-
per_level: dict[
|
|
312
|
+
per_level: dict[
|
|
313
|
+
int, dict[int, dict[int, tuple[npt.NDArray[np.float64], npt.NDArray[np.int64]]]]
|
|
314
|
+
] = {}
|
|
288
315
|
#: The simplifier's own estimate of how far it strayed, per ``(level, cell)`` -- the
|
|
289
316
|
#: decimation half of that cell's ``lod_error``.
|
|
290
317
|
displacement: dict[tuple[int, int], float] = {}
|
|
@@ -294,7 +321,9 @@ def build_collection(
|
|
|
294
321
|
at_the_floor: dict[int, int] = {level: 0 for level in range(levels)}
|
|
295
322
|
|
|
296
323
|
for level in range(levels):
|
|
297
|
-
level_cells: dict[
|
|
324
|
+
level_cells: dict[
|
|
325
|
+
int, dict[int, tuple[npt.NDArray[np.float64], npt.NDArray[np.int64]]]
|
|
326
|
+
] = {}
|
|
298
327
|
level_extent = cell_size_array * (2**level)
|
|
299
328
|
for object_id in object_ids:
|
|
300
329
|
fragments = fragments_by_object[object_id]
|
|
@@ -305,7 +334,10 @@ def build_collection(
|
|
|
305
334
|
# level. The eight children tile their parent exactly, so a coarse cell is
|
|
306
335
|
# assembled by merging -- never by cutting again, which is what would move a
|
|
307
336
|
# boundary and break LOCKED.
|
|
308
|
-
groups: dict[
|
|
337
|
+
groups: dict[
|
|
338
|
+
tuple[int, int, int],
|
|
339
|
+
list[tuple[npt.NDArray[np.float64], npt.NDArray[np.int64]]],
|
|
340
|
+
] = {}
|
|
309
341
|
for triple, (vertices, faces, _) in fragments.items():
|
|
310
342
|
coarse = tuple(int(component) // (2**level) for component in triple)
|
|
311
343
|
groups.setdefault(coarse, []).append((vertices, faces))
|
|
@@ -327,7 +359,9 @@ def build_collection(
|
|
|
327
359
|
# than the face floor, or when the piece is already under the target. Both
|
|
328
360
|
# are a different explanation for a missed budget than a boundary the
|
|
329
361
|
# simplifier may not spend, and the warning must not confuse them.
|
|
330
|
-
at_floor =
|
|
362
|
+
at_floor = (
|
|
363
|
+
wanted_faces <= schedule.floor_faces or len(faces) <= target
|
|
364
|
+
)
|
|
331
365
|
result, relaxed = simplify_to_target(
|
|
332
366
|
backend, vertices, faces, fixed=own_faces, target_faces=target
|
|
333
367
|
)
|
|
@@ -352,10 +386,16 @@ def build_collection(
|
|
|
352
386
|
#: Each cell's resolved `lod_error`, filled level by level so a parent can dominate its
|
|
353
387
|
#: children -- levels are assembled finest-first, so a child is always already in here.
|
|
354
388
|
cell_errors: dict[tuple[int, int], float] = {}
|
|
355
|
-
object_cells: dict[int, list[dict[str, int]]] = {
|
|
356
|
-
|
|
389
|
+
object_cells: dict[int, list[dict[str, int]]] = {
|
|
390
|
+
object_id: [] for object_id in object_ids
|
|
391
|
+
}
|
|
392
|
+
object_totals: dict[int, tuple[int, int]] = {
|
|
393
|
+
object_id: (0, 0) for object_id in object_ids
|
|
394
|
+
}
|
|
357
395
|
#: The (low, high) corner of each object's bounds, in voxels.
|
|
358
|
-
object_bounds: dict[
|
|
396
|
+
object_bounds: dict[
|
|
397
|
+
int, tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]
|
|
398
|
+
] = {}
|
|
359
399
|
#: The (low, high) corner of everything written, in voxels -- what `shape` is derived from.
|
|
360
400
|
collection_low: npt.NDArray[np.float64] | None = None
|
|
361
401
|
collection_high: npt.NDArray[np.float64] | None = None
|
|
@@ -389,7 +429,10 @@ def build_collection(
|
|
|
389
429
|
low, high = vertices.min(axis=0), vertices.max(axis=0)
|
|
390
430
|
if object_id in object_bounds:
|
|
391
431
|
prev_low, prev_high = object_bounds[object_id]
|
|
392
|
-
low, high =
|
|
432
|
+
low, high = (
|
|
433
|
+
np.minimum(low, prev_low),
|
|
434
|
+
np.maximum(high, prev_high),
|
|
435
|
+
)
|
|
393
436
|
object_bounds[object_id] = (low, high)
|
|
394
437
|
object_cells[object_id].append({"level": level, "cell": cell})
|
|
395
438
|
|
|
@@ -400,11 +443,17 @@ def build_collection(
|
|
|
400
443
|
# quadric-optimal position, which can sit slightly outside the hull of the vertices
|
|
401
444
|
# it replaced. Taking the union over what was actually written is the only bound
|
|
402
445
|
# that cannot be narrower than the geometry.
|
|
403
|
-
collection_low =
|
|
404
|
-
|
|
446
|
+
collection_low = (
|
|
447
|
+
low if collection_low is None else np.minimum(collection_low, low)
|
|
448
|
+
)
|
|
449
|
+
collection_high = (
|
|
450
|
+
high if collection_high is None else np.maximum(collection_high, high)
|
|
451
|
+
)
|
|
405
452
|
|
|
406
453
|
_, extent = cell_box(cell, level, grid.cell_size)
|
|
407
|
-
lod_error = float(
|
|
454
|
+
lod_error = float(
|
|
455
|
+
extent.max() / QUANT_MAX + displacement.get((level, cell), 0.0)
|
|
456
|
+
)
|
|
408
457
|
|
|
409
458
|
# A parent's error must dominate its children's, and this is what makes a descent
|
|
410
459
|
# well-founded: a planner keeps a cell when its error fits the budget and descends
|
|
@@ -429,7 +478,9 @@ def build_collection(
|
|
|
429
478
|
codec=codec,
|
|
430
479
|
compression=compression,
|
|
431
480
|
),
|
|
432
|
-
"indices": encode_indices(
|
|
481
|
+
"indices": encode_indices(
|
|
482
|
+
faces, codec=codec, compression=compression
|
|
483
|
+
),
|
|
433
484
|
"vertex_count": len(vertices),
|
|
434
485
|
"index_count": len(faces) * 3,
|
|
435
486
|
"object_ids": ids,
|
|
@@ -464,7 +515,14 @@ def build_collection(
|
|
|
464
515
|
shards.append((level, build_table(rows, schemas["geometry"])))
|
|
465
516
|
|
|
466
517
|
_warn_if_decimation_missed(
|
|
467
|
-
cell_rows,
|
|
518
|
+
cell_rows,
|
|
519
|
+
levels,
|
|
520
|
+
locked,
|
|
521
|
+
pinnable,
|
|
522
|
+
grid.cell_size,
|
|
523
|
+
at_the_floor,
|
|
524
|
+
schedule,
|
|
525
|
+
backend,
|
|
468
526
|
)
|
|
469
527
|
|
|
470
528
|
object_rows = []
|
|
@@ -489,7 +547,9 @@ def build_collection(
|
|
|
489
547
|
|
|
490
548
|
manifest = Manifest(
|
|
491
549
|
grid=grid,
|
|
492
|
-
encoding=Encoding(
|
|
550
|
+
encoding=Encoding(
|
|
551
|
+
codec=codec, compression=compression, decimation=schedule.declaration
|
|
552
|
+
),
|
|
493
553
|
shape=_shape_of(collection_low, collection_high),
|
|
494
554
|
axes=tuple(axes), # type: ignore[arg-type]
|
|
495
555
|
counts={
|
|
@@ -561,7 +621,8 @@ def _warn_if_decimation_missed(
|
|
|
561
621
|
is pinned by ``boundary: LOCKED`` and the decimator may not spend it.
|
|
562
622
|
"""
|
|
563
623
|
faces_at = {
|
|
564
|
-
level: sum(row["index_count"] for row in cell_rows if row["level"] == level)
|
|
624
|
+
level: sum(row["index_count"] for row in cell_rows if row["level"] == level)
|
|
625
|
+
// 3
|
|
565
626
|
for level in range(levels)
|
|
566
627
|
}
|
|
567
628
|
|
|
@@ -570,7 +631,9 @@ def _warn_if_decimation_missed(
|
|
|
570
631
|
# resort in `_decimate_to_the_tightest_target_that_survives`, where a surface this decimator
|
|
571
632
|
# cannot coarsen at any target is kept whole -- rare, and stated rather than hidden, because
|
|
572
633
|
# nothing downstream would report it.
|
|
573
|
-
inverted = [
|
|
634
|
+
inverted = [
|
|
635
|
+
level for level in range(1, levels) if faces_at[level] > faces_at[level - 1]
|
|
636
|
+
]
|
|
574
637
|
if inverted:
|
|
575
638
|
worst_inversion = ", ".join(
|
|
576
639
|
f"level {level} holds {faces_at[level]} faces against level {level - 1}'s {faces_at[level - 1]}"
|
|
@@ -587,13 +650,15 @@ def _warn_if_decimation_missed(
|
|
|
587
650
|
missed = [
|
|
588
651
|
(level, faces_at[level] / faces_at[level - 1])
|
|
589
652
|
for level in range(1, levels)
|
|
590
|
-
if faces_at[level - 1]
|
|
653
|
+
if faces_at[level - 1]
|
|
654
|
+
and faces_at[level] / faces_at[level - 1] > 2 * schedule.ratio
|
|
591
655
|
]
|
|
592
656
|
if not missed:
|
|
593
657
|
return
|
|
594
658
|
|
|
595
659
|
worst = ", ".join(
|
|
596
|
-
f"level {level} kept {ratio:.0%} of level {level - 1}"
|
|
660
|
+
f"level {level} kept {ratio:.0%} of level {level - 1}"
|
|
661
|
+
for level, ratio in missed
|
|
597
662
|
)
|
|
598
663
|
|
|
599
664
|
# Two quite different causes look identical in the ratio, so the message names whichever
|
|
@@ -635,7 +700,9 @@ def _warn_if_decimation_missed(
|
|
|
635
700
|
)
|
|
636
701
|
|
|
637
702
|
|
|
638
|
-
def _children_of(
|
|
703
|
+
def _children_of(
|
|
704
|
+
cell: int, level: int, per_level: Mapping[int, Mapping[int, Any]]
|
|
705
|
+
) -> list[int]:
|
|
639
706
|
"""The cells at level ``L-1`` that hold geometry inside this cell."""
|
|
640
707
|
if level == 0:
|
|
641
708
|
return []
|
|
@@ -650,7 +717,9 @@ def _children_of(cell: int, level: int, per_level: Mapping[int, Mapping[int, Any
|
|
|
650
717
|
return found
|
|
651
718
|
|
|
652
719
|
|
|
653
|
-
def _child_mask(
|
|
720
|
+
def _child_mask(
|
|
721
|
+
cell: int, level: int, per_level: Mapping[int, Mapping[int, Any]]
|
|
722
|
+
) -> int:
|
|
654
723
|
"""Which of the eight children at level ``L-1`` hold geometry. Zero at the finest level.
|
|
655
724
|
|
|
656
725
|
Bit ``dx | dy<<1 | dz<<2`` is set when the child at triple ``(2i+dx, 2j+dy, 2k+dz)`` holds
|
|
@@ -56,7 +56,9 @@ def codec_for(codec: str) -> BlobCodec:
|
|
|
56
56
|
try:
|
|
57
57
|
return _CODECS[codec]
|
|
58
58
|
except KeyError:
|
|
59
|
-
raise FormatError(
|
|
59
|
+
raise FormatError(
|
|
60
|
+
f"`codec` is {codec!r}; the format defines {CODEC_NONE} and {CODEC_MESHOPT}."
|
|
61
|
+
) from None
|
|
60
62
|
|
|
61
63
|
|
|
62
64
|
def encode_positions(
|
|
@@ -83,8 +85,14 @@ def encode_positions(
|
|
|
83
85
|
# A boundary vertex is pinned to exactly 0.0 or 1.0, so the slack only has to absorb
|
|
84
86
|
# floating-point noise -- a fraction of a quantum, not a quantum.
|
|
85
87
|
tolerance = 0.5 / QUANT_MAX
|
|
86
|
-
if normalized.size and (
|
|
87
|
-
|
|
88
|
+
if normalized.size and (
|
|
89
|
+
normalized.min() < -tolerance or normalized.max() > 1.0 + tolerance
|
|
90
|
+
):
|
|
91
|
+
stray = int(
|
|
92
|
+
((normalized < -tolerance) | (normalized > 1.0 + tolerance))
|
|
93
|
+
.any(axis=1)
|
|
94
|
+
.sum()
|
|
95
|
+
)
|
|
88
96
|
raise PartitioningError(
|
|
89
97
|
f"{stray} vertex/vertices fall outside cell {cell} at level {level}, whose box is "
|
|
90
98
|
f"{origin.tolist()} + {extent.tolist()} voxels (worst normalized coordinate "
|
|
@@ -122,7 +130,9 @@ def decode_positions(
|
|
|
122
130
|
"uncompressed length is known, since the format's ZSTD framing carries no size of its own."
|
|
123
131
|
)
|
|
124
132
|
origin, extent = cell_box(cell, level, cell_size)
|
|
125
|
-
quantized = implementation.decode_positions(
|
|
133
|
+
quantized = implementation.decode_positions(
|
|
134
|
+
blob, compression=compression, vertex_count=vertex_count
|
|
135
|
+
)
|
|
126
136
|
return origin + np.asarray(quantized, dtype=np.float64) / QUANT_MAX * extent
|
|
127
137
|
|
|
128
138
|
|
|
@@ -141,7 +151,9 @@ def encode_indices(
|
|
|
141
151
|
"""
|
|
142
152
|
implementation = codec_for(codec)
|
|
143
153
|
faces = np.asarray(faces, dtype="<u4").reshape(-1, 3)
|
|
144
|
-
return implementation.encode_indices(
|
|
154
|
+
return implementation.encode_indices(
|
|
155
|
+
faces, compression=compression, vertex_count=vertex_count
|
|
156
|
+
)
|
|
145
157
|
|
|
146
158
|
|
|
147
159
|
def decode_indices(
|
|
@@ -164,7 +176,9 @@ def decode_indices(
|
|
|
164
176
|
"`index_count` is required to decode a compressed indices blob: it is how the "
|
|
165
177
|
"uncompressed length is known, since the format's ZSTD framing carries no size of its own."
|
|
166
178
|
)
|
|
167
|
-
return implementation.decode_indices(
|
|
179
|
+
return implementation.decode_indices(
|
|
180
|
+
blob, compression=compression, index_count=index_count
|
|
181
|
+
)
|
|
168
182
|
|
|
169
183
|
|
|
170
184
|
__all__ = [
|
|
@@ -13,7 +13,7 @@ become required here rather than merely checked.
|
|
|
13
13
|
|
|
14
14
|
from __future__ import annotations
|
|
15
15
|
|
|
16
|
-
from
|
|
16
|
+
from types import ModuleType
|
|
17
17
|
|
|
18
18
|
import numpy as np
|
|
19
19
|
import numpy.typing as npt
|
|
@@ -26,7 +26,9 @@ from fabriks.manifest import CODEC_MESHOPT, CODEC_NONE
|
|
|
26
26
|
STRIDE = 8
|
|
27
27
|
|
|
28
28
|
|
|
29
|
-
def require_meshoptimizer(
|
|
29
|
+
def require_meshoptimizer(
|
|
30
|
+
what: str = f"`codec={CODEC_MESHOPT!r}`",
|
|
31
|
+
) -> ModuleType:
|
|
30
32
|
"""Import meshoptimizer, saying what asked for it when the install is missing it."""
|
|
31
33
|
try:
|
|
32
34
|
import meshoptimizer # type: ignore
|
|
@@ -39,7 +41,11 @@ def require_meshoptimizer(what: str = f"`codec={CODEC_MESHOPT!r}`") -> Any: # n
|
|
|
39
41
|
return meshoptimizer
|
|
40
42
|
|
|
41
43
|
|
|
42
|
-
def _decode_vertices(
|
|
44
|
+
def _decode_vertices(
|
|
45
|
+
module: ModuleType,
|
|
46
|
+
blob: bytes,
|
|
47
|
+
vertex_count: int,
|
|
48
|
+
) -> npt.NDArray[np.uint16]:
|
|
43
49
|
"""Decode a meshopt vertex buffer to ``(n, 4)`` uint16, avoiding the binding's unsafe path.
|
|
44
50
|
|
|
45
51
|
``meshoptimizer.decode_vertex_buffer`` takes a ``dtype`` argument, and **it must not be
|
|
@@ -62,7 +68,9 @@ class MeshoptCodec:
|
|
|
62
68
|
"""Name the manifest value this implements."""
|
|
63
69
|
return f"MeshoptCodec({self.name!r})"
|
|
64
70
|
|
|
65
|
-
def encode_positions(
|
|
71
|
+
def encode_positions(
|
|
72
|
+
self, quantized: npt.NDArray[np.uint16], *, compression: str
|
|
73
|
+
) -> bytes:
|
|
66
74
|
"""Pad each triple to four components and run meshopt's vertex codec over them.
|
|
67
75
|
|
|
68
76
|
The padding is not an invention: meshopt's codec requires a stride that is a multiple of
|
|
@@ -74,7 +82,11 @@ class MeshoptCodec:
|
|
|
74
82
|
quantized = np.ascontiguousarray(quantized, dtype="<u2").reshape(-1, 3)
|
|
75
83
|
padded = np.zeros((len(quantized), 4), dtype="<u2")
|
|
76
84
|
padded[:, :3] = quantized
|
|
77
|
-
return bytes(
|
|
85
|
+
return bytes(
|
|
86
|
+
module.encode_vertex_buffer(
|
|
87
|
+
np.ascontiguousarray(padded), len(padded), STRIDE
|
|
88
|
+
)
|
|
89
|
+
)
|
|
78
90
|
|
|
79
91
|
def decode_positions(
|
|
80
92
|
self, blob: bytes, *, compression: str, vertex_count: int | None
|
|
@@ -89,14 +101,24 @@ class MeshoptCodec:
|
|
|
89
101
|
module = require_meshoptimizer()
|
|
90
102
|
return _decode_vertices(module, blob, vertex_count)[:, :3]
|
|
91
103
|
|
|
92
|
-
def encode_indices(
|
|
104
|
+
def encode_indices(
|
|
105
|
+
self,
|
|
106
|
+
faces: npt.NDArray[np.uint32],
|
|
107
|
+
*,
|
|
108
|
+
compression: str,
|
|
109
|
+
vertex_count: int | None,
|
|
110
|
+
) -> bytes:
|
|
93
111
|
"""Run meshopt's index codec over the triangle list, in the order it was handed over."""
|
|
94
112
|
del compression
|
|
95
113
|
module = require_meshoptimizer()
|
|
96
114
|
faces = np.ascontiguousarray(faces, dtype="<u4").reshape(-1, 3)
|
|
97
115
|
if vertex_count is None:
|
|
98
116
|
vertex_count = int(faces.max()) + 1 if faces.size else 0
|
|
99
|
-
return bytes(
|
|
117
|
+
return bytes(
|
|
118
|
+
module.encode_index_buffer(
|
|
119
|
+
np.ascontiguousarray(faces.reshape(-1)), faces.size, vertex_count
|
|
120
|
+
)
|
|
121
|
+
)
|
|
100
122
|
|
|
101
123
|
def decode_indices(
|
|
102
124
|
self, blob: bytes, *, compression: str, index_count: int | None
|
|
@@ -109,7 +131,11 @@ class MeshoptCodec:
|
|
|
109
131
|
"`index_count`."
|
|
110
132
|
)
|
|
111
133
|
module = require_meshoptimizer()
|
|
112
|
-
return
|
|
134
|
+
return (
|
|
135
|
+
module.decode_index_buffer(index_count, 4, blob)
|
|
136
|
+
.reshape(-1, 3)
|
|
137
|
+
.astype(np.int64)
|
|
138
|
+
)
|
|
113
139
|
|
|
114
140
|
|
|
115
141
|
__all__ = ["STRIDE", "MeshoptCodec", "require_meshoptimizer"]
|
|
@@ -32,7 +32,9 @@ class BlobCodec(Protocol):
|
|
|
32
32
|
#: The ``encoding.codec`` value that selects this implementation.
|
|
33
33
|
name: str
|
|
34
34
|
|
|
35
|
-
def encode_positions(
|
|
35
|
+
def encode_positions(
|
|
36
|
+
self, quantized: npt.NDArray[np.uint16], *, compression: str
|
|
37
|
+
) -> bytes:
|
|
36
38
|
"""Pack an ``(n, 3)`` array of ``uint16`` quantized coordinates into a blob."""
|
|
37
39
|
...
|
|
38
40
|
|
|
@@ -47,7 +49,13 @@ class BlobCodec(Protocol):
|
|
|
47
49
|
"""
|
|
48
50
|
...
|
|
49
51
|
|
|
50
|
-
def encode_indices(
|
|
52
|
+
def encode_indices(
|
|
53
|
+
self,
|
|
54
|
+
faces: npt.NDArray[np.uint32],
|
|
55
|
+
*,
|
|
56
|
+
compression: str,
|
|
57
|
+
vertex_count: int | None,
|
|
58
|
+
) -> bytes:
|
|
51
59
|
"""Pack an ``(m, 3)`` triangle array into a blob, preserving triangle order.
|
|
52
60
|
|
|
53
61
|
Order is not cosmetic: ``object_index_offsets`` names ranges into the concatenated
|
|
@@ -30,9 +30,14 @@ class RawCodec:
|
|
|
30
30
|
"""Name the manifest value this implements."""
|
|
31
31
|
return f"RawCodec({self.name!r})"
|
|
32
32
|
|
|
33
|
-
def encode_positions(
|
|
33
|
+
def encode_positions(
|
|
34
|
+
self, quantized: npt.NDArray[np.uint16], *, compression: str
|
|
35
|
+
) -> bytes:
|
|
34
36
|
"""Interleave the ``uint16`` triples and hand back the bytes."""
|
|
35
|
-
return compress(
|
|
37
|
+
return compress(
|
|
38
|
+
np.ascontiguousarray(quantized, dtype="<u2").reshape(-1).tobytes(),
|
|
39
|
+
compression,
|
|
40
|
+
)
|
|
36
41
|
|
|
37
42
|
def decode_positions(
|
|
38
43
|
self, blob: bytes, *, compression: str, vertex_count: int | None
|
|
@@ -48,14 +53,22 @@ class RawCodec:
|
|
|
48
53
|
)
|
|
49
54
|
return quantized
|
|
50
55
|
|
|
51
|
-
def encode_indices(
|
|
56
|
+
def encode_indices(
|
|
57
|
+
self,
|
|
58
|
+
faces: npt.NDArray[np.uint32],
|
|
59
|
+
*,
|
|
60
|
+
compression: str,
|
|
61
|
+
vertex_count: int | None,
|
|
62
|
+
) -> bytes:
|
|
52
63
|
"""Flatten the triangle list to little-endian ``uint32``.
|
|
53
64
|
|
|
54
65
|
``vertex_count`` is unused here and accepted only so the two codecs take the same
|
|
55
66
|
arguments -- a raw index needs nothing but its own width.
|
|
56
67
|
"""
|
|
57
68
|
del vertex_count
|
|
58
|
-
return compress(
|
|
69
|
+
return compress(
|
|
70
|
+
np.ascontiguousarray(faces, dtype="<u4").reshape(-1).tobytes(), compression
|
|
71
|
+
)
|
|
59
72
|
|
|
60
73
|
def decode_indices(
|
|
61
74
|
self, blob: bytes, *, compression: str, index_count: int | None
|
|
@@ -70,7 +70,14 @@ import time. Reach for it by name -- ``from fabriks.contrib import read_glb``.
|
|
|
70
70
|
from fabriks.contrib.catalog import fit_of, names_of
|
|
71
71
|
from fabriks.contrib.fit import DEFAULT_RESOLUTION, VoxelFit
|
|
72
72
|
from fabriks.contrib.gltf import read_glb, read_gltf, write_glb, write_gltf
|
|
73
|
-
from fabriks.contrib.meshes import
|
|
73
|
+
from fabriks.contrib.meshes import (
|
|
74
|
+
read_off,
|
|
75
|
+
read_ply,
|
|
76
|
+
read_stl,
|
|
77
|
+
write_off,
|
|
78
|
+
write_ply,
|
|
79
|
+
write_stl,
|
|
80
|
+
)
|
|
74
81
|
from fabriks.contrib.obj import read_obj, write_obj
|
|
75
82
|
from fabriks.contrib.scene import Imported, ReadOptions, Source, WriteOptions
|
|
76
83
|
|
|
@@ -34,7 +34,11 @@ NAME_COLUMN = "name"
|
|
|
34
34
|
#: The uniform scale an import applied, repeated on every row.
|
|
35
35
|
FIT_SCALE_COLUMN = "fit_scale"
|
|
36
36
|
#: The translation an import applied, one column per component, repeated on every row.
|
|
37
|
-
FIT_TRANSLATION_COLUMNS = (
|
|
37
|
+
FIT_TRANSLATION_COLUMNS = (
|
|
38
|
+
"fit_translation_x",
|
|
39
|
+
"fit_translation_y",
|
|
40
|
+
"fit_translation_z",
|
|
41
|
+
)
|
|
38
42
|
|
|
39
43
|
|
|
40
44
|
def _table(collection: Any) -> pa.Table: # noqa: ANN401 - a built or an opened collection
|
|
@@ -85,7 +89,7 @@ def attach(collection: Any, *, names: Mapping[int, str], fit: VoxelFit) -> None:
|
|
|
85
89
|
pa.array([fit.scale] * len(ids), type=pa.float64()),
|
|
86
90
|
),
|
|
87
91
|
]
|
|
88
|
-
for axis, component in zip(FIT_TRANSLATION_COLUMNS, fit.translation):
|
|
92
|
+
for axis, component in zip(FIT_TRANSLATION_COLUMNS, fit.translation, strict=True):
|
|
89
93
|
columns.append(
|
|
90
94
|
(
|
|
91
95
|
pa.field(axis, pa.float64(), nullable=True),
|
|
@@ -113,7 +117,7 @@ def names_of(collection: Any) -> dict[int, str]: # noqa: ANN401 - an opened col
|
|
|
113
117
|
names = table.column(NAME_COLUMN).to_pylist()
|
|
114
118
|
return {
|
|
115
119
|
object_id: str(name)
|
|
116
|
-
for object_id, name in zip(_object_ids(table), names)
|
|
120
|
+
for object_id, name in zip(_object_ids(table), names, strict=True)
|
|
117
121
|
if name is not None
|
|
118
122
|
}
|
|
119
123
|
|
|
@@ -127,7 +131,9 @@ def fit_of(collection: Any) -> VoxelFit | None: # noqa: ANN401 - an opened coll
|
|
|
127
131
|
"""
|
|
128
132
|
table = _table(collection)
|
|
129
133
|
wanted = (FIT_SCALE_COLUMN, *FIT_TRANSLATION_COLUMNS)
|
|
130
|
-
if table.num_rows == 0 or any(
|
|
134
|
+
if table.num_rows == 0 or any(
|
|
135
|
+
column not in table.column_names for column in wanted
|
|
136
|
+
):
|
|
131
137
|
return None
|
|
132
138
|
|
|
133
139
|
numbers: list[float] = []
|