1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//-------------------------------------------------------------------------
// Filename      : WeightedOctree.hpp
//
// Purpose       : Class for O(ln n) search for nodes within a specified
//                 tolerance of a specified position.
//
//
// Creator       : Corey McBride 
//
// Creation Date : 02/18/10
//-------------------------------------------------------------------------

#ifndef WEIGHTEDOCT_TREE_HPP
#define WEIGHTEDOCT_TREE_HPP

#include "CubitVector.hpp"
#include <vector>
#include <map>


template <class X> class DefaultWeightedNodeQuery
{
  public:
    static inline CubitVector coordinates(const X& x)
    {
      return x.coordinates();
    }
    static inline int weight(const X& x)
    {
      return x.weight();
    }

    template<class C>
    static inline void setLastCell( X& x,C* c)
    {
       x.setLastCell(c);
    }

};
struct DefaultOctreeCell
{
};


template < class X, class C = DefaultOctreeCell, class E = DefaultWeightedNodeQuery<X> >
class WeightedOctree
{
  public:
   class Cell: public C
    {

    public:
      Cell();
       
    
    bool removeNode(const X& node);
    void addNode( const X&  node );
      // asserts if not a leaf node.
      
    bool leaf(); 
      // Is this box a leaf in the tree?

    void appendNodes( std::vector<X>& list );
    const std::vector<X>& nodes() const { return Nodes; }

      // If this node is a leaf node, append all contained CubitNodes


    int nodeCount() { return Nodes.size(); }

    void setWeight(int w);
    void addWeight(int w);
    int weight();

    void setCenter(double& x,double& y,double& z);
    void setCenter(CubitVector& center);
    CubitVector& center() {return Center;}

    void removeAllNodes();



      enum {
        X_MIN = 0,
        Y_MIN = 0,
        Z_MIN = 0,
        X_MAX = 1,
        Y_MAX = 2,
        Z_MAX = 4
      };
      // Constants for selecting one of 8 child boxes.
      // Can be either bitwise-or'ed (|) or added (+).
      // MAX values take precedence over MIN values 
      // if both are specified.  (MIN is just the
      // absense of MAX).
      //
      Cell* child( int quadrant )
      {
        assert( quadrant >= 0 && quadrant < 8);
        return this->Children[quadrant];
      }
      // quadrant should be a bitwise OR (or sum) of 
      // the constants above.  If both a MIN and MAX 
      // value is specified for a given principal 
      // direction, the M  : children_(0),

      void setChild(int quadrant,Cell* c);

      void setParent(Cell*);
      Cell* parent() {return this->Parent;}

      void appendAllNodes( std::vector<X>& list );
      // If this node is a leaf node, append all contained CubitNodes.
      // Otherwise descend oct-tree getting CubitNodes contained in
      // all child leaf nodes.


    protected:
      Cell* Parent;
      Cell* Children[8]; 

      std::vector<X> Nodes;
      int TotalWeight;
      CubitVector Center;
      bool IsLeaf;

    };


   
  public:

    WeightedOctree(int max_weight_per_box,double tolerance=1e-6);
      //- Constructor
      //- tolerance - tolerance for comparison of node positions
      //- max_weight_per_box - subdivide box if total weight exceeds this 
      //- min_box_dimension - don't subdivide boxes in tree if
      //-                     all three dimensions are less than this

    ~WeightedOctree();
      // - Destructor
    
    bool initilizeTree(std::vector<X>& nodes,std::vector<Cell*>& newCells);
    void clear();

    bool addNode(const X& node,
      std::vector<Cell*>& oldCells,
      std::vector<Cell*>& newCells,
      std::vector<Cell*>& modifiedCells);
    bool removeNode(X& node,
      std::vector<Cell*>& oldCells,
      std::vector<Cell*>& newCells,
      std::vector<Cell*>& modifiedCells);



  protected:
    bool calculateCellCenter(Cell* cell); 

    void mergeCellChildren(Cell* cell,std::vector<Cell*>& oldLeafs);

    bool splitCell(Cell* cell,std::vector<Cell*>& newLeafs);
      // Change from leaf cell to non-leaf.  Splits nodes amongst 8 new
      // cell boxes using the value of centroid of the cell
      

     bool rebalanceBranch(Cell* cell,
         std::vector<Cell*>& oldLeafs,
         std::vector<Cell*>& newLeafs);

     bool recursiveSplitCell(Cell* cell,
         std::vector<Cell*>& newLeafs);



    int MaxBoxWeight;
      // values passed to constructor
  
    Cell* Root;
      // root of oct-tree

    int TotalWeight;
    
    std::map<X,Cell*> NodeToCellMap;

    double Tolerance;
    double ToleranceSquared;
  

 

};

#include "WeightedOctree.cpp"

#endif