Commit 2c88bfbe authored by Guilhem Saurel's avatar Guilhem Saurel Committed by Pierre Fernbach
Browse files

remove StringIO, not available in Python 3

auto reformat may have moved a few other things ;)
parent d73a9fda
# FOR PRINT, TO REMOVE import unittest
import StringIO
import sys
# END FOR PRINT
from numpy import matrix from numpy import matrix
from numpy.linalg import norm from numpy.linalg import norm
import unittest from curves import (bezier3, bezier6, bezier_from_hermite, bezier_from_polynomial, cubic_hermite_spline,
curve_constraints, exact_cubic, hermite_from_bezier, hermite_from_polynomial,
piecewise_bezier3_curve, piecewise_bezier6_curve, piecewise_cubic_hermite_curve,
piecewise_polynomial_curve, polynomial, polynomial_from_bezier, polynomial_from_hermite)
from curves import bezier3, bezier6
from curves import curve_constraints, polynomial, exact_cubic, cubic_hermite_spline
from curves import piecewise_polynomial_curve, piecewise_bezier3_curve, piecewise_bezier6_curve, piecewise_cubic_hermite_curve
from curves import polynomial_from_bezier, polynomial_from_hermite
from curves import bezier_from_hermite, bezier_from_polynomial
from curves import hermite_from_bezier, hermite_from_polynomial
class TestCurves(unittest.TestCase): class TestCurves(unittest.TestCase):
#def print_str(self, inStr): #def print_str(self, inStr):
...@@ -27,10 +20,10 @@ class TestCurves(unittest.TestCase): ...@@ -27,10 +20,10 @@ class TestCurves(unittest.TestCase):
# - Variables : degree, nbWayPoints # - Variables : degree, nbWayPoints
__EPS = 1e-6 __EPS = 1e-6
waypoints = matrix([[1., 2., 3.]]).T waypoints = matrix([[1., 2., 3.]]).T
a = bezier3(waypoints,0.,2.) a = bezier3(waypoints, 0., 2.)
t = 0. t = 0.
while t < 2.: while t < 2.:
self.assertTrue (norm(a(t) - matrix([1., 2., 3.]).T) < __EPS) self.assertTrue(norm(a(t) - matrix([1., 2., 3.]).T) < __EPS)
t += 0.1 t += 0.1
waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose() waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose()
waypoints6 = matrix([[1., 2., 3., 7., 5., 5.], [4., 5., 6., 4., 5., 6.]]).transpose() waypoints6 = matrix([[1., 2., 3., 7., 5., 5.], [4., 5., 6., 4., 5., 6.]]).transpose()
...@@ -40,25 +33,25 @@ class TestCurves(unittest.TestCase): ...@@ -40,25 +33,25 @@ class TestCurves(unittest.TestCase):
a = bezier3(waypoints, 0., 3.) a = bezier3(waypoints, 0., 3.)
# Test : Degree, min, max, derivate # Test : Degree, min, max, derivate
#self.print_str(("test 1") #self.print_str(("test 1")
self.assertEqual (a.degree, a.nbWaypoints - 1) self.assertEqual(a.degree, a.nbWaypoints - 1)
a.min() a.min()
a.max() a.max()
a(0.4) a(0.4)
self.assertTrue ((a.derivate(0.4, 0) == a(0.4)).all()) self.assertTrue((a.derivate(0.4, 0) == a(0.4)).all())
a.derivate(0.4, 2) a.derivate(0.4, 2)
a = a.compute_derivate(100) a = a.compute_derivate(100)
prim = a.compute_primitive(1) prim = a.compute_primitive(1)
# Check primitive and derivate - order 1 # Check primitive and derivate - order 1
for i in range(10): for i in range(10):
t = float(i) / 10. t = float(i) / 10.
self.assertTrue ((a(t) == prim.derivate(t, 1)).all()) self.assertTrue((a(t) == prim.derivate(t, 1)).all())
self.assertTrue ((prim(0) == matrix([0., 0., 0.])).all()) self.assertTrue((prim(0) == matrix([0., 0., 0.])).all())
# Check primitive and derivate - order 2 # Check primitive and derivate - order 2
prim = a.compute_primitive(2) prim = a.compute_primitive(2)
for i in range(10): for i in range(10):
t = float(i) / 10. t = float(i) / 10.
self.assertTrue ((a(t) == prim.derivate(t, 2)).all()) self.assertTrue((a(t) == prim.derivate(t, 2)).all())
self.assertTrue ((prim(0) == matrix([0., 0., 0.])).all()) self.assertTrue((prim(0) == matrix([0., 0., 0.])).all())
# Create new bezier3 curve # Create new bezier3 curve
waypoints = matrix([[1., 2., 3.], [4., 5., 6.], [4., 5., 6.], [4., 5., 6.], [4., 5., 6.]]).transpose() waypoints = matrix([[1., 2., 3.], [4., 5., 6.], [4., 5., 6.], [4., 5., 6.], [4., 5., 6.]]).transpose()
a0 = bezier3(waypoints) a0 = bezier3(waypoints)
...@@ -68,11 +61,11 @@ class TestCurves(unittest.TestCase): ...@@ -68,11 +61,11 @@ class TestCurves(unittest.TestCase):
# Check change in argument time_t of bezier3 # Check change in argument time_t of bezier3
for i in range(10): for i in range(10):
t = float(i) / 10. t = float(i) / 10.
self.assertTrue (norm(a0(t) - a1(3 * t)) < __EPS) self.assertTrue(norm(a0(t) - a1(3 * t)) < __EPS)
self.assertTrue (norm(a0.derivate(t, 1) - a1.derivate(3 * t, 1) * 3.) < __EPS) self.assertTrue(norm(a0.derivate(t, 1) - a1.derivate(3 * t, 1) * 3.) < __EPS)
self.assertTrue (norm(a0.derivate(t, 2) - a1.derivate(3 * t, 2) * 9.) < __EPS) self.assertTrue(norm(a0.derivate(t, 2) - a1.derivate(3 * t, 2) * 9.) < __EPS)
self.assertTrue (norm(prim0(t) - prim1(t * 3) / 3.) < __EPS) self.assertTrue(norm(prim0(t) - prim1(t * 3) / 3.) < __EPS)
self.assertTrue ((prim(0) == matrix([0., 0., 0.])).all()) self.assertTrue((prim(0) == matrix([0., 0., 0.])).all())
# testing bezier with constraints # testing bezier with constraints
c = curve_constraints() c = curve_constraints()
c.init_vel = matrix([0., 1., 1.]).transpose() c.init_vel = matrix([0., 1., 1.]).transpose()
...@@ -82,20 +75,20 @@ class TestCurves(unittest.TestCase): ...@@ -82,20 +75,20 @@ class TestCurves(unittest.TestCase):
#Check derivate with constraints #Check derivate with constraints
waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose() waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose()
a = bezier3(waypoints, c) a = bezier3(waypoints, c)
self.assertTrue (norm(a.derivate(0, 1) - c.init_vel) < 1e-10) self.assertTrue(norm(a.derivate(0, 1) - c.init_vel) < 1e-10)
self.assertTrue (norm(a.derivate(1, 2) - c.end_acc) < 1e-10) self.assertTrue(norm(a.derivate(1, 2) - c.end_acc) < 1e-10)
return return
def test_polynomial(self): def test_polynomial(self):
# To test : # To test :
# - Functions : constructor, min, max, derivate # - Functions : constructor, min, max, derivate
waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose() waypoints = matrix([[1., 2., 3.], [4., 5., 6.]]).transpose()
a = polynomial(waypoints) # Defined on [0.,1.] a = polynomial(waypoints) # Defined on [0.,1.]
a = polynomial(waypoints, -1., 3.) # Defined on [-1.,3.] a = polynomial(waypoints, -1., 3.) # Defined on [-1.,3.]
a.min() a.min()
a.max() a.max()
a(0.4) a(0.4)
self.assertTrue ((a.derivate(0.4, 0) == a(0.4)).all()) self.assertTrue((a.derivate(0.4, 0) == a(0.4)).all())
a.derivate(0.4, 2) a.derivate(0.4, 2)
return return
...@@ -104,14 +97,14 @@ class TestCurves(unittest.TestCase): ...@@ -104,14 +97,14 @@ class TestCurves(unittest.TestCase):
# - Functions : constructor, min, max, derivate, add_curve, is_continuous # - Functions : constructor, min, max, derivate, add_curve, is_continuous
waypoints1 = matrix([[1., 1., 1.]]).transpose() waypoints1 = matrix([[1., 1., 1.]]).transpose()
waypoints2 = matrix([[1., 1., 1.], [1., 1., 1.]]).transpose() waypoints2 = matrix([[1., 1., 1.], [1., 1., 1.]]).transpose()
a = polynomial(waypoints1, 0.,1.) a = polynomial(waypoints1, 0., 1.)
b = polynomial(waypoints2, 1., 3.) b = polynomial(waypoints2, 1., 3.)
ppc = piecewise_polynomial_curve(a) ppc = piecewise_polynomial_curve(a)
ppc.add_curve(b) ppc.add_curve(b)
ppc.min() ppc.min()
ppc.max() ppc.max()
ppc(0.4) ppc(0.4)
self.assertTrue ((ppc.derivate(0.4, 0) == ppc(0.4)).all()) self.assertTrue((ppc.derivate(0.4, 0) == ppc(0.4)).all())
ppc.derivate(0.4, 2) ppc.derivate(0.4, 2)
ppc.is_continuous(0) ppc.is_continuous(0)
ppc.is_continuous(1) ppc.is_continuous(1)
...@@ -128,7 +121,7 @@ class TestCurves(unittest.TestCase): ...@@ -128,7 +121,7 @@ class TestCurves(unittest.TestCase):
ppc.min() ppc.min()
ppc.max() ppc.max()
ppc(0.4) ppc(0.4)
self.assertTrue ((ppc.derivate(0.4, 0) == ppc(0.4)).all()) self.assertTrue((ppc.derivate(0.4, 0) == ppc(0.4)).all())
ppc.derivate(0.4, 2) ppc.derivate(0.4, 2)
ppc.is_continuous(0) ppc.is_continuous(0)
ppc.is_continuous(1) ppc.is_continuous(1)
...@@ -145,7 +138,7 @@ class TestCurves(unittest.TestCase): ...@@ -145,7 +138,7 @@ class TestCurves(unittest.TestCase):
ppc.min() ppc.min()
ppc.max() ppc.max()
ppc(0.4) ppc(0.4)
self.assertTrue ((ppc.derivate(0.4, 0) == ppc(0.4)).all()) self.assertTrue((ppc.derivate(0.4, 0) == ppc(0.4)).all())
ppc.derivate(0.4, 2) ppc.derivate(0.4, 2)
ppc.is_continuous(0) ppc.is_continuous(0)
ppc.is_continuous(1) ppc.is_continuous(1)
...@@ -165,7 +158,7 @@ class TestCurves(unittest.TestCase): ...@@ -165,7 +158,7 @@ class TestCurves(unittest.TestCase):
ppc.min() ppc.min()
ppc.max() ppc.max()
ppc(0.4) ppc(0.4)
self.assertTrue ((ppc.derivate(0.4, 0) == ppc(0.4)).all()) self.assertTrue((ppc.derivate(0.4, 0) == ppc(0.4)).all())
ppc.derivate(0.4, 2) ppc.derivate(0.4, 2)
ppc.is_continuous(0) ppc.is_continuous(0)
ppc.is_continuous(1) ppc.is_continuous(1)
...@@ -180,7 +173,7 @@ class TestCurves(unittest.TestCase): ...@@ -180,7 +173,7 @@ class TestCurves(unittest.TestCase):
a.min() a.min()
a.max() a.max()
a(0.4) a(0.4)
self.assertTrue ((a.derivate(0.4, 0) == a(0.4)).all()) self.assertTrue((a.derivate(0.4, 0) == a(0.4)).all())
a.derivate(0.4, 2) a.derivate(0.4, 2)
a.getNumberSplines() a.getNumberSplines()
a.getSplineAt(0) a.getSplineAt(0)
...@@ -212,7 +205,7 @@ class TestCurves(unittest.TestCase): ...@@ -212,7 +205,7 @@ class TestCurves(unittest.TestCase):
a.min() a.min()
a.max() a.max()
a(0.4) a(0.4)
self.assertTrue ((a.derivate(0.4, 0) == a(0.4)).all()) self.assertTrue((a.derivate(0.4, 0) == a(0.4)).all())
a.derivate(0.4, 2) a.derivate(0.4, 2)
return return
...@@ -222,25 +215,26 @@ class TestCurves(unittest.TestCase): ...@@ -222,25 +215,26 @@ class TestCurves(unittest.TestCase):
a = bezier3(waypoints) a = bezier3(waypoints)
# converting bezier to polynomial # converting bezier to polynomial
a_pol = polynomial_from_bezier(a) a_pol = polynomial_from_bezier(a)
self.assertTrue (norm(a(0.3) - a_pol(0.3)) < __EPS) self.assertTrue(norm(a(0.3) - a_pol(0.3)) < __EPS)
# converting polynomial to hermite # converting polynomial to hermite
a_chs = hermite_from_polynomial(a_pol); a_chs = hermite_from_polynomial(a_pol)
self.assertTrue (norm(a_chs(0.3) - a_pol(0.3)) < __EPS) self.assertTrue(norm(a_chs(0.3) - a_pol(0.3)) < __EPS)
# converting hermite to bezier # converting hermite to bezier
a_bc = bezier_from_hermite(a_chs); a_bc = bezier_from_hermite(a_chs)
self.assertTrue (norm(a_chs(0.3) - a_bc(0.3)) < __EPS) self.assertTrue(norm(a_chs(0.3) - a_bc(0.3)) < __EPS)
self.assertTrue (norm(a(0.3) - a_bc(0.3)) < __EPS) self.assertTrue(norm(a(0.3) - a_bc(0.3)) < __EPS)
# converting bezier to hermite # converting bezier to hermite
a_chs = hermite_from_bezier(a); a_chs = hermite_from_bezier(a)
self.assertTrue (norm(a(0.3) - a_chs(0.3)) < __EPS) self.assertTrue(norm(a(0.3) - a_chs(0.3)) < __EPS)
# converting hermite to polynomial # converting hermite to polynomial
a_pol = polynomial_from_hermite(a_chs) a_pol = polynomial_from_hermite(a_chs)
self.assertTrue (norm(a_pol(0.3) - a_chs(0.3)) < __EPS) self.assertTrue(norm(a_pol(0.3) - a_chs(0.3)) < __EPS)
# converting polynomial to bezier # converting polynomial to bezier
a_bc = bezier_from_polynomial(a_pol) a_bc = bezier_from_polynomial(a_pol)
self.assertTrue (norm(a_bc(0.3) - a_pol(0.3)) < __EPS) self.assertTrue(norm(a_bc(0.3) - a_pol(0.3)) < __EPS)
self.assertTrue (norm(a(0.3) - a_bc(0.3)) < __EPS) self.assertTrue(norm(a(0.3) - a_bc(0.3)) < __EPS)
return return
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment