static int[] EnsureCorrectOrdering(VertexWithIndex a, VertexWithIndex b, VertexWithIndex c) { int[] indices = new int[3]; Vector3 AC = c.vertex - a.vertex; AC = new Vector3(AC.x, AC.z, AC.y); Vector3 AB = b.vertex - a.vertex; AB = new Vector3(AB.x, AB.z, AB.y); if (Vector3.Cross(AC, AB).sqrMagnitude > 0) { indices[0] = a.index; indices[1] = b.index; indices[2] = c.index; } else { indices[0] = a.index; indices[1] = c.index; indices[2] = b.index; } return(indices); }
public bool Is(VertexWithIndex other) { if (other.vertex == this.vertex && other.index == this.index) { return(true); } return(false); }
public static int[] TriangulateHull(Vector2[] inputVerts) { if (inputVerts.Length < 3) { if (inputVerts.Length == 2) { return(new int[] { 0, 1, }); } return(null); } List <VertexWithIndex> vertices = new List <VertexWithIndex>(); for (int i = 0; i < inputVerts.Length; i++) { vertices.Add(new VertexWithIndex(inputVerts[i], i)); } List <int> tris = new List <int>(); VertexWithIndex randomSelected = vertices.OrderBy(a => a.vertex.y + a.vertex.x).First(); // Heuristic to pick the lowest x+y value results in all angles being in [0, 90] List <VertexWithIndex> restOfVertices = vertices.Where(v => !v.Is(randomSelected)).ToList(); Stack <VertexWithIndex> V = new Stack <VertexWithIndex>(restOfVertices.OrderBy(x => x.vertex.PositiveAngleTo(randomSelected.vertex))); //The issue is here, it isn't ordering them correctly use the debugger for this method VertexWithIndex secondVertexInTriangle = V.Pop(); VertexWithIndex firstPoppedVertex = secondVertexInTriangle; //So what this algorithm does is triangulates by connecting a vertex with all other vertices. //Then //It only works for convex hulls. while (V.Count > 0) { tris.Add(randomSelected.index); tris.Add(secondVertexInTriangle.index); if (V.Count == 0) { tris.Add(firstPoppedVertex.index); } else { tris.Add(V.Peek().index); } secondVertexInTriangle = V.Pop(); } return(tris.ToArray()); }
public override ChunkContent Process(VoxContent input, ContentProcessorContext context) { m_InputVoxContent = input; var voxels = m_InputVoxContent.Voxels; // load voxels into a grid so we can easily check neighbors var grid = BuildGrid(m_InputVoxContent); var actives = new List <VertexWithIndex>(); var inactives = new List <VertexWithIndex>(); for (var i = 0; i < m_InputVoxContent.Voxels.Length; i++) { var v = voxels[i]; // if the voxel is outside the grid or empty, skip it if (v.X >= grid.Length || v.Y >= grid[0].Length || v.Z >= grid[0][0].Length || v.IsEmpty) { continue; } var data = new VertexWithIndex(voxels[i].X, voxels[i].Y, voxels[i].Z, voxels[i].ColorIndex); if (IsActive(data.X, data.Y, data.Z, grid)) { actives.Add(data); } else { inactives.Add(data); } } var chunk = new ChunkContent(Vector3.Zero, m_InputVoxContent.SizeX, m_InputVoxContent.SizeY, m_InputVoxContent.SizeZ, actives.Concat(inactives).ToArray(), m_InputVoxContent.Palette, actives.Count); return(chunk); }
public static void SortFromPositiveAngleToXAxis(this IEnumerable <VertexWithIndex> points, VertexWithIndex reference) { points.OrderBy(x => reference.vertex.PositiveAngleTo(x.vertex)); }