/// <summary> /// Returns true if triangle created from three nodes intersects with any other triangles /// </summary> /// <param name="node1"></param> /// <param name="node2"></param> /// <param name="node3"></param> /// <returns></returns> public bool AssessFaceIntersection(NodeController node1, NodeController node2, NodeController node3) { Vector3 centroid = ( node1.transform.position + node2.transform.position + node3.transform.position) / 3f; // shift all points by an epsilon inwards, to make sure points aren't erronously said to // overlap due to floating point errors. Vector3 pos1 = Vector3.MoveTowards( node1.transform.position, centroid, .01f ); Vector3 pos2 = Vector3.MoveTowards( node2.transform.position, centroid, .01f ); Vector3 pos3 = Vector3.MoveTowards( node3.transform.position, centroid, .01f ); foreach (var triangle in currentTriangles) { bool intersects = TriTriOverlap.TriTriIntersect( pos1, pos2, pos3, triangle[0].transform.position, triangle[1].transform.position, triangle[2].transform.position ); if (intersects) { return(true); } } return(false); }
static bool TestIfSolvable(List <PositionedShape> shapes) { IEnumerable <Vector3[]> tris = shapes.SelectMany(x => x.ToPositionedTriangles()); // get pairs of triangles IEnumerable <IEnumerable <Vector3[]> > pairs = Extensions.GetPermutations(tris, 2); foreach (IEnumerable <Vector3[]> pair in pairs) { var pairList = pair.ToList(); bool overlaps = TriTriOverlap.TriTriIntersect( pairList[0][0], // triangle 0, point 0 pairList[0][1], // triangle 0, point 1 pairList[0][2], // etc pairList[1][0], pairList[1][1], pairList[1][2] ); if (debug) { float w = 3; Debug.DrawLine(pairList[0][0], pairList[0][1], Color.red, w); Debug.DrawLine(pairList[0][0], pairList[0][2], Color.red, w); Debug.DrawLine(pairList[0][1], pairList[0][2], Color.red, w); Debug.DrawLine(pairList[1][0], pairList[1][1], Color.blue, w); Debug.DrawLine(pairList[1][0], pairList[1][2], Color.blue, w); Debug.DrawLine(pairList[1][1], pairList[1][2], Color.blue, w); } if (overlaps) { return(false); } } IEnumerable <Vector3[]> intersectors = shapes.SelectMany(x => x.ToPositionedIntersectors()); // add some additional intersectors - lowers the number of solutions // now, at least one triangle must intersect with each intersector foreach (Vector3[] intersector in intersectors) { bool atLeastOneOverlap = false; foreach (Vector3[] tri in tris) { bool overlaps = TriTriOverlap.TriTriIntersect( tri[0], tri[1], tri[2], intersector[0], intersector[1], intersector[2] ); if (overlaps) { atLeastOneOverlap = true; break; } } // one of the intersectors didnt overlap, fail! if (!atLeastOneOverlap) { return(false); } } // all the tests passed, it must be solvable return(true); }