POV-Ray : Newsgroups : povray.advanced-users : Deriving a matrix from a transform Server Time
24 Nov 2024 19:22:14 EST (-0500)
  Deriving a matrix from a transform (Message 1 to 2 of 2)  
From: David Wallace
Subject: Deriving a matrix from a transform
Date: 30 Mar 2005 00:20:39
Message: <424a3727$1@news.povray.org>
If I have a complex transform like:

<code>
#macro ReorientCenter(Point1,Axis1,Point2,Axis2)
   #local vX1=vnormalize(Axis1);
   #local vX2=vnormalize(Axis2);
   #local vY=vnormalize(vcross(vX1,vX2));
   #local vZ1=vnormalize(vcross(vX1,vY));
   #local vZ2=vnormalize(vcross(vX2,vY));
   translate -Point1
   matrix < vX1.x, vY.x,vZ1.x, vX1.y,vY.y,vZ1.y, vX1.z,vY.z, vZ1.z, 0,0,0 >
   matrix < vX2.x,vX2.y,vX2.z,  vY.x,vY.y, vY.z, vZ2.x,vZ2.y,vZ2.z, 0,0,0 >
   translate Point2
#end
</code>

How would I retrieve the resulting matrix so that I could use it in a file like 
this:

#write (lFile, "object { Lamb matrix <",mxx, mxy, mxz, myx, myy, myz, mzx, mzy, 
mzz, mtx, mty ,mtz,"> }\n")

My guess would be this:

<code>
#macro vtransform(vec, trans)
    #local fn = function { transform { trans } }
    #local result = (fn(vec.x, vec.y, vec.z));
    result
#end

#macro Trans_Matrix(trn)
   #declare _mtx = array[12]
   #local _rt = vtransform(<0,0,0>,trn);
   #set _mtx[9] = _rt.x;
   #set _mtx[10] = _rt.y;
   #set _mtx[11] = _rt.z;
   #local _xt = vtransform(x,trn)-_rt;
   #set _mtx[0] = _xt.x;
   #set _mtx[1] = _xt.y;
   #set _mtx[2] = _xt.z;
   #local _yt = vtransform(y,trn)-_rt;
   #set _mtx[3] = _yt.x;
   #set _mtx[4] = _yt.y;
   #set _mtx[5] = _yt.z;
   #local _zt = vtransform(z,trn)-_rt;
   #set _mtx[6] = _zt.x;
   #set _mtx[7] = _zt.y;
   #set _mtx[8] = _zt.z;
#end

#declare trnLamb = transform { ReorientCenter(org, ori, trg, pnt) }
Trans_Matrix(trnLamb)
#write (lFile, "object { Lamb matrix <")
#local i = 0;
#while (i<12)
   #write (lFile, _mtx[i])
   #set i = i + 1;
#end
#write (lFile, "> }\n")
</code>

Does anyone have an easier solution?

--------------
David Wallace
TenArbor Consulting
"Just In Time Cash"
www.tenarbor.com
1-866-572-CASH


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Deriving a matrix from a transform
Date: 30 Mar 2005 20:15:01
Message: <web.424b4def1c5f7f5152d573c20@news.povray.org>
David Wallace <dar### [at] earthlinknet> wrote:
> If I have a complex transform like:
>
> <code>
> #macro ReorientCenter(Point1,Axis1,Point2,Axis2)
>    #local vX1=vnormalize(Axis1);
>    #local vX2=vnormalize(Axis2);
>    #local vY=vnormalize(vcross(vX1,vX2));
>    #local vZ1=vnormalize(vcross(vX1,vY));
>    #local vZ2=vnormalize(vcross(vX2,vY));
>    translate -Point1
>    matrix < vX1.x, vY.x,vZ1.x, vX1.y,vY.y,vZ1.y, vX1.z,vY.z, vZ1.z, 0,0,0 >
>    matrix < vX2.x,vX2.y,vX2.z,  vY.x,vY.y, vY.z, vZ2.x,vZ2.y,vZ2.z, 0,0,0 >
>    translate Point2
> #end
> </code>
>
> How would I retrieve the resulting matrix so that I could use it in a file like
> this:
>
> #write (lFile, "object { Lamb matrix <",mxx, mxy, mxz, myx, myy, myz, mzx, mzy,
> mzz, mtx, mty ,mtz,"> }n")
>
> My guess would be this:
>
> <code>

.....

> </code>
>
> Does anyone have an easier solution?

Apart from some possible optimizations, an illegal keyword and questionable
use of a global variable and, your guess seems ok. (Which POV-Ray version
are you using btw. ?)

- But why do you want to do it that way instead of just writing out the
transformations to the file directly ?

Like this:


#macro AppendReorientCenter(FileName, Point1, Axis1, Point2, Axis2)

  #local vX1 = vnormalize(Axis1);
  #local vX2 = vnormalize(Axis2);
  #local vY = vnormalize(vcross(vX1, vX2));
  #local vZ1 = vcross(vX1, vY);
  #local vZ2 = vcross(vX2, vY);

  #fopen File FileName append
  #write(File, "ntransform {")
  #write(File, "n  translate ", -Point1)
  #write(File, "n  matrix <")
  #write(File, "n    ", vX1.x, ", ", vY.x, ", ", vZ1.x, ", ")
  #write(File, "n    ", vX1.y, ", ", vY.y, ", ", vZ1.y, ", ")
  #write(File, "n    ", vX1.z, ", ", vY.z, ", ", vZ1.z, ", ")
  #write(File, "n    ",     0, ", ",    0, ", ",     0)
  #write(File, "n  >")
  #write(File, "n  matrix <")
  #write(File, "n    ", vX2.x, ", ", vX2.y, ", ", vX2.z, ", ")
  #write(File, "n    ",  vY.x, ", ",  vY.y, ", ",  vY.z, ", ")
  #write(File, "n    ", vZ2.x, ", ", vZ2.y, ", ", vZ2.z, ", ")
  #write(File, "n    ",     0, ", ",     0, ", ",     0)
  #write(File, "n  >")
  #write(File, "n  translate ", Point2)
  #write(File, "n}")
  #fclose File

#end // macro AppendReorientCenter


#declare YourFileName = "ReorientCenterTrans.txt";
#fopen FileHandle YourFileName write
#fclose FileHandle

AppendReorientCenter(
  YourFileName,
  <1, 3, 4>, <1, 5, 7>, <6, 4, 2>, <1, 9, 1>
)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.