Ooops
July 28, 2021, 06:26:01

Author Topic: [bb] Ray / Line Segment Intersection by sswift [ 1+ years ago ]  (Read 1160 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Ray / Line Segment  Intersection
Author : sswift
Posted : 1+ years ago

Description : The Ray_Intersect_Triangle function I have provided here provides functionality which even Blitz's own line intersect function does not.

It provides the ability to define a line segment instead of a ray just as you can with Blitz's line intersect function, but it also allows infinite rays, and it allows backface culling, so if you fire a ray from inside a mesh it will be as if the mesh does not exist.  This is very useful for light map calculations, which is why I wrote it.

The Ray_Intersect_Mesh function is an extension of the Ray_Intersect_Triangle function.  It behaves more like Blitz's line intersect function because it checks whole meshes rather than just a single triangle.  It examines all triangles in all surfaces until it hits one and then it exits early.

This early out functionality should make it faster than Blitz's own lineintersect function.  The drawback is that it does not tell you what the CLOSEST intersection was like Blitz's lineintersect function, but you could easily modify the Ray_Intersect_Mesh function to provide that capability.


Code :
Code: BlitzBasic
  1. ; -------------------------------------------------------------------------------------------------------------------
  2. ; This function returns TRUE if a ray intersects a triangle.
  3. ;       It also calculates the UV coordinates of said colision as part of the intersection test,
  4. ;       but does not return them.
  5. ;
  6. ; V0xyz, V1xyz, and V2xyz are the locations of the three vertices of the triangle.
  7. ;
  8. ;       These vertices should be wound in CLOCKWISE order.  By clockwise I mean that if you face the front side of the
  9. ;       trianngle, the vertcies go around the trangle from V0 to V1 to V2 in a clockwise direction.  This is the same
  10. ;       as Blitz, so just pass the vertcies for a triangle in the same order that Blitz does.
  11. ;
  12. ;       The UV's generated by the function are set up as follows:
  13. ;               V0 is the location of UV(0,0)
  14. ;               V1 is the location of UV(0,1)
  15. ;               V2 is the location of UV(1,0)
  16. ;
  17. ;       This is useful to know if you want to know the exact location in texture space of the collision.
  18. ;       You can easily modify the function to return the values of T#, U#, and V#.
  19. ;
  20. ; Pxyz is a the start of the line.
  21. ;       Triangles which are "behind" this point are ignored.
  22. ;       "Behind" is defined as the direction opposite that which Dxyz points in.
  23. ;
  24. ; Dxyz is a vector providing the slope of the line.
  25. ;       Dxyz does not have to be normalized.
  26. ;
  27. ; If Extend_To_Infinity is set to false, then the length of Dxyz is how far the ray extends.
  28. ;       So if you want an endpoint on your ray beyond which no triangles will be detected, subtract the position
  29. ;       of Pxyz from your endpoint's position, and pass that ato the function as Dxyz.  Ie: (Dx = P2x-P1x)
  30. ;
  31. ; If Cull_Backfaces is set to true, then if the specified ray passes through the triangle from it's back side, then
  32. ; it will not register that it hit that triangle.
  33. ; -------------------------------------------------------------------------------------------------------------------
  34. Function Ray_Intersect_Triangle(Px#, Py#, Pz#, Dx#, Dy#, Dz#, V0x#, V0y#, V0z#, V1x#, V1y#, V1z#, V2x#, V2y#, V2z#, Extend_To_Infinity=True, Cull_Backfaces=False)
  35.        
  36.         ; crossproduct(b,c) =
  37.         ; ax = (by * cz) - (cy * bz)
  38.         ; ay = (bz * cx) - (cz * bx)    
  39.         ; az = (bx * cy) - (cx * by)
  40.  
  41.         ; dotproduct(v,q) =
  42.         ; (vx * qx) + (vy * qy) + (vz * qz)    
  43.         ; DP =  1 = Vectors point in same direction.          (  0 degrees of seperation)
  44.         ; DP =  0 = Vectors are perpendicular to one another. ( 90 degrees of seperation)
  45.         ; DP = -1 = Vectors point in opposite directions.     (180 degrees of seperation)
  46.         ;
  47.         ; The dot product is also reffered to as "the determinant" or "the inner product"
  48.  
  49.         ; Calculate the vector that represents the first side of the triangle.
  50.         E1x# = V2x# - V0x#
  51.         E1y# = V2y# - V0y#
  52.         E1z# = V2z# - V0z#
  53.  
  54.         ; Calculate the vector that represents the second side of the triangle.
  55.         E2x# = V1x# - V0x#
  56.         E2y# = V1y# - V0y#
  57.         E2z# = V1z# - V0z#
  58.  
  59.         ; Calculate a vector which is perpendicular to the vector between point 0 and point 1,
  60.         ; and the direction vector for the ray.
  61.         ; Hxyz = Crossproduct(Dxyz, E2xyz)
  62.         Hx# = (Dy# * E2z#) - (E2y# * Dz#)
  63.         Hy# = (Dz# * E2x#) - (E2z# * Dx#)
  64.         Hz# = (Dx# * E2y#) - (E2x# * Dy#)
  65.  
  66.         ; Calculate the dot product of the above vector and the vector between point 0 and point 2.
  67.         A# = (E1x# * Hx#) + (E1y# * Hy#) + (E1z# * Hz#)
  68.  
  69.         ; If we should ignore triangles the ray passes through the back side of,
  70.         ; and the ray points in the same direction as the normal of the plane,
  71.         ; then the ray passed through the back side of the plane,  
  72.         ; and the ray does not intersect the plane the triangle lies in.
  73.         If (Cull_Backfaces = True) And (A# >= 0) Then Return False
  74.                
  75.         ; If the ray is almost parralel to the plane,
  76.         ; then the ray does not intersect the plane the triangle lies in.
  77.         If (A# > -0.00001) And (A# < 0.00001) Then Return False
  78.        
  79.         ; Inverse Determinant. (Dot Product)
  80.         ; (Scaling factor for UV's?)
  81.         F# = 1.0 / A#
  82.  
  83.         ; Calculate a vector between the starting point of our ray, and the first point of the triangle,
  84.         ; which is at UV(0,0)
  85.         Sx# = Px# - V0x#
  86.         Sy# = Py# - V0y#
  87.         Sz# = Pz# - V0z#
  88.        
  89.         ; Calculate the U coordinate of the intersection point.
  90.         ;
  91.         ;       Sxyz is the vector between the start of our ray and the first point of the triangle.
  92.         ;       Hxyz is the normal of our triangle.
  93.         ;      
  94.         ; U# = F# * (DotProduct(Sxyz, Hxyz))
  95.         U# = F# * ((Sx# * Hx#) + (Sy# * Hy#) + (Sz# * Hz#))
  96.        
  97.         ; Is the U coordinate outside the range of values inside the triangle?
  98.         If (U# < 0.0) Or (U# > 1.0)
  99.  
  100.                 ; The ray has intersected the plane outside the triangle.
  101.                 Return False
  102.        
  103.         EndIf
  104.  
  105.         ; Not sure what this is, but it's definitely NOT the intersection point.
  106.         ;
  107.         ;       Sxyz is the vector from the starting point of the ray to the first corner of the triangle.
  108.         ;       E1xyz is the vector which represents the first side of the triangle.
  109.         ;       The crossproduct of these two would be a vector which is perpendicular to both.
  110.         ;
  111.         ; Qxyz = CrossProduct(Sxyz, E1xyz)
  112.         Qx# = (Sy# * E1z#) - (E1y# * Sz#)
  113.         Qy# = (Sz# * E1x#) - (E1z# * Sx#)
  114.         Qz# = (Sx# * E1y#) - (E1x# * Sy#)
  115.        
  116.         ; Calculate the V coordinate of the intersection point.
  117.         ;      
  118.         ;       Dxyz is the vector which represents the direction the ray is pointing in.
  119.         ;       Qxyz is the intersection point I think?
  120.         ;
  121.         ; V# = F# * DotProduct(Dxyz, Qxyz)
  122.         V# = F# * ((Dx# * Qx#) + (Dy# * Qy#) + (Dz# * Qz#))
  123.        
  124.         ; Is the V coordinate outside the range of values inside the triangle? 
  125.         ; Does U+V exceed 1.0?  
  126.         If (V# < 0.0) Or ((U# + V#) > 1.0)
  127.  
  128.                 ; The ray has intersected the plane outside the triangle.              
  129.                 Return False
  130.  
  131.                 ; The reason we check U+V is because if you imagine the triangle as half a square, U=1 V=1 would
  132.                 ; be in the lower left hand corner which would be in the lower left triangle making up the square.
  133.                 ; We are looking for the upper right triangle, and if you think about it, U+V will always be less
  134.                 ; than or equal to 1.0 if the point is in the upper right of the triangle.
  135.  
  136.         EndIf
  137.  
  138.         ; Calculate the distance of the intersection point from the starting point of the ray, Pxyz.
  139.         ; This distance is scaled so that at Pxyz, the start of the ray, T=0, and at Dxyz, the end of the ray, T=1.
  140.         ; If the intersection point is behind Pxyz, then T will be negative, and if the intersection point is
  141.         ; beyond Dxyz then T will be greater than 1.
  142.         T# = F# * ((E2x# * Qx#) + (E2y# * Qy#) + (E2z# * Qz#))
  143.  
  144.         ; If the triangle is behind Pxyz, ignore this intersection.
  145.         ; We want a directional ray, which only intersects triangles in the direction it points.
  146.         If (T# < 0) Then Return False
  147.  
  148.         ; If the plane is beyond Dxyz, amd we do not want the ray to extend to infinity, then ignore this intersection.
  149.         If (Extend_To_Infinity = False) And (T# > 1) Return False
  150.  
  151.         ; The ray intersects the triangle!             
  152.         Return True
  153.  
  154. End Function
  155.  
  156.  
  157. ; -------------------------------------------------------------------------------------------------------------------
  158. ; This function returns true if a ray intersects a mesh.
  159. ;
  160. ; This function differs from LinePick in that the specified mesh does not need to have a pickmode set,
  161. ; and this function can optionally ignore backfacing polygons in the mesh.  So if you do a pick from
  162. ; inside a mesh, it will not register a hit.
  163. ; -------------------------------------------------------------------------------------------------------------------
  164. Function Ray_Intersect_Mesh(Mesh, Px#, Py#, Pz#, Dx#, Dy#, Dz#, Extend_To_Infinity=True, Cull_Backfaces=False)
  165.  
  166.         Surfaces = CountSurfaces(Mesh)
  167.  
  168.         ; Make sure there's a surface, because the mesh might be empty.
  169.         If Surfaces > 0
  170.  
  171.                 For SurfaceLoop = 1 To Surfaces
  172.  
  173.                         Surface = GetSurface(Mesh, SurfaceLoop)
  174.        
  175.                         ; Examine all triangles in this surface.       
  176.                         Tris  = CountTriangles(Surface)
  177.                         For TriLoop = 0 To Tris-1
  178.        
  179.                                 V0 = TriangleVertex(Surface, TriLoop, 0)
  180.                                 V1 = TriangleVertex(Surface, TriLoop, 1)
  181.                                 V2 = TriangleVertex(Surface, TriLoop, 2)
  182.                
  183.                                 V0x# = VertexX#(Surface, V0)
  184.                                 V0y# = VertexY#(Surface, V0)
  185.                                 V0z# = VertexZ#(Surface, V0)
  186.  
  187.                                 V1x# = VertexX#(Surface, V1)
  188.                                 V1y# = VertexY#(Surface, V1)
  189.                                 V1z# = VertexZ#(Surface, V1)
  190.  
  191.                                 V2x# = VertexX#(Surface, V2)
  192.                                 V2y# = VertexY#(Surface, V2)
  193.                                 V2z# = VertexZ#(Surface, V2)
  194.  
  195.                                 TFormPoint V0x#, V0y#, V0z#, Mesh, 0
  196.                                 V0x# = TFormedX#()
  197.                                 V0y# = TFormedY#()
  198.                                 V0z# = TFormedZ#()
  199.  
  200.                                 TFormPoint V1x#, V1y#, V1z#, Mesh, 0
  201.                                 V1x# = TFormedX#()
  202.                                 V1y# = TFormedY#()
  203.                                 V1z# = TFormedZ#()
  204.                        
  205.                                 TFormPoint V2x#, V2y#, V2z#, Mesh, 0
  206.                                 V2x# = TFormedX#()
  207.                                 V2y# = TFormedY#()
  208.                                 V2z# = TFormedZ#()
  209.                                
  210.                                 Intersected = Ray_Intersect_Triangle(Px#, Py#, Pz#, Dx#, Dy#, Dz#, V0x#, V0y#, V0z#, V1x#, V1y#, V1z#, V2x#, V2y#, V2z#, Extend_To_Infinity, Cull_Backfaces)
  211.                                 If Intersected Then Return True
  212.                
  213.                         Next
  214.        
  215.                 Next
  216.                
  217.         EndIf
  218.        
  219.         Return False
  220.  
  221. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal