$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
core/concept/box.hh
1 // Copyright (C) 2007, 2008, 2009, 2011, 2013 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_CORE_CONCEPT_BOX_HH
28 # define MLN_CORE_CONCEPT_BOX_HH
29 
33 
34 # include <mln/core/concept/site_set.hh>
35 
36 
37 namespace mln
38 {
39 
47  template <typename E>
48  struct Box : public Site_Set<E>
49  {
50  /*
51  const site& pmin() const;
52  const site& pmax() const;
53  */
54 
61  unsigned len(unsigned i) const;
62 
63  // Box associated type.
64  typedef const E& q_box;
65 
73  const E& bbox() const;
74 
82  unsigned nsites() const;
83 
85  bool is_empty() const;
86 
87  protected:
88  Box();
89  };
90 
91 
92 
93 
101  template <typename Bl, typename Br>
102  bool operator<=(const Box<Bl>& lhs, const Box<Br>& rhs);
103 
104 
112  template <typename Bl, typename Br>
113  bool operator<(const Box<Bl>& lhs, const Box<Br>& rhs);
114 
115 
116 
117 
118 # ifndef MLN_INCLUDE_ONLY
119 
120  // Box<E>
121 
122  template <typename E>
123  inline
124  const E& Box<E>::bbox() const
125  {
126  return exact(*this);
127  }
128 
129  template <typename E>
130  inline
131  unsigned Box<E>::len(unsigned i) const
132  {
133  return
134  exact(this)->is_valid()
135  ? static_cast<unsigned>(1 + exact(this)->pmax()[i]
136  - exact(this)->pmin()[i])
137  : 0u;
138  }
139 
140  template <typename E>
141  inline
142  Box<E>::Box()
143  {
144  typedef mln_site(E) site;
145  site (E::*m1)() const = & E::pmin;
146  (void) m1;
147  site (E::*m2)() const = & E::pmax;
148  (void) m2;
149  }
150 
151  template <typename E>
152  inline
153  unsigned
154  Box<E>::nsites() const
155  {
156  if (! exact(this)->is_valid())
157  return 0;
158  unsigned count = 1;
159  typedef mln_site(E) P; // Helps g++-3.3.5.
160  for (unsigned i = 0; i < P::dim; ++i)
161  count *= exact(this)->len(i);
162  return count;
163  }
164 
165  template <typename E>
166  inline
167  bool
168  Box<E>::is_empty() const
169  {
170  // A non-valid box is empty.
171  return ! exact(this)->is_valid();
172  }
173 
174 
175  // Operators.
176 
177  template <typename Bl, typename Br>
178  inline
179  bool operator<=(const Box<Bl>& lhs_, const Box<Br>& rhs_)
180  {
181  // FIXME: Same grid!
182  const Bl& lhs = exact(lhs_);
183  const Br& rhs = exact(rhs_);
184  typedef mln_site(Bl) P;
185  for (unsigned i = 0; i < P::dim; ++i)
186  if (lhs.pmin()[i] < rhs.pmin()[i] ||
187  lhs.pmax()[i] > rhs.pmax()[i])
188  return false;
189  return true;
190  }
191 
192  template <typename Bl, typename Br>
193  inline
194  bool operator<(const Box<Bl>& lhs_, const Box<Br>& rhs_)
195  {
196  // FIXME: Same grid!
197  const Bl& lhs = exact(lhs_);
198  const Br& rhs = exact(rhs_);
199  return lhs <= rhs && ! (lhs == rhs);
200  }
201 
202 # endif // ! MLN_INCLUDE_ONLY
203 
204 } // end of namespace mln
205 
206 
207 #endif // ! MLN_CORE_CONCEPT_BOX_HH