|
![](/i/fill.gif) |
Ooops.
I think that my "Intersection at one or two edges"
comment in the code I just posted is wrong.
See my new RayIntersectsTriangle() macro below.
If one does not want the edges and vertices to count as
part of the triangle, then just replace the >= and <=
operators below with > and < operators.
Tor Olav
Tor Olav Kristensen wrote:
>...
> I think that my RayIntersectsTriangle() macro in the code below
> provides such a shortcut (*), but I'm not sure yet if it is optimal.
>...
> // ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
> // Copyright 2002 by Tor Olav Kristensen
> // Email: tor### [at] hotmail com
> // http://www.crosswinds.net/~tok/povray
> // ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
>
> #version 3.5;
> #include "colors.inc"
>
> // ===== 1 ======= 2 ======= 3 ======= 4 ======= 5 ======= 6 ======= 7
> // Intersection macros
>
> #macro RayIntersectsTriangle(pRay, vRay, pA, pB, pC)
>
> #local vRA = pA - pRay;
> #local vRB = pB - pRay;
> #local vRC = pC - pRay;
> #local vABN = vcross(vRA, vRB);
> #local vBCN = vcross(vRB, vRC);
> #local vCAN = vcross(vRC, vRA);
> #local STP = vdot(vABN, vRC);
> #if (STP = 0)
> #debug "Macro RayIntersectsTriangle: "
> #debug "Degenerate triangle or "
> #debug "origin of ray lies in same plane as triangle\n"
> #local Intersect = false;
> #else
> #local AB = vdot(vRay, vABN);
> #local BC = vdot(vRay, vBCN);
> #local CA = vdot(vRay, vCAN);
> #if (AB = 0 | BC = 0 | CA = 0)
> #local Intersect = true; // Intersection at one or two edges
> #else
> #if (STP > 0)
> #local Intersect = (AB > 0 & BC > 0 & CA > 0);
> #else
> #local Intersect = (AB < 0 & BC < 0 & CA < 0);
> #end // if
> #end // if
> #end // if
>
> Intersect
>
> #end // macro RayIntersectsTriangle
#macro RayIntersectsTriangle(pRay, vRay, pA, pB, pC)
#local vRA = pA - pRay;
#local vRB = pB - pRay;
#local vRC = pC - pRay;
#local vABN = vcross(vRA, vRB);
#local vBCN = vcross(vRB, vRC);
#local vCAN = vcross(vRC, vRA);
#local STP = vdot(vABN, vRC);
#if (STP = 0)
#debug "Macro RayIntersectsTriangle: "
#debug "Degenerate triangle or "
#debug "origin of ray lies in same plane as triangle\n"
#local Intersect = false;
#else
#local AB = vdot(vRay, vABN);
#local BC = vdot(vRay, vBCN);
#local CA = vdot(vRay, vCAN);
#if (STP > 0)
#local Intersect = (AB >= 0 & BC >= 0 & CA >= 0);
#else
#local Intersect = (AB <= 0 & BC <= 0 & CA <= 0);
#end // if
#end // if
Intersect
#end // macro RayIntersectsTriangle
Post a reply to this message
|
![](/i/fill.gif) |