///<summary> /// Tests a ray against the triangle mesh. ///</summary> ///<param name="ray">Ray to test against the mesh.</param> ///<param name="hitCount">Number of hits between the ray and the mesh.</param> ///<returns>Whether or not the ray hit the mesh.</returns> public bool RayCast(Ray ray, out int hitCount) { var rayHits = CommonResources.GetRayHitList(); bool toReturn = RayCast(ray, rayHits); hitCount = rayHits.Count; CommonResources.GiveBack(rayHits); return(toReturn); }
///<summary> /// Tests a ray against the triangle mesh. ///</summary> ///<param name="ray">Ray to test against the mesh.</param> /// <param name="maximumLength">Maximum length of the ray in units of the ray direction's length.</param> /// <param name="sidedness">Sidedness to apply to the mesh for the ray cast.</param> ///<param name="rayHit">Hit data for the ray, if any.</param> ///<returns>Whether or not the ray hit the mesh.</returns> public bool RayCast(Ray ray, float maximumLength, TriangleSidedness sidedness, out RayHit rayHit) { var rayHits = CommonResources.GetRayHitList(); bool toReturn = RayCast(ray, maximumLength, sidedness, rayHits); if (toReturn) { rayHit = rayHits[0]; for (int i = 1; i < rayHits.Count; i++) { RayHit hit = rayHits[i]; if (hit.T < rayHit.T) { rayHit = hit; } } } else { rayHit = new RayHit(); } CommonResources.GiveBack(rayHits); return(toReturn); }
TriangleSidedness ComputeSolidSidednessHelper(Ray ray) { TriangleSidedness toReturn; var hitList = CommonResources.GetIntList(); if (triangleMesh.Tree.GetOverlaps(ray, hitList)) { Vector3 vA, vB, vC; var hits = CommonResources.GetRayHitList(); //Identify the first and last hits. int minimum = 0; int maximum = 0; Fix64 minimumT = Fix64.MaxValue; Fix64 maximumT = -1; for (int i = 0; i < hitList.Count; i++) { triangleMesh.Data.GetTriangle(hitList[i], out vA, out vB, out vC); RayHit hit; if (Toolbox.FindRayTriangleIntersection(ref ray, Fix64.MaxValue, TriangleSidedness.DoubleSided, ref vA, ref vB, ref vC, out hit) && IsHitUnique(hits, ref hit)) { if (hit.T < minimumT) { minimumT = hit.T; minimum = hitList[i]; } if (hit.T > maximumT) { maximumT = hit.T; maximum = hitList[i]; } } } if (hits.Count % 2 == 0) { //Since we were outside, the first hit triangle should be calibrated //such that it faces towards us. triangleMesh.Data.GetTriangle(minimum, out vA, out vB, out vC); var normal = Vector3.Cross(vA - vB, vA - vC); if (Vector3.Dot(normal, ray.Direction) < F64.C0) { toReturn = TriangleSidedness.Clockwise; } else { toReturn = TriangleSidedness.Counterclockwise; } } else { //Since we were inside, the last hit triangle should be calibrated //such that it faces away from us. triangleMesh.Data.GetTriangle(maximum, out vA, out vB, out vC); var normal = Vector3.Cross(vA - vB, vA - vC); if (Vector3.Dot(normal, ray.Direction) < F64.C0) { toReturn = TriangleSidedness.Counterclockwise; } else { toReturn = TriangleSidedness.Clockwise; } } CommonResources.GiveBack(hits); } else { toReturn = TriangleSidedness.DoubleSided; //This is a problem... } CommonResources.GiveBack(hitList); return(toReturn); }