Reputation: 21
My Google-fu has only turned up one result that returns the point of intersection between a ray and a triangle: http://geomalgorithms.com/a06-_intersect-2.html
And I am having absolutely no luck compiling it. I have done this for the points/vectors:
typedef struct {
float x, y, z;
} Vector;
typedef struct {
float x, y, z;
} Point;
typedef struct {
Vector P0, P1;
} Ray;
typedef struct {
Point V0, V1, V2;
} Triangle;
And then it throws errors about this code:
u = T.V1 - T.V0;
v = T.V2 - T.V0;
n = u * v;
"No match for operator - (operand types are Point and Point)"
So then I rewrote all of the code to be like this:
u.x=T.V1.x-T.V0.x;
u.y=T.V1.y-T.V0.y;
u.z=T.V1.z-T.V0.z;
v.x=T.V2.x-T.V0.x;
v.y=T.V2.y-T.V0.y;
v.z=T.V2.z-T.V0.z;
n.x=u.x*v.x;
n.y=u.y*v.y;
n.z=u.z*v.z;
But it still says the ray { 3, 1, -3 } { 3, -1, -3 } is not inside the triangle { -10, 0, -10 } { 10, 0, 10 } { 10, 0, -10 }. I checked this in Blender, though, and the ray indeed intersects the triangle.
Can anyone please point out what I'm doing incorrectly, or link to another page with a function that will return the exact point where a line segment intersects a triangle? I would rather do this in C than have to link some vector library. Thank you!
Upvotes: 0
Views: 135
Reputation: 413
I believe the author of the page did not intend to publish valid out-of-the-box C++ there, as hinted by the quotes he used to write the name of the language. To turn that pseudocode into compilable code, you would need to implement a few operators yourself, such as the cross product of two 3D vectors (this is trivial, by the way).
Assume that classes are already given for the objects: ...
Upvotes: 0
Reputation: 17114
Here is the problem:
n = u * v;
This has to be interpreted as the vector product n = u x v, not component-wise multiplication.
Upvotes: 1