def transform(self, transformation): """Transform the frame. Parameters ---------- transformation : :class:`Transformation` The transformation used to transform the Frame. Examples -------- >>> from compas.geometry import Frame >>> from compas.geometry import Transformation >>> f1 = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15]) >>> T = Transformation.from_frame(f1) >>> f2 = Frame.worldXY() >>> f2.transform(T) >>> f1 == f2 True """ T = transformation * Transformation.from_frame(self) point = T.translation xaxis, yaxis = T.basis_vectors self.point = point self.xaxis = xaxis self.yaxis = yaxis
def to_world_coords(self, object_in_lcs): """Returns the object's coordinates in the global coordinate frame. Parameters ---------- object_in_lcs : :class:`Point` or :class:`Vector` or :class:`Frame` or list of float An object in local coordinate system of the frame. Returns ------- :class:`Point` or :class:`Vector` or :class:`Frame` The object in the world coordinate frame. Notes ----- If you pass a list of float, it is assumed to represent a point. Examples -------- >>> from compas.geometry import Frame >>> frame = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15]) >>> pl = Point(1.632, -0.090, 0.573) # point in frame >>> pw = frame.to_world_coords(pl) # point in wcf >>> frame.to_local_coords(pw) Point(1.632, -0.090, 0.573) """ T = Transformation.change_basis(self, Frame.worldXY()) if isinstance(object_in_lcs, list): return Point(*object_in_lcs).transformed(T) else: return object_in_lcs.transformed(T)
def local_to_local_coords(frame1, frame2, object_in_frame1): """Returns the object's coordinates in frame1 in the local coordinates of frame2. Parameters ---------- frame1 : :class:`Frame` A frame representing one local coordinate system. frame2 : :class:`Frame` A frame representing another local coordinate system. object_in_frame1 : :class:`Point` or :class:`Vector` or :class:`Frame` or list of float An object in the coordinate frame1. If you pass a list of float, it is assumed to represent a point. Returns ------- :class:`Point` or :class:`Vector` or :class:`Frame` The object in the local coordinate system of frame2. Examples -------- >>> from compas.geometry import Point, Frame >>> frame1 = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15]) >>> frame2 = Frame([2, 1, 3], [1., 0., 0.], [0., 1., 0.]) >>> p1 = Point(2, 2, 2) # point in frame1 >>> p2 = Frame.local_to_local_coords(frame1, frame2, p1) # point in frame2 >>> Frame.local_to_local_coords(frame2, frame1, p2) Point(2.000, 2.000, 2.000) """ T = Transformation.change_basis(frame1, frame2) if isinstance(object_in_frame1, list): return Point(*object_in_frame1).transformed(T) else: return object_in_frame1.transformed(T)
def test_reflection(): point = [1, 1, 1] normal = [0, 0, 1] R1 = Reflection(point, normal) R2 = Transformation.from_matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -1, 2], [0, 0, 0, 1]]) assert R1 == R2
def test_reflection_from_frame(): point = [1, 1, 1] x = [1, 0, 0] y = [0, 1, 0] f = Frame(point, x, y) R1 = Reflection.from_frame(f) R2 = Transformation.from_matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -1, 2], [0, 0, 0, 1]]) assert R1 == R2
def test_from_frame(): f1 = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15]) T = Transformation.from_frame(f1) f2 = Frame.from_transformation(T) assert np.allclose(f1, f2)
def test_transformation(): assert Transformation().matrix == [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
def test_inverse(): f = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15]) T = Transformation.from_frame(f) assert Transformation() == T * T.inverse()
def test_rotation_property(): T = Transformation() r = T.rotation assert type(r) == Rotation assert r.matrix == [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
def test_from_frame(): f1 = Frame([2, 2, 2], [0.12, 0.58, 0.81], [-0.80, 0.53, -0.26]) f2 = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15]) T = Transformation.from_frame_to_frame(f1, f2) f1.transform(T) assert f1 == f2
def test_from_list(): numbers = [1, 0, 0, 3, 0, 1, 0, 4, 0, 0, 1, 5, 0, 0, 0, 1] T = Transformation.from_list(numbers) assert T.matrix == [[1.0, 0.0, 0.0, 3.0], [0.0, 1.0, 0.0, 4.0], [0.0, 0.0, 1.0, 5.0], [0.0, 0.0, 0.0, 1.0]]
def test_from_matrix(): t = Transformation().matrix assert Transformation.from_matrix(t).matrix == t
def test_copy(): T = Transformation() assert T.matrix == T.copy().matrix
def test_list(): T = Transformation() assert T.list == [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ]