def testInitPose(self):
        bsp = bsplines.BSplinePose(4,sm.RotationVector())
        # Create two random transformations.
        T_n_0 = bsp.curveValueToTransformation(numpy.random.random(6))
        T_n_1 = bsp.curveValueToTransformation(numpy.random.random(6))
        
        # Initialize the curve.
        bsp.initPoseSpline(0.0,1.0,T_n_0, T_n_1)
        # Check the values.
        self.assertEqual(bsp.t_min(),0.0);
        self.assertEqual(bsp.t_max(),1.0);

        curve_T_n_0 = bsp.transformation(0.0);
        self.assertMatricesEqual(T_n_0, curve_T_n_0, 1e-9,"T_n_0")

        curve_T_n_1 = bsp.transformation(1.0);
        self.assertMatricesEqual(T_n_1, curve_T_n_1, 1e-9,"T_n_1")

        tend = 2.0
        # Extend the segment.
        T_n_25 = bsp.curveValueToTransformation(numpy.random.random(6))
        bsp.addPoseSegment(tend, T_n_25);

        # Check the values.
        self.assertEqual(bsp.t_min(),0.0);
        self.assertEqual(bsp.t_max(),tend);

        curve_T_n_0 = bsp.transformation(0.0);
        self.assertMatricesEqual(T_n_0, curve_T_n_0, 1e-6, "T_n_0")

        curve_T_n_1 = bsp.transformation(1.0);
        self.assertMatricesEqual(T_n_1, curve_T_n_1, 1e-4, "T_n_1")
        
        curve_T_n_25 = bsp.transformation(tend);
        self.assertMatricesEqual(T_n_25, curve_T_n_25, 1e-4, "T_n_25")
    def getUpdatedSpline(self, poseSpline, knots, splineOrder):
        """Get a spline with the new knot sequence build upon the poses of the old spline"""

        # linearly sample the old spline
        times = np.linspace(poseSpline.t_min(), poseSpline.t_max(), len(knots))

        splinePoses = np.zeros((6, len(knots)))
        for i, time in enumerate(times):
            splinePoses[:, i] = poseSpline.eval(time)

        # guarantee that beginning and end times of the spline remain unchanged
        oldKnots = poseSpline.knots()

        i = 0
        while oldKnots[i] < knots[0]:
            i += 1
        knots = np.insert(knots, 0, oldKnots[0:i])

        i = -1
        while oldKnots[i] > knots[-1]:
            i -= 1
        knots = np.append(knots, oldKnots[i:])

        newPoseSpline = bsplines.BSplinePose(splineOrder,
                                             poseSpline.rotation())
        newPoseSpline.initPoseSplineSparseKnots(times, splinePoses,
                                                np.array(knots), 1e-6)

        return newPoseSpline
예제 #3
0
    def __generateInitialSpline(self, splineOrder, timeOffsetPadding, numberOfKnots = None, framerate = None):
        poseSpline = bsplines.BSplinePose(splineOrder, sm.RotationVector())

        # Get the observation times.
        times = np.array([observation.time().toSec() for observation in self.__observations ])
        # get the pose values of the initial transformations at observation time
        curve = np.matrix([ poseSpline.transformationToCurveValue( observation.T_t_c().T() ) for observation in self.__observations]).T
        # make sure all values are well defined
        if np.isnan(curve).any():
            raise RuntimeError("Nans in curve values")
            sys.exit(0)
        # Add 2 seconds on either end to allow the spline to slide during optimization
        times = np.hstack((times[0] - (timeOffsetPadding * 2.0), times, times[-1] + (timeOffsetPadding * 2.0)))
        curve = np.hstack((curve[:,0], curve, curve[:,-1]))

        self.__ensureContinuousRotationVectors(curve)

        seconds = times[-1] - times[0]

        # fixed number of knots
        if (numberOfKnots is not None):
            knots = numberOfKnots
        # otherwise with framerate estimate
        else:
            knots = int(round(seconds * framerate/3))

        print
        print "Initializing a pose spline with %d knots (%f knots per second over %f seconds)" % ( knots, 100, seconds)
        poseSpline.initPoseSplineSparse(times, curve, knots, 1e-4)

        return poseSpline
 def testCurveToTransformation(self):
     rvs = (sm.RotationVector(), sm.EulerAnglesZYX(), sm.EulerRodriguez())
     for r in rvs:
         bsp = bsplines.BSplinePose(4,r)
         # Build a random, valid transformation.
         T1 = bsp.curveValueToTransformation(numpy.random.random(6))
         p = bsp.transformationToCurveValue(T1)
         T2 = bsp.curveValueToTransformation(p)
         self.assertMatricesEqual(T1, T2, 1e-9,"Checking the invertiblity of the transformation to curve values:")
 def testAngularVelocity(self):
     bsp = bsplines.BSplinePose(4,sm.EulerAnglesZYX())
     # Create two 
     e1 = numpy.array([0,0,0,0,0,0])
     T_n_0 = bsp.curveValueToTransformation(e1)
     e2 = numpy.array([0,0,0,math.pi*0.5,0,0])
     T_n_1 = bsp.curveValueToTransformation(e2)
     
     # Initialize the curve.
     bsp.initPoseSpline(0.0,1.0,T_n_0, T_n_1)
예제 #6
0
    def initPoseSplineFromCamera(self,
                                 splineOrder=6,
                                 poseKnotsPerSecond=100,
                                 timeOffsetPadding=0.02):
        T_c_b = self.T_extrinsic.T()
        pose = bsplines.BSplinePose(splineOrder, sm.RotationVector())

        # Get the checkerboard times.
        times = np.array([
            obs.time().toSec() + self.timeshiftCamToImuPrior
            for obs in self.targetObservations
        ])
        curve = np.matrix([
            pose.transformationToCurveValue(np.dot(obs.T_t_c().T(), T_c_b))
            for obs in self.targetObservations
        ]).T

        if np.isnan(curve).any():
            raise RuntimeError("Nans in curve values")
            sys.exit(0)

        # Add 2 seconds on either end to allow the spline to slide during optimization
        times = np.hstack((times[0] - (timeOffsetPadding * 2.0), times,
                           times[-1] + (timeOffsetPadding * 2.0)))
        curve = np.hstack((curve[:, 0], curve, curve[:, -1]))

        # Make sure the rotation vector doesn't flip
        for i in range(1, curve.shape[1]):
            previousRotationVector = curve[3:6, i - 1]
            r = curve[3:6, i]
            angle = np.linalg.norm(r)
            axis = r / angle
            best_r = r
            best_dist = np.linalg.norm(best_r - previousRotationVector)

            for s in range(-3, 4):
                aa = axis * (angle + math.pi * 2.0 * s)
                dist = np.linalg.norm(aa - previousRotationVector)
                if dist < best_dist:
                    best_r = aa
                    best_dist = dist
            curve[3:6, i] = best_r

        seconds = times[-1] - times[0]
        knots = int(round(seconds * poseKnotsPerSecond))

        print
        print "Initializing a pose spline with %d knots (%f knots per second over %f seconds)" % (
            knots, poseKnotsPerSecond, seconds)
        pose.initPoseSplineSparse(times, curve, knots, 1e-4)
        return pose
 def testInversePose2(self):
     rvs = (sm.RotationVector(), sm.EulerAnglesZYX(), sm.EulerRodriguez())
     for r in rvs:
         bsp = bsplines.BSplinePose(4,r)
         # Create two random transformations.
         T_n_0 = bsp.curveValueToTransformation(numpy.random.random(6))
         T_n_1 = bsp.curveValueToTransformation(numpy.random.random(6))
         
         # Initialize the curve.
         bsp.initPoseSpline(0.0,1.0,T_n_0, T_n_1)
         for t in numpy.arange(0.0,1.0,0.1):
             T = bsp.transformation(t)
             invT,J,C = bsp.inverseTransformationAndJacobian(t)
             one = numpy.dot(T,invT)
             self.assertMatricesEqual(one,numpy.eye(4),1e-14,"T * inv(T)")
 def testAngularVelocityBodyFrameJacobian(self):
     r = sm.EulerAnglesZYX();
     for order in range(2,7):
         bsp = bsplines.BSplinePose(order,r)
         T_n_0 = bsp.curveValueToTransformation(numpy.random.random(6))
         T_n_1 = bsp.curveValueToTransformation(numpy.random.random(6))
             
         # Initialize the curve.
         bsp.initPoseSpline(0.0,1.0,T_n_0, T_n_1)
             
         for t in numpy.linspace(bsp.t_min(), bsp.t_max(), 4):
             
             oJI = bsp.angularVelocityBodyFrameAndJacobian(t);
             #print "TJI: %s" % (TJI)
             je = nd.Jacobian(lambda c: bsp.setLocalCoefficientVector(t,c) or bsp.angularVelocityBodyFrame(t))
             estJ = je(bsp.localCoefficientVector(t))
             J = oJI[1]
             
             self.assertMatricesEqual(J, estJ, 1e-9,"omega Jacobian")
예제 #9
0
    def __init__(self,
                 order=4,
                 target_point=[0., 0, 0],
                 ctrl_point_num=10,
                 time=10.,
                 random_range=[[0, 0, 0.], [1., 1., 1.]]):
        self.target_point = np.array(target_point)
        self.bsp = bsplines.BSplinePose(4, sm.RotationVector())
        self.ctrl_point_num = ctrl_point_num
        random_range = np.array(random_range) + self.target_point

        self.curve = self.TargetOrientedPose(ctrl_point_num, random_range)
        self.time = time

        times = np.linspace(0, time, ctrl_point_num)
        curve_in = np.array(self.curve)
        curve_in[3:] *= -1
        self.bsp.initPoseSplineSparse(times, curve_in,
                                      int(ctrl_point_num * 3.), 1e-5)
예제 #10
0
 def testInverseOrientationJacobian(self):
     rvs = (sm.RotationVector(), sm.EulerAnglesZYX(), sm.EulerRodriguez())
     for r in rvs:
         for order in range(2,7):
             bsp = bsplines.BSplinePose(order,r)
             T_n_0 = bsp.curveValueToTransformation(numpy.random.random(6))
             T_n_1 = bsp.curveValueToTransformation(numpy.random.random(6))
             
             # Initialize the curve.
             bsp.initPoseSpline(0.0,1.0,T_n_0, T_n_1)
             
             for t in numpy.linspace(bsp.t_min(), bsp.t_max(), 4):
                 # Create a random homogeneous vector
                 v = numpy.random.random(3)
                 CJI = bsp.inverseOrientationAndJacobian(t);
                 #print "TJI: %s" % (TJI)
                 je = nd.Jacobian(lambda c: bsp.setLocalCoefficientVector(t,c) or numpy.dot(bsp.inverseOrientation(t), v))
                 estJ = je(bsp.localCoefficientVector(t))
                 JT = CJI[1]
                 J = numpy.dot(sm.crossMx(numpy.dot(CJI[0],v)), JT)                    
                 self.assertMatricesEqual(J, estJ, 1e-8,"C_n_0")
예제 #11
0
import sm
import bsplines
import numpy as np
from scipy.spatial.transform import Rotation as R

if __name__ == "__main__":
    bsp = bsplines.BSplinePose(4, sm.RotationVector())
    curve = np.array([[0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [0.1, 0, 0, 0, 0, 0.1],
                      [0.2, 0, 0, 0, 0, 0.1]]).T
    bsp.initPoseSplineSparse(np.array([0., 1, 2]), curve, 5, 1e-5)
    r0 = R.from_dcm(bsp.orientation(0.0)).as_rotvec()
    r0_ = R.from_dcm(bsp.curveValueToTransformation(
        curve.T[0])[:3, :3]).as_rotvec()
    t0_ = bsp.curveValueToTransformation(curve.T[0])[:3, 3]
    print(r0)
    print(t0_)