public void CanComputeDelaunay() { var maxX = 100; var maxY = 100; var point0 = new DelaunayPoint(0, 0); var point1 = new DelaunayPoint(0, maxY); var point2 = new DelaunayPoint(maxX, maxY); var point3 = new DelaunayPoint(maxX, 0); var point4 = new DelaunayPoint(maxX / 2.0, maxY / 2.0); var triangle1 = new DelaunayTriangle(point0, point1, point2); var triangle2 = new DelaunayTriangle(point0, point2, point3); var border = new List <DelaunayTriangle> { triangle1, triangle2 }; var delaunay = Delaunay.Compute( new List <DelaunayPoint> { point0, point1, point2, point3, point4 }, border ) .ToList(); Assert.True(delaunay.Count == 4); var voronoi = Delaunay.Voronoi(delaunay); // TODO: We expect 8 because it currently outputs the lines repeated twice. Assert.True(voronoi.ToList().Count == 8); }
// Use this for initialization void Awake() { d = new Delaunay(); vec = new List <Vector3>(); for (int i = 0; i < vtxnum; i++) { vec.Add(Random.insideUnitSphere * R); } d.SetData(vec); Mesh mesh = new Mesh(); Vector3[] vertices = new Vector3[d.triangles.Count * 3]; int[] indices = new int[d.triangles.Count * 3]; for (int i = 0; i < d.triangles.Count; i++) { vertices[3 * i + 0] = d.triangles[i].v1; vertices[3 * i + 1] = d.triangles[i].v2; vertices[3 * i + 2] = d.triangles[i].v3; for (int j = 0; j < 3; j++) { indices[3 * i + j] = 3 * i + j; } } mesh.vertices = vertices; // TODO mesh.uv mesh.triangles = indices; mesh.RecalculateNormals(); mf = gameObject.GetComponent <MeshFilter>(); mf.mesh = mesh; }
/// <summary> /// Obtains the navigable Delaunay triangulation and instantiates the pathfinder /// with it. /// </summary> void Start() { Dungeoneer dungeoneer = GameObject.Find("DungeonSummoner").GetComponent <Dungeoneer>(); Delaunay graph = dungeoneer.navigableDelaunay; pathfinder = new AStar(graph); }
private void GenMesh() { Delaunay delaunay = new Delaunay(); List <Point2D> points = new List <Point2D>(); foreach (Entity entity in entityBox1.GetEntities()) { Point screen = entityBox1.LambdaToScreen(entity.LambdaX, entity.LambdaY); points.Add(new Point2D(screen.X, screen.Y)); } // // Триангулируем // List <Triangle> mesh; try { mesh = delaunay.GenMesh(points); } catch { return; } // // Отобразить треугольную сетку // foreach (Triangle tri in mesh) { AddTriangle(entityBox1, tri); } }
private void OnSceneGUI() { var area = target as Area; if (area.Corners.Count <= 2) { return; } Handles.color = Color.white; var triangles = Delaunay.Triangulate(area.Corners); foreach (var tri in triangles) { var pts = tri.ToVertexList().Select(p => new Vector3(p.x, 0f, p.y)).ToList(); pts.Add(pts[0]); for (int i = 0; i < 3; ++i) { Handles.DrawLine(pts[i], pts[i + 1]); } } Handles.color = Color.blue; for (int i = 0; i < area.Corners.Count; ++i) { ShowCorner(area, i); } }
/// <summary> /// Initializes a new instance of the <see cref="AbstractTriangulationPageViewModel" /> class. /// </summary> public AbstractTriangulationPageViewModel() { this.SourcePicture = new Picture(); this.delaunay = new Delaunay(); this.NamedColors = new List <NamedColor>(); this.ResetColorPropertyInfos(); }
/// <summary> /// Резиновая трансформация изображений /// </summary> /// <param name="boxFrom"></param> /// <param name="boxTo"></param> private void RubberWarping(EntityBox boxFrom, EntityBox boxTo) { Delaunay delaunay = new Delaunay(); // // Получить ключевые точки // List <Point2D> pointsLeft = GetKeypoints(boxFrom); List <Point2D> pointsRight = GetKeypoints(boxTo); // // Добавить точки по краям изображения // float gapFromWidth = 25; float gapFromHeight = 25; float scale = new Vec(pointsLeft[0], pointsLeft[1]).Length() / new Vec(pointsRight[0], pointsRight[1]).Length(); float gapToWidth = gapFromWidth * scale; float gapToHeight = gapFromHeight * scale; AddCornerKeypoints(ref pointsLeft, gapFromWidth, gapFromHeight); AddCornerKeypoints(ref pointsRight, gapToWidth, gapToHeight); // // Триангулируем // List <Triangle> mesh = delaunay.GenMesh(pointsLeft); // // Сформировать изображение справа // Point2D bottomDownTo = BottomDownPoint(pointsRight); Bitmap bitmap = new Bitmap((int)bottomDownTo.X + 1, (int)bottomDownTo.Y + 1); Graphics gr = Graphics.FromImage(bitmap); gr.Clear(Color.Gray); boxTo.Image0 = bitmap; // // Трилатеральный перенос треугольников левого изображения в правое // foreach (Triangle sourceTri in mesh) { Triangle destTri = new Triangle(); destTri.a = MapPoint(sourceTri.a, pointsRight); destTri.b = MapPoint(sourceTri.b, pointsRight); destTri.c = MapPoint(sourceTri.c, pointsRight); NonAffineTransform.WarpTriangle(boxFrom.Image0, boxTo.Image0, sourceTri, destTri); } }
private void renderDelaunayTriangulation(Delaunay del, Color color, float renderHeight) { // Start immediate mode drawing for lines GL.PushMatrix(); foreach (Triangle tri in del.triangles) { GL.Begin(GL.LINES); setGraphColors(color); Vector3 a = tri.a.toVector3AtHeight(renderHeight); Vector3 b = tri.b.toVector3AtHeight(renderHeight); Vector3 c = tri.c.toVector3AtHeight(renderHeight); // ab GL.Vertex3(a.x, a.y, a.z); GL.Vertex3(b.x, b.y, b.z); // bc GL.Vertex3(c.x, c.y, c.z); // ca GL.Vertex3(a.x, a.y, a.z); GL.End(); } GL.PopMatrix(); }
/// <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 generateDelaunay() { Debug.Log("Generating Delaunay"); Poisson dist = new Poisson(currentDungeonType.minimumPoissonDistance, currentDungeonType.width, currentDungeonType.height); dt = new Delaunay(dist.vertices); Debug.Log($"Generated Delaunay with {dt.vertices.Count} vertices."); }
/// <summary> /// Instantiates AStar using the given Delaunay Triangulation. /// </summary> /// <param name="graph"></param> public AStar(Delaunay graph) { navGraph = graph; // DEBUG: Uncomment if pathfinding not working correctly. // Debug.Log("AStar is working. Graph: " + navGraph); // outputGraphToFile(); }
// Random endless level generation // Determines the amount of shepherds placed based on the current level number private ShepherdLevel CreateEndlessLevel(int level) { // create the output scriptable object var asset = ScriptableObject.CreateInstance <ShepherdLevel>(); // place the shepherds and sheep randomly List <Vector2> shepherds = RandomPos(level + 4); List <Vector2> sheep = RandomPos(2 * (level + 4)); // Print locations string sls = "Shepherd locations: \n"; foreach (Vector2 v in shepherds) { sls += "(" + v.x + ", " + v.y + "), "; } Debug.Log(sls); string shls = "Sheep locations: \n"; foreach (Vector2 v in sheep) { shls += "(" + v.x + ", " + v.y + "), "; } Debug.Log(shls); // Construct the voronoi diagram corresponding to the shepherd locations StartVoronoi(); foreach (Vector2 me in shepherds) { // Add vertex to the triangulation and update the voronoi Delaunay.AddVertex(m_delaunay, me); m_delaunay.SetOwner(me, Random.Range(0, 4)); m_dcel = Voronoi.Create(m_delaunay); } // Create vertical decomposition VerticalDecomposition vd = VertDecomp(m_dcel); // Use the vertical decomposition to determine the ownership of each sheep // and add the sheep to the level foreach (Vector2 s in sheep) { Trapezoid trap = vd.Search(s); Face area = trap.bottom.face; int i = area.owner; asset.addSheep(s, i); } // Normalize coordinates var rect = BoundingBoxComputer.FromPoints(asset.SheepList); asset.SheepList = Normalize(rect, 6f, asset.SheepList); // Set shepherd budget asset.setBudget(shepherds.Count); return(asset); }
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 RemovePoint(Transform transf) { points.RemoveAt(pointsTr.findIndex(transf)); points.sortX(); convexHull = Jarvis.GetConvexHull(points); triangulation = new Delaunay(points); voronoi = new Voronoi(triangulation); RecreatePoints(); }
public MainControllerImpl(IMainView view) { _view = view; _delaunay = new Delaunay(); _watershed = new Watershed(_delaunay); _root = new Node("0"); _vertices = new List <Vertex>(); }
private void Init() { //_pointList = new List<PVector>(SetOuPoint(3)); _pointList = new List <PVector>(SetRndPoint(10)); _d = new Delaunay(_pointList); _renderTriangleList = new List <Triangle>(_d.OutsideTriangleList); }
/// <summary> /// Creates a Voronoi DCEL from a triangulation. Triangulation should be Delaunay /// </summary> /// <param name="m_Delaunay"></param> /// <returns></returns> public static DCEL Create(Triangulation m_Delaunay) { if (!Delaunay.IsValid(m_Delaunay)) { throw new GeomException("Triangulation should be delaunay for the Voronoi diagram."); } var dcel = new DCEL(); // create vertices for each triangles circumcenter and store them in a dictionary Dictionary <Triangle, DCELVertex> vertexMap = new Dictionary <Triangle, DCELVertex>(); foreach (var triangle in m_Delaunay.Triangles) { // degenerate triangle, just ignore if (!triangle.Circumcenter.HasValue) { continue; } var vertex = new DCELVertex(triangle.Circumcenter.Value); dcel.AddVertex(vertex); vertexMap.Add(triangle, vertex); } // remember which edges where visited // since each edge has a twin var edgesVisited = new HashSet <TriangleEdge>(); foreach (var edge in m_Delaunay.Edges) { // either already visited twin edge or edge is outer triangle if (edgesVisited.Contains(edge) || edge.IsOuter) { continue; } // add edge between the two adjacent triangles vertices // vertices at circumcenter of triangle if (edge.T != null && edge.Twin.T != null) { var v1 = vertexMap[edge.T]; var v2 = vertexMap[edge.Twin.T]; HalfEdge e1 = dcel.AddEdge(v1, v2); e1.Twin.Face.owner = m_Delaunay.GetOwner(edge.Point1); e1.Face.owner = m_Delaunay.GetOwner(edge.Point2); edgesVisited.Add(edge); edgesVisited.Add(edge.Twin); } } return(dcel); }
private void OnDrawGizmos() { if (!isEnabled) { return; } //Generate the random sites List <Vector2> randomSites = new List <Vector2>(); //Generate random numbers with a seed Random.InitState(seed); int max = mapRadius; int min = -mapRadius; for (int i = 0; i < numberOfPoints; i++) { int randomX = Random.Range(min, max); int randomZ = Random.Range(min, max); randomSites.Add(new Vector2(randomX, randomZ)); } //Points outside of the screen for voronoi which has some cells that are infinite float bigSize = mapRadius * 5f; //Star shape which will give a better result when a cell is infinite large //When using other shapes, some of the infinite cells misses triangles randomSites.Add(new Vector2(0f, bigSize)); randomSites.Add(new Vector2(0f, -bigSize)); randomSites.Add(new Vector2(bigSize, 0f)); randomSites.Add(new Vector2(-bigSize, 0f)); //Generate the voronoi diagram var(vertices, triangles) = Delaunay.GenerateTriangulation(randomSites); var cells = Delaunay.GenerateVoronoiCells((vertices, triangles)); //Display the voronoi diagram LevelDataTester.DrawShapes(cells.Values.ToArray(), Vector3.zero); if (showDelaunayDiagam) { LevelDataTester.DrawShapes(triangles.Values.ToArray(), Vector3.forward, true); } //Display the sites Gizmos.color = Color.white; for (int i = 0; i < randomSites.Count; i++) { float radius = 0.2f; Gizmos.DrawSphere(randomSites[i], radius); } }
}//createMeshes public void addPoint(Vector2 pos) { points.Add(pos); points.sortX(); convexHull = Jarvis.GetConvexHull(points); triangulation = new Delaunay(points); voronoi = new Voronoi(triangulation); RecreatePoints(); }
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 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 updateview2() { if (Delaunay.Checked && dlay.Count > 0) { Delaunay.PerformClick(); } if (Tyson.Checked && dlay.Count > 0) { Tyson.PerformClick(); } }
void UpdateMesh(Delaunay delaunay) { Vector3[] outVertices; int[] outTriangles; delaunay.GenerateMesh(out outVertices, out outTriangles); _mesh.vertices = outVertices; _mesh.triangles = outTriangles; _mesh.RecalculateBounds(); _mesh.RecalculateNormals(); }
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 InValidTriangulationTest() { var m_delaunay = new Triangulation(); var t1 = new Triangle(m_topVertex, m_rightVertex, m_botVertex); var t2 = new Triangle(m_topVertex, m_botVertex, m_leftVertex); t1.E2.Twin = t2.E0; t2.E0.Twin = t1.E2; m_delaunay.AddTriangle(t1); m_delaunay.AddTriangle(t2); Assert.IsFalse(Delaunay.IsValid(m_delaunay)); }
public void Initialize() { UniformPoissonDiskSampler.Random = new Random(Seed); Points = UniformPoissonDiskSampler.SampleRectangle(new Vector2(0, 0), new Vector2(Width, Height), Radius); Delaunay = Delaunay.from(Points.Select(point => new double[] { point.X, point.Y }).ToArray()); Voronoi = Delaunay.voronoi(new Bounds { x0 = 0.5, y0 = 0.5, x1 = Width - 0.5, y1 = Height - 0.5 }); CellPolygons = Voronoi.cellPolygons().ToList(); }
public Triangle[] Delaunay() { Vertex[] apexs = GetAllDelaunayApex(); Delaunay delaunay = new Delaunay(); delaunay.Triangulate(apexs); //为三角形添加颜色 var tris = delaunay.Triangles; SampleColor(tris); return(tris.ToArray()); }
/// <summary> /// Сгенерировать и показать треугольную сетку /// </summary> /// <param name="box"></param> private void GenerateMesh(EntityBox box) { Delaunay delaunay = new Delaunay(); List <Point2D> points = new List <Point2D>(); // // Получить ключевые точки // foreach (Entity entity in box.GetEntities()) { if (entity.IsVias()) { Point p = box.LambdaToScreen(entity.LambdaX, entity.LambdaY); Point2D point = new Point2D(p.X, p.Y); points.Add(point); } } if (points.Count < 3) { MessageBox.Show("3 or more keypoints required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } // // Добавить ключевые точки по углам // AddCornerKeypoints(ref points, 25, 25); // // Триангулируем // List <Triangle> mesh = delaunay.GenMesh(points); // // Отобразить треугольную сетку // Random rnd = new Random(); foreach (Triangle tri in mesh) { Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)); AddTriangle(box, tri, randomColor); } }
private void GenDelaunayTreeDecks(List <Vector3> points) { GameObject DeckNode = new GameObject("Deck Node"); // Magic and beautiful Delaunay triangulation List <Triangle> triangulation = Delaunay.TriangulateByFlippingEdges(points); // Area filter setup double[] check_range = MathFilter.AreaSpan(triangulation, MaxLength, MinAngle, RelativeArea); foreach (Triangle tri in triangulation) { // Length constraint. if 0, no constraint if (MathFilter.LengthFilter(tri, MaxLength)) { List <double> TriAngles = MathFilter.AngleDegrees(tri); // Angle constraint if (TriAngles.TrueForAll(angle => angle >= MinAngle)) { double area = MathFilter.TriangleArea(tri); // Area constraint if (check_range[0] <= area && area <= check_range[1]) { Vector3 P1 = tri.v1.position; Vector3 P2 = tri.v2.position; Vector3 P3 = tri.v3.position; // Brett deck GameObject TreeDeck = Instantiate(DeckObject); Treehouse TreehouseComp = TreeDeck.AddComponent <Treehouse>(); TreeDeck.AddComponent <Deck>(); // Deck component needs 3 GameObjects. In the future this // may become 3 Vector3s. GameObject newTreeNode1 = Instantiate(DeckNode, P1, rot); GameObject newTreeNode2 = Instantiate(DeckNode, P2, rot); GameObject newTreeNode3 = Instantiate(DeckNode, P3, rot); List <GameObject> TreesTri = new List <GameObject>(); TreesTri.Add(newTreeNode1); TreesTri.Add(newTreeNode2); TreesTri.Add(newTreeNode3); TreehouseComp.Trees = TreesTri; } } } } }
public void Awake() { _triangles = new List <Triangle>(); _triangles.AddRange(Delaunay.Triangulate(Corners)); _areaSum = 0f; _triangles.ForEach(x => _areaSum += x.TriArea()); foreach (var tri in _triangles) { var pts = tri.ToVertexList().Select(p => new Vector3(p.x, 0f, p.y)).ToList(); pts.Add(pts[0]); _linesToDraw[tri] = pts; } }
private void BuildGraph(IEnumerable<Vector2> points, Delaunay.Voronoi voronoi) { // Build graph data structure in 'edges', 'centers', 'corners', // based on information in the Voronoi results: point.neighbors // will be a list of neighboring points of the same type (corner // or center); point.edges will be a list of edges that include // that point. Each edge connects to four points: the Voronoi edge // edge.{v0,v1} and its dual Delaunay triangle edge edge.{d0,d1}. // For boundary polygons, the Delaunay edge will have one null // point, and the Voronoi edge may be null. var libedges = voronoi.Edges(); var centerLookup = new Dictionary<Vector2?, Center>(); // Build Center objects for each of the points, and a lookup map // to find those Center objects again as we build the graph foreach (var point in points) { var p = new Center { index = centers.Count, point = point }; centers.Add(p); centerLookup[point] = p; } // Workaround for Voronoi lib bug: we need to call region() // before Edges or neighboringSites are available foreach (var p in centers) { voronoi.Region(p.point); } foreach (var libedge in libedges) { var dedge = libedge.DelaunayLine(); var vedge = libedge.VoronoiEdge(); // Fill the graph data. Make an Edge object corresponding to // the edge from the voronoi library. var edge = new Edge { index = edges.Count, river = 0, // Edges point to corners. Edges point to centers. v0 = MakeCorner(vedge.p0), v1 = MakeCorner(vedge.p1), d0 = centerLookup[dedge.p0], d1 = centerLookup[dedge.p1] }; if (vedge.p0.HasValue && vedge.p1.HasValue) edge.midpoint = Vector2Extensions.Interpolate(vedge.p0.Value, vedge.p1.Value, 0.5f); edges.Add(edge); // Centers point to edges. Corners point to edges. if (edge.d0 != null) { edge.d0.borders.Add(edge); } if (edge.d1 != null) { edge.d1.borders.Add(edge); } if (edge.v0 != null) { edge.v0.protrudes.Add(edge); } if (edge.v1 != null) { edge.v1.protrudes.Add(edge); } // Centers point to centers. if (edge.d0 != null && edge.d1 != null) { AddToCenterList(edge.d0.neighbors, edge.d1); AddToCenterList(edge.d1.neighbors, edge.d0); } // Corners point to corners if (edge.v0 != null && edge.v1 != null) { AddToCornerList(edge.v0.adjacent, edge.v1); AddToCornerList(edge.v1.adjacent, edge.v0); } // Centers point to corners if (edge.d0 != null) { AddToCornerList(edge.d0.corners, edge.v0); AddToCornerList(edge.d0.corners, edge.v1); } if (edge.d1 != null) { AddToCornerList(edge.d1.corners, edge.v0); AddToCornerList(edge.d1.corners, edge.v1); } // Corners point to centers if (edge.v0 != null) { AddToCenterList(edge.v0.touches, edge.d0); AddToCenterList(edge.v0.touches, edge.d1); } if (edge.v1 != null) { AddToCenterList(edge.v1.touches, edge.d0); AddToCenterList(edge.v1.touches, edge.d1); } } // TODO: use edges to determine these var topLeft = centers.OrderBy(p => p.point.x + p.point.y).First(); AddCorner(topLeft, 0, 0); var bottomRight = centers.OrderByDescending(p => p.point.x + p.point.y).First(); AddCorner(bottomRight, Width, Height); var topRight = centers.OrderByDescending(p => Width - p.point.x + p.point.y).First(); AddCorner(topRight, 0, Height); var bottomLeft = centers.OrderByDescending(p => p.point.x + Height - p.point.y).First(); AddCorner(bottomLeft, Width, 0); // required for polygon fill foreach (var center in centers) { center.corners.Sort(ClockwiseComparison(center)); } }
// Use this for initialization void Start() { GetComponent<MeshFilter>().mesh = _mesh = new Mesh(); _delaunay = new Delaunay(largeTriangle[0], largeTriangle[1], largeTriangle[2]); UpdateMesh (_delaunay); }
private void Start() { m_Delaunay = new Delaunay(); m_Delaunay.Create(); }