POV-Ray : Newsgroups : povray.advanced-users : A couple of questions about matrix transformations : Re: A couple of questions about matrix transformations Server Time
29 Jul 2024 02:33:16 EDT (-0400)
  Re: A couple of questions about matrix transformations  
From: Micha Riser
Date: 1 Apr 2003 17:02:29
Message: <3e8a0c75@news.povray.org>
Johannes Dahlstrom wrote:

> I was implementing a cylinder primitive to my little pet project, a
> raytracer, when I run into some problems.
> 
> The cylinder is defined the same way as in POV, by two vectors and a
> float, representing the base and apex points and the radius, respectively.
> Writing an intersection finder and other needed stuff for such a general
> case would be rather difficult, so I reasoned that it would be easiest
> (and perhaps most efficient, too?) to make the cylinder always lie along a
> coordinate axis (say, the Y axis) in object space, and then provide an
> appropriate transformation from object space to world space.
> 
> The problem is, how would I go about constructing such a transformation? I
> took a look at the relevant part of the POV source code, but didn't quite
> figure out how it does the trick. I could of course just grab the code,
> but I'd rather understand the algorithm behind it.

I exactly have implemented this for my ray-tracer recently. My base cylinder 
is z-axe aligned radius 1, length 1. To allow arbitrary cylinders from base 
and cap points you need to implement an axis-rotate (vaxis_rotate in pov I 
think). You can do this from:

    // R(phi,N) = cos(phi)*I + (1-cos(phi))*N^T*N + sin(phi) * A
    // with phi: angle; N: axis; A = [[0 N3 -N2] [-N3 0 N1] [N2 -N1 0]]

Where R(phi,N) is the rotation matrix, N is the axe to rotate around. You 
can see this implemented in c++ in 
http://www.povworld.org/raytracer/cvsweb.cgi/~checkout~/raytracer/raytracer/transformatable.cpp?rev=1.9&content-type=text/plain

Then for the rotation matrix for your cylinder rotate around the axe which 
stands orthogonal on the base-cylinder-axe and the to-cylinder-axe by the 
necessary angle. Again you can see this in c++ in
http://www.povworld.org/raytracer/cvsweb.cgi/~checkout~/raytracer/raytracer/cylinder.cpp?rev=1.5&content-type=text/plain

> 
> I'd also like some insight into how to invert a transformation (4x4)
> matrix. I did a bit googling but didn't come by anything too relevant.
> 

Basically there is the Gauss-algorithm for inverting matrixes. But when you 
have fixed size you probably want to hardcode it. I use 3x3 matrixes in my 
ray-tracer so this time the code won't be usable for you. (matrix.cpp would 
be the file if you're intersted)

At
http://www.povworld.org/raytracer/cvsweb.cgi/raytracer/raytracer/
you can browse the whole CVS version of my ray-tracer.

- Micha

-- 
POV-Ray Objects Collection: http://objects.povworld.org


Post a reply to this message

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