def test_partial_chamfer(self): cube = Box(Vector(0, 0, 0), Vector(1, 1, 1)) edges = [ LineSegment(Point(0, 0, 0), Point(1, 0, 0)), LineSegment(Point(1, 0, 0), Point(1, 1, 0)) ] chamfer = Chamfer(cube, edges, 0.1) self.assertValidPoints(cube - chamfer)
def test_view_recursion(self): a = solid.Box(Point(0, 0, 0), Vector(1, 1, 1)) b = solid.Box(Point(0, 0, 2), Vector(1, 1, 1)) collection = solid.Collection([a.view(color='red'), b]) self.assertEqual( collection.view(opacity=0.5).nodes[0].view_data, { 'color': 'red', 'opacity': 0.5 })
def test_cut(self): cube = Box(Vector(0, 0, 0), Vector(1, 1, 1)) edge = LineSegment(Point(0, 0, 0), Point(1, 0, 0)) chamfer = EdgeChamfer(cube.polygons, edge, 0.1) polygons = [*chamfer.insets(), chamfer.start_cap(), chamfer.end_cap()] normals = [p.plane.normal for p in cube.polygons] for p in polygons: normal = p.plane.normal self.assertIn(normal, normals)
def test_addition(self): a = solid.Box(Point(0, 0, 0), Vector(1, 1, 1)).view(color='red') b = solid.Box(Point(0, 0, 2), Vector(1, 1, 1)).view(color='blue') collection = solid.Collection([a, b]) + Vector.basis.x self.assertEqual([n.view_data for n in collection.nodes], [{ 'color': 'red' }, { 'color': 'blue' }])
def test_subtraction(self): a = solid.Box(Point(0, 0, 0), Vector(1, 1, 1)) b = solid.Box(Point(0, 0, 2), Vector(1, 1, 1)) collection = solid.Collection([a, b]) cut = solid.Box(Point(0.25, 0.25, 0.25), Vector(0.5, 0.5, 2)) collection = collection - cut inset = Vector(0.05, 0.05, 0.05) inside = solid.Box(cut.origin + inset, cut.size() - (inset * 2)) for n in collection.nodes: self.assertTrue(len((n * inside).polygons) == 0)
def test_polygon_inset_cut_a(self): inwards = Vector(0.00, 0.10, 0.00) points = [ Point(0, 0, 0), Point(0, 1, 0), Point(1, 1, 0), Point(1, 0, 0), ] normal = Vector(0, 0, -1) edge = LineSegment(points[-1], points[0]) polygon = Polygon(points) edge = polygon_inset(polygon, edge, inwards) self.assertEqual(edge.p1.xyz, (0, 0.1, 0)) self.assertEqual(edge.p2.xyz, (1, 0.1, 0))
def test_polygon_inset(self): inwards = Vector(0, 0.5, 0) points = [ Point(1, 3, 0), Point(2, 2, 0), Point(3, 2, 0), Point(4, 3, 0), ] normal = Vector(0, 0, 1) edge = LineSegment(points[1], points[2]) polygon = Polygon(points) edge = polygon_inset(polygon, edge, inwards) self.assertEqual(edge.p1.xyz, (3.5, 2.5, 0)) self.assertEqual(edge.p2.xyz, (1.5, 2.5, 0))
def test_inner_methods(self): original = {'color': 'red'} a = solid.Box(Point(0, 0, 0), Vector(1, 1, 1)).view(**original) b = solid.Box(Point(0.25, 0.25, 0.25), Vector(0.5, 0.5, 2)) self.assertEqual((a + b).view_data, original) self.assertEqual((a * b).view_data, original) self.assertEqual((a / 2).view_data, original) self.assertEqual((a - b).view_data, original) self.assertEqual(a.scale(Vector(2, 2, 2)).view_data, original) self.assertEqual(a.translate(Vector(1, 2, 3)).view_data, original) self.assertEqual(a.rotate(Vector.basis.z, tau / 3).view_data, original) self.assertEqual( a.rotate_at(Point(1, 1, 1), Vector.basis.z, tau / 3).view_data, original)
def test_inner_methods(self): a = solid.Box(Point(0, 0, 0), Vector(1, 1, 1)).view(color='red') b = solid.Box(Point(0, 0, 2), Vector(1, 1, 1)).view(color='blue') c = solid.Collection([a, b]) + Vector.basis.x original = [{'color': 'red'}, {'color': 'blue'}] def _vd(c): return [n.view_data for n in c.nodes] self.assertEqual(_vd(c.scale(Vector(2, 2, 2))), original) self.assertEqual(_vd(c.translate(Vector(1, 2, 3))), original) self.assertEqual(_vd(c.rotate(Vector.basis.z, tau / 3)), original) self.assertEqual( _vd(c.rotate_at(Point(1, 1, 1), Vector.basis.z, tau / 3)), original)
def test_multiplication(self): a = solid.Box(Point(0, 0, 0), Vector(1, 1, 1)).view(color='red') b = solid.Box(Point(0, 0, 2), Vector(1, 1, 1)) collection = solid.Collection([a, b]) common = solid.Box(Point(0.25, 0.25, 0.25), Vector(0.5, 0.5, 2)) collection = collection * common self.assertEqual(len(collection.nodes), 2) self.assertEqual(collection.nodes[0].view_data, {'color': 'red'}) collection = solid.Collection([a, b]) common = solid.Box(Point(0.25, 0.25, 0.25), Vector(0.5, 0.5, 0.5)) self.assertTrue(len(collection.nodes), 1) shift_units = (collection * u.cm).m_as(u.mm) self.assertEqual(shift_units.envelope().size(), Vector(10, 10, 30))
def test_subtraction(self): a = solid.Box(Vector(0, 0, 0), Vector(3, 3, 1)) b = solid.Box(Vector(1, 1, 0.5), Vector(1, 1, 3)) combined = a - b.translate(Vector(0, 0, -0.25)) self.assertTrue( len(combined.polygons) > len(a.polygons) + len(b.polygons)) self.assertEqual((a - Vector(1, 2, 3)).envelope().origin, Point(-1, -2, -3))
def test_basic_case(self): a = Box(Point(0, 0, 0), Vector(1, 1, 1)) b = Box(Point(0, 0, 2), Vector(1, 1, 1)) cut = Box(Point(0.25, 0.25, 0.5), Vector(0.5, 0.5, 2)) solid = (a + b + cut) r = Rectangle(Point(0, 0), Vector(5, 5)) sliced = Sliced(solid, Basis.xy, r, 0.25, 2.75, 1.5) self.assertEqual(sliced.slices, [ (0.25, [Polygon([Point(0.0, 0.0, 0.25), Point(0.0, 1.0, 0.25), Point(1.0, 1.0, 0.25), Point(1.0, 0.0, 0.25)])]), (1.75, [Polygon([Point(0.75, 0.25, 1.75), Point(0.25, 0.25, 1.75), Point(0.25, 0.75, 1.75), Point(0.75, 0.75, 1.75)])]), (2.75, [Polygon([Point(0.0, 0.0, 2.75), Point(0.0, 1.0, 2.75), Point(1.0, 1.0, 2.75), Point(1.0, 0.0, 2.75)])]) ])
def test_recursion(self): a = solid.Box(Point(0, 0, 0), Vector(1, 1, 1)) view = a.view(color='red').view(opacity=0.5).view(color='blue') self.assertEqual(view.view_data, {'color': 'blue', 'opacity': 0.5})