Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gridutil.h
Go to the documentation of this file.
1 /* *******************************************************************
2  * Illinois Open Source License *
3  * *
4  * University of Illinois/NCSA *
5  * Open Source License *
6  * *
7  * Copyright@2008, University of Illinois. All rights reserved. *
8  * *
9  * Developed by: *
10  * *
11  * Center for Simulation of Advanced Rockets *
12  * *
13  * University of Illinois *
14  * *
15  * www.csar.uiuc.edu *
16  * *
17  * Permission is hereby granted, free of charge, to any person *
18  * obtaining a copy of this software and associated documentation *
19  * files (the "Software"), to deal with the Software without *
20  * restriction, including without limitation the rights to use, *
21  * copy, modify, merge, publish, distribute, sublicense, and/or *
22  * sell copies of the Software, and to permit persons to whom the *
23  * Software is furnished to do so, subject to the following *
24  * conditions: *
25  * *
26  * *
27  * @ Redistributions of source code must retain the above copyright *
28  * notice, this list of conditions and the following disclaimers. *
29  * *
30  * @ Redistributions in binary form must reproduce the above *
31  * copyright notice, this list of conditions and the following *
32  * disclaimers in the documentation and/or other materials *
33  * provided with the distribution. *
34  * *
35  * @ Neither the names of the Center for Simulation of Advanced *
36  * Rockets, the University of Illinois, nor the names of its *
37  * contributors may be used to endorse or promote products derived *
38  * from this Software without specific prior written permission. *
39  * *
40  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
41  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
42  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
43  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
44  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
46  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
47  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
48  *********************************************************************
49  * Please acknowledge The University of Illinois Center for *
50  * Simulation of Advanced Rockets in works and publications *
51  * resulting from this software or its derivatives. *
52  *********************************************************************/
53 /*
54 Simple grid manipulation routines.
55 
56 Orion Sky Lawlor, olawlor@acm.org, 5/30/2001
57 */
58 #ifndef __CSAR_GRIDUTIL_H
59 #define __CSAR_GRIDUTIL_H
60 
61 #include <math.h>
62 #include <iostream>
63 using std::ostream;
64 using std::endl;
65 using std::cout;
66 using std::cerr;
67 
68 //This typedef allows us to easily change the floating-point
69 // type used in all geometry calculations.
70 typedef float real;
71 
72 #include "vector3d.h"
73 
74 
75 //Permutation of (i,j,k) from source to dest:
76 // Source axis i maps to dest. axis orient[i]. (zero-based)
77 class orient_t {
78  int orient[3];
79  public:
80  int &operator[](int i) {return orient[i];}
81  const int &operator[](int i) const {return orient[i];}
82  orient_t inverse(void) const {
83  orient_t ret;
84  for (int i=0;i<3;i++) ret.orient[orient[i]]=i;
85  return ret;
86  }
87 };
88 
89 
90 //An i,j,k location in one 3D grid block
91 class blockDim;
92 class blockLoc {
93 protected:
94  int i,j,k;
95 public:
96  blockLoc() { }
97  blockLoc(int i_,int j_,int k_)
98  :i(i_), j(j_), k(k_) { }
99  blockLoc operator+ (const blockLoc &b) const
100  { return blockLoc(i+b.i,j+b.j,k+b.k); }
102  { i+=b.i; j+=b.j; k+=b.k; return *this; }
103  blockDim operator- (const blockLoc &b) const;
104  friend blockLoc operator*(const blockLoc &a,int k)
105  { return blockLoc(a.i*k,a.j*k,a.k*k); }
106  friend blockLoc operator*(int k,const blockLoc &a)
107  { return a*k; }
108 
109  bool operator==(const blockLoc &o) const
110  {return i==o.i && j==o.j && k==o.k; }
111  bool operator!=(const blockLoc &o) const
112  {return i!=o.i || j!=o.j || k!=o.k; }
113 
114  //Dimension indexing
115  int &operator[](int d) {return (&i)[d];}
116  int operator[](int d) const {return (&i)[d];}
117 };
118 
119 //The i, j, and k dimentions of one block
120 class blockDim : public blockLoc {
121 public:
122  blockDim() { }
123  blockDim(int i_,int j_,int k_)
124  :blockLoc(i_,j_,k_)
125  { }
126  int getSize(void) const
127  { return i*j*k; }
128  //Return the (0-based) array index of the (0-based) point (xi,xj,xk)
129  int c_index(int xi,int xj,int xk) const
130  { return xi+i*(xj+j*xk); }
131 
132  //Shorthand for above
133  int operator[](const blockLoc &l) const
134  { return c_index(l[0],l[1],l[2]); }
135 
136  //Dimension indexing
137  int &operator[](int d) {return (&i)[d];}
138  int operator[](int d) const {return (&i)[d];}
139 };
140 
141 inline blockDim blockLoc::operator- (const blockLoc &b) const
142  { return blockDim(i-b.i,j-b.j,k-b.k); }
143 
144 //Some subset of a block
145 class blockSpan {
146  //Swap the endpoints of the range (start,end+1)
147  static void swapSpan(int &start,int &end) {
148  end--;
149  int tmp=start;
150  start=end;
151  end=tmp;
152  end++;
153  }
154 public:
155  blockLoc start; //First included grid location
156  blockLoc end; //Last included grid location PLUS 1 ON EACH AXIS
157 
158  blockSpan() { }
159  blockSpan(const blockLoc &s,const blockLoc &e)
160  :start(s), end(e) { }
161 
162  blockDim getDim(void) const { return end-start; }
163 
164  //Swap me so start and end are sensible
165  void orient(void) {
166  for (int axis=0;axis<3;axis++) {
167  if (start[axis]>=end[axis]) {
168  swapSpan(start[axis],end[axis]);
169  }
170  }
171  }
172  //Swap both of us so my start and end is sensible
173  void orient(blockSpan &dest,const orient_t &src2dest) {
174  for (int axis=0;axis<3;axis++) {
175  if (start[axis]>=end[axis]) {
176  swapSpan(start[axis],end[axis]);
177  int dAxis=src2dest[axis];
178  swapSpan(dest.start[dAxis],dest.end[dAxis]);
179  }
180  }
181  }
182 
183  //Return the axis we have no thickness along, or -1 if none
184  int getFlatAxis(void) const {
185  for (int axis=0;axis<3;axis++)
186  if (start[axis]+1==end[axis])
187  return axis;
188  return -1;
189  }
190  //Return the block::face number we apply to, or -1 if none
191  int getFace(void) const {
192  int axis=getFlatAxis();
193  if (axis==-1) return -1;
194  if (start[axis]==0) return axis;
195  else return axis+3;
196  }
197 
198  //Return true if we contain the given location
199  bool contains(const blockLoc &l) const
200  {
201  for (int axis=0;axis<3;axis++)
202  if (!(start[axis]<=l[axis] && l[axis]<end[axis]))
203  return false;
204  return true;
205  }
206 
207  //Return true if this span represents a planar nonempty block:
208  bool hasArea(void) const {
209  int nThick=0;
210  for (int axis=0;axis<3;axis++) {
211  if (start[axis]>=end[axis])
212  return false; //A completely empty block
213  if (start[axis]<end[axis]-1)
214  nThick++; //Some nonzero thickness on this axis
215  }
216  return 2==nThick;
217  }
218 
219  blockSpan operator+(const blockLoc &l) const
220  {return blockSpan(start+l,end+l);}
221  blockSpan operator-(const blockLoc &l) const
222  {return blockSpan(start-l,end-l);}
223 
224  bool operator==(const blockSpan &o) const
225  {return start==o.start && end==o.end;}
226  bool operator!=(const blockSpan &o) const
227  {return start!=o.start || end!=o.end;}
228 };
229 
230 #define BLOCKSPAN_FOR(i,span) \
231  blockSpan loop_iter=span; \
232  for (i[2]=loop_iter.start[2];i[2]<loop_iter.end[2];i[2]++) \
233  for (i[1]=loop_iter.start[1];i[1]<loop_iter.end[1];i[1]++) \
234  for (i[0]=loop_iter.start[0];i[0]<loop_iter.end[0];i[0]++)
235 
236 
237 //Accepts blocks read from a file.
238 // Inherit from this abstract class, and call read.
240 public:
241  virtual ~blockConsumer();
242 
243  //Pass each block encountered in file to conume.
244  // Returns an error string; or NULL on success
245  const char *read(const char *gridFile);
246 
247  //Location vector block allocation/deallocation
248  virtual vector3d *allocateBlock(blockDim &dim);
249  virtual void freeBlock(vector3d *blk);
250 
251  //Returns error string on error; NULL on success
252  virtual const char *consume(
253  const blockDim &dim,//Dimentions of incoming block
254  vector3d *locs)=0; //X,Y,Z coordinates (ni x nj x nk)
255 
256 };
257 
258 #endif
259 
260 
261 
262 
263 
264 
265 
266 
267 
bool operator!=(const blockSpan &o) const
Definition: gridutil.h:226
int operator[](int d) const
Definition: gridutil.h:116
blockSpan operator-(const blockLoc &l) const
Definition: gridutil.h:221
friend blockLoc operator*(int k, const blockLoc &a)
Definition: gridutil.h:106
const NT & d
void orient(void)
Definition: gridutil.h:165
float real
Definition: gridutil.h:70
double s
Definition: blastest.C:80
blockLoc & operator+=(const blockLoc &b)
Definition: gridutil.h:101
bool operator==(const blockLoc &o) const
Definition: gridutil.h:109
blockLoc operator+(const blockLoc &b) const
Definition: gridutil.h:99
blockSpan(const blockLoc &s, const blockLoc &e)
Definition: gridutil.h:159
blockDim getDim(void) const
Definition: gridutil.h:162
blockLoc(int i_, int j_, int k_)
Definition: gridutil.h:97
int operator[](int d) const
Definition: gridutil.h:138
orient_t inverse(void) const
Definition: gridutil.h:82
int k
Definition: gridutil.h:94
bool hasArea(void) const
Definition: gridutil.h:208
virtual vector3d * allocateBlock(blockDim &dim)
Definition: read.cpp:146
blockLoc()
Definition: gridutil.h:96
virtual void freeBlock(vector3d *blk)
Definition: read.cpp:150
blockDim()
Definition: gridutil.h:122
blockLoc start
Definition: gridutil.h:155
blockDim operator-(const blockLoc &b) const
Definition: gridutil.h:141
virtual ~blockConsumer()
Definition: read.cpp:155
blockLoc i
Definition: read.cpp:79
blockLoc end
Definition: gridutil.h:156
int c_index(int xi, int xj, int xk) const
Definition: gridutil.h:129
blockSpan()
Definition: gridutil.h:158
static void swapSpan(int &start, int &end)
Definition: gridutil.h:147
int i
Definition: gridutil.h:94
int getFlatAxis(void) const
Definition: gridutil.h:184
const int & operator[](int i) const
Definition: gridutil.h:81
int orient[3]
Definition: gridutil.h:78
const char * read(const char *gridFile)
Definition: read.cpp:140
void orient(blockSpan &dest, const orient_t &src2dest)
Definition: gridutil.h:173
int operator[](const blockLoc &l) const
Definition: gridutil.h:133
int & operator[](int i)
Definition: gridutil.h:80
int & operator[](int d)
Definition: gridutil.h:115
int j
Definition: gridutil.h:94
int getSize(void) const
Definition: gridutil.h:126
virtual const char * consume(const blockDim &dim, vector3d *locs)=0
bool operator!=(const blockLoc &o) const
Definition: gridutil.h:111
bool operator==(const blockSpan &o) const
Definition: gridutil.h:224
int getFace(void) const
Definition: gridutil.h:191
bool contains(const blockLoc &l) const
Definition: gridutil.h:199
friend blockLoc operator*(const blockLoc &a, int k)
Definition: gridutil.h:104
int & operator[](int d)
Definition: gridutil.h:137
blockSpan operator+(const blockLoc &l) const
Definition: gridutil.h:219
blockDim(int i_, int j_, int k_)
Definition: gridutil.h:123