0f3884c507524dcd3f0527ff2699a6419d6916b3
1 # Copyright 2014 MINES ParisTech
3 # This file is part of LinPy.
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.
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.
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/>.
21 from ..geometry
import *
22 from ..linexprs
import Symbol
23 from ..polyhedra
import Eq
26 class TestPoint(unittest
.TestCase
):
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})
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)]),
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):
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()),
66 def test_isorigin(self
):
67 self
.assertFalse(self
.pt1
.isorigin())
68 self
.assertTrue(Point({}).isorigin())
71 self
.assertTrue(self
.pt1
)
72 self
.assertFalse(Point({}))
75 self
.assertEqual(self
.pt1
+ self
.vec1
,
76 Point({self
.x
: 30, self
.y
: 35, self
.z
: 41}))
77 with self
.assertRaises(TypeError):
81 self
.assertEqual(self
.pt1
- self
.pt2
,
82 Vector({self
.x
: -5, self
.y
: -35, self
.z
: -59}))
83 self
.assertEqual(self
.pt1
- self
.vec1
,
84 Point({self
.x
: -10, self
.y
: -25, self
.z
: -39}))
87 self
.assertEqual(self
.pt1
, self
.pt1
)
88 self
.assertNotEqual(self
.pt1
, self
.pt2
)
89 self
.assertNotEqual(self
.pt1
, self
.vec1
)
91 def test_aspolyhedron(self
):
92 self
.assertEqual(self
.pt1
.aspolyhedron(),
93 Eq(self
.x
, 10) & Eq(self
.y
, 5) & Eq(self
.z
, 1))
96 self
.assertEqual(repr(self
.pt1
), 'Point({x: 10, y: 5, z: 1})')
99 class TestVector(unittest
.TestCase
):
105 self
.pt1
= Point({self
.x
: 10, self
.y
: 5, self
.z
: 1})
106 self
.pt2
= Point({self
.x
: 15, self
.y
: 40, self
.z
: 60})
107 self
.vec1
= Vector({self
.x
: 20, self
.y
: 30, self
.z
: 40})
108 self
.vec2
= Vector({self
.x
: 45, self
.y
: 70, self
.z
: 80})
111 self
.assertEqual(self
.vec1
+ self
.pt1
, Point({self
.x
: 30, self
.y
: 35, self
.z
: 41}))
112 self
.assertEqual(self
.vec1
+ self
.vec2
, Vector({self
.x
: 65, self
.y
: 100, self
.z
: 120}))
114 def test_angle(self
):
115 self
.assertEqual(math
.degrees(self
.vec1
.angle(self
.vec1
)), 0)
116 self
.assertAlmostEqual(math
.degrees(self
.vec1
.angle(self
.vec2
)), 4.15129, places
=5)
117 self
.assertAlmostEqual(math
.degrees(self
.vec2
.angle(self
.vec1
)), 4.15129, places
=5)
119 def test_cross(self
):
120 self
.assertEqual(self
.vec1
.cross(self
.vec2
), Vector({self
.x
: -400, self
.y
: 200, self
.z
: 50}))
123 self
.assertEqual(self
.vec1
/ 10, Vector({self
.x
: 2, self
.y
: 3, self
.z
: 4}))
126 self
.assertEqual(self
.vec1
.dot(self
.vec2
), 6200)
129 self
.assertEqual(self
.vec1
, self
.vec1
)
130 self
.assertNotEqual(self
.vec1
, self
.vec2
)
133 self
.assertEqual(75 * self
.vec1
, Vector({self
.x
: 1500, self
.y
: 2250, self
.z
: 3000}))
134 self
.assertEqual(self
.vec1
* 75, Vector({self
.x
: 1500, self
.y
: 2250, self
.z
: 3000}))
137 self
.assertEqual(-self
.vec1
, Vector({self
.x
: -20, self
.y
: -30, self
.z
: -40}))
140 self
.assertAlmostEqual(self
.vec1
.norm(), 53.85165, places
=5)
142 def test_norm2(self
):
143 self
.assertEqual(self
.vec1
.norm2(), 2900)
145 def test_asunit(self
):
146 unit
= self
.vec1
.asunit()
147 self
.assertAlmostEqual(unit
[self
.x
], 0.37139, 5)
148 self
.assertAlmostEqual(unit
[self
.y
], 0.55709, 5)
149 self
.assertAlmostEqual(unit
[self
.z
], 0.74278, 5)
152 self
.assertEqual(self
.vec1
- self
.pt1
, Point({self
.x
: 10, self
.y
: 25, self
.z
: 39}))
153 self
.assertEqual(self
.vec1
- self
.vec2
, Vector({self
.x
: -25, self
.y
: -40, self
.z
: -40}))