/// <summary> /// Create Voronoi DCEL from a collection of vertices. /// First creates a delaunay triangulation and then the corresponding Voronoi diagram /// </summary> /// <param name="vertices"></param> /// <returns>DCEL representation of Voronoi diagram</returns> public static DCEL Create(IEnumerable <Vector2> vertices) { // create delaunay triangulation // from this the voronoi diagram can be obtained var m_Delaunay = Delaunay.Create(vertices); return(Create(m_Delaunay)); }
public void VoronoiFromDelaunayTest2() { var delaunay = Delaunay.Create(m_allVertices); var voronoi = Voronoi.Create(delaunay); Assert.AreEqual(6, voronoi.VertexCount); Assert.AreEqual(7, voronoi.EdgeCount); Assert.AreEqual(3, voronoi.FaceCount); }
public void DelaunayTriangulation2() { var m_delaunay = Delaunay.Create(m_arrowVertices); Assert.IsTrue(Delaunay.IsValid(m_delaunay)); var triangles = (System.Collections.ICollection)m_delaunay.Triangles; Assert.Contains(new Triangle(m_topVertex, m_rightVertex, m_botVertex), triangles); Assert.Contains(new Triangle(m_topVertex, m_farRightVertex, m_rightVertex), triangles); Assert.Contains(new Triangle(m_rightVertex, m_farRightVertex, m_botVertex), triangles); }
public void DelaunayTriangulation1() { var m_delaunay = Delaunay.Create(m_diamondVertices); Assert.IsTrue(Delaunay.IsValid(m_delaunay)); Assert.AreEqual(m_diamond.Area, m_delaunay.Area, MathUtil.EPS); var triangles = (System.Collections.ICollection)m_delaunay.Triangles; Assert.Contains(new Triangle(m_topVertex, m_rightVertex, m_leftVertex), triangles); Assert.Contains(new Triangle(m_leftVertex, m_rightVertex, m_botVertex), triangles); }
public void TestCocircularPoints() { var m_Delaunay = Delaunay.Create(); m_Delaunay.AddVertex(new Vector2(1, 1)); m_Delaunay.AddVertex(new Vector2(1, 2)); m_Delaunay.AddVertex(new Vector2(2, 2)); m_Delaunay.AddVertex(new Vector2(2, 1)); m_Delaunay.AddVertex(new Vector2(1, 3)); m_Delaunay.AddVertex(new Vector2(2, 3)); }
public void TestColinearPoints() { var m_Delaunay = Delaunay.Create(); m_Delaunay.AddVertex(new Vector2(1, 1)); m_Delaunay.AddVertex(new Vector2(2, 2)); m_Delaunay.AddVertex(new Vector2(3, 3)); m_Delaunay.AddVertex(new Vector2(1, 4)); m_Delaunay.AddVertex(new Vector2(2, 8)); m_Delaunay.AddVertex(new Vector2(3, 12)); }
public void DelaunayAddVertexTest() { var m_delaunay = Delaunay.Create(); Delaunay.AddVertex(m_delaunay, m_topVertex); Delaunay.AddVertex(m_delaunay, m_botVertex); Delaunay.AddVertex(m_delaunay, m_leftVertex); Delaunay.AddVertex(m_delaunay, m_farRightVertex); Delaunay.AddVertex(m_delaunay, m_rightVertex); m_delaunay.RemoveInitialTriangle(); Assert.IsTrue(Delaunay.IsValid(m_delaunay)); // check for triangles var triangles = (System.Collections.ICollection)m_delaunay.Triangles; Assert.Contains(new Triangle(m_topVertex, m_rightVertex, m_leftVertex), triangles); Assert.Contains(new Triangle(m_leftVertex, m_rightVertex, m_botVertex), triangles); Assert.Contains(new Triangle(m_topVertex, m_farRightVertex, m_rightVertex), triangles); Assert.Contains(new Triangle(m_rightVertex, m_farRightVertex, m_botVertex), triangles); }
void Generate() { del.Create(); foreach (Triangle t in del.triangles) { if (t.GetReplacements() != null) { continue; } if ((t[0] == 0 || t[0] == 1 || t[0] == 2) || (t[(0 + 1) % 3] == 0 || t[(0 + 1) % 3] == 1 || t[(0 + 1) % 3] == 2) || (t[(0 + 2) % 3] == 0 || t[(0 + 2) % 3] == 1 || t[(0 + 2) % 3] == 2)) { continue; } // Add link CreateLink(systems[new Coordinate(del.vertices[t[0]].coordinate)], systems[new Coordinate(del.vertices[t[1]].coordinate)]); CreateLink(systems[new Coordinate(del.vertices[t[1]].coordinate)], systems[new Coordinate(del.vertices[t[2]].coordinate)]); CreateLink(systems[new Coordinate(del.vertices[t[2]].coordinate)], systems[new Coordinate(del.vertices[t[0]].coordinate)]); } Emit("OnGenerate", new object[] { }); }
// Initial call for the construction of the Voronoi diagram public void StartVoronoi() { m_delaunay = Delaunay.Create(); m_dcel = Voronoi.Create(m_delaunay); VoronoiDrawer.setDCEL(m_dcel); }
// Handle inputs each frame void Update() { if (Input.GetKeyDown("c")) { VoronoiDrawer.CircleOn = !VoronoiDrawer.CircleOn; } if (Input.GetKeyDown("e")) { VoronoiDrawer.EdgesOn = !VoronoiDrawer.EdgesOn; } if (Input.GetKeyDown("v")) { VoronoiDrawer.VoronoiOn = !VoronoiDrawer.VoronoiOn; } // Handle mouse clicks if (Input.GetMouseButtonUp(0)) // LMB was clicked this frame { // Cast a ray, get everything it hits RaycastHit2D[] hit = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity); if (hit.Length > 0) // If we hit something { // Grab the top hit GameObject GameObject lastHitObject = hit[hit.Length - 1].collider.gameObject; // If a shepherd was clicked, remove it if (lastHitObject.name == "shepherd(Clone)") { m_shepherds.Remove(lastHitObject); // remove from shepherd clone list shepherdLocs.Remove(lastHitObject.transform.position); // remove location from list Destroy(lastHitObject); // Create new Delaunay triangulation m_delaunay = Delaunay.Create(); foreach (KeyValuePair <Vector2, int> o in shepherdLocs) { Delaunay.AddVertex(m_delaunay, o.Key); m_delaunay.SetOwner(o.Key, o.Value); } // Create new Voronoi diagram m_dcel = Voronoi.Create(m_delaunay); VoronoiDrawer.setDCEL(m_dcel); // Create vertical decomposition and check solution if (shepherdLocs.Count > 0) { VerticalDecomposition vd = VertDecomp(m_dcel); Debug.LogAssertion("The current solution is " + (CheckSolution(vd) == 0 ? "correct!" : "wrong!")); VoronoiDrawer.SetVD(vd); } else { // If we do not have any shepherds, do not create vertical decomposition // Solution without shepherds is always wrong Debug.LogAssertion("The current solution is wrong!"); continueButton.SetActive(false); UpdateText(m_sheep.Count); } // Update Voronoi drawing UpdateMesh(); } } else // LMB was clicked on empty space { // Add shepherd at mouse location var mousePos = Input.mousePosition; if (!EventSystem.current.IsPointerOverGameObject() && shepherdLocs.Count < budget) { mousePos.z = 2.0f; var objectPos = Camera.main.ScreenToWorldPoint(mousePos); var obj = Instantiate(m_shepherdPrefab, objectPos, Quaternion.identity); SpriteRenderer sr = obj.GetComponent <SpriteRenderer>(); sr.color = Colors[m_activeShepherd]; // The new vertex var me = new Vector2(objectPos.x, objectPos.y); // store owner of vertex shepherdLocs.Add(me, m_activeShepherd); m_shepherds.Add(obj); // Add vertex to the triangulation Delaunay.AddVertex(m_delaunay, me); m_delaunay.SetOwner(me, m_activeShepherd); // Update Voronoi m_dcel = Voronoi.Create(m_delaunay); VoronoiDrawer.setDCEL(m_dcel); // Create vertical decomposition and check solution VerticalDecomposition vd = VertDecomp(m_dcel); CheckSolution(vd); // Update VD in drawer VoronoiDrawer.SetVD(vd); // Update Voronoi drawing UpdateMesh(); } } } }
private void Start() { m_Delaunay = new Delaunay(); m_Delaunay.Create(); }
// Use this for initialization void Start() { Delaunay del = new Delaunay(); Vector3[] starts = new Vector3[] { new Vector3(-10, 5, 0), new Vector3(0, -15, 0), new Vector3(10, 5, 0), }; foreach (Vector3 start in starts) { //GameObject v = Instantiate(vert); //v.transform.position = start/*;*/ del.vertices.Add(new Vertex { coordinate = start }); } //del.vertices.Add(null); //del.vertices.Add(null); //del.vertices.Add(null); int Npoints = 25; for (int i = 0; i < Npoints; ++i) { float x = Random.Range(-5.0f, 5.0f); float y = Random.Range(-5.0f, 5.0f); //if(y > 5 - x) { // continue; //} Vector3 pos = new Vector3(x, y, 0); GameObject v = Instantiate(vert); v.transform.SetParent(transform); v.transform.localPosition = pos; del.vertices.Add(new Vertex { coordinate = pos }); } vert.SetActive(false); del.Create(); foreach (Triangle t in del.triangles) { if (t.GetReplacements() != null) { continue; } if ((t[0] == 0 || t[0] == 1 || t[0] == 2) || (t[(0 + 1) % 3] == 0 || t[(0 + 1) % 3] == 1 || t[(0 + 1) % 3] == 2) || (t[(0 + 2) % 3] == 0 || t[(0 + 2) % 3] == 1 || t[(0 + 2) % 3] == 2)) { continue; } for (int i = 0; i < 3; ++i) { Debug.DrawLine( del.vertices[t[i]].coordinate, del.vertices[t[(i + 1) % 3]].coordinate, Color.yellow, 100 ); GameObject edge = new GameObject("edge"); edge.transform.SetParent(transform); LineRenderer lr = edge.AddComponent <LineRenderer>(); lr.SetPositions(new Vector3[] { del.vertices[t[i]].coordinate, del.vertices[t[(i + 1) % 3]].coordinate }); lr.startWidth = 0.05f; lr.endWidth = 0.05f; lr.startColor = Color.red; lr.endColor = Color.red; lr.gameObject.layer = (int)AppInfo.Layer.StarMap; lr.material = starPathMaterial; } } }