$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
morpho/attribute/volume.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_VOLUME_HH
28 # define MLN_MORPHO_ATTRIBUTE_VOLUME_HH
29 
34 
35 # include <mln/accu/internal/base.hh>
36 # include <mln/math/diff_abs.hh>
37 # include <mln/util/pix.hh>
38 
39 
40 namespace mln
41 {
42 
43  // Forward declaration.
44  namespace morpho {
45  namespace attribute {
46  template <typename I> struct volume;
47  }
48  }
49 
50 
51  // Traits.
52 
53  namespace trait
54  {
55 
56  template <typename I>
57  struct accumulator_< morpho::attribute::volume<I> >
58  {
59  typedef accumulator::has_untake::no has_untake;
60  typedef accumulator::has_set_value::no has_set_value;
61  typedef accumulator::has_stop::no has_stop;
62  typedef accumulator::when_pix::use_v when_pix;
63  };
64 
65  } // end of namespace mln::trait
66 
67 
68  namespace morpho
69  {
70 
71  namespace attribute
72  {
73 
78  template <typename I>
79  struct volume
80  : public mln::accu::internal::base< unsigned , volume<I> >
81  {
82  typedef mln_value(I) argument;
83 
84  volume();
85 
88  void init();
89 
90  void take(const mln_value(I)& v);
91  void take(const util::pix<I>& px);
92  void take(const volume<I>& other);
93 
94  void take_as_init_(const mln_value(I)& v);
95  void take_as_init_(const util::pix<I>& px);
97 
99  unsigned to_result() const;
100 
103  bool is_valid() const;
104 
106  unsigned area() const;
107 
108  protected:
110  mln_value(I) cur_level_;
112  unsigned area_;
114  unsigned volume_;
115  };
116 
117 
118 
119 # ifndef MLN_INCLUDE_ONLY
120 
121  template <typename I>
122  inline
124  {
125  init();
126  }
127 
128  template <typename I>
129  inline
130  void
132  {
133  volume_ = 0;
134  }
135 
136  template <typename I>
137  inline
138  void
139  volume<I>::take(const mln_value(I)& v)
140  {
141  mln_invariant(volume_ != mln_max(unsigned));
142  if (! is_valid())
143  {
144  take_as_init_(v);
145  return;
146  }
147  ++area_;
148  volume_ += 1 + math::diff_abs(v, cur_level_);
149  cur_level_ = v;
150  }
151 
152  template <typename I>
153  inline
154  void
155  volume<I>::take(const util::pix<I>& px)
156  {
157  mln_invariant(volume_ != mln_max(unsigned));
158  take(px.v());
159  }
160 
161  template <typename I>
162  inline
163  void
164  volume<I>::take(const volume<I>& other)
165  {
166  mln_invariant(volume_ != mln_max(unsigned));
167  area_ += other.area_;
168  volume_ +=
169  other.volume_ +
170  other.area_ * math::diff_abs(other.cur_level_, cur_level_);
171  // cur_level_ do not change.
172  }
173 
174  template <typename I>
175  inline
176  void
177  volume<I>::take_as_init_(const mln_value(I)& v)
178  {
179  cur_level_ = v;
180  area_ = 1;
181  volume_ = 1;
182  }
183 
184  template <typename I>
185  inline
186  void
187  volume<I>::take_as_init_(const util::pix<I>& px)
188  {
189  take_as_init_(px.v());
190  }
191 
192  template <typename I>
193  inline
194  unsigned
195  volume<I>::to_result() const
196  {
197  return volume_;
198  }
199 
200  template <typename I>
201  inline
202  unsigned
203  volume<I>::area() const
204  {
205  return area_;
206  }
207 
208  template <typename I>
209  inline
210  bool
211  volume<I>::is_valid() const
212  {
213  return volume_ != 0;
214  }
215 
216 # endif // ! MLN_INCLUDE_ONLY
217 
218  } // end of namespace mln::morpho::attribute
219 
220  } // end of namespace mln::morpho
221 
222 } // end of namespace mln
223 
224 
225 #endif // ! MLN_MORPHO_ATTRIBUTE_VOLUME_HH