/// <summary> /// This method gets the index of a point in a list, if not already there then it returns -1 /// This only checks for the vector part of the indexedpoint /// </summary> /// <param name="points">The list of indexedpoints</param> /// <param name="point">The point of which the index should be found</param> /// <returns>The index of the point, -1 if not there</returns> private static int indexOfPointInList(List <IndexedPoint> points, IndexedPoint point) { for (int i = 0; i < points.Count; i++) { if (points[i].p == point.p) { return(i); } } return(-1); }
IEnumerator MoveCoroutine() { // now, lets do the moving float distance = 0; // the current distance int index = 0; while (distance <= line.totalDistance) { // get the indexed point IndexedPoint iPoint = line.GetPoint(distance, index); // update the transform and index transform.position = iPoint.point; index = iPoint.index; // wait until the next frame yield return(null); // update the distance used. distance += GameController.enemySpeed * Time.deltaTime; } }
public static void Get2DConcaveTriangulation(List <Vector2> points, List <int> triangles) { _points.Clear(); for (int i = 0; i < points.Count; i++) { IndexedPoint point; point.position = points[i]; point.index = _points.Count; _points.Add(point); } int index0 = 1; int lastIndex0 = 0; while (_points.Count > 2) { if (index0 == lastIndex0) { triangles.Clear(); return; } int index1 = (index0 + 1) % _points.Count; int index2 = (index0 + 2) % _points.Count; IndexedPoint v0 = _points[index0]; IndexedPoint v1 = _points[index1]; IndexedPoint v2 = _points[index2]; int edgeA = (index2 + 1) % _points.Count; int edgeB = (index0 - 1 + _points.Count) % _points.Count; IndexedPoint edgePointA = _points[edgeA]; IndexedPoint edgePointB = _points[edgeB]; bool doesIntersect = false; if (cross(v2.position - v1.position, v1.position - v0.position) <= 0) { doesIntersect = true; } if (_points.Count > 3) { if (doesEdgeIntersect(v1.position, v2.position, v0.position, edgePointA.position)) { doesIntersect = true; } if (doesEdgeIntersect(v1.position, v0.position, v2.position, edgePointB.position)) { doesIntersect = true; } while (edgeA != edgeB) { int edgeA1 = (edgeA + 1) % _points.Count; IndexedPoint s0 = _points[edgeA]; IndexedPoint s1 = _points[edgeA1]; if (Do2DSegmentsIntersect(s0.position, s1.position, v0.position, v2.position)) { doesIntersect = true; break; } edgeA = edgeA1; } } if (doesIntersect) { index0 = (index0 + 1) % _points.Count; continue; } triangles.Add(v0.index); triangles.Add(v1.index); triangles.Add(v2.index); _points.RemoveAt(index1); index0 = index0 % _points.Count; lastIndex0 = (index0 - 1 + _points.Count) % _points.Count; } }
public static void optimiseOriginalTriangles() { //We do not work with triangles in plotting mode if (Global.Values.materialType == MaterialType.Pen) { return; } //To optimise the triangles we will firstly go through all the the triangles and check if all three of its points already exist and then check //if any of the existing references is to an existing triangle containg all three points //This list will contain the new triangles which are not duplicates and contain the indices of the triangles touching it List <Triangle> newTriangles = new List <Triangle>(); //This list will contain all the points that exist in the model in an indexed form where they have the indices of all the triangles that they //are used in List <IndexedPoint> newPoints = new List <IndexedPoint>(); //This dictionary contains a hashtable whith all the points to allow us to detect if a point already exists much faster, the points also contain //there index in the large list Dictionary <int, List <IndexedPoint> > pMap = new Dictionary <int, List <IndexedPoint> >(); //We will now go through each triangle and add it to the new list if not a duplicate for (int i = 0; i < Global.Values.initialTriangleList.Count(); i++) { var triangle = Global.Values.initialTriangleList[i]; //Calculate the hash values of the three points int hashA = hashForPoint(triangle.Point1); int hashB = hashForPoint(triangle.Point2); int hashC = hashForPoint(triangle.Point3); bool duplicate = false; IndexedPoint tA = new IndexedPoint(triangle.Point1, newTriangles.Count()); IndexedPoint tB = new IndexedPoint(triangle.Point2, newTriangles.Count()); IndexedPoint tC = new IndexedPoint(triangle.Point3, newTriangles.Count()); //Check if points already exist if (!pMap.ContainsKey(hashA)) { pMap.Add(hashA, new List <IndexedPoint>()); } if (!pMap.ContainsKey(hashB)) { pMap.Add(hashB, new List <IndexedPoint>()); } if (!pMap.ContainsKey(hashC)) { pMap.Add(hashC, new List <IndexedPoint>()); } //Go through each triangle refrenced by each of the points of the current riangles and check if there is already //a triangle that contains all three points for (int a = 0; a < pMap[hashA].Count && !duplicate; a++) { var pA = pMap[hashA][a]; for (int b = 0; b < pMap[hashB].Count && !duplicate; b++) { var pB = pMap[hashB][b]; for (int c = 0; c < pMap[hashC].Count && !duplicate; c++) { var pC = pMap[hashC][c]; bool sameTriangle = false; for (int a2 = 0; a2 < pA.indexList.Count && !sameTriangle; a2++) { int aIs = pA.indexList[a2]; for (int b2 = 0; b2 < pB.indexList.Count && !sameTriangle; b2++) { int bIs = pB.indexList[b2]; for (int c2 = 0; c2 < pC.indexList.Count && !sameTriangle; c2++) { int cIs = pC.indexList[c2]; if (aIs == bIs && bIs == cIs) { sameTriangle = true; break; } } } } //If point A, B and C are not from the same triangle then continue //if (pA.index != pB.index && pA.index != pC.index) if (!sameTriangle) { continue; } //Check if the triangle is a duplicate if (tA == pA && tB == pB && tC == pC) { duplicate = true; break; } } } } if (!duplicate) { //If this is a new triangle then we should add the index of this triangle to each of its three points //in the hashed and larger list //Determine the index of each point in the hashtable int indexA = indexOfPointInList(pMap[hashA], tA); int indexB = indexOfPointInList(pMap[hashB], tB); int indexC = indexOfPointInList(pMap[hashC], tC); if (indexA == -1) { //If this is a new point then add it to the hash table and the larger list tA.index = newPoints.Count; pMap[hashA].Add(tA); triangle.p1Idx = newPoints.Count; newPoints.Add(tA); } else { //If the point already exists then add the index of the current triangle to the list of triangles //that contain the point pMap[hashA][indexA].indexList.Add(newTriangles.Count); triangle.p1Idx = pMap[hashA][indexA].index; //Store the index of the point in the triangle newPoints[pMap[hashA][indexA].index].indexList.Add(newTriangles.Count); } if (indexB == -1) { tB.index = newPoints.Count; pMap[hashB].Add(tB); triangle.p2Idx = newPoints.Count; newPoints.Add(tB); } else { pMap[hashB][indexB].indexList.Add(newTriangles.Count); triangle.p2Idx = pMap[hashB][indexB].index; newPoints[pMap[hashB][indexB].index].indexList.Add(newTriangles.Count); } if (indexC == -1) { tC.index = newPoints.Count; pMap[hashC].Add(tC); triangle.p3Idx = newPoints.Count; newPoints.Add(tC); } else { pMap[hashC][indexC].indexList.Add(newTriangles.Count); triangle.p3Idx = pMap[hashC][indexC].index; newPoints[pMap[hashC][indexC].index].indexList.Add(newTriangles.Count); } //Add the new triangle to the list newTriangles.Add(triangle); } } //For each triangle determine if and which other triangle also contains two of its points and store the index of the other //triangle as the one touching each side for (int i = 0; i < newTriangles.Count; i++) { var triangle = newTriangles[i]; triangle.indexTouchingSide[0] = faceWithIndexPoints(newPoints, triangle.p1Idx, triangle.p2Idx, i); triangle.indexTouchingSide[1] = faceWithIndexPoints(newPoints, triangle.p1Idx, triangle.p3Idx, i); triangle.indexTouchingSide[2] = faceWithIndexPoints(newPoints, triangle.p2Idx, triangle.p3Idx, i); } Global.Values.initialTriangleList = newTriangles; }