Coverage for tests/test_Vector3d.py: 21%
72 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
1# This file is part of sphgeom.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import math
29import pickle
30import unittest
32from lsst.sphgeom import Angle, UnitVector3d, Vector3d
35class Vector3dTestCase(unittest.TestCase):
36 """Test 3D vector."""
38 def testConstruction(self):
39 v = Vector3d(1, 2, 3)
40 self.assertEqual(v.x(), 1)
41 self.assertEqual(v.y(), 2)
42 self.assertEqual(v.z(), 3)
44 def testComparison(self):
45 v = Vector3d(1, 2, 3)
46 self.assertEqual(v, Vector3d(1, 2, 3))
47 self.assertNotEqual(v, Vector3d(1, 2, 4))
49 def testIsZero(self):
50 self.assertTrue(Vector3d(0, 0, 0).isZero())
51 self.assertFalse(Vector3d(0, 0, 1).isZero())
53 def testAccess(self):
54 v = Vector3d(1, 2, 3)
55 self.assertEqual(len(v), 3)
56 self.assertEqual(v[0], 1)
57 self.assertEqual(v[1], 2)
58 self.assertEqual(v[2], 3)
59 self.assertEqual(v[-3], 1)
60 self.assertEqual(v[-2], 2)
61 self.assertEqual(v[-1], 3)
62 with self.assertRaises(IndexError):
63 v[-4]
64 with self.assertRaises(IndexError):
65 v[3]
67 def testNormal(self):
68 v = Vector3d(0, 2, 0)
69 self.assertEqual(v.getSquaredNorm(), 4)
70 self.assertEqual(v.getNorm(), 2)
71 v.normalize()
72 self.assertTrue(v.isNormalized())
73 self.assertEqual(v, Vector3d(0, 1, 0))
75 def testDot(self):
76 x = Vector3d(1, 0, 0)
77 y = Vector3d(0, 1, 0)
78 self.assertEqual(x.dot(y), 0)
80 def testCross(self):
81 x = Vector3d(1, 0, 0)
82 y = Vector3d(0, 1, 0)
83 self.assertEqual(x.cross(y), Vector3d(0, 0, 1))
85 def testArithmeticOperators(self):
86 self.assertEqual(-Vector3d(1, 1, 1), Vector3d(-1, -1, -1))
87 self.assertEqual(Vector3d(1, 1, 1) * 2, Vector3d(2, 2, 2))
88 self.assertEqual(Vector3d(2, 2, 2) / 2, Vector3d(1, 1, 1))
89 self.assertEqual(Vector3d(1, 1, 1) + Vector3d(1, 1, 1), Vector3d(2, 2, 2))
90 self.assertEqual(Vector3d(1, 1, 1) - Vector3d(1, 1, 1), Vector3d())
91 v = Vector3d(1, 1, 1)
92 v += Vector3d(3, 3, 3)
93 v -= Vector3d(2, 2, 2)
94 v *= 2.0
95 v /= 4.0
96 self.assertEqual(v, Vector3d(1, 1, 1))
97 self.assertEqual(v.cwiseProduct(Vector3d(2, 3, 4)), Vector3d(2, 3, 4))
99 def testRotation(self):
100 v = Vector3d(0, 1, 0).rotatedAround(UnitVector3d.X(), Angle(0.5 * math.pi))
101 self.assertAlmostEqual(v.x(), 0.0, places=15)
102 self.assertAlmostEqual(v.y(), 0.0, places=15)
103 self.assertAlmostEqual(v.z(), 1.0, places=15)
105 def testString(self):
106 v = Vector3d(1, 0, 0)
107 self.assertEqual(str(v), "[1.0, 0.0, 0.0]")
108 self.assertEqual(repr(v), "Vector3d(1.0, 0.0, 0.0)")
109 self.assertEqual(v, eval(repr(v), {"Vector3d": Vector3d}))
111 def testPickle(self):
112 v = Vector3d(1, 2, 3)
113 w = pickle.loads(pickle.dumps(v))
114 self.assertEqual(v, w)
117if __name__ == "__main__":
118 unittest.main()