Crossover Detection Algorithm for rays and triangles in 3D space

Source: Internet
Author: User
Introduction

Ray has many important applications in 3D graphics. For example, the pick operation is implemented by Ray, and Ray can be used for bullet Ray collision detection. Therefore, in this blog, we will briefly introduce how to perform Ray-triangle cross detection.




Ray-triangle crossover Detection Algorithm

In the Mt97 thesis of Tomas Moeller, a new algorithm is proposed. This algorithm reduces the memory consumption required for Ray-triangle cross-detection. Previously, the ray-triangle cross detection was mainly used to calculate the intersection of the plane formed by the ray and the triangle, and then re-determine whether the intersection is on the triangle to determine whether there is a cross. This method is intuitive and conforms to the mathematical knowledge we have been learning. However, this detection method requires a lot of calculations, and the plane where the triangle is located needs to be calculated based on this, at the same time, we also need to open up space to save the calculated plane.

The beauty of mathematics lies in the ability to find other methods to replace this obvious method, so as to simplify the problem to a certain extent. This simplified process does not need to be implemented in the code. We only need to calculate the final conclusion on the draft paper according to the conditions in advance, we only need to directly use the final conclusion in our code.

In Tomas Moller's paper, it mentioned the following concept:

If a vertex is located on the triangle v0, V1, and V2, the vertex can be expressed as follows:

T (u, v) = (1-u-v) * V0 + u * V1 + V * V2;

Here u + v <= 1, u> = 0, V> = 0

For Ray, we generally use the following equation to represent it:

R (t) = O + T * D; (o is the starting point of the ray, and D is the direction of the ray)

Therefore, since they need to have intersection points, we can use the following method directly:

O + T * D = (1-u-v) * V0 + u * V1 + V * v2

Then, after a series of transformations, the results are finally obtained. Interested readers can read the papers of Tomas Moller on their own. The derivation process is explained in detail in this paper. I will not go into details here.




Ray-triangle crossover Detection Algorithm Implementation

The following is the Moeller Algorithm Implementation of the ray-triangle crossover detection algorithm. Basically, it is a copy of the code in the Tomas Moeller paper, as shown below:

<span style="font-family:Microsoft YaHei;">bool Ray::intersectWithTriangle(VECTOR3 v0,VECTOR3 v1, VECTOR3 v2,bool bCull, float *t){VECTOR3 edge1, edge2, tvec, pvec, qvec ;float det, inv_det ;float u,v ;//Find vectors for two edges sharing vert0Vec3Sub(edge1, v1, v0);Vec3Sub(edge2, v2, v0);//Begin calculating determinant - also used to calculate U parameterVec3Cross(pvec, dir, edge2);//If the determinant is near zero, ray lies in plane of triangleVec3Dot(det, edge1, pvec);//If bCull is trueif(bCull){if(det < 0.00001f)return false ;//Calculate distance from vert0 to ray originVec3Sub(tvec, origin, v0);//Calculate U parameter and test boundsVec3Dot(u, tvec, pvec);if(u < 0.0 || u > det)return false ;//Prepare to test v parameterVec3Cross(qvec, tvec, edge1);//Calculate V parameter and test boundsVec3Dot(v, dir, qvec);if(v < 0.0f || u + v > det)return false ;//Calculate t , scale paramter, ray intersect triangleVec3Dot(*t, edge2, qvec);inv_det = 1.0f / det ;*t *= inv_det ;u *= inv_det ;v *= inv_det ;}else{if(det > -0.00001f && det < 0.00001)return false ;inv_det = 1.0f / det ;//calculate distance from v0 to ray originVec3Sub(tvec, origin, v0);//Calculate u parameter  and test boundsVec3Dot(u, tvec, pvec);u *= inv_det ;if(u < 0.0 || u > 1.0)return false ;//prepare to test v parameterVec3Cross(qvec, tvec, edge1);//Calculate v parameter and test boundsVec3Dot(v, dir, qvec);v *= inv_det ;if(v < 0.0 || u + v > 1.0)return false ;//calculate t, ray intersect triangleVec3Dot(*t, edge2, qvec);*t *= inv_det ;}return true ;}// end for intersectWithTriangle</span>




Sample program

This figure is the case when there is no crossover,

After a crossover occurs:


Today's note is over. This article will appear in the future. Please pay attention to it !!!

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.