Hello Daniel, On 01/06/2011 10:22 AM, Daniel Wickeroth wrote: > I'm writing a simple program which uses Kinect to move a puppet made > with OpenSG. >> From Kinect's API I get a Matrix for each Joint which looks like this: > > 0.999527 0.0301296 0.00610329 > -0.0286029 0.984236 -0.174532 > -0.0112657 0.174275 0.984633 > > The determinat of the matrix is 1, the length of each column vector is also 1. > The dot products of the column vectors are close to 0 (around 1 * 10 ^-5). > So I figured it's a rotation matrix. > > I found a way to apply this to a Component Transform, and it works, > but I'm not sure it's the best way to do it. > > This is the code: > > //turn Kinect Matrix into OpenSG Matrix > osg::Matrix makeOsgMatrix(float* kinectMatrix){ > osg::Matrix matrix = osg::Matrix(kinectMatrix[0], kinectMatrix[1], > kinectMatrix[2], 0.0f, > kinectMatrix[3], kinectMatrix[4], > kinectMatrix[5], 0.0f, > kinectMatrix[6], kinectMatrix[7], > kinectMatrix[8], 0.0f, > 0.0f, 0.0f, > 0.0f, 1.0f); > > return matrix; > } > > //rotMat is the Kinect Matrix > void Puppet::setRightUpperArmRot(osg::Matrix rotMat){ > > Vec3f from, to; > > //the arm is supposed to be initially horizontal, but i can only > make vertical cones, so I rotate it first. > from = Vec3f(0.0, -1.0, 0.0); > to = Vec3f(1.0, 0.0, 0.0); > Quaternion quatA(from, to); > > //turn the kinect matrix into a quaternion and multiply with > initial quaternion > Quaternion quatB(rotMat); > quatA.multLeft(quatB); > > //apply final quaternion to ComponentTransform > beginEditCP(rightUpperArmTransCore); > rightUpperArmTransCore->setRotation( quatA ); > endEditCP(rightUpperArmTransCore); > } > > Is there a faster way to do this? Or do I have to turn the kinect > matrix into a quaternion before applying it to the ComponentTransform? > I have to do it for all the joints 30 times a second, so I want it to be fast.
hmm, constructing a Quaternion from a Matrix is not totally trivial, so it takes a bit of computation. If you can use just Transform instead of ComponentTransform you could just multiply/set the matrix directly. You can also directly set the Matrix of a ComponentTransform, but then it internally has to decompose that matrix to keep the other fields of ComponentTransform (translation, rotation, scale, etc.) consistent with the matrix. Unless testing has determined this part of the code to be a performance bottleneck I'd leave it the way you have it, otherwise I would suggest to see if there is a more efficient way to directly compute the Quaternion from the kinect matrix (using OpenSG's code requires that you first create an OpenSG Matrix). Also it may be written to deal with more corner/error cases, but you already know the kinect matrix contains (only) a rotation so there may be potential for optimization. Cheers, Carsten ------------------------------------------------------------------------------ Learn how Oracle Real Application Clusters (RAC) One Node allows customers to consolidate database storage, standardize their database environment, and, should the need arise, upgrade to a full multi-node Oracle RAC database without downtime or disruption http://p.sf.net/sfu/oracle-sfdevnl _______________________________________________ Opensg-users mailing list Opensg-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensg-users