NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

test_mathCommon.py (7895B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\script\util\test\test_mathCommon.py
      2 from math import pi, pow, sqrt
      3 from mock import patch
      4 from testsuites.testcases import ServerClientTestCase
      5 from unittest import main
      6 
      7 class Geo2Mock(object):
      8 
      9     def Vec3Length(self, v):
     10         return sqrt(sum([ pow(v[i], 2) for i in xrange(3) ]))
     11 
     12     def Vec3Subtract(self, v1, v2):
     13         return [ v1[i] - v2[i] for i in xrange(3) ]
     14 
     15     def MatrixMultiply(self, m1, m2):
     16         return [ [ sum((a * b for a, b in zip(m1_row, m2_col))) for m2_col in zip(*m2) ] for m1_row in m1 ]
     17 
     18 
     19 class GetAngleBetweenTwoVectorsTestCase(ServerClientTestCase):
     20 
     21     def setUp(self):
     22         super(GetAngleBetweenTwoVectorsTestCase, self).setUp()
     23         with patch.dict('sys.modules', self._mock_imported_modules()):
     24             from carbon.common.script.util.mathCommon import FloatCloseEnough, GetAngleBetweenTwoVectors
     25             from carbon.common.script.util.mathErrors import InvalidInput
     26             self.get_angle_between_two_vectors = GetAngleBetweenTwoVectors
     27             self.is_float_close_enough = FloatCloseEnough
     28             self.invalid_input = InvalidInput
     29 
     30     def _mock_imported_modules(self):
     31         imported_modules = {'geo2': Geo2Mock()}
     32         return imported_modules
     33 
     34     def _test_angle_is_as_expected(self, v1, v2, expected_angle):
     35         test_angle = self.get_angle_between_two_vectors(v1, v2)
     36         self.assertTrue(expr=self.is_float_close_enough(test_angle, expected_angle), msg='Result: {test} vs Expected: {expected}'.format(test=test_angle, expected=expected_angle))
     37 
     38     def _test_exception_raised_when_calculating_angle(self, v1, v2):
     39         self.assertRaises(self.invalid_input, self.get_angle_between_two_vectors, v1, v2)
     40 
     41     def test_angle_between_two_vectors_1(self):
     42         v1 = (4.0, 0.0, 7.0)
     43         v2 = (-2.0, 1.0, 3.0)
     44         self._test_angle_is_as_expected(v1, v2, expected_angle=1.12525242)
     45 
     46     def test_angle_between_two_vectors_2(self):
     47         v1 = (3.0, 4.0, -7.0)
     48         v2 = (-2.0, 1.0, 3.0)
     49         self._test_angle_is_as_expected(v1, v2, expected_angle=2.36681159)
     50 
     51     def test_angle_between_two_vectors_parallel(self):
     52         v1 = (1.0, 0.0, 0.0)
     53         v2 = (2.0, 0.0, 0.0)
     54         self._test_angle_is_as_expected(v1, v2, expected_angle=0.0)
     55 
     56     def test_angle_between_two_vectors_opposite(self):
     57         v1 = (1.0, 0.0, 0.0)
     58         v2 = (-2.0, 0.0, 0.0)
     59         self._test_angle_is_as_expected(v1, v2, expected_angle=pi)
     60 
     61     def test_angle_between_two_vectors_perpendicular(self):
     62         v1 = (1.0, 0.0, 0.0)
     63         v2 = (0.0, -1.0, 0.0)
     64         self._test_angle_is_as_expected(v1, v2, expected_angle=pi / 2.0)
     65 
     66     def test_angle_between_vector_and_zero_vector(self):
     67         v1 = (1.0, 0.0, 0.0)
     68         v2 = (0.0, 0.0, 0.0)
     69         self._test_exception_raised_when_calculating_angle(v1, v2)
     70 
     71     def test_angle_between_two_zero_vectors(self):
     72         v1 = (0.0, 0.0, 0.0)
     73         v2 = (0.0, 0.0, 0.0)
     74         self._test_exception_raised_when_calculating_angle(v1, v2)
     75 
     76     def test_angle_between_two_same_vectors(self):
     77         v1 = (1.0, 0.0, 0.0)
     78         v2 = (1.0, 0.0, 0.0)
     79         self._test_angle_is_as_expected(v1, v2, expected_angle=0.0)
     80 
     81 
     82 class GetSignedDistanceFromPointToPlaneTestCase(ServerClientTestCase):
     83 
     84     def setUp(self):
     85         super(GetSignedDistanceFromPointToPlaneTestCase, self).setUp()
     86         with patch.dict('sys.modules', self._mock_imported_modules()):
     87             from carbon.common.script.util.mathCommon import FloatCloseEnough, GetSignedDistanceFromPointToPlane
     88             from carbon.common.script.util.mathErrors import InvalidInput
     89             self.get_signed_distance_from_point_to_plane = GetSignedDistanceFromPointToPlane
     90             self.is_float_close_enough = FloatCloseEnough
     91             self.invalid_input = InvalidInput
     92 
     93     def _mock_imported_modules(self):
     94         imported_modules = {'geo2': Geo2Mock()}
     95         return imported_modules
     96 
     97     def _test_distance_is_as_expected(self, point, plane, expected_distance):
     98         test_distance = self.get_signed_distance_from_point_to_plane(point, plane)
     99         self.assertTrue(expr=self.is_float_close_enough(test_distance, expected_distance), msg='Result: {test} vs Expected: {expected}'.format(test=test_distance, expected=expected_distance))
    100 
    101     def _test_exception_raised_when_calculating_distance(self, point, plane):
    102         self.assertRaises(self.invalid_input, self.get_signed_distance_from_point_to_plane, point, plane)
    103 
    104     def test_distance_0_between_point_0_and_plane_z0(self):
    105         point = (0.0, 0.0, 0.0)
    106         plane = (0.0, 0.0, 1.0, 0.0)
    107         self._test_distance_is_as_expected(point, plane, expected_distance=0.0)
    108 
    109     def test_distance_0_between_point_z0_and_plane_z0(self):
    110         point = (22.0, -30.0, 0.0)
    111         plane = (0.0, 0.0, 1.0, 0.0)
    112         self._test_distance_is_as_expected(point, plane, expected_distance=0.0)
    113 
    114     def test_distance_1_between_point_z1_and_plane_z0(self):
    115         point = (0.0, 0.0, 1.0)
    116         plane = (0.0, 0.0, 1.0, 0.0)
    117         self._test_distance_is_as_expected(point, plane, expected_distance=1.0)
    118 
    119     def test_distance_10_between_point_z10_and_plane_z0(self):
    120         point = (0.0, 0.0, 10.0)
    121         plane = (0.0, 0.0, 1.0, 0.0)
    122         self._test_distance_is_as_expected(point, plane, expected_distance=10.0)
    123 
    124     def test_distance_between_point_and_not_a_plane(self):
    125         point = (0.0, 0.0, 10.0)
    126         plane = (0.0, 0.0, 0.0, 0.0)
    127         self._test_exception_raised_when_calculating_distance(point, plane)
    128 
    129 
    130 class IsWithinFrustumTestCase(ServerClientTestCase):
    131 
    132     def setUp(self):
    133         super(IsWithinFrustumTestCase, self).setUp()
    134         with patch.dict('sys.modules', self._mock_imported_modules()):
    135             from carbon.common.script.util.mathCommon import IsWithinFrustum
    136             self.is_within_frustum = IsWithinFrustum
    137 
    138     def _mock_imported_modules(self):
    139         imported_modules = {'geo2': Geo2Mock()}
    140         return imported_modules
    141 
    142     def _test_is_in_view_as_expected(self, point, view_matrix, projection_matrix, expected_is_in_view):
    143         test_is_in_view = self.is_within_frustum(point, view_matrix, projection_matrix)
    144         self.assertTrue(expr=test_is_in_view == expected_is_in_view, msg='Result: {test} vs Expected: {expected}'.format(test=test_is_in_view, expected=expected_is_in_view))
    145 
    146     def test_point_is_in_view(self):
    147         point = (-46147.09375, 1563.0390625, -4425.38671875)
    148         view_matrix = ((0.025582458823919296, -0.10342695564031601, 0.9943079352378845, 0.0),
    149          (0.0, 0.9946334362030029, 0.10346081852912903, 0.0),
    150          (-0.9996727108955383, -0.0026467822026461363, 0.02544517070055008, 0.0),
    151          (-0.3162713050842285, 0.028678221628069878, -17.96691131591797, 1.0))
    152         projection_matrix = ((1.8118325471878052, 0.0, 0.0, 0.0),
    153          (0.0, 3.0685949325561523, 0.0, 0.0),
    154          (0.0, 0.0, -1.0000005960464478, -1.0),
    155          (0.0, 0.0, -6.000003337860107, 0.0))
    156         self._test_is_in_view_as_expected(point, view_matrix, projection_matrix, expected_is_in_view=True)
    157 
    158     def test_point_is_not_in_view(self):
    159         point = (-46147.09375, 1563.0390625, -4425.38671875)
    160         view_matrix = ((-0.639927864074707, -0.07032500207424164, 0.7652102112770081, 0.0),
    161          (0.0, 0.9958034753799438, 0.09151717275381088, 0.0),
    162          (-0.768435001373291, 0.05856438726186752, -0.6372423768043518, 0.0),
    163          (-0.23369121551513672, 0.014873266220092773, -18.198253631591797, 1.0))
    164         projection_matrix = ((1.8118325471878052, 0.0, 0.0, 0.0),
    165          (0.0, 3.0685951709747314, 0.0, 0.0),
    166          (0.0, 0.0, -1.0000005960464478, -1.0),
    167          (0.0, 0.0, -6.000003337860107, 0.0))
    168         self._test_is_in_view_as_expected(point, view_matrix, projection_matrix, expected_is_in_view=False)
    169 
    170 
    171 if __name__ == '__main__':
    172     main()