int[] FindClosestEdge(Vector3 p, SubsetDelegate include) { float distance_min = DISTANCE_EDGE_MIN; int closest = -1; for (int i = 0; i < triangles.Length; i++) { int t1 = triangles[i]; int t2 = triangles[LineNext(i)]; if (!include(t1) || !include(t2)) { continue; } float distance = DistanceToEdge(vertices[t1], vertices[t2], p); if (distance < distance_min) { closest = i; distance_min = distance * 0.99f; } } if (closest < 0) { return(null); } return(new int[] { triangles[closest], triangles[LineNext(closest)] }); }
int[] FindClosestTriangle(Vector3 p, SubsetDelegate include) { float distance_min = DISTANCE_TRIANGLE_MIN; int closest = -1; for (int i = 0; i < triangles.Length; i += 3) { int t0 = triangles[i + 0]; int t1 = triangles[i + 1]; int t2 = triangles[i + 2]; if (!include(t0) || !include(t1) || !include(t2)) { continue; } float distance = DistanceToTriangle(vertices[t0], vertices[t1], vertices[t2], p); if (distance < distance_min) { closest = i; distance_min = distance * 0.99f; } } if (closest < 0) { return(null); } return(new int[] { triangles[closest], triangles[closest + 1], triangles[closest + 2] }); }
int[] FindSelection(Controller controller, SubsetDelegate include = null) { if (include == null) { include = (index) => true; } Vector3 p = transform.InverseTransformPoint(controller.position); int[] result = FindClosestVertex(p, include); if (result == null) { result = FindClosestEdge(p, include); } if (result == null) { result = FindClosestTriangle(p, include); } return(result); }
int[] FindClosestVertex(Vector3 p, SubsetDelegate include, float distance_min = DISTANCE_VERTEX_MIN) { int closest = -1; for (int i = 0; i < vertices.Length; i++) { if (!include(i)) { continue; } float distance = DistanceToVertex(vertices[i], p); if (distance < distance_min) { closest = i; distance_min = distance * 0.99f; } } if (closest < 0) { return(null); } return(new int[] { closest }); }
/// <summary> /// Generates all subsets of T[] array. With each /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="handler"></param> /// <returns>Number of subsets</returns> public static int AllSubsets <T>(T[] array, SubsetDelegate <T> handler) { int i, len = array.Length; bool[] included = new bool[len]; int[] index = new int[len]; int[] changeIndex = new int[len]; /* * included.Initialize(); //set all false * index.Initialize(); //set all 0 * changeIndex.Initialize(); //set all 0 */ T[] subarray = new T[len]; //allocate the full amount int subsetLength = 0; int numSubsets = 1 << len; //2 ^ len int toChange; int ch; int ich; // TODO: Possible off by one error. 1 to numSubsets is only (2^n) - 1 subsets, not 2^n for (i = 1; i < numSubsets; ++i) { toChange = change(i); if (included[toChange]) //remove it { included[toChange] = false; --subsetLength; ch = changeIndex[subsetLength]; ich = index[toChange]; subarray[ich] = subarray[subsetLength]; changeIndex[ich] = ch; index[ch] = ich; } else //add it { //Index of added substroke is the last available spot index[toChange] = subsetLength; //Where it came from changeIndex[subsetLength] = toChange; //Set the last spot equal to the substroke subarray[subsetLength] = array[toChange]; //Increase size ++subsetLength; included[toChange] = true; } //Call the designated function handler(subarray, subsetLength); } return(numSubsets); }