The spica renderer
hash_grid_detail.h
1 #ifndef _SPICA_HASH_GRID_DETAIL_H_
2 #define _SPICA_HASH_GRID_DETAIL_H_
3 
4 #include <algorithm>
5 
6 namespace spica {
7 
8  template <class T>
9  HashGrid<T>::HashGrid()
10  : _hashSize(-1)
11  , _bbox()
12  , _hashScale(0.0)
13  , _data()
14  {
15  }
16 
17  template <class T>
18  HashGrid<T>::~HashGrid()
19  {
20  }
21 
22  template <class T>
23  void HashGrid<T>::construct(std::vector<T>& points, const int imageW, const int imageH) {
24 
25  }
26 
27  template <class T>
28  void HashGrid<T>::init(const int hashSize, const double hashScale, const Bounds3d& bbox) {
29  this->_hashSize = hashSize;
30  this->_hashScale = hashScale;
31  this->_bbox = bbox;
32  this->_data.resize(hashSize);
33  }
34 
35  template <class T>
36  void HashGrid<T>::add(const T& p, const Point3d& boxMin, const Point3d& boxMax) {
37  const Vector3d bMin = (boxMin - _bbox.posMin()) * _hashScale;
38  const Vector3d bMax = (boxMax - _bbox.posMin()) * _hashScale;
39 
40  const int minZ = std::abs(static_cast<int>(bMin.z()));
41  const int maxZ = std::abs(static_cast<int>(bMax.z()));
42  const int minY = std::abs(static_cast<int>(bMin.y()));
43  const int maxY = std::abs(static_cast<int>(bMax.y()));
44  const int minX = std::abs(static_cast<int>(bMin.x()));
45  const int maxX = std::abs(static_cast<int>(bMax.x()));
46  for (int iz = minZ; iz <= maxZ; iz++) {
47  for (int iy = minY; iy <= maxY; iy++) {
48  for (int ix = minX; ix <= maxX; ix++) {
49  unsigned int h = hash(ix, iy, iz);
50  _data[h].push_back(p);
51  }
52  }
53  }
54  }
55 
56  template <class T>
58  _data.clear();
59  }
60 
61  template <class T>
62  unsigned int HashGrid<T>::hash(const int ix, const int iy, const int iz) const {
63  Assertion(_hashSize > 0, "hash size is not initialized");
64  return (unsigned int)((ix * 73856093) ^ (iy * 19349663) ^ (iz * 83492791)) % _hashSize;
65  }
66 
67  template <class T>
68  const typename std::vector<T>& HashGrid<T>::operator[](const Point3d& v) const {
69  Vector3d b = (v - _bbox.posMin()) * _hashScale;
70  const int ix = std::abs(static_cast<int>(b.x()));
71  const int iy = std::abs(static_cast<int>(b.y()));
72  const int iz = std::abs(static_cast<int>(b.z()));
73  return _data[hash(ix, iy, iz)];
74  }
75 
76 } // namespace spica
77 
78 #endif // _SPICA_HASH_GRID_DETAIL_H_
79 
T z() const
Get z.
Definition: vector3d.h:106
void init(const int hashSize, const double hashScale, const Bounds3d &bbox)
Initialize grid.
Definition: hash_grid_detail.h:28
void add(const T &p, const Point3d &boxMin, const Point3d &boxMax)
Set point data for the cells inside the specifed bounding box.
Definition: hash_grid_detail.h:36
T x() const
Get x.
Definition: vector3d.h:98
void construct(std::vector< T > &points, const int imageW=-1, const int imageH=-1)
Construct hash grid.
Definition: hash_grid_detail.h:23
void clear()
Clear grid data.
Definition: hash_grid_detail.h:57
T y() const
Get y.
Definition: vector3d.h:102
Definition: hash_grid.h:12