// A floating point struct fPoint { float x; float y; }; /* IsPointInTri() - * Used by IsPointInQuad(). Function takes the point and the triangle's * vertices. It finds the area of the passed triangle (v1 v2 v3), and then the * areas of the three triangles (pt v2 v3), (pt v1 v3), and (pt v1 v2). If the * sum of these three is greater than the first, then the point is outside of * the triangle. */ bool IsPointInTri(const fPoint *pt, const fPoint *v1, const fPoint *v2, const fPoint *v3) { float TotalArea = CalcTriArea(v1, v2, v3); float Area1 = CalcTriArea(pt, v2, v3); float Area2 = CalcTriArea(pt, v1, v3); float Area3 = CalcTriArea(pt, v1, v2); if((Area1 + Area2 + Area3) > TotalArea) return false; else return true; } /* CalcTriArea() - * Find the area of a triangle. This function uses the 1/2 determinant * method. Given three points (x1, y1), (x2, y2), (x3, y3): * | x1 y1 1 | * Area = .5 * | x2 y2 1 | * | x3 y3 1 | * From: http://mcraefamily.com/MathHelp/GeometryTriangleAreaDeterminant.htm */ float CalcTriArea(fPoint *v1, fPoint *v2, fPoint *v3) { float det = 0.0f; det = ((v1->x - v3->x) * (v2->y - v3->y)) - ((v2->x - v3->x) * (v1->y - v3->y)); return (det / 2.0f); }

**0**

# Is this a better point in triangle test (2D)?

Started by kingnosis, Jan 21 2005 11:10 AM

7 replies to this topic

###
#1
Members - Reputation: **267**

Posted 21 January 2005 - 11:10 AM

So I was looking around for a way to do the old point in triangle test, and I was finding a lot of ways to do it using trig. Then I found one using areas, and this was simple enough, but the only way I knew to do areas was Heron's formula, and that's a lot of sqrts (16 per triangle test, I believe).
Then I found a way to do triangle areas using the determinant. No sqrt, no cos, or sin, etc.
I'm working from this.
Is this sort of thing going to work, or am I missing something? I haven't written any testing code.
But I did write a couple of functions (one to calc area, one to compare areas). They are here:

Sponsor:

###
#2
Members - Reputation: **2059**

Posted 21 January 2005 - 12:13 PM

I didn't look at the code, but yes that should work. I'm guessing you're using the signed area of the triangle formed by an edge and the point as a predicate to see which side of the edge the point is on. This is similar to using tetrahedral volumes in 3D, which is a good method.

The only gotcha I can think of is that as the point gets very close to an edge, the predicate result becomes more or less random due to floating point error. In ray/mesh collision this is an issue because rays can 'fall through the cracks' between triangles. The solution to this problem is to use the same predicate result for any edge shared by two triangles.

If you just want to know whether a single point is inside a single triangle, that shouldn't be an issue though. Points on the edge may register as inside or outside, but whether that's a problem for you or not depends on the application.

The only gotcha I can think of is that as the point gets very close to an edge, the predicate result becomes more or less random due to floating point error. In ray/mesh collision this is an issue because rays can 'fall through the cracks' between triangles. The solution to this problem is to use the same predicate result for any edge shared by two triangles.

If you just want to know whether a single point is inside a single triangle, that shouldn't be an issue though. Points on the edge may register as inside or outside, but whether that's a problem for you or not depends on the application.

###
#3
Crossbones+ - Reputation: **2029**

Posted 21 January 2005 - 01:30 PM

I would probably not advise an algorithm like that, or based on angles, precisely because of the innacuracies. There are lots of ways, one of which is to make sure the point's sign from the edge plane being always positive (or negative) for each planes, or the analytical solution invovling barycentric coordiantes, of using plucker coordinates.

P is in the triangle (A, B, C) if it verifies,

P = A + (B - A) * t + (C - A) * u

and

(t < 1, u < 1, 0 < t + u < 1)

I think that's the inequations you have to solve. But really, the plane check should be enough.

bool b0 = (Vector(P.x - A.x, P.y - A.y) * Vector(A.y - B.y, B.x - A.x) > 0);

bool b1 = (Vector(P.x - B.x, P.y - B.y) * Vector(B.y - C.y, C.x - B.x) > 0);

bool b2 = (Vector(P.x - C.x, P.y - C.y) * Vector(C.y - A.y, A.x - C.x) > 0);

return (b0 == b1 && b1 == b2);

should work for both counter clockwise and clocksie triangles.

P is in the triangle (A, B, C) if it verifies,

P = A + (B - A) * t + (C - A) * u

and

(t < 1, u < 1, 0 < t + u < 1)

I think that's the inequations you have to solve. But really, the plane check should be enough.

bool b0 = (Vector(P.x - A.x, P.y - A.y) * Vector(A.y - B.y, B.x - A.x) > 0);

bool b1 = (Vector(P.x - B.x, P.y - B.y) * Vector(B.y - C.y, C.x - B.x) > 0);

bool b2 = (Vector(P.x - C.x, P.y - C.y) * Vector(C.y - A.y, A.x - C.x) > 0);

return (b0 == b1 && b1 == b2);

should work for both counter clockwise and clocksie triangles.

###
#4
Members - Reputation: **2059**

Posted 21 January 2005 - 02:24 PM

oliii, if I'm reading his code right I think he's pretty much doing the same thing as your plane check example.

Unless I'm screwing up the math somewhere (quite possible), the tri area test is equivalent to the perp-dot between two of the tri edges, which is exactly what your 'plane test' is doing.

However (to the OP), it's not necessary to add the areas, or whatever you're doing. As soon as the sign of one of the predicates is negative (or positive, depending on which way you do the test), the point is known to be outside the triangle and you're done. However, if all three predicates pass, the point is inside the triangle.

Anyway, maybe I missed something somewhere, in which case I assume someone will point out my error...

[Edit: Sorry, tri area isn't quite equivalent to perp-dot, but the sign is the same.]

Unless I'm screwing up the math somewhere (quite possible), the tri area test is equivalent to the perp-dot between two of the tri edges, which is exactly what your 'plane test' is doing.

However (to the OP), it's not necessary to add the areas, or whatever you're doing. As soon as the sign of one of the predicates is negative (or positive, depending on which way you do the test), the point is known to be outside the triangle and you're done. However, if all three predicates pass, the point is inside the triangle.

Anyway, maybe I missed something somewhere, in which case I assume someone will point out my error...

[Edit: Sorry, tri area isn't quite equivalent to perp-dot, but the sign is the same.]

###
#5
Members - Reputation: **2059**

Posted 21 January 2005 - 02:35 PM

To the OP, here is your point/tri test re-written to be a little more efficient and perhaps more robust:

That's untested off-the-top-of-my-head code, so I can't vouch for its correctness. But the idea is sound, and is pretty much equivalent to oliii's second example, the plane check.

bool IsPointInTri(Point p, Point v1, Point v2, Point v3)

{

if (Sign(p, v1, v2) < 0.0f)

return false;

if (Sign(p, v2, v3) < 0.0f)

return false;

if (Sign(p, v3, v1) < 0.0f)

return false;

// Or > 0.0f in case of opposite winding

}

float Sign(Point v1, Point v2, Point v3)

{

return (v1->x - v3->x) * (v2->y - v3->y) - (v2->x - v3->x) * (v1->y - v3->y);

}

That's untested off-the-top-of-my-head code, so I can't vouch for its correctness. But the idea is sound, and is pretty much equivalent to oliii's second example, the plane check.

###
#6
Members - Reputation: **267**

Posted 21 January 2005 - 03:42 PM

Thanks. The area test wasn't working the way I thought it would when I wrote a test program. But I did get it working using a mix between your Sign() func (which is basically a cross product, if I understand it correctly) and oliii's three bools, since I plan to check both clockswise and ccw triangles.

The final funcs are (for posterity)

Thanks for the help.

The final funcs are (for posterity)

float Sign(fPoint p1, fPoint p2, fPoint p3)

{

return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);

}

bool IsPointInTri(fPoint pt, fPoint v1, fPoint v2, fPoint v3)

{

bool b1, b2, b3;

b1 = Sign(pt, v1, v2) < 0.0f;

b2 = Sign(pt, v2, v3) < 0.0f;

b3 = Sign(pt, v3, v1) < 0.0f;

return ((b1 == b2) && (b2 == b3));

}

Thanks for the help.

###
#7
Members - Reputation: **2059**

Posted 21 January 2005 - 03:53 PM

Yeah, the perp-dot is the same as the z-term in a 3D cross product. In other words, if you took two 2D vectors and made them 3D by setting z = 0, then took their cross product in 3D, the resulting vector would have x = 0, y = 0, and z = the perp-dot of the two vectors in 2D. In fact, I've seen some Vector2 classes just call the perp-dot function Cross() (Dave Eberly calls it 'Kross').

Anyway, glad you got it sorted :-)

Anyway, glad you got it sorted :-)

###
#8
Crossbones+ - Reputation: **2029**

Posted 22 January 2005 - 03:56 AM

yes, sorry. the code I gave is just the standard edge plane per product thingy, extended to handle both CCW and CW tests.

The angle/area thing accumulates the error, since you add the results together. As you said, no need to accumulate, just make sure the point if facing every edges the same way, then it's in the triangle. If the point is outside, it cannot mathematically face every edges the same way [grin], no matter is the triangle is ordered CW or CCW.

That also works for convex polys (like quads, ect..).

The angle/area thing accumulates the error, since you add the results together. As you said, no need to accumulate, just make sure the point if facing every edges the same way, then it's in the triangle. If the point is outside, it cannot mathematically face every edges the same way [grin], no matter is the triangle is ordered CW or CCW.

That also works for convex polys (like quads, ect..).