public TriangleEdge(PMXVertex vtx1b, PMXVertex vtx2b, bool identicalLocation) { this._identicalLocation = identicalLocation; PMXExtendedVertex vtx1 = (PMXExtendedVertex)vtx1b; PMXExtendedVertex vtx2 = (PMXExtendedVertex)vtx2b; if (vtx1.EasySlashIndex < vtx2.EasySlashIndex) { this.Vertex1 = vtx1; this.Vertex2 = vtx2; } else { this.Vertex1 = vtx2; this.Vertex2 = vtx1; } if (!identicalLocation) { this._hashCode = ((~this.Vertex1.EasySlashIndex) ^ this.Vertex2.EasySlashIndex); } else { this._hashCode = (this.Vertex1.Position.X + this.Vertex1.Position.Y + this.Vertex1.Position.Z + this.Vertex2.Position.X + this.Vertex2.Position.Y + this.Vertex2.Position.Z).GetHashCode(); } }
private PMXVertex GetVertexOfIndex(ref int index) { PMXVertex vtx = null; if (pmxModel.Vertices.Count <= index) { vtx = new PMXExtendedVertex(pmxModel); pmxModel.Vertices.Add(vtx); } else { vtx = pmxModel.Vertices[index]; } index++; return(vtx); }
static void CleanUpModel(PMXModel md) { Console.WriteLine("Duplicate search started."); Stopwatch sw = Stopwatch.StartNew(); PMXVertex[] vtxArrayBase = md.Vertices.ToArray(); //Arrays are faster to index than lists PMXExtendedVertex[] vtxArray = new PMXExtendedVertex[vtxArrayBase.Length]; for (int i = 0; i < vtxArrayBase.Length; i++) { vtxArray[i] = (PMXExtendedVertex)vtxArrayBase[i]; } int[] duplicateOf = new int[vtxArray.Length]; for (int i = 0; i < duplicateOf.Length; i++) { ((PMXExtendedVertex)vtxArray[i]).EasySlashIndex = i; duplicateOf[i] = -1; } Console.WriteLine(duplicateOf.Length + " vertices to consider!"); Console.WriteLine("Sorting"); PMXExtendedVertex[] sortedArray = new PMXExtendedVertex[vtxArray.Length]; vtxArray.CopyTo(sortedArray, 0); Array.Sort(sortedArray); Console.WriteLine("Vertices sorted"); for (int i = 0; i < duplicateOf.Length - 1; i++) { int originalIndexI = sortedArray[i].EasySlashIndex; if (i % 5000 == 0 && i != 0) { Console.WriteLine(i + " vertices verified"); } if (duplicateOf[originalIndexI] >= 0) //Already marked as duplicate { continue; } for (int j = i + 1; j < duplicateOf.Length; j++) { int originalIndexJ = sortedArray[j].EasySlashIndex; if (duplicateOf[originalIndexJ] >= 0) //Already marked as duplicate of some other vertex { continue; } if (sortedArray[i].Equals(sortedArray[j])) { duplicateOf[originalIndexJ] = originalIndexI; } else { break; } } } List <int> slashables = new List <int>(); for (int i = duplicateOf.Length - 1; i >= 0; i--) { if (duplicateOf[i] >= 0) //Already marked as duplicate { slashables.Add(i); } } foreach (PMXMaterial mat in md.Materials) { foreach (PMXTriangle tri in mat.Triangles) { tri.Vertex1 = ReplaceVertexIfRequired(tri.Vertex1, md, duplicateOf); tri.Vertex2 = ReplaceVertexIfRequired(tri.Vertex2, md, duplicateOf); tri.Vertex3 = ReplaceVertexIfRequired(tri.Vertex3, md, duplicateOf); } } foreach (int slash in slashables) { md.Vertices.RemoveAt(slash); } sw.Stop(); Console.WriteLine(slashables.Count + " duplicate vertices identified - " + sw.ElapsedMilliseconds + " milliseconds"); }