# Path: imusim/io/bvh.py
# def loadBVHFile(filename, conversionFactor=1):
#     """
#     Load sampled body model data from a BVH file.
# 
#     @param filename: Name of file to load.
#     @param conversionFactor: Scale factor to apply to position values, e.g.
#         to convert meters to centimeters use conversionFactor=100.
# 
#     @return: A L{SampledBodyModel} object.
#     """
#     with open(filename,'r') as bvhFile:
#         loader = BVHLoader(bvhFile,conversionFactor)
#         loader._readHeader()
#         loader._readMotionData()
#     return loader.model
# 
# def saveBVHFile(model, filename, samplePeriod, conversionFactor = 1):
#     """
#     Save a body model data to a BVH file.
# 
#     @param model: Body model to save.
#     @param filename: Name of file to write to.
#     @param samplePeriod: The time between frames in the BVH output.
#     @param conversionFactor: Scale factor to apply to position values, e.g.
#         to convert meters to centimeters use conversionFactor=100.
#     """
#     with open(filename,'w') as bvhFile:
#         exporter = BVHExporter(model,bvhFile, samplePeriod, conversionFactor)
#         exporter.writeHeader()
#         for frame in xrange(exporter.frames):
#             exporter.writeFrame(model.startTime + frame * samplePeriod)
# 
# Path: imusim/maths/transforms.py
# def convertCGtoNED(param):
#     """
#     Convert parameters specified in Computer Graphics co-ordinates to
#     North East Down co-ordinates.
# 
#     @param param: A L{Quaternion} or 3x1 vector in CG frame.
# 
#     @return:  The corresponding L{Quaternion} or vector in NED frame
#     """
#     if isinstance(param, (Quaternion,QuaternionArray)):
#         return _CGtoNED.conjugate * param * _CGtoNED
#     else:
#         return _CGtoNED.rotateFrame(param)


from imusim.io import loadBVHFile, saveBVHFile
from imusim.maths.vectors import vector
from imusim.maths.quaternions import Quaternion
from numpy.testing import assert_almost_equal
from imusim.testing.quaternions import assertQuaternionAlmostEqual
from imusim.maths.transforms import convertCGtoNED
from nose.tools import raises
import tempfile
        {
            OFFSET 0.0 10.0 0.0
        }
    }
}
MOTION
Frames: 1
Frame Time: 0.1
0.0 0.0 0.0 0.0 0.0 0.0 90.0 0.0 0.0
    """
    testFile = tempfile.NamedTemporaryFile()
    with testFile:
        testFile.write(data)
        testFile.flush()
        model = loadBVHFile(testFile.name)

        assert len(list(model.points)) == 3

        assert model.name == 'root'
        assert len(model.channels) == 6
        assert len(model.children) == 1
        assert_almost_equal(model.position(0),vector(0,0,0))
        assertQuaternionAlmostEqual(model.rotation(0),
               convertCGtoNED(Quaternion(1,0,0,0)))

        j1 = model.getJoint('j1')
        assert j1.parent is model
        assert len(j1.channels) == 3
        assert len(j1.children) == 1
        assert_almost_equal(j1.positionOffset, vector(10,0,0))

# Path: imusim/io/bvh.py
# def loadBVHFile(filename, conversionFactor=1):
#     """
#     Load sampled body model data from a BVH file.
# 
#     @param filename: Name of file to load.
#     @param conversionFactor: Scale factor to apply to position values, e.g.
#         to convert meters to centimeters use conversionFactor=100.
# 
#     @return: A L{SampledBodyModel} object.
#     """
#     with open(filename,'r') as bvhFile:
#         loader = BVHLoader(bvhFile,conversionFactor)
#         loader._readHeader()
#         loader._readMotionData()
#     return loader.model
# 
# def saveBVHFile(model, filename, samplePeriod, conversionFactor = 1):
#     """
#     Save a body model data to a BVH file.
# 
#     @param model: Body model to save.
#     @param filename: Name of file to write to.
#     @param samplePeriod: The time between frames in the BVH output.
#     @param conversionFactor: Scale factor to apply to position values, e.g.
#         to convert meters to centimeters use conversionFactor=100.
#     """
#     with open(filename,'w') as bvhFile:
#         exporter = BVHExporter(model,bvhFile, samplePeriod, conversionFactor)
#         exporter.writeHeader()
#         for frame in xrange(exporter.frames):
#             exporter.writeFrame(model.startTime + frame * samplePeriod)
# 
# Path: imusim/maths/transforms.py
# def convertCGtoNED(param):
#     """
#     Convert parameters specified in Computer Graphics co-ordinates to
#     North East Down co-ordinates.
# 
#     @param param: A L{Quaternion} or 3x1 vector in CG frame.
# 
#     @return:  The corresponding L{Quaternion} or vector in NED frame
#     """
#     if isinstance(param, (Quaternion,QuaternionArray)):
#         return _CGtoNED.conjugate * param * _CGtoNED
#     else:
#         return _CGtoNED.rotateFrame(param)


# Path: imusim/tests/io/bvh_tests.py
from imusim.io import loadBVHFile, saveBVHFile
from imusim.maths.vectors import vector
from imusim.maths.quaternions import Quaternion
from numpy.testing import assert_almost_equal
from imusim.testing.quaternions import assertQuaternionAlmostEqual
from imusim.maths.transforms import convertCGtoNED
from nose.tools import raises
import tempfile

        {
            OFFSET 0.0 10.0 0.0
        }
    }
}
MOTION
Frames: 1
Frame Time: 0.1
0.0 0.0 0.0 0.0 0.0 0.0 90.0 0.0 0.0
    """
    testFile = tempfile.NamedTemporaryFile()
    with testFile:
        testFile.write(data)
        testFile.flush()
        model = loadBVHFile(testFile.name)

        assert len(list(model.points)) == 3

        assert model.name == 'root'
        assert len(model.channels) == 6
        assert len(model.children) == 1
        assert_almost_equal(model.position(0),vector(0,0,0))
        assertQuaternionAlmostEqual(model.rotation(0),
               convertCGtoNED(Quaternion(1,0,0,0)))

        j1 = model.getJoint('j1')
        assert j1.parent is model
        assert len(j1.channels) == 3
        assert len(j1.children) == 1
        assert_almost_equal(j1.positionOffset, vector(10,0,0))

        assert_almost_equal(j1.position(0), convertCGtoNED(vector(10,0,0)))