sptree.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. *
  3. * Copyright (c) 2014, Laurens van der Maaten (Delft University of Technology)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the Delft University of Technology.
  16. * 4. Neither the name of the Delft University of Technology nor the names of
  17. * its contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY LAURENS VAN DER MAATEN ''AS IS'' AND ANY EXPRESS
  21. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  23. * EVENT SHALL LAURENS VAN DER MAATEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  26. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. */
  32. #ifndef SPTREE_H
  33. #define SPTREE_H
  34. using namespace std;
  35. class Cell {
  36. unsigned int dimension;
  37. double* corner;
  38. double* width;
  39. public:
  40. Cell(unsigned int inp_dimension);
  41. Cell(unsigned int inp_dimension, double* inp_corner, double* inp_width);
  42. ~Cell();
  43. double getCorner(unsigned int d);
  44. double getWidth(unsigned int d);
  45. void setCorner(unsigned int d, double val);
  46. void setWidth(unsigned int d, double val);
  47. bool containsPoint(double point[]);
  48. };
  49. class SPTree
  50. {
  51. // Fixed constants
  52. static const unsigned int QT_NODE_CAPACITY = 1;
  53. // A buffer we use when doing force computations
  54. double* buff;
  55. // Properties of this node in the tree
  56. SPTree* parent;
  57. unsigned int dimension;
  58. bool is_leaf;
  59. unsigned int size;
  60. unsigned int cum_size;
  61. // Axis-aligned bounding box stored as a center with half-dimensions to represent the boundaries of this quad tree
  62. Cell* boundary;
  63. // Indices in this space-partitioning tree node, corresponding center-of-mass, and list of all children
  64. double* data;
  65. double* center_of_mass;
  66. unsigned int index[QT_NODE_CAPACITY];
  67. // Children
  68. SPTree** children;
  69. unsigned int no_children;
  70. public:
  71. SPTree(unsigned int D, double* inp_data, unsigned int N);
  72. SPTree(unsigned int D, double* inp_data, double* inp_corner, double* inp_width);
  73. SPTree(unsigned int D, double* inp_data, unsigned int N, double* inp_corner, double* inp_width);
  74. SPTree(SPTree* inp_parent, unsigned int D, double* inp_data, unsigned int N, double* inp_corner, double* inp_width);
  75. SPTree(SPTree* inp_parent, unsigned int D, double* inp_data, double* inp_corner, double* inp_width);
  76. ~SPTree();
  77. void setData(double* inp_data);
  78. SPTree* getParent();
  79. void construct(Cell boundary);
  80. bool insert(unsigned int new_index);
  81. void subdivide();
  82. bool isCorrect();
  83. void rebuildTree();
  84. void getAllIndices(unsigned int* indices);
  85. unsigned int getDepth();
  86. void computeNonEdgeForces(unsigned int point_index, double theta, double neg_f[], double* sum_Q);
  87. void computeEdgeForces(unsigned int* row_P, unsigned int* col_P, double* val_P, int N, double* pos_f);
  88. void print();
  89. private:
  90. void init(SPTree* inp_parent, unsigned int D, double* inp_data, double* inp_corner, double* inp_width);
  91. void fill(unsigned int N);
  92. unsigned int getAllIndices(unsigned int* indices, unsigned int loc);
  93. bool isChild(unsigned int test_index, unsigned int start, unsigned int end);
  94. };
  95. #endif