Rigid spatial transform between coordinate systems in 3D space. Attributes: rotation (scipy.spatial.transform.Rotation) translation (np.ndarray)
| 17 | |
| 18 | |
| 19 | class Transform(object): |
| 20 | """Rigid spatial transform between coordinate systems in 3D space. |
| 21 | |
| 22 | Attributes: |
| 23 | rotation (scipy.spatial.transform.Rotation) |
| 24 | translation (np.ndarray) |
| 25 | """ |
| 26 | |
| 27 | def __init__(self, rotation, translation): |
| 28 | assert isinstance(rotation, scipy.spatial.transform.Rotation) |
| 29 | assert isinstance(translation, (np.ndarray, list)) |
| 30 | |
| 31 | self.rotation = rotation |
| 32 | self.translation = np.asarray(translation, np.double) |
| 33 | |
| 34 | def as_matrix(self): |
| 35 | """Represent as a 4x4 matrix.""" |
| 36 | return np.vstack( |
| 37 | (np.c_[self.rotation.as_matrix(), self.translation], [0.0, 0.0, 0.0, 1.0]) |
| 38 | ) |
| 39 | |
| 40 | def to_dict(self): |
| 41 | """Serialize Transform object into a dictionary.""" |
| 42 | return { |
| 43 | "rotation": self.rotation.as_quat().tolist(), |
| 44 | "translation": self.translation.tolist(), |
| 45 | } |
| 46 | |
| 47 | def to_list(self): |
| 48 | return np.r_[self.rotation.as_quat(), self.translation] |
| 49 | |
| 50 | def __mul__(self, other): |
| 51 | """Compose this transform with another.""" |
| 52 | rotation = self.rotation * other.rotation |
| 53 | translation = self.rotation.apply(other.translation) + self.translation |
| 54 | return self.__class__(rotation, translation) |
| 55 | |
| 56 | def transform_point(self, point): |
| 57 | return self.rotation.apply(point) + self.translation |
| 58 | |
| 59 | def transform_vector(self, vector): |
| 60 | return self.rotation.apply(vector) |
| 61 | |
| 62 | def inverse(self): |
| 63 | """Compute the inverse of this transform.""" |
| 64 | rotation = self.rotation.inv() |
| 65 | translation = -rotation.apply(self.translation) |
| 66 | return self.__class__(rotation, translation) |
| 67 | |
| 68 | @classmethod |
| 69 | def from_matrix(cls, m): |
| 70 | """Initialize from a 4x4 matrix.""" |
| 71 | rotation = Rotation.from_matrix(m[:3, :3]) |
| 72 | translation = m[:3, 3] |
| 73 | return cls(rotation, translation) |
| 74 | |
| 75 | @classmethod |
| 76 | def from_dict(cls, dictionary): |
no outgoing calls
no test coverage detected