CMS 3D CMS Logo

LinearGridInterpolator3D.h

Go to the documentation of this file.
00001 #ifndef Interpolation_LinearGridInterpolator3D_h
00002 #define Interpolation_LinearGridInterpolator3D_h
00003 
00013 #include "MagneticField/VolumeGeometry/interface/MagExceptions.h"
00014 
00015 #ifdef DEBUG_LinearGridInterpolator3D
00016 #include <iostream>
00017 using namespace std;
00018 #include "MagneticField/Interpolation/src/InterpolationDebug.h"
00019 #endif
00020 
00021 template <class Value, class T>
00022 class LinearGridInterpolator3D {
00023 public:
00024 
00025   typedef T  Scalar;
00026 
00027   LinearGridInterpolator3D( const Grid3D<Value,T>& g) :
00028     grid(g), grida(g.grida()), gridb(g.gridb()), gridc(g.gridc()) {}
00029 
00030   Value interpolate( Scalar a, Scalar b, Scalar c) {
00031     int i = grida.index(a);
00032     int j = gridb.index(b);
00033     int k = gridc.index(c);
00034 
00035     if (i==-1 || j==-1 || k==-1) {
00036       // point outside of grid validity!
00037       throw GridInterpolator3DException( grida.lower(),gridb.lower(),gridc.lower(),
00038                                          grida.upper(),gridb.upper(),gridc.upper());
00039     }
00040 
00041     Scalar s = (a - grida.node(i)) / grida.step();
00042     Scalar t = (b - gridb.node(j)) / gridb.step();
00043     Scalar u = (c - gridc.node(k)) / gridc.step();
00044 
00045 #ifdef DEBUG_LinearGridInterpolator3D
00046     if (InterpolationDebug::debug) {
00047       cout << "LinearGridInterpolator3D called with a,b,c " << a << "," << b << "," << c << endl;
00048       cout <<" i,j,k = " << i << "," << j << "," << k 
00049            << " s,t,u = " << s << "," << t << "," << u << endl;
00050       cout << "Node positions for " << i << "," << j << "," << k << " : "
00051            << grida.node(i) << "," << gridb.node(j) << "," << gridc.node(k) << endl;
00052       cout << "Node positions for " << i+1 << "," << j+1 << "," << k+1 << " : "
00053            << grida.node(i+1) << "," << gridb.node(j+1) << "," << gridc.node(k+1) << endl;
00054       cout << "Grid(" << i << "," << j << "," << k << ") = " << grid(i,  j,  k) << " ";
00055       cout << "Grid(" << i << "," << j << "," << k+1 << ") = " << grid(i,j,k+1) << endl;
00056       cout << "Grid(" << i << "," << j+1 << "," << k << ") = " << grid(i,j+1,k) << " ";
00057       cout << "Grid(" << i << "," << j+1 << "," << k+1 << ") = " << grid(i,j+1,k+1) << endl;
00058       cout << "Grid(" << i+1 << "," << j << "," << k << ") = " << grid(i+1,j,k) << " ";
00059       cout << "Grid(" << i+1 << "," << j << "," << k+1 << ") = " << grid(i+1,j,k+1) << endl;
00060       cout << "Grid(" << i+1 << "," << j+1 << "," << k << ") = " << grid(i+1,j+1,k) << " ";
00061       cout << "Grid(" << i+1 << "," << j+1 << "," << k+1 << ") = " << grid(i+1,j+1,k+1) << endl;
00062       cout << "number of nodes: " << grida.nodes() << "," << gridb.nodes() << "," << gridc.nodes() << endl;
00063 
00064 //     cout << (1-s)*(1-t)*(1-u)*grid(i,  j,  k) << " " << (1-s)*(1-t)*u*grid(i,  j,  k+1) << endl 
00065 //       << (1-s)*   t *(1-u)*grid(i,  j+1,k) << " " << (1-s)*   t *u*grid(i,  j+1,k+1) << endl 
00066 //       << s    *(1-t)*(1-u)*grid(i+1,j,  k) << " " <<  s    *(1-t)*u*grid(i+1,j,  k+1) << endl 
00067 //       << s    *   t *(1-u)*grid(i+1,j+1,k) << " " << s    *   t *u*grid(i+1,j+1,k+1) << endl;
00068     }
00069 
00070 #endif
00071 
00072     Value result = 
00073       (1-s)*(1-t)*(1-u)*grid(i,  j,  k) + (1-s)*(1-t)*u*grid(i,  j,  k+1) + 
00074       (1-s)*   t *(1-u)*grid(i,  j+1,k) + (1-s)*   t *u*grid(i,  j+1,k+1) +
00075       s    *(1-t)*(1-u)*grid(i+1,j,  k) + s    *(1-t)*u*grid(i+1,j,  k+1) + 
00076       s    *   t *(1-u)*grid(i+1,j+1,k) + s    *   t *u*grid(i+1,j+1,k+1);
00077 
00078     return result;
00079   }
00080 
00081   //  Value operator()( Scalar a, Scalar b, Scalar c) {return interpolate(a,b,c);}
00082 
00083 private:
00084   const Grid3D<Value,T>& grid;
00085   const Grid1D<T>& grida;
00086   const Grid1D<T>& gridb;
00087   const Grid1D<T>& gridc;
00088 
00089 };
00090 
00091 #endif

Generated on Tue Jun 9 17:40:34 2009 for CMSSW by  doxygen 1.5.4