heltonbiker
heltonbiker

Reputation: 27615

HitTest and RayMeshGeometry3DHitTestResult programmatically in a window-less console application

I need to perform the intersection of a line and a triangle mesh.

That operation would be very conveniently performed if I could use VistualTreeHelper.HitTest method, that would return a RayMeshGeometry3DHitTestResult structure.

The problem is: VisualTreeHelper.HitTest requires a Visual and a Point, while I have only a Visual3D whose Geometry property is a MeshGeometry3D and a Ray custom class made from a Point3D (its origin) and a Vector3D (its direction).

So what I would like is:

Point3D intersection = GetIntersection(MeshGeometry3D mesh, Point3D rayOrigin, Vector3D rayD

irection);

But the framework offers me:

HitTestResult result = VisualTreeHelper.HitTest(model, point);
if (result is RayMeshGeometry3DHitTestResult hitTestResult)]
{
    Point3D intersection = result.PointHit;
}

From what I've read, usually the desired Visual3D would be put inside a ViewPort3DVisual, and the Point would be somehow transformed into a ray by the viewport transform, or something like that.

Since I don't have any Window so that I could have a ViewPort3D to put inside it, etc., I am clueless as to how I could use these helpers to get what I need.

Alternatively, if there is a library that would do that, I could gladly use it instead of WPF's 3D methods.

Upvotes: 1

Views: 507

Answers (1)

Charlie Rix
Charlie Rix

Reputation: 61

The code to do this is a little more than will fit in a reply

https://github.com/charlierix/AsteroidMiner/tree/master/Src/HelperClassesWPF

Look in Math3D.cs

GetIntersection_Hull_Ray()
GetIntersection_Triangle_Ray()

Note that in GetIntersection_Plane_Line(), the third line is lineDir.Normalize(). This is a bug that I commented out, but haven't pushed up to github yet

The original source that this function was built off of is from:

Author: DigiBen [email protected]
Program: PolygonCollision
Date: 7/11/01

Upvotes: 2

Related Questions