Files
pylib/tests/test_geometry2d.py

260 lines
10 KiB
Python

"""Test of geometry2d module.
:Date: 2019-08-20
.. module:: test_geometry2d
:platform: *nix, Windows
:synopsis: Test of geometry2d module.
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
import unittest
import os
import sys
import math
from numpy import allclose
sys.path.insert(0, os.path.abspath('..'))
from pylib.data import fold_list
from pylib.geometry2d import translate_xy, rotate_xy, interpolate_hermite, lines, cubics
from pylib.geometry2d_plot import plot_lines, plot_cubic_lines
class TestGeometry2d(unittest.TestCase):
global_points = [(0, 0),
(0, 3),
(3, 3),
(3, 0),
(3, 6),
(5, 6),
(5, 3),
(5, 0),
(7, 0)]
inc = [[1, 2],
[2, 3],
[4, 3],
[3, 5],
[5, 6],
[6, 7],
[7, 8],
[9, 7]]
# global deformation, horizontal right, vertical down, rotation
U = [
0.00000, 0.00000, -5.02012,
12.77012, 0.06333, -2.72988,
12.69350, 0.08599, 3.14685,
0.00000, 0.00000, -7.92017,
-12.39817, 0.17450, 4.14446,
-12.43490, 0.32001, -3.10697,
-26.77684, 0.16528, 2.98214,
0.00000, 0.00000, 0.00000,
-26.50991, 0.00000, -1.36053,
]
U = [Ui/1000 for Ui in U]
# global deformation, only horizontal right and vertical down
U2 = [ui for i, ui in enumerate(U) if i%3 != 2]
# global deformation, horizontal right, vertical up, rotation
UN = [Ui if i%3 != 1 else -Ui for i, Ui in enumerate(U)]
# local deformation, horizontal right, vertical down, rotation
u = [
[ 0.000000, 0.000000, -0.005020, -0.000063, 0.012770, -0.002730],
[ 0.012770, 0.000063, -0.002730, 0.012693, 0.000086, 0.003147],
[ 0.000000, 0.000000, -0.007920, -0.000086, 0.012693, 0.003147],
[-0.000086, 0.012693, 0.003147, -0.000175, -0.012398, 0.004144],
[-0.012398, 0.000175, 0.004144, -0.012435, 0.000320, -0.003107],
[ 0.000320, 0.012435, -0.003107, 0.000165, 0.026777, 0.002982],
[ 0.000165, 0.026777, 0.002982, 0.000000, 0.000000, 0.000000],
[ 0.014705, -0.022058, -0.001361, 0.014716, -0.022371, 0.002982],
]
# local deformation, horizontal right, vertical up, rotation
uN = [[uii if i%3 != 1 else -uii for i, uii in enumerate(ui)] for ui in u]
# exaggeration factor for the deformation
factor = 20
# test of cubic lines
test_lns_l = [((0, 0), (0, 3), [0.0, -0.0, -0.1004, -0.00126, -0.2554, -0.0546]),
((0, 3), (3, 3), [0.2554, -0.00126, -0.0546, 0.25386, -0.00172, 0.06294]),
((3, 0), (3, 3), [0.0, -0.0, -0.1584, -0.00172, -0.25386, 0.06294]),
((3, 3), (3, 6), [-0.00172, -0.25386, 0.06294, -0.0035, 0.24796, 0.08288]),
((3, 6), (5, 6), [-0.24796, -0.0035, 0.08288, -0.2487, -0.0064, -0.06214]),
((5, 6), (5, 3), [0.0064, -0.2487, -0.06214, 0.0033, -0.53554, 0.05964]),
((5, 3), (5, 0), [0.0033, -0.53554, 0.05964, 0.0, -0.0, 0.0]),
((7, 0), (5, 3), [0.2941, 0.44116, -0.02722, 0.29432, 0.44742, 0.05964])]
test_lns_g = [((0, 0), (0, 3), [0.0, -0.0, -0.1004024, -0.0012666, -0.2554024, -0.0545976]),
((0, 3), (3, 3), [0.2554024, -0.0012666, -0.0545976, 0.25387, -0.0017198, 0.062937]),
((3, 0), (3, 3), [0.0, -0.0, -0.1584034, -0.0017198, -0.25387, 0.062937]),
((3, 3), (3, 6), [-0.0017198, -0.25387, 0.062937, -0.00349, 0.2479634, 0.0828892]),
((3, 6), (5, 6), [-0.2479634, -0.00349, 0.0828892, -0.248698, -0.0064002, -0.0621394]),
((5, 6), (5, 3), [0.0064002, -0.248698, -0.0621394, 0.0033056, -0.5355368, 0.0596428]),
((5, 3), (5, 0), [0.0033056, -0.5355368, 0.0596428, 0.0, 0.0, 0.0]),
((7, 0), (5, 3), [0.2941010455782632, 0.441151568367395, -0.0272106, 0.29431194259286797, 0.4474271690373891, 0.0596428])]
def test_lines(self):
"""test lines function (undeformed structure)"""
lns = lines(self.global_points, inc=self.inc, index_offset=1)
test = [((0, 0), (0, 3)), ((0, 3), (3, 3)), ((3, 0), (3, 3)),
((3, 3), (3, 6)), ((3, 6), (5, 6)), ((5, 6), (5, 3)),
((5, 3), (5, 0)), ((7, 0), (5, 3))]
self.assertEqual(lns, test)
plot_lines(lns, linestyle=':')
def test_lines_deformation(self):
"""test lines function with deformation (deformed structure)"""
lns = lines(self.global_points, deformation=fold_list(self.U2, 2), factor=self.factor, inc=self.inc, index_offset=1)
test = [((0.0, 0.0), (0.25540240000000003, 3.0012666)), ((0.25540240000000003, 3.0012666), (3.25387, 3.0017198)), ((3.0, 0.0), (3.25387, 3.0017198)), ((3.25387, 3.0017198), (2.7520366, 6.00349)), ((2.7520366, 6.00349), (4.751302, 6.0064002)), ((4.751302, 6.0064002), (4.4644632, 3.0033056)), ((4.4644632, 3.0033056), (5.0, 0.0)), ((6.4698018, 0.0), (4.4644632, 3.0033056))]
self.assertEqual(lns, test)
plot_lines(lns, color='C2')
def test_translate_xy(self):
"""test translate_xy function with single values"""
x = 2
y = 4
xp, yp = translate_xy((0.5, 1), x, y)
xt = 2.5
yt = 5
self.assertEqual(xp, xt)
self.assertEqual(yp, yt)
def test_translate_xy_list(self):
"""test translate_xy function"""
x = [ 0. , 0.33333333, 0.66666667, 1. , 1.33333333,
1.66666667, 2. , 2.33333333, 2.66666667, 3. ]
y = [ 6.30000000e-05, -7.58828532e-04, -1.39770233e-03,
-1.84370370e-03, -2.08691495e-03, -2.11741838e-03,
-1.92529630e-03, -1.50063100e-03, -8.33504801e-04,
8.60000000e-05]
xp, yp = translate_xy((1, 0.001), x, y)
xt = [1.0,
1.3333333333333333,
1.6666666666666665,
2.0,
2.333333333333333,
2.666666666666667,
3.0,
3.333333333333333,
3.6666666666666665,
4.0]
yt = [0.0010629999999999999,
0.00024117146776406044,
-0.00039770233196159102,
-0.0008437037037037035,
-0.0010869149519890263,
-0.0011174183813443071,
-0.00092529629629629624,
-0.00050063100137174286,
0.0001664951989026062,
0.0010859999999999999]
self.assertTrue(allclose(xp, xt))
self.assertTrue(allclose(yp, yt))
def test_rotate_xy(self):
"""test translate_xy function with single values"""
x = 2
y = 4
xp, yp = rotate_xy((0.5, 1), math.pi/2, x, y)
xt = -2.5
yt = 2.5
self.assertEqual(xp, xt)
self.assertEqual(yp, yt)
def test_rotate_xy_list(self):
"""test translate_xy function"""
x = [ 0. , 0.33333333, 0.66666667, 1. , 1.33333333,
1.66666667, 2. , 2.33333333, 2.66666667, 3. ]
y = [ 6.30000000e-05, -7.58828532e-04, -1.39770233e-03,
-1.84370370e-03, -2.08691495e-03, -2.11741838e-03,
-1.92529630e-03, -1.50063100e-03, -8.33504801e-04,
8.60000000e-05]
xp, yp = rotate_xy((0, 0), math.pi/2, x, y)
xt = [-6.3e-05,
0.00075882853223595997,
0.0013977023319616318,
0.0018437037037037647,
0.0020869149519891078,
0.002117418381344409,
0.0019252962962964188,
0.0015006310013718858,
0.0008335048010975571,
-8.5999999999816312e-05]
yt = [3.8576374173141622e-21,
0.33333333333333331,
0.66666666666666663,
1.0,
1.3333333333333333,
1.6666666666666667,
2.0,
2.333333333333333,
2.6666666666666665,
3.0]
self.assertTrue(allclose(xp, xt))
self.assertTrue(allclose(yp, yt))
def test_interpolate_hermite(self):
"""test translate_xy function"""
# u:
# - left side displacement to right
# - left side rotation counterclockwise
# - right side displacement to right
# - right side rotation counterclockwise
u = [ 0.012770, 0.000063, -0.002730, 0.012693, 0.000086, 0.003147]
x, y = interpolate_hermite(u[1], u[2], u[4], u[5], scale_x=3)
xt = [ 0. , 0.33333333, 0.66666667, 1. , 1.33333333,
1.66666667, 2. , 2.33333333, 2.66666667, 3. ]
yt = [ 6.30000000e-05, -7.58828532e-04, -1.39770233e-03,
-1.84370370e-03, -2.08691495e-03, -2.11741838e-03,
-1.92529630e-03, -1.50063100e-03, -8.33504801e-04,
8.60000000e-05]
self.assertTrue(allclose(x, xt))
self.assertTrue(allclose(y, yt))
def test_cubics_deformation_xz(self):
"""test cubics function with (local) deformation"""
lns = cubics(self.global_points, deformation=self.u, rotation_plane='xz', factor=self.factor, inc=self.inc, index_offset=1)
for l, t in zip(lns, self.test_lns_l):
self.assertEqual(l[0], t[0]) # left point
self.assertEqual(l[1], t[1]) # right point
#self.assertEqual(l[2], t[2]) # deformation
self.assertTrue(allclose(l[2], t[2]))
plot_cubic_lines(lns, color='C3', samples=40)
def test_cubics_deformation_xy(self):
"""test cubics function with (local) deformation"""
lns = cubics(self.global_points, deformation=self.uN, rotation_plane='xy', factor=self.factor, inc=self.inc, index_offset=1)
for l, t in zip(lns, self.test_lns_l):
self.assertEqual(l[0], t[0]) # left point
self.assertEqual(l[1], t[1]) # right point
#self.assertEqual(l[2], t[2]) # deformation
self.assertTrue(allclose(l[2], t[2]))
plot_cubic_lines(lns, color='C3', samples=40)
def test_cubics_global_deformation_xz(self):
"""test cubics function with global_deformation"""
lns = cubics(self.global_points, global_deformation=fold_list(self.U, 3), rotation_plane='xz', factor=self.factor, inc=self.inc, index_offset=1)
for l, t in zip(lns, self.test_lns_g):
self.assertEqual(l[0], t[0]) # left point
self.assertEqual(l[1], t[1]) # right point
#self.assertEqual(l[2], t[2]) # deformation
self.assertTrue(allclose(l[2], t[2]))
plot_cubic_lines(lns, color='C4', samples=40)
def test_cubics_global_deformation_xy(self):
"""test cubics function with global_deformation"""
lns = cubics(self.global_points, global_deformation=fold_list(self.UN, 3), rotation_plane='xy', factor=self.factor, inc=self.inc, index_offset=1)
for l, t in zip(lns, self.test_lns_g):
self.assertEqual(l[0], t[0]) # left point
self.assertEqual(l[1], t[1]) # right point
#self.assertEqual(l[2], t[2]) # deformation
self.assertTrue(allclose(l[2], t[2]))
plot_cubic_lines(lns, color='C4', samples=40)
if __name__ == '__main__':
unittest.main()