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