PDA

View Full Version : Ray-triangle intersection: Example using new function enabled TYPEs



Petr Schreiber
20-04-2014, 21:15
Hi,

you can download sample code for ray-triangle intersection in the attachement.
It uses newly introduced function enabled types to produce easy to follow code.

Thanks to this, the main code can be very short:


Uses "Console"

#INCLUDE Once "tTriangle.tBasicU"
#INCLUDE Once "tVector3D.tBasicU"
#INCLUDE Once "tRay.tBasicU"

Function TBMain()

Dim triangle As tTriangle

triangle.Init(-1, -1, 0, ' -- A
1, -1, 0, ' -- B
0, 1, 0) ' -- C

Dim ray As tRay

ray.Init(0, 0, 1, ' -- Point
0, 0,-1) ' -- Direction

Dim collisionPoint As tVector3D

PrintL "Ray : " + ray.ToString()
PrintL "Triangle: " + triangle.ToString()
PrintL "-----"

If ray.IntersectsTriangle(triangle, collisionPoint) Then
PrintL "Intersection found at " + collisionPoint.ToString()
Else
PrintL "No intersection found"
End If

WaitKey

End Function



Petr

ReneMiner
20-04-2014, 21:22
Really very small and very structured. I like small functions. And I'm addicted to structured code :)

Edit: One makes me think...


Function tVectorUtils.MakeVector(pointA As tVector3D, pointB As tVector3D) As String

Dim v As tVector3D

v.x = pointA.x - pointB.x
v.y = pointA.y - pointB.y
v.z = pointA.z - pointB.z

Return v

End Function

(same to tVectorUtils.CrossProduct3D)

I think... if the function result is a string, and v is a tVector3D, and it returns v:
does tB automatically convert tVector3d to a string so this is equal to


Return Memory_Get(VarPtr(v), SizeOf(tVector3D) )

???

Petr Schreiber
21-04-2014, 09:42
Hi Rene,

BASICally yes. Thanks for bringing it up, I made a feature suggestion (http://www.thinbasic.com/community/project.php?issueid=466) :)


Petr