Coverage for tests/test_UnitVector3d.py: 20%
65 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:50 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:50 -0700
1#
2# LSST Data Management System
3# See COPYRIGHT file at the top of the source tree.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <https://www.lsstcorp.org/LegalNotices/>.
21#
23import math
24import pickle
25import unittest
27from lsst.sphgeom import Angle, LonLat, UnitVector3d, Vector3d
30class UnitVector3dTestCase(unittest.TestCase):
31 """Test 3D UnitVector."""
33 def testConstruction(self):
34 v = Vector3d(1, 1, 1)
35 u = UnitVector3d.orthogonalTo(v)
36 self.assertAlmostEqual(u.dot(v), 0.0, places=15)
37 a = UnitVector3d(1, 1, 1)
38 self.assertEqual(a, UnitVector3d(Vector3d(1, 1, 1)))
39 self.assertAlmostEqual(a.x(), math.sqrt(3.0) / 3.0, places=15)
40 self.assertAlmostEqual(a.y(), math.sqrt(3.0) / 3.0, places=15)
41 self.assertAlmostEqual(a.z(), math.sqrt(3.0) / 3.0, places=15)
42 b = UnitVector3d(Angle.fromDegrees(45), Angle.fromDegrees(45))
43 self.assertEqual(b, UnitVector3d(LonLat.fromDegrees(45, 45)))
44 self.assertAlmostEqual(b.x(), 0.5, places=15)
45 self.assertAlmostEqual(b.y(), 0.5, places=15)
46 self.assertAlmostEqual(b.z(), 0.5 * math.sqrt(2.0), places=15)
47 c = UnitVector3d.northFrom(b)
48 d = UnitVector3d(LonLat.fromDegrees(225, 45))
49 self.assertAlmostEqual(c.x(), d.x(), places=15)
50 self.assertAlmostEqual(c.y(), d.y(), places=15)
51 self.assertAlmostEqual(c.z(), d.z(), places=15)
53 def testComparison(self):
54 self.assertEqual(UnitVector3d.X(), UnitVector3d.X())
55 self.assertNotEqual(UnitVector3d.Y(), UnitVector3d.Z())
57 def testAccess(self):
58 v = UnitVector3d.X()
59 self.assertEqual(len(v), 3)
60 self.assertEqual(v[0], 1)
61 self.assertEqual(v[1], 0)
62 self.assertEqual(v[2], 0)
63 self.assertEqual(v[-3], 1)
64 self.assertEqual(v[-2], 0)
65 self.assertEqual(v[-1], 0)
66 with self.assertRaises(IndexError):
67 v[-4]
68 with self.assertRaises(IndexError):
69 v[3]
71 def testDot(self):
72 self.assertEqual(UnitVector3d.X().dot(UnitVector3d.Z()), 0)
74 def testCross(self):
75 self.assertEqual(UnitVector3d.X().cross(UnitVector3d.Y()), Vector3d(0, 0, 1))
76 self.assertEqual(UnitVector3d.X().robustCross(UnitVector3d.Y()), Vector3d(0, 0, 2))
78 def testArithmeticOperators(self):
79 self.assertEqual(-UnitVector3d.X(), UnitVector3d(-1, 0, 0))
80 self.assertEqual(UnitVector3d.X() - UnitVector3d.X(), Vector3d(0, 0, 0))
81 self.assertEqual(UnitVector3d.X() + UnitVector3d(1, 0, 0), UnitVector3d.X() * 2)
82 self.assertEqual(UnitVector3d.Y() - Vector3d(0, 0.5, 0), UnitVector3d.Y() / 2)
83 self.assertEqual(UnitVector3d.Z().cwiseProduct(Vector3d(2, 3, 4)), Vector3d(0, 0, 4))
85 def testRotation(self):
86 v = UnitVector3d.Y().rotatedAround(UnitVector3d.X(), Angle(0.5 * math.pi))
87 self.assertAlmostEqual(v.x(), 0.0, places=15)
88 self.assertAlmostEqual(v.y(), 0.0, places=15)
89 self.assertAlmostEqual(v.z(), 1.0, places=15)
91 def testString(self):
92 v = UnitVector3d.X()
93 self.assertEqual(str(v), "[1.0, 0.0, 0.0]")
94 self.assertEqual(repr(v), "UnitVector3d(1.0, 0.0, 0.0)")
95 self.assertEqual(v, eval(repr(v), {"UnitVector3d": UnitVector3d}))
97 def testPickle(self):
98 v = UnitVector3d(1, 1, 1)
99 w = pickle.loads(pickle.dumps(v, pickle.HIGHEST_PROTOCOL))
100 self.assertEqual(v, w)
103if __name__ == "__main__":
104 unittest.main()