$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trilinear.hh
1 // Copyright (C) 2008, 2009, 2012 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_FUN_X2V_TRILINEAR_HH
28 # define MLN_FUN_X2V_TRILINEAR_HH
29 
30 # include <mln/core/image/image2d.hh>
31 # include <mln/core/concept/function.hh>
32 # include <mln/fun/internal/selector.hh>
33 # include <mln/convert/to.hh>
34 # include <mln/algebra/vec.hh>
35 
41 
42 namespace mln
43 {
44 
45  namespace fun
46  {
47 
48  namespace x2v
49  {
50 
53  template < typename I >
54  struct trilinear
55  : public fun::internal::selector_<const algebra::vec<3,float>,
56  // 3,float is a dummy parameter (real is n,T)
57  mln_value(I), trilinear<I> >::ret
58  {
59  typedef mln_value(I) result;
60 
61  trilinear(const I& ima);
62 
63  template <typename T>
64  mln_value(I)
65  operator()(const algebra::vec<3,T>& v) const;
66 
67  const I& ima;
68  };
69 
70 
71 # ifndef MLN_INCLUDE_ONLY
72 
73  template <typename I>
74  trilinear<I>::trilinear(const I& ima) : ima(ima)
75  {
76  mlc_bool(I::psite::dim == 3)::check();
77  }
78 
79 
80  template <typename I>
81  template <typename T>
82  mln_value(I)
83  trilinear<I>::operator()(const algebra::vec<3,T>& v) const
84  {
85  typedef mln_sum(mln_value(I)) vsum;
86 
87  double x = v[0]; // row
88  double y = v[1]; // col
89  double z = v[2]; // sli
90 
91  unsigned x1 = math::round<double>(std::floor(x));
92  unsigned x2 = math::round<double>(std::floor(x) + 1);
93  unsigned y1 = math::round<double>(std::floor(y));
94  unsigned y2 = math::round<double>(std::floor(y) + 1);
95  unsigned z1 = math::round<double>(std::floor(z));
96  unsigned z2 = math::round<double>(std::floor(z) + 1);
97 
98  double xd = x - x1;
99  double yd = y - y1;
100  double zd = z - z1;
101 
102  // interpolating in z direction
103  // Following access are supposed valid.
104  vsum i1 = ima(point3d(z1,x1,y1)) * (1 - zd)
105  + ima(point3d(z2,x1,y1)) * zd;
106 
107  vsum i2 = ima(point3d(z1,x1,y2)) * (1 - zd)
108  + ima(point3d(z2,x1,y2)) * zd;
109 
110  vsum j1 = ima(point3d(z1,x2,y1)) * (1 - zd)
111  + ima(point3d(z2,x2,y1)) * zd;
112 
113  vsum j2 = ima(point3d(z1,x2,y2)) * (1 - zd)
114  + ima(point3d(z2,x2,y2)) * zd;
115 
116  // interpolating in y direction
117  vsum w1 = i1 * (1 - yd) + i2 * yd;
118  vsum w2 = j1 * (1 - yd) + j2 * yd;
119 
120  // interpolating in x direction
121  vsum res = w1 * (1 - xd) + w2 * xd;
122 
123  return convert::to<mln_value(I)>(res);
124  }
125 
126 
127 # endif // ! MLN_INCLUDE_ONLY
128 
129  } // end of namespace mln::fun::x2v
130 
131  } // end of namespace mln::fun
132 
133 } // end of namespace mln
134 
135 
136 #endif // ! MLN_FUN_X2V_TRILINEAR_HH