162
154
///// End of TwoDGrid /////
164
///// Mesh::Iterator /////
166
Mesh::Iterator::Iterator() {;}
167
Mesh::Iterator::Iterator(const Mesh::Iterator& in) : _mesh(in._mesh), _state(in._state) {;}
168
Mesh::Iterator::Iterator(std::vector<int> state, const Mesh* mesh) : _mesh(mesh), _state(state) {;}
169
Mesh::Iterator::~Iterator() {;}
171
const Mesh::Iterator& Mesh::Iterator::operator= (const Mesh::Iterator& rhs)
178
void Mesh::Iterator::Position(double* point) const
180
_mesh->Position(*this, point);
183
std::vector<double> Mesh::Iterator::Position() const
185
std::vector<double> PointV(_mesh->PositionDimension());
186
_mesh->Position(*this, &PointV[0]);
190
Mesh::Iterator& operator++(Mesh::Iterator& lhs) {return lhs._mesh->AddOne(lhs);}
191
Mesh::Iterator& operator--(Mesh::Iterator& lhs) {return lhs._mesh->SubOne(lhs);}
192
Mesh::Iterator operator++(Mesh::Iterator& lhs, int) {Mesh::Iterator copy = lhs; lhs._mesh->AddOne(lhs); return copy;}
193
Mesh::Iterator operator--(Mesh::Iterator& lhs, int) {Mesh::Iterator copy = lhs; lhs._mesh->SubOne(lhs); return copy;}
195
Mesh::Iterator operator- (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs)
197
Mesh::Iterator lhsCopy(lhs);
198
Mesh::Iterator lhsNew(lhs._mesh->SubEquals(lhsCopy, rhs));
202
Mesh::Iterator operator- (const Mesh::Iterator& lhs, const int& difference)
204
Mesh::Iterator lhsCopy(lhs);
205
Mesh::Iterator lhsNew(lhs._mesh->SubEquals(lhsCopy, difference));
209
Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs)
211
Mesh::Iterator lhsCopy(lhs);
212
Mesh::Iterator lhsNew(lhs._mesh->AddEquals(lhsCopy, rhs));
216
Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const int& difference)
218
Mesh::Iterator lhsCopy(lhs);
219
Mesh::Iterator lhsNew(lhs._mesh->AddEquals(lhsCopy, difference));
224
Mesh::Iterator& operator+= (Mesh::Iterator& lhs, const Mesh::Iterator& rhs) { return lhs._mesh->AddEquals(lhs, rhs); }
225
Mesh::Iterator& operator-= (Mesh::Iterator& lhs, const Mesh::Iterator& rhs) { return lhs._mesh->SubEquals(lhs, rhs); }
226
Mesh::Iterator& operator+= (Mesh::Iterator& lhs, const int& rhs) { return lhs._mesh->AddEquals(lhs, rhs); }
227
Mesh::Iterator& operator-= (Mesh::Iterator& lhs, const int& rhs) { return lhs._mesh->SubEquals(lhs, rhs); }
230
bool operator>=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs)
232
if(lhs._mesh->IsGreater(lhs, rhs) || lhs==rhs) return true;
236
bool operator<=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs)
238
if(lhs._mesh->IsGreater(lhs, rhs)) return false;
242
bool operator>(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs)
247
bool operator<(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs)
252
bool operator==(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs)
254
if(lhs._state==rhs._state)
259
bool operator!=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs)
261
if(lhs._mesh==rhs._mesh && lhs._state==rhs._state)
266
std::ostream& operator<<(std::ostream& out, const Mesh::Iterator& it)
268
out << std::setw(5) << it.ToInteger() << " ** ";
269
for(unsigned int i=0; i<it.State().size(); i++) out << std::setw(5) << it[i] << " ";
271
for(unsigned int i=0; i<it.Position().size(); i++) out << std::scientific << std::setprecision(3) << std::setw(12) << it.Position()[i] << " ";
276
////// Mesh::Iterator end /////
278
////// ThreeDGrid //////
280
ThreeDGrid::ThreeDGrid() : _x(2, 0), _y(2, 0), _z(2, 0), _xSize(0), _ySize(0), _zSize(0), _maps(0), _constantSpacing(false)
282
SetConstantSpacing();
283
_x[1] = _y[1] = _z[1] = 1.;
286
ThreeDGrid::ThreeDGrid(int xSize, const double *x, int ySize, const double *y, int zSize, const double *z)
287
: _x (x, x+xSize), _y(y, y+ySize), _z(z, z+zSize), _xSize(xSize), _ySize(ySize), _zSize(zSize), _maps(), _constantSpacing(false)
289
SetConstantSpacing();
290
if(_xSize < 2 || _ySize < 2 || _zSize <2) throw(Squeal(Squeal::recoverable, "3D Grid must be at least 2x2x2 grid", "ThreeDGrid::ThreeDGrid(...)"));
293
ThreeDGrid::ThreeDGrid(std::vector<double> x, std::vector<double> y, std::vector<double> z)
294
: _x (x), _y(y), _z(z), _xSize(x.size()), _ySize(y.size()), _zSize(z.size()), _maps(), _constantSpacing(false)
296
SetConstantSpacing();
297
if(_xSize < 2 || _ySize < 2 || _zSize <2) throw(Squeal(Squeal::recoverable, "3D Grid must be at least 2x2x2 grid", "ThreeDGrid::ThreeDGrid(...)"));
300
ThreeDGrid::ThreeDGrid(double dX, double dY, double dZ, double minX, double minY, double minZ,
301
int numberOfXCoords, int numberOfYCoords, int numberOfZCoords)
302
: _x(numberOfXCoords), _y(numberOfYCoords), _z(numberOfZCoords), _xSize(numberOfXCoords), _ySize(numberOfYCoords), _zSize(numberOfZCoords), _maps(), _constantSpacing(true)
304
for(int i=0; i<numberOfXCoords; i++) _x[i] = minX+i*dX;
305
for(int j=0; j<numberOfYCoords; j++) _y[j] = minY+j*dY;
306
for(int k=0; k<numberOfZCoords; k++) _z[k] = minZ+k*dZ;
308
SetConstantSpacing();
311
ThreeDGrid::~ThreeDGrid()
315
//state starts at 1,1,1
316
Mesh::Iterator ThreeDGrid::Begin() const
318
return Mesh::Iterator(std::vector<int>(3,1), this);
321
Mesh::Iterator ThreeDGrid::End() const
323
std::vector<int> end(3, 1); end[0] = _xSize+1; return Mesh::Iterator(end, this);
326
void ThreeDGrid::Position(const Mesh::Iterator& it, double * position) const
328
position[0] = x(it._state[0]);
329
position[1] = y(it._state[1]);
330
position[2] = z(it._state[2]);
334
Mesh::Iterator& ThreeDGrid::AddEquals(Mesh::Iterator& lhs, int difference) const
336
if(difference < 0) return SubEquals(lhs, -difference);
338
lhs._state[0] += difference/(_ySize*_zSize);
339
difference = difference%(_ySize*_zSize);
340
lhs._state[1] += difference/(_zSize);
341
lhs._state[2] += difference%(_zSize);
343
if(lhs._state[1] > _ySize) {lhs._state[0]++; lhs._state[1] -= _ySize;}
344
if(lhs._state[2] > _zSize) {lhs._state[1]++; lhs._state[2] -= _zSize;}
349
Mesh::Iterator& TwoDGrid::SubEquals(Mesh::Iterator& lhs, int difference) const
351
if(difference < 0) return AddEquals(lhs, -difference);
352
lhs._state[0] -= difference/(_ySize);
353
lhs._state[1] -= difference%(_ySize);
354
if(lhs._state[1] < 1) {lhs._state[1] += _ySize; lhs._state[0]--;}
358
Mesh::Iterator& ThreeDGrid::SubEquals(Mesh::Iterator& lhs, int difference) const
360
if(difference < 0) return AddEquals(lhs, -difference);
362
lhs._state[0] -= difference/(_ySize*_zSize);
363
difference = difference%(_ySize*_zSize);
364
lhs._state[1] -= difference/(_zSize);
365
lhs._state[2] -= difference%(_zSize);
367
while(lhs._state[2] < 1) {lhs._state[1]--; lhs._state[2] += _zSize;}
368
while(lhs._state[1] < 1) {lhs._state[0]--; lhs._state[1] += _ySize;}
374
Mesh::Iterator& ThreeDGrid::AddEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const
376
return AddEquals(lhs, rhs.ToInteger());
379
Mesh::Iterator& ThreeDGrid::SubEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const
381
return SubEquals(lhs, rhs.ToInteger());
384
Mesh::Iterator& ThreeDGrid::AddOne (Mesh::Iterator& lhs) const
386
if(lhs._state[1] == _ySize && lhs._state[2] == _zSize) {++lhs._state[0]; lhs._state[1] = lhs._state[2] = 1;}
387
else if(lhs._state[2] == _zSize) {++lhs._state[1]; lhs._state[2] = 1;}
388
else ++lhs._state[2];
392
Mesh::Iterator& ThreeDGrid::SubOne (Mesh::Iterator& lhs) const
394
if(lhs._state[1] == 1 && lhs._state[2] == 1) {--lhs._state[0]; lhs._state[1] = _ySize; lhs._state[2] = _zSize;}
395
else if(lhs._state[2] == 1) {--lhs._state[1]; lhs._state[2] = _zSize;}
396
else --lhs._state[2];
399
//Check relative position
400
bool ThreeDGrid::IsGreater(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const
402
if(lhs._state[0] > rhs._state[0]) return true;
403
else if (lhs._state[0] == rhs._state[0] && lhs._state[1] > rhs._state[1]) return true;
404
else if (lhs._state[0] == rhs._state[0] && lhs._state[1] == rhs._state[1] && lhs._state[2] > rhs._state[2]) return true;
408
void ThreeDGrid::Remove(VectorMap* map) //remove *map if it exists; delete this if there are no more VectorMaps
410
std::vector<VectorMap*>::iterator it = std::find(_maps.begin(), _maps.end(), map);
411
if(it<_maps.end()) { _maps.erase(it); }
412
if(_maps.begin() == _maps.end()) { delete this; }
415
void ThreeDGrid::Add(VectorMap* map) //add *map if it has not already been added
417
std::vector<VectorMap*>::iterator it = std::find(_maps.begin(), _maps.end(), map);
418
if(it==_maps.end()) { _maps.push_back(map); }
421
void ThreeDGrid::SetConstantSpacing()
423
_constantSpacing = true;
424
for(unsigned int i=0; i<_x.size()-1; i++) if( fabs(1-(_x[i+1]-_x[i])/(_x[1]-_x[0])) > 1e-9 ) {_constantSpacing = false; return;}
425
for(unsigned int i=0; i<_y.size()-1; i++) if( fabs(1-(_y[i+1]-_y[i])/(_y[1]-_y[0])) > 1e-9 ) {_constantSpacing = false; return;}
426
for(unsigned int i=0; i<_z.size()-1; i++) if( fabs(1-(_z[i+1]-_z[i])/(_z[1]-_z[0])) > 1e-9 ) {_constantSpacing = false; return;}
429
Mesh::Iterator ThreeDGrid::Nearest(const double* position) const
431
std::vector<int> index(3);
432
LowerBound(position[0], index[0], position[1], index[1], position[2], index[2]);
433
if(index[0] < _xSize-1 && index[0]>=0) index[0] += (2*(position[0] - _x[index[0]]) > _x[index[0]+1]-_x[index[0]] ? 2 : 1);
435
if(index[1] < _ySize-1 && index[1]>=0) index[1] += (2*(position[1] - _y[index[1]]) > _y[index[1]+1]-_y[index[1]] ? 2 : 1);
437
if(index[2] < _zSize-1 && index[2]>=0) index[2] += (2*(position[2] - _z[index[2]]) > _z[index[2]+1]-_z[index[2]] ? 2 : 1);
439
if(index[0] < 1) index[0] = 1;
440
if(index[1] < 1) index[1] = 1;
441
if(index[2] < 1) index[2] = 1;
442
if(index[0] > _xSize) index[0] = _xSize;
443
if(index[1] > _ySize) index[1] = _ySize;
444
if(index[2] > _zSize) index[2] = _zSize;
445
return Mesh::Iterator(index, this);
448
/// ////// ThreeDGrid END //////
450
157
/// ////// NDGrid ///////
451
158
NDGrid::NDGrid() : _coord(), _maps(), _constantSpacing(false)