public void build(PrimitiveList primitives)
 {
     this.primitives = primitives;
     int n = primitives.getNumPrimitives();
     UI.printDetailed(UI.Module.ACCEL, "Getting bounding box ...");
     bounds = primitives.getWorldBounds(null);
     objects = new int[n];
     for (int i = 0; i < n; i++)
         objects[i] = i;
     UI.printDetailed(UI.Module.ACCEL, "Creating tree ...");
     int initialSize = 3 * (2 * 6 * n + 1);
     List<int> tempTree = new List<int>((initialSize + 3) / 4);
     BuildStats stats = new BuildStats();
     Timer t = new Timer();
     t.start();
     buildHierarchy(tempTree, objects, stats);
     t.end();
     UI.printDetailed(UI.Module.ACCEL, "Trimming tree ...");
     tree = tempTree.ToArray();
     // display stats
     stats.printStats();
     UI.printDetailed(UI.Module.ACCEL, "  * Creation time:  %s", t);
     UI.printDetailed(UI.Module.ACCEL, "  * Usage of init:  %3d%%", 100 * tree.Length / initialSize);
     UI.printDetailed(UI.Module.ACCEL, "  * Tree memory:    %s", Memory.SizeOf(tree));
     UI.printDetailed(UI.Module.ACCEL, "  * Indices memory: %s", Memory.SizeOf(objects));
 }
        public void build(PrimitiveList primitives)
        {
            this.primitives = primitives;
            int n = primitives.getNumPrimitives();

            UI.printDetailed(UI.Module.ACCEL, "Getting bounding box ...");
            bounds  = primitives.getWorldBounds(null);
            objects = new int[n];
            for (int i = 0; i < n; i++)
            {
                objects[i] = i;
            }
            UI.printDetailed(UI.Module.ACCEL, "Creating tree ...");
            int        initialSize = 3 * (2 * 6 * n + 1);
            List <int> tempTree    = new List <int>((initialSize + 3) / 4);
            BuildStats stats       = new BuildStats();
            Timer      t           = new Timer();

            t.start();
            buildHierarchy(tempTree, objects, stats);
            t.end();
            UI.printDetailed(UI.Module.ACCEL, "Trimming tree ...");
            tree = tempTree.ToArray();
            // display stats
            stats.printStats();
            UI.printDetailed(UI.Module.ACCEL, "  * Creation time:  {0}", t);
            UI.printDetailed(UI.Module.ACCEL, "  * Usage of init:  {0,9:0.00}%", (double)(100 * tree.Length) / initialSize);
            UI.printDetailed(UI.Module.ACCEL, "  * Tree memory:    {0}", Memory.SizeOf(tree));
            UI.printDetailed(UI.Module.ACCEL, "  * Indices memory: {0}", Memory.SizeOf(objects));
        }
        public void build <T>(List <T> primitives, uint leafSize = 3, bool printStats = false) where T : IModel
        {
            if (primitives.Count == 0)
            {
                init_empty();
                return;
            }

            buildData dat;

            dat.maxPrims  = (int)leafSize;
            dat.numPrims  = (uint)primitives.Count;
            dat.indices   = new uint[dat.numPrims];
            dat.primBound = new AxisAlignedBox[dat.numPrims];
            bounds        = primitives[0].getBounds();
            for (int i = 0; i < dat.numPrims; ++i)
            {
                dat.indices[i]   = (uint)i;
                dat.primBound[i] = primitives[i].getBounds();
                bounds.merge(dat.primBound[i]);
            }
            List <uint> tempTree = new List <uint>();
            BuildStats  stats    = new BuildStats();

            buildHierarchy(tempTree, dat, stats);
            if (printStats)
            {
                stats.printStats();
            }

            for (int i = 0; i < dat.numPrims; ++i)
            {
                objects.Add(dat.indices[i]);
            }
            tree = tempTree;
        }
示例#4
0
        public void build(PrimitiveList primitives)
        {
            UI.printDetailed(UI.Module.ACCEL, "KDTree settings");
            UI.printDetailed(UI.Module.ACCEL, "  * Max Leaf Size:  {0}", maxPrims);
            UI.printDetailed(UI.Module.ACCEL, "  * Max Depth:      {0}", MAX_DEPTH);
            UI.printDetailed(UI.Module.ACCEL, "  * Traversal cost: {0}", TRAVERSAL_COST);
            UI.printDetailed(UI.Module.ACCEL, "  * Intersect cost: {0}", INTERSECT_COST);
            UI.printDetailed(UI.Module.ACCEL, "  * Empty bonus:    {0}", EMPTY_BONUS);
            UI.printDetailed(UI.Module.ACCEL, "  * Dump leaves:    {0}", dump ? "enabled" : "disabled");
            Timer total = new Timer();

            total.start();
            this.primitiveList = primitives;
            // get the object space bounds
            bounds = primitives.getWorldBounds(null);
            int       nPrim = primitiveList.getNumPrimitives(), nSplits = 0;
            BuildTask task    = new BuildTask(nPrim);
            Timer     prepare = new Timer();

            prepare.start();
            for (int i = 0; i < nPrim; i++)
            {
                for (int axis = 0; axis < 3; axis++)
                {
                    float ls = primitiveList.getPrimitiveBound(i, 2 * axis + 0);
                    float rs = primitiveList.getPrimitiveBound(i, 2 * axis + 1);
                    if (ls == rs)
                    {
                        // flat in this dimension
                        task.splits[nSplits] = pack(ls, PLANAR, axis, i);
                        nSplits++;
                    }
                    else
                    {
                        task.splits[nSplits + 0] = pack(ls, OPENED, axis, i);
                        task.splits[nSplits + 1] = pack(rs, CLOSED, axis, i);
                        nSplits += 2;
                    }
                }
            }
            task.n = nSplits;
            prepare.end();
            Timer      t        = new Timer();
            List <int> tempTree = new List <int>();
            List <int> tempList = new List <int>();

            tempTree.Add(0);
            tempTree.Add(1);
            t.start();
            // sort it
            Timer sorting = new Timer();

            sorting.start();
            radix12(task.splits, task.n);
            sorting.end();
            // build the actual tree
            BuildStats stats = new BuildStats();

            buildTree(bounds.getMinimum().x, bounds.getMaximum().x, bounds.getMinimum().y, bounds.getMaximum().y, bounds.getMinimum().z, bounds.getMaximum().z, task, 1, tempTree, 0, tempList, stats);
            t.end();
            // write out arrays
            // free some memory
            task            = null;
            tree            = tempTree.ToArray();
            tempTree        = null;
            this.primitives = tempList.ToArray();
            tempList        = null;
            total.end();
            // display some extra info
            stats.printStats();
            UI.printDetailed(UI.Module.ACCEL, "  * Node memory:    {0}", Memory.SizeOf(tree));
            UI.printDetailed(UI.Module.ACCEL, "  * Object memory:  {0}", Memory.SizeOf(this.primitives));
            UI.printDetailed(UI.Module.ACCEL, "  * Prepare time:   {0}", prepare);
            UI.printDetailed(UI.Module.ACCEL, "  * Sorting time:   {0}", sorting);
            UI.printDetailed(UI.Module.ACCEL, "  * Tree creation:  {0}", t);
            UI.printDetailed(UI.Module.ACCEL, "  * Build time:     {0}", total);
            if (dump)
            {
                try
                {
                    UI.printInfo(UI.Module.ACCEL, "Dumping mtls to {0}.mtl ...", dumpPrefix);
                    StreamWriter mtlFile = new StreamWriter(dumpPrefix + ".mtl");
                    int          maxN    = stats.maxObjects;
                    for (int n = 0; n <= maxN; n++)
                    {
                        float blend = (float)n / (float)maxN;
                        Color nc;
                        if (blend < 0.25)
                        {
                            nc = Color.blend(Color.BLUE, Color.GREEN, blend / 0.25f);
                        }
                        else if (blend < 0.5)
                        {
                            nc = Color.blend(Color.GREEN, Color.YELLOW, (blend - 0.25f) / 0.25f);
                        }
                        else if (blend < 0.75)
                        {
                            nc = Color.blend(Color.YELLOW, Color.RED, (blend - 0.50f) / 0.25f);
                        }
                        else
                        {
                            nc = Color.MAGENTA;
                        }
                        mtlFile.WriteLine(string.Format("newmtl mtl{0}", n));
                        float[] rgb = nc.getRGB();
                        mtlFile.WriteLine("Ka 0.1 0.1 0.1");
                        mtlFile.WriteLine(string.Format("Kd {0}g {1}g {2}g", rgb[0], rgb[1], rgb[2]));
                        mtlFile.WriteLine("illum 1\n");
                    }
                    StreamWriter objFile = new StreamWriter(dumpPrefix + ".obj");
                    UI.printInfo(UI.Module.ACCEL, "Dumping tree to {0}.obj ...", dumpPrefix);
                    dumpObj(0, 0, maxN, new BoundingBox(bounds), objFile, mtlFile);
                    objFile.Close();
                    mtlFile.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
示例#5
0
 public void build(PrimitiveList primitives)
 {
     UI.printDetailed(UI.Module.ACCEL, "KDTree settings");
     UI.printDetailed(UI.Module.ACCEL, "  * Max Leaf Size:  {0}", maxPrims);
     UI.printDetailed(UI.Module.ACCEL, "  * Max Depth:      {0}", MAX_DEPTH);
     UI.printDetailed(UI.Module.ACCEL, "  * Traversal cost: {0}", TRAVERSAL_COST);
     UI.printDetailed(UI.Module.ACCEL, "  * Intersect cost: {0}", INTERSECT_COST);
     UI.printDetailed(UI.Module.ACCEL, "  * Empty bonus:    {0}", EMPTY_BONUS);
     UI.printDetailed(UI.Module.ACCEL, "  * Dump leaves:    {0}", dump ? "enabled" : "disabled");
     Timer total = new Timer();
     total.start();
     primitiveList = primitives;
     // get the object space bounds
     bounds = primitives.getWorldBounds(null);
     int nPrim = primitiveList.getNumPrimitives(), nSplits = 0;
     BuildTask task = new BuildTask(nPrim);
     Timer prepare = new Timer();
     prepare.start();
     for (int i = 0; i < nPrim; i++)
     {
         for (int axis = 0; axis < 3; axis++)
         {
             float ls = primitiveList.getPrimitiveBound(i, 2 * axis + 0);
             float rs = primitiveList.getPrimitiveBound(i, 2 * axis + 1);
             if (ls == rs)
             {
                 // flat in this dimension
                 task.splits[nSplits] = pack(ls, PLANAR, axis, i);
                 nSplits++;
             }
             else
             {
                 task.splits[nSplits + 0] = pack(ls, OPENED, axis, i);
                 task.splits[nSplits + 1] = pack(rs, CLOSED, axis, i);
                 nSplits += 2;
             }
         }
     }
     task.n = nSplits;
     prepare.end();
     Timer t = new Timer();
     List<int> tempTree = new List<int>();
     List<int> tempList = new List<int>();
     tempTree.Add(0);
     tempTree.Add(1);
     t.start();
     // sort it
     Timer sorting = new Timer();
     sorting.start();
     radix12(task.splits, task.n);
     sorting.end();
     // build the actual tree
     BuildStats stats = new BuildStats();
     buildTree(bounds.getMinimum().x, bounds.getMaximum().x, bounds.getMinimum().y, bounds.getMaximum().y, bounds.getMinimum().z, bounds.getMaximum().z, task, 1, tempTree, 0, tempList, stats);
     t.end();
     // write out arrays
     // free some memory
     task = null;
     tree = tempTree.ToArray();
     tempTree = null;
     this.primitives = tempList.ToArray();
     tempList = null;
     total.end();
     // display some extra info
     stats.printStats();
     UI.printDetailed(UI.Module.ACCEL, "  * Node memory:    {0}", Memory.SizeOf(tree));
     UI.printDetailed(UI.Module.ACCEL, "  * Object memory:  {0}", Memory.SizeOf(this.primitives));
     UI.printDetailed(UI.Module.ACCEL, "  * Prepare time:   {0}", prepare);
     UI.printDetailed(UI.Module.ACCEL, "  * Sorting time:   {0}", sorting);
     UI.printDetailed(UI.Module.ACCEL, "  * Tree creation:  {0}", t);
     UI.printDetailed(UI.Module.ACCEL, "  * Build time:     {0}", total);
     if (dump)
     {
         try
         {
             UI.printInfo(UI.Module.ACCEL, "Dumping mtls to {0}.mtl ...", dumpPrefix);
             StreamWriter mtlFile = new StreamWriter(dumpPrefix + ".mtl");
             int maxN = stats.maxObjects;
             for (int n = 0; n <= maxN; n++)
             {
                 float blend = (float)n / (float)maxN;
                 Color nc;
                 if (blend < 0.25)
                     nc = Color.blend(Color.BLUE, Color.GREEN, blend / 0.25f);
                 else if (blend < 0.5)
                     nc = Color.blend(Color.GREEN, Color.YELLOW, (blend - 0.25f) / 0.25f);
                 else if (blend < 0.75)
                     nc = Color.blend(Color.YELLOW, Color.RED, (blend - 0.50f) / 0.25f);
                 else
                     nc = Color.MAGENTA;
                 mtlFile.WriteLine(string.Format("newmtl mtl{0}", n));
                 float[] rgb = nc.getRGB();
                 mtlFile.WriteLine("Ka 0.1 0.1 0.1");
                 mtlFile.WriteLine(string.Format("Kd {0}g {1}g {2}g", rgb[0], rgb[1], rgb[2]));
                 mtlFile.WriteLine("illum 1\n");
             }
             StreamWriter objFile = new StreamWriter(dumpPrefix + ".obj");
             UI.printInfo(UI.Module.ACCEL, "Dumping tree to {0}.obj ...", dumpPrefix);
             dumpObj(0, 0, maxN, new BoundingBox(bounds), objFile, mtlFile);
             objFile.Close();
             mtlFile.Close();
         }
         catch (Exception e)
         {
             Console.WriteLine(e);
         }
     }
 }