public static bool LinesCross(Vector3 aStart, Vector3 aEnd, Vector3 bStart, Vector3 bEnd) { var slopeA = Slope.FromPoints(aStart, aEnd); var slopeB = Slope.FromPoints(bStart, bEnd); var intersection = slopeA.CalculateIntersection(slopeB); if (intersection.Type == IntersectionType.NONE) { return(false); } if (intersection.Type == IntersectionType.OVERLAP) { return(true); } var p = intersection.Point; var minAX = Mathf.Min(aStart.x, aEnd.x); var maxAX = Mathf.Max(aStart.x, aEnd.x); var minAZ = Mathf.Min(aStart.z, aEnd.z); var maxAZ = Mathf.Max(aStart.z, aEnd.z); if (p.x < minAX || p.x > maxAX || p.z < minAZ || p.z > maxAZ) { return(false); } var minBX = Mathf.Min(bStart.x, bEnd.x); var maxBX = Mathf.Max(bStart.x, bEnd.x); var minBZ = Mathf.Min(bStart.z, bEnd.z); var maxBZ = Mathf.Max(bStart.z, bEnd.z); return(!(p.x < minBX || p.x > maxBX || p.z < minBZ || p.z > maxBZ)); }
public void Check_that_slopes_do_not_overlap() { var verticalFromOrigo = Slope.FromPoints(Vector3.zero, Vector3.forward); var horizontalFromOrigo = Slope.FromPoints(Vector3.zero, Vector3.right); var verticalOffset = Slope.FromPoints(new Vector3(1, 0, 0), new Vector3(1, 0, 1)); var horizontalOffset = Slope.FromPoints(new Vector3(0, 0, 1), new Vector3(2, 0, 1)); var upRightFromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, 1)); var upRightOffsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 2), new Vector3(2, 0, 3)); var downRightFromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, -1)); var downRightOffsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 1), new Vector3(2, 0, 0)); var fromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, 3)); var offsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 1), new Vector3(2, 0, 4)); checkNotOverlap(verticalFromOrigo, horizontalFromOrigo); checkNotOverlap(verticalFromOrigo, verticalOffset); checkNotOverlap(verticalFromOrigo, horizontalOffset); checkNotOverlap(verticalFromOrigo, upRightFromOrigo); checkNotOverlap(verticalFromOrigo, upRightOffsetFromOrigo); checkNotOverlap(verticalFromOrigo, upRightFromOrigo); checkNotOverlap(verticalFromOrigo, upRightOffsetFromOrigo); checkNotOverlap(verticalFromOrigo, downRightFromOrigo); checkNotOverlap(verticalFromOrigo, downRightOffsetFromOrigo); checkNotOverlap(verticalFromOrigo, fromOrigo); checkNotOverlap(verticalFromOrigo, offsetFromOrigo); }
public void Check_that_slopes_overlap() { var verticalFromOrigo = Slope.FromPoints(Vector3.zero, Vector3.forward); var horizontalFromOrigo = Slope.FromPoints(Vector3.zero, Vector3.right); var verticalOffset = Slope.FromPoints(new Vector3(1, 0, 0), new Vector3(1, 0, 1)); var horizontalOffset = Slope.FromPoints(new Vector3(0, 0, 1), new Vector3(2, 0, 1)); var upRightFromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, 1)); var upRightOffsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 2), new Vector3(2, 0, 3)); var downRightFromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, -1)); var downRightOffsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 1), new Vector3(2, 0, 0)); var fromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, 3)); var offsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 1), new Vector3(2, 0, 4)); checkOverlapsSelf(verticalFromOrigo); checkOverlapsSelf(horizontalFromOrigo); checkOverlapsSelf(verticalOffset); checkOverlapsSelf(horizontalOffset); checkOverlapsSelf(upRightFromOrigo); checkOverlapsSelf(upRightOffsetFromOrigo); checkOverlapsSelf(downRightFromOrigo); checkOverlapsSelf(downRightOffsetFromOrigo); checkOverlapsSelf(fromOrigo); checkOverlapsSelf(offsetFromOrigo); }
public void Check_that_the_slopes_intersect() { var verticalFromOrigo = Slope.FromPoints(Vector3.zero, Vector3.forward); var horizontalFromOrigo = Slope.FromPoints(Vector3.zero, Vector3.right); var verticalOffset = Slope.FromPoints(new Vector3(1, 0, 0), new Vector3(1, 0, 1)); var horizontalOffset = Slope.FromPoints(new Vector3(0, 0, 1), new Vector3(2, 0, 1)); var upRightFromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, 1)); var upRightOffsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 2), new Vector3(2, 0, 3)); var downRightFromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, -1)); var downRightOffsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 1), new Vector3(2, 0, 0)); var fromOrigo = Slope.FromPoints(Vector3.zero, new Vector3(1, 0, 3)); var offsetFromOrigo = Slope.FromPoints(new Vector3(1, 0, 1), new Vector3(2, 0, 4)); checkIntersect(verticalFromOrigo, horizontalFromOrigo, Vector3.zero); checkIntersect(verticalFromOrigo, horizontalOffset, new Vector3(0, 0, 1)); checkIntersect(verticalFromOrigo, upRightFromOrigo, Vector3.zero); checkIntersect(verticalFromOrigo, upRightOffsetFromOrigo, new Vector3(0, 0, 1)); checkIntersect(verticalFromOrigo, downRightFromOrigo, Vector3.zero); checkIntersect(verticalFromOrigo, downRightOffsetFromOrigo, new Vector3(0, 0, 2)); checkIntersect(verticalFromOrigo, fromOrigo, Vector3.zero); checkIntersect(verticalFromOrigo, offsetFromOrigo, new Vector3(0, 0, -2)); checkIntersect(horizontalFromOrigo, verticalFromOrigo, Vector3.zero); checkIntersect(horizontalFromOrigo, upRightFromOrigo, Vector3.zero); checkIntersect(horizontalFromOrigo, upRightOffsetFromOrigo, new Vector3(-1, 0, 0)); checkIntersect(horizontalFromOrigo, downRightFromOrigo, Vector3.zero); checkIntersect(horizontalFromOrigo, fromOrigo, Vector3.zero); checkIntersect(verticalOffset, upRightFromOrigo, new Vector3(1, 0, 1)); checkIntersect(verticalOffset, downRightFromOrigo, new Vector3(1, 0, -1)); checkIntersect(verticalOffset, fromOrigo, new Vector3(1, 0, 3)); checkIntersect(upRightFromOrigo, downRightFromOrigo, Vector3.zero); checkIntersect(upRightFromOrigo, offsetFromOrigo, new Vector3(1, 0, 1)); }
public static bool IsPointInsideMesh(Vector3 point, JMesh meshTransformed) { // Assumes that the point is withing the AABB var edgesCrossed = 0; var vertices = meshTransformed.EdgeVertices; var end = vertices.Length - 1; var horizontalPointSlope = Slope.FromPoints(point, new Vector3(point.x - 1, 0, point.z)); var endOfRayCast = new Vector3(meshTransformed.AABB.min.x - 1f, 0, point.z); for (var i = 0; i < end; i++) { var pointA = vertices[i]; var pointB = vertices[i + 1]; /** * Quick check to see if the point is exactly on a vertex. * If it is, the point counts as being inside the mesh. **/ if (pointA.x == point.x && pointA.z == point.z) { //Debug.Log("Point is exactly on an existing point, should return true"); return(true); } var edgeSlope = Slope.FromPoints(pointA, pointB); if (edgeSlope.IsPointOnLineInXZPlane(point, POINT_MAX_DIFF)) { //Debug.Log("Point is on line in XZ plane, edge: " + (pointB - pointA)); return(true); } var intersection = edgeSlope.CalculateIntersection(horizontalPointSlope); if (LinesCross(point, endOfRayCast, pointA, pointB)) { //Debug.Log("Crossed edge! " + (pointB - pointA)); edgesCrossed++; } //if ((pointA.z <= point.z && point.z <= pointB.z) || (pointB.z <= point.z && point.z <= pointA.z)) //{ // // Can cross vertically // if (pointA.x <= point.x || pointB.x <= point.x) // { // /** The edge starts or ends to the left of our point, // * meaning we cross the edge with our horizontal leftwards raycast // */ // /** // * Check if point is on an edge. This must be done before the next type of check // * that checks for a single point being added twice to edgesCrossed. // **/ // var slope = Slope.FromPoints(pointA, pointB); // if (slope.IsPointOnLineInXZPlane(point, POINT_MAX_DIFF)) // { // return true; // } // /** // * Need to perform an additional check so that we don't include points crossed twice. // * If our point is on the exact same "axis" (x or z position) as our end-point, don't // * count it again since its been crossed when it was on pointA. // * We only count point-crossing for pointA to avoid double-counting. // */ // if (pointB.x == point.x && pointB.z == point.z) // { // continue; // } // var xIntersection = slope.CalculateIntersectionWithHorizontalLine(point.z); // var minX = Mathf.Min(pointA.x, pointB.x); // if (minX <= xIntersection && xIntersection <= point.x) // { // edgesCrossed++; // } // } //} } return(edgesCrossed % 2 != 0); }