Octree.cpp
// Drills down until the desired resolution is met in order to add or delete the cube to the octree.
Drills down until the desired resolution is met in order to add or delete the cube to the octree.
Drills down until the desired resolution is met in order to add or delete the cube to the octree .
void Octree::RecursiveAddDeleteCube(double xL, double yL, double zL, double xH, double yH, double zH, double x, double y, double z, OctreeNode *pCurrentNode, bool isAdd)
{
    if (AtResolution(xL, yL, zL, xH, yH, zH))
    {
        pCurrentNode->nodeData.isFilled = isAdd;
        pCurrentNode->nodeData.hasConsensus = true;
        return;
    }
    else if (pCurrentNode->nodeData.isFilled == isAdd && pCurrentNode->nodeData.hasConsensus)
    {
        return;
    }
    CubeOctant octant = GetOctant(xL, yL, zL, xH, yH, zH, x, y, z);
    if (pCurrentNode->miniCubes[octant] == nullptr)
    {
        pCurrentNode->miniCubes[octant] = new OctreeNode(pCurrentNode->nodeData.isFilled);
        pCurrentNode->nodeData.hasConsensus = false;
    }
    RecursiveAddDeleteCube(LowerX(xL, xH, octant), LowerY(yL, yH, octant), LowerZ(zL, zH, octant), UpperX(xL, xH, octant), UpperY(yL, yH, octant), UpperZ(zL, zH, octant), 
        x, y, z, pCurrentNode->miniCubes[octant], isAdd);
    if (!pCurrentNode->nodeData.hasConsensus)
    {
        bool allHaveConsensus = true;
        bool foundClear = false;
        bool foundFilled = false;
        for (int i = 0; i < OCTANT_COUNT; i++)
        {
            if (pCurrentNode->miniCubes[i] == nullptr || !pCurrentNode->miniCubes[i]->nodeData.hasConsensus)
            {
                allHaveConsensus = false;
                break;
            }
            if (pCurrentNode->miniCubes[i]->nodeData.isFilled)
            {
                foundFilled = true;
            }
            else
            {
                foundClear = true;
            }
        }
        if (allHaveConsensus && !(foundClear && foundFilled))
        {
            pCurrentNode->nodeData.hasConsensus = true;
            pCurrentNode->nodeData.isFilled = pCurrentNode->miniCubes[0]->nodeData.isFilled;
            for (int i = 0; i < OCTANT_COUNT; i++)
            {
                delete pCurrentNode->miniCubes[i];
                pCurrentNode->miniCubes[i] = nullptr;
            }
        }
    }
}
void Octree :: RecursiveAddDeleteCube ( double xL , double yL , double zL , double xH , double yH , double zH , double x , double y , double z , OctreeNode * pCurrentNode , bool isAdd ) { if ( AtResolution ( xL , yL , zL , xH , yH , zH ) ) { pCurrentNode -> nodeData . isFilled = isAdd ; pCurrentNode -> nodeData . hasConsensus = true ; return ; } else if ( pCurrentNode -> nodeData . isFilled == isAdd && pCurrentNode -> nodeData . hasConsensus ) { return ; } CubeOctant octant = GetOctant ( xL , yL , zL , xH , yH , zH , x , y , z ) ; if ( pCurrentNode -> miniCubes [ octant ] == nullptr ) { pCurrentNode -> miniCubes [ octant ] = new OctreeNode ( pCurrentNode -> nodeData . isFilled ) ; pCurrentNode -> nodeData . hasConsensus = false ; } RecursiveAddDeleteCube ( LowerX ( xL , xH , octant ) , LowerY ( yL , yH , octant ) , LowerZ ( zL , zH , octant ) , UpperX ( xL , xH , octant ) , UpperY ( yL , yH , octant ) , UpperZ ( zL , zH , octant ) , x , y , z , pCurrentNode -> miniCubes [ octant ] , isAdd ) ; if ( ! pCurrentNode -> nodeData . hasConsensus ) { bool allHaveConsensus = true ; bool foundClear = false ; bool foundFilled = false ; for ( int i = 0 ; i < OCTANT_COUNT ; i ++ ) { if ( pCurrentNode -> miniCubes [ i ] == nullptr || ! pCurrentNode -> miniCubes [ i ] -> nodeData . hasConsensus ) { allHaveConsensus = false ; break ; } if ( pCurrentNode -> miniCubes [ i ] -> nodeData . isFilled ) { foundFilled = true ; } else { foundClear = true ; } } if ( allHaveConsensus && ! ( foundClear && foundFilled ) ) { pCurrentNode -> nodeData . hasConsensus = true ; pCurrentNode -> nodeData . isFilled = pCurrentNode -> miniCubes [ 0 ] -> nodeData . isFilled ; for ( int i = 0 ; i < OCTANT_COUNT ; i ++ ) { delete pCurrentNode -> miniCubes [ i ] ; pCurrentNode -> miniCubes [ i ] = nullptr ; } } } }
Drills down until the desired resolution is met in order to add or delete the cube to the octree.
Drills down until the desired resolution is met in order to add or delete the cube to the octree .
// At the level we can save this cube. Note that there can be no smaller cubes than this one. // The supernode is the same as the individual one, so there's no point in repartitioning. // This location no longer has consensus, by virtue of us adding this node // Check to see if we can form consensus (and clean up the tree) // If they all have consensus and are the same, delete the subnodes and combine.