Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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# 

22 

23import math 

24import pickle 

25import unittest 

26 

27from lsst.sphgeom import Angle, UnitVector3d, Vector3d 

28 

29 

30class Vector3dTestCase(unittest.TestCase): 

31 

32 def testConstruction(self): 

33 v = Vector3d(1, 2, 3) 

34 self.assertEqual(v.x(), 1) 

35 self.assertEqual(v.y(), 2) 

36 self.assertEqual(v.z(), 3) 

37 

38 def testComparison(self): 

39 v = Vector3d(1, 2, 3) 

40 self.assertEqual(v, Vector3d(1, 2, 3)) 

41 self.assertNotEqual(v, Vector3d(1, 2, 4)) 

42 

43 def testIsZero(self): 

44 self.assertTrue(Vector3d(0, 0, 0).isZero()) 

45 self.assertFalse(Vector3d(0, 0, 1).isZero()) 

46 

47 def testAccess(self): 

48 v = Vector3d(1, 2, 3) 

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

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

51 self.assertEqual(v[1], 2) 

52 self.assertEqual(v[2], 3) 

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

54 self.assertEqual(v[-2], 2) 

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

56 with self.assertRaises(IndexError): 

57 v[-4] 

58 with self.assertRaises(IndexError): 

59 v[3] 

60 

61 def testNormal(self): 

62 v = Vector3d(0, 2, 0) 

63 self.assertEqual(v.getSquaredNorm(), 4) 

64 self.assertEqual(v.getNorm(), 2) 

65 v.normalize() 

66 self.assertTrue(v.isNormalized()) 

67 self.assertEqual(v, Vector3d(0, 1, 0)) 

68 

69 def testDot(self): 

70 x = Vector3d(1, 0, 0) 

71 y = Vector3d(0, 1, 0) 

72 self.assertEqual(x.dot(y), 0) 

73 

74 def testCross(self): 

75 x = Vector3d(1, 0, 0) 

76 y = Vector3d(0, 1, 0) 

77 self.assertEqual(x.cross(y), Vector3d(0, 0, 1)) 

78 

79 def testArithmeticOperators(self): 

80 self.assertEqual(-Vector3d(1, 1, 1), Vector3d(-1, -1, -1)) 

81 self.assertEqual(Vector3d(1, 1, 1) * 2, Vector3d(2, 2, 2)) 

82 self.assertEqual(Vector3d(2, 2, 2) / 2, Vector3d(1, 1, 1)) 

83 self.assertEqual(Vector3d(1, 1, 1) + Vector3d(1, 1, 1), 

84 Vector3d(2, 2, 2)) 

85 self.assertEqual(Vector3d(1, 1, 1) - Vector3d(1, 1, 1), Vector3d()) 

86 v = Vector3d(1, 1, 1) 

87 v += Vector3d(3, 3, 3) 

88 v -= Vector3d(2, 2, 2) 

89 v *= 2.0 

90 v /= 4.0 

91 self.assertEqual(v, Vector3d(1, 1, 1)) 

92 self.assertEqual(v.cwiseProduct(Vector3d(2, 3, 4)), Vector3d(2, 3, 4)) 

93 

94 def testRotation(self): 

95 v = Vector3d(0, 1, 0).rotatedAround(UnitVector3d.X(), 

96 Angle(0.5 * math.pi)) 

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

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

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

100 

101 def testString(self): 

102 v = Vector3d(1, 0, 0) 

103 self.assertEqual(str(v), '[1.0, 0.0, 0.0]') 

104 self.assertEqual(repr(v), 'Vector3d(1.0, 0.0, 0.0)') 

105 self.assertEqual(v, eval(repr(v), dict(Vector3d=Vector3d))) 

106 

107 def testPickle(self): 

108 v = Vector3d(1, 2, 3) 

109 w = pickle.loads(pickle.dumps(v)) 

110 self.assertEqual(v, w) 

111 

112 

113if __name__ == '__main__': 113 ↛ 114line 113 didn't jump to line 114, because the condition on line 113 was never true

114 unittest.main()