POV-Ray : Newsgroups : povray.general : Re: Rotating vectors in 3D Server Time
30 Jul 2024 18:18:20 EDT (-0400)
  Re: Rotating vectors in 3D (Message 1 to 1 of 1)  
From: Tor Olav Kristensen
Subject: Re: Rotating vectors in 3D
Date: 14 Oct 2008 18:55:47
Message: <48f52373@news.povray.org>
Tor Olav Kristensen wrote:
> Severi Salminen wrote:
>> My head is spinning. I have a fairly simple task:
>>
>> V1 = 0,1,0 (this is a vector pointing up along y-axis)
>> V2 = user defined unit vector pointing anywhere.
>>
>> How do I rotate V1 using rotateX(), rotateY() and rotateZ() so that it
>> equals V2?
>>
>> This would be intersting, too:
>>
>> What is the resulting 4x4 transformation matrix that does the same thing?
>>
>> Actually, I'd prefer just the transformation matrix because I guess I
>> could avoid using trigonometric functions back and forth.
> 
> You are right: No trigonometric functions are needed.
> 
> Below is a rewritten version of the Reorient_Trans() macro.
> 
> Follow up set to povray.general
> 

The code below shows how the axis rotate transformation and the
reorient transformation are related.


// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
// Copyright 2008 Tor Olav Kristensen
// http://subcube.com
// Relationship between Reorient_Trans() and Axis_Rotate_Trans()
// ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7

#version 3.6;

#include "colors.inc"

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

#macro Reorient_Trans(v1, v2)

   #local v1n = vnormalize(v1);
   #local v2n = vnormalize(v2);
   #local vA = vcross(v1n, v2n);
   #local Sin = vlength(vA);
   #local Cos = vdot(v1n, v2n);
   #local M = 1 - Cos;
   #local vAn = vnormalize(vA);
   #local X = vAn.x;
   #local Y = vAn.y;
   #local Z = vAn.z;

   transform {
     matrix <
       X*X*M +   Cos, Y*X*M + Z*Sin, Z*X*M - Y*Sin,
       X*Y*M - Z*Sin, Y*Y*M +   Cos, Z*Y*M + X*Sin,
       X*Z*M + Y*Sin, Y*Z*M - X*Sin, Z*Z*M +   Cos,
                   0,             0,             0
     >
   }

#end // macro Reorient_Trans


#macro Axis_Rotate_Trans(vA, Angle)

   #local Phi = radians(Angle);
   #local Sin = sin(Phi);
   #local Cos = cos(Phi);
   #local M = 1 - Cos;
   #local vAn = vnormalize(vA);
   #local X = vAn.x;
   #local Y = vAn.y;
   #local Z = vAn.z;

   transform {
     matrix <
       X*X*M +   Cos, Y*X*M + Z*Sin, Z*X*M - Y*Sin,
       X*Y*M - Z*Sin, Y*Y*M +   Cos, Z*Y*M + X*Sin,
       X*Z*M + Y*Sin, Y*Z*M - X*Sin, Z*Z*M +   Cos,
                   0,             0,             0
     >
   }

#end // macro Axis_Rotate_Trans

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

-- 
Tor Olav


Post a reply to this message

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