Coverage for tests/test_UnitVector3d.py: 20%

65 statements  

« 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/>. 

27 

28import math 

29import pickle 

30import unittest 

31 

32from lsst.sphgeom import Angle, LonLat, UnitVector3d, Vector3d 

33 

34 

35class UnitVector3dTestCase(unittest.TestCase): 

36 """Test 3D UnitVector.""" 

37 

38 def testConstruction(self): 

39 v = Vector3d(1, 1, 1) 

40 u = UnitVector3d.orthogonalTo(v) 

41 self.assertAlmostEqual(u.dot(v), 0.0, places=15) 

42 a = UnitVector3d(1, 1, 1) 

43 self.assertEqual(a, UnitVector3d(Vector3d(1, 1, 1))) 

44 self.assertAlmostEqual(a.x(), math.sqrt(3.0) / 3.0, places=15) 

45 self.assertAlmostEqual(a.y(), math.sqrt(3.0) / 3.0, places=15) 

46 self.assertAlmostEqual(a.z(), math.sqrt(3.0) / 3.0, places=15) 

47 b = UnitVector3d(Angle.fromDegrees(45), Angle.fromDegrees(45)) 

48 self.assertEqual(b, UnitVector3d(LonLat.fromDegrees(45, 45))) 

49 self.assertAlmostEqual(b.x(), 0.5, places=15) 

50 self.assertAlmostEqual(b.y(), 0.5, places=15) 

51 self.assertAlmostEqual(b.z(), 0.5 * math.sqrt(2.0), places=15) 

52 c = UnitVector3d.northFrom(b) 

53 d = UnitVector3d(LonLat.fromDegrees(225, 45)) 

54 self.assertAlmostEqual(c.x(), d.x(), places=15) 

55 self.assertAlmostEqual(c.y(), d.y(), places=15) 

56 self.assertAlmostEqual(c.z(), d.z(), places=15) 

57 

58 def testComparison(self): 

59 self.assertEqual(UnitVector3d.X(), UnitVector3d.X()) 

60 self.assertNotEqual(UnitVector3d.Y(), UnitVector3d.Z()) 

61 

62 def testAccess(self): 

63 v = UnitVector3d.X() 

64 self.assertEqual(len(v), 3) 

65 self.assertEqual(v[0], 1) 

66 self.assertEqual(v[1], 0) 

67 self.assertEqual(v[2], 0) 

68 self.assertEqual(v[-3], 1) 

69 self.assertEqual(v[-2], 0) 

70 self.assertEqual(v[-1], 0) 

71 with self.assertRaises(IndexError): 

72 v[-4] 

73 with self.assertRaises(IndexError): 

74 v[3] 

75 

76 def testDot(self): 

77 self.assertEqual(UnitVector3d.X().dot(UnitVector3d.Z()), 0) 

78 

79 def testCross(self): 

80 self.assertEqual(UnitVector3d.X().cross(UnitVector3d.Y()), Vector3d(0, 0, 1)) 

81 self.assertEqual(UnitVector3d.X().robustCross(UnitVector3d.Y()), Vector3d(0, 0, 2)) 

82 

83 def testArithmeticOperators(self): 

84 self.assertEqual(-UnitVector3d.X(), UnitVector3d(-1, 0, 0)) 

85 self.assertEqual(UnitVector3d.X() - UnitVector3d.X(), Vector3d(0, 0, 0)) 

86 self.assertEqual(UnitVector3d.X() + UnitVector3d(1, 0, 0), UnitVector3d.X() * 2) 

87 self.assertEqual(UnitVector3d.Y() - Vector3d(0, 0.5, 0), UnitVector3d.Y() / 2) 

88 self.assertEqual(UnitVector3d.Z().cwiseProduct(Vector3d(2, 3, 4)), Vector3d(0, 0, 4)) 

89 

90 def testRotation(self): 

91 v = UnitVector3d.Y().rotatedAround(UnitVector3d.X(), Angle(0.5 * math.pi)) 

92 self.assertAlmostEqual(v.x(), 0.0, places=15) 

93 self.assertAlmostEqual(v.y(), 0.0, places=15) 

94 self.assertAlmostEqual(v.z(), 1.0, places=15) 

95 

96 def testString(self): 

97 v = UnitVector3d.X() 

98 self.assertEqual(str(v), "[1.0, 0.0, 0.0]") 

99 self.assertEqual(repr(v), "UnitVector3d(1.0, 0.0, 0.0)") 

100 self.assertEqual(v, eval(repr(v), {"UnitVector3d": UnitVector3d})) 

101 

102 def testPickle(self): 

103 v = UnitVector3d(1, 1, 1) 

104 w = pickle.loads(pickle.dumps(v, pickle.HIGHEST_PROTOCOL)) 

105 self.assertEqual(v, w) 

106 

107 

108if __name__ == "__main__": 

109 unittest.main()