def test_downscaling_valueerror(self): poly = Polygon2D( [Vector(0, 0), Vector(1, 0), Vector(1, 1), Vector(0, 1)]) self.assertRaises(ValueError, lambda: poly.downscale(2))
def test_path(self): poly = Polygon2D( [Vector(0, 0), Vector(10, 0), Vector(10, 10), Vector(0, 10)]) path = cover_polygon2d_with_path(poly, Vector(1, 1), 1, 0.5, 0.0)
def test_point_of_polygon(self): poly = Polygon2D( [Vector(0, 0), Vector(10, 0), Vector(10, 10), Vector(0, 10)]) point = poly.get_point_on_polygon(10) segment, point = point.get_segment_and_vector()
def test_init(self): v = Vector(1, 2, 3) assert v.x == 1 assert v.y == 2 assert v.z == 3 v = Vector((3.1416, 6.2832, 1.41421)) assert v.x == 3.1416 assert v.y == 6.2832 assert v.z == 1.41421
def triangle_from_string(cls, str): assert len(str) >= 50 bytes, str = str[:50], str[50:] coords = struct.unpack('ffffffffffffH', bytes) normal = Vector(coords[:3]) vertex1 = Vector(coords[3:6]) vertex2 = Vector(coords[6:9]) vertex3 = Vector(coords[9:12]) t = Triangle(vertex1, vertex2, vertex3, normal) return (t, str)
def test_to_path(self): poly = Polygon2D( [Vector(0, 0), Vector(10, 0), Vector(10, 10), Vector(0, 10)]) path1 = poly.to_path(0.1, Vector(1, 1)) path2 = Path.from_to(Vector(0, 0), Vector(10, 0), Vector(1, 1), 0.1) intersects = list(path1.get_intersecting_boxes(path2)) self.assertGreater(len(intersects), 1)
def from_input_file(self, inf): triangleList = [] R = inf.read() words = re.split('\W+', R) if 'facet' in words and 'normal' in words and 'vertex' in words: # this is an ASCII STL file del words lines = re.split('\n', R) self.preamble = lines.pop(0) while lines: line = lines.pop(0).strip() words = re.split('[ \t]+', line) if words[0] == 'endsolid': break assert words[0] == 'facet' and words[1] == 'normal', words normal = Vector(string.atof(words[2]), string.atof(words[3]), string.atof(words[4])) vertices = [] line = lines.pop(0).strip() assert line == 'outer loop', line for i in range(3): line = lines.pop(0).strip() words = re.split('[ \t]+', line) assert words[0] == 'vertex', words vertex = Vector(string.atof(words[1]), string.atof(words[2]), string.atof(words[3])) vertices.append(vertex) line = lines.pop(0).strip() assert line == 'endloop', line line = lines.pop(0).strip() assert line == 'endfacet', line tri = apply(Triangle, vertices + [normal]) triangleList.append(tri) else: # this is a binary STL file self.preamble = ''.join(filter(lambda ch: ch != '\0', list(R[:80]))) R = R[80:] numTriangles, R = struct.unpack('I', R[:4])[0], R[4:] for i in range(numTriangles): tri, R = self.triangle_from_string(R) triangleList.append(tri) return triangleList
def test_copy(self): bbox1 = BBox(Vector(1, 2, 3), Vector(4, 5, 6)) bbox2 = bbox1.copy() bbox1._max = Vector(7, 8, 9) assert bbox2._min == Vector(1, 2, 3) assert bbox2._max == Vector(4, 5, 6) assert bbox1._min == Vector(1, 2, 3) assert bbox1._max == Vector(7, 8, 9)
def test_iter_from(self): poly = Polygon2D( [Vector(0, 0), Vector(10, 0), Vector(10, 10), Vector(0, 10)]) self.assertEqual( list(poly.iter_from(12)), [Vector(10, 0), Vector(10, 10), Vector(0, 10), Vector(0, 0)])
def test_path(self): path1 = Path.from_to(Vector(0, 0), Vector(10, 0), Vector(1, 1), 0.1) path2 = Path.from_to(Vector(10, 10), Vector(10, 0), Vector(1, 1), 0.1) intersecting_boxes = list(path1.get_intersecting_boxes(path2)) self.assertGreater(len(intersecting_boxes), 1) self.assertTrue(path1.does_collide(path2))
def te_st_get_point_list(self): A = Vector(0, 0, 0) B = Vector(1, 0, 0) C = Vector(0, 1, 0) D = Vector(0, 0, 1) stl = Stl(Triangle(A, B, D), Triangle(A, C, B), Triangle(A, D, C), Triangle(B, C, D)) tz = stl.triangles (I, _), (J, _) = stl.get_point_list(0.25, 0.25, tz) assert approximate(I.x, 0) assert approximate(I.y, 0.25) assert approximate(I.z, 0.25) assert approximate(J.x, 0.5) assert approximate(J.y, 0.25) assert approximate(J.z, 0.25) assert stl.get_point_list(2, 2) == [] assert stl.get_point_list(1, 1) == [] (I, _), = stl.get_point_list(0.5, 0.5) assert approximate(I.x, 0) assert approximate(I.y, 0.5) assert approximate(I.z, 0.5)
def test_nonintersecting(self): pc = [ Coordinates(50.022313, 21.990220), Coordinates(50.022935, 21.990492), Coordinates(50.021613, 21.991670), Coordinates(50.021544, 21.991540), Coordinates(50.022149, 21.991335), Coordinates(50.021385, 21.992079) ] xy_pc = XYPointCollection(pc) poly1 = Polygon2D([xypoint.to_vector() for xypoint in xy_pc[:3]]) poly2 = Polygon2D([xypoint.to_vector() for xypoint in xy_pc[3:]]) path1 = cover_polygon2d_with_path(poly1, Vector(5, 5, 5), 10, 0.3, 0) path1.set_z(50) path2 = cover_polygon2d_with_path(poly2, Vector(5, 5, 5), 10, 0.3, 0) path2.set_z(50) path1, path2 = make_nonintersecting([ MakeNonintersectingPaths(0, 100, path1), MakeNonintersectingPaths(0, 100, path2) ]) self.assertGreater(path2.length, 400) self.assertTrue(are_mutually_nonintersecting([path1, path2])) path1.simplify() path2.simplify()
def test_collision(self): box1 = Box(Vector(0, 0, 0), Vector(10, 10, 10)) box2 = Box(Vector(5, 5, 5), Vector(15, 15, 15)) box3 = Box(Vector(20, 20, 20), Vector(25, 25, 25)) self.assertTrue(box1.collides(box2)) self.assertTrue(box2.collides(box1)) self.assertFalse(box1.collides(box3)) self.assertFalse(box2.collides(box3)) self.assertFalse(box3.collides(box1)) self.assertFalse(box3.collides(box2))
def test_make_nonintersecting(self): p1 = Path.from_to(Vector(0, 0, 2), Vector(10, 10, 2), Vector(1, 1, 1), 0.1) p2 = Path.from_to(Vector(5, 5, 2), Vector(15, 15, 2), Vector(1, 1, 1), 0.1) self.assertTrue(p1.does_collide(p2)) m1 = MakeNonintersectingPaths(-5, 10, p1) m2 = MakeNonintersectingPaths(-5, 10, p2) p1, p2, = make_nonintersecting([m1, m2]) self.assertFalse(p1.does_collide(p2))
def test_get_unit_vector_towards_polygon(self): poly = Polygon2D( [Vector(0, 0), Vector(10, 0), Vector(10, 10), Vector(0, 10)]) self.assertEqual(poly.total_perimeter_length, 40) self.assertEqual(poly.len_segments, [10, 10, 10, 10]) point = poly.get_point_on_polygon(0) self.assertEqual(point.get_unit_vector_towards_polygon(), Vector(1, 1).unitize()) point.advance(5) self.assertEqual(point.to_vector(), Vector(5, 0)) self.assertEqual(point.get_unit_vector_towards_polygon(), Vector(0, 1)) self.assertEqual(point.get_unit_vector_away_polygon(), Vector(0, -1)) point.advance(-10) self.assertEqual(point.to_vector(), Vector(0, 5)) self.assertEqual(point.get_unit_vector_towards_polygon(), Vector(1, 0)) point = poly.get_point_on_polygon(10) self.assertTrue(point.is_on_vertex()) self.assertEqual(point.get_unit_vector_towards_polygon(), Vector(-1, 1).unitize())
def test_downscaling(self): poly = Polygon2D( [Vector(0, 0), Vector(10, 0), Vector(10, 10), Vector(0, 10)]) poly = poly.downscale(1) self.assertEqual(poly.points[0], Vector(1, 1).unitize()) self.assertEqual(poly.points[1], Vector(10, 0) + Vector(-1, 1).unitize()) self.assertEqual(poly.points[2], Vector(10, 10) + Vector(-1, -1).unitize()) self.assertEqual(poly.points[3], Vector(0, 10) + Vector(1, -1).unitize())
def test_eq(self): assert Vector(1.0, 2.0, 3.0) == \ Vector(1.00000000001, 1.999999999999, 3.0) assert Vector(1.0, 2.0, 3.0) != Vector(1.00001, 1.9999, 3.0)
def test_point_contains(self): poly = Polygon2D([Vector(0, 0, 0), Vector(1, 0, 0), Vector(1, 1, 0)]) self.assertIn(Vector(0.5, 0.5, 0), poly) self.assertIn(Vector(0, 0, 0), poly) self.assertNotIn(Vector(2, 0.5, 0), poly) self.assertNotIn(Vector(2, 2, 0), poly)
def test_unit_length(self): p = (1. / 3)**.5 v = Vector(1, 1, 1).unit_length() assert v == Vector(p, p, p)
def test_dot(self): v = Vector(1, 2, 3) assert v.dot(Vector(4, 5, 6)) == 32
def test_size(self): assert BBox(Vector(0, -1, 0), Vector(1, 1, 3)).size() == Vector(1, 2, 3)
def test_abs(self): v = Vector(1, 2, 3) expected = (1 + 4 + 9)**.5 assert abs(v) == expected
def test_contains(self): bbox = BBox(Vector(0, 0, 0), Vector(1, 1, 1)) assert Vector(0.5, 0.5, 0.5) in bbox assert Vector(0.5, 0.5, 1.1) not in bbox
def test_diff(self): assert Vector(4, 6, 8).diff(Vector(1, 2, 3)) == Vector(3, 4, 5)
def test_add(self): v = Vector(4, 6, 8) assert v.add(Vector(1, 2, 3)) == Vector(5, 8, 11)
def test_cross(self): v = Vector(1, 2, 3) assert v.cross(Vector(4, 5, 6)) == Vector(-3, 6, -3)
def test_add(self): v = Vector(4, 6, 8) assert v.add(Vector(1, 2, 3)) == Vector(5, 8, 11)
def test_cross(self): v = Vector(1, 2, 3) assert v.cross(Vector(4, 5, 6)) == Vector(-3, 6, -3)
def test_set_size(self): bbox = BBox(Vector(0, 0, 0), Vector(1, 1, 1)) bbox.set_size(Vector(2, 3, 4)) assert bbox._min == Vector(-0.5, -1.0, -1.5) assert bbox._max == Vector(1.5, 2.0, 2.5)
def test_getXiterator(self): bb = BBox(Vector(0.0, 0.0, 1.0), Vector(1.0, 2.0, 4.0)) assert [x for x in bb.getXiterator(5)] == \ [0.0, 0.25, 0.5, 0.75, 1.0] assert [x for x in bb.getXiterator(5)] == \ [0.0, 0.25, 0.5, 0.75, 1.0]
def test_point_closest(self): poly = Polygon2D([Vector(0, 0, 0), Vector(1, 0, 0), Vector(1, 1, 0)]) closest = poly.get_closest_to(Vector(0.5, -0.5, 0), 10) self.assertLess(abs(closest - 0.5), 0.01) closest = poly.get_closest_to(Vector(2, 2, 0), 10) self.assertLess(abs(closest - 2), 0.01)
def test_init(self): bbox = BBox(Vector(0, 0, 0), Vector(1, 1, 1)) assert bbox._min == Vector(0, 0, 0) assert bbox._max == Vector(1, 1, 1)
def test_scale(self): assert Vector(1, 2, 3).scale(2) == Vector(2, 4, 6)