/// <summary> /// GetBoxTriangleIntersectionPoints /// Pushes intersection points onto the back of pts. Returns the /// number of points found. /// Points that are close together (compared to /// combinationDistance) get combined /// </summary> /// <param name="pts"></param> /// <param name="box"></param> /// <param name="triangle"></param> /// <param name="combinationDistance"></param> /// <returns></returns> private static int GetBoxTriangleIntersectionPoints(List<Vector3> pts, Box box, Triangle triangle, float combinationDistance) { // first intersect each edge of the box with the triangle Box.Edge[] edges; box.GetEdges(out edges); Vector3[] boxPts; box.GetCornerPoints(out boxPts); float tS; float tv1, tv2; int iEdge; for (iEdge = 0; iEdge < 12; ++iEdge) { Box.Edge edge = edges[iEdge]; Segment seg = new Segment(boxPts[(int)edge.Ind0], boxPts[(int)edge.Ind1] - boxPts[(int)edge.Ind0]); if (Intersection.SegmentTriangleIntersection(out tS, out tv1, out tv2, seg, triangle)) { AddPoint(pts, seg.GetPoint(tS), combinationDistance * combinationDistance); } } Vector3 pos, n; // now each edge of the triangle with the box for (iEdge = 0; iEdge < 3; ++iEdge) { Vector3 pt0 = triangle.GetPoint(iEdge); Vector3 pt1 = triangle.GetPoint((iEdge + 1) % 3); Segment s1 = new Segment(pt0, pt1 - pt0); Segment s2 = new Segment(pt1, pt0 - pt1); if (box.SegmentIntersect(out tS, out pos, out n, s1)) AddPoint(pts, pos, combinationDistance * combinationDistance); if (box.SegmentIntersect(out tS, out pos, out n, s2)) AddPoint(pts, pos, combinationDistance * combinationDistance); } return pts.Count; }
/// <summary> /// GetBoxTriangleIntersectionPoints /// Pushes intersection points onto the back of pts. Returns the /// number of points found. /// Points that are close together (compared to /// combinationDistance) get combined /// </summary> /// <param name="pts"></param> /// <param name="box"></param> /// <param name="triangle"></param> /// <param name="combinationDistance"></param> /// <returns>int</returns> private static int GetBoxTriangleIntersectionPoints(List<Vector3> pts, Box box, Triangle triangle, float combinationDistance) { // first intersect each edge of the box with the triangle Box.Edge[] edges; box.GetEdges(out edges); Vector3[] boxPts; box.GetCornerPoints(out boxPts); float tS; float tv1, tv2; // BEN-OPTIMISATION: Allocating just one Vector3 to be reused. Vector3 point = new Vector3(); int iEdge; for (iEdge = 0; iEdge < 12; ++iEdge) { Box.Edge edge = edges[iEdge]; Segment seg = new Segment(boxPts[(int)edge.Ind0], boxPts[(int)edge.Ind1] - boxPts[(int)edge.Ind0]); if (Intersection.SegmentTriangleIntersection(out tS, out tv1, out tv2, seg, triangle)) { // BEN-OPTIMISATION: Reusing the existing point variable instead allocating new ones. // This also allows point to be based by reference. seg.GetPoint(ref point, tS); AddPoint(pts, ref point, combinationDistance * combinationDistance); } } Vector3 pos, n; // now each edge of the triangle with the box for (iEdge = 0; iEdge < 3; ++iEdge) { #region "BEN-OPTIMISATION: Remove excess allocations and pass variables by reference." // ORIGINAL CODE: /*Vector3 pt0 = triangle.GetPoint(iEdge); Vector3 pt1 = triangle.GetPoint((iEdge + 1) % 3); Segment s1 = new Segment(pt0, pt1 - pt0); Segment s2 = new Segment(pt1, pt0 - pt1);*/ // OPTIMISED CODE: Vector3 pt0 = triangle.GetPoint(iEdge); Vector3 pt1 = triangle.GetPoint((iEdge + 1) % 3); Vector3 difference1; Vector3 difference2; Vector3.Subtract(ref pt1, ref pt0, out difference1); Vector3.Subtract(ref pt0, ref pt1, out difference2); Segment s1 = new Segment(ref pt0, ref difference1); Segment s2 = new Segment(ref pt1, ref difference2); #endregion if (box.SegmentIntersect(out tS, out pos, out n, s1)) AddPoint(pts, ref pos, combinationDistance * combinationDistance); if (box.SegmentIntersect(out tS, out pos, out n, s2)) AddPoint(pts, ref pos, combinationDistance * combinationDistance); } return pts.Count; }
/// <summary> /// Pushes intersection points (in world space) onto the back of pts. /// Intersection is between an AABox faces and an orientated box's /// edges. orient and pos are used to transform the points from the /// AABox frame back into the original frame. /// </summary> /// <param name="pts"></param> /// <param name="sides"></param> /// <param name="box"></param> /// <param name="origBoxOrient"></param> /// <param name="origBoxPos"></param> /// <param name="combinationDistanceSq"></param> /// <returns>int</returns> private static unsafe int GetAABox2BoxEdgesIntersectionPoints(List<ContactPoint> pts, ref Vector3 sides, Box box, ref Matrix origBoxOrient, ref Vector3 origBoxPos, float combinationDistanceSq) { int num = 0; Vector3[] boxPts; box.GetCornerPoints(out boxPts); Box.Edge[] edges; box.GetEdges(out edges); for (int iedge = 0; iedge < 12; ++iedge) { Vector3 edgePt0 = boxPts[(int)edges[iedge].Ind0]; Vector3 edgePt1 = boxPts[(int)edges[iedge].Ind1]; num += GetAABox2EdgeIntersectionPoints(pts, ref sides, box, ref edgePt0, ref edgePt1, ref origBoxOrient, ref origBoxPos, combinationDistanceSq); // Don't think we can get more than 8... and anyway if we get too many // then the penetration must be so bad who cares about the details? if (num >= 8) return num; } return num; }