greedsin
greedsin

Reputation: 1272

Inside-Outside-Test of an Triangle

I try to write an method bool intersect(const Ray& ray, Intersection& intersection) that returns true, when the Intersection is inside the Triangle.

What i've done so far , is check if there are Points on the Plane, that is created by 2 Vectors of the Triangle.

The Problem is now to check, if the Point is inside the Triangle.I use barycentric Coordinates

Vec3 AB = b_-a_;
Vec3 AC = c_-a_;
double areaABC = vec_normal_triangle.dot(AB.cross(AC));

Vec3 PB = b_-intersection.pos;
Vec3 PC = c_-intersection.pos;
double alpha = vec_normal_triangle.dot(PB.cross(PC));

Vec3 PA = a_-position.pos;
double beta = vec_normal_triangle.dot(PC.cross(PA));
double gamma = 1-alpha-beta;

if((beta+gamma) < 1 && beta > 0 && gamma > 0) {
    return true;
}

Actually its not even a triangle, just about random Points. Can someone explain me or knows how i compute the barycentric Coordinates for 3 given Vectors?

Upvotes: 1

Views: 626

Answers (1)

pixcel
pixcel

Reputation: 66

Assuming vec_normal_triangle is the vector computed as AB.cross(AC) normalized (in other words, the triangle's normal), you should divide alpha and beta by areaABC to get the barycentric coordinates of the intersecton point.

double alpha = vec_normal_triangle.dot(PB.cross(PC)) / areaABC;

and

double beta = vec_normal_triangle.dot(PC.cross(PA)) / areaABC;

This normalizes alpha and beta so that your computation of gamma and comparison against 1 make sense.

I'd also like to make a suggestion. To avoid recomputation and make the code a bit cleaner you could replace your test with the following.

if(alpha > 0 && beta > 0 && gamma > 0) {
    return true;
}

Aside from that, I see that you first use intersection.pos and then position.pos. Is this intentional? My guess is that you need to use intersection.pos both times.

Upvotes: 2

Related Questions