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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

# 

# LSST Data Management System 

# See COPYRIGHT file at the top of the source tree. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

from __future__ import absolute_import, division, print_function 

 

import math 

import pickle 

import unittest 

 

from lsst.sphgeom import Angle, UnitVector3d, Vector3d 

 

 

class Vector3dTestCase(unittest.TestCase): 

 

def testConstruction(self): 

v = Vector3d(1, 2, 3) 

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

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

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

 

def testComparison(self): 

v = Vector3d(1, 2, 3) 

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

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

 

def testIsZero(self): 

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

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

 

def testAccess(self): 

v = Vector3d(1, 2, 3) 

self.assertEqual(len(v), 3) 

self.assertEqual(v[0], 1) 

self.assertEqual(v[1], 2) 

self.assertEqual(v[2], 3) 

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

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

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

with self.assertRaises(IndexError): 

v[-4] 

with self.assertRaises(IndexError): 

v[3] 

 

def testNormal(self): 

v = Vector3d(0, 2, 0) 

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

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

v.normalize() 

self.assertTrue(v.isNormalized()) 

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

 

def testDot(self): 

x = Vector3d(1, 0, 0) 

y = Vector3d(0, 1, 0) 

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

 

def testCross(self): 

x = Vector3d(1, 0, 0) 

y = Vector3d(0, 1, 0) 

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

 

def testArithmeticOperators(self): 

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

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

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

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

Vector3d(2, 2, 2)) 

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

v = Vector3d(1, 1, 1) 

v += Vector3d(3, 3, 3) 

v -= Vector3d(2, 2, 2) 

v *= 2.0 

v /= 4.0 

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

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

 

def testRotation(self): 

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

Angle(0.5 * math.pi)) 

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

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

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

 

def testString(self): 

v = Vector3d(1, 0, 0) 

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

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

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

 

def testPickle(self): 

v = Vector3d(1, 2, 3) 

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

self.assertEqual(v, w) 

 

 

114 ↛ 115line 114 didn't jump to line 115, because the condition on line 114 was never trueif __name__ == '__main__': 

unittest.main()