$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
l1.hh
1 // Copyright (C) 2007, 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_NORM_L1_HH
28 # define MLN_NORM_L1_HH
29 
34 
35 # include <mln/math/abs.hh>
36 # include <mln/algebra/vec.hh>
37 
38 
39 namespace mln
40 {
41 
42  namespace norm
43  {
44 
48 
49  template <unsigned n, typename C>
50  mln_sum_product(C,C) l1(const C (&vec)[n]);
51 
52  template <unsigned n, typename C>
53  mln_sum_product(C,C) l1(const algebra::vec<n,C>& vec);
55 
59 
60  template <unsigned n, typename C>
61  mln_sum_product(C,C) l1_distance(const C (&vec1)[n], const C (&vec2)[n]);
62 
63  template <unsigned n, typename C>
64  mln_sum_product(C,C) l1_distance(const algebra::vec<n,C>& vec1,
65  const algebra::vec<n,C>& vec2);
67 
68 
69 # ifndef MLN_INCLUDE_ONLY
70 
71  namespace impl
72  {
73  template <unsigned n, typename C, typename V>
74  inline
75  mln_sum_product(C,C)
76  l1_(const V& vec)
77  {
78  typedef mln_sum_product(C,C) M;
79  M m = 0;
80  for (unsigned i = 0; i < n; ++i)
81  {
82  M v_i = static_cast<M>(mln::math::abs(vec[i]));
83  m = static_cast<M>(m + v_i);
84  }
85  return m;
86  }
87 
88  template <unsigned n, typename C, typename V>
89  inline
90  mln_sum_product(C,C)
91  l1_distance_(const V& vec1, const V& vec2)
92  {
93  typedef mln_sum_product(C,C) D;
94  D d = 0;
95  for (unsigned i = 0; i < n; ++i)
96  {
97  D v1_v2 = static_cast<D>(mln::math::abs(vec1[i] - vec2[i]));
98  d = static_cast<D>(d + v1_v2);
99  }
100  return d;
101  }
102 
103  } // end of namespace mln::norm::impl
104 
105 
106  /*----------.
107  | Facades. |
108  `----------*/
109 
110  template <unsigned n, typename C>
111  inline
112  mln_sum_product(C,C)
113  l1(const C (&vec)[n])
114  {
115  return impl::l1_<n, C>(vec);
116  }
117 
118  template <unsigned n, typename C>
119  inline
120  mln_sum_product(C,C)
121  l1(const algebra::vec<n,C>& vec)
122  {
123  return impl::l1_<n, C>(vec);
124  }
125 
126  template <unsigned n, typename C>
127  inline
128  mln_sum_product(C,C)
129  l1_distance(const C (&vec1)[n], const C (&vec2)[n])
130  {
131  return impl::l1_distance_<n, C>(vec1, vec2);
132  }
133 
134  template <unsigned n, typename C>
135  inline
136  mln_sum_product(C,C)
137  l1_distance(const algebra::vec<n,C>& vec1, const algebra::vec<n,C>& vec2)
138  {
139  return impl::l1_distance_<n, C>(vec1, vec2);
140  }
141 
142 # endif // ! MLN_INCLUDE_ONLY
143 
144  } // end of namespace mln::norm
145 
146 } // end of namespace mln
147 
148 
149 #endif // ! MLN_NORM_L1_HH