PDA

View Full Version : How to get the rotation matrix of an object . . .


AtrusDni
07-13-2009, 06:33 PM
I am working on a script, to test if points lie on a side of a plane or not. Basically I get the plane matrix and multiply it by the normal and test if the point is behind the plane with this:


float $value = ($pntPos - $planePos) * $wPoint;


$pntPos is a vector representing the xyz position in space that I want to check.
$planePos is a vector representing the xyz position in space of the plane.
$wPoint is the worldspace normal of the plane.

I am using xform -q -m pPlane to get the rotation matrix from the object and am multiplying that by the plane normal so I get the normal in world space.

It works quite well, BUT! it is not fast because it has to interact with geometry, and has a bit of overhead. I would like to do this all through mel with no object interaction, and have it be strictly math based. So, my question is . . . .

How would I go about accuiring the rotation matrix of an object if:
1) I have an XYZ vector describing its position, so thats good to go.
2) If I have two points XYZ position, how would I (thru mel) get the direction vector pointing from one to the other? (this would be the same as a plane's worldspace normal, am I right?)


Thanks again for the help! I am no good at math so I am learning as I go.

cbamber85
07-13-2009, 11:24 PM
I am working on a script, to test if points lie on a side of a plane or not.http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/ Bottom of the page is what you want, obviously don't absolute the output because a positive number means it's on the side of the plane that the normal points away from, and a vice versa for a negative.

How would I go about accuiring the rotation matrix of an objectFor what you're doing I wouldn't bother, matrices only really become useful when concatenating several transformations together, or passing many objects through the same transformation.

If I have two points XYZ position, how would I (thru mel) get the direction vector pointing from one to the other?Subtract the source point from the destination point.

(this would be the same as a plane's worldspace normal, am I right?)Depends where the points are... Look into calculating the cross-product of two vectors that lie on the plane (that don't point in the same direction), I think that's what you're thinking of. Remember though, in Maya the user can manually change the normal of face or vertex without changing the underlying topology, which would invalidate any calculations you do at this level (assuming this is for mesh operations). There's probably a way of accessing worldSpace face normal data through MEL (without an expensive rotation matrix operation), but I don't know it. Personally I find it much easier to access the API through Python now it's all script editor friendly, this is also gives you access to all the internal MVector, MPoint, MQuaternion, etc. types that greatly simplify computational geometry tasks.

tbaypaul
07-14-2009, 03:42 AM
some of the answers to your questions will show you why cbamber85 is pushing python and the api....cause it is messy in mel....run this script on a selection of faces of a poly object...and it prints out the transformed normals....I cobbled this together from snippets of Dave Gould's books.....great resources...

{
string $sels[] = `ls -sl -flatten`;
string $buffer1[];
tokenize $sels[0] "." $buffer1;
//print ($buffer1[0] + "\n");

for($sel in $sels){
select -replace $sel;
string $buffer2[];
string $dataString[] = `polyInfo -faceNormals`;
tokenize $dataString[0] $buffer2;
//print $buffer2;
vector $faceNormal = <<(float)$buffer2[2],(float)$buffer2[3],(float)$buffer2[4]>>;
print ("Face Normal is: " + $faceNormal.x + ", " + $faceNormal.y + ", " + $faceNormal.z + "\n");

float $result[3];
// this xform call gets the full matrix, but we want
// the inverse matrix. Normals are transformed
// by the transpose of the inverse Matrix.
//float $m[] = `xform -q -matrix $buffer1[0]`;
float $m[] = `getAttr ($buffer1[0] + ".inverseMatrix")`;
$result[0] = $faceNormal.x*$m[0] + $faceNormal.y*$m[1] + $faceNormal.z*$m[2];
$result[1] = $faceNormal.x*$m[4] + $faceNormal.y*$m[5] + $faceNormal.z*$m[6];
$result[2] = $faceNormal.x*$m[8] + $faceNormal.y*$m[9] + $faceNormal.z*$m[10];
print ("Transformed Normal is: " + $result[0] + ", " + $result[1] + ", " + $result[2] + "\n");
}
select $sels;
}

CGTalk Moderation
07-14-2009, 03:42 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.