Skip to content

Commit

Permalink
Chipping away in the test mines...
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhogg committed Jan 3, 2025
1 parent b7ffecb commit eddc905
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
3 changes: 3 additions & 0 deletions tests/test_matrix44.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ def test_immul(self):
m = Matrix44.scale(2)
m @= Matrix44.translate([1, 2, 3])
self.assertEqual(m, [2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 2, 4, 6, 1])
m @= [1, 1, 1]
self.assertIsNot(m, m1)
self.assertEqual(m, [4, 6, 8])

def test_vmul(self):
self.assertEqual(Matrix44() @ None, None)
Expand Down
52 changes: 51 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def test_sphere(self):
self.assertAlmostEqual(mesh.volume, 4/3*math.pi, places=int(math.log10(segments)))
self.assertEqual(Model.sphere(0).name, '!sphere-4')
self.assertEqual(Model.sphere(5).name, '!sphere-8')
self.assertIs(Model.sphere(), Model.sphere(DefaultSegments))

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

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


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


class TestUVRemapping(utils.TestCase):
class TestUnaryOperations(utils.TestCase):
def tearDown(self):
Model.flush_caches(0, 0)

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

def test_flatten(self):
model = Model.sphere()
nrows = DefaultSegments // 4
mesh = model.get_trimesh()
self.assertEqual(len(mesh.vertices), 4*(nrows+1)*(nrows+2))
self.assertEqual(len(mesh.faces), 8*nrows*nrows)
self.assertAlmostEqual(mesh.area, 4*math.pi, places=1)
self.assertAlmostEqual(mesh.volume, 4/3*math.pi, places=1)
flat_model = model.flatten()
self.assertIsNot(flat_model, model)
mesh = flat_model.get_trimesh()
self.assertEqual(len(mesh.vertices), 3*8*nrows*nrows)
self.assertEqual(len(mesh.faces), 8*nrows*nrows)
self.assertAlmostEqual(mesh.area, 4*math.pi, places=1)
self.assertAlmostEqual(mesh.volume, 4/3*math.pi, places=1)


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


class TestSdfTrim(utils.TestCase):
def tearDown(self):
Model.flush_caches(0, 0)

def test_trim_sphere_to_box(self):
model = Model.sphere()
for x in (-1, 1):
model = model.trim((x/2, 0, 0), (x, 0, 0))
for y in (-1, 1):
model = model.trim((0, y/2, 0), (0, y, 0))
for z in (-1, 1):
model = model.trim((0, 0, z/2), (0, 0, z))
model = Model.sdf(None, model, (-1, -1, -1), (1, 1, 1), 0.05)
mesh = model.get_trimesh()
self.assertEqual(mesh.bounds.tolist(), [[-0.5, -0.5, -0.5], [0.5, 0.5, 0.5]])
self.assertAlmostEqual(mesh.area, 6)
self.assertAlmostEqual(mesh.volume, 1)

def test_trim_to_nothing(self):
model = Model.box()
model = model.trim((0, 0, -1), (0, 0, 1))
model = Model.sdf(None, model, (-1, -1, -1), (1, 1, 1), 0.05)
with unittest.mock.patch('flitter.render.window.models.logger') as mock_logger:
manifold = model.get_manifold()
mock_logger.warning.assert_called_with("Result of operation was empty mesh: {}", model.name)
self.assertIsNone(manifold)
self.assertIsNone(model.get_trimesh())


class TestBuffers(utils.TestCase):
def tearDown(self):
Model.flush_caches(0, 0)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ def test_match(self):
self.assertEqual(Vector.symbol('foo').concat(Vector.symbol('bar')).match(2, str), ['foo', 'bar'])
self.assertEqual(Vector.symbol('foo').concat(Vector(1)).match(2), ['foo', 1])
self.assertEqual(Vector.symbol('foo').concat(Vector(1)).match(2, str), None)
self.assertEqual(Vector.symbol('foo').concat(Vector(1)).match(2, int), None)
self.assertEqual(Vector.symbol('foo').concat(Vector(1)).match(2, float), [FOO_SYMBOL_NUMBER, 1])
self.assertEqual(Vector(FOO_SYMBOL_NUMBER).match(1, str), 'foo')
self.assertEqual(Vector(FOO_SYMBOL_NUMBER).match(2, None), ['foo', 'foo'])
Expand Down Expand Up @@ -780,7 +781,7 @@ def test_maximum(self):
def test_minimum(self):
self.assertTrue(math.isnan(null.minimum()))
self.assertTrue(math.isnan(Vector('hello').minimum()))
self.assertEqual(Vector.range(10).minimum(), 0)
self.assertEqual(Vector.range(9, -1, -1).minimum(), 0)

def test_squared_sum(self):
self.assertTrue(math.isnan(null.squared_sum()))
Expand Down

0 comments on commit eddc905

Please sign in to comment.