POV-Ray : Newsgroups : povray.general : Q: reflected vector Server Time
15 Nov 2024 17:13:22 EST (-0500)
  Q: reflected vector (Message 1 to 8 of 8)  
From: Margus Ramst
Subject: Q: reflected vector
Date: 31 Dec 1998 16:17:00
Message: <368BE775.4778132F@peak.edu.ee>
Hi all, I'm once again having math trouble :(
A macro I'm writing needs to calculate reflected vectors (it simulates
reflected caustics in Superpatch, I'll probably post it soon...)
However, the function I use to calculate the reflected vectors has 2
shortcomings:
1) it doesn't work when the incoming ray is parallel/anti-parallel with the
surface normal (the vaxis_rotate function would have the rotation axis of
<0,0,0>)
2) because I use asin, the reflecion vector is incorrect when the ray hits the
surface at >=0 degrees (for example when surface normals point into the object
in CSG)
Here's the macro I use to calculate the vector; if anybody could provide a
solution to these problems or give a more optimal method, I'd be thankful
indeed.

Margus

//V - ray direction vector (from intersection to origin)
//N - surface normal

#macro v_mirror(V,N)
    (
    vnormalize
        (
        vaxis_rotate
            (
            (V),
            vcross((V),N),
            2*degrees(asin(vlength(vcross(vnormalize((V)),vnormalize(N)))))
            )
        )
    )
#end


Post a reply to this message

From: John VanSickle
Subject: Re: Q: reflected vector
Date: 1 Jan 1999 12:47:14
Message: <368D0AA0.3019DD1C@erols.com>
Margus Ramst wrote:
> 
> Hi all, I'm once again having math trouble :(
> A macro I'm writing needs to calculate reflected vectors (it simulates
> reflected caustics in Superpatch, I'll probably post it soon...)
> However, the function I use to calculate the reflected vectors has 2
> shortcomings:
> 1) it doesn't work when the incoming ray is parallel/anti-parallel with the
> surface normal (the vaxis_rotate function would have the rotation axis of
> <0,0,0>)
> 2) because I use asin, the reflecion vector is incorrect when the ray hits the
> surface at >=0 degrees (for example when surface normals point into the object
> in CSG)
> Here's the macro I use to calculate the vector; if anybody could provide a
> solution to these problems or give a more optimal method, I'd be thankful
> indeed.
> 
> Margus
> 
> //V - ray direction vector (from intersection to origin)
> //N - surface normal
> 
> #macro v_mirror(V,N)
>     (
>     vnormalize
>         (
>         vaxis_rotate
>             (
>             (V),
>             vcross((V),N),
>             2*degrees(asin(vlength(vcross(vnormalize((V)),vnormalize(N)))))
>             )
>         )
>     )
> #end

The formula you appear to be using is really strange looking.

Why not use this:

#macro v_mirror(V,N)
  ( V-2*N*vdot(V,N) )
#end

Regards,
John


Post a reply to this message

From: Ronald L  Parker
Subject: Re: Q: reflected vector
Date: 1 Jan 1999 13:08:47
Message: <36920d89.71268632@news.povray.org>
On Thu, 31 Dec 1998 23:07:01 +0200, Margus Ramst <mar### [at] peakeduee>
wrote:

>Hi all, I'm once again having math trouble :(
>A macro I'm writing needs to calculate reflected vectors (it simulates
>reflected caustics in Superpatch, I'll probably post it soon...)
>However, the function I use to calculate the reflected vectors has 2
>shortcomings:

Why not determine the component of the ray that does not lie along the

normal vector, negate it, and add it back to the componenent that does
lie along the normal?  something like this (untested) code:

// This assumes that R is relative to the intersection point and
// points AWAY from the intersection point.  This is probably not
// the way you have it set up if you're using the SuperPatch.

// R = ray
// N = normal

#macro mirror( R, N )
  #local nN = vnormalize( N );
  #local par = vdot( R, nN );
  #local perp = R-par*nN;
  par*nN-perp
#end

Something else to chew on: I've added XenoArch's projected_through
patch to the next version of the superpatch, and that will allow easy
simulation of reflective caustics from planar surfaces, for the price
of an extra light source per reflected light source per reflective
surface.


Post a reply to this message

From: Margus Ramst
Subject: Re: Q: reflected vector
Date: 1 Jan 1999 22:15:57
Message: <368d8f6d.0@news.povray.org>
Thanks, this might be it (didn't quite get it yet, my brain isn't working
too well around this time of the year...)

Margus

Ronald L. Parker wrote in message <36920d89.71268632@news.povray.org>...

>Why not determine the component of the ray that does not lie along the
>
>normal vector, negate it, and add it back to the componenent that does
>lie along the normal?  something like this (untested) code:
>
>// This assumes that R is relative to the intersection point and
>// points AWAY from the intersection point.  This is probably not
>// the way you have it set up if you're using the SuperPatch.
>
>// R = ray
>// N = normal
>
>#macro mirror( R, N )
>  #local nN = vnormalize( N );
>  #local par = vdot( R, nN );
>  #local perp = R-par*nN;
>  par*nN-perp
>#end
>
>Something else to chew on: I've added XenoArch's projected_through
>patch to the next version of the superpatch, and that will allow easy
>simulation of reflective caustics from planar surfaces, for the price
>of an extra light source per reflected light source per reflective
>surface.
>


Post a reply to this message

From: John VanSickle
Subject: Re: Q: reflected vector
Date: 1 Jan 1999 22:28:16
Message: <368D92D0.AD8424B2@erols.com>
Ronald L. Parker wrote:
> 
> On Thu, 31 Dec 1998 23:07:01 +0200, Margus Ramst <mar### [at] peakeduee>
> wrote:
> 
> >Hi all, I'm once again having math trouble :(
> >A macro I'm writing needs to calculate reflected vectors (it simulates
> >reflected caustics in Superpatch, I'll probably post it soon...)
> >However, the function I use to calculate the reflected vectors has 2
> >shortcomings:
> 
> Why not determine the component of the ray that does not lie along the
> 
> normal vector, negate it, and add it back to the componenent that does
> lie along the normal?  something like this (untested) code:
> 
> #macro mirror( R, N )
>   #local nN = vnormalize( N );
>   #local par = vdot( R, nN );
>   #local perp = R-par*nN;
>   par*nN-perp
> #end

The par*nN term needs to be doubled.  I posted the correct code earlier.

Regards,
John


Post a reply to this message

From: Ronald L  Parker
Subject: Re: Q: reflected vector
Date: 2 Jan 1999 00:14:41
Message: <368da793.110707913@news.povray.org>
On Fri, 01 Jan 1999 22:30:24 -0500, John VanSickle
<van### [at] erolscom> wrote:

>Ronald L. Parker wrote:
>> 
>> On Thu, 31 Dec 1998 23:07:01 +0200, Margus Ramst <mar### [at] peakeduee>
>> wrote:
>> 
>> >Hi all, I'm once again having math trouble :(
>> >A macro I'm writing needs to calculate reflected vectors (it simulates
>> >reflected caustics in Superpatch, I'll probably post it soon...)
>> >However, the function I use to calculate the reflected vectors has 2
>> >shortcomings:
>> 
>> Why not determine the component of the ray that does not lie along the
>> 
>> normal vector, negate it, and add it back to the componenent that does
>> lie along the normal?  something like this (untested) code:
>> 
>> #macro mirror( R, N )
>>   #local nN = vnormalize( N );
>>   #local par = vdot( R, nN );
>>   #local perp = R-par*nN;
>>   par*nN-perp
>> #end
>
>The par*nN term needs to be doubled.  I posted the correct code earlier.

If you don't mind my asking... um... which one, and why?  The doubling

you seek is there, I think, in that if you put the last two lines
together you get par*nN-(R-par*nN) = 2*par*nN-R .  I was just trying 
to make it look a little less like magic and a little more like simple

math.

Or perhaps the difference can best be illuminated (heh) by some bad
ASCII art:

A
\   |
 \  |
  \ |
   \|
N---*
   /|\
  / | \
 /  |  \
/   |   \
R   P    B

Assuming all the vectors are pointing away from the * at the center,
and the line marked with a P is of course the reflecting surface, I
was trying to provide the equation for the ray marked 'A'.  I assume
you're assuming that R is pointing inward, in which case your answer
is correct for A.  With my assumptions (admittedly chosen to make my
job easier) and your equation, one would get B.  That's why I made
sure to make my assumptions about the parameters known:

>// This assumes that R is relative to the intersection point and
>// points AWAY from the intersection point.  This is probably not
>// the way you have it set up if you're using the SuperPatch.


Post a reply to this message

From: John VanSickle
Subject: Re: Q: reflected vector
Date: 3 Jan 1999 19:51:49
Message: <3690112B.A01CCE0B@erols.com>
Ronald L. Parker wrote:
> 
> On Fri, 01 Jan 1999 22:30:24 -0500, John VanSickle
> <van### [at] erolscom> wrote:
> 
> >Ronald L. Parker wrote:
> >>
> >> Why not determine the component of the ray that does not lie along the
> >>
> >> normal vector, negate it, and add it back to the componenent that does
> >> lie along the normal?  something like this (untested) code:
> >>
> >> #macro mirror( R, N )
> >>   #local nN = vnormalize( N );
> >>   #local par = vdot( R, nN );
> >>   #local perp = R-par*nN;
> >>   par*nN-perp
> >> #end
> >
> >The par*nN term needs to be doubled.  I posted the correct code earlier.
> 
> If you don't mind my asking... um... which one, and why?  The doubling
> 
> you seek is there, I think, in that if you put the last two lines
> together you get par*nN-(R-par*nN) = 2*par*nN-R .  I was just trying
> to make it look a little less like magic and a little more like simple
> math.

Yep, I misunderstood how the last two lines work together, but now that
I do, 2*par*nN-R is wrong by a factor of -1.



> 
> Or perhaps the difference can best be illuminated (heh) by some bad
> ASCII art:
> 
> A
> \   |
>  \  |
>   \ |
>    \|
> N---*
>    /|\
>   / | \
>  /  |  \
> /   |   \
> R   P    B
> 
> Assuming all the vectors are pointing away from the * at the center,

Which is wrong, because the original ray is supposed to be *incoming*
(see the original post).

And it doesn't matter if the direction is reversed on the N vector;
multiplying N by <-1,-1,-1> causes par to be negated, which undoes the
negative scaling of N when par and nN are multiplied together.

Regards,
John


Post a reply to this message

From: Ronald L  Parker
Subject: Re: Q: reflected vector
Date: 3 Jan 1999 21:50:58
Message: <36922a5e.82603853@news.povray.org>
On Sun, 03 Jan 1999 19:54:03 -0500, John VanSickle
<van### [at] erolscom> wrote:

>Yep, I misunderstood how the last two lines work together, but now that
>I do, 2*par*nN-R is wrong by a factor of -1.

Only if you assume that the ray you're mirroring is incoming, which I
specifically stated I was not.  I even stated that that was not likely
to be how he's doing it.

>Which is wrong, because the original ray is supposed to be *incoming*
>(see the original post).

The original post gives these parameters:

//V - ray direction vector (from intersection to origin)
//N - surface normal

"from intersection to origin" sounds like it's pointing away from the
intersection point, no?

>And it doesn't matter if the direction is reversed on the N vector;
>multiplying N by <-1,-1,-1> causes par to be negated, which undoes the
>negative scaling of N when par and nN are multiplied together.

I didn't say it did matter if N is reversed.  In fact, I verified that
it didn't matter before I posted my solution.  If R is reversed,
however, the sign of both terms is reversed and my result is as
correct as yours, with the added benefits of (a) taking the parameters
the original poster was apparently already using and (b) stating
without uncertainty what exact parameters it does take.  
 
In any case, by now the original poster has resolved any sign
discrepancies he may have encountered, so it's probably safe to drop
it.


Post a reply to this message

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