• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

trilinear.hh

00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_FUN_X2V_TRILINEAR_HH
00027 # define MLN_FUN_X2V_TRILINEAR_HH
00028 
00029 # include <mln/core/image/image2d.hh>
00030 # include <mln/core/concept/function.hh>
00031 # include <mln/fun/internal/selector.hh>
00032 # include <mln/convert/to.hh>
00033 # include <mln/algebra/vec.hh>
00034 
00040 
00041 namespace mln
00042 {
00043 
00044   namespace fun
00045   {
00046 
00047     namespace x2v
00048     {
00049 
00052       template < typename I >
00053       struct trilinear
00054         : public fun::internal::selector_<const algebra::vec<3,float>,
00055                                           // 3,float is a dummy parameter (real is n,T)
00056                                           mln_value(I), trilinear<I> >::ret
00057       {
00058         typedef mln_value(I) result;
00059 
00060         trilinear(const I& ima);
00061 
00062         template <typename T>
00063         mln_value(I)
00064         operator()(const algebra::vec<3,T>& v) const;
00065 
00066         const I& ima;
00067       };
00068 
00069 
00070 # ifndef MLN_INCLUDE_ONLY
00071 
00072       template <typename I>
00073       trilinear<I>::trilinear(const I& ima) : ima(ima)
00074       {
00075         mlc_bool(I::psite::dim == 3)::check();
00076       }
00077 
00078 
00079       template <typename I>
00080       template <typename T>
00081       mln_value(I)
00082       trilinear<I>::operator()(const algebra::vec<3,T>& v) const
00083       {
00084         typedef mln_sum(mln_value(I)) vsum;
00085 
00086         double x = v[0]; // row
00087         double y = v[1]; // col
00088         double z = v[2]; // sli
00089 
00090         math::round<double> f;
00091         unsigned x1 = f(std::floor(x));
00092         unsigned x2 = f(std::floor(x) + 1);
00093         unsigned y1 = f(std::floor(y));
00094         unsigned y2 = f(std::floor(y) + 1);
00095         unsigned z1 = f(std::floor(z));
00096         unsigned z2 = f(std::floor(z) + 1);
00097 
00098         double xd = x - x1;
00099         double yd = y - y1;
00100         double zd = z - z1;
00101 
00102         // interpolating in z direction
00103         // Following access are supposed valid.
00104         vsum i1 = ima(point3d(z1,x1,y1)) * (1 - zd)
00105                 + ima(point3d(z2,x1,y1)) * zd;
00106 
00107         vsum i2 = ima(point3d(z1,x1,y2)) * (1 - zd)
00108                 + ima(point3d(z2,x1,y2)) * zd;
00109 
00110         vsum j1 = ima(point3d(z1,x2,y1)) * (1 - zd)
00111                 + ima(point3d(z2,x2,y1)) * zd;
00112 
00113         vsum j2 = ima(point3d(z1,x2,y2)) * (1 - zd)
00114                 + ima(point3d(z2,x2,y2)) * zd;
00115 
00116         // interpolating in y direction
00117         vsum w1 = i1 * (1 - yd) + i2 * yd;
00118         vsum w2 = j1 * (1 - yd) + j2 * yd;
00119 
00120         // interpolating in x direction
00121         vsum res = w1 * (1 - xd) + w2 * xd;
00122 
00123         return convert::to<mln_value(I)>(res);
00124       }
00125 
00126 
00127 # endif // ! MLN_INCLUDE_ONLY
00128 
00129     } // end of namespace mln::fun::x2v
00130 
00131   } // end of namespace mln::fun
00132 
00133 } // end of namespace mln
00134 
00135 
00136 #endif // ! MLN_FUN_X2V_TRILINEAR_HH

Generated on Tue Oct 4 2011 15:24:45 for Milena (Olena) by  doxygen 1.7.1