gradientspace / geometry3Sharp

C# library for 2D/3D geometric computation, mesh algorithms, and so on. Boost license.
http://www.gradientspace.com
Boost Software License 1.0
1.71k stars 387 forks source link

How to find Ray3 and Triangle3 intersection point coordinates (e.g. Xhit, Yhit, Zhit)? #163

Open petersesztak opened 3 years ago

petersesztak commented 3 years ago

First of all: thanks for sharing your powerfull library !

I have a very easy tasks -but can't figure out how to do that with geometry3Sharp? :

In 3D space: how to find Ray3 and Triangle3 intersection point coordinates (e.g. Xhit, Yhit, Zhit)? All what I can do to find out : bool IsIntersected and distance, but I need intersection point coordinates as well:

This is where I'm actually:

g3.Vector3d origin = new g3.Vector3d(0, 0, 50); g3.Vector3d direction = new g3.Vector3d(t_point_X, t_point_Y, t_point_Z); g3.Ray3d ray = new g3.Ray3d(origin, direction);

                g3.Vector3d V0 = new g3.Vector3d(ClosestFACE.Vertices[0].Position[0], ClosestFACE.Vertices[0].Position[1], ClosestFACE.Vertices[0].Position[2]);
                g3.Vector3d V1 = new g3.Vector3d(ClosestFACE.Vertices[1].Position[0], ClosestFACE.Vertices[1].Position[1], ClosestFACE.Vertices[1].Position[2]);
                g3.Vector3d V2 = new g3.Vector3d(ClosestFACE.Vertices[2].Position[0], ClosestFACE.Vertices[2].Position[1], ClosestFACE.Vertices[2].Position[2]);

                double rayT = 0;

                bool IsIntersected = g3.IntrRay3Triangle3.Intersects(ref ray, ref V0, ref V1, ref V2, out rayT);

So, ho to find intersection point coordinates ?

Thanks for your kind help in advance, Best Regards, Peter

petersesztak commented 3 years ago

Another attempt:

g3.Vector3d origin = new g3.Vector3d(0, 0, 50); g3.Vector3d direction = new g3.Vector3d(t_point_a, t_point_b, t_point_L); g3.Ray3d ray = new g3.Ray3d(origin, direction);

g3.Triangle3d triangle3D = new g3.Triangle3d(V0, V1, V2); g3.IntrRay3Triangle3 intrRay3Triangle3 = new g3.IntrRay3Triangle3(ray, triangle3D); intrRay3Triangle3.Compute(); var intersectionResult = intrRay3Triangle3.Result; // Intersects : OK, as expected // How to get the intersection point coordinates ?

rms80 commented 3 years ago

You can use the TriangleBaryCoords member. This is the barycentric coordinates of the intersection point in the triangle. So the intersection point is BaryCoords.XV0 + BaryCoords.YV1 + BaryCoords.Z*V2, or use Triangle3d.PointAt() which takes the barycentric coordinates.

(more details: https://en.wikipedia.org/wiki/Barycentric_coordinate_system#Barycentric_coordinates_on_triangles )

petersesztak commented 3 years ago

Dear Ryan, Great thanks ! : works like a charm :) Br, Péter