File size: 5,579 Bytes
6686473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np

def _skew(v):
    return np.array([
        [0.0, -v[2], v[1]],
        [v[2], 0.0, -v[0]],
        [-v[1], v[0], 0.0]
    ])

def _rot_z(theta):
    c, s = np.cos(theta), np.sin(theta)
    return np.array([
        [c, -s, 0.0, 0.0],
        [s,  c, 0.0, 0.0],
        [0.0, 0.0, 1.0, 0.0],
        [0.0, 0.0, 0.0, 1.0]
    ])

def _translate(x, y, z):
    return np.array([
        [1.0, 0.0, 0.0, x],
        [0.0, 1.0, 0.0, y],
        [0.0, 0.0, 1.0, z],
        [0.0, 0.0, 0.0, 1.0]
    ])

class TaskManifold:
    def get_transform(self, u: np.ndarray) -> np.ndarray:
        raise NotImplementedError

    def get_transform_jacobian(self, u: np.ndarray) -> np.ndarray:
        raise NotImplementedError
        
    def get_initial_guess(self) -> np.ndarray:
        raise NotImplementedError
        
    def get_bounds(self):
        raise NotImplementedError


class CylinderTaskManifold(TaskManifold):
    """
    Cylinder manifold for grasps.
    u = [z, theta]
    T_grasp = T_obj * T_z(z) * R_z(theta) * T_offset
    """
    def __init__(self, T_obj: np.ndarray, height: float, T_offset: np.ndarray):
        self.T_obj = T_obj
        self.height = height
        self.T_offset = T_offset
        self.R_obj = T_obj[:3, :3]
        self.p_obj = T_obj[:3, 3]
        self.p_offset = T_offset[:3, 3]

    def get_transform(self, u: np.ndarray) -> np.ndarray:
        z, theta = u
        T_task = _translate(0.0, 0.0, z) @ _rot_z(theta)
        return self.T_obj @ T_task @ self.T_offset

    def get_transform_jacobian(self, u: np.ndarray) -> np.ndarray:
        """
        Returns a 6x2 spatial Jacobian [v; w] in the base frame.
        """
        z, theta = u
        # T_obj = [R_obj, p_obj; 0, 1]
        
        # 1. Partial w.r.t z (translation along local Z)
        # linear velocity: R_obj * [0, 0, 1]^T
        v_z = self.R_obj[:, 2]
        w_z = np.zeros(3)
        J_z = np.concatenate([v_z, w_z])
        
        # 2. Partial w.r.t theta (rotation around local Z)
        # angular velocity: R_obj * [0, 0, 1]^T
        w_theta = self.R_obj[:, 2]
        # linear velocity at grasping point p_grasp:
        # p_grasp = p_obj + R_obj * [0, 0, z]^T + R_obj * R_z(theta) * p_offset
        # dp_grasp/dtheta = w_theta x (R_obj * R_z(theta) * p_offset)
        R_z_theta = _rot_z(theta)[:3, :3]
        r_offset = self.R_obj @ R_z_theta @ self.p_offset
        v_theta = np.cross(w_theta, r_offset)
        J_theta = np.concatenate([v_theta, w_theta])
        
        return np.column_stack([J_z, J_theta])

    def get_initial_guess(self) -> np.ndarray:
        return np.array([0.0, 0.0])

    def get_bounds(self):
        # bounds for z and theta
        return [(-self.height / 2.0, self.height / 2.0), (-np.pi, np.pi)]


class BoxTaskManifold(TaskManifold):
    """
    Box manifold for grasps on a specific face.
    u = [u_coord, v_coord, theta]
    T_grasp = T_face * T_xy(u_coord, v_coord) * R_z(theta) * T_offset
    """
    def __init__(self, T_face: np.ndarray, width: float, height: float, T_offset: np.ndarray):
        self.T_face = T_face
        self.width = width
        self.height = height
        self.T_offset = T_offset
        self.R_face = T_face[:3, :3]
        self.p_offset = T_offset[:3, 3]

    def get_transform(self, u: np.ndarray) -> np.ndarray:
        u_coord, v_coord, theta = u
        T_task = _translate(u_coord, v_coord, 0.0) @ _rot_z(theta)
        return self.T_face @ T_task @ self.T_offset

    def get_transform_jacobian(self, u: np.ndarray) -> np.ndarray:
        """
        Returns a 6x3 spatial Jacobian [v; w] in the base frame.
        """
        u_coord, v_coord, theta = u
        
        # 1. Partial w.r.t u_coord (translation along local X)
        v_u = self.R_face[:, 0]
        w_u = np.zeros(3)
        J_u = np.concatenate([v_u, w_u])
        
        # 2. Partial w.r.t v_coord (translation along local Y)
        v_v = self.R_face[:, 1]
        w_v = np.zeros(3)
        J_v = np.concatenate([v_v, w_v])
        
        # 3. Partial w.r.t theta (rotation around local Z)
        w_theta = self.R_face[:, 2]
        R_z_theta = _rot_z(theta)[:3, :3]
        r_offset = self.R_face @ R_z_theta @ self.p_offset
        v_theta = np.cross(w_theta, r_offset)
        J_theta = np.concatenate([v_theta, w_theta])
        
        return np.column_stack([J_u, J_v, J_theta])

    def get_initial_guess(self) -> np.ndarray:
        return np.array([0.0, 0.0, 0.0])

    def get_bounds(self):
        return [(-self.width / 2.0, self.width / 2.0), (-self.height / 2.0, self.height / 2.0), (-np.pi, np.pi)]


class GenericMeshTaskManifold(TaskManifold):
    def __init__(self):
        # The contract states that it must fail immediately if parameterized grasp surface patches are not pre-supplied.
        raise ValueError("GenericMeshTaskManifold: Parameterized grasp surface patches not supplied. Implementation MUST stop.")


class TaskManifoldFactory:
    @staticmethod
    def create_manifold(shape_type: str, T_obj: np.ndarray, dimensions: dict, T_offset: np.ndarray) -> TaskManifold:
        if shape_type == "cylinder":
            return CylinderTaskManifold(T_obj, dimensions["height"], T_offset)
        elif shape_type == "box":
            # For a box, one must specify the specific T_face as T_obj.
            return BoxTaskManifold(T_obj, dimensions["width"], dimensions["height"], T_offset)
        elif shape_type == "mesh":
            return GenericMeshTaskManifold()
        else:
            raise ValueError(f"Unknown shape type: {shape_type}")