KrisLibrary  1.0.0
VolumeGrid.h
1 #ifndef VOLUME_GRID_H
2 #define VOLUME_GRID_H
3 
4 #include <KrisLibrary/structs/array3d.h>
5 #include <KrisLibrary/math3d/AABB3D.h>
7 #include <KrisLibrary/utils/indexing.h>
8 
9 namespace Meshing {
10 
11  using namespace Math3D;
12 
19 template <class T>
21 {
22  VolumeGridIterator(const Array3D<T>& cells,const AABB3D& bb);
23  void setRange(const IntTriple& bmin,const IntTriple& bmax);
24  inline const T& operator *() const { return *it; }
25  inline T& operator *() { return *it; }
26  inline const IntTriple& getIndex() const { return index; }
27  void operator ++();
28  inline bool isDone() const { return index.a > hi.a; }
29  inline void getCell(AABB3D& cell) const
30  { cell.bmin=cellCorner; cell.bmax=cellCorner+cellSize; }
31  inline void getCellCenter(Vector3& c) const { c=cellCorner+cellSize*Half; }
32 
33  const Array3D<T>& cells;
34  const AABB3D& bb;
35  typename Array3D<T>::iterator it;
36  IntTriple lo,hi;
37  IntTriple index;
38  Vector3 cellCorner,cellSize;
39  Vector3 bbMin;
40 };
41 
57 template <class T>
59 {
60  public:
61  typedef VolumeGridTemplate<T> MyT;
62 
63  bool IsEmpty() const { return value.empty(); }
64  void Resize(int m,int n,int p) { value.resize(m,n,p); }
65  void ResizeByResolution(const Vector3& res);
66  template <class T2>
67  void MakeSimilar(const VolumeGridTemplate<T2>& grid);
68  template <class T2>
69  bool IsSimilar(const VolumeGridTemplate<T2>& grid) const;
70  void GetCell(int i,int j,int k,AABB3D& cell) const;
71  void GetCellCenter(int i,int j,int k,Vector3& center) const;
72  Vector3 GetCellSize() const;
74  void GetIndex(const Vector3& pt,int& i,int& j,int& k) const;
76  void GetIndexAndParams(const Vector3& pt,IntTriple& index,Vector3& params) const;
78  void GetIndexRange(const AABB3D& range,IntTriple& imin,IntTriple& imax) const;
80  bool GetIndexChecked(const Vector3& pt,int& i,int& j,int& k) const;
82  bool GetIndexAndParamsChecked(const Vector3& pt,IntTriple& index,Vector3& params) const;
84  bool GetIndexRangeClamped(const AABB3D& range,IntTriple& imin,IntTriple& imax) const;
85  inline void GetCell(const IntTriple& index,AABB3D& cell) const { GetCell(index.a,index.b,index.c,cell); }
86  inline void GetCenter(const IntTriple& index,Vector3& center) const { GetCellCenter(index.a,index.b,index.c,center); }
87  inline void GetIndex(const Vector3& pt,IntTriple& index) const { GetIndex(pt,index.a,index.b,index.c); }
88  inline void SetValue(int i,int j,int k,const T& v) { value(i,j,k)=v; }
89  inline void SetValue(const Vector3& pt,const T& v) { int i,j,k; GetIndex(pt,i,j,k); value(i,j,k)=v; }
90  inline T GetValue(int i,int j,int k) const { return value(i,j,k); }
91  inline T GetValue(const Vector3& pt) const { int i,j,k; GetIndex(pt,i,j,k); return value(i,j,k); }
92 
94  T TrilinearInterpolate(const Vector3& pt) const;
96  T MinimumFreeInterpolate(const Vector3& pt) const;
98  T Average(const AABB3D& range) const;
100  void ResampleTrilinear(const MyT& grid);
102  void ResampleAverage(const MyT& grid);
104  void Gradient_ForwardDifference(const IntTriple& index,Vector3& grad) const;
106  void Gradient_CenteredDifference(const IntTriple& index,Vector3& grad) const;
109  void Gradient(const Vector3& pt,Vector3& grad) const;
110  void Add(const MyT& grid);
111  void Subtract(const MyT& grid);
112  void Multiply(const MyT& grid);
113  void Max(const MyT& grid);
114  void Min(const MyT& grid);
115  void Add(T val);
116  void Multiply(T val);
117  void Max(T val);
118  void Min(T val);
119 
120  typedef VolumeGridIterator<T> iterator;
121  iterator getIterator() const { return iterator(value,bb); }
122 
123  Array3D<T> value;
124  AABB3D bb;
125 };
126 
128 
129 template <class T>
130 std::istream& operator >> (std::istream& in,VolumeGridTemplate<T>& grid)
131 {
132  in>>grid.bb.bmin>>grid.bb.bmax;
133  in>>grid.value;
134  return in;
135 }
136 
137 template <class T>
138 std::ostream& operator << (std::ostream& out,const VolumeGridTemplate<T>& grid)
139 {
140  out<<grid.bb.bmin<<" "<<grid.bb.bmax<<std::endl;
141  out<<grid.value<<std::endl;
142  return out;
143 }
144 
145 
146 template <class T>
147 template <class T2>
149 {
150  bb=grid.bb;
151  value.resize(grid.value.m,grid.value.n,grid.value.p);
152 }
153 
154 template <class T>
155 template <class T2>
157 {
158  return (grid.value.m == value.m && grid.value.n == value.n && grid.value.p == value.p) && (bb.bmin == grid.bb.bmin && bb.bmax == grid.bb.bmax);
159 }
160 
161 template <class T>
163  :cells(_cells),bb(_bb),it(&cells)
164 {
165  cellSize.x = (bb.bmax.x-bb.bmin.x)/Real(cells.m);
166  cellSize.y = (bb.bmax.y-bb.bmin.y)/Real(cells.n);
167  cellSize.z = (bb.bmax.z-bb.bmin.z)/Real(cells.p);
168  lo.set(0,0,0);
169  hi.set(_cells.m-1,_cells.n-1,_cells.p-1);
170  index=lo;
171  cellCorner = bbMin = bb.bmin;
172 }
173 
174 template <class T>
175 void VolumeGridIterator<T>::setRange(const IntTriple& bmin,const IntTriple& bmax)
176 {
177  lo=bmin;
178  hi=bmax;
179  bbMin = bb.bmin;
180  bbMin.x += Real(lo.a)*cellSize.x;
181  bbMin.y += Real(lo.b)*cellSize.y;
182  bbMin.z += Real(lo.c)*cellSize.z;
183  cellCorner = bbMin;
184  it = cells.begin(Range3Indices(lo.a,hi.a+1,
185  lo.b,hi.b+1,
186  lo.c,hi.c+1));
187  index=lo;
188 }
189 
190 template <class T>
192 {
193  ++it;
194  index.c++;
195  cellCorner.z+=cellSize.z;
196  if(index.c > hi.c) {
197  index.c=lo.c;
198  cellCorner.z=bbMin.z;
199  index.b++;
200  cellCorner.y+=cellSize.y;
201  if(index.b > hi.b) {
202  index.b=lo.b;
203  cellCorner.y=bbMin.y;
204  index.a++;
205  cellCorner.x+=cellSize.x;
206  }
207  }
208  Assert(index == it.getElement());
209 }
210 
211 
212 } //namespace Meshing
213 
214 #endif
215 
A lightweight integer 3-tuple class.
Definition: IntTriple.h:9
The namespace for all classes/functions in the Meshing package.
Definition: AnyGeometry.h:11
A 3D vector class.
Definition: math3d/primitives.h:136
A 3D lattice of regular ranges.
Definition: utils/indexing.h:175
A 3D axis-aligned bounding box.
Definition: AABB3D.h:13
Class declarations for useful 3D math types.
Definition: array3d.h:71
Contains all the definitions in the Math3D package.
Definition: AnyGeometry.h:13
A 3D array over an axis-aligned 3D volume, containing Real values.
Definition: AnyGeometry.h:11
Iterator over a 3D volume grid.
Definition: VolumeGrid.h:20
A three-dimensional m x n x parray.
Definition: array3d.h:31