Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT > Class Template Reference

objects that go over 16 sectors are considered huge and better to check against everything. More...

#include <hashtable_3d.h>

Public Member Functions

int hash_int (const double aye)
 Hashes a single value to a value on the collide table truncated to all 3d constraints. Consider using a swizzle. More...
 
void Clear ()
 clears entire table More...
 
int Get (const QVector &Exact, std::vector< T > *retval[])
 returns any objects residing in the sector occupied by Exact More...
 
std::vector< T > & GetHuge ()
 Returns all objects too big to be conveniently fit in the array. More...
 
int Get (const LineCollide *target, std::vector< T > *retval[])
 Returns all objects within sector(s) occupied by target. More...
 
void Put (LineCollide *target, const T objectToPut)
 Adds objectToPut into collide table with limits specified by target. More...
 
bool Eradicate (T objectToKill)
 
bool Remove (const LineCollide *target, T &objectToKill)
 Removes objectToKill from collide table with span of Target. More...
 

Static Public Member Functions

static bool removeFromVector (std::vector< T > &myvector, T &objectToKill)
 

Detailed Description

template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
class Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >

objects that go over 16 sectors are considered huge and better to check against everything.

Hashtable3d is a 3d datastructure that holds various starships that are near enough to crash into each other (or also lights that are big enough to shine on nearby units.

Definition at line 20 of file hashtable_3d.h.

Member Function Documentation

template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
void Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Clear ( )
inline

clears entire table

Definition at line 47 of file hashtable_3d.h.

References COLLIDETABLESIZE, and size.

48  {
49  hugeobjects.clear();
50  for (int i = 0; i <= COLLIDETABLESIZE-1; i++)
51  for (int j = 0; j <= COLLIDETABLESIZE-1; j++)
52  for (int k = 0; k <= COLLIDETABLESIZE-1; k++)
53  if ( table[i][j][k].size() )
54  table[i][j][k].clear();
55  }
template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
bool Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Eradicate ( objectToKill)
inline

Definition at line 161 of file hashtable_3d.h.

References COLLIDETABLESIZE, and Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::removeFromVector().

162  {
163  bool ret = removeFromVector( hugeobjects, objectToKill );
164  for (unsigned int i = 0; i <= COLLIDETABLESIZE-1; i++)
165  for (unsigned int j = 0; j <= COLLIDETABLESIZE-1; j++)
166  for (unsigned int k = 0; k <= COLLIDETABLESIZE-1; k++)
167  ret |= removeFromVector( table[i][j][k], objectToKill );
168  return ret;
169  }
template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
int Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Get ( const QVector Exact,
std::vector< T > *  retval[] 
)
inline

returns any objects residing in the sector occupied by Exact

Definition at line 57 of file hashtable_3d.h.

References Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::hash_int().

58  {
59  retval[1] = &table[hash_int( Exact.i )][hash_int( Exact.j )][hash_int( Exact.k )];
60  //retval+=hugeobjects;
61  //blah = blooh;
62  retval[0] = &hugeobjects;
63  return 2;
64  }
template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
int Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Get ( const LineCollide target,
std::vector< T > *  retval[] 
)
inline

Returns all objects within sector(s) occupied by target.

Definition at line 71 of file hashtable_3d.h.

References COLLIDETABLEACCURACY, Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::hash_int(), LineCollide::hhuge, HUGEOBJECT, LineCollide::Maxi, maxx, maxy, maxz, LineCollide::Mini, x, and y.

72  {
73  unsigned int sizer = 1;
74  //int minx,miny,minz,maxx,maxy,maxz;
75  //hash_vec(Min,minx,miny,minz);
76  //hash_vec(Max,maxx,maxy,maxz);
77  double maxx = ( ceil( target->Maxi.i/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
78  double maxy = ( ceil( target->Maxi.j/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
79  double maxz = ( ceil( target->Maxi.k/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
80  int x, y, z;
81  if (target->Mini.i == maxx) maxx += COLLIDETABLEACCURACY/2;
82  if (target->Mini.j == maxy) maxy += COLLIDETABLEACCURACY/2;
83  if (target->Mini.k == maxz) maxz += COLLIDETABLEACCURACY/2;
84  retval[0] = &hugeobjects;
85  if (target->hhuge)
86  return sizer; //we can't get _everything
87  for (double i = target->Mini.i; i < maxx; i += COLLIDETABLEACCURACY) {
88  x = hash_int( i );
89  for (double j = target->Mini.j; j < maxy; j += COLLIDETABLEACCURACY) {
90  y = hash_int( j );
91  for (double k = target->Mini.k; k < maxz; k += COLLIDETABLEACCURACY) {
92  z = hash_int( k );
93  if ( !table[x][y][z].empty() ) {
94  retval[sizer] = &table[x][y][z];
95  sizer++;
96  if (sizer >= HUGEOBJECT+1)
97  return sizer;
98  }
99  }
100  }
101  }
102  assert( sizer <= HUGEOBJECT+1 ); //make sure we didn't overrun our array
103  return sizer;
104  }
template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
std::vector< T >& Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::GetHuge ( )
inline

Returns all objects too big to be conveniently fit in the array.

Definition at line 66 of file hashtable_3d.h.

67  {
68  return hugeobjects;
69  }
template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
int Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::hash_int ( const double  aye)
inline

Hashes a single value to a value on the collide table truncated to all 3d constraints. Consider using a swizzle.

Definition at line 40 of file hashtable_3d.h.

References COLLIDETABLEACCURACY, and COLLIDETABLESIZE.

Referenced by Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Get(), Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Put(), Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Remove(), and gfx_light::RemoveFromTable().

41  {
42  return ( (int) ( ( (aye < 0) ? (aye
43  -COLLIDETABLEACCURACY) : aye )
45  }
template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
void Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Put ( LineCollide target,
const T  objectToPut 
)
inline

Adds objectToPut into collide table with limits specified by target.

Definition at line 106 of file hashtable_3d.h.

References COLLIDETABLEACCURACY, Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::hash_int(), LineCollide::hhuge, HUGEOBJECT, LineCollide::Maxi, maxx, maxy, maxz, LineCollide::Mini, minx, miny, minz, x, and y.

Referenced by gfx_light::AddToTable().

107  {
108  int x, y, z;
109  double maxx = ( ceil( target->Maxi.i/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
110  double maxy = ( ceil( target->Maxi.j/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
111  double maxz = ( ceil( target->Maxi.k/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
112  //for huge calculation...not sure it's necessary
113  double minx = ( floor( target->Mini.i/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
114  double miny = ( floor( target->Mini.j/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
115  double minz = ( floor( target->Mini.k/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
116  if (target->Mini.i == maxx)
117  maxx += COLLIDETABLEACCURACY/2;
118  if (target->Mini.j == maxy) maxy += COLLIDETABLEACCURACY/2;
119  if (target->Mini.k == maxz) maxz += COLLIDETABLEACCURACY/2;
120  if ( fabs( (maxx
121  -minx)
122  *(maxy
123  -miny)
124  *(maxz
125  -minz) ) > ( (double) COLLIDETABLEACCURACY )*( (double) COLLIDETABLEACCURACY )
126  *( (double) COLLIDETABLEACCURACY )
127  *( (double) HUGEOBJECT ) ) {
128  target->hhuge = true;
129  hugeobjects.push_back( objectToPut );
130  return;
131  } else {
132  target->hhuge = false;
133  }
134  for (double i = target->Mini.i; i < maxx; i += COLLIDETABLEACCURACY) {
135  x = hash_int( i );
136  for (double j = target->Mini.j; j < maxy; j += COLLIDETABLEACCURACY) {
137  y = hash_int( j );
138  for (double k = target->Mini.k; k < maxz; k += COLLIDETABLEACCURACY) {
139  z = hash_int( k );
140  table[x][y][z].push_back( objectToPut );
141  }
142  }
143  }
144  }
template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
bool Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Remove ( const LineCollide target,
T &  objectToKill 
)
inline

Removes objectToKill from collide table with span of Target.

Definition at line 171 of file hashtable_3d.h.

References COLLIDETABLEACCURACY, fprintf, Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::hash_int(), LineCollide::hhuge, LineCollide::Maxi, maxx, maxy, maxz, LineCollide::Mini, Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::removeFromVector(), x, and y.

Referenced by gfx_light::RemoveFromTable().

172  {
173  //int minx,miny,minz,maxx,maxy,maxz;
174  //hash_vec(target->Mini,minx,miny,minz);
175  //hash_vec(target->Maxi,maxx,maxy,maxz);
176  bool ret = false;
177  int x, y, z;
178  double maxx = ( ceil( target->Maxi.i/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
179  double maxy = ( ceil( target->Maxi.j/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
180  double maxz = ( ceil( target->Maxi.k/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
181  if (target->Mini.i == maxx) maxx += COLLIDETABLEACCURACY/2;
182  if (target->Mini.j == maxy) maxy += COLLIDETABLEACCURACY/2;
183  if (target->Mini.k == maxz) maxz += COLLIDETABLEACCURACY/2;
184  if (!target->hhuge)
185  for (double i = target->Mini.i; i < maxx; i += COLLIDETABLEACCURACY) {
186  x = hash_int( i );
187  for (double j = target->Mini.j; j < maxy; j += COLLIDETABLEACCURACY) {
188  y = hash_int( j );
189  for (double k = target->Mini.k; k < maxz; k += COLLIDETABLEACCURACY) {
190  z = hash_int( k );
191  ret |= removeFromVector( table[x][y][z], objectToKill );
192  }
193  }
194  }
195  if (!ret && !target->hhuge)
196  fprintf( stderr, "Nonfatal Collide Error\n" );
197  if (!ret || target->hhuge)
198  ret |= removeFromVector( hugeobjects, objectToKill );
199  return ret;
200  }
template<class T, int COLLIDETABLESIZE, int COLLIDETABLEACCURACY, int HUGEOBJECT>
static bool Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::removeFromVector ( std::vector< T > &  myvector,
T &  objectToKill 
)
inlinestatic

Definition at line 145 of file hashtable_3d.h.

Referenced by Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Eradicate(), and Hashtable3d< T, COLLIDETABLESIZE, COLLIDETABLEACCURACY, HUGEOBJECT >::Remove().

146  {
147  bool ret = false;
148  typename std::vector< T >::iterator removal = myvector.begin();
149  while ( removal != myvector.end() ) {
150  removal = std::find( removal, myvector.end(), objectToKill );
151  if ( removal != myvector.end() ) {
152  ret = true;
153  int offset = removal-myvector.begin();
154  objectToKill = *removal;
155  myvector.erase( removal );
156  removal = myvector.begin()+offset;
157  }
158  }
159  return ret;
160  }

The documentation for this class was generated from the following file: