def test_almost_equal(self): set1 = [Vertex((0.0, 0.0, 0.0)), Vertex((1.0, 1.0, 1.0))] set2 = [ Vertex((0.000000001, 0.000000001, 0.000000000001)), Vertex((1.0000000001, 1.0000001, 1.0000001)) ] self.assertFalse(set1 == set2) self.assertTrue(almost_equal_lists(set1, set2))
def test_bezier_curves(self): self.simple_curve_test( [Vertex((0.0, 0.0, 0.0)), Vertex((1.0, 1.0, 1.0))], bezier_curve_1) self.simple_curve_test([ Vertex((0.0, 0.0, 0.0)), Vertex((5.0, 2.0, 3.0)), Vertex((1.0, 1.0, 1.0)) ], bezier_curve_2)
def test_bernstein(self): verts = [ Vertex([0.0, 0.0, 0.0]), Vertex([1.0, 0.0, 0.0]), Vertex([0.0, 1.0, 0.0]), Vertex([1.0, 1.0, 0.0]) ] patch = bezier_patch_n(verts) self.assertEqual( set(tuple(v) for v in verts), set(tuple(patch(u, v)) for u in [0, 1] for v in [0, 1]))
def test_abs(self): self.assertEqual(Vertex([1.0, 2.0, 3.0]), abs(Vertex([1.0, 2.0, 3.0]))) self.assertEqual(Vertex([1.0, 2.0, 3.0]), abs(Vertex([1.0, -2.0, 3.0]))) self.assertNotEqual(Vertex([1.0, -2.0, 3.0]), abs(Vertex([1.0, -2.0, 3.0]))) self.assertNotEqual( Vertex((0.05555557459592819, -0.05555557459592819, 0.0)), Vertex((0.05555557459592819, -0.5, 0.0))) self.assertFalse( Vertex((0.05555557459592819, -0.05555557459592819, 0.0)) < Vertex((0.05555557459592819, -0.5, 0.0)))
def test_bezier_patch(self, cpoints): cverts = [Vertex(p) for p in cpoints] bzr_patch = compile_bezier_patch(cverts) points = sample_func_2D(bzr_patch, 0.1) estimated_points = fit_bezier_patch(points, bezier_patch_2_tuple) estimaded_cverts = [Vertex(p) for p in estimated_points] if VERBOSE: print(".....................................") print(cverts) print(estimaded_cverts) print(".....................................") self.assertTrue(almost_equal_lists(cverts, estimaded_cverts))
def simple_curve_test(self, coeffs, out_curve): func = compile_bezier_curve(coeffs) points = list(sample_func(func, 0.1)) estimated_coeffs = [ Vertex(x) for x in fit_bezier_curve(points, out_curve) ] self.assertTrue(almost_equal_lists(coeffs, estimated_coeffs))
def setUp(self): verts = list( map(lambda x: Vertex(x), [(0.0, 0.0, 0.0), (0.0, 0.0, 0.0), (0.0, 0.5, 0.0), (1.0, 1.0, 1.0)])) edges = list( map(lambda x: Curve(x), [(0, 0, 1), (0, 0, 2), (0, 0, 3), (1, 1, 2), (1, 1, 3), (2, 2, 3)])) faces = list( map(lambda x: Face(x), [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)])) self.mesh = Mesh(verts, edges, faces)
def write_slices(data, num_slices, output_path): """Découpe le modèle 3D en tranches et écrit le résultat dans des fichiers SVG. """ height = abs(data['zmax'] - data['zmin']) slices = [ data['zmin'] + i * height / (num_slices + 1) for i in range(1, num_slices + 1) ] for i, height in enumerate(slices): print('Création de la tranche numéro', i) path = os.path.join(output_path, 'slice_{}.svg'.format(i)) file_ = open(path, 'w') svg_width = int(abs(data['xmax'] - data['xmin'])) + 10 svg_height = int(abs(data['ymax'] - data['ymin'])) + 10 file_.write(header(svg_width, svg_height)) for triangle in data['triangles']: segment = [] for side in itertools.combinations(triangle.vertices, 2): if intersect(height, side): xa, ya, za = side[0].position xb, yb, zb = side[1].position if zb - za != 0: c = (height - za) / (zb - za) vertex = Vertex( (xa + c * (xb - xa), ya + c * (yb - ya), height)) segment.append(vertex) else: print('divide by 0') if len(segment) > 0: file_.write( line(segment[0].x - data['xmin'], segment[0].y - data['ymin'], segment[1].x - data['xmin'], segment[1].y - data['ymin'])) file_.write(footer()) file_.close()
def parse_stl(filename): """Parse un fichier STL.""" data = { 'triangles': [], # Extremums 'xmin': float('inf'), 'xmax': float('-inf'), 'ymin': float('inf'), 'ymax': float('-inf'), 'zmin': float('inf'), 'zmax': float('-inf'), } file = open(filename, 'rb') parser = BinaryParser(file) parser.read_bytes(80) data['num_triangles'] = parser.read_uint32() print('Nombre de triangles :', data['num_triangles']) for _ in range(data['num_triangles']): # Vecteur normal parser.read_float(3) vertices = [] for _ in range(3): vertex = Vertex(parser.read_float(3)) data['xmin'], data['xmax'] = extremums( data['xmin'], data['xmax'], vertex.x) data['ymin'], data['ymax'] = extremums( data['ymin'], data['ymax'], vertex.y) data['zmin'], data['zmax'] = extremums( data['zmin'], data['zmax'], vertex.z) vertices.append(vertex) # Attribute byte count parser.read_uint16() triangle = Triangle(vertices) data['triangles'].append(triangle) file.close() return data
def setUp(self): verts_args = [(0.0, 0.0, 0.0), (0.5, 1.0, 0.0), (1.0, 0.0, 0.0), (1.5, 0.5, 0.0), (2.0, 0.0, 0.0)] verts = [Vertex(args) for args in verts_args] self.mesh = Mesh(verts, None, None)
def test_double_patch(self): points = [Vertex(p) for p in two_patches_points] estimated_points = fit_bezier_patch(points, bezier_patch_2_tuple) print(estimated_points)
def setUp(self): self.verts = list( map(lambda x: Vertex(x), [(0.0, 0.0, 0.0), (0.0, 0.0, 0.0), (0.0, 0.5, 0.0), (1.0, 1.0, 1.0)]))
def test_squared_errors(self): vs1 = sample2D(self.patch, self.grid, 1.0) vs2 = sample2D(self.patch, self.grid, 1.0) self.assertEqual(squared_error_verts(vs1, vs2), 0.0) vs2[0] = Vertex((0.05555557459592819, -0.05555557459592819, 0.5)) self.assertNotEqual(squared_error_verts(vs1, vs2), 0.0)
def test_module(self): v = Vertex((1.0, 1.0, 1.0)) self.assertEqual(module(v), 3.0)
def test_vertex_operations(self): self.assertEqual(Vertex((1, 1, 1)), -Vertex((-1, -1, -1))) self.assertEqual( Vertex((1, 1, 1)) + Vertex((1, 2, 3)), Vertex((2, 3, 4))) self.assertEqual( Vertex((1, 1, 1)) - Vertex((1, 2, 3)), Vertex((0, -1, -2))) self.assertEqual(Vertex((1, 1, 1)) * 2, Vertex((2, 2, 2))) self.assertEqual(2 * Vertex((1, 1, 1)), Vertex((2, 2, 2))) self.assertEqual(Vertex((1, 1, 1)) + 1, Vertex((2, 2, 2))) self.assertEqual(1 + Vertex((1, 1, 1)), Vertex((2, 2, 2))) self.assertEqual(round(Vertex([1.11, 1.11, 1.11])), Vertex([1, 1, 1])) self.assertEqual(round(Vertex([1.11, 1.11, 1.11]), 1), Vertex([1.1, 1.1, 1.1])) self.assertNotEqual( Vertex((0.05555557459592819, -0.05555557459592819, 0.0)), Vertex((0.05555557459592819, -0.5, 0.0)))