Several methods to judge the planar point within the triangle

Source: Internet
Author: User
Tags square root

Recently in a unity implementation of the 3D modeling software, which needs to operate on the model surface, need to use point and triangle position relationship of the decision algorithm. Since a model is often thousands of triangular slices, the decision algorithm must be efficient, otherwise it will affect the overall performance of the final program. Here to record some algorithms, such as the error please point out, thank you!

First the false setpoint and the triangle in the same plane, if not the same plane, need to use other methods first filter.

There are several common methods for determining the location relationship of planar points-triangles (the following algorithm must ensure that the points and triangles are in the same plane):

1. Clockwise/Counterclockwise Determination method

The method requires that the order of the points is clockwise or counterclockwise, and if the point is clockwise, go along 3 edges, if the target point P is within the triangle, then P is always on the right side of the edge.

Similarly, if it is counterclockwise, the target point P should always be on the left side of the edge.

Example: Counter-clockwise three point ABC, Judge Abxap, BCXBP, CAXCP, if the z-values of the three vector cross product are all the same, and both are negative (left-hand system), indicating that p points in the triangle interior.

Vector cross product is relatively simple, here no longer repeat.

2. Area method

The three-point coordinates of the known triangles, which can be directly obtained by Helen's formula, only need to compare the size relationship between SPAB+SPBC+SPCA and SABC. However, because the area formula involves opening square root, it will produce the precision problem of floating-point number.

3. Angle method

Similar to the area method, if the point in the triangle, then pa,pb,pc and Ab,bc,ca, forming 6 angles, the 6 angle of the and should be 180 degrees. Also because the angle of view involves trigonometric operation, the efficiency is slow, and has the precision problem of floating point number, so this method is generally not adopted.

4. Oblique coordinate system method

This method is I mainly want to introduce the method, ideas from the Internet, I tidied up a bit, the sense of efficiency should be the fastest of these methods.

First of all:

Firstly, the oblique coordinate system is established by using AC, AB as I and j base vectors. The first thing to know is that the oblique coordinate system has two properties:

1) The conclusions in the plane vectors are established in the oblique coordinate system.

2) The linear equation is the same as the Cartesian coordinate system.

2) can be deduced in the oblique coordinate system with the geometrical properties of the straight line, and the concrete method is similar to the deduced linear equation in the Cartesian coordinate system, except that the angle trigonometric function of the oblique coordinate system is more.

The following AB, AP, AC, I, j are vectors, and i==ac,j==ab, is the base vector of the oblique coordinate system.

and the linear equation of BC is i+j=1;

Set P coordinate for (p_i,p_j), to P point within the triangle, must satisfy 1) p_i>=0, 2) p_j>=0, 3) p_i+p_j<=1.

This can be expressed in vectors as: ap=p_i*ac+p_j*ab

The right and left side of the same ride AC, AB. Two equations can be obtained.

AP ac=p_i* (AC ac) +p_j (ab AC)-----1

AP ab=p_i* (ac ab) +p_j* (ab AB)-----2

By these two equations can be obtained:

P_i= ((AP AC) * (AB AB)-(AP Ab) * (AC AB))/((AC AC) * (AB AB)-(AC AB) * (AC ab));

P_j= ((AP Ab) * (AC AC)-(AP AC) * (AB AC))/((AC AC) * (AB AB)-(AC AB) * (AC ab));

--------------------------------------------------

It is now possible to determine whether the point is within the triangle, the following can be aided by Cauchy inequality, to avoid floating-point division, instead of subtraction.

The vector form of Cauchy inequality can be obtained by:

(AC AC) * (ab AB) >= (ac ab) * (AC ab) Heng set up, so the denominator will not be negative, we only need to judge the positive and negative molecules.

That is, judging p_i>=0, p_j>=0, equivalent to:

(AP AC) * (AB AB)-(AP Ab) * (ac ab) >=0, (AP Ab) * (AC AC)-(AP AC) * (ab AC) >=0,

And p_i+p_j<=1, can be multiplied by the denominator on both sides, avoids the division operation of floating-point number, namely the following formula:

((AP AC) * (AB AB)-(AP Ab) * (AC AB)) + ((AP Ab) * (AC AC)-(AP AC) * (AB AC))-((AC AC) * (AB AB)-(AC AB) * (ac ab)) <=0

The code is as follows:

1     Static  Public BOOLpointintriangle (Vector3 P,vector3 A,vector3 B,vector3 c)2     {3         if(Pointintriangleplane (p,a,b,c) = =false)4             return false;5Vector3 ac=c-A;6Vector3 ab=b-A;7Vector3 ap=p-A;8 9         floatF_i=vector3.dot (AP,AC) *vector3.dot (ab,ab)-vector3.dot (AP,AB) *Vector3.dot (ac,ab);Ten         floatF_j=vector3.dot (Ap,ab) *vector3.dot (AC,AC)-vector3.dot (AP,AC) *Vector3.dot (AB,AC); One         floatF_d=vector3.dot (AC,AC) *vector3.dot (ab,ab)-vector3.dot (AC,AB) *Vector3.dot (ac,ab); A         if(f_d<0) Debug.Log ("Erro f_d<0"); -         //p_i==f_i/f_d -         //p_j==f_j/f_d the         if(f_i>=0&& f_j>=0&& f_i+f_j-f_d<=0) -             return true; -         Else -             return false; +}

Several methods to judge the planar point within the triangle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.