Ejemplo n.º 1
0
        /// <summary>
        /// collect triangles by group id. Returns array of triangle lists (stored as arrays).
        /// This requires 2 passes over mesh, but each pass is linear
        /// </summary>
        public static int[][] FindTriangleSetsByGroup(DMesh3 mesh, int ignoreGID = int.MinValue)
        {
            if (!mesh.HasTriangleGroups)
            {
                return(new int[0][]);
            }

            // find # of groups and triangle count for each
            SparseList <int> counts   = CountAllGroups(mesh);
            List <int>       GroupIDs = new List <int>();

            foreach (var idxval in counts.Values())
            {
                if (idxval.Key != ignoreGID && idxval.Value > 0)
                {
                    GroupIDs.Add(idxval.Key);
                }
            }
            GroupIDs.Sort();        // might as well sort ascending...
            SparseList <int> groupMap = new SparseList <int>(mesh.MaxGroupID, GroupIDs.Count, -1);

            // allocate sets
            int[][] sets     = new int[GroupIDs.Count][];
            int[]   counters = new int[GroupIDs.Count];
            for (int i = 0; i < GroupIDs.Count; ++i)
            {
                int gid = GroupIDs[i];
                sets[i]       = new int[counts[gid]];
                counters[i]   = 0;
                groupMap[gid] = i;
            }

            // accumulate triangles
            int NT = mesh.MaxTriangleID;

            for (int tid = 0; tid < NT; ++tid)
            {
                if (mesh.IsTriangle(tid))
                {
                    int gid = mesh.GetTriangleGroup(tid);
                    int i   = groupMap[gid];
                    if (i >= 0)
                    {
                        int k = counters[i]++;
                        sets[i][k] = tid;
                    }
                }
            }

            return(sets);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// count number of tris in each group in mesh
        /// returned pairs are [group_id, tri_count]
        /// </summary>
        public static SparseList <int> CountAllGroups(DMesh3 mesh)
        {
            SparseList <int> GroupCounts = new SparseList <int>(mesh.MaxGroupID, 0, 0);

            if (mesh.HasTriangleGroups)
            {
                int NT = mesh.MaxTriangleID;
                for (int tid = 0; tid < NT; ++tid)
                {
                    if (mesh.IsTriangle(tid))
                    {
                        int gid = mesh.GetTriangleGroup(tid);
                        GroupCounts[gid] = GroupCounts[gid] + 1;
                    }
                }
            }
            return(GroupCounts);
        }