Skip to content

Commit 404163c

Browse files
committed
Yet more tests + fixes related to them
1 parent 2fc69e5 commit 404163c

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

src/flitter/render/window/models.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ cdef class Model:
230230
return self._snap_edges(float(snap_angle), float(minimum_area))
231231

232232
cdef Model _transform(self, Matrix44 transform_matrix):
233+
if transform_matrix.eq(IdentityTransform) is true_:
234+
return self
233235
return Transform._get(self, transform_matrix)
234236

235237
def transform(self, transform_matrix):
@@ -249,7 +251,7 @@ cdef class Model:
249251

250252
@staticmethod
251253
cdef Model _intersect(list models):
252-
return BooleanOperation._get('intersection', models)
254+
return BooleanOperation._get('intersect', models)
253255

254256
@staticmethod
255257
def intersect(*models):
@@ -430,7 +432,7 @@ cdef class SnapEdges(UnaryOperation):
430432
snap_angle = min(max(0, snap_angle), 0.5)
431433
minimum_area = min(max(0, minimum_area), 1)
432434
cdef str name = 'snap_edges(' + original.name
433-
if snap_angle != DefaultSnapAngle:
435+
if minimum_area or snap_angle != DefaultSnapAngle:
434436
name += f', {snap_angle:g}'
435437
if minimum_area:
436438
name += f', {minimum_area:g}'
@@ -472,8 +474,6 @@ cdef class Transform(UnaryOperation):
472474

473475
@staticmethod
474476
cdef Model _get(Model original, Matrix44 transform_matrix):
475-
if transform_matrix.eq(IdentityTransform) is true_:
476-
return original
477477
cdef str name = f'{original.name}@{hex(transform_matrix.hash(False))[2:]}'
478478
cdef Transform model = <Transform>ModelCache.get(name, None)
479479
if model is None:
@@ -515,7 +515,7 @@ cdef class UVRemap(UnaryOperation):
515515

516516
@staticmethod
517517
cdef UVRemap _get(Model original, str mapping):
518-
cdef str name = f'uvremap({original.name}, {mapping})'
518+
cdef str name = f'uv_remap({original.name}, {mapping})'
519519
cdef UVRemap model = <UVRemap>ModelCache.get(name, None)
520520
if model is None:
521521
model = UVRemap.__new__(UVRemap)
@@ -689,7 +689,7 @@ cdef class BooleanOperation(Model):
689689
if manifold is None:
690690
if self.operation is 'difference' and i == 0:
691691
return None
692-
if self.operation is 'intersection':
692+
if self.operation is 'intersect':
693693
return None
694694
else:
695695
manifolds.append(manifold)
@@ -699,7 +699,7 @@ cdef class BooleanOperation(Model):
699699
return manifolds[0]
700700
if self.operation is 'union':
701701
manifold = manifold3d.Manifold.batch_boolean(manifolds, manifold3d.OpType.Add)
702-
elif self.operation is 'intersection':
702+
elif self.operation is 'intersect':
703703
manifold = manifold3d.Manifold.batch_boolean(manifolds, manifold3d.OpType.Intersect)
704704
elif self.operation is 'difference':
705705
manifold = manifold3d.Manifold.batch_boolean(manifolds, manifold3d.OpType.Subtract)

tests/test_models.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import trimesh
88

9-
from flitter.model import Matrix44
9+
from flitter.model import Vector, Matrix44
1010
from flitter.render.window.models import Model
1111

1212
from . import utils
@@ -208,3 +208,69 @@ def test_invalidate_dependency(self):
208208
self.assertIsNot(model.get_bounds(), bounds)
209209
self.assertIsNot(model.get_trimesh(), mesh)
210210
self.assertIsNot(model.get_manifold(), manifold)
211+
212+
213+
class TestStructuring(utils.TestCase):
214+
def test_flatten(self):
215+
self.assertEqual(Model.box().flatten().name, 'flatten(!box)')
216+
self.assertEqual(Model.box().flatten().flatten().name, 'flatten(!box)')
217+
self.assertEqual(Model.box().flatten().snap_edges().name, 'flatten(!box)')
218+
219+
def test_invert(self):
220+
self.assertEqual(Model.box().invert().name, 'invert(!box)')
221+
self.assertEqual(Model.box().invert().invert().name, '!box')
222+
self.assertEqual(Model.box().invert().repair().name, 'invert(repair(!box))')
223+
self.assertEqual(Model.box().invert().snap_edges().name, 'invert(snap_edges(!box))')
224+
M = Matrix44.translate(1)
225+
self.assertEqual(Model.box().invert().transform(M).name, f'invert(!box@{hex(M.hash(False))[2:]})')
226+
227+
def test_repair(self):
228+
self.assertEqual(Model.box().repair().name, 'repair(!box)')
229+
self.assertEqual(Model.box().repair().repair().name, 'repair(!box)')
230+
231+
def test_snap_edges(self):
232+
self.assertEqual(Model.box().snap_edges(0).name, 'flatten(!box)')
233+
self.assertEqual(Model.box().snap_edges().name, 'snap_edges(!box)')
234+
self.assertEqual(Model.box().snap_edges(0.05).name, 'snap_edges(!box)')
235+
self.assertEqual(Model.box().snap_edges(0.25).name, 'snap_edges(!box, 0.25)')
236+
self.assertEqual(Model.box().snap_edges(0.05, 0.25).name, 'snap_edges(!box, 0.05, 0.25)')
237+
self.assertEqual(Model.box().snap_edges(0.25, 0.25).name, 'snap_edges(!box, 0.25, 0.25)')
238+
239+
def test_transform(self):
240+
self.assertEqual(Model.box().transform(Matrix44()).name, '!box')
241+
M = Matrix44.translate(1)
242+
H = hex(M.hash(False))[2:]
243+
self.assertEqual(Model.box().transform(M).name, f'!box@{H}')
244+
self.assertEqual(Model.box().transform(M).repair().name, f'repair(!box)@{H}')
245+
M2 = Matrix44.translate(2)
246+
H2 = hex(M2.hash(False))[2:]
247+
self.assertEqual(Model.box().transform(M).transform(M).name, f'!box@{H2}')
248+
249+
def test_uvremap(self):
250+
self.assertEqual(Model.box().uv_remap('sphere').name, 'uv_remap(!box, sphere)')
251+
252+
def test_slice(self):
253+
P = Vector((0, 0, 0))
254+
N = Vector((1, 0, 0))
255+
H = hex(P.hash(False) ^ N.hash(False))[2:]
256+
self.assertEqual(Model.box().slice(P, N).name, f'slice(!box, {H})')
257+
self.assertEqual(Model.box().slice(P, N).repair().name, f'slice(!box, {H})')
258+
259+
def test_union(self):
260+
self.assertIsNone(Model.union())
261+
self.assertEqual(Model.union(Model.box()).name, '!box')
262+
self.assertEqual(Model.union(Model.box(), Model.box()).name, '!box')
263+
self.assertEqual(Model.union(Model.box(), Model.sphere()).name, 'union(!box, !sphere)')
264+
self.assertEqual(Model.union(Model.box(), Model.union(Model.sphere(), Model.cylinder())).name, 'union(!box, !sphere, !cylinder)')
265+
266+
def test_intersect(self):
267+
self.assertIsNone(Model.intersect())
268+
self.assertEqual(Model.intersect(Model.box()).name, '!box')
269+
self.assertEqual(Model.intersect(Model.box(), Model.box()).name, '!box')
270+
self.assertEqual(Model.intersect(Model.box(), Model.sphere()).name, 'intersect(!box, !sphere)')
271+
272+
def test_difference(self):
273+
self.assertIsNone(Model.difference())
274+
self.assertEqual(Model.difference(Model.box()).name, '!box')
275+
self.assertIsNone(Model.difference(Model.box(), Model.box()))
276+
self.assertEqual(Model.difference(Model.box(), Model.sphere()).name, 'difference(!box, !sphere)')

0 commit comments

Comments
 (0)