I took some game engines classes before and detecting a point in a 3D triangle is a common task, usually in the context of a ray point hitting a triangle.
The way I was taught was with calculating the barycentric coordinates of our point P, with respect to the 3 points of the triangle, P0,P1,P2.
Nian980
We represent it with weights, P=w0∗P0+w1∗P1+w2∗P2 , where w1,w2,w3 are scalars weights that add up to 1.
Nian980
We can then simplify it to P−P0=w1∗(P1−P0)+w2∗(P2−P0)
Nian980
Then take the dot product of both sides of the equation with (P1−P0) then (P2−P0)
Nian980
That result can be converted to matrix form, which I don't know how to type out, and we can solve for w1 and w2. Point lies inside the triangle if and only if all 3 weights are non-negative, i.e. w1+w2≤1
Nian980
Overall, I feel that the 3-line test here is much more intuitive, but it may be that the barycentric method is much faster, due to the matrix calculation. (Sorry, was struggling with server errors so had to split comments up?)
Staffrishiu
@Nian980 Thank you for sharing this! You are totally right this is another way we can think about the point in triangle test and we actually will use this exact method later in class when we learn about ray-triangle intersection. Your thinking about the speed is also super interesting - I was thinking about it and it seems to me in the 2D case this 3-line test might be faster since we only need some algebra and dot products compared to setting up the matrices/solving though I could definitely be wrong and it would be interesting to see some comparisons!
aramk-hub
Coming back to this slide, why do we multiply the difference of the x coordinates by dYi and the difference in y by dXi? What does this reverse multiplication do for us in terms of correctly rasterizing the image?
I took some game engines classes before and detecting a point in a 3D triangle is a common task, usually in the context of a ray point hitting a triangle. The way I was taught was with calculating the barycentric coordinates of our point P, with respect to the 3 points of the triangle, P0,P1,P2.
We represent it with weights, P=w0∗P0+w1∗P1+w2∗P2 , where w1,w2,w3 are scalars weights that add up to 1.
We can then simplify it to P−P0=w1∗(P1−P0)+w2∗(P2−P0)
Then take the dot product of both sides of the equation with (P1−P0) then (P2−P0)
That result can be converted to matrix form, which I don't know how to type out, and we can solve for w1 and w2. Point lies inside the triangle if and only if all 3 weights are non-negative, i.e. w1+w2≤1
Overall, I feel that the 3-line test here is much more intuitive, but it may be that the barycentric method is much faster, due to the matrix calculation. (Sorry, was struggling with server errors so had to split comments up?)
@Nian980 Thank you for sharing this! You are totally right this is another way we can think about the point in triangle test and we actually will use this exact method later in class when we learn about ray-triangle intersection. Your thinking about the speed is also super interesting - I was thinking about it and it seems to me in the 2D case this 3-line test might be faster since we only need some algebra and dot products compared to setting up the matrices/solving though I could definitely be wrong and it would be interesting to see some comparisons!
Coming back to this slide, why do we multiply the difference of the x coordinates by dYi and the difference in y by dXi? What does this reverse multiplication do for us in terms of correctly rasterizing the image?