Re: Scripting : Align along normal

Date : Mon, 31 Oct 2005 22:07:09 +0100
To : XSI(at)Softimage.COM
From : guillaume laforge <guillaume.laforge.3d(at)gmail.com>
Subject : Re: Scripting : Align along normal
Thanks for the help Kim. Raf function gives me a clearer vision of matrix use with XSI :-). So two vector cross products are needed to create the 3X3 matrix ? Not so complicated :-)
 
However, I found my problem. It came from a wrong normal vector. Maybe some users on this list learn 3d math like me so here is the answer : 
 
 In my previous script the polygon normal is :
//---------------------------------------------------------
var polySel = Selection.item(0).subcomponent.componentcollection;
var oPolygonFace = polySel(0);
//Get the Polygon Normal
var oPolygonNode = oPolygonFace.Nodes(0);
var >//-------------------------------------------------------
 
In fact it returns a point normal.
So I use this snipet to find the polygon normal :

//---------------------------------------------------------
//Get the selected polygon
var polySel = Selection.item(0).subcomponent.componentcollection;
var oPolygonFace = polySel(0);
nbPoints = oPolygonFace.NeighborVertices (1);
var >for(i=0;i<nbPoints.count;i++)
{
   oNormal.AddInPlace(nbPoints.item(i).normal);
}
oNormal.ScaleInPlace(1/nbPoints.count);
//---------------------------------------------------------

Thanks to Heldge Mathee for his scripting tutorials (Getting Started with Scripting in SOFTIMAGE|XSI ). It helps a lot !

Cheers,

Guillaume Laforge
Cg Artist | Jr Td



 
2005/10/31, kim aldis <kim(at)cg-soup.com>:
try this, with thanks to Raf. vDir is the normal vector, vUp is the, um, up vector. the normalisation of the vectors is quite important:
 
function OrientateToVector( vDir, vUp ) {
 

 var vp1 = XSIMath.CreateVector3();
 var vp2 = XSIMath.CreateVector3();
 
 var vMat3 = XSIMath.CreateMatrix3()
 
 vUp.NormalizeInPlace();
 vDir.NormalizeInPlace();
 

 vp1.Cross( vDir , vUp );
 vp2 .Cross( vp1, vDir );
 vp1.NormalizeInPlace();
 vp2 .NormalizeInPlace();
  
 vMat3.Set(
    vp1.x, vp1.y, vp1.z,
    vDir .x, vDir .y, vDir.z ,
    vp2.x, vp2.y, vp2.z
   );
 

 oRotation= XSIMath.CreateRotation();
 oRotation.SetFromMatrix3( vMat3 );
 oRotation.GetXYZAngles( vp1 );
 

 return vp1
 
}


From: owner-xsi(at)Softimage.COM [mailto: owner-xsi(at)Softimage.COM] On Behalf Of guillaume laforge
Sent: 31-October-2005 12:34
To: XSI(at)Softimage.COM
Subject: Scripting : Align along normal

 
Hi list :-)
 
I'm learning XSIMath and I can't find how to align an object along the POLYGON normal ? My script seems to work but it doesn't align perfectly along polygon normal :-/. As I'm new to those things I'm wondering if I miss something in the 3d math logic or if in XSI the polygon normal is a special case (compared to normals on points ).
 
Here is my script :
 

//AlignAlongNormal.js
//Select one polygon, run the script and pick an object.
//-----------------------------------------------------

//Get the selected polygon
var polySel = Selection.item(0).subcomponent.componentcollection;
var oPolygonFace = polySel(0);
//Get the Polygon Normal
var oPolygonNode = oPolygonFace.Nodes(0);
var >
//Get the Polygon points
1 );
//-----------------------------------------
//Find the Position.
var oPos = XSIMath.CreateVector3();
for ( var i = 0 ; i < oNborsPoints.count ; i++ )
{
oPos.x = oPos.x+oNborsPoints(i).Position.x;
oPos.y = oPos.y+oNborsPoints(i).Position.y;
oPos.z = oPos.z +oNborsPoints(i).Position.z;
}
oPos.x = oPos.x/oNborsPoints.count;
oPos.y = oPos.y/oNborsPoints.count;
oPos.z = oPos.z /oNborsPoints.count; 

//-------------Align the picked object------------------
//Pick session
var oPicked = PickObject ("Please pick an object") ;
var buttonChoice = oPicked.Value( "ButtonPressed" ) ;
var MyObj = oPicked.item(2);

//Rotation
MyObj.kinematics.global.transform = RotationFromPoly(oNormal) ;

//Position
MyObj.Kinematics.Global.Parameters("posx").value = oPos.x;
MyObj.Kinematics.Global.Parameters("posy").value = oPos.y;
MyObj.Kinematics.Global.Parameters("posz").value = oPos.z;

//**********From Ray-T Function**********
function RotationFromPoly(polyNormal)
{
var upVec = XSIMath.CreateVector3();
upVec.set(0, 1, 0);
var axis = XSIMath.CreateVector3();
axis.cross(upVec, polyNormal);
var angle = Math.acos(upVec.dot(polyNormal));
var rot = XSIMath.CreateRotation();
rot.SetFromAxisAngle(axis, angle);
var transfo = XSIMath.CreateTransform();
transfo.SetRotation(rot);

return transfo;
}

Any advice from experienced user is welcome !

Cheers,

Guillaume Laforge
CG artist | Jr td :)

 



Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.