Coverage for tests/test_UnitVector3d.py: 22%
67 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-02 18:10 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-02 18:10 -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 pickle
25import math
26import unittest
28from lsst.sphgeom import Angle, LonLat, UnitVector3d, Vector3d
31class UnitVector3dTestCase(unittest.TestCase):
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()),
76 Vector3d(0, 0, 1))
77 self.assertEqual(UnitVector3d.X().robustCross(UnitVector3d.Y()),
78 Vector3d(0, 0, 2))
80 def testArithmeticOperators(self):
81 self.assertEqual(-UnitVector3d.X(), UnitVector3d(-1, 0, 0))
82 self.assertEqual(UnitVector3d.X() - UnitVector3d.X(), Vector3d(0, 0, 0))
83 self.assertEqual(UnitVector3d.X() + UnitVector3d(1, 0, 0),
84 UnitVector3d.X() * 2)
85 self.assertEqual(UnitVector3d.Y() - Vector3d(0, 0.5, 0),
86 UnitVector3d.Y() / 2)
87 self.assertEqual(UnitVector3d.Z().cwiseProduct(Vector3d(2, 3, 4)),
88 Vector3d(0, 0, 4))
90 def testRotation(self):
91 v = UnitVector3d.Y().rotatedAround(UnitVector3d.X(),
92 Angle(0.5 * math.pi))
93 self.assertAlmostEqual(v.x(), 0.0, places=15)
94 self.assertAlmostEqual(v.y(), 0.0, places=15)
95 self.assertAlmostEqual(v.z(), 1.0, places=15)
97 def testString(self):
98 v = UnitVector3d.X()
99 self.assertEqual(str(v), '[1.0, 0.0, 0.0]')
100 self.assertEqual(repr(v), 'UnitVector3d(1.0, 0.0, 0.0)')
101 self.assertEqual(v, eval(repr(v), dict(UnitVector3d=UnitVector3d)))
103 def testPickle(self):
104 v = UnitVector3d(1, 1, 1)
105 w = pickle.loads(pickle.dumps(v, pickle.HIGHEST_PROTOCOL))
106 self.assertEqual(v, w)
109if __name__ == '__main__': 109 ↛ 110line 109 didn't jump to line 110, because the condition on line 109 was never true
110 unittest.main()