$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
morpho/attribute/height.hh
1 // Copyright (C) 2007, 2008, 2009, 2011 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_MORPHO_ATTRIBUTE_HEIGHT_HH
28 # define MLN_MORPHO_ATTRIBUTE_HEIGHT_HH
29 
33 
34 # include <mln/accu/internal/base.hh>
35 
36 # include <mln/util/pix.hh>
37 # include <mln/math/diff_abs.hh>
38 # include <mln/math/min.hh>
39 # include <mln/math/max.hh>
40 
41 
42 namespace mln
43 {
44 
45  // Forward declaration.
46  namespace morpho {
47  namespace attribute {
48  template <typename I> struct height;
49  }
50  }
51 
52 
53  // Traits.
54 
55  namespace trait
56  {
57 
58  template <typename I>
59  struct accumulator_< morpho::attribute::height<I> >
60  {
61  typedef accumulator::has_untake::no has_untake;
62  typedef accumulator::has_set_value::no has_set_value;
63  typedef accumulator::has_stop::no has_stop;
64  typedef accumulator::when_pix::use_v when_pix;
65  };
66 
67  } // end of namespace mln::trait
68 
69 
70  namespace morpho
71  {
72 
73  namespace attribute
74  {
75 
80  template <typename I>
81  struct height
82  : public mln::accu::internal::base< unsigned , height<I> >
83  {
84  typedef mln_value(I) argument;
85 
86  height();
87 
90  void init();
91  void take(const mln_value(I)& v);
92  void take(const util::pix<I>& v);
93  void take(const height<I>& other);
94  void take_as_init_(const mln_value(I)& v);
95  void take_as_init_(const util::pix<I>& px);
97 
100  bool is_valid() const;
101 
103  unsigned to_result() const;
104 
106  unsigned base_level() const;
107  unsigned current_level() const;
108 
109 
110  protected:
112  unsigned ref_;
114  unsigned cur_;
117  };
118 
119 
120 # ifndef MLN_INCLUDE_ONLY
121 
122  template <typename I>
123  inline
125  : initialized_ (false)
126  {
127  }
128 
129  template <typename I>
130  inline
131  void
133  {
134  mln_invariant(0);
135  }
136 
137  template <typename I>
138  inline
139  void
140  height<I>::take(const mln_value(I)& v)
141  {
142  if (!is_valid ())
143  {
144  take_as_init_(v);
145  }
146  cur_ = v;
147  }
148 
149  template <typename I>
150  inline
151  void
152  height<I>::take(const util::pix<I>& px)
153  {
154  take(px.v());
155  }
156 
157  template <typename I>
158  inline
159  void
160  height<I>::take(const height<I>& other)
161  {
162  mln_invariant(((ref_ <= cur_) && (other.ref_ <= other.cur_))
163  || ((ref_ >= cur_) && (other.ref_ >= other.cur_)));
164 
165  if (!is_valid())
166  {
167  ref_ = other.ref_;
168  cur_ = other.cur_;
169  }
170  else if (ref_ < cur_)
171  {
172  // Values are increasing.
173  ref_ = math::min(ref_, other.ref_);
174  cur_ = math::max(cur_, other.cur_);
175  }
176  else
177  {
178  // Values are decreasing.
179  ref_ = math::max(ref_, other.ref_);
180  cur_ = math::min(cur_, other.cur_);
181  }
182  }
183 
184  template <typename I>
185  inline
186  void
187  height<I>::take_as_init_(const mln_value(I)& v)
188  {
189  cur_ = ref_ = v;
190  initialized_ = true;
191  }
192 
193  template <typename I>
194  inline
195  void
196  height<I>::take_as_init_(const util::pix<I>& px)
197  {
198  take_as_init_(px.v());
199  }
200 
201 
202  template <typename I>
203  inline
204  unsigned
205  height<I>::to_result() const
206  {
207  mln_invariant(is_valid());
208 
209  return math::diff_abs(ref_, cur_);
210  }
211 
212  template <typename I>
213  inline
214  unsigned
215  height<I>::base_level() const
216  {
217  return ref_;
218  }
219 
220  template <typename I>
221  inline
222  unsigned
224  {
225  return cur_;
226  }
227 
228 
229  template <typename I>
230  inline
231  bool
232  height<I>::is_valid() const
233  {
234  return initialized_;
235  }
236 
237 # endif // ! MLN_INCLUDE_ONLY
238 
239  } // end of namespace mln::morpho::attribute
240 
241  } // end of namespace mln::morpho
242 
243 } // end of namespace mln
244 
245 
246 #endif // ! MLN_MORPHO_ATTRIBUTE_HEIGHT_HH