$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
accu/shape/bbox.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_ACCU_SHAPE_BBOX_HH
27 # define MLN_ACCU_SHAPE_BBOX_HH
28 
32 
33 # include <mln/core/site_set/box.hh>
34 # include <mln/core/concept/meta_accumulator.hh>
35 # include <mln/accu/internal/base.hh>
36 
37 
38 namespace mln
39 {
40 
41  namespace accu
42  {
43 
44  namespace shape
45  {
46 
47 
53  //
54  template <typename P>
55  struct bbox : public mln::accu::internal::base< const box<P>& , bbox<P> >
56  {
57  typedef P argument;
58 
59  bbox();
60 
63  void init();
64  void take_as_init_(const P& p);
65  void take(const P& p);
66  void take(const bbox<P>& other);
67  void take(const box<P>& b);
69 
71  const box<P>& to_result() const;
72 
75  bool is_valid() const;
76 
77  protected:
78 
79  bool is_valid_;
81  };
82 
83 
84  } // end of namespace mln::accu::shape
85 
86 
87  namespace meta
88  {
89 
90  namespace shape
91  {
92 
94  struct bbox : public Meta_Accumulator< bbox >
95  {
96  template <typename T>
97  struct with
98  {
100  };
101  };
102 
103  } // end of namespace mln::accu::meta::shape
104 
105  } // end of namespace mln::accu::meta
106 
107 
108 
109 # ifndef MLN_INCLUDE_ONLY
110 
111  namespace shape
112  {
113 
114  template <typename P>
115  inline
116  bbox<P>::bbox()
117  {
118  init();
119  }
120 
121  template <typename P>
122  inline
123  void
124  bbox<P>::init()
125  {
126  is_valid_ = false;
127  }
128 
129  template <typename P>
130  inline
131  void
132  bbox<P>::take_as_init_(const P& p)
133  {
134  b_.pmin() = p;
135  b_.pmax() = p;
136  is_valid_ = true;
137  }
138 
139  template <typename P>
140  inline
141  void
142  bbox<P>::take(const P& p)
143  {
144  if (! is_valid_)
145  {
146  b_.pmin() = p;
147  b_.pmax() = p;
148  is_valid_ = true;
149  return;
150  }
151  for (unsigned i = 0; i < P::dim; ++i)
152  if (p[i] < b_.pmin()[i])
153  b_.pmin()[i] = p[i];
154  else if (p[i] > b_.pmax()[i])
155  b_.pmax()[i] = p[i];
156  }
157 
158  template <typename P>
159  inline
160  void
161  bbox<P>::take(const bbox<P>& other)
162  {
163  if (! other.is_valid_)
164  {
165  // no-op
166  return;
167  }
168  if (! this->is_valid_)
169  {
170  // 'other' makes '*this' valid
171  *this = other;
172  is_valid_ = true;
173  return;
174  }
175  // both are valids so:
176  const box<P>& o_b = other.b_;
177  for (unsigned i = 0; i < P::dim; ++i)
178  {
179  if (o_b.pmin()[i] < b_.pmin()[i])
180  b_.pmin()[i] = o_b.pmin()[i];
181  if (o_b.pmax()[i] > b_.pmax()[i])
182  b_.pmax()[i] = o_b.pmax()[i];
183  }
184  }
185 
186  template <typename P>
187  inline
188  void
189  bbox<P>::take(const box<P>& b)
190  {
191  if (! b.is_valid())
192  {
193  // no-op
194  return;
195  }
196  if (! this->is_valid_)
197  {
198  b_ = b;
199  is_valid_ = true;
200  return;
201  }
202  // both are valids so:
203  for (unsigned i = 0; i < P::dim; ++i)
204  {
205  if (b.pmin()[i] < b_.pmin()[i])
206  b_.pmin()[i] = b.pmin()[i];
207  if (b.pmax()[i] > b_.pmax()[i])
208  b_.pmax()[i] = b.pmax()[i];
209  }
210  }
211 
212  template <typename P>
213  inline
214  const box<P>&
215  bbox<P>::to_result() const
216  {
217  // mln_precondition(is_valid_);
218  return b_;
219  }
220 
221  template <typename P>
222  inline
223  bool
224  bbox<P>::is_valid() const
225  {
226  return is_valid_;
227  }
228 
229  } // end of namespace mln::accu::shape
230 
231 # endif // ! MLN_INCLUDE_ONLY
232 
233  } // end of namespace mln::accu
234 
235 } // end of namespace mln
236 
237 
238 #endif // ! MLN_ACCU_SHAPE_BBOX_HH