Re: Fw: [script] normal vector to xyz rotation?
| Date : Mon, 5 Sep 2005 22:40:28 +0200 |
| To : <XSI(at)Softimage.COM> |
| From : "peterb" <peter_b(at)skynet.be> |
| Subject : Re: Fw: [script] normal vector to xyz rotation? |
|
yes,
as it seems the
formula for arccos below is correct.
(i've found several sources that
confirm)
however, in case of a 1 you get a
division by zero.
but... the arccos of 1 is 0, and the arccos of -1
is PI
so thats quickly added into the script with a
condition, and yet a few more lines. where
the simple "arccos" should be all :-(
so I got my 2 scripts running (hurrah!), but the
version with the arccos isnt giving proper results.
could be my math, could be my script... or
both
I've added them at the bottom of this mail, if
anyone wants to look, see whats wrong
(or laugh at my scripting :-)
peter
That would be correct (cos always returns a value between -1 and 1) - if you've got a dot product greater than that your vectors weren't unit vectors (normalized). 'this is the script that is working fine, based on rafaelle's suggested
method
'select a mesh and run, it creates nulls, aligned to the
mesh's points
'---------------------------------------------------
option explicit
dim sel, oGeometry, oPoints, pnt, oNull
Dim V1, V2, vUP, oMatrix, vNORM, transfo
set V1 = XSIMath.CreateVector3
set V2 = XSIMath.CreateVector3
set vUP = XSImath.CreateVector3
set oMatrix = XSIMath.CreateMatrix3
set vNORM = XSImath.CreateVector3
set transfo = XSIMath.CreateTransform
vUP.set 0.0,1.0,0.0
set sel = selection(0)
set oGeometry = sel.activeprimitive.geometry
set oPoints = oGeometry.Points
for each pnt in oPoints
vNORM.set pnt.normal.x,pnt.normal.y,pnt.normal.z
MatrixFromNormal (vNORM)
transfo.setRotationFromMatrix3 oMatrix
transfo.SetTranslationFromValues pnt.position.x, pnt.position.y,
pnt.position.z
set ("name" & pnt.index)
oNull.Kinematics.local.transform = transfo
next
Function MatrixFromNormal (norm)
V1.cross norm,vUP
V2.cross V1,norm
V1.normalizeInPlace
V2.normalizeInPlace
norm.normalizeInPlace
oMatrix.set V1.x, V1.y, V1.z,_
norm.x, norm.y, norm.z,_
V2.x, V2.y, V2.z
set MatrixFromNormal = oMatrix
end Function
'----------------------------------------------------
'this is the script that is not working fine, based on the math
method
'the nulls are not properly aligned...
'--------------------------------------------------- set sel = selection(0)
set oGeometry = sel.activeprimitive.geometry
set oPointer = oGeometry.Points
set rot = XSIMath.CreateRotation
set vUP = XSIMath.CreateVector3
set axis = XSIMath.CreateVector3
set vNORM = XSIMath.CreateVector3
set vPos = XSIMath.CreateVector3
set vRot = XSIMath.CreateVector3
vUP.set 0,1,0
for i = 0 to oPointer.count-1
set oPoint = oGeometry.Points(i)
set vPos = oPoint.Position
set vNORM = oPoint.Normal
RotationFromNormal (vNORM)
rot.GetXYZAngles vRot
set ("name" & i)
oNull.Kinematics.local.parameters("posx").value = vPos.x
oNull.Kinematics.local.parameters("posy").value = vPos.y
oNull.Kinematics.local.parameters("posz").value = vPos.z
oNull.Kinematics.global.parameters("rotx").value =
xsimath.radianstodegrees(vRot.x)
oNull.Kinematics.global.parameters("roty").value =
xsimath.radianstodegrees(vRot.y)
oNull.Kinematics.global.parameters("rotz").value =
xsimath.radianstodegrees(vRot.z)
next
Function RotationFromNormal (norm)
axis.cross vUP, norm
DOT = vUP.dot(norm)
if DOT => 1 then
angle = 0
elseif DOT <= -1 then
angle = XSIMath.PI
else
angle = 2*atn(1) - atn(DOT/sqr(1-DOT*DOT))
end if
rot.SetFromAxisAngle axis, angle
set RotationFromNormal = rot
end Function
'----------------------------------------------------------------
|
- References:
- Fw: [script] normal vector to xyz rotation?
- From: "peterb" <peter_b(at)skynet.be>
- Re: Fw: [script] normal vector to xyz rotation?
- From: Alan Jones <skyphyr(at)gmail.com>
- Fw: [script] normal vector to xyz rotation?
| DATE: | << | >> | THREAD: | << | >> | INDEX: | Main | Thread |
|---|
- Previous by Date: Re: [script] normal vector to xyz rotation?
- Next by Date: Re: [script] normal vector to xyz rotation?
- Previous by Thread: Re: [script] normal vector to xyz rotation?
- Next by Thread: Re: [script] normal vector to xyz rotation?
- Index(es):
| Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available. |