# 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
ROOT root
{
    OFFSET 0.0 0.0 0.0
    CHANNELS 6 Xposition Yposition Zposition Zrotation Xrotation Yrotation
    JOINT j1
    {
        OFFSET 10.0 0.0 0.0
        CHANNELS 3 Zrotation Xrotation Yrotation
        End Site
        {
            OFFSET 0.0 10.0 0.0
        }
    }
}
MOTION
Frames: 5
Frame Time: 0.1
0.0 0.0 0.0 0.0 0.0 0.0 90.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0 0.0 80.0 0.0 5.0
0.0 2.0 0.0 0.0 0.0 0.0 70.0 0.0 10.0
0.0 3.0 0.0 0.0 0.0 0.0 60.0 0.0 15.0
0.0 4.0 0.0 0.0 0.0 0.0 50.0 0.0 20.0
    """
    testFile = tempfile.NamedTemporaryFile()
    exportFile = tempfile.NamedTemporaryFile()
    with testFile:
        testFile.write(data)
        testFile.flush()
        model = loadBVHFile(testFile.name)
        saveBVHFile(model, exportFile.name, 0.1)

# 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

ROOT root
{
    OFFSET 0.0 0.0 0.0
    CHANNELS 6 Xposition Yposition Zposition Zrotation Xrotation Yrotation
    JOINT j1
    {
        OFFSET 10.0 0.0 0.0
        CHANNELS 3 Zrotation Xrotation Yrotation
        End Site
        {
            OFFSET 0.0 10.0 0.0
        }
    }
}
MOTION
Frames: 5
Frame Time: 0.1
0.0 0.0 0.0 0.0 0.0 0.0 90.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0 0.0 80.0 0.0 5.0
0.0 2.0 0.0 0.0 0.0 0.0 70.0 0.0 10.0
0.0 3.0 0.0 0.0 0.0 0.0 60.0 0.0 15.0
0.0 4.0 0.0 0.0 0.0 0.0 50.0 0.0 20.0
    """
    testFile = tempfile.NamedTemporaryFile()
    exportFile = tempfile.NamedTemporaryFile()
    with testFile:
        testFile.write(data)
        testFile.flush()
        model = loadBVHFile(testFile.name)
        saveBVHFile(model, exportFile.name, 0.1)

        exportedModel = loadBVHFile(exportFile.name)