Skip to content

Geometry

Geometry data models.

Pose3D(rotation=Rotation3D_Matrix(), translation=Translation3D()) dataclass

3D pose: 3D rotation + 3D translation, a.k.a. \(SE(3)\).

Attributes:

Name Type Description
rotation Rotation3D

3D rotation.

translation Translation3D

3D translation.

chain(pose)

Chain two poses.

Parameters:

Name Type Description Default
pose Pose3D

The pose to chain.

required

Returns:

Type Description
Pose3D

The chained pose.

Source code in src/compas_camcal/models/geometry.py
300
301
302
303
304
305
306
307
308
309
310
def chain(self, pose: "Pose3D") -> "Pose3D":
    """
    Chain two poses.

    Args:
        pose: The pose to chain.

    Returns:
        The chained pose.
    """
    return Pose3D.from_matrix(self.to_matrix() @ pose.to_matrix())

from_matrix(matrix) classmethod

Initialize from a 3D homogeneous transformation matrix.

Parameters:

Name Type Description Default
matrix ndarray

3D homogeneous transformation matrix as a 4x4 numpy array.

required

Returns:

Type Description
Pose3D

3D pose.

Source code in src/compas_camcal/models/geometry.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
@classmethod
def from_matrix(cls, matrix: np.ndarray) -> "Pose3D":
    """
    Initialize from a 3D homogeneous transformation matrix.

    Args:
        matrix: 3D homogeneous transformation matrix as a 4x4 numpy array.

    Returns:
        3D pose.
    """
    translation_vector, rotation_matrix = decompose_homogeneous_transform(matrix)
    return cls(
        rotation=Rotation3D_Matrix(rotation_matrix),
        translation=Translation3D.from_vector(translation_vector),
    )

invert()

Invert the pose.

Returns:

Type Description
Pose3D

The inverted pose.

Source code in src/compas_camcal/models/geometry.py
291
292
293
294
295
296
297
298
def invert(self) -> "Pose3D":
    """
    Invert the pose.

    Returns:
        The inverted pose.
    """
    return Pose3D.from_matrix(np.linalg.inv(self.to_matrix()))

to_matrix()

Make a homogeneous transformation matrix.

Returns:

Type Description
ndarray

3D homogeneous transformation matrix as a 4x4 numpy array.

Source code in src/compas_camcal/models/geometry.py
263
264
265
266
267
268
269
270
271
272
def to_matrix(self) -> np.ndarray:
    """
    Make a homogeneous transformation matrix.

    Returns:
        3D homogeneous transformation matrix as a 4x4 numpy array.
    """
    return compose_homogeneous_transform(
        self.translation.to_vector(), self.rotation.to_matrix()
    )

transform(points)

Transform a set of points.

Parameters:

Name Type Description Default
points ndarray

Points to transform as a 3xN or 4xN numpy array.

required

Returns:

Type Description
ndarray

Transformed points as a 3xN or 4xN numpy array.

Source code in src/compas_camcal/models/geometry.py
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
def transform(self, points: np.ndarray) -> np.ndarray:
    """
    Transform a set of points.

    Args:
        points: Points to transform as a 3xN or 4xN numpy array.

    Returns:
        Transformed points as a 3xN or 4xN numpy array.
    """
    homogeneous = points.shape[0] == 4
    n = points.shape[1]

    if not homogeneous:
        points = np.vstack((points, np.ones((1, n))))

    points = self.to_matrix() @ points

    if not homogeneous:
        points /= points[3, :]  # Normalize homogeneous coordinates
        points = points[:3, :]  # Remove homogeneous coordinate

    return points

Rotation3D() dataclass

Bases: ABC

Abstract 3D rotation, a.k.a. \(SO(3)\). Subclasses must implement the to_matrix and from_matrix methods.

as_(rotation_class)

Convert to a different rotation class.

Parameters:

Name Type Description Default
rotation_class type

The rotation class to convert to.

required

Returns:

Type Description
Rotation3D

The converted rotation.

Source code in src/compas_camcal/models/geometry.py
101
102
103
104
105
106
107
108
109
110
111
def as_(self, rotation_class: type) -> "Rotation3D":
    """
    Convert to a different rotation class.

    Args:
        rotation_class: The rotation class to convert to.

    Returns:
        The converted rotation.
    """
    return rotation_class.from_matrix(self.to_matrix())

from_matrix(matrix) abstractmethod classmethod

Initialize from a rotation matrix.

Parameters:

Name Type Description Default
matrix ndarray

3D rotation matrix as a 3x3 numpy array.

required

Returns:

Type Description
Rotation3D

3D rotation.

Source code in src/compas_camcal/models/geometry.py
87
88
89
90
91
92
93
94
95
96
97
98
99
@classmethod
@abstractmethod
def from_matrix(cls, matrix: np.ndarray) -> "Rotation3D":
    """
    Initialize from a rotation matrix.

    Args:
        matrix: 3D rotation matrix as a 3x3 numpy array.

    Returns:
        3D rotation.
    """
    pass

to_matrix() abstractmethod

Make a rotation matrix.

Returns:

Type Description
ndarray

3D rotation matrix as a 3x3 numpy array.

Source code in src/compas_camcal/models/geometry.py
77
78
79
80
81
82
83
84
85
@abstractmethod
def to_matrix(self) -> np.ndarray:
    """
    Make a rotation matrix.

    Returns:
        3D rotation matrix as a 3x3 numpy array.
    """
    pass

Rotation3D_Euler(angle_1=0.0, angle_2=0.0, angle_3=0.0) dataclass

Bases: Rotation3D

3D rotation using Euler angles.

Attributes:

Name Type Description
angle_1 float

Angle \(\theta_1\) in radians.

angle_2 float

Angle \(\theta_2\) in radians.

angle_3 float

Angle \(\theta_3\) in radians.

from_matrix(matrix) classmethod

Initialize from a rotation matrix.

Parameters:

Name Type Description Default
matrix ndarray

3D rotation matrix as a 3x3 numpy array.

required

Returns:

Type Description
Rotation3D_Euler

3D rotation.

Source code in src/compas_camcal/models/geometry.py
165
166
167
168
169
170
171
172
173
174
175
176
@classmethod
def from_matrix(cls, matrix: np.ndarray) -> "Rotation3D_Euler":
    """
    Initialize from a rotation matrix.

    Args:
        matrix: 3D rotation matrix as a 3x3 numpy array.

    Returns:
        3D rotation.
    """
    return cls(*rotation_matrix_to_euler(matrix))

to_matrix()

Make a rotation matrix.

Returns:

Type Description
ndarray

3D rotation matrix as a 3x3 numpy array.

Source code in src/compas_camcal/models/geometry.py
156
157
158
159
160
161
162
163
def to_matrix(self) -> np.ndarray:
    """
    Make a rotation matrix.

    Returns:
        3D rotation matrix as a 3x3 numpy array.
    """
    return euler_to_rotation_matrix(self.angle_1, self.angle_2, self.angle_3)

Rotation3D_Matrix(matrix=(lambda: np.eye(3))()) dataclass

Bases: Rotation3D

3D rotation using a rotation matrix.

Attributes:

Name Type Description
matrix ndarray

3D rotation matrix as a 3x3 numpy array.

Rotation3D_Quaternion(r=1.0, i=0.0, j=0.0, k=0.0) dataclass

Bases: Rotation3D

3D rotation using a quaternion.

Attributes:

Name Type Description
r float

Real part of the quaternion \(q_r\).

i float

Imaginary part of the quaternion \(q_i\).

j float

Imaginary part of the quaternion \(q_j\).

k float

Imaginary part of the quaternion \(q_k\).

to_matrix()

Make a rotation matrix.

Returns:

Type Description
ndarray

3D rotation matrix as a 3x3 numpy array.

Source code in src/compas_camcal/models/geometry.py
197
198
199
200
201
202
203
204
def to_matrix(self) -> np.ndarray:
    """
    Make a rotation matrix.

    Returns:
        3D rotation matrix as a 3x3 numpy array.
    """
    return quaternion_to_rotation_matrix(self.r, self.i, self.j, self.k)

Rotation3D_Rodrigues(vector=(lambda: np.zeros((3, 1)))()) dataclass

Bases: Rotation3D

3D rotation using a Rodrigues vector.

Attributes:

Name Type Description
vector ndarray

Rodrigues vector as a 3x1 numpy array.

to_matrix()

Make a rotation matrix.

Returns:

Type Description
ndarray

3D rotation matrix as a 3x3 numpy array.

Source code in src/compas_camcal/models/geometry.py
229
230
231
232
233
234
235
236
def to_matrix(self) -> np.ndarray:
    """
    Make a rotation matrix.

    Returns:
        3D rotation matrix as a 3x3 numpy array.
    """
    return cv2.Rodrigues(self.vector)[0]

Translation3D(x=0.0, y=0.0, z=0.0) dataclass

3D translation.

Attributes:

Name Type Description
x float

X coordinate

y float

Y coordinate

z float

Z coordinate

from_vector(vector) classmethod

Initialize from a translation vector.

Parameters:

Name Type Description Default
vector ndarray

3D translation vector as a 3x1 numpy array.

required

Returns:

Type Description
Translation3D

3D translation.

Source code in src/compas_camcal/models/geometry.py
56
57
58
59
60
61
62
63
64
65
66
67
@classmethod
def from_vector(cls, vector: np.ndarray) -> "Translation3D":
    """
    Initialize from a translation vector.

    Args:
        vector: 3D translation vector as a 3x1 numpy array.

    Returns:
        3D translation.
    """
    return cls(*vector.ravel())

to_vector()

Make a translation column vector of the form:

\[ \left[\begin{array}{c} x \\ y \\ z \end{array}\right] \]

Returns:

Type Description
ndarray

3D translation vector as a 3x1 numpy array.

Source code in src/compas_camcal/models/geometry.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def to_vector(self) -> np.ndarray:
    """
    Make a translation column vector of the form:

    $$
    \\left[\\begin{array}{c}
    x \\\\
    y \\\\
    z
    \\end{array}\\right]
    $$

    Returns:
        3D translation vector as a 3x1 numpy array.
    """
    return np.array([[self.x, self.y, self.z]]).T