Package sfc :: Package dofcode :: Module Ptv
[hide private]
[frames] | no frames]

Source Code for Module sfc.dofcode.Ptv

  1   
  2  header = """ 
  3  #ifndef SFC_PTV_IS_INCLUDED 
  4  #define SFC_PTV_IS_INCLUDED 
  5   
  6  #include <functional> 
  7  #include <iostream> 
  8   
  9   
 10  namespace sfc 
 11  { 
 12   
 13    class Ptv 
 14    { 
 15    public: 
 16      unsigned int dim; 
 17      double* v; 
 18     
 19      static double tol; 
 20   
 21    public: 
 22       
 23      Ptv(); 
 24       
 25      Ptv(unsigned int size_); 
 26       
 27      Ptv(unsigned int size_, double* v_); 
 28       
 29      Ptv(double x, double y); 
 30       
 31      Ptv(double x, double y, double z); 
 32       
 33      Ptv(const Ptv& p); 
 34       
 35      ~Ptv(); 
 36   
 37      void redim(unsigned int size_, double *v_); 
 38       
 39      void redim(unsigned int size_);  
 40       
 41      void fill(double *v_); 
 42   
 43      const unsigned int size() const 
 44      { return dim; } 
 45   
 46      double* getPtr() 
 47      { return v; } 
 48   
 49      const double& operator [] (unsigned int i) const; 
 50       
 51      double& operator [] (unsigned int i); 
 52       
 53      Ptv& operator = (const Ptv& p); 
 54       
 55      bool less(const Ptv& p) const; 
 56   
 57      bool operator<(const Ptv& rh) const 
 58      { return less(rh); } 
 59   
 60  //    friend std::ostream & operator<< ( std::ostream& os, const Ptv& p); 
 61    }; 
 62   
 63  /* 
 64    struct Ptv_is_less : public std::binary_function<sfc::Ptv, sfc::Ptv, bool> { 
 65      bool operator() (const sfc::Ptv &lh, const sfc::Ptv &rh) const { return lh.less(rh);  } 
 66    }; 
 67  */ 
 68   
 69  /* 
 70    class Ptv_match : public std::unary_function<sfc::Ptv, bool> { 
 71    protected: 
 72      static double tol; 
 73      unsigned int d; 
 74      double v; 
 75    public: 
 76      Ptv_match(); 
 77      Ptv_match(unsigned int d_, double v_); 
 78      virtual ~Ptv_match() {} 
 79      bool operator() (const Ptv &p); 
 80    }; 
 81  */ 
 82   
 83  } // namespace sfc 
 84   
 85  std::ostream & operator<< ( std::ostream& os, const sfc::Ptv& p); 
 86   
 87  #endif 
 88   
 89  """ 
 90   
 91   
 92   
 93   
 94  implementation = """ 
 95  #include "Ptv.h" 
 96  #include <iostream> 
 97  #include <math.h> 
 98  #include <stdexcept> 
 99   
100   
101  using std::ostream; 
102  using std::endl; 
103  using namespace sfc; 
104   
105   
106  double Ptv::tol = 1.0e-9; 
107  //double Ptv_match::tol = 1.0e-9; 
108  //double Ptv::tol = .0; 
109  //double Ptv_match::tol = .0; 
110   
111  Ptv::Ptv(): 
112    dim(0) 
113  { 
114    v = new double[0]; 
115  } 
116   
117  Ptv::Ptv(double x, double y): 
118    dim(2) 
119  { 
120    v = new double[dim]; 
121    v[0] = x; 
122    v[1] = y; 
123  } 
124   
125  Ptv::Ptv(double x, double y, double z): 
126    dim(3) 
127  { 
128    v = new double[dim]; 
129    v[0] = x; 
130    v[1] = y; 
131    v[2] = z; 
132  } 
133   
134  Ptv::Ptv(unsigned int size_): 
135    dim(size_) 
136  { 
137    v = new double[dim]; 
138    for (unsigned int i=0; i< dim; ++i) 
139    { 
140      v[i] = 0.0; 
141    } 
142  } 
143   
144   
145  // FIXME:  
146  // The constructor which takes unsigned int, double* could/should work 
147  // on the double* provided instead of creating a copy.   
148  // This however affects the destructor. Since Ptv should 
149  // not delete memory.      
150  // We could introduce a bool external_storage in Ptv which  
151  // is used as a test in the destructor.  
152   
153  Ptv::Ptv(unsigned int size_, double* v_): 
154    dim(size_)  
155  { 
156    v = new double[dim]; 
157    for (unsigned int i=0; i< dim; ++i) 
158    { 
159      v[i] = v_[i]; 
160    } 
161  } 
162   
163  Ptv::Ptv(const Ptv& p) 
164  { 
165    dim = p.size(); 
166    v = new double[dim]; 
167    for (unsigned int i=0; i< dim; ++i)  
168    { 
169      v[i] = p[i]; 
170    } 
171  } 
172   
173  Ptv::~Ptv() 
174  { 
175    delete [] v; 
176  } 
177   
178  void Ptv::redim(unsigned int size, double* v_) 
179  { 
180    if (dim != size ) 
181    { 
182      delete [] v; 
183      dim = size; 
184      v = new double[dim]; 
185    } 
186   
187    for (unsigned int i=0; i< dim; ++i) 
188    { 
189      v[i] = v_[i]; 
190    } 
191  } 
192   
193  void Ptv::redim(unsigned int size)  
194  { 
195    if (dim != size ) 
196    { 
197      delete [] v; 
198      dim = size; 
199      v = new double[dim]; 
200    } 
201    for (unsigned int i=0; i< dim; ++i) 
202    { 
203      v[i] = 0.0;   
204    } 
205  } 
206   
207  void Ptv::fill(double* v_) 
208  { 
209    for (unsigned int i=0; i< dim; ++i) 
210    { 
211      v[i] = v_[i]; 
212    } 
213  } 
214   
215  const double& Ptv::operator [] (unsigned int i) const 
216  { 
217    // FIXME: should be possible to turn off at compile time 
218    if ( i < 0 || i >= dim ) 
219    { 
220      throw std::out_of_range("The index is out of range!"); 
221    } 
222    return v[i]; 
223  } 
224   
225  double& Ptv::operator [] (unsigned int i) 
226  { 
227    // FIXME: should be possible to turn off at compile time 
228    if ( i < 0 || i >= dim ) 
229    { 
230      throw std::out_of_range("The index is out of range!"); 
231    } 
232    return v[i]; 
233  } 
234   
235  Ptv& Ptv::operator = (const Ptv& p) 
236  { 
237    if ( this != &p) 
238    { 
239      if ( dim != p.dim) 
240      { 
241        delete [] v; 
242        dim = p.dim; 
243        v = new double[dim]; 
244      } 
245      for (unsigned int i=0; i< dim; ++i) 
246      { 
247        v[i] = p[i]; 
248      } 
249    } 
250    return *this; 
251  } 
252   
253  inline bool less_with_tol(double a, double b, double tol) 
254  { 
255    return a + tol < b - tol; 
256  } 
257   
258  bool Ptv::less(const Ptv& p) const 
259  { 
260    /* 
261    if ( dim < p.dim ) return true; 
262    if ( dim > p.dim)  return false; 
263    */ 
264    if ( dim != p.dim ) 
265    { 
266      throw std::runtime_error("Non-matching dimensions!"); 
267    } 
268  /* 
269    for (unsigned int i=dim-1; i>= 0; i--) 
270    { 
271      if ( v[i] + tol >= p[i] - tol &&  v[i] - tol <= p[i] + tol ) 
272      { 
273      } 
274      else if (v[i] + tol  < p[i] - tol  ) 
275      { 
276          return true; 
277      } 
278      else if ( v[i] - tol > p[i] + tol  ) 
279      {  
280          return false; 
281      } 
282    } 
283  */ 
284    for (unsigned int i=0; i<dim; i++) 
285    { 
286      bool a_less_b = less_with_tol(v[i], p[i], tol); 
287      bool b_less_a = less_with_tol(p[i], v[i], tol); 
288   
289      if ( a_less_b ) 
290      { 
291          return true; 
292      } 
293      else if ( b_less_a  ) 
294      {  
295          return false; 
296      } 
297    } 
298   
299    return false; 
300  } 
301   
302   
303   
304  ostream & operator<< ( ostream& os, const Ptv& p) 
305  { 
306    if (p.dim >= 1) 
307    { 
308      os << "["; 
309      for (unsigned int i=0; i<p.dim-1; ++i) 
310      { 
311        os << p[i] << ","; 
312      } 
313      os << p[p.dim-1] << "]"; 
314    } 
315    else 
316    { 
317      os << "Ptv not created properly"; 
318    } 
319    return os; 
320  } 
321   
322   
323   
324  /* 
325  Ptv_match::Ptv_match() { 
326    d = 0 ; v = 0.0; 
327  } 
328   
329  Ptv_match::Ptv_match(unsigned int d_, double v_) { 
330    d = d_ ; v = v_; 
331  } 
332   
333  bool Ptv_match:: operator () (const Ptv &p) { 
334    if ( v + tol >= p[d] && v - tol <= p[d] ) return true; 
335    else return false; 
336  } 
337  */ 
338   
339  """ 
340