POV-Ray : Newsgroups : povray.advanced-users : Tranformation matrix to change coordinate spaces : Re: Tranformation matrix to change coordinate spaces Server Time
19 May 2024 03:26:08 EDT (-0400)
  Re: Tranformation matrix to change coordinate spaces  
From: Tor Olav Kristensen
Date: 6 Aug 2001 14:19:33
Message: <3B6EE0A3.31C9731C@hotmail.com>
John VanSickle wrote:
>...
> If you happen to have this matrix:
> 
> matrix < vecx.x vecx.y, vecx.z,
>          vecy.x vecy.y, vecy.z,
>          vecz.x vecz.y, vecz.z,
>          vecl.x vecl.y, vecl.z >
> 
> The following macro will invert it.  It cheats a bit, BTW,
> in that it undoes the translation component (the vecl part),
> and then undoes the rotation, scaling, and shearing that
> is done by vecz, vecy, vecz.
> 
> #macro Inverse(vecx,vecy,vecz,vecl)
>   #local vx=(vecx);
>   #local vy=(vecy);
>   #local vz=(vecz);
>   #local det=vdot(vx,vcross(vy,vz));
>   #if (det=0)
>     #fatal "Invalid matrix supplied to macro.\n"
>   #end
>   #local ca=vx.x;  #local cb=vx.y;  #local cc=vx.z;
>   #local cd=vy.x;  #local ce=vy.y;  #local cf=vy.z;
>   #local cg=vz.x;  #local ch=vz.y;  #local ci=vz.z;
> 
>   translate -(vecl)
>   matrix <
>   (ce*ci-cf*ch)/det,(cc*ch-cb*ci)/det,(cb*cf-cc*ce)/det,
>   (cf*cg-cd*ci)/det,(ca*ci-cc*cg)/det,(cc*cd-ca*cf)/det,
>   (cd*ch-cg*ce)/det,(cb*cg-ca*ch)/det,(ca*ce-cb*cd)/det, 0,0,0>
> #end
>...

Here's a shorter version of it:


#macro InvTransform(vX, vY, vZ, vL)

  #local Det = vdot(vX, vcross(vY, vZ));
  #if (Det = 0)
    #error "Macro InvTransform: Invalid matrix supplied.\n"
  #end // if
  #local vA = vcross(vY, vZ)/Det;
  #local vB = vcross(vZ, vX)/Det;
  #local vC = vcross(vX, vY)/Det;

  translate -vL
  matrix <
    vA.x, vB.x, vC.x,
    vA.y, vB.y, vC.y,
    vA.z, vB.z, vC.z,
    0,    0,    0
  >

#end // macro InvTransform


For those of you that want to see an 
example on how to use this macro;
see my example code below.


Best regards,

-- 
Tor Olav Kristensen
Email: tor### [at] hotmailcom
http://hjem.sol.no/t-o-k
http://www.crosswinds.net/~tok


// ===== 1 ======= 3 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =
// By Tor Olav Kristensen

#version 3.1;

#include "colors.inc"

// ===== 1 ======= 3 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#macro Transform(vX, vY, vZ, vL)

  #local Det = vdot(vX, vcross(vY, vZ));
  #if (Det = 0)
    #error "Macro Transform: Invalid matrix supplied.\n"
  #end // if

  matrix <
    vX.x, vX.y, vX.z,
    vY.x, vY.y, vY.z,
    vZ.x, vZ.y, vZ.z,
    vL.x, vL.y, vL.z
  >

#end // macro Transform


#macro InvTransform(vX, vY, vZ, vL)

  #local Det = vdot(vX, vcross(vY, vZ));
  #if (Det = 0)
    #error "Macro InvTransform: Invalid matrix supplied.\n"
  #end // if
  #local vA = vcross(vY, vZ)/Det;
  #local vB = vcross(vZ, vX)/Det;
  #local vC = vcross(vX, vY)/Det;

  translate -vL
  matrix <
    vA.x, vB.x, vC.x,
    vA.y, vB.y, vC.y,
    vA.z, vB.z, vC.z,
    0,    0,    0
  >

#end // macro InvTransform

// ===== 1 ======= 3 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

#declare vXX = <1, -0.3, 2>;
#declare vYY = <2, 1, 0>;
#declare vZZ = <0.3, 0, 1>;
#declare vLL = <0, -1.5, 0>;

#declare Box = box { -<1, 2, 3>, <1, 2, 3> }

object { Box pigment { color White } }

object {
  Box
  scale 1.00001 // scale 0.99999
  pigment { color Red }
  Transform(vXX, vYY, vZZ, vLL)
//  InvTransform(vXX, vYY, vZZ, vLL)
}
// Uncomment the InvTransform line above
// to see the Box transformed back.

// ===== 1 ======= 3 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =

background { color Blue /2 } 

light_source { <2, 3, -1>*100 color White }

camera {
  location <1, 1, -2>*5
  look_at <0, 0, 0>
}

// ===== 1 ======= 3 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7 =


Post a reply to this message

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