Skip to content

Commit eddc905

Browse files
committed
Chipping away in the test mines...
1 parent b7ffecb commit eddc905

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

tests/test_matrix44.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ def test_immul(self):
180180
m = Matrix44.scale(2)
181181
m @= Matrix44.translate([1, 2, 3])
182182
self.assertEqual(m, [2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 2, 4, 6, 1])
183+
m @= [1, 1, 1]
184+
self.assertIsNot(m, m1)
185+
self.assertEqual(m, [4, 6, 8])
183186

184187
def test_vmul(self):
185188
self.assertEqual(Matrix44() @ None, None)

tests/test_models.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def test_sphere(self):
5353
self.assertAlmostEqual(mesh.volume, 4/3*math.pi, places=int(math.log10(segments)))
5454
self.assertEqual(Model.sphere(0).name, '!sphere-4')
5555
self.assertEqual(Model.sphere(5).name, '!sphere-8')
56+
self.assertIs(Model.sphere(), Model.sphere(DefaultSegments))
5657

5758
def test_cylinder(self):
5859
for segments in (4, DefaultSegments, 1024):
@@ -70,6 +71,8 @@ def test_cylinder(self):
7071
else:
7172
self.assertAlmostEqual(mesh.area, 4*math.pi, places=int(math.log10(segments)))
7273
self.assertAlmostEqual(mesh.volume, math.pi, places=int(math.log10(segments)))
74+
self.assertEqual(Model.cylinder(0).name, '!cylinder-2')
75+
self.assertIs(Model.cylinder(), Model.cylinder(DefaultSegments))
7376

7477
def test_cone(self):
7578
for segments in (4, DefaultSegments, 1024):
@@ -87,6 +90,8 @@ def test_cone(self):
8790
else:
8891
self.assertAlmostEqual(mesh.area, (1+math.sqrt(2))*math.pi, places=int(math.log10(segments)))
8992
self.assertAlmostEqual(mesh.volume, math.pi/3, places=int(math.log10(segments)))
93+
self.assertEqual(Model.cone(0).name, '!cone-2')
94+
self.assertIs(Model.cone(), Model.cone(DefaultSegments))
9095

9196

9297
class TestBasicFunctionality(utils.TestCase):
@@ -470,7 +475,7 @@ def test_difference(self):
470475
self.assertEqual(Model.difference(Model.box(), Model.sphere()).trim(self.P, self.N).name, f'difference(trim(!box, {self.PN_hash}), !sphere)')
471476

472477

473-
class TestUVRemapping(utils.TestCase):
478+
class TestUnaryOperations(utils.TestCase):
474479
def tearDown(self):
475480
Model.flush_caches(0, 0)
476481

@@ -483,6 +488,22 @@ def test_uv_remap_sphere(self):
483488
v = (math.atan2(z, r) / math.pi + 0.5) % 1
484489
self.assertAllAlmostEqual(uv, [u, v])
485490

491+
def test_flatten(self):
492+
model = Model.sphere()
493+
nrows = DefaultSegments // 4
494+
mesh = model.get_trimesh()
495+
self.assertEqual(len(mesh.vertices), 4*(nrows+1)*(nrows+2))
496+
self.assertEqual(len(mesh.faces), 8*nrows*nrows)
497+
self.assertAlmostEqual(mesh.area, 4*math.pi, places=1)
498+
self.assertAlmostEqual(mesh.volume, 4/3*math.pi, places=1)
499+
flat_model = model.flatten()
500+
self.assertIsNot(flat_model, model)
501+
mesh = flat_model.get_trimesh()
502+
self.assertEqual(len(mesh.vertices), 3*8*nrows*nrows)
503+
self.assertEqual(len(mesh.faces), 8*nrows*nrows)
504+
self.assertAlmostEqual(mesh.area, 4*math.pi, places=1)
505+
self.assertAlmostEqual(mesh.volume, 4/3*math.pi, places=1)
506+
486507

487508
class TestTrim(utils.TestCase):
488509
def tearDown(self):
@@ -621,6 +642,35 @@ def wrap_name(self, name):
621642
return f'sdf({name}, -3;-3;-3, 3;3;3, 0.1)'
622643

623644

645+
class TestSdfTrim(utils.TestCase):
646+
def tearDown(self):
647+
Model.flush_caches(0, 0)
648+
649+
def test_trim_sphere_to_box(self):
650+
model = Model.sphere()
651+
for x in (-1, 1):
652+
model = model.trim((x/2, 0, 0), (x, 0, 0))
653+
for y in (-1, 1):
654+
model = model.trim((0, y/2, 0), (0, y, 0))
655+
for z in (-1, 1):
656+
model = model.trim((0, 0, z/2), (0, 0, z))
657+
model = Model.sdf(None, model, (-1, -1, -1), (1, 1, 1), 0.05)
658+
mesh = model.get_trimesh()
659+
self.assertEqual(mesh.bounds.tolist(), [[-0.5, -0.5, -0.5], [0.5, 0.5, 0.5]])
660+
self.assertAlmostEqual(mesh.area, 6)
661+
self.assertAlmostEqual(mesh.volume, 1)
662+
663+
def test_trim_to_nothing(self):
664+
model = Model.box()
665+
model = model.trim((0, 0, -1), (0, 0, 1))
666+
model = Model.sdf(None, model, (-1, -1, -1), (1, 1, 1), 0.05)
667+
with unittest.mock.patch('flitter.render.window.models.logger') as mock_logger:
668+
manifold = model.get_manifold()
669+
mock_logger.warning.assert_called_with("Result of operation was empty mesh: {}", model.name)
670+
self.assertIsNone(manifold)
671+
self.assertIsNone(model.get_trimesh())
672+
673+
624674
class TestBuffers(utils.TestCase):
625675
def tearDown(self):
626676
Model.flush_caches(0, 0)

tests/test_vector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ def test_match(self):
412412
self.assertEqual(Vector.symbol('foo').concat(Vector.symbol('bar')).match(2, str), ['foo', 'bar'])
413413
self.assertEqual(Vector.symbol('foo').concat(Vector(1)).match(2), ['foo', 1])
414414
self.assertEqual(Vector.symbol('foo').concat(Vector(1)).match(2, str), None)
415+
self.assertEqual(Vector.symbol('foo').concat(Vector(1)).match(2, int), None)
415416
self.assertEqual(Vector.symbol('foo').concat(Vector(1)).match(2, float), [FOO_SYMBOL_NUMBER, 1])
416417
self.assertEqual(Vector(FOO_SYMBOL_NUMBER).match(1, str), 'foo')
417418
self.assertEqual(Vector(FOO_SYMBOL_NUMBER).match(2, None), ['foo', 'foo'])
@@ -780,7 +781,7 @@ def test_maximum(self):
780781
def test_minimum(self):
781782
self.assertTrue(math.isnan(null.minimum()))
782783
self.assertTrue(math.isnan(Vector('hello').minimum()))
783-
self.assertEqual(Vector.range(10).minimum(), 0)
784+
self.assertEqual(Vector.range(9, -1, -1).minimum(), 0)
784785

785786
def test_squared_sum(self):
786787
self.assertTrue(math.isnan(null.squared_sum()))

0 commit comments

Comments
 (0)