VectorSet.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __VECTORSET_H__
  21. #define __VECTORSET_H__
  22. /*
  23. ===============================================================================
  24. Vector Set
  25. Creates a set of vectors without duplicates.
  26. ===============================================================================
  27. */
  28. template< class type, int dimension >
  29. class idVectorSet : public idList<type> {
  30. public:
  31. idVectorSet( void );
  32. idVectorSet( const type &mins, const type &maxs, const int boxHashSize, const int initialSize );
  33. // returns total size of allocated memory
  34. size_t Allocated( void ) const { return idList<type>::Allocated() + hash.Allocated(); }
  35. // returns total size of allocated memory including size of type
  36. size_t Size( void ) const { return sizeof( *this ) + Allocated(); }
  37. void Init( const type &mins, const type &maxs, const int boxHashSize, const int initialSize );
  38. void ResizeIndex( const int newSize );
  39. void Clear( void );
  40. int FindVector( const type &v, const float epsilon );
  41. private:
  42. idHashIndex hash;
  43. type mins;
  44. type maxs;
  45. int boxHashSize;
  46. float boxInvSize[dimension];
  47. float boxHalfSize[dimension];
  48. };
  49. template< class type, int dimension >
  50. ID_INLINE idVectorSet<type,dimension>::idVectorSet( void ) {
  51. hash.Clear( idMath::IPow( boxHashSize, dimension ), 128 );
  52. boxHashSize = 16;
  53. memset( boxInvSize, 0, dimension * sizeof( boxInvSize[0] ) );
  54. memset( boxHalfSize, 0, dimension * sizeof( boxHalfSize[0] ) );
  55. }
  56. template< class type, int dimension >
  57. ID_INLINE idVectorSet<type,dimension>::idVectorSet( const type &mins, const type &maxs, const int boxHashSize, const int initialSize ) {
  58. Init( mins, maxs, boxHashSize, initialSize );
  59. }
  60. template< class type, int dimension >
  61. ID_INLINE void idVectorSet<type,dimension>::Init( const type &mins, const type &maxs, const int boxHashSize, const int initialSize ) {
  62. int i;
  63. float boxSize;
  64. idList<type>::AssureSize( initialSize );
  65. idList<type>::SetNum( 0, false );
  66. hash.Clear( idMath::IPow( boxHashSize, dimension ), initialSize );
  67. this->mins = mins;
  68. this->maxs = maxs;
  69. this->boxHashSize = boxHashSize;
  70. for ( i = 0; i < dimension; i++ ) {
  71. boxSize = ( maxs[i] - mins[i] ) / (float) boxHashSize;
  72. boxInvSize[i] = 1.0f / boxSize;
  73. boxHalfSize[i] = boxSize * 0.5f;
  74. }
  75. }
  76. template< class type, int dimension >
  77. ID_INLINE void idVectorSet<type,dimension>::ResizeIndex( const int newSize ) {
  78. idList<type>::Resize( newSize );
  79. hash.ResizeIndex( newSize );
  80. }
  81. template< class type, int dimension >
  82. ID_INLINE void idVectorSet<type,dimension>::Clear( void ) {
  83. idList<type>::Clear();
  84. hash.Clear();
  85. }
  86. template< class type, int dimension >
  87. ID_INLINE int idVectorSet<type,dimension>::FindVector( const type &v, const float epsilon ) {
  88. int i, j, k, hashKey, partialHashKey[dimension];
  89. for ( i = 0; i < dimension; i++ ) {
  90. assert( epsilon <= boxHalfSize[i] );
  91. partialHashKey[i] = (int) ( ( v[i] - mins[i] - boxHalfSize[i] ) * boxInvSize[i] );
  92. }
  93. for ( i = 0; i < ( 1 << dimension ); i++ ) {
  94. hashKey = 0;
  95. for ( j = 0; j < dimension; j++ ) {
  96. hashKey *= boxHashSize;
  97. hashKey += partialHashKey[j] + ( ( i >> j ) & 1 );
  98. }
  99. for ( j = hash.First( hashKey ); j >= 0; j = hash.Next( j ) ) {
  100. const type &lv = (*this)[j];
  101. for ( k = 0; k < dimension; k++ ) {
  102. if ( idMath::Fabs( lv[k] - v[k] ) > epsilon ) {
  103. break;
  104. }
  105. }
  106. if ( k >= dimension ) {
  107. return j;
  108. }
  109. }
  110. }
  111. hashKey = 0;
  112. for ( i = 0; i < dimension; i++ ) {
  113. hashKey *= boxHashSize;
  114. hashKey += (int) ( ( v[i] - mins[i] ) * boxInvSize[i] );
  115. }
  116. hash.Add( hashKey, idList<type>::Num() );
  117. Append( v );
  118. return idList<type>::Num()-1;
  119. }
  120. /*
  121. ===============================================================================
  122. Vector Subset
  123. Creates a subset without duplicates from an existing list with vectors.
  124. ===============================================================================
  125. */
  126. template< class type, int dimension >
  127. class idVectorSubset {
  128. public:
  129. idVectorSubset( void );
  130. idVectorSubset( const type &mins, const type &maxs, const int boxHashSize, const int initialSize );
  131. // returns total size of allocated memory
  132. size_t Allocated( void ) const { return idList<type>::Allocated() + hash.Allocated(); }
  133. // returns total size of allocated memory including size of type
  134. size_t Size( void ) const { return sizeof( *this ) + Allocated(); }
  135. void Init( const type &mins, const type &maxs, const int boxHashSize, const int initialSize );
  136. void Clear( void );
  137. // returns either vectorNum or an index to a previously found vector
  138. int FindVector( const type *vectorList, const int vectorNum, const float epsilon );
  139. private:
  140. idHashIndex hash;
  141. type mins;
  142. type maxs;
  143. int boxHashSize;
  144. float boxInvSize[dimension];
  145. float boxHalfSize[dimension];
  146. };
  147. template< class type, int dimension >
  148. ID_INLINE idVectorSubset<type,dimension>::idVectorSubset( void ) {
  149. hash.Clear( idMath::IPow( boxHashSize, dimension ), 128 );
  150. boxHashSize = 16;
  151. memset( boxInvSize, 0, dimension * sizeof( boxInvSize[0] ) );
  152. memset( boxHalfSize, 0, dimension * sizeof( boxHalfSize[0] ) );
  153. }
  154. template< class type, int dimension >
  155. ID_INLINE idVectorSubset<type,dimension>::idVectorSubset( const type &mins, const type &maxs, const int boxHashSize, const int initialSize ) {
  156. Init( mins, maxs, boxHashSize, initialSize );
  157. }
  158. template< class type, int dimension >
  159. ID_INLINE void idVectorSubset<type,dimension>::Init( const type &mins, const type &maxs, const int boxHashSize, const int initialSize ) {
  160. int i;
  161. float boxSize;
  162. hash.Clear( idMath::IPow( boxHashSize, dimension ), initialSize );
  163. this->mins = mins;
  164. this->maxs = maxs;
  165. this->boxHashSize = boxHashSize;
  166. for ( i = 0; i < dimension; i++ ) {
  167. boxSize = ( maxs[i] - mins[i] ) / (float) boxHashSize;
  168. boxInvSize[i] = 1.0f / boxSize;
  169. boxHalfSize[i] = boxSize * 0.5f;
  170. }
  171. }
  172. template< class type, int dimension >
  173. ID_INLINE void idVectorSubset<type,dimension>::Clear( void ) {
  174. idList<type>::Clear();
  175. hash.Clear();
  176. }
  177. template< class type, int dimension >
  178. ID_INLINE int idVectorSubset<type,dimension>::FindVector( const type *vectorList, const int vectorNum, const float epsilon ) {
  179. int i, j, k, hashKey, partialHashKey[dimension];
  180. const type &v = vectorList[vectorNum];
  181. for ( i = 0; i < dimension; i++ ) {
  182. assert( epsilon <= boxHalfSize[i] );
  183. partialHashKey[i] = (int) ( ( v[i] - mins[i] - boxHalfSize[i] ) * boxInvSize[i] );
  184. }
  185. for ( i = 0; i < ( 1 << dimension ); i++ ) {
  186. hashKey = 0;
  187. for ( j = 0; j < dimension; j++ ) {
  188. hashKey *= boxHashSize;
  189. hashKey += partialHashKey[j] + ( ( i >> j ) & 1 );
  190. }
  191. for ( j = hash.First( hashKey ); j >= 0; j = hash.Next( j ) ) {
  192. const type &lv = vectorList[j];
  193. for ( k = 0; k < dimension; k++ ) {
  194. if ( idMath::Fabs( lv[k] - v[k] ) > epsilon ) {
  195. break;
  196. }
  197. }
  198. if ( k >= dimension ) {
  199. return j;
  200. }
  201. }
  202. }
  203. hashKey = 0;
  204. for ( i = 0; i < dimension; i++ ) {
  205. hashKey *= boxHashSize;
  206. hashKey += (int) ( ( v[i] - mins[i] ) * boxInvSize[i] );
  207. }
  208. hash.Add( hashKey, vectorNum );
  209. return vectorNum;
  210. }
  211. #endif /* !__VECTORSET_H__ */