View Full Version : Line Triangle intersection - Is this output correct?

11-10-2012, 05:41 PM
Hello, I guess I missed too many classes on geometry.
This code should compute the intersection between a line, with origin at s1 and s2 as direction, and a triangle, with vertices p0, p1 and p2. I'm using a function from GLM extensions to chack for these intersections. It writes the result to intersec

#include <cstdio>
#include <glm/glm.hpp>
#include <glm/gtx/intersect.hpp>

int main() {

glm::vec3 p0(1,0,1), p1(1,0,-1), p2(1,1,0);
glm::vec3 s1(0,0,0), s2(1,0,0);
glm::vec3 intersec;

if ( glm::intersectLineTriangle(s1,s2,p0,p1,p2,intersec ) )
printf("Intersected on (%lf %lf %lf)\n",intersec.x,intersec.y,intersec.z);
printf("Not intersected\n");


Does this output make sense?

Intersected on (1.000000 0.500000 -0.000000)

Shouldn't it be (1.00000 0.000000 0.0000000)?

Dan Bartlett
11-11-2012, 03:10 AM
Looking at the source (https://github.com/g-truc/glm/blob/master/glm/gtx/intersect.hpp), the result it returns is in barycentric coordinates (http://en.wikipedia.org/wiki/Barycentric_coordinate_system_(mathematics)).