CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/MagneticField/Interpolation/src/Grid1D.h

Go to the documentation of this file.
00001 #ifndef Grid1D_H
00002 #define Grid1D_H
00003 #include <cmath>
00004 #include <algorithm>
00005 
00006 class Grid1D {
00007 public:
00008 
00009   typedef float   Scalar;
00010   //  typedef double Scalar;
00011 
00012   Grid1D() {}
00013 
00014   Grid1D( Scalar lower, Scalar upper, int nodes) : 
00015     lower_(lower), upper_(upper), edges_(nodes-2) {
00016     stepinv_ =  (nodes-1)/(upper - lower);
00017   }
00018 
00019 
00020   Scalar step() const {return 1./stepinv_;}
00021   Scalar lower() const {return lower_;}
00022   Scalar upper() const {return upper_;}
00023   int nodes() const {return  edges_+2;}
00024   int cells() const {return  edges_+1;}
00025 
00026   Scalar node( int i) const { return i*step() + lower();}
00027 
00028   bool inRange(int i) const {
00029     return i>=0 && i<=edges_;
00030   }
00031 
00032   // return index and fractional part...
00033   int index(Scalar a, Scalar & f) const {
00034     Scalar b;
00035     f = modff((a-lower())*stepinv_, &b);
00036     return b;
00037   }
00038 
00039   // move index and fraction in range..
00040   void normalize(int & ind,  Scalar & f) const {
00041     if (ind<0) {
00042       f -= ind;
00043       ind = 0;
00044     }
00045     else if (ind>edges_) {
00046       f += ind-edges_;
00047       ind = edges_;
00048     }
00049   }
00050 
00051 
00052   Scalar closestNode( Scalar a) const {
00053     Scalar b = (a-lower())/step();
00054     Scalar c = floor(b);
00055     Scalar tmp = (b-c < 0.5) ? std::max(c,0.f) : std::min(c+1.f,static_cast<Scalar>(nodes()-1));
00056     return tmp*step()+lower();
00057   }
00058 
00060   int index( Scalar a) const {
00061     int ind = static_cast<int>((a-lower())/step());
00062     // FIXME: this causes an exception to be thrown later. Should be tested
00063     // more carefully before release
00064     //  if (ind < -1 || ind > cells()) {
00065     //     std::cout << "**** ind = " << ind << " cells: " << cells() << std::endl;
00066     //    return -1;
00067     //  }
00068     return std::max(0, std::min( cells()-1, ind));
00069   }
00070 
00071  
00072 private:
00073 
00074   Scalar stepinv_;
00075   Scalar lower_;
00076   Scalar upper_;
00077   int    edges_; // number of lower edges = nodes-2...
00078 
00079 };
00080 
00081 #endif