Waitrud Weber’s blog

things and reminders for memories

ray trace of triangle

20180703

depth is scalar.
ray is normal vector.
eye is cordinate.
intersection is also coordinate and the cross-section between triangle( P1, P2, P3 ) and expanded ray.
the meaning of coordinate and vector are different but they have same type of elements like eye(x3, y3, z3) and ray( x2, y2, z2).
n is normal vector of the surface where the triangle is.

eye + depth * ray = intersection( xi, yi, zi )

P1P2 x P1P3 / | P1P2 x P1P3 | = n ( a, b, c )

 

( x3, y3, z3 ) +  depth*(x2, y2, z2 ) =( xi, yi, zi )

d = -a*xi -b*yi -c*zi

d = -a*(x3+depth*x2)  -b*(y3+depth*y2)  -c*(z3+depth*z2)

 

P1(x4, y4, z4)

d = -a*x4 -b*y4 -c*z4

-a*(x3+depth*x2)  -b*(y3+depth*y2)  -c*(z3+depth*z2)  = -a*x4 -b*y4 -c*z4

a*( x4 -x3 - depth*x2) + b* (y4-y3-depth*y2)  + c*(z4-z3-depth*z2) = 0 

a*x4 -a*x3 + b*y4 - b*y3 + c*z4 -c*z3 -depth*(a*x2 + b*y2 + c*z2) = 0

depth = ( a*x4 -a*x3 + b*y4 - b*y3 + c*z4 -c*z3 ) / (a*x2 + b*y2 + c*z2) 

 

we could ensure a, b, c, d are calculated by use of cross on the above and solve depth and intersection.


---------

#include "vPoint.h"
#include "vTriangle.h"
#include "vCalculation.h"
#include "vIntersection.h";

vIntersection::vIntersection() {
}

vPoint vIntersection::Intersect ( vTriangle tri, vPoint eye, vPoint ray ) {

 vCalculation *cal = nullptr;

 cal = new vCalculation ();

 vPoint n = tri.getNormal ();
 double d = -n.x*tri.p1.x - n.y*tri.p1.y - n.z * tri.p1.z;
 double depth = ( d - n.x*eye.x - n.y*eye.y - n.z*eye.z ) / ( n.x*ray.x + n.y*ray.y + n.z*ray.z );

 vPoint scale_ray = cal->scale( ray, depth);
 vPoint intersection = cal->add( eye, scale_ray);

 delete( cal );

 return intersection;
}