1 # Copyright 2014 MINES ParisTech
2 #
3 # This file is part of LinPy.
4 #
5 # LinPy is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # LinPy is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with LinPy. If not, see <http://www.gnu.org/licenses/>.
18 import math
19 import unittest
21 from ..geometry import Point, Vector
22 from ..linexprs import Symbol
23 from ..polyhedra import Eq
26 class TestPoint(unittest.TestCase):
28 def setUp(self):
29 self.x = Symbol('x')
30 self.y = Symbol('y')
31 self.z = Symbol('z')
32 self.t = Symbol('t')
33 self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
34 self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
35 self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
37 def test_new(self):
38 self.assertEqual(Point({self.x: 10, self.y: 5, self.z: 1}), self.pt1)
39 self.assertEqual(Point([(self.x, 10), (self.y, 5), (self.z, 1)]),
40 self.pt1)
42 def test_symbols(self):
43 self.assertTupleEqual(self.pt1.symbols, (self.x, self.y, self.z))
45 def test_dimension(self):
46 self.assertEqual(self.pt1.dimension, 3)
48 def test_coordinate(self):
49 self.assertEqual(self.pt1.coordinate(self.x), 10)
50 with self.assertRaises(KeyError):
51 self.pt1.coordinate(self.t)
53 def test_getitem(self):
54 self.assertEqual(self.pt1[self.x], 10)
55 with self.assertRaises(KeyError):
56 self.pt1[self.t]
58 def test_coordinates(self):
59 self.assertListEqual(list(self.pt1.coordinates()),
60 [(self.x, 10), (self.y, 5), (self.z, 1)])
62 def test_values(self):
63 self.assertListEqual(list(self.pt1.values()), [10, 5, 1])
65 def test_isorigin(self):
66 self.assertFalse(self.pt1.isorigin())
67 self.assertTrue(Point({}).isorigin())
69 def test_bool(self):
70 self.assertTrue(self.pt1)
71 self.assertFalse(Point({}))
73 def test_add(self):
74 self.assertEqual(self.pt1 + self.vec1,
75 Point({self.x: 30, self.y: 35, self.z: 41}))
76 with self.assertRaises(TypeError):
77 self.pt1 + self.pt2
79 def test_sub(self):
80 self.assertEqual(self.pt1 - self.pt2,
81 Vector({self.x: -5, self.y: -35, self.z: -59}))
82 self.assertEqual(self.pt1 - self.vec1,
83 Point({self.x: -10, self.y: -25, self.z: -39}))
85 def test_eq(self):
86 self.assertEqual(self.pt1, self.pt1)
87 self.assertNotEqual(self.pt1, self.pt2)
88 self.assertNotEqual(self.pt1, self.vec1)
90 def test_aspolyhedron(self):
91 self.assertEqual(self.pt1.aspolyhedron(),
92 Eq(self.x, 10) & Eq(self.y, 5) & Eq(self.z, 1))
94 def test_repr(self):
95 self.assertEqual(repr(self.pt1), 'Point({x: 10, y: 5, z: 1})')
98 class TestVector(unittest.TestCase):
100 def setUp(self):
101 self.x = Symbol('x')
102 self.y = Symbol('y')
103 self.z = Symbol('z')
104 self.pt1 = Point({self.x: 10, self.y: 5, self.z: 1})
105 self.pt2 = Point({self.x: 15, self.y: 40, self.z: 60})
106 self.vec1 = Vector({self.x: 20, self.y: 30, self.z: 40})
107 self.vec2 = Vector({self.x: 45, self.y: 70, self.z: 80})
109 def test_add(self):
110 self.assertEqual(self.vec1 + self.pt1,
111 Point({self.x: 30, self.y: 35, self.z: 41}))
112 self.assertEqual(self.vec1 + self.vec2,
113 Vector({self.x: 65, self.y: 100, self.z: 120}))
115 def test_angle(self):
116 self.assertEqual(math.degrees(self.vec1.angle(self.vec1)), 0)
117 self.assertAlmostEqual(math.degrees(self.vec1.angle(self.vec2)),
118 4.15129, places=5)
119 self.assertAlmostEqual(math.degrees(self.vec2.angle(self.vec1)),
120 4.15129, places=5)
122 def test_cross(self):
123 self.assertEqual(self.vec1.cross(self.vec2),
124 Vector({self.x: -400, self.y: 200, self.z: 50}))
126 def test_div(self):
127 self.assertEqual(self.vec1 / 10,
128 Vector({self.x: 2, self.y: 3, self.z: 4}))
130 def test_dot(self):
131 self.assertEqual(self.vec1.dot(self.vec2), 6200)
133 def test_eq(self):
134 self.assertEqual(self.vec1, self.vec1)
135 self.assertNotEqual(self.vec1, self.vec2)
137 def test_mul(self):
138 self.assertEqual(75 * self.vec1,
139 Vector({self.x: 1500, self.y: 2250, self.z: 3000}))
140 self.assertEqual(self.vec1 * 75,
141 Vector({self.x: 1500, self.y: 2250, self.z: 3000}))
143 def test_neg(self):
144 self.assertEqual(-self.vec1,
145 Vector({self.x: -20, self.y: -30, self.z: -40}))
147 def test_norm(self):
148 self.assertAlmostEqual(self.vec1.norm(), 53.85165, places=5)
150 def test_norm2(self):
151 self.assertEqual(self.vec1.norm2(), 2900)
153 def test_asunit(self):
154 unit = self.vec1.asunit()
155 self.assertAlmostEqual(unit[self.x], 0.37139, 5)
156 self.assertAlmostEqual(unit[self.y], 0.55709, 5)
157 self.assertAlmostEqual(unit[self.z], 0.74278, 5)
159 def test_sub(self):
160 self.assertEqual(self.vec1 - self.pt1,
161 Point({self.x: 10, self.y: 25, self.z: 39}))
162 self.assertEqual(self.vec1 - self.vec2,
163 Vector({self.x: -25, self.y: -40, self.z: -40}))