|
30 | 30 | from ansys.geometry.core.typing import Real, RealSequence |
31 | 31 |
|
32 | 32 | if TYPE_CHECKING: |
| 33 | + from ansys.geometry.core.math.frame import Frame |
33 | 34 | from ansys.geometry.core.math.vector import Vector3D # For type hints |
34 | 35 |
|
35 | 36 | DEFAULT_MATRIX33 = np.identity(3) |
@@ -308,3 +309,61 @@ def create_rotation( |
308 | 309 | ] |
309 | 310 | ) |
310 | 311 | return matrix |
| 312 | + |
| 313 | + @classmethod |
| 314 | + def create_matrix_from_rotation_about_axis(cls, axis: "Vector3D", angle: float) -> "Matrix44": |
| 315 | + """ |
| 316 | + Create a matrix representing a rotation about a given axis. |
| 317 | +
|
| 318 | + Parameters |
| 319 | + ---------- |
| 320 | + axis : Vector3D |
| 321 | + The axis of rotation. |
| 322 | + angle : float |
| 323 | + The angle of rotation in radians. |
| 324 | +
|
| 325 | + Returns |
| 326 | + ------- |
| 327 | + Matrix44 |
| 328 | + A 4x4 matrix representing the rotation. |
| 329 | + """ |
| 330 | + axis_dir = axis.normalize() |
| 331 | + x, y, z = axis_dir[0], axis_dir[1], axis_dir[2] |
| 332 | + |
| 333 | + k = np.array([[0, -z, y], [z, 0, -x], [-y, x, 0]]) |
| 334 | + |
| 335 | + identity = np.eye(3) |
| 336 | + cos_theta = np.cos(angle) |
| 337 | + sin_theta = np.sin(angle) |
| 338 | + |
| 339 | + # Rodrigues' rotation formula |
| 340 | + rotation_3x3 = identity + sin_theta * k + (1 - cos_theta) * (k @ k) |
| 341 | + |
| 342 | + # Convert to a 4x4 homogeneous matrix |
| 343 | + rotation_matrix = np.eye(4) |
| 344 | + rotation_matrix[:3, :3] = rotation_3x3 |
| 345 | + |
| 346 | + return cls(rotation_matrix) |
| 347 | + |
| 348 | + @classmethod |
| 349 | + def create_matrix_from_mapping(cls, frame: "Frame") -> "Matrix44": |
| 350 | + """ |
| 351 | + Create a matrix representing the specified mapping. |
| 352 | +
|
| 353 | + Parameters |
| 354 | + ---------- |
| 355 | + frame : Frame |
| 356 | + The frame containing the origin and direction vectors. |
| 357 | +
|
| 358 | + Returns |
| 359 | + ------- |
| 360 | + Matrix44 |
| 361 | + A 4x4 matrix representing the translation and rotation defined by the frame. |
| 362 | + """ |
| 363 | + from ansys.geometry.core.math.vector import Vector3D |
| 364 | + |
| 365 | + translation_matrix = Matrix44.create_translation( |
| 366 | + Vector3D([frame.origin[0], frame.origin[1], frame.origin[2]]) |
| 367 | + ) |
| 368 | + rotation_matrix = Matrix44.create_rotation(frame.direction_x, frame.direction_y) |
| 369 | + return translation_matrix * rotation_matrix |
0 commit comments