Пример #1
0
    def from_frame_to_frame(cls, frame_from, frame_to):
        """Computes a change of basis transformation between two frames.

        This transformation maps geometry from one Cartesian coordinate system
        defined by "frame_from" to the other Cartesian coordinate system
        defined by "frame_to".

        Args:
            frame_from (:class:`Frame`): a frame defining the original
                Cartesian coordinate system
            frame_to (:class:`Frame`): a frame defining the targeted
                Cartesian coordinate system

        Example:
            >>> from compas.geometry import 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 == f1.transform(T)
            True
        """
        T1 = matrix_from_frame(frame_from)
        T2 = matrix_from_frame(frame_to)

        return cls(multiply_matrices(T2, inverse(T1)))
Пример #2
0
    def from_frame(cls, frame):
        """Computes a transformation from world XY to frame.

        Parameters
        ----------
        frame : :class:`Frame`
            A frame describing the targeted Cartesian coordinate system.

        Returns
        -------
        Transformation
            The ``Transformation`` object.

        Examples
        --------
        >>> from compas.geometry import 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)
        >>> f1 == f2
        True

        Notes
        -----
        It is the same as from_frame_to_frame(Frame.worldXY(), frame).

        """
        return cls(matrix_from_frame(frame))
Пример #3
0
    def from_frame(cls, frame):
        """Computes the rotational transformation from world XY to frame.

        Notes
        -----
        Creating a rotation from a frame means that we omit all translational
        components. If that is unwanted, use ``Transformation.from_frame(frame)``.

        Parameters
        ----------
        frame : :class:`Frame`
            A frame describing the targeted Cartesian coordinate system.

        Examples
        --------
        >>> from compas.geometry import 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)
        >>> f1 == f2
        True
        """
        R = cls()
        matrix = matrix_from_frame(frame)
        matrix[0][3] = 0.0
        matrix[1][3] = 0.0
        matrix[2][3] = 0.0
        R.matrix = matrix
        return R
Пример #4
0
    def represent_vector_in_global_coordinates(self, vector):
        """Represents a vector in local coordinates in the world coordinate system.

        Parameters
        ----------
        vector: :obj:`list` of :obj:`float` or :class:`Vector`
            A vector in local coordinates.

        Returns
        -------
        :class:`Vector`
            A vector in the world coordinate system.

        Examples
        --------
        >>> from compas.geometry import Frame
        >>> f = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
        >>> pw1 = [2, 2, 2]
        >>> pf = f.represent_vector_in_local_coordinates(pw1)
        >>> pw2 = f.represent_vector_in_global_coordinates(pf)
        >>> allclose(pw1, pw2)
        True

        """
        T = matrix_from_frame(self)
        vec = Vector(*vector)
        vec.transform(T)
        return vec
Пример #5
0
    def represent_point_in_global_coordinates(self, point):
        """Represents a point from local coordinates in the world coordinate system.

        Parameters
        ----------
        point : :obj:`list` of :obj:`float` or :class:`Point`
            A point in local coordinates.

        Returns
        -------
        :class:`Point`
            A point in the world coordinate system.

        Examples
        --------
        >>> from compas.geometry import Frame
        >>> f = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
        >>> pw1 = [2, 2, 2]
        >>> pf = f.represent_point_in_local_coordinates(pw1)
        >>> pw2 = f.represent_point_in_global_coordinates(pf)
        >>> allclose(pw1, pw2)
        True

        """
        T = matrix_from_frame(self)
        pt = Point(*point)
        pt.transform(T)
        return pt
Пример #6
0
    def from_factors(cls, factors, frame=None):
        """Construct a scale transformation from scale factors.

        Parameters
        ----------
        factors : list of float
            The scale factors along X, Y, Z.
        frame : :class:`compas.geometry.Frame`, optional
            The anchor frame for the scaling transformation.
            Defaults to ``None``.

        Returns
        -------
        Scale
            A scale transformation.

        Examples
        --------
        >>> point = Point(2, 5, 0)
        >>> frame = Frame(point, (1, 0, 0), (0, 1, 0))
        >>> points = [point, Point(2, 10, 0)]
        >>> S = Scale.from_factors([2.] * 3, frame)
        >>> [p.transformed(S) for p in points]
        [Point(2.000, 5.000, 0.000), Point(2.000, 15.000, 0.000)]
        """
        S = cls()
        if frame:
            Tw = matrix_from_frame(frame)
            Tl = matrix_inverse(Tw)
            Sc = matrix_from_scale_factors(factors)
            S.matrix = multiply_matrices(multiply_matrices(Tw, Sc), Tl)
        else:
            S.matrix = matrix_from_scale_factors(factors)
        return S
Пример #7
0
def test_basis_vectors_from_matrix():
    f = Frame([0, 0, 0], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
    R = matrix_from_frame(f)
    xaxis, yaxis = basis_vectors_from_matrix(R)
    assert np.allclose(
        xaxis, [0.6807833515407016, 0.6807833515407016, 0.2703110366411609])
    assert np.allclose(
        yaxis, [-0.6687681911461376, 0.7282315441900513, -0.14975955581430114])
Пример #8
0
def test_matrix_from_frame():
    f = Frame([1, 1, 1], [0.68, 0.68, 0.27], [-0.67, 0.73, -0.15])
    T = matrix_from_frame(f)
    t = [[0.6807833515407016, -0.6687681911461376, -0.29880283595731283, 1.0],
         [0.6807833515407016, 0.7282315441900513, -0.0788216106888398, 1.0],
         [0.2703110366411609, -0.14975955581430114, 0.9510541619236438, 1.0],
         [0.0, 0.0, 0.0, 1.0]]
    assert np.allclose(T, t)
Пример #9
0
    def from_frame(cls, frame):
        """Computes a change of basis transformation from world XY to frame.

        It is the same as from_frame_to_frame(Frame.worldXY(), frame).

        Args:
            frame (:class:`Frame`): a frame describing the targeted Cartesian
                coordinate system

        Example:
            >>> from compas.geometry import 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)
            >>> f1 == f2
            True
        """
        T = cls()
        T.matrix = matrix_from_frame(frame)
        return T
Пример #10
0
    def from_frame(cls, frame):
        """Computes a change of basis transformation from world XY to frame.

        It is the same as from_frame_to_frame(Frame.worldXY(), frame).

        Parameters
        ----------
        frame : :class:`Frame`
            A frame describing the targeted Cartesian coordinate system.

        Examples
        --------
        >>> from compas.geometry import 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)
        >>> f1 == f2
        True
        """
        R = cls()
        R.matrix = matrix_from_frame(frame)
        R.matrix[0][3], R.matrix[1][3], R.matrix[2][3] = [0., 0., 0.]
        return R