Beispiel #1
0
 static BVHTree2D()
 {
     PREALLOC = new BVHBuildEntry[128];
     for (int i = 0; i < 128; ++i)
     {
         PREALLOC[i] = new BVHBuildEntry();
     }
 }
Beispiel #2
0
        public void Build()
        {
            mNumNodes = mNumLeafs = 0;
            int  stackptr     = 0;
            uint Untouched    = 0xffffffff;
            uint TouchedTwice = 0xfffffffd;

            PREALLOC[stackptr].mStart  = 0;
            PREALLOC[stackptr].mEnd    = (uint)mBuildPrims.Count;
            PREALLOC[stackptr].mParent = 0xfffffffc;
            stackptr++;
            List <BVHFlatNode2> buildnodes = new List <BVHFlatNode2>(mBuildPrims.Count * 2);

            while (stackptr > 0)
            {
                BVHBuildEntry bnode  = PREALLOC[--stackptr];
                uint          start  = bnode.mStart;
                uint          end    = bnode.mEnd;
                uint          nPrims = end - start;
                mNumNodes++;
                BVHFlatNode2 node = new BVHFlatNode2();
                node.mStartIndex  = start;
                node.mLeafCount   = nPrims;
                node.mRightOffset = Untouched;
                BVHAABB2 bb = new BVHAABB2(mBuildPrims[(int)start].GetAABB().mMin, mBuildPrims[(int)start].GetAABB().mMax);
                BVHAABB2 bc = new BVHAABB2(mBuildPrims[(int)start].GetCenter(), mBuildPrims[(int)start].GetCenter());
                for (uint p = start + 1; p < end; ++p)
                {
                    bb.ExpandToInclude(mBuildPrims[(int)p].GetAABB());
                    bc.ExpandToInclude(mBuildPrims[(int)p].GetCenter());
                }
                node.mBox = bb;
                if (nPrims <= mNodeMaxLeafSize)
                {
                    node.mRightOffset = 0;
                    mNumLeafs++;
                }

                buildnodes.Add(node);
                // 记录父节点关于右孩子结点相对父结点的偏移值mRightOffset
                // 第一次为左孩子,相对父结点的偏移值为1
                // 每个父节点最多被两次 hit
                if (bnode.mParent != 0xfffffffc)
                {
                    buildnodes[(int)bnode.mParent].mRightOffset--;
                    if (buildnodes[(int)bnode.mParent].mRightOffset == TouchedTwice)
                    {
                        buildnodes[(int)bnode.mParent].mRightOffset = (uint)mNumNodes - 1 - bnode.mParent;
                    }
                }
                if (node.mRightOffset == 0)
                {
                    continue;
                }
                // 选择合适的分割维度
                uint  split_dim   = (uint)bc.MaxDimension();
                float split_coord = 0.5f * (bc.mMin[(int)split_dim] + bc.mMax[(int)split_dim]);
                uint  mid         = start;
                // 交换 start 和 end 之间 的数据
                for (uint i = start; i < end; ++i)
                {
                    if (mBuildPrims[(int)i].GetCenter()[(int)split_dim] < split_coord)
                    {
                        BVHObject2 temp = mBuildPrims[(int)i];
                        mBuildPrims[(int)i]   = mBuildPrims[(int)mid];
                        mBuildPrims[(int)mid] = temp;
                        ++mid;
                    }
                }
                if (mid == start || mid == end)
                {
                    mid = start + (end - start) / 2;
                }
                // 右孩子
                PREALLOC[stackptr].mStart  = mid;
                PREALLOC[stackptr].mEnd    = end;
                PREALLOC[stackptr].mParent = (uint)mNumNodes - 1;
                stackptr++;
                // 左孩子
                PREALLOC[stackptr].mStart  = start;
                PREALLOC[stackptr].mEnd    = mid;
                PREALLOC[stackptr].mParent = (uint)mNumNodes - 1;
                stackptr++;
            }
            if (mFlatTreeList != null)
            {
                mFlatTreeList.Clear();
            }
            mFlatTreeList = new List <BVHFlatNode2>(mNumNodes);
            for (uint n = 0; n < mNumNodes; ++n)
            {
                mFlatTreeList.Add(buildnodes[(int)n]);
            }
        }
Beispiel #3
0
        private void Build()
        {
            int stackptr = 0;

            mNumNodes = mNumLeafs = 0;
            uint Untouched    = 0xffffffff;
            uint TouchedTwice = 0xfffffffd;

            PREALLOC[stackptr].Start  = 0;
            PREALLOC[stackptr].End    = (uint)mBuildPrims.Count;
            PREALLOC[stackptr].Parent = 0xfffffffc;
            stackptr++;
            List <BVHFlatNode> buildnodes = new List <BVHFlatNode>(mBuildPrims.Count * 2);

            while (stackptr > 0)
            {
                BVHBuildEntry bnode  = PREALLOC[--stackptr];
                uint          start  = bnode.Start;
                uint          end    = bnode.End;
                uint          nPrims = end - start;
                BVHFlatNode   node   = new BVHFlatNode();
                node.StartIndex  = start;
                node.LeafCount   = nPrims;
                node.RightOffset = Untouched;
                BVHBox bb = new BVHBox(mBuildPrims[(int)start].GetBBox().Min, mBuildPrims[(int)start].GetBBox().Max);
                BVHBox bc = new BVHBox(mBuildPrims[(int)start].GetCentroid());
                // 计算 bnode 的 BBox
                for (uint p = start + 1; p < end; ++p)
                {
                    bb.ExpandToInclude(mBuildPrims[(int)p].GetBBox());
                    bc.ExpandToInclude(mBuildPrims[(int)p].GetCentroid());
                }
                node.Box = bb;
                // 节点数小于叶节点设置的数量,不继续划分,叶节点数量增1
                if (nPrims <= mNodeMaxLeafSize)
                {
                    node.RightOffset = 0;
                    mNumLeafs++;
                }
                // 添加
                mNumNodes++; // 实际上为 buildnodes 的Count
                buildnodes.Add(node);
                // 记录父节点关于右孩子结点相对父结点的偏移值mRightOffset
                // 第一次为左孩子,相对父结点的偏移值为1
                // 每个父节点最多被两次 hit
                // 后面入栈:先右孩子再左孩子
                // 执行出站:先出左孩子再出右孩子
                // 父节点最开始设置为 RightOffset = Untouched = 0xffffffff
                // 执行到 左孩子时 RightOffset -= 1 = 0xfffffffe
                // 执行到 右孩子时 RightOffset -= 1 = 0xfffffffd = TouchedTwice
                if (bnode.Parent != 0xfffffffc)
                {
                    buildnodes[(int)bnode.Parent].RightOffset--;
                    if (buildnodes[(int)bnode.Parent].RightOffset == TouchedTwice)
                    {
                        // 此时处理的是右孩子,记录右孩子的起始索引
                        buildnodes[(int)bnode.Parent].RightOffset = (uint)mNumNodes - 1 - bnode.Parent;
                    }
                }

                // 叶节点
                if (node.RightOffset == 0)
                {
                    continue;
                }
                // 选择合适的分割维度
                uint split_dim = (uint)bc.MaxDimension();
                // 计算分割值
                float split_coord = 0.5f * (bc.Min[(int)split_dim] + bc.Max[(int)split_dim]);
                uint  mid         = start;
                // 交换 start 和 end 之间 的数据
                for (uint i = start; i < end; ++i)
                {
                    // 将 小于 分割值 放 左边
                    if (mBuildPrims[(int)i].GetCentroid()[(int)split_dim] < split_coord)
                    {
                        BVHObject temp = mBuildPrims[(int)i];
                        mBuildPrims[(int)i]   = mBuildPrims[(int)mid];
                        mBuildPrims[(int)mid] = temp;
                        ++mid;
                    }
                }
                // 全部大于 或者 全部小于 则 取中间平分两块
                if (mid == start || mid == end)
                {
                    mid = start + (end - start) / 2;
                }
                // 右孩子
                PREALLOC[stackptr].Start  = mid;
                PREALLOC[stackptr].End    = end;
                PREALLOC[stackptr].Parent = (uint)mNumNodes - 1;
                stackptr++;
                // 左孩子
                PREALLOC[stackptr].Start  = start;
                PREALLOC[stackptr].End    = mid;
                PREALLOC[stackptr].Parent = (uint)mNumNodes - 1;
                stackptr++;
            }
            if (mFlatTreeList != null)
            {
                mFlatTreeList.Clear();
            }
            mFlatTreeList = new List <BVHFlatNode>(mNumNodes);
            for (uint n = 0; n < mNumNodes; ++n)
            {
                mFlatTreeList.Add(buildnodes[(int)n]);
            }
        }