Coverage for tests/test_UnitVector3d.py: 22%
67 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-09 10:49 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-09 10:49 +0000
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 def testConstruction(self):
32 v = Vector3d(1, 1, 1)
33 u = UnitVector3d.orthogonalTo(v)
34 self.assertAlmostEqual(u.dot(v), 0.0, places=15)
35 a = UnitVector3d(1, 1, 1)
36 self.assertEqual(a, UnitVector3d(Vector3d(1, 1, 1)))
37 self.assertAlmostEqual(a.x(), math.sqrt(3.0) / 3.0, places=15)
38 self.assertAlmostEqual(a.y(), math.sqrt(3.0) / 3.0, places=15)
39 self.assertAlmostEqual(a.z(), math.sqrt(3.0) / 3.0, places=15)
40 b = UnitVector3d(Angle.fromDegrees(45), Angle.fromDegrees(45))
41 self.assertEqual(b, UnitVector3d(LonLat.fromDegrees(45, 45)))
42 self.assertAlmostEqual(b.x(), 0.5, places=15)
43 self.assertAlmostEqual(b.y(), 0.5, places=15)
44 self.assertAlmostEqual(b.z(), 0.5 * math.sqrt(2.0), places=15)
45 c = UnitVector3d.northFrom(b)
46 d = UnitVector3d(LonLat.fromDegrees(225, 45))
47 self.assertAlmostEqual(c.x(), d.x(), places=15)
48 self.assertAlmostEqual(c.y(), d.y(), places=15)
49 self.assertAlmostEqual(c.z(), d.z(), places=15)
51 def testComparison(self):
52 self.assertEqual(UnitVector3d.X(), UnitVector3d.X())
53 self.assertNotEqual(UnitVector3d.Y(), UnitVector3d.Z())
55 def testAccess(self):
56 v = UnitVector3d.X()
57 self.assertEqual(len(v), 3)
58 self.assertEqual(v[0], 1)
59 self.assertEqual(v[1], 0)
60 self.assertEqual(v[2], 0)
61 self.assertEqual(v[-3], 1)
62 self.assertEqual(v[-2], 0)
63 self.assertEqual(v[-1], 0)
64 with self.assertRaises(IndexError):
65 v[-4]
66 with self.assertRaises(IndexError):
67 v[3]
69 def testDot(self):
70 self.assertEqual(UnitVector3d.X().dot(UnitVector3d.Z()), 0)
72 def testCross(self):
73 self.assertEqual(UnitVector3d.X().cross(UnitVector3d.Y()), Vector3d(0, 0, 1))
74 self.assertEqual(UnitVector3d.X().robustCross(UnitVector3d.Y()), Vector3d(0, 0, 2))
76 def testArithmeticOperators(self):
77 self.assertEqual(-UnitVector3d.X(), UnitVector3d(-1, 0, 0))
78 self.assertEqual(UnitVector3d.X() - UnitVector3d.X(), Vector3d(0, 0, 0))
79 self.assertEqual(UnitVector3d.X() + UnitVector3d(1, 0, 0), UnitVector3d.X() * 2)
80 self.assertEqual(UnitVector3d.Y() - Vector3d(0, 0.5, 0), UnitVector3d.Y() / 2)
81 self.assertEqual(UnitVector3d.Z().cwiseProduct(Vector3d(2, 3, 4)), Vector3d(0, 0, 4))
83 def testRotation(self):
84 v = UnitVector3d.Y().rotatedAround(UnitVector3d.X(), Angle(0.5 * math.pi))
85 self.assertAlmostEqual(v.x(), 0.0, places=15)
86 self.assertAlmostEqual(v.y(), 0.0, places=15)
87 self.assertAlmostEqual(v.z(), 1.0, places=15)
89 def testString(self):
90 v = UnitVector3d.X()
91 self.assertEqual(str(v), "[1.0, 0.0, 0.0]")
92 self.assertEqual(repr(v), "UnitVector3d(1.0, 0.0, 0.0)")
93 self.assertEqual(v, eval(repr(v), dict(UnitVector3d=UnitVector3d)))
95 def testPickle(self):
96 v = UnitVector3d(1, 1, 1)
97 w = pickle.loads(pickle.dumps(v, pickle.HIGHEST_PROTOCOL))
98 self.assertEqual(v, w)
101if __name__ == "__main__": 101 ↛ 102line 101 didn't jump to line 102, because the condition on line 101 was never true
102 unittest.main()