public bool intersectPt(Vector3 point, Vector3 direction, ref Vector3 r) { pxyz[0] = -1; int length = plotAll(point.x, point.y, point.z, point.x + direction.x, point.y + direction.y, point.z + direction.z, pxyz); if (pxyz[0] == -1) { return(false); } for (int i = 0; i < length; i += 3) { int hash = pxyz[i] + rows * (pxyz[i + 1] + depth * pxyz[i + 2]); //List<GridDataPointer> list = grid[pxyz[i],pxyz[i + 1],pxyz[i + 2]]; if (grid[hash].dataPt.Count > 0) { Vector3 nearestPt = Vector3.zero; float nearestSqDist = float.PositiveInfinity; bool ret = false; for (int j = 0; j < grid[hash].dataPt.Count; j++) { TriangleData dp = grid[hash].dataPt[j]; Vector3[] tPoints = dp.getTransformedPoints(); Vector3 nPt = point + direction; if (TriangleTests.IntersectLineTriangle(ref point, ref nPt, ref tPoints[0], ref tPoints[1], ref tPoints[2], ref r)) { ret = true; float npSqDist = (point - r).sqrMagnitude; if (npSqDist < nearestSqDist) { nearestSqDist = npSqDist; nearestPt = r; } } } if (ret) { r = nearestPt; return(true); } } } return(false); }
public Vector3 closestPt(Vector3 point, Vector3 direction) { pxyz[0] = -1; //int[] data = plot(point.x, point.y, point.z, point.x + direction.x, point.y + direction.y, point.z + direction.z, pxyz); int length = plotAll(point.x, point.y, point.z, point.x + direction.x, point.y + direction.y, point.z + direction.z, pxyz); if (pxyz[0] == -1) { return(Vector3.zero); } Vector3 nearestPt = Vector3.zero; float nearestSqDist = float.PositiveInfinity; for (int i = 0; i < length; i += 3) { //Debug.Log("x: " + pxyz[i] + " y: " + pxyz[i + 1] + " z: " + pxyz[i + 2]); int hash = pxyz[i] + rows * (pxyz[i + 1] + depth * pxyz[i + 2]); if (grid[hash].dataPt.Count > 0) { for (int j = 0; j < grid[hash].dataPt.Count; j++) { TriangleData dp = grid[hash].dataPt[j]; Vector3[] tPoints = dp.getTransformedPoints(); Vector3 np = new Vector3(); TriangleTests.ClosestPointTriangle(ref tPoints[0], ref tPoints[1], ref tPoints[2], ref point, ref np); float npSqDist = (point - np).sqrMagnitude; if (npSqDist < nearestSqDist) { nearestSqDist = npSqDist; nearestPt = np; } } } } return(nearestPt); }