public bool IntersectsMesh(TransformMatrix thisTransform, Octave3DMesh otherMesh, TransformMatrix otherTransform) { OrientedBox thisOOBB = new OrientedBox(ModelAABB); thisOOBB.Transform(thisTransform); OrientedBox otherOOBB = new OrientedBox(otherMesh.ModelAABB); otherOOBB.Transform(otherTransform); if (!thisOOBB.Intersects(otherOOBB)) { return(false); } List <Triangle3D> thisTriangles = GetOverlappedTriangles(otherOOBB, thisTransform); for (int thisTriIndex = 0; thisTriIndex < thisTriangles.Count; ++thisTriIndex) { Triangle3D thisTri = thisTriangles[thisTriIndex]; OrientedBox thisTriOOBB = new OrientedBox(thisTri.GetEncapsulatingBox()); List <Triangle3D> otherTriangles = otherMesh.GetOverlappedTriangles(thisTriOOBB, otherTransform); for (int otherTriIndex = 0; otherTriIndex < otherTriangles.Count; ++otherTriIndex) { Triangle3D otherTri = otherTriangles[otherTriIndex]; if (thisTri.IntersectsTriangle(otherTri)) { return(true); } } } return(false); }
public OrientedBox GetOrientedBox(TransformMatrix transformMatrix) { if (_mesh == null) { return(OrientedBox.GetInvalid()); } OrientedBox orientedBox = new OrientedBox(GetBox()); orientedBox.Transform(transformMatrix); return(orientedBox); }
public OrientedBox GetOOBB(TransformMatrix transformMatrix) { if (_mesh == null) { return(OrientedBox.GetInvalid()); } OrientedBox oobb = new OrientedBox(ModelAABB); oobb.Transform(transformMatrix); return(oobb); }
public List <Triangle3D> GetOverlappedWorldTriangles(OrientedBox box, TransformMatrix meshTransformMatrix) { // If the tree was not yet build, we need to build it because we need // the triangle information in order to perform the raycast. if (!_wasBuilt) { Build(); } // Work in mesh model space because the tree data exists in model space OrientedBox meshSpaceBox = new OrientedBox(box); Matrix4x4 inverseTransform = meshTransformMatrix.ToMatrix4x4x.inverse; meshSpaceBox.Transform(inverseTransform); List <SphereTreeNode <MeshSphereTreeTriangle> > overlappedNodes = _sphereTree.OverlapBox(meshSpaceBox); if (overlappedNodes.Count == 0) { return(new List <Triangle3D>()); } Box queryBox = Box.FromPoints(meshSpaceBox.GetCenterAndCornerPoints()); var overlappedWorldTriangles = new List <Triangle3D>(50); foreach (var node in overlappedNodes) { int triangleIndex = node.Data.TriangleIndex; MeshTriangleInfo triangleInfo = _octave3DMesh.GetMeshTriangleInfo(triangleIndex); if (triangleInfo.ModelSpaceTriangle.IntersectsBox(queryBox)) { triangleInfo.ModelSpaceTriangle.TransformPoints(meshTransformMatrix); overlappedWorldTriangles.Add(triangleInfo.ModelSpaceTriangle); } } return(overlappedWorldTriangles); }
public List <Triangle3DIntersectInfo> GetIntersectingTriangles(TransformMatrix thisTransform, Octave3DMesh otherMesh, TransformMatrix otherTransform) { OrientedBox thisOOBB = new OrientedBox(ModelAABB); thisOOBB.Transform(thisTransform); OrientedBox otherOOBB = new OrientedBox(otherMesh.ModelAABB); otherOOBB.Transform(otherTransform); if (thisOOBB.Intersects(otherOOBB)) { List <Triangle3D> thisTriangles = GetOverlappedTriangles(otherOOBB, thisTransform); var output = new List <Triangle3DIntersectInfo>(50); Triangle3DIntersectInfo intersectInfo; for (int thisTriIndex = 0; thisTriIndex < thisTriangles.Count; ++thisTriIndex) { Triangle3D thisTri = thisTriangles[thisTriIndex]; OrientedBox thisTriOOBB = new OrientedBox(thisTri.GetEncapsulatingBox()); List <Triangle3D> otherTriangles = otherMesh.GetOverlappedTriangles(thisTriOOBB, otherTransform); for (int otherTriIndex = 0; otherTriIndex < otherTriangles.Count; ++otherTriIndex) { Triangle3D otherTri = otherTriangles[otherTriIndex]; if (thisTri.IntersectsTriangle(otherTri, out intersectInfo)) { output.Add(intersectInfo); } } } return(output); } else { return(new List <Triangle3DIntersectInfo>()); } }
/// <summary> /// Returns the world space vertices overlapped by the specified box. /// </summary> public List <Vector3> GetOverlappedWorldVerts(OrientedBox box, TransformMatrix meshTransformMatrix) { // If the tree was not yet build, we need to build it because we need // the triangle information in order to perform the raycast. if (!_wasBuilt) { Build(); } // Work in mesh model space because the tree data exists in model space OrientedBox meshSpaceOOBB = new OrientedBox(box); Matrix4x4 inverseTransform = meshTransformMatrix.ToMatrix4x4x.inverse; meshSpaceOOBB.Transform(inverseTransform); // Used to avoid duplicate indices since there can be triangles which share the same vertices // and we don't want to return the same vertex multiple times. HashSet <int> usedIndices = new HashSet <int>(); // Retrieve the nodes overlapped by the specified box List <SphereTreeNode <MeshSphereTreeTriangle> > overlappedNodes = _sphereTree.OverlapBox(meshSpaceOOBB); if (overlappedNodes.Count == 0) { return(new List <Vector3>()); } // Loop through all nodes var overlappedWorldVerts = new List <Vector3>(50); foreach (var node in overlappedNodes) { // Get the triangle associated with the node int triangleIndex = node.Data.TriangleIndex; MeshTriangleInfo triangleInfo = _octave3DMesh.GetMeshTriangleInfo(triangleIndex); List <Vector3> trianglePoints = triangleInfo.ModelSpaceTriangle.GetPoints(); for (int ptIndex = 0; ptIndex < trianglePoints.Count; ++ptIndex) { if (usedIndices.Contains(triangleInfo.VertIndices[ptIndex])) { continue; } Vector3 point = trianglePoints[ptIndex]; if (meshSpaceOOBB.ContainsPoint(point)) { overlappedWorldVerts.Add(meshTransformMatrix.MultiplyPoint(point)); usedIndices.Add(triangleInfo.VertIndices[ptIndex]); } } /*Triangle3D modelSpaceTriangle = _octave3DMesh.GetTriangle(triangleIndex); * * // Now check which of the triangle points resides inside the box * List<Vector3> trianglePoints = modelSpaceTriangle.GetPoints(); * foreach(var pt in trianglePoints) * { * // When a point resides inside the box, we will transform it in world space and add it to the final point list * if (box.ContainsPoint(pt)) overlappedWorldVerts.Add(meshTransformMatrix.MultiplyPoint(pt)); * }*/ } return(overlappedWorldVerts); }