| 1 |
# Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without |
| 5 |
# modification, are permitted provided that the following conditions are met: |
| 6 |
# |
| 7 |
# * Redistributions of source code must retain the above copyright |
| 8 |
# notice, this list of conditions and the following disclaimer. |
| 9 |
# |
| 10 |
# * Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# |
| 14 |
# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of |
| 15 |
# its contributors may be used to endorse or promote products derived |
| 16 |
# from this software without specific prior written permission. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
# POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
import numpy as np |
| 31 |
|
| 32 |
from .geometry import normalize_vec, vec_angular_dist_deg |
| 33 |
|
| 34 |
|
| 35 |
class TestNormalizeVec: |
| 36 |
def test_unit_vector(self): |
| 37 |
vec = np.array([1.0, 0.0, 0.0]) |
| 38 |
normalized = normalize_vec(vec) |
| 39 |
np.testing.assert_allclose(normalized, vec) |
| 40 |
np.testing.assert_almost_equal(np.linalg.norm(normalized), 1.0) |
| 41 |
|
| 42 |
def test_non_unit_vector(self): |
| 43 |
vec = np.array([3.0, 4.0, 0.0]) |
| 44 |
normalized = normalize_vec(vec) |
| 45 |
expected = np.array([0.6, 0.8, 0.0]) |
| 46 |
np.testing.assert_allclose(normalized, expected) |
| 47 |
np.testing.assert_almost_equal(np.linalg.norm(normalized), 1.0) |
| 48 |
|
| 49 |
def test_zero_vector(self): |
| 50 |
vec = np.array([0.0, 0.0, 0.0]) |
| 51 |
normalized = normalize_vec(vec) |
| 52 |
assert np.linalg.norm(normalized) < 1e-8 |
| 53 |
|
| 54 |
|
| 55 |
class TestVecAngularDistDeg: |
| 56 |
def test_identical_vectors(self): |
| 57 |
vec1 = np.array([1.0, 0.0, 0.0]) |
| 58 |
vec2 = np.array([1.0, 0.0, 0.0]) |
| 59 |
dist = vec_angular_dist_deg(vec1, vec2) |
| 60 |
np.testing.assert_almost_equal(dist, 0.0) |
| 61 |
|
| 62 |
def test_opposite_vectors(self): |
| 63 |
vec1 = np.array([1.0, 0.0, 0.0]) |
| 64 |
vec2 = np.array([-1.0, 0.0, 0.0]) |
| 65 |
dist = vec_angular_dist_deg(vec1, vec2) |
| 66 |
np.testing.assert_almost_equal(dist, 180.0) |
| 67 |
|
| 68 |
def test_perpendicular_vectors(self): |
| 69 |
vec1 = np.array([1.0, 0.0, 0.0]) |
| 70 |
vec2 = np.array([0.0, 1.0, 0.0]) |
| 71 |
dist = vec_angular_dist_deg(vec1, vec2) |
| 72 |
np.testing.assert_almost_equal(dist, 90.0) |
| 73 |
|
| 74 |
def test_45_degree_vectors(self): |
| 75 |
vec1 = np.array([1.0, 0.0, 0.0]) |
| 76 |
vec2 = np.array([1.0, 1.0, 0.0]) |
| 77 |
dist = vec_angular_dist_deg(vec1, vec2) |
| 78 |
np.testing.assert_almost_equal(dist, 45.0) |
| 79 |
|
| 80 |
def test_non_unit_vectors(self): |
| 81 |
vec1 = np.array([2.0, 0.0, 0.0]) |
| 82 |
vec2 = np.array([0.0, 3.0, 0.0]) |
| 83 |
dist = vec_angular_dist_deg(vec1, vec2) |
| 84 |
# Should normalize internally |
| 85 |
np.testing.assert_almost_equal(dist, 90.0) |