00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Michiel Hagedoorn 00008 * Side Effects Software Inc 00009 * 123 Front Street West, Suite 1401 00010 * Toronto, Ontario 00011 * Canada M5J 2M2 00012 * 416-504-9876 00013 * 00014 * NAME: UT_RTreeGeneric.h (UT Library, C++) 00015 * 00016 * COMMENTS: 00017 */ 00018 00019 #ifndef __UT_RTreeGeneric__ 00020 #define __UT_RTreeGeneric__ 00021 00022 #include <SYS/SYS_Types.h> 00023 #include <vector> 00024 #include <limits> 00025 #include "UT_API.h" 00026 #include "UT_Math.h" 00027 00028 // UT_BoxT represents either an axis-aligned box or the empty set. 00029 // UT_BoxT is closed as a set; all operations assume that 00030 // the boundary is included. 00031 template< typename T > 00032 class UT_BoxT 00033 { 00034 public: 00035 typedef T Scalar; 00036 00037 // Default construct: the empty set 00038 UT_BoxT(); 00039 00040 // Construct box containing a single point position, 00041 // which is represented by its array of three coordinates. 00042 explicit UT_BoxT(const T p[3]); 00043 00044 // Return whether this represents the empty set 00045 bool isEmpty() const; 00046 00047 // Get minimum coordinate for given standard axis 00048 // Assumes this is not empty! 00049 T getMin(const int c) const; 00050 00051 // Get maximum coordinate for given standard axis 00052 // Assumes this is not empty! 00053 T getMax(const int c) const; 00054 00055 // Make a box that's a single point 00056 void assignPoint(const T*const p); 00057 00058 // Expand the box just enough so that it contains a point p 00059 void absorbPoint(const T*const p); 00060 00061 // Expand the box just enough so that it contains a box b 00062 void absorbBox(const UT_BoxT<T>& b); 00063 00064 // Expand in the directions of the axes: 00065 // In case this is the empty set, the result is still the empty set 00066 // This is equivalent to making *this the union of all cubes with 00067 // radius l placed at points of the original set. 00068 void expandDistance(const T l); 00069 00070 // Return whether a point is contained in the closed set 00071 bool contains(const T*const p) const; 00072 00073 // Return whether the closed set *this intersects the closed set b 00074 bool intersects(const UT_BoxT<T>& b) const; 00075 00076 // Return the axis in which the box is the widest. 00077 // For the empty set, this always returns axis 0 00078 int getLargestAxis() const; 00079 00080 private: 00081 // This represents set of all points x 00082 // with myMin[c] <= x[c] <= myMax[c] for each c = 0, 1, 2 00083 T myMin[3]; 00084 T myMax[3]; 00085 }; 00086 00087 template< int ORDER > 00088 class UT_RTreeGeneric; 00089 00090 // This is an assigment of a box to each item stored in the R-tree. 00091 // It is passed an argument to queries on the R-tree. 00092 // This way, an R-tree can be re-used efficiently if the item boxes change. 00093 // This will only remain efficient as long as the configuration of boxes 00094 // doesn't change dramatically. 00095 template< typename T > 00096 class UT_RTreeBoxAssignmentT 00097 { 00098 public: 00099 UT_RTreeBoxAssignmentT() {} 00100 ~UT_RTreeBoxAssignmentT() {} 00101 00102 private: 00103 typedef UT_BoxT<T> Box; 00104 std::vector<Box> myBoxesForSharedNodes; 00105 00106 template< int ORDER > 00107 friend class UT_RTreeGeneric; 00108 }; 00109 00110 template< int ORDER > 00111 class UT_RNode; 00112 00113 // The order of the R-Tree is the number of children that each 00114 // internal node in the tree has. Picking a higher value of 00115 // ORDER will reduce the height of the R-tree. 00116 // R-tree stores a containment structure for the boxes that are 00117 // passed to its contructor, but doesn't store any actual boxes. 00118 // That's why UT_RTreeGeneric doesn't take the number type T as a template parameter. 00119 template< int ORDER > 00120 class UT_RTreeGeneric 00121 { 00122 public: 00123 // Construct R-tree that contains the range of items [0, size), 00124 // During the construction, item_boxes[i] is used as the bounding box 00125 // for item #i. These boxes determine the initial structure of the R-tree. 00126 // However, the box positions and dimensions are not stored in the R-tree. 00127 template< typename T > 00128 UT_RTreeGeneric(const UT_BoxT<T> item_boxes[], const int size); 00129 00130 ~UT_RTreeGeneric(); 00131 00132 // The number of items stored in the tree 00133 int getNumItems() const; 00134 00135 // Create an assignment of a box to each item in the tree. 00136 // That is, item i is assigned box item_boxes[i]. 00137 // This box assignment can be used in intersection queries. 00138 template< typename T > 00139 void createBoxAssignment( 00140 UT_RTreeBoxAssignmentT<T>& assignment, 00141 const UT_BoxT<T> item_boxes[] 00142 ) const; 00143 00144 // Return all items i for which item_boxes[i] intersects query_box. 00145 // The resulting items will be stored in the range [items_begin, items_end), 00146 // where items_end is the return value. 00147 // It is assumed that the array starting at items_begin is large enough 00148 // to contain the results (at most the total number of items). 00149 template< typename T > 00150 int* getIntersectingItems( 00151 const UT_BoxT<T>& box, 00152 const UT_RTreeBoxAssignmentT<T>& assignment, 00153 int*const items_begin 00154 ) const; 00155 00156 private: 00157 typedef UT_RNode<ORDER> Node; 00158 00159 int mySharedNodesCapacity; 00160 Node* mySharedNodes; 00161 Node* myRoot; 00162 int myNumItems; 00163 00164 // Disallow 00165 UT_RTreeGeneric(); 00166 UT_RTreeGeneric(const UT_RTreeGeneric&); 00167 UT_RTreeGeneric& operator=(const UT_RTreeGeneric&); 00168 }; 00169 00170 #if defined( WIN32 ) || defined( LINUX ) || defined( MBSD ) || defined(GAMEOS) 00171 #include "UT_RTree.C" 00172 #endif 00173 00174 // R-tree boxes for various float types 00175 typedef UT_BoxT<fpreal32> UT_BoxF; 00176 typedef UT_BoxT<fpreal64> UT_BoxD; 00177 typedef UT_BoxT<fpreal64> UT_Box; 00178 00179 // R-tree box assignments for various float types 00180 typedef UT_RTreeBoxAssignmentT<fpreal32> UT_RTreeBoxAssignmentF; 00181 typedef UT_RTreeBoxAssignmentT<fpreal64> UT_RTreeBoxAssignmentD; 00182 typedef UT_RTreeBoxAssignmentT<fpreal64> UT_RTreeBoxAssignment; 00183 00184 // Tree of various orders 00185 typedef UT_RTreeGeneric<2> UT_RTree2; 00186 typedef UT_RTreeGeneric<16> UT_RTree16; 00187 typedef UT_RTreeGeneric<2> UT_RTree; 00188 00189 #endif 00190
1.5.9