| 141 | |
| 142 | |
| 143 | class RandomRotation: |
| 144 | def __init__(self, axis=None, max_theta=180, max_theta2=15): |
| 145 | self.axis = axis |
| 146 | self.max_theta = max_theta # Rotation around axis |
| 147 | self.max_theta2 = max_theta2 # Smaller rotation in random direction |
| 148 | |
| 149 | def _M(self, axis, theta): |
| 150 | return expm(np.cross(np.eye(3), axis / norm(axis) * theta)).astype(np.float32) |
| 151 | |
| 152 | def __call__(self, coords): |
| 153 | if self.axis is not None: |
| 154 | axis = self.axis |
| 155 | else: |
| 156 | axis = np.random.rand(3) - 0.5 |
| 157 | R = self._M(axis, (np.pi * self.max_theta / 180) * 2 * (np.random.rand(1) - 0.5)) |
| 158 | if self.max_theta2 is None: |
| 159 | coords = coords @ R |
| 160 | else: |
| 161 | R_n = self._M(np.random.rand(3) - 0.5, (np.pi * self.max_theta2 / 180) * 2 * (np.random.rand(1) - 0.5)) |
| 162 | coords = coords @ R @ R_n |
| 163 | |
| 164 | return coords |
| 165 | |
| 166 | |
| 167 | class RandomTranslation: |