CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/MagneticField/Interpolation/src/LinearGridInterpolator3D.cc

Go to the documentation of this file.
00001 #include "LinearGridInterpolator3D.h"
00002 #include "Grid1D.h"
00003 #include "Grid3D.h"
00004 #include "MagneticField/VolumeGeometry/interface/MagExceptions.h"
00005 
00006 void
00007 LinearGridInterpolator3D::throwGridInterpolator3DException(void)
00008 {
00009   throw GridInterpolator3DException(grida.lower(),gridb.lower(),gridc.lower(),
00010                                     grida.upper(),gridb.upper(),gridc.upper());
00011 }
00012 
00013 LinearGridInterpolator3D::ValueType 
00014 LinearGridInterpolator3D::interpolate( Scalar a, Scalar b, Scalar c) 
00015 {
00016   /*
00017   int i = grida.index(a);
00018   int j = gridb.index(b);
00019   int k = gridc.index(c);
00020 
00021   if (i==-1 || j==-1 || k==-1) {
00022     // point outside of grid validity!
00023     throwGridInterpolator3DException();
00024   }
00025 
00026 
00027   Scalar s = (a - grida.node(i)) / grida.step();
00028   Scalar t = (b - gridb.node(j)) / gridb.step();
00029   Scalar u = (c - gridc.node(k)) / gridc.step();
00030 
00031   */
00032 
00033   Scalar s, t, u;
00034   int i = grida.index(a,s);
00035   int j = gridb.index(b,t);
00036   int k = gridc.index(c,u);
00037   
00038   // test range??
00039 
00040   grida.normalize(i,s);
00041   gridb.normalize(j,t);
00042   gridc.normalize(k,u);
00043 
00044 #ifdef DEBUG_LinearGridInterpolator3D
00045   if (InterpolationDebug::debug) {
00046     using std::cout;
00047     using std::endl;
00048     cout << "LinearGridInterpolator3D called with a,b,c " << a << "," << b << "," << c << endl;
00049     cout <<" i,j,k = " << i << "," << j << "," << k
00050          << " s,t,u = " << s << "," << t << "," << u << endl;
00051     cout << "Node positions for " << i << "," << j << "," << k << " : "
00052          << grida.node(i) << "," << gridb.node(j) << "," << gridc.node(k) << endl;
00053     cout << "Node positions for " << i+1 << "," << j+1 << "," << k+1 << " : "
00054          << grida.node(i+1) << "," << gridb.node(j+1) << "," << gridc.node(k+1) << endl;
00055     cout << "Grid(" << i << "," << j << "," << k << ") = " << grid(i,  j,  k) << " ";
00056     cout << "Grid(" << i << "," << j << "," << k+1 << ") = " << grid(i,j,k+1) << endl;
00057     cout << "Grid(" << i << "," << j+1 << "," << k << ") = " << grid(i,j+1,k) << " ";
00058     cout << "Grid(" << i << "," << j+1 << "," << k+1 << ") = " << grid(i,j+1,k+1) << endl;
00059     cout << "Grid(" << i+1 << "," << j << "," << k << ") = " << grid(i+1,j,k) << " ";
00060     cout << "Grid(" << i+1 << "," << j << "," << k+1 << ") = " << grid(i+1,j,k+1) << endl;
00061     cout << "Grid(" << i+1 << "," << j+1 << "," << k << ") = " << grid(i+1,j+1,k) << " ";
00062     cout << "Grid(" << i+1 << "," << j+1 << "," << k+1 << ") = " << grid(i+1,j+1,k+1) << endl;
00063     cout << "number of nodes: " << grida.nodes() << "," << gridb.nodes() << "," << gridc.nodes() << endl;
00064 
00065 //     cout << (1-s)*(1-t)*(1-u)*grid(i,  j,  k) << " " << (1-s)*(1-t)*u*grid(i,  j,  k+1) << endl 
00066 //       << (1-s)*   t *(1-u)*grid(i,  j+1,k) << " " << (1-s)*   t *u*grid(i,  j+1,k+1) << endl 
00067 //       << s    *(1-t)*(1-u)*grid(i+1,j,  k) << " " <<  s    *(1-t)*u*grid(i+1,j,  k+1) << endl 
00068 //       << s    *   t *(1-u)*grid(i+1,j+1,k) << " " << s    *   t *u*grid(i+1,j+1,k+1) << endl;
00069   }
00070 
00071 #endif
00072 
00073   int ind = grid.index(i,j,k);
00074   int s1 = grid.stride1(); 
00075   int s2 = grid.stride2(); 
00076   int s3 = grid.stride3(); 
00077   //chances are this is more numerically precise this way
00078 
00079 
00080 
00081   // this code for test to check  properly inline of wrapped math...
00082 #if defined(CMS_TEST_RAWSSE)
00083 
00084   __m128 resultSIMD =                 _mm_mul_ps(_mm_set1_ps((1.f - s) * (1.f - t) * u), _mm_sub_ps(grid(ind           + s3).v.vec, grid(ind          ).v.vec));
00085   resultSIMD = _mm_add_ps(resultSIMD, _mm_mul_ps(_mm_set1_ps((1.f - s) *         t * u), _mm_sub_ps(grid(ind      + s2 + s3).v.vec, grid(ind      + s2).v.vec)));
00086   resultSIMD = _mm_add_ps(resultSIMD, _mm_mul_ps(_mm_set1_ps(        s * (1.f - t) * u), _mm_sub_ps(grid(ind + s1      + s3).v.vec, grid(ind + s1     ).v.vec)));
00087   resultSIMD = _mm_add_ps(resultSIMD, _mm_mul_ps(_mm_set1_ps(        s *         t * u), _mm_sub_ps(grid(ind + s1 + s2 + s3).v.vec, grid(ind + s1 + s2).v.vec)));
00088   resultSIMD = _mm_add_ps(resultSIMD, _mm_mul_ps(_mm_set1_ps((1.f - s) *         t    ), _mm_sub_ps(grid(ind      + s2     ).v.vec, grid(ind          ).v.vec)));
00089   resultSIMD = _mm_add_ps(resultSIMD, _mm_mul_ps(_mm_set1_ps(        s *         t    ), _mm_sub_ps(grid(ind + s1 + s2     ).v.vec, grid(ind + s1     ).v.vec)));
00090   resultSIMD = _mm_add_ps(resultSIMD, _mm_mul_ps(_mm_set1_ps(        s                ), _mm_sub_ps(grid(ind + s1          ).v.vec, grid(ind          ).v.vec)));
00091   resultSIMD = _mm_add_ps(resultSIMD,                                                                                             grid(ind          ).v.vec);
00092   
00093   ValueType result; result.v=resultSIMD;
00094 
00095 
00096 #else
00097   
00098   ValueType result = ((1.f-s)*(1.f-t)*u)*(grid(ind      +s3) - grid(ind      ));
00099   result =  result + ((1.f-s)*     t *u)*(grid(ind   +s2+s3) - grid(ind   +s2));
00100   result =  result + (s      *(1.f-t)*u)*(grid(ind+s1   +s3) - grid(ind+s1   ));
00101   result =  result + (s      *     t *u)*(grid(ind+s1+s2+s3) - grid(ind+s1+s2)); 
00102   result =  result + (        (1.f-s)*t)*(grid(ind   +s2   ) - grid(ind      ));
00103   result =  result + (      s        *t)*(grid(ind+s1+s2   ) - grid(ind+s1   ));
00104   result =  result + (                s)*(grid(ind+s1      ) - grid(ind      ));
00105   result =  result +                                           grid(ind      );
00106 
00107 
00108 #endif
00109 
00110 
00111 
00112   //      (1-s)*(1-t)*(1-u)*grid(i,  j,  k) + (1-s)*(1-t)*u*grid(i,  j,  k+1) + 
00113   //      (1-s)*   t *(1-u)*grid(i,  j+1,k) + (1-s)*   t *u*grid(i,  j+1,k+1) +
00114   //      s    *(1-t)*(1-u)*grid(i+1,j,  k) + s    *(1-t)*u*grid(i+1,j,  k+1) + 
00115   //      s    *   t *(1-u)*grid(i+1,j+1,k) + s    *   t *u*grid(i+1,j+1,k+1);
00116 
00117 
00118   return result;
00119 
00120 
00121 }