示例#1
0
        public List <STLReader> RecursiveSplit(int MaxTriCount)
        {
            // this algorithm subdivides the stl part into a list of small pieces
            // This is 3d quadtree like in that it keeps on dividing along the dimension of greatest length
            // eg if the part is bounded by 12in x 5in x 16in box
            // the new boxes are 12 x 5 x 8 in size
            // and if needs be, then further subdivided to 6 x 5 x 8 then to 6 x 5 x 4 and so on...
            //MaxTriangleCount = MaxTriCount;
            var MasterList = new List <STLReader>();

            if (TriangleCount < MaxTriCount)
            {
                MasterList.Add(this);
                return(MasterList);
            }

            var Lengths = new double[3];

            Lengths[0] = Extrema.Max.x - Extrema.Min.x;
            Lengths[1] = Extrema.Max.y - Extrema.Min.y;
            Lengths[2] = Extrema.Max.z - Extrema.Min.z;
            int    MaxDim        = 0;
            double MaxLength     = 0;
            double SliceLocation = 0;
            double StartPt       = 0;

            for (int i = 0; i < 3; i++)
            {
                if (Lengths[i] > MaxLength)
                {
                    MaxLength = Lengths[i];
                    MaxDim    = i;
                }
            }
            if (MaxDim == 0)
            {
                SliceLocation = Extrema.Min.x + MaxLength / 2;
            }
            else if (MaxDim == 1)
            {
                SliceLocation = Extrema.Min.y + MaxLength / 2;
            }
            else
            {
                SliceLocation = Extrema.Min.z + MaxLength / 2;
            }

            var       Slice = new Plane(SliceLocation, MaxDim);
            STLReader Part1;
            STLReader Part2;

            SplitPart(Slice, out Part1, out Part2);
            Part1.WriteToFile("Part1_" + Part1.TriangleCount.ToString() + ".stl");
            Part2.WriteToFile("Part2_" + Part2.TriangleCount.ToString() + ".stl");

            /*
             * // Multithreaded implementation
             * var Obj1 = new SubDividerObj();
             * Obj1.Part = Part1;
             * Obj1.MaxTriCount = MaxTriCount;
             * var Obj2 = new SubDividerObj();
             * Obj2.Part = Part2;
             * Obj2.MaxTriCount = MaxTriCount;
             * var Thrd1 = new System.Threading.Thread(SubDividerObj.DoWork);
             * var Thrd2 = new System.Threading.Thread(SubDividerObj.DoWork);
             * Thrd1.Start(Obj1);
             * Thrd2.Start(Obj2);
             * Thrd1.Join();
             * Thrd2.Join();
             * MasterList.AddRange(Obj1.Output);
             * MasterList.AddRange(Obj2.Output);
             */
            //
            // Single Threaded Implementation
            MasterList.AddRange(Part1.RecursiveSplit(MaxTriCount));
            MasterList.AddRange(Part2.RecursiveSplit(MaxTriCount));
            return(MasterList);
        }