Files
pylib/tests/test_geometry.py

150 lines
4.1 KiB
Python

"""Test of geometry module.
:Date: 2019-12-21
.. module:: test_geometry
:platform: *nix, Windows
:synopsis: Test of geometry module.
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
import unittest
import os
import sys
import math
sys.path.insert(0, os.path.abspath('..'))
from pylib.geometry import Direction, Point, CS, Wireframe, Line, Polygon, Circle
class TestGeometry(unittest.TestCase):
def test_Direction(self):
p = Direction()
self.assertIsInstance(
p,
list)
self.assertEqual(
p,
[1, 0, 0, 0])
self.assertEqual(
Direction(2, 3, 4),
[2, 3, 4, 0])
def test_Direction_getitem(self):
self.assertEqual(
Direction()[:3],
[1, 0, 0])
self.assertEqual(
Direction(1, -1, 1)[:3],
[1, -1, 1])
def test_Point(self):
p = Point()
self.assertIsInstance(
p,
list)
self.assertEqual(
p,
[0, 0, 0, 1])
self.assertEqual(
Point(2, 3, 4),
[2, 3, 4, 1])
def test_Point_getitem(self):
self.assertEqual(
Point()[:3],
[0, 0, 0])
self.assertEqual(
Point(1, -1, 1)[:3],
[1, -1, 1])
def test_Point_rotate(self):
[self.assertAlmostEqual(i, j) for i, j in
zip(Point(1, 1, 1).rotate_x(math.pi/4), [1, 0, 1.414213562373095, 1])]
def test_CS(self):
self.assertEqual(
CS(),
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
def test_CS_rotate(self):
[self.assertAlmostEqual(k, l) for i, j in
zip(CS().rotate_y(-math.pi/2),
[[0, 0.0, -1.0, 0.0], [0, 1, 0, 0], [1.0, 0.0, 0, 0.0], [0, 0, 0, 1]])
for k, l in zip(i, j)]
def test_Wireframe(self):
self.assertEqual(
Wireframe().points(),
[])
# it is possible to assign other objects then Points but/and the
# class will not test if Points are used, this is just assumed
self.assertEqual(
Wireframe([]).points(),
[[]])
self.assertEqual(
Wireframe(Point()).points(),
[[0, 0, 0, 1]])
def test_Line(self):
self.assertEqual(
Line().points(),
[[-1, 0, 0, 1], [1, 0, 0, 1]])
def test_Polygon(self):
p0 = Point()
p1 = Point(1, 0, 0)
p2 = Point(1, 1, 1)
p3 = Point(0, 1, 1)
pg = Polygon(p0, p1, p2, p3)
self.assertEqual(
pg.points(),
[[0, 0, 0, 1], [1, 0 , 0, 1], [1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 0, 1]])
def test_Polygon_rotate(self):
pg = Polygon(Point(1, 1, 1)).rotate_x(math.pi/4)
[self.assertAlmostEqual(i, j) for i, j in
zip(pg.points()[0], [1, 0, 1.414213562373095, 1])]
[self.assertAlmostEqual(k, l) for i, j in
zip(pg.xyz(), ((1, 1), (0, 0), (1.414213562373095, 1.414213562373095)))
for k, l in zip(i, j)]
def test_Polygon_ch_cs(self):
# example object to rotate
p0 = Point()
p1 = Point(1, 0, 0)
p2 = Point(1, 1, 1)
p3 = Point(0, 1, 1)
theta = math.pi/2
test = [[0.0, 0.0, 0.0, 1], [0, 0.0, -1.0, 1], [1.0, 1.0, -1, 1],
[1.0, 1.0, 0, 1], [0.0, 0.0, 0.0, 1]]
# using a coordinate system object
pg = Polygon(p0, p1, p2, p3)
cs = CS().rotate_y(theta)
pg.ch_cs(cs)
[self.assertAlmostEqual(i, j) for i, j in zip(pg.points(), test)]
def test_Circle(self):
cs = Circle(n=4).points()
test = [[1, 0, 0, 1], [0, 1, 0, 1], [-1, 0, 0, 1], [0, -1, 0 ,1], [1, 0, 0, 1]]
[self.assertAlmostEqual(i, j) for i, j in zip(cs, test)]
cs = Circle().points()
test = [[1.0, 0.0, 0, 1],
[0.8090169943749475, 0.5877852522924731, 0, 1],
[0.30901699437494745, 0.9510565162951535, 0, 1],
[-0.30901699437494734, 0.9510565162951536, 0, 1],
[-0.8090169943749473, 0.5877852522924732, 0, 1],
[-1.0, 1.2246467991473532e-16, 0, 1],
[-0.8090169943749476, -0.587785252292473, 0, 1],
[-0.30901699437494756, -0.9510565162951535, 0, 1],
[0.30901699437494723, -0.9510565162951536, 0, 1],
[0.8090169943749473, -0.5877852522924734, 0, 1]]
[self.assertAlmostEqual(k, l) for i, j in zip(cs, test) for k, l in zip(i, j) ]
if __name__ == '__main__':
unittest.main(verbosity=2)