Skip to content

Commit f831655

Browse files
committed
Add tests of models boolean logic functionality
1 parent ffddf73 commit f831655

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tests/test_models.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,79 @@ def test_trim_to_nothing(self):
407407
mock_logger.warning.assert_called_with("Result of trim was empty: {}", model.name)
408408
self.assertIsNone(manifold)
409409
self.assertIsNone(model.get_trimesh())
410+
411+
412+
class TestBoolean(utils.TestCase):
413+
def setUp(self):
414+
self.nested_box_models = [Model.box().transform(Matrix44.scale(2)), Model.box()]
415+
self.cross_models = [Model.box().transform(Matrix44.scale([3, 1, 1])),
416+
Model.box().transform(Matrix44.scale([1, 3, 1])),
417+
Model.box().transform(Matrix44.scale([1, 1, 3]))]
418+
self.capsule_models = [Model.cylinder().transform(Matrix44.scale([1, 1, 2])),
419+
Model.sphere().transform(Matrix44.translate([0, 0, 1])),
420+
Model.sphere().transform(Matrix44.translate([0, 0, -1]))]
421+
422+
def tearDown(self):
423+
Model.flush_caches(0, 0)
424+
425+
def test_nested_box_union(self):
426+
model = Model.union(*self.nested_box_models)
427+
mesh = model.get_trimesh()
428+
self.assertAlmostEqual(mesh.area, 24)
429+
self.assertAlmostEqual(mesh.volume, 8)
430+
431+
def test_nested_box_intersect(self):
432+
model = Model.intersect(*self.nested_box_models)
433+
mesh = model.get_trimesh()
434+
self.assertAlmostEqual(mesh.area, 6)
435+
self.assertAlmostEqual(mesh.volume, 1)
436+
437+
def test_nested_box_difference(self):
438+
model = Model.difference(*self.nested_box_models)
439+
mesh = model.get_trimesh()
440+
self.assertAlmostEqual(mesh.area, 30)
441+
self.assertAlmostEqual(mesh.volume, 7)
442+
443+
def test_nested_box_reversed_difference(self):
444+
model = Model.difference(*reversed(self.nested_box_models))
445+
with unittest.mock.patch('flitter.render.window.models.logger') as mock_logger:
446+
mesh = model.get_trimesh()
447+
mock_logger.warning.assert_called_with("Result of {} was empty: {}", "difference", model.name)
448+
self.assertIsNone(mesh)
449+
450+
def test_cross_union(self):
451+
model = Model.union(*self.cross_models)
452+
mesh = model.get_trimesh()
453+
self.assertAlmostEqual(mesh.area, 30)
454+
self.assertAlmostEqual(mesh.volume, 7)
455+
456+
def test_cross_intersect(self):
457+
model = Model.intersect(*self.cross_models)
458+
mesh = model.get_trimesh()
459+
self.assertAlmostEqual(mesh.area, 6)
460+
self.assertAlmostEqual(mesh.volume, 1)
461+
462+
def test_cross_difference(self):
463+
model = Model.difference(*self.cross_models)
464+
mesh = model.get_trimesh()
465+
self.assertAlmostEqual(mesh.area, 12)
466+
self.assertAlmostEqual(mesh.volume, 2)
467+
468+
def test_capsule_union(self):
469+
model = Model.union(*self.capsule_models)
470+
mesh = model.get_trimesh()
471+
self.assertAlmostEqual(mesh.area, 4*math.pi + 4*math.pi, places=1)
472+
self.assertAlmostEqual(mesh.volume, 2*math.pi + 4/3*math.pi, places=1)
473+
474+
def test_capsule_intersect(self):
475+
model = Model.intersect(*self.capsule_models)
476+
with unittest.mock.patch('flitter.render.window.models.logger') as mock_logger:
477+
mesh = model.get_trimesh()
478+
mock_logger.warning.assert_called_with("Result of {} was empty: {}", "intersect", model.name)
479+
self.assertIsNone(mesh)
480+
481+
def test_capsule_difference(self):
482+
model = Model.difference(*self.capsule_models)
483+
mesh = model.get_trimesh()
484+
self.assertAlmostEqual(mesh.area, 4*math.pi + 4*math.pi, places=1)
485+
self.assertAlmostEqual(mesh.volume, 2*math.pi - 4/3*math.pi, places=1)

0 commit comments

Comments
 (0)