Example #1
0
    def test_multiplication_with_object_and_transform(self):
        # Multiplication is used for both intersection (Object * Object) and transform (Transform * Object)
        a = Sphere(2)
        b = Cuboid(3, 3, 3)
        s = ScaleAxes(1, 2, -1)
        t = Translate([0, 0, 0])

        self.assertEqual( t * (a  * b), Transformed(t, Intersection([a, b])))
        self.assertEqual((t *  a) * b , Intersection([Transformed(t, a), b]))
        self.assertEqual((s *  t) * a , Transformed(Chained([s, t]), a))
        self.assertEqual( s * (t  * a), Transformed(Chained([s, t]), a))
Example #2
0
    def test_repr(self):
        t = Translate([10, 20, 30])
        cube = Cuboid(1, 1, 1)

        self.assertRepr(
            Transformed(t, cube),
            "Transformed(Translate(Vector(10, 20, 30)), Cuboid(1, 1, 1))")
Example #3
0
    def __mul__(self, other):
        from cadlib.transform.chained import Chained
        from cadlib.object import Object, Transformed

        if isinstance(other, Vector):
            # Transform * Vector - calculate transformed vector
            if other.dimensions != 3:
                raise ValueError(
                    f"Dimension mismatch: Transform ({3}) x Vector ({other.dimensions})"
                )
            return self.to_matrix().homogeneous_mul(other)
        elif isinstance(other, Chained):
            # Transform * Chained - defer to Chained.__rmul__
            return NotImplemented
        elif isinstance(other, Transform):
            # Transform * Transform - create Chained
            return Chained([self, other])
        elif isinstance(other, Transformed):
            # Transform * Transformed - defer to Transformed.__rmul__
            return NotImplemented
        elif isinstance(other, Object):
            # Transform * Object - create Transformed
            return Transformed(self, other)
        else:
            # Transform * other - unknown
            return NotImplemented
Example #4
0
    def test_construction(self):
        # Transformed objects can also be created by multiplying a transform with an object. This is
        # tested in test_object.py, as it concerns the operators defined by Object.

        # Transformed object
        t = Translate([10, 20, 30])
        cube = Cuboid(1, 1, 1)

        with self.assertNothingRaised():
            Transformed(t, cube)

        with self.assertRaises(TypeError):
            Transformed(t, t)
        with self.assertRaises(TypeError):
            Transformed(cube, cube)
        with self.assertRaises(TypeError):
            Transformed(t, None)
        with self.assertRaises(TypeError):
            Transformed(None, cube)
Example #5
0
    def test_operators_with_transforms(self):
        a = Sphere(2)
        t = Translate([0, 0, 0])

        # The only operation we can do with a Transform is Transform*Object, which results in a Transformed Object.
        with self.assertRaises(TypeError): a + t
        with self.assertRaises(TypeError): a - t
        with self.assertRaises(TypeError): a * t
        with self.assertRaises(TypeError): t + a
        with self.assertRaises(TypeError): t - a
        self.assertEqual(t * a, Transformed(t, a))
Example #6
0
    def test_to_scad(self):
        r = RotateXyz(60, 30, 15)
        s = ScaleAxes(1, 2, -1)
        cube = Cuboid(11, 11, 11)

        # Simple transform
        self.assertEqual(
            Transformed(r, cube).to_scad(),
            ScadObject("rotate", [[60, 30, 15]], None, [
                ScadObject("cube", [[11, 11, 11]], None, None),
            ]))

        # Chained transform
        self.assertEqual(
            Transformed(Chained([r, s]), cube).to_scad(),
            ScadObject("rotate", [[60, 30, 15]], None, [
                ScadObject("scale", [[1, 2, -1]], None, [
                    ScadObject("cube", [[11, 11, 11]], None, None),
                ])
            ]))
Example #7
0
    def test_equality(self):
        scale1a = ScaleUniform(1)
        scale1b = ScaleUniform(1)
        scale2 = ScaleUniform(2)

        cube1a = Cuboid(1, 1, 1)
        cube1b = Cuboid(1, 1, 1)
        cube2 = Cuboid(2, 2, 2)

        self.assertEqual(Transformed(scale1a, cube1a),
                         Transformed(scale1a, cube1b))  # Equal objects
        self.assertEqual(Transformed(scale1a, cube1a),
                         Transformed(scale1b, cube1a))  # Equal transform
        self.assertNotEqual(Transformed(scale1a, cube1a),
                            cube1a)  # Transformed / original
        self.assertNotEqual(Transformed(scale1a, cube1a),
                            Transformed(scale2, cube1a))  # Different transform
        self.assertNotEqual(Transformed(scale1a, cube1a),
                            Transformed(scale1a, cube2))  # Different objects
Example #8
0
    def test_transform_shortcuts(self):
        a = Cuboid(11, 11, 11)

        self.assertEqual(a.up     (1), Transformed(Translate([ 0,  0,  1]), a))
        self.assertEqual(a.down   (2), Transformed(Translate([ 0,  0, -2]), a))
        self.assertEqual(a.left   (3), Transformed(Translate([-3,  0,  0]), a))
        self.assertEqual(a.right  (4), Transformed(Translate([ 4,  0,  0]), a))
        self.assertEqual(a.forward(5), Transformed(Translate([ 0,  5,  0]), a))
        self.assertEqual(a.back   (6), Transformed(Translate([ 0, -6,  0]), a))

        self.assertEqual(a.yaw_left  (1), Transformed(RotateYpr( 1,  0,  0), a))
        self.assertEqual(a.yaw_right (2), Transformed(RotateYpr(-2,  0,  0), a))
        self.assertEqual(a.pitch_up  (3), Transformed(RotateYpr( 0,  3,  0), a))
        self.assertEqual(a.pitch_down(4), Transformed(RotateYpr( 0, -4,  0), a))
        self.assertEqual(a.roll_right(5), Transformed(RotateYpr( 0,  0,  5), a))
        self.assertEqual(a.roll_left (6), Transformed(RotateYpr( 0,  0, -6), a))
Example #9
0
    def test_str(self):
        t = Translate([10, 20, 30])
        cube = Cuboid(1, 1, 1)

        self.assertStr(Transformed(t, cube), "Transformed object")