$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
h_vec.hh
1 // Copyright (C) 2007, 2008, 2009, 2010, 2012 EPITA Research and
2 // Development 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_ALGEBRA_H_VEC_HH
28 # define MLN_ALGEBRA_H_VEC_HH
29 
33 
34 # include <mln/algebra/vec.hh>
35 # include <mln/literal/one.hh>
36 
37 
38 namespace mln
39 {
40 
41  // Forward declaration.
42  namespace algebra {
43  template <unsigned d, typename C> class h_vec;
44  }
45 
46 
47  namespace trait
48  {
49 
50  // For unary traits.
51 
52  template < template <class> class Name,
53  unsigned d, typename C >
54  struct set_precise_unary_< Name, algebra::h_vec<d, C> >
55  {
56  typedef mln_trait_unary(Name, C) V;
57  typedef algebra::h_vec<d, V> ret;
58  };
59 
60  // For binary traits.
61 
62  template < template <class, class> class Name,
63  unsigned d, typename C,
64  typename Q >
65  struct set_precise_binary_< Name,
66  algebra::h_vec<d, C>, algebra::h_vec<d, Q> >
67  {
68  typedef mln_trait_binary(Name, C, Q) V;
69  typedef algebra::h_vec<d, V> ret;
70  };
71 
72  template < template <class, class> class Name,
73  unsigned d, typename C,
74  typename S >
75  struct set_precise_binary_< Name,
76  algebra::h_vec<d, C>, mln::value::scalar_<S> >
77  {
78  typedef mln_trait_binary(Name, C, S) V;
79  typedef algebra::h_vec<d, V> ret;
80  };
81 
82  } // end of namespace mln::trait
83 
84 
85 
86  namespace algebra
87  {
88 
89 
93  template <unsigned d, typename C>
94  class h_vec : public vec<d + 1, C>
95  {
96  public:
98  enum { dim = d };
99 
101  h_vec();
103  h_vec(const vec<d+1, C>& other);
104 
105  h_vec& operator=(const vec<d+1, C>& rhs);
106 
108  vec<d,C> to_vec() const;
109  };
110 
111 
112 
113 # ifndef MLN_INCLUDE_ONLY
114 
115  template <unsigned d, typename C>
116  inline
117  h_vec<d,C>::h_vec()
118  {
119  /* Safety measure: set the last component to the unit (1). This
120  way, converting an unitialized h_vec to a vector won't trigger
121  division-by-zero errors if this last component were randomly
122  initialized to 0. */
123  this->data_[d] = literal::one;
124  }
125 
126  template <unsigned d, typename C>
127  inline
128  h_vec<d,C>::h_vec(const vec<d+1, C>& other)
129  : vec<d+1, C>(other)
130  {
131  }
132 
133  template <unsigned d, typename C>
134  inline
135  h_vec<d,C>& h_vec<d,C>::operator=(const vec<d+1, C>& rhs)
136  {
137  if (& rhs == this)
138  return *this;
139  this->vec<d+1, C>::operator=(rhs);
140  return *this;
141  }
142 
143  template <unsigned d, typename C>
144  inline
145  vec<d,C> h_vec<d,C>::to_vec() const
146  {
147  const C w = this->data_[d];
148  mln_assertion(w != 0);
149 
150  vec<d,C> tmp;
151  for (unsigned i = 0; i < d; ++i)
152  tmp[i] = static_cast<C>(this->data_[i] / w);
153  return tmp;
154  }
155 
156  // Immersion of a vector in its homogeneous space.
157 
158  template <unsigned n, typename T>
159  inline
160  h_vec<n, T>
161  vec<n,T>::to_h_vec() const
162  {
163  h_vec<n, T> tmp;
164  for (unsigned i = 0; i < n; ++i)
165  tmp[i] = this->data_[i];
166  tmp[n] = literal::one;
167  return tmp;
168  }
169 
170 # endif // ! MLN_INCLUDE_ONLY
171 
172  } // end of namespace mln::algebra
173 
174 } // end of namespace mln
175 
176 
177 #endif // ! MLN_ALGEBRA_H_VEC_HH