##########################################################################
#
#  Copyright (c) 2008, Image Engine Design Inc. All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#     * Neither the name of Image Engine Design nor the names of any
#       other contributors to this software may be used to endorse or
#       promote products derived from this software without specific prior
#       written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
#  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################

import math
import unittest
import random
import imath
import IECore

class CubicTest( unittest.TestCase ) :

	def testConstructor( self ) :

		m = imath.M44f( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 )
		b = IECore.CubicBasisf( m, 10 )
		self.assertEqual( b.matrix, m )
		self.assertEqual( b.step, 10 )

	def testAccessors( self ) :

		m = imath.M44f( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 )
		b = IECore.CubicBasisf( m, 10 )
		self.assertEqual( b.matrix, m )
		self.assertEqual( b.step, 10 )

		m2 = imath.M44f( 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 )
		b.matrix = m2
		self.assertEqual( b.matrix, m2 )

		b.step = 1
		self.assertEqual( b.matrix, m2 )

	def testStaticMethods( self ) :

		b = IECore.CubicBasisf.bezier()
		b = IECore.CubicBasisf.catmullRom()
		b = IECore.CubicBasisf.bSpline()

	def testCoefficients( self ) :

		b = IECore.CubicBasisf.bezier()

		c = b.coefficients( 0 )
		self.assertEqual( len( c ), 4 )
		self.assertEqual( c, ( 1, 0, 0, 0 ) )

		c = b.coefficients( 1 )
		self.assertEqual( len( c ), 4 )
		self.assertEqual( c, ( 0, 0, 0, 1 ) )

	def testDerivativeCoefficients( self ) :

		b = IECore.CubicBasisf.bezier()

		c = b.derivativeCoefficients( 0 )
		self.assertEqual( len( c ), 4 )
		self.assertEqual( c, ( -3, 3, 0, 0 ) )

		c = b.derivativeCoefficients( 1 )
		self.assertEqual( len( c ), 4 )
		self.assertEqual( c, ( 0, 0, -3, 3 ) )

		b = IECore.CubicBasisf.linear()

		c = b.derivativeCoefficients( 0 )
		self.assertEqual( len( c ), 4 )
		self.assertEqual( c, ( -1, 1, 0, 0 ) )

		c = b.derivativeCoefficients( 1 )
		self.assertEqual( len( c ), 4 )
		self.assertEqual( c, ( -1, 1, 0, 0 ) )

	def testCall( self ) :

		b = IECore.CubicBasisf.bezier()

		p0 = imath.V3f( 0 )
		p1 = imath.V3f( 0, 1, 0 )
		p2 = imath.V3f( 1, 1, 0 )
		p3 = imath.V3f( 1, 0, 0 )

		p = b( 0, p0, p1, p2, p3 )
		self.assertEqual( p, p0 )

		p = b( 1, p0, p1, p2, p3 )
		self.assertEqual( p, p3 )

	def testCallWithScalars( self ) :

		b = IECore.CubicBasisf.bezier()

		p0 = 0
		p1 = 1
		p2 = 1
		p3 = 0

		p = b( 0, p0, p1, p2, p3 )
		self.assertEqual( p, p0 )

		p = b( 1, p0, p1, p2, p3 )
		self.assertEqual( p, p3 )

	def testEquality( self ) :

		self.assertEqual( IECore.CubicBasisf.bezier(), IECore.CubicBasisf.bezier() )
		self.assertNotEqual( IECore.CubicBasisf.linear(), IECore.CubicBasisf.bezier() )

	def testRepr( self ) :

		import IECore

		b = IECore.CubicBasisf.bezier()
		bb = eval( repr( b ) )
		self.assertEqual( b, bb )

	def testLinearCoefficients( self ) :

		b = IECore.CubicBasisf.linear()
		for i in range( 0, 100 ) :
			t = i / 99.0
			c = b.coefficients( t )

			self.assertAlmostEqual( c[0], 1-t )
			self.assertAlmostEqual( c[1], t )
			self.assertAlmostEqual( c[2], 0 )
			self.assertAlmostEqual( c[3], 0 )

	def testIntegral( self ) :

		random.seed( 200 )

		b = IECore.CubicBasisf.catmullRom()
		for i in range( 0, 1000 ) :

			p0 = random.random()
			p1 = random.random()
			p2 = random.random()
			p3 = random.random()

			t0 = random.random()
			t1 = random.random()

			steps = 1000
			tStep = (t1 - t0) / (steps-1)
			a = 0
			prevV = None
			for j in range( 0, steps ) :

				t = t0 + j * tStep

				v =  b( t, p0, p1, p2, p3 )

				if prevV is not None :
					a += tStep * ( v + prevV ) / 2.0

				prevV = v

			self.assertAlmostEqual( a, b.integral( t0, t1, p0, p1, p2, p3 ), 6 )

	def testReadEnum( self ):

		b = IECore.CubicBasisf.linear()
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.Linear )

		b = IECore.CubicBasisf.bezier()
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.Bezier )

		b = IECore.CubicBasisf.bSpline()
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.BSpline )

		b = IECore.CubicBasisf.catmullRom()
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.CatmullRom )

		b = IECore.CubicBasisf(imath.M44f(), 0)
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.Unknown )

	def testConstructFromEnum( self ):

		b = IECore.CubicBasisf ( IECore.StandardCubicBasis.Linear )
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.Linear )
		self.assertEqual( b, IECore.CubicBasisf.linear() )

		b = IECore.CubicBasisf ( IECore.StandardCubicBasis.Bezier )
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.Bezier )
		self.assertEqual( b, IECore.CubicBasisf.bezier() )

		b = IECore.CubicBasisf ( IECore.StandardCubicBasis.BSpline )
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.BSpline )
		self.assertEqual( b, IECore.CubicBasisf.bSpline() )

		b = IECore.CubicBasisf ( IECore.StandardCubicBasis.CatmullRom )
		self.assertEqual( b.standardBasis(), IECore.StandardCubicBasis.CatmullRom )
		self.assertEqual( b, IECore.CubicBasisf.catmullRom() )

		with self.assertRaises( Exception ) as cm:
			b = IECore.CubicBasisf ( IECore.StandardCubicBasis.Unknown )

if __name__ == "__main__":
    unittest.main()

