public static void Restore(DMesh3 mesh, BinaryReader reader) { int version = reader.ReadInt32(); if (version != DMesh3Version) { throw new Exception("gSerialization.Restore: Incorrect DMesh3Version!"); } MeshComponents components = (MeshComponents)reader.ReadInt32(); Restore(mesh.VerticesBuffer, reader); Restore(mesh.TrianglesBuffer, reader); Restore(mesh.EdgesBuffer, reader); Restore(mesh.EdgesRefCounts.RawRefCounts, reader); if ((components & MeshComponents.VertexNormals) != 0) { mesh.EnableVertexNormals(Vector3F.AxisY); Restore(mesh.NormalsBuffer, reader); } else { mesh.DiscardVertexNormals(); } if ((components & MeshComponents.VertexColors) != 0) { mesh.EnableVertexColors(Vector3F.One); Restore(mesh.ColorsBuffer, reader); } else { mesh.DiscardVertexColors(); } if ((components & MeshComponents.VertexUVs) != 0) { mesh.EnableVertexUVs(Vector2F.Zero); Restore(mesh.UVBuffer, reader); } else { mesh.DiscardVertexUVs(); } if ((components & MeshComponents.FaceGroups) != 0) { mesh.EnableTriangleGroups(0); Restore(mesh.GroupsBuffer, reader); } else { mesh.DiscardTriangleGroups(); } mesh.RebuildFromEdgeRefcounts(); }
List <Polygon2d> decompose_cluster_up(DMesh3 mesh) { optimize_mesh(mesh); mesh.CompactInPlace(); mesh.DiscardTriangleGroups(); mesh.EnableTriangleGroups(0); double minLength = Settings.MaxBridgeWidthMM * 0.75; double minArea = minLength * minLength; Dictionary <int, double> areas = new Dictionary <int, double>(); Dictionary <int, HashSet <int> > trisets = new Dictionary <int, HashSet <int> >(); HashSet <int> active_groups = new HashSet <int>(); Action <int, int> add_tri_to_group = (tid, gid) => { mesh.SetTriangleGroup(tid, gid); areas[gid] = areas[gid] + mesh.GetTriArea(tid); trisets[gid].Add(tid); }; Action <int, int> add_group_to_group = (gid, togid) => { var set = trisets[togid]; foreach (int tid in trisets[gid]) { mesh.SetTriangleGroup(tid, togid); set.Add(tid); } areas[togid] += areas[gid]; active_groups.Remove(gid); }; Func <IEnumerable <int>, int> find_min_area_group = (tri_itr) => { int min_gid = -1; double min_area = double.MaxValue; foreach (int tid in tri_itr) { int gid = mesh.GetTriangleGroup(tid); double a = areas[gid]; if (a < min_area) { min_area = a; min_gid = gid; } } return(min_gid); }; foreach (int eid in MeshIterators.InteriorEdges(mesh)) { Index2i et = mesh.GetEdgeT(eid); if (mesh.GetTriangleGroup(et.a) != 0 || mesh.GetTriangleGroup(et.b) != 0) { continue; } int gid = mesh.AllocateTriangleGroup(); areas[gid] = 0; trisets[gid] = new HashSet <int>(); active_groups.Add(gid); add_tri_to_group(et.a, gid); add_tri_to_group(et.b, gid); } foreach (int tid in mesh.TriangleIndices()) { if (mesh.GetTriangleGroup(tid) != 0) { continue; } int gid = find_min_area_group(mesh.TriTrianglesItr(tid)); add_tri_to_group(tid, gid); } IndexPriorityQueue pq = new IndexPriorityQueue(mesh.MaxGroupID); foreach (var pair in areas) { pq.Insert(pair.Key, (float)pair.Value); } while (pq.Count > 0) { int gid = pq.First; pq.Remove(gid); if (areas[gid] > minArea) // ?? { break; } List <int> nbr_groups = find_neighbour_groups(mesh, gid, trisets[gid]); int min_gid = -1; double min_area = double.MaxValue; foreach (int ngid in nbr_groups) { double a = areas[ngid]; if (a < min_area) { min_area = a; min_gid = ngid; } } if (min_gid != -1) { add_group_to_group(gid, min_gid); pq.Remove(min_gid); pq.Insert(min_gid, (float)areas[min_gid]); } } List <Polygon2d> result = new List <Polygon2d>(); int[][] sets = FaceGroupUtil.FindTriangleSetsByGroup(mesh); foreach (var set in sets) { result.Add(make_poly(mesh, set)); } return(result); }