Overture  Version 25
smesh.hh
Go to the documentation of this file.
1 #ifndef __SMESH_HH__
2 #define __SMESH_HH__
3 
4 #include <vector>
5 #include <algorithm>
6 
7 #include "OvertureTypes.h"
8 #include "TFIMapping.h"
9 #include "UnstructuredMapping.h"
10 #include "SquareMapping.h"
11 #include "StretchTransform.h"
12 #include "ArraySimple.h"
13 #include "NurbsMapping.h"
14 
15 // getVertexID is used to set global ids for all the vertices
16 extern int getVertexID();
17 extern int freeVertexID();
18 extern void resetVertexIDs();
19 extern int currentIDCounter();
20 
21 class Region;
22 
24 class Curve {
25 public:
26 
27  // Curve() : startPointID(-1), endPointID(-1) { regions[0]=regions[1]=0; }
28  Curve(int ps=-1, int pe=-1) : startPointID(ps), endPointID(pe), id(idCounter++)
29  { regions[0]=regions[1]=0; }
30 
31  virtual ~Curve() { }
32 
33  // get a NURBS representation of the curve suitable for plotting and mapping
34  virtual NurbsMapping & getNurbs()=0;
35 
36  // get the range bound for the curve
37  virtual real getRangeBound(int side, int axis)=0;
38 
39  // get the primary and secondary region pointers
40  Region *region_1() { return regions[0]; }
41  Region *region_2() { return regions[1]; }
42 
43  // set the primary and secondary region pointers
44  virtual void region_1(Region *r) { regions[0]=r; }
45  virtual void region_2(Region *r) { regions[1]=r;}
46 
47  // remove a specific region
48  virtual void unbindRegion(Region *r)
49  {
50  if ( regions[0]==r )
51  {
52  regions[0] = regions[1];
53  regions[1] = 0;
54  }
55  else if ( regions[1]==r )
56  regions[1] = 0;
57  }
58 
59  // get/set endpoints
60  void setStartPointID(int pid) { startPointID=pid; };
61  void setEndPointID(int pid) { endPointID=pid; }
62  int getStartPointID() { return startPointID; };
63  int getEndPointID() { return endPointID; }
64 
66  // result in the array grid
67  virtual void discretize(real dxRef, real dyRef, ArraySimple<real> &grid)=0;
68 
69  virtual ArraySimple<int> & getGridIDList() = 0;
70  virtual ArraySimple<real> & getVertices() = 0;
71  virtual void resetIDList() { gridIDList = ArraySimple<int>(); }
72 
73  // return the number of points in the discretized curve
74  virtual int numberOfPoints()=0;
75 
76  int ID() const { return id; }
77 
78 protected:
80 
81 private:
82  int startPointID, endPointID;
83  Region * regions[2];
84  int id;
85  static int idCounter;
86 };
87 
89 class Region {
90 
91 public:
92  Region(real dx=.1, real dy=.1, std::string nm="") :
93  dxRef(dx), dyRef(dy), name(nm), id(idCounter++) { }
94 
95  virtual ~Region()
96  {
97  // use this while loop because delCurve invalidates curve_iterators
98  while ( curve_begin()!=curve_end() )
100 
101  };
102 
103  typedef std::vector<Curve *>::iterator curve_iterator;
104 
106  void addCurve(Curve *c)
107  {
108  if ( c )
109  {
110  real minmax[2][2];
111  referenceGrid.getVertices(minmax[0][0],minmax[1][0],
112  minmax[0][1],minmax[1][1]);
113 
114  // NurbsMapping &n = c->getNurbs();
115  // for ( int a=0; a<2; a++ )
116  // cout<<"nr nurb rangebound"<<a<<" "<<n.getRangeBound(0,a)<<" "<<n.getRangeBound(1,a)<<endl;
117 
118  if ( curves.size()!=0 )
119  for ( int a=0; a<2; a++ )
120  {
121  minmax[0][a] = min(minmax[0][a],real(c->getRangeBound(0,a)),
122  real(c->getRangeBound(1,a)));
123  minmax[1][a] = max(minmax[1][a],real(c->getRangeBound(0,a)),
124  real(c->getRangeBound(1,a)));
125  }
126  else // this is the first curve
127  for ( int a=0; a<2; a++ )
128  {
129  minmax[0][a] = min(real(c->getRangeBound(0,a)),
130  real(c->getRangeBound(1,a)));
131  minmax[1][a] = max(real(c->getRangeBound(0,a)),
132  real(c->getRangeBound(1,a)));
133  }
134 
135  specifyReferenceGridBounds(minmax[0][0],minmax[0][1],minmax[1][0],
136  minmax[1][1]);
137  curves.push_back(c);
138 
139  if ( c->region_1() )
140  c->region_2(this);
141  else
142  c->region_1(this);
143  }
144  }
145 
147  void delCurve(Curve *c)
148  {
149  if ( c )
150  c->unbindRegion(this);
151 
152  curves.erase(std::find(curves.begin(),curves.end(),c));
153  // fail silently if curve c is not in the region
154  }
155 
156  curve_iterator curve_begin() { return curves.begin(); }
157  curve_iterator curve_end() { return curves.end(); }
158 
159  int numberOfCurves() const { return curves.size(); }
160  virtual int numberOfVertices() = 0;
161  virtual int numberOfElements() = 0;
162  virtual ArraySimpleFixed<int,4,1,1,1> getElement(int e) = 0;
163 
164  // return the Region as an unstructured mesh
165  virtual Mapping &getMapping()=0;
166 
167  virtual ArraySimple<int> & getGridIDList() = 0;
168  virtual ArraySimple<real> & getVertices() = 0;
169  virtual void resetIDList() { gridIDList = ArraySimple<int>(); }
170 
172  const SquareMapping &getReferenceGrid() const { return referenceGrid; }
173 
174  real getDx() const { return dxRef; }
175  real getDy() const { return dyRef; }
176  void setDx(real d) { dxRef=d; resetReferenceGrid(); }
177  void setDy(real d) { dyRef=d; resetReferenceGrid(); }
178 
179  std::string getName() const { return name; }
180  void setName(std::string nm) { name=nm; }
181 
182  int ID() const { return id; }
183 
184 protected:
185  // bounds are usually obtained from the curve bounding boxes
186  void specifyReferenceGridBounds(real x1, real y1, real x2, real y2);
187  void resetReferenceGrid();
188 
191 
192 private:
193 
194  std::vector<Curve*> curves;
195  // ArraySimple<int> gridIDList;
196  // ArraySimple<int> mask;
197  SquareMapping referenceGrid;
198  std::string name;
199  int id;
200 
201  static int idCounter;
202 
203 };
204 
206 class TFIRegion : public Region {
207 public:
208  TFIRegion(real dx=.1, real dy=.1) : Region(dx,dy), tfi_built(false) { }
210 
211  virtual Mapping & getMapping();
212  virtual ArraySimple<int> & getGridIDList();
213  virtual ArraySimple<real> & getVertices();
214 
215  virtual int numberOfVertices();
216  virtual int numberOfElements();
217 
219 
220 protected:
221  bool tfiUpToDate() const { return tfi_built; }
223 
224 private:
225  TFIMapping tfi;
226  UnstructuredMapping umap;
227  bool tfi_built;
228 };
229 
231 class UnstructuredRegion : public Region {
232 public:
233  UnstructuredRegion(real dx=.1, real dy=.1) :
234  Region(dx,dy), umap_built(false), use_cutout(true) { }
236 
237  virtual Mapping &getMapping();
238  virtual ArraySimple<int> & getGridIDList();
239  virtual ArraySimple<real> & getVertices();
240 
241  virtual int numberOfVertices();
242  virtual int numberOfElements();
243 
244  void useCutout() { use_cutout = true; }
245  void dontUseCutout() { use_cutout = false; }
246 
248 
249 protected:
250  bool umapUpToDate() const { return umap_built; }
253 
254 private:
255  UnstructuredMapping umap;
256  bool umap_built;
257  bool use_cutout;
258 };
259 
261 class SimpleCurve : public Curve {
262 public :
263  SimpleCurve() : Curve(), localGrid(),discretized(false), nPoints(-1) { }
264  SimpleCurve(const NurbsMapping &nurb, int ps, int pe) :
265  Curve(ps,pe), nurbRep(nurb), localGrid(),
266  discretized(false), nPoints(-1), stretching(0)
267  { }
268  virtual ~SimpleCurve() { if ( stretching ) delete stretching; }
269 
270  virtual NurbsMapping &getNurbs() { return nurbRep; }
271 
272  // get the range bound for the curve
273  virtual real getRangeBound(int side, int axis)
274  { return nurbRep.getRangeBound(side,axis); }
275 
276  virtual void discretize(real dxRef, real dyRef, ArraySimple<real> &grid);
277 
278  virtual ArraySimple<int> & getGridIDList();
279  virtual ArraySimple<real> & getVertices();
280 
281  virtual int numberOfPoints()
282  {
283  if ( !discretized && Curve::region_1() )
284  discretize(Curve::region_1()->getDx(), Curve::region_1()->getDy(), localGrid);
285 
286  return localGrid.size() ? localGrid.size(0) : nPoints>1 ? nPoints : 0 ;
287  }
288 
289  virtual void region_1(Region *r) { discretized=false; Curve::region_1(r); }
290  virtual void region_2(Region *r) { Curve::region_2(r); }
291 
292  // virtual void computeGridIDs(ArraySimple<int> &gridID);
293 
294  virtual void setNumberOfPoints(int n);
295  virtual bool autoGridSize() const { return nPoints<2; }
296 
297  virtual void deleteStretching();
298  virtual void stretchPoints();
299 
300 private:
301  NurbsMapping nurbRep;
302  ArraySimple<real> localGrid;
303  bool discretized;
304  int nPoints;
305 
306  StretchTransform *stretching;
307 };
308 
310 // vertex ids are specified by the component curves
311 class CompositeCurve : public Curve {
312 public:
313  CompositeCurve() : Curve(), localGrid() { }
314  virtual ~CompositeCurve() { }
315 
316  virtual NurbsMapping &getNurbs() { return nurbRep; }
317 
318  // get the range bound for the curve
319  virtual real getRangeBound(int side, int axis)
320  {
321  real b=0;
322  if ( side==0 )
323  {
324  b=REAL_MAX;
325  for ( std::vector<Curve *>::iterator c=curves.begin(); c!=curves.end(); c++ )
326  b=min(b,(*c)->getRangeBound(side,axis));
327  }
328  else
329  {
330  b=-REAL_MAX;
331  for ( std::vector<Curve *>::iterator c=curves.begin(); c!=curves.end(); c++ )
332  b=max(b,(*c)->getRangeBound(side,axis));
333  }
334 
335  return b;
336  }
337 
338  virtual void discretize(real dxRef, real dyRef, ArraySimple<real> &grid);
339 
340  virtual ArraySimple<int> & getGridIDList();
341  virtual ArraySimple<real> & getVertices();
342 
343  virtual int numberOfPoints();
344 
345  virtual void region_1(Region *r);
346  virtual void region_2(Region *r);
347 
348  virtual void unbindRegion(Region *r);
349 
350  typedef std::vector<Curve*>::iterator curve_iterator;
351  curve_iterator curve_begin() { return this->curves.begin(); }
352  curve_iterator curve_end() { return this->curves.end(); }
353  int curve_size() const { return this->curves.size(); }
354 
355  bool push(Curve *c);
356  Curve *pop();
357 
358 private:
359  NurbsMapping nurbRep;
360 
361  std::vector<Curve *> curves;
362  std::vector<bool> reverse;
363  ArraySimple<real> localGrid;
364 };
365 
366 //inline double pow(double d, int p) { return pow( d, (double)p); }
367 #endif