|
24 | 24 |
|
25 | 25 | from beartype.roar import BeartypeCallHintParamViolation
|
26 | 26 | import numpy as np
|
| 27 | +from pint import Quantity |
27 | 28 | import pytest
|
28 | 29 |
|
29 | 30 | from ansys.geometry.core.math import (
|
@@ -628,6 +629,24 @@ def test_vector2d_errors():
|
628 | 629 | v1.get_angle_between(v2)
|
629 | 630 |
|
630 | 631 |
|
| 632 | +def test_rotate_vector(): |
| 633 | + """Test the rotate_vector method.""" |
| 634 | + # Define the vectors and angle |
| 635 | + axis = Vector3D([0.0, 0.0, 1.0]) |
| 636 | + vector = Vector3D([1.0, 0.0, 0.0]) |
| 637 | + |
| 638 | + angle = Quantity(np.pi / 2) # 90 degrees |
| 639 | + |
| 640 | + # Expected result after rotating vector around axis by 90 degrees |
| 641 | + expected_vector = Vector3D([0.0, 1.0, 0.0]) |
| 642 | + |
| 643 | + # Call the method under test |
| 644 | + result_vector = axis.rotate_vector(vector, angle) |
| 645 | + |
| 646 | + # Assert that the result matches the expected vector |
| 647 | + assert np.allclose(result_vector, expected_vector) |
| 648 | + |
| 649 | + |
631 | 650 | def test_matrix():
|
632 | 651 | """Simple test to create a ``Matrix``."""
|
633 | 652 | # Create two matrix objects
|
@@ -828,6 +847,82 @@ def test_create_rotation_matrix():
|
828 | 847 | assert np.array_equal(expected_matrix, rotation_matrix)
|
829 | 848 |
|
830 | 849 |
|
| 850 | +def test_create_matrix_from_rotation_about_axis_x(): |
| 851 | + """Test the create_matrix_from_rotation_about_axis method for rotation about the x-axis.""" |
| 852 | + axis = Vector3D([1.0, 0.0, 0.0]) |
| 853 | + angle = np.pi / 2 # 90 degrees |
| 854 | + expected_matrix = Matrix44([[1, 0, 0, 0], [0, 0, -1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) |
| 855 | + |
| 856 | + result_matrix = Matrix44.create_matrix_from_rotation_about_axis(axis, angle) |
| 857 | + |
| 858 | + print(result_matrix) |
| 859 | + assert np.allclose(result_matrix, expected_matrix) |
| 860 | + |
| 861 | + |
| 862 | +def test_create_matrix_from_rotation_about_axis_y(): |
| 863 | + """Test the create_matrix_from_rotation_about_axis method for rotation about the y-axis.""" |
| 864 | + axis = Vector3D([0.0, 1.0, 0.0]) |
| 865 | + angle = np.pi / 2 # 90 degrees |
| 866 | + expected_matrix = Matrix44([[0, 0, 1, 0], [0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, 0, 1]]) |
| 867 | + |
| 868 | + result_matrix = Matrix44.create_matrix_from_rotation_about_axis(axis, angle) |
| 869 | + assert np.allclose(result_matrix, expected_matrix) |
| 870 | + |
| 871 | + |
| 872 | +def test_create_matrix_from_rotation_about_axis_z(): |
| 873 | + """Test the create_matrix_from_rotation_about_axis method for rotation about the z-axis.""" |
| 874 | + axis = Vector3D([0.0, 0.0, 1.0]) |
| 875 | + angle = np.pi / 2 # 90 degrees |
| 876 | + expected_matrix = Matrix44([[0, -1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) |
| 877 | + |
| 878 | + result_matrix = Matrix44.create_matrix_from_rotation_about_axis(axis, angle) |
| 879 | + assert np.allclose(result_matrix, expected_matrix) |
| 880 | + |
| 881 | + |
| 882 | +def test_create_matrix_from_rotation_about_arbitrary_axis(): |
| 883 | + """Test the create_matrix_from_rotation_about_axis method for |
| 884 | + rotation about an arbitrary axis. |
| 885 | + """ |
| 886 | + axis = Vector3D([1.0, 1.0, 1.0]).normalize() |
| 887 | + angle = np.pi / 3 # 60 degrees |
| 888 | + # Expected matrix calculated using external tools or libraries |
| 889 | + expected_matrix = Matrix44( |
| 890 | + [ |
| 891 | + [0.66666667, -0.33333333, 0.66666667, 0], |
| 892 | + [0.66666667, 0.66666667, -0.33333333, 0], |
| 893 | + [-0.33333333, 0.66666667, 0.66666667, 0], |
| 894 | + [0, 0, 0, 1], |
| 895 | + ] |
| 896 | + ) |
| 897 | + |
| 898 | + result_matrix = Matrix44.create_matrix_from_rotation_about_axis(axis, angle) |
| 899 | + assert np.allclose(result_matrix, expected_matrix) |
| 900 | + |
| 901 | + |
| 902 | +def test_create_matrix_from_mapping(): |
| 903 | + """Test the create_matrix_from_mapping method.""" |
| 904 | + # Define the frame with origin and direction vectors |
| 905 | + origin = Vector3D([1.0, 2.0, 3.0]) |
| 906 | + direction_x = Vector3D([1.0, 0.0, 0.0]) |
| 907 | + direction_y = Vector3D([0.0, 1.0, 0.0]) |
| 908 | + frame = Frame(origin, direction_x, direction_y) |
| 909 | + |
| 910 | + # Create the expected translation matrix |
| 911 | + expected_translation_matrix = Matrix44.create_translation(origin) |
| 912 | + |
| 913 | + # Create the expected rotation matrix |
| 914 | + expected_rotation_matrix = Matrix44.create_rotation(direction_x, direction_y) |
| 915 | + |
| 916 | + # Create the expected result by multiplying the translation and rotation matrices |
| 917 | + expected_matrix = expected_translation_matrix * expected_rotation_matrix |
| 918 | + |
| 919 | + # Call the method under test |
| 920 | + result_matrix = Matrix44.create_matrix_from_mapping(frame) |
| 921 | + |
| 922 | + # Assert that the result matches the expected matrix |
| 923 | + assert np.allclose(result_matrix, expected_matrix) |
| 924 | + |
| 925 | + |
831 | 926 | def test_frame():
|
832 | 927 | """``Frame`` construction and equivalency."""
|
833 | 928 | origin = Point3D([42, 99, 13])
|
|
0 commit comments