File size: 10,624 Bytes
5f38e95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
r"""Rigid-body kinematic primitives: SE(3) poses, twists, and wrenches.

Conventions
-----------

* Right-handed coordinate frames throughout.
* A pose :math:`(\mathbf{p}, \mathbf{R}) \in SE(3)` maps a point expressed in
  the body frame to the world frame via :math:`\mathbf{x}_W = \mathbf{R}\,\mathbf{x}_B + \mathbf{p}`.
* Twists and wrenches use the **linear-first** stacking
  :math:`\boldsymbol{\xi} = (\mathbf{v}^\top, \boldsymbol{\omega}^\top)^\top`
  matching Pott, *Cable-Driven Parallel Robots* (Springer, 2018, Ch. 5).
* Quaternions follow SciPy's scalar-last :math:`(x, y, z, w)` convention; the
  ``Pose`` helpers accept and return that form when working with quaternion
  representations explicitly.

The objects defined here are deliberately small. They wrap SciPy's
``Rotation`` (which is well-tested, vectorised, and avoids us reimplementing
quaternion normalisation) and add the few operations that the rest of the
framework actually needs: composition, inversion, point/vector transformation,
adjoint transport of twists and wrenches.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Self, overload

import numpy as np
from numpy.typing import ArrayLike, NDArray
from scipy.spatial.transform import Rotation


# ---------------------------------------------------------------------------
# so(3) <-> R^3 helpers
# ---------------------------------------------------------------------------

def hat(omega: ArrayLike) -> NDArray[np.float64]:
    r"""Skew-symmetric matrix :math:`[\boldsymbol{\omega}]_\times` of a 3-vector.

    Supports a single 3-vector ``(3,)`` or a stack ``(..., 3)``; the trailing
    two axes of the result are the :math:`3\times 3` skew matrices.
    """
    w = np.asarray(omega, dtype=np.float64)
    if w.shape[-1] != 3:
        raise ValueError(f"hat() expects last axis of size 3, got shape {w.shape}")
    zero = np.zeros(w.shape[:-1], dtype=np.float64)
    wx, wy, wz = w[..., 0], w[..., 1], w[..., 2]
    return np.stack(
        [
            np.stack([zero, -wz, wy], axis=-1),
            np.stack([wz, zero, -wx], axis=-1),
            np.stack([-wy, wx, zero], axis=-1),
        ],
        axis=-2,
    )


def vee(skew: ArrayLike) -> NDArray[np.float64]:
    r"""Inverse of :func:`hat`: extract the 3-vector from a skew matrix.

    No symmetry check is performed; the antisymmetric part is read directly so
    callers can pass slightly non-skew matrices produced by finite-difference
    code without an exception.
    """
    S = np.asarray(skew, dtype=np.float64)
    if S.shape[-2:] != (3, 3):
        raise ValueError(f"vee() expects last two axes (3, 3), got shape {S.shape}")
    return np.stack([S[..., 2, 1], S[..., 0, 2], S[..., 1, 0]], axis=-1)


# ---------------------------------------------------------------------------
# SE(3) pose
# ---------------------------------------------------------------------------

@dataclass(slots=True)
class Pose:
    r"""Rigid-body pose :math:`(\mathbf{p}, \mathbf{R}) \in SE(3)`.

    The translation lives in ``position`` (shape ``(3,)`` or a leading batch
    ``(..., 3)``) and the orientation in ``rotation`` (a SciPy ``Rotation``
    which natively handles batching). The two must broadcast compatibly.
    """

    position: NDArray[np.float64]
    rotation: Rotation = field(default_factory=lambda: Rotation.identity())

    # --- construction ----------------------------------------------------

    def __post_init__(self) -> None:
        self.position = np.asarray(self.position, dtype=np.float64)
        if self.position.shape[-1] != 3:
            raise ValueError(
                f"Pose.position last axis must be 3, got shape {self.position.shape}"
            )

    @classmethod
    def identity(cls) -> Self:
        return cls(position=np.zeros(3), rotation=Rotation.identity())

    @classmethod
    def from_matrix(cls, T: ArrayLike) -> Self:
        T = np.asarray(T, dtype=np.float64)
        if T.shape[-2:] != (4, 4):
            raise ValueError(f"Expected (..., 4, 4) homogeneous matrix, got {T.shape}")
        return cls(position=T[..., :3, 3], rotation=Rotation.from_matrix(T[..., :3, :3]))

    @classmethod
    def from_quaternion(cls, position: ArrayLike, quat_xyzw: ArrayLike) -> Self:
        return cls(position=np.asarray(position), rotation=Rotation.from_quat(quat_xyzw))

    @classmethod
    def from_euler(
        cls, position: ArrayLike, angles: ArrayLike, seq: str = "xyz", degrees: bool = False
    ) -> Self:
        return cls(
            position=np.asarray(position),
            rotation=Rotation.from_euler(seq, angles, degrees=degrees),
        )

    @classmethod
    def from_axis_angle(cls, position: ArrayLike, rotvec: ArrayLike) -> Self:
        return cls(position=np.asarray(position), rotation=Rotation.from_rotvec(rotvec))

    # --- inspection ------------------------------------------------------

    @property
    def matrix(self) -> NDArray[np.float64]:
        R = self.rotation.as_matrix()
        T = np.zeros(R.shape[:-2] + (4, 4), dtype=np.float64)
        T[..., :3, :3] = R
        T[..., :3, 3] = self.position
        T[..., 3, 3] = 1.0
        return T

    @property
    def quaternion_xyzw(self) -> NDArray[np.float64]:
        return self.rotation.as_quat()

    @property
    def is_batched(self) -> bool:
        return self.position.ndim > 1

    # --- algebra ---------------------------------------------------------

    def inverse(self) -> Pose:
        Rinv = self.rotation.inv()
        return Pose(position=-Rinv.apply(self.position), rotation=Rinv)

    def compose(self, other: Pose) -> Pose:
        r"""Group product :math:`T = T_\text{self} \cdot T_\text{other}`."""
        return Pose(
            position=self.position + self.rotation.apply(other.position),
            rotation=self.rotation * other.rotation,
        )

    def __matmul__(self, other: Pose) -> Pose:
        return self.compose(other)

    # --- transforming points / vectors / spatial quantities --------------

    @overload
    def transform_point(self, point: ArrayLike) -> NDArray[np.float64]: ...
    @overload
    def transform_point(self, point: NDArray[np.float64]) -> NDArray[np.float64]: ...

    def transform_point(self, point: ArrayLike) -> NDArray[np.float64]:
        r""":math:`\mathbf{x}_W = \mathbf{R}\,\mathbf{x}_B + \mathbf{p}`."""
        p = np.asarray(point, dtype=np.float64)
        return self.rotation.apply(p) + self.position

    def transform_vector(self, vec: ArrayLike) -> NDArray[np.float64]:
        r"""Rotate a free vector (no translation): :math:`\mathbf{v}_W = \mathbf{R}\,\mathbf{v}_B`."""
        return self.rotation.apply(np.asarray(vec, dtype=np.float64))

    def __repr__(self) -> str:  # pragma: no cover - cosmetic
        rv = self.rotation.as_rotvec()
        return f"Pose(p={np.asarray(self.position).round(4).tolist()}, rotvec={np.round(rv, 4).tolist()})"


# ---------------------------------------------------------------------------
# Spatial 6-vectors (twist, wrench)
# ---------------------------------------------------------------------------

def _check_six(vec: NDArray[np.float64], name: str) -> NDArray[np.float64]:
    if vec.shape[-1] != 6:
        raise ValueError(f"{name} expects last axis of size 6, got shape {vec.shape}")
    return vec


@dataclass(slots=True, frozen=True)
class Twist:
    r"""Spatial velocity :math:`\boldsymbol{\xi} = (\mathbf{v}^\top, \boldsymbol{\omega}^\top)^\top \in \mathbb{R}^6`.

    The default stacking is *linear-first*. Use :attr:`linear` and
    :attr:`angular` to access the parts without remembering the order.
    """

    data: NDArray[np.float64]

    def __post_init__(self) -> None:
        d = np.asarray(self.data, dtype=np.float64)
        _check_six(d, "Twist.data")
        object.__setattr__(self, "data", d)

    @classmethod
    def from_parts(cls, linear: ArrayLike, angular: ArrayLike) -> Twist:
        return cls(np.concatenate([np.asarray(linear), np.asarray(angular)], axis=-1))

    @property
    def linear(self) -> NDArray[np.float64]:
        return self.data[..., :3]

    @property
    def angular(self) -> NDArray[np.float64]:
        return self.data[..., 3:]

    def transform(self, T: Pose) -> Twist:
        r"""Map this twist through pose ``T`` using the adjoint of :math:`SE(3)`.

        .. math::

            \operatorname{Ad}_T \boldsymbol{\xi}
            = \begin{bmatrix} \mathbf{R} & [\mathbf{p}]_\times \mathbf{R} \\ \mathbf{0} & \mathbf{R} \end{bmatrix}
              \boldsymbol{\xi}.
        """
        R = T.rotation.as_matrix()
        v = R @ self.linear + np.cross(T.position, R @ self.angular)
        w = R @ self.angular
        return Twist.from_parts(v, w)


@dataclass(slots=True, frozen=True)
class Wrench:
    r"""Spatial force :math:`\mathbf{w} = (\mathbf{f}^\top, \boldsymbol{\tau}^\top)^\top \in \mathbb{R}^6`.

    Linear-first stacking (force, torque). Multiplication by a tension vector
    in the statics layer yields the platform wrench produced by the cables.
    """

    data: NDArray[np.float64]

    def __post_init__(self) -> None:
        d = np.asarray(self.data, dtype=np.float64)
        _check_six(d, "Wrench.data")
        object.__setattr__(self, "data", d)

    @classmethod
    def from_parts(cls, force: ArrayLike, torque: ArrayLike) -> Wrench:
        return cls(np.concatenate([np.asarray(force), np.asarray(torque)], axis=-1))

    @classmethod
    def gravity(cls, mass: float, g: ArrayLike = (0.0, 0.0, -9.81)) -> Wrench:
        g = np.asarray(g, dtype=np.float64)
        return cls.from_parts(mass * g, np.zeros(3))

    @property
    def force(self) -> NDArray[np.float64]:
        return self.data[..., :3]

    @property
    def torque(self) -> NDArray[np.float64]:
        return self.data[..., 3:]

    def transform(self, T: Pose) -> Wrench:
        r"""Co-adjoint transport of the wrench through pose ``T``.

        Wrenches transform with :math:`\operatorname{Ad}_T^{-\top}`, which for
        ``T = (p, R)`` evaluates to

        .. math::

            \begin{bmatrix} \mathbf{R} & \mathbf{0} \\ [\mathbf{p}]_\times \mathbf{R} & \mathbf{R} \end{bmatrix}.
        """
        R = T.rotation.as_matrix()
        f = R @ self.force
        tau = R @ self.torque + np.cross(T.position, f)
        return Wrench.from_parts(f, tau)

    def __add__(self, other: Wrench) -> Wrench:
        return Wrench(self.data + other.data)

    def __sub__(self, other: Wrench) -> Wrench:
        return Wrench(self.data - other.data)

    def __neg__(self) -> Wrench:
        return Wrench(-self.data)