9
#include "Maths/Vector.hh"
12
//Mesh is a set of classes to describe a grid of points in various dimensions etc
13
//Initially this is for interpolation type algorithms, but one might also use these
14
//things for e.g. numerical integration etc
16
//So I define Mesh which is an abstract data type (i.e. can't be imlpemented, only inherited from)
17
//which has an Iterator object that behaves like a normal stl iterator
19
//Concrete classes are TwoDGrid, ThreeDGrid and NDGrid, rectangular grids with either constant
20
//spacing or dynamically allocated spacing
22
//See also TriangularMesh for an example of a (Delaunay) 2D triangular meshing algorithm
34
//Return the first point on the mesh
35
virtual Mesh::Iterator Begin() const = 0;
36
//Return the last point+1 on the mesh
37
virtual Mesh::Iterator End() const = 0;
38
//Return a copy of child object - copy constructor will only copy the parent object
39
virtual Mesh* Clone() = 0;
40
//Return the "Dual" of the mesh
41
//Dual is a polyhedron that has centre of each face as a point on the mesh
42
virtual Mesh* Dual () = 0;
45
//Overload these functions for Mesh::Iterator to work with your base class
46
//Return the position of a point in the mesh
47
virtual void Position(const Mesh::Iterator& it, double * position) const = 0;
48
//Return the dimension of the mesh
49
virtual int PositionDimension() const = 0;
50
//Map from iterator to an integer used to index the iterator
51
virtual int ToInteger(const Mesh::Iterator& lhs) const = 0;
52
//Find the point on the mesh nearest to some point
53
virtual Mesh::Iterator Nearest(const double* position) const = 0;
56
//Change the position of an iterator
57
//This is the minimal set of functions you need to define for the iterator to work
58
virtual Mesh::Iterator& AddEquals(Mesh::Iterator& lhs, int difference) const = 0;
59
virtual Mesh::Iterator& SubEquals(Mesh::Iterator& lhs, int difference) const = 0;
60
virtual Mesh::Iterator& AddEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const = 0;
61
virtual Mesh::Iterator& SubEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const = 0;
62
virtual Mesh::Iterator& AddOne (Mesh::Iterator& lhs) const = 0;
63
virtual Mesh::Iterator& SubOne (Mesh::Iterator& lhs) const = 0;
64
//Check relative position
65
virtual bool IsGreater(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const = 0;
68
//Iterator needs to know about internal workings of the Mesh to work properly
69
friend Mesh::Iterator operator++(Mesh::Iterator& lhs, int);
70
friend Mesh::Iterator operator--(Mesh::Iterator& lhs, int);
71
friend Mesh::Iterator& operator++(Mesh::Iterator& lhs);
72
friend Mesh::Iterator& operator--(Mesh::Iterator& lhs);
73
friend Mesh::Iterator operator- (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
74
friend Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
75
friend Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
76
friend Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
77
friend Mesh::Iterator operator- (const Mesh::Iterator& lhs, const int& rhs);
78
friend Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const int& rhs);
79
friend Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const int& rhs);
80
friend Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const int& rhs);
82
friend bool operator==(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
83
friend bool operator!=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
84
friend bool operator>=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
85
friend bool operator<=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
86
friend bool operator< (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
87
friend bool operator> (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
93
//Used to loop over some, or all, points in the mesh, as in stl
94
//Enables e,g, generic file I/O operations on a field map without knowing
95
//the details of what is in the mesh or what shape it has...
100
Iterator(const Mesh::Iterator& in);
101
Iterator(std::vector<int> state, const Mesh* mesh);
103
const Mesh::Iterator& operator=(const Mesh::Iterator& rhs);
104
//return position referenced by the iterator
105
virtual void Position(double* point) const;
106
virtual std::vector<double> Position() const;
108
int ToInteger() const {return _mesh->ToInteger(*this);}
109
std::vector<int> State() const {return _state;}
110
int& operator[](int i) {return _state[i]; }
111
const int& operator[](int i) const {return _state[i]; }
112
const Mesh* GetMesh() const {return _mesh;}
115
friend class TwoDGrid;
116
friend class ThreeDGrid;
118
friend class TriangularMesh;
120
friend Mesh::Iterator operator++(Mesh::Iterator& lhs, int);
121
friend Mesh::Iterator operator--(Mesh::Iterator& lhs, int);
122
friend Mesh::Iterator& operator++(Mesh::Iterator& lhs);
123
friend Mesh::Iterator& operator--(Mesh::Iterator& lhs);
124
friend Mesh::Iterator operator- (const Mesh::Iterator& lhs, const int& difference);
125
friend Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const int& difference);
126
friend Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const int& difference);
127
friend Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const int& difference);
128
friend Mesh::Iterator operator- (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
129
friend Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
130
friend Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
131
friend Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
133
friend bool operator==(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
134
friend bool operator!=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
135
friend bool operator>=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
136
friend bool operator<=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
137
friend bool operator< (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
138
friend bool operator> (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
143
std::vector<int> _state;
146
Mesh::Iterator& operator++(Mesh::Iterator& lhs); //no int means prefix operator ++it
147
Mesh::Iterator& operator--(Mesh::Iterator& lhs);
148
Mesh::Iterator operator++(Mesh::Iterator& lhs, int); //int means postfix operator it++
149
Mesh::Iterator operator--(Mesh::Iterator& lhs, int);
150
Mesh::Iterator operator- (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
151
Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
152
Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
153
Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
154
Mesh::Iterator operator- (const Mesh::Iterator& lhs, const int& rhs);
155
Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const int& rhs);
156
Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const int& rhs);
157
Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const int& rhs);
159
bool operator==(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
160
bool operator!=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
161
bool operator>=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
162
bool operator<=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
163
bool operator< (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
164
bool operator> (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
166
std::ostream& operator<<(std::ostream& out, const Mesh::Iterator& it);
10
#include "src/legacy/Interface/Meshing/Mesh.hh"
11
#include "src/legacy/Interface/Meshing/ThreeDGrid.hh"
168
13
//Holds grid info for 2dTo1d interpolation algorithm;
169
14
//also controls memory usage (essentially a boost::shared_pointer but not as elegent);
274
//Holds grid info for 2dTo1d interpolation algorithm;
275
//also controls memory usage (essentially a boost::shared_pointer but not as elegant);
276
class ThreeDGrid : public Mesh
280
Mesh * Clone() {return new ThreeDGrid(*this);}
281
Mesh * Dual () {return NULL;}
283
//ERROR SHOULD NOT ALLOW MESH WITH 1 GRID POINT (causes error in LowerBound)
285
ThreeDGrid(double dX, double dY, double dZ, double minX, double minY, double minZ, int numberOfXCoords, int numberOfYCoords, int numberOfZCoords);
286
ThreeDGrid(int xSize, const double *x, int ySize, const double *y, int zSize, const double *z);
287
ThreeDGrid(std::vector<double> x, std::vector<double> y, std::vector<double> z);
290
//get coordinate; round bracket indexing starts at 1 goes to nCoords
291
inline double& x (const int& i) {return _x[i-1];}
292
inline double& y (const int& j) {return _y[j-1];}
293
inline double& z (const int& k) {return _z[k-1];}
294
inline const double& x (const int& i) const {return _x[i-1];}
295
inline const double& y (const int& j) const {return _y[j-1];}
296
inline const double& z (const int& j) const {return _z[j-1];}
297
inline int xSize() const {return int(_x.size());}
298
inline int ySize() const {return int(_y.size());}
299
inline int zSize() const {return int(_z.size());}
301
std::vector<double> xVector() {return std::vector<double>(_x);}
302
std::vector<double> yVector() {return std::vector<double>(_y);}
303
std::vector<double> zVector() {return std::vector<double>(_z);}
305
double* newXArray() {double *x = new double[_x.size()]; for(unsigned int i=0; i<_x.size(); i++) x[i]=_x[i]; return x;}
306
double* newYArray() {double *y = new double[_y.size()]; for(unsigned int i=0; i<_y.size(); i++) y[i]=_y[i]; return y;}
307
double* newZArray() {double *z = new double[_z.size()]; for(unsigned int i=0; i<_z.size(); i++) z[i]=_z[i]; return z;}
309
//if you are sure the grid has constant spacing ConstSpacing is quicker; VarSpacing in either case
310
//indexing starts at 0 and goes to nCoords+1
311
inline void xLowerBound (const double& x, int& xIndex) const
312
{if(_constantSpacing) xIndex = static_cast<int>(floor( (x - _x[0])/(_x[1]-_x[0]) )); else xIndex = std::lower_bound(_x.begin(), _x.end(), x)-_x.begin()-1;}
313
inline void yLowerBound (const double& y, int& yIndex) const
314
{if(_constantSpacing) yIndex = static_cast<int>(floor( (y - _y[0])/(_y[1]-_y[0]) )); else yIndex = std::lower_bound(_y.begin(), _y.end(), y)-_y.begin()-1;}
315
inline void zLowerBound (const double& z, int& zIndex) const
316
{if(_constantSpacing) zIndex = static_cast<int>(floor( (z - _z[0])/(_z[1]-_z[0]) )); else zIndex = std::lower_bound(_z.begin(), _z.end(), z)-_z.begin()-1;}
317
inline void LowerBound (const double& x, int& xIndex, const double& y, int& yIndex, const double& z, int& zIndex) const
318
{xLowerBound(x, xIndex); yLowerBound(y, yIndex); zLowerBound(z, zIndex);}
319
inline void LowerBound (const double& x, const double& y, const double& z, Mesh::Iterator& it) const
320
{xLowerBound(x, it[0]); yLowerBound(y, it[1]); zLowerBound(z, it[2]); it[0]++; it[1]++; it[2]++;}
321
inline double MinX() const {return _x[0];}
322
inline double MaxX() const {return _x[_xSize-1];}
323
inline double MinY() const {return _y[0];}
324
inline double MaxY() const {return _y[_ySize-1];}
325
inline double MinZ() const {return _z[0];}
326
inline double MaxZ() const {return _z[_zSize-1];}
328
void SetX(int nXCoords, double * x) {_x = std::vector<double>(x,x+nXCoords);}
329
void SetY(int nYCoords, double * y) {_y = std::vector<double>(y,y+nYCoords);}
330
void SetZ(int nZCoords, double * z) {_z = std::vector<double>(z,z+nZCoords);}
332
void Add(VectorMap* map); //add *map if it has not already been added
333
void Remove(VectorMap* map); //remove *map if it exists; delete this if there are no more VectorMaps
335
Mesh::Iterator Begin() const;
336
Mesh::Iterator End() const;
337
virtual void Position(const Mesh::Iterator& it, double * position) const;
338
int PositionDimension() const {return 3;}
339
int ToInteger(const Mesh::Iterator& lhs) const {return (lhs.State()[0]-1)*_zSize*_ySize+(lhs.State()[1]-1)*_zSize+(lhs.State()[2]-1);}
341
void SetConstantSpacing(bool spacing) {_constantSpacing = spacing;}
342
void SetConstantSpacing();
343
bool GetConstantSpacing() const {return _constantSpacing; }
344
Mesh::Iterator Nearest(const double* position) const;
349
virtual Mesh::Iterator& AddEquals(Mesh::Iterator& lhs, int difference) const;
350
virtual Mesh::Iterator& SubEquals(Mesh::Iterator& lhs, int difference) const;
351
virtual Mesh::Iterator& AddEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
352
virtual Mesh::Iterator& SubEquals(Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
353
virtual Mesh::Iterator& AddOne (Mesh::Iterator& lhs) const;
354
virtual Mesh::Iterator& SubOne (Mesh::Iterator& lhs) const;
355
//Check relative position
356
virtual bool IsGreater(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs) const;
359
std::vector<double> _x;
360
std::vector<double> _y;
361
std::vector<double> _z;
365
std::vector<VectorMap*> _maps;
366
bool _constantSpacing;
368
friend Mesh::Iterator operator++(Mesh::Iterator& lhs, int);
369
friend Mesh::Iterator operator--(Mesh::Iterator& lhs, int);
370
friend Mesh::Iterator& operator++(Mesh::Iterator& lhs);
371
friend Mesh::Iterator& operator--(Mesh::Iterator& lhs);
372
friend Mesh::Iterator operator- (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
373
friend Mesh::Iterator operator+ (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
374
friend Mesh::Iterator& operator-=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
375
friend Mesh::Iterator& operator+=(Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
377
friend bool operator==(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
378
friend bool operator!=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
379
friend bool operator>=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
380
friend bool operator<=(const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
381
friend bool operator< (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
382
friend bool operator> (const Mesh::Iterator& lhs, const Mesh::Iterator& rhs);
386
119
//Holds grid info for some vector mapping;
387
120
//also controls memory usage (essentially a boost::shared_pointer but not as elegent);
388
121
class NDGrid : public Mesh