KrisLibrary  1.0.0
AreaGrid.h
1 #ifndef AREA_GRID_H
2 #define AREA_GRID_H
3 
4 #include <KrisLibrary/structs/array2d.h>
5 #include <KrisLibrary/math3d/AABB2D.h>
7 #include <KrisLibrary/utils/indexing.h>
8 
9 namespace Meshing {
10 
11  using namespace Math3D;
12 
19 template <class T>
21 {
22  AreaGridIterator(const Array2D<T>& cells,const AABB2D& bb);
23  void setRange(const IntPair& bmin,const IntPair& bmax);
24  inline const T& operator *() const { return *it; }
25  inline T& operator *() { return *it; }
26  inline const IntPair& getIndex() const { return index; }
27  void operator ++();
28  inline bool isDone() const { return index.a > hi.a; }
29  inline void getCell(AABB2D& cell) const
30  { cell.bmin=cellCorner; cell.bmax=cellCorner+cellSize; }
31  inline void getCellCenter(Vector2& c) const { c=cellCorner+cellSize*Half; }
32 
33  const Array2D<T>& cells;
34  const AABB2D& bb;
35  typename Array2D<T>::iterator it;
36  IntPair lo,hi;
37  IntPair index;
38  Vector2 cellCorner,cellSize;
39  Vector2 bbMin;
40 };
41 
57 template <class T>
59 {
60  public:
61  typedef AreaGridTemplate<T> MyT;
62 
63  bool IsEmpty() const { return value.empty(); }
64  void Resize(int m,int n) { value.resize(m,n); }
65  void ResizeByResolution(const Vector2& res);
66  template <class T2>
67  void MakeSimilar(const AreaGridTemplate<T2>& grid);
68  template <class T2>
69  bool IsSimilar(const AreaGridTemplate<T2>& grid) const;
70  void GetCell(int i,int j,AABB2D& cell) const;
71  void GetCellCenter(int i,int j,Vector2& center) const;
72  Vector2 GetCellSize() const;
73  void GetIndex(const Vector2& pt,int& i,int& j) const;
74  void GetIndexAndParams(const Vector2& pt,IntPair& index,Vector2& params) const;
75  void GetIndexRange(const AABB2D& range,IntPair& imin,IntPair& imax) const;
76  inline void GetCell(const IntPair& index,AABB2D& cell) const { GetCell(index.a,index.b,cell); }
77  inline void GetCenter(const IntPair& index,Vector2& center) const { GetCellCenter(index.a,index.b,center); }
78  inline void GetIndex(const Vector2& pt,IntPair& index) const { GetIndex(pt,index.a,index.b); }
79  inline void SetValue(int i,int j,const T& v) { value(i,j)=v; }
80  inline void SetValue(const Vector2& pt,const T& v) { int i,j; GetIndex(pt,i,j); value(i,j)=v; }
81  inline T GetValue(int i,int j) const { return value(i,j); }
82  inline T GetValue(const Vector2& pt) const { int i,j; GetIndex(pt,i,j); return value(i,j); }
83 
85  T BilinearInterpolate(const Vector2& pt) const;
87  T Average(const AABB2D& range) const;
89  void ResampleBilinear(const MyT& grid);
91  void ResampleAverage(const MyT& grid);
93  void Gradient_ForwardDifference(const IntPair& index,Vector2& grad) const;
95  void Gradient_CenteredDifference(const IntPair& index,Vector2& grad) const;
98  void Gradient(const Vector2& pt,Vector2& grad) const;
99  void Add(const MyT& grid);
100  void Subtract(const MyT& grid);
101  void Multiply(const MyT& grid);
102  void Max(const MyT& grid);
103  void Min(const MyT& grid);
104  void Add(T val);
105  void Multiply(T val);
106  void Max(T val);
107  void Min(T val);
108 
110  iterator getIterator() const { return iterator(value,bb); }
111 
112  Array2D<T> value;
113  AABB2D bb;
114 };
115 
117 
118 template <class T>
119 std::istream& operator >> (std::istream& in,AreaGridTemplate<T>& grid)
120 {
121  in>>grid.bb.bmin>>grid.bb.bmax;
122  in>>grid.value;
123  return in;
124 }
125 
126 template <class T>
127 std::ostream& operator << (std::ostream& out,const AreaGridTemplate<T>& grid)
128 {
129  out<<grid.bb.bmin<<" "<<grid.bb.bmax<<std::endl;
130  out<<grid.value<<std::endl;
131  return out;
132 }
133 
134 
135 template <class T>
136 template <class T2>
138 {
139  bb=grid.bb;
140  value.resize(grid.value.m,grid.value.n);
141 }
142 
143 template <class T>
144 template <class T2>
146 {
147  return (grid.value.m == value.m && grid.value.n == value.n) && (bb.bmin == grid.bb.bmin && bb.bmax == grid.bb.bmax);
148 }
149 
150 template <class T>
152  :cells(_cells),bb(_bb),it(&cells)
153 {
154  cellSize.x = (bb.bmax.x-bb.bmin.x)/Real(cells.m);
155  cellSize.y = (bb.bmax.y-bb.bmin.y)/Real(cells.n);
156  lo.set(0,0);
157  hi.set(_cells.m-1,_cells.n-1);
158  index=lo;
159  cellCorner = bbMin = bb.bmin;
160 }
161 
162 template <class T>
163 void AreaGridIterator<T>::setRange(const IntPair& bmin,const IntPair& bmax)
164 {
165  lo=bmin;
166  hi=bmax;
167  bbMin = bb.bmin;
168  bbMin.x += Real(lo.a)*cellSize.x;
169  bbMin.y += Real(lo.b)*cellSize.y;
170  cellCorner = bbMin;
171  it = cells.begin(Range2Indices(lo.a,hi.a+1,
172  lo.b,hi.b+1));
173  index=lo;
174 }
175 
176 template <class T>
178 {
179  ++it;
180  index.b++;
181  cellCorner.y+=cellSize.y;
182  if(index.b > hi.b) {
183  index.b=lo.b;
184  cellCorner.y=bbMin.y;
185  index.a++;
186  cellCorner.x+=cellSize.x;
187  }
188  Assert(index == it.getElement());
189 }
190 
191 
192 } //namespace Meshing
193 
194 #endif
195 
Definition: array2d.h:70
The namespace for all classes/functions in the Meshing package.
Definition: AnyGeometry.h:11
Iterator over a 2D area grid.
Definition: AreaGrid.h:20
A 2D array over an axis-aligned 2D box, containing Real values.
Definition: AreaGrid.h:58
A lightweight integer 2-tuple class.
Definition: IntPair.h:9
Class declarations for useful 3D math types.
A two-dimensional m x n array.
Definition: array2d.h:30
Contains all the definitions in the Math3D package.
Definition: AnyGeometry.h:13
A 2D lattice of regular ranges.
Definition: utils/indexing.h:116
A 2D vector class.
Definition: math3d/primitives.h:41
A 2D axis-aligned bounding box.
Definition: AABB2D.h:13