def test_size_of_x_and_y_for_const_graph(self): def const_2(x, y): return 2 graph = ZGraph((0,1), (0,1), const_2, 10) solid = graph.solid_output() x_list = [ v[0] for f in solid.facets for v in f.vertices ] y_list = [ v[1] for f in solid.facets for v in f.vertices ] self.assertEqual(0, min(x_list)) self.assertEqual(1, max(x_list)) self.assertEqual(0, min(y_list)) self.assertEqual(1, max(y_list))
def test_size_of_x_and_y_for_const_graph(self): def const_2(x, y): return 2 graph = ZGraph((0, 1), (0, 1), const_2, 10) solid = graph.solid_output() x_list = [v[0] for f in solid.facets for v in f.vertices] y_list = [v[1] for f in solid.facets for v in f.vertices] self.assertEqual(0, min(x_list)) self.assertEqual(1, max(x_list)) self.assertEqual(0, min(y_list)) self.assertEqual(1, max(y_list))
def test_triangulate_triangle(self): normal = stl.Vector3d(1, 1, 1) a = stl.Vector3d(1, 0, 0) b = stl.Vector3d(0, 1, 0) c = stl.Vector3d(0, 0, 1) expected = stl.Facet(normal, (a, b, c)) result = ZGraph.triangulate(normal, (a, b, c)) self.assertEqual(result[0], expected)
def test_triangulate_square(self): top_left_front = stl.Vector3d(0, 0, 2) top_right_front = stl.Vector3d(1, 0, 2) top_left_back = stl.Vector3d(0, 1, 2) top_right_back = stl.Vector3d(1, 1, 2) corners = [ top_left_front, top_left_back, top_right_back, top_right_front ] normal = stl.Vector3d(0, 0, 1) result = ZGraph.triangulate(normal, corners) self.assertEqual(len(result), 2)
def test_triangulate_square(self): top_left_front = stl.Vector3d(0, 0, 2) top_right_front = stl.Vector3d(1, 0, 2) top_left_back = stl.Vector3d(0, 1, 2) top_right_back = stl.Vector3d(1, 1, 2) corners = [top_left_front, top_left_back, top_right_back, top_right_front] normal = stl.Vector3d(0, 0, 1) result = ZGraph.triangulate(normal, corners) self.assertEqual(len(result), 2)
def test_solid_output_for_simple_graph(self): def simple(x, y): if x > 0.5 and y > 0.5: return 2 else: return 1 graph = ZGraph((0, 1), (0, 1), simple, 1) top_left_front = stl.Vector3d(0, 0, 1) top_right_front = stl.Vector3d(1, 0, 1) top_left_back = stl.Vector3d(0, 1, 1) top_right_back = stl.Vector3d(1, 1, 2) bottom_left_front = stl.Vector3d(0, 0, 0) bottom_right_front = stl.Vector3d(1, 0, 0) bottom_left_back = stl.Vector3d(0, 1, 0) bottom_right_back = stl.Vector3d(1, 1, 0) top = [ stl.Facet(stl.Vector3d(0, -1 / sqrt(2), 1 / sqrt(2)), [top_left_front, top_right_front, top_right_back]), stl.Facet(stl.Vector3d(-1 / sqrt(2), 0, 1 / sqrt(2)), [top_left_front, top_right_back, top_left_back]), ] bottom = ZGraph.triangulate(stl.Vector3d(0, 0, -1), (bottom_left_front, bottom_left_back, bottom_right_back, bottom_right_front)) front = ZGraph.triangulate(stl.Vector3d(0, -1, 0), (bottom_left_front, bottom_right_front, top_right_front, top_left_front)) right = ZGraph.triangulate(stl.Vector3d(1, 0, 0), (bottom_right_front, bottom_right_back, top_right_back, top_right_front)) back = ZGraph.triangulate(stl.Vector3d(0, 1, 0), (bottom_left_back, top_left_back, top_right_back, bottom_right_back)) left = ZGraph.triangulate(stl.Vector3d(-1, 0, 0), (bottom_left_front, top_left_front, top_left_back, bottom_left_back)) facets = top + bottom + front + right + back + left expected = stl.Solid("ZGraph", facets) self.assertEqual(graph.solid_output(), expected)
def test_solid_output_for_const_graph(self): def const_2(x, y): return 2 graph = ZGraph((0, 1), (0, 1), const_2, 1) top_left_front = stl.Vector3d(0, 0, 2) top_right_front = stl.Vector3d(1, 0, 2) top_left_back = stl.Vector3d(0, 1, 2) top_right_back = stl.Vector3d(1, 1, 2) bottom_left_front = stl.Vector3d(0, 0, 0) bottom_right_front = stl.Vector3d(1, 0, 0) bottom_left_back = stl.Vector3d(0, 1, 0) bottom_right_back = stl.Vector3d(1, 1, 0) top = ZGraph.triangulate( stl.Vector3d(0, 0, 1), (top_left_front, top_right_front, top_right_back, top_left_back)) bottom = ZGraph.triangulate(stl.Vector3d(0, 0, -1), (bottom_left_front, bottom_left_back, bottom_right_back, bottom_right_front)) front = ZGraph.triangulate(stl.Vector3d(0, -1, 0), (bottom_left_front, bottom_right_front, top_right_front, top_left_front)) right = ZGraph.triangulate(stl.Vector3d(1, 0, 0), (bottom_right_front, bottom_right_back, top_right_back, top_right_front)) back = ZGraph.triangulate(stl.Vector3d(0, 1, 0), (bottom_left_back, top_left_back, top_right_back, bottom_right_back)) left = ZGraph.triangulate(stl.Vector3d(-1, 0, 0), (bottom_left_front, top_left_front, top_left_back, bottom_left_back)) facets = top + bottom + front + right + back + left expected = stl.Solid("ZGraph", facets) self.assertEqual(graph.solid_output(), expected)
def test_solid_output_for_simple_graph(self): def simple(x, y): if x > 0.5 and y > 0.5: return 2 else: return 1 graph = ZGraph((0,1), (0,1), simple, 1) top_left_front = stl.Vector3d(0, 0, 1) top_right_front = stl.Vector3d(1, 0, 1) top_left_back = stl.Vector3d(0, 1, 1) top_right_back = stl.Vector3d(1, 1, 2) bottom_left_front = stl.Vector3d(0, 0, 0) bottom_right_front = stl.Vector3d(1, 0, 0) bottom_left_back = stl.Vector3d(0, 1, 0) bottom_right_back = stl.Vector3d(1, 1, 0) top = [ stl.Facet( stl.Vector3d(0, -1/sqrt(2), 1/sqrt(2)), [ top_left_front, top_right_front, top_right_back ] ), stl.Facet( stl.Vector3d(-1/sqrt(2), 0, 1/sqrt(2)), [ top_left_front, top_right_back, top_left_back ] ), ] bottom = ZGraph.triangulate( stl.Vector3d(0, 0, -1), ( bottom_left_front, bottom_left_back, bottom_right_back, bottom_right_front ) ) front = ZGraph.triangulate( stl.Vector3d(0, -1, 0), ( bottom_left_front, bottom_right_front, top_right_front, top_left_front ) ) right = ZGraph.triangulate( stl.Vector3d(1, 0, 0), ( bottom_right_front, bottom_right_back, top_right_back, top_right_front ) ) back = ZGraph.triangulate( stl.Vector3d(0, 1, 0), ( bottom_left_back, top_left_back, top_right_back, bottom_right_back ) ) left = ZGraph.triangulate( stl.Vector3d(-1, 0, 0), ( bottom_left_front, top_left_front, top_left_back, bottom_left_back ) ) facets = top + bottom + front + right + back + left expected = stl.Solid("ZGraph", facets) self.assertEqual(graph.solid_output(), expected)
def test_nb_vertices_for_const_graph(self): def const_2(x, y): return 2 graph = ZGraph((0,1), (0,1), const_2, 10) self.assertEqual(246, len(graph.solid_output().facets))
def test_solid_output_for_const_graph(self): def const_2(x, y): return 2 graph = ZGraph((0,1), (0,1), const_2, 1) top_left_front = stl.Vector3d(0, 0, 2) top_right_front = stl.Vector3d(1, 0, 2) top_left_back = stl.Vector3d(0, 1, 2) top_right_back = stl.Vector3d(1, 1, 2) bottom_left_front = stl.Vector3d(0, 0, 0) bottom_right_front = stl.Vector3d(1, 0, 0) bottom_left_back = stl.Vector3d(0, 1, 0) bottom_right_back = stl.Vector3d(1, 1, 0) top = ZGraph.triangulate( stl.Vector3d(0, 0, 1), ( top_left_front, top_right_front, top_right_back, top_left_back ) ) bottom = ZGraph.triangulate( stl.Vector3d(0, 0, -1), ( bottom_left_front, bottom_left_back, bottom_right_back, bottom_right_front ) ) front = ZGraph.triangulate( stl.Vector3d(0, -1, 0), ( bottom_left_front, bottom_right_front, top_right_front, top_left_front ) ) right = ZGraph.triangulate( stl.Vector3d(1, 0, 0), ( bottom_right_front, bottom_right_back, top_right_back, top_right_front ) ) back = ZGraph.triangulate( stl.Vector3d(0, 1, 0), ( bottom_left_back, top_left_back, top_right_back, bottom_right_back ) ) left = ZGraph.triangulate( stl.Vector3d(-1, 0, 0), ( bottom_left_front, top_left_front, top_left_back, bottom_left_back ) ) facets = top + bottom + front + right + back + left expected = stl.Solid("ZGraph", facets) self.assertEqual(graph.solid_output(), expected)
from math import sin from z_graph import ZGraph PI = 3.14159 RANGE = (0, PI) def f(x, y): return (4 * sin(x) * sin(y) + 2 * sin(x) + 2 * sin(y)) / 10 if __name__ == "__main__": z = ZGraph(RANGE, RANGE, f, 100) solid = z.solid_output() f_ = open("output.stl", "w") solid.write_ascii(f_)
def test_nb_vertices_for_const_graph(self): def const_2(x, y): return 2 graph = ZGraph((0, 1), (0, 1), const_2, 10) self.assertEqual(246, len(graph.solid_output().facets))