Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
petersesztak opened this issue Jul 20, 2021 · 3 comments

Comments

@petersesztak
Copy link

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
Copy link
Author

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
Copy link
Contributor

rms80 commented Jul 20, 2021

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants