public void TestToArray() { var pool = new TrianglePool(); // Create 4 triangles. pool.Get(); pool.Get(); pool.Get(); pool.Get(); var a = pool.ToArray(); Assert.AreEqual(4, a.Length); Assert.AreEqual(0, a[0].ID); Assert.AreEqual(1, a[1].ID); Assert.AreEqual(2, a[2].ID); Assert.AreEqual(3, a[3].ID); pool.Release(a[1]); a = pool.ToArray(); Assert.AreEqual(3, a.Length); Assert.AreEqual(0, a[0].ID); Assert.AreEqual(2, a[1].ID); Assert.AreEqual(3, a[2].ID); pool.Release(a[1]); a = pool.ToArray(); Assert.AreEqual(2, a.Length); Assert.AreEqual(0, a[0].ID); Assert.AreEqual(3, a[1].ID); var t2 = pool.Get(); a = pool.ToArray(); Assert.AreEqual(3, a.Length); Assert.AreEqual(0, a[0].ID); Assert.AreEqual(2, a[1].ID); Assert.AreEqual(2, t2.ID); Assert.AreEqual(3, a[2].ID); var t1 = pool.Get(); a = pool.ToArray(); Assert.AreEqual(4, a.Length); Assert.AreEqual(0, a[0].ID); Assert.AreEqual(1, a[1].ID); Assert.AreEqual(1, t1.ID); Assert.AreEqual(2, a[2].ID); Assert.AreEqual(3, a[3].ID); }
public void TestRestart() { var pool = new TrianglePool(); Assert.AreEqual(0, pool.Count); Assert.AreEqual(0, pool.Capacity); int n = 10; for (int i = 0; i < n; i++) { Assert.AreEqual(i, pool.Get().ID); } Assert.AreEqual(n, pool.Count); pool.Restart(); Assert.AreEqual(0, pool.Count); Assert.AreEqual(10, pool.Capacity); }
public void TestGetRelease() { var pool = new TrianglePool(); var t0 = pool.Get(); var t1 = pool.Get(); var t2 = pool.Get(); Assert.AreEqual(0, t0.ID); Assert.AreEqual(1, t1.ID); Assert.AreEqual(2, t2.ID); Assert.AreEqual(3, pool.Count); pool.Release(t0); Assert.AreEqual(2, pool.Count); Assert.Less(t0.GetHashCode(), 0); pool.Release(t1); Assert.AreEqual(1, pool.Count); Assert.Less(t1.GetHashCode(), 0); var t4 = pool.Get(); Assert.AreEqual(2, pool.Count); Assert.AreEqual(t1.ID, t4.ID); var t5 = pool.Get(); Assert.AreEqual(3, pool.Count); Assert.AreEqual(t0.ID, t5.ID); var t6 = pool.Get(); Assert.AreEqual(4, pool.Count); Assert.AreEqual(3, t6.ID); }
/* loop through each point to add, brute force search through each triangle, if * the point is contained in the triangle's circumcircle remove it. Create a polygon with * each removed triangle's outer edge, and add the triangles made by linking each edge to * the new point */ private void Triangulate() { triangulation.Clear(); trianglePool.Clear(); // first triangle, containing the whole playground superTriangle = trianglePool.Get(); superTriangle.Populate(new Vector2(-halfWidth * 2.5f * scalePlayground, -halfHeight * 2 * scalePlayground), new Vector2(halfWidth * 2.5f * scalePlayground, -halfHeight * 2 * scalePlayground), new Vector2(0.0f, halfHeight * 3 * scalePlayground)); triangulation.Add(superTriangle); List <Triangle> badTriangles = new List <Triangle>(); List <Edge> polygon = new List <Edge>(); for (int i = 0; i < points.Count; i++) { Vector2 point = points[i].position; badTriangles.Clear(); polygon.Clear(); // check if the triangle contains point in its circumcircle foreach (Triangle triangle in triangulation) { if (triangle.isPointInsideCircumcircle(point)) { badTriangles.Add(triangle); } } // create the outer polygon for (int outer = 0; outer < badTriangles.Count; outer++) { for (int edge = 0; edge < 3; edge++) { bool isShared = false; for (int inner = 0; inner < badTriangles.Count; inner++) { if (inner != outer && !isShared) { for (int badEdge = 0; badEdge < 3; badEdge++) { if (badTriangles[outer].edges[edge].Compare(badTriangles[inner].edges[badEdge])) { isShared = true; } } } } if (!isShared) { polygon.Add(badTriangles[outer].edges[edge]); } } } // remove bad triangles for (int j = 0; j < badTriangles.Count; j++) { trianglePool.Remove(badTriangles[j]); triangulation.Remove(badTriangles[j]); } // create new triangles for (int j = 0; j < polygon.Count; j++) { Triangle thisTriangle = trianglePool.Get(); thisTriangle.Populate(polygon[j].A, polygon[j].B, point); triangulation.Add(thisTriangle); } } }