Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
core/concept/neighborhood.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_CORE_CONCEPT_NEIGHBORHOOD_HH
27 # define MLN_CORE_CONCEPT_NEIGHBORHOOD_HH
28 
32 
33 # include <mln/core/concept/window.hh>
34 # include <mln/trait/windows.hh>
35 
36 
37 
38 # define mln_is_simple_neighborhood(N) mln_is_simple_window(mln_window(N))
39 
40 
41 
42 
43 namespace mln
44 {
45 
46 
47  // Forward declaration.
48  template <typename E> struct Neighborhood;
49 
50 
52 
53  template <>
54  struct Neighborhood<void>
55  {
56  typedef Object<void> super;
57  };
58 
59 
64 
65  template <typename E>
66  struct Neighborhood : public Object<E>
67  {
69 
70  /*
71  typedef niter;
72  typedef fwd_niter;
73  typedef bkd_niter;
74 
75  typedef window;
76  either
77  const window& win() const;
78  or
79  window win() const;
80  */
81 
82  protected:
83  Neighborhood();
84  };
85 
86 
87 
88  template <typename L, typename R>
89  bool operator==(const Neighborhood<L>& lhs, const Neighborhood<R>& rhs);
90 
91 
92  template <typename N>
93  std::ostream&
94  operator<<(std::ostream&ostr, const Neighborhood<N>& nbh);
95 
96 
97  // FIXME: Move as a method of Image?
98  template <typename I, typename N>
100  offsets_wrt(const Image<I>& ima, const Neighborhood<N>& nbh);
101 
102  template <typename I, typename N>
104  positive_offsets_wrt(const Image<I>& ima, const Neighborhood<N>& nbh);
105 
106  template <typename I, typename N>
108  negative_offsets_wrt(const Image<I>& ima, const Neighborhood<N>& nbh);
109 
110 
111 
112 # ifndef MLN_INCLUDE_ONLY
113 
114  template <typename E>
115  inline
117  {
118  typedef mln_niter(E) niter;
119  typedef mln_fwd_niter(E) fwd_niter;
120  typedef mln_bkd_niter(E) bkd_niter;
121 
122  typedef mln_window(E) window;
123  bool m = (& E::win) == (& E::win);
124  m = 0;
125  bool (E::*m2)() const = &E::is_valid;
126  m2 = 0;
127 
128 # if 0
129  /* FIXME: Disabled, as win() can either return a const reference
130  or a copy of the window (see documentation above). Hence the
131  simpler, relaxed check above (m0). */
132  const window& (E::*m1)() const = & E::win;
133  m = m1;
134 # endif
135  }
136 
137 
138  template <typename L, typename R>
139  inline
140  bool operator==(const Neighborhood<L>& lhs, const Neighborhood<R>& rhs)
141  {
142  return exact(lhs).win() == exact(rhs).win();
143  }
144 
145  template <typename N>
146  inline
147  std::ostream&
148  operator<<(std::ostream&ostr, const Neighborhood<N>& nbh)
149  {
150  return ostr << exact(nbh).win();
151  }
152 
153  template <typename I, typename N>
154  inline
155  util::array<int>
156  offsets_wrt(const Image<I>& ima_, const Neighborhood<N>& nbh_)
157  {
158  mln_is_simple_neighborhood(N)::check();
159 
160  const I& ima = exact(ima_);
161  const N& nbh = exact(nbh_);
162  mln_precondition(ima.is_valid());
163  mln_precondition(nbh.is_valid());
164 
165  return offsets_wrt(ima, nbh.win());
166  }
167 
168  template <typename I, typename N>
169  util::array<int>
170  positive_offsets_wrt(const Image<I>& ima_, const Neighborhood<N>& nbh_)
171  {
172  mln_is_simple_neighborhood(N)::check();
173 
174  const I& ima = exact(ima_);
175  const N& nbh = exact(nbh_);
176  mln_precondition(ima.is_valid());
177  mln_precondition(nbh.is_valid());
178 
179  return positive_offsets_wrt(ima, nbh.win());
180  }
181 
182  template <typename I, typename N>
183  util::array<int>
184  negative_offsets_wrt(const Image<I>& ima_, const Neighborhood<N>& nbh_)
185  {
186  mln_is_simple_neighborhood(N)::check();
187 
188  const I& ima = exact(ima_);
189  const N& nbh = exact(nbh_);
190  mln_precondition(ima.is_valid());
191  mln_precondition(nbh.is_valid());
192 
193  return negative_offsets_wrt(ima, nbh.win());
194  }
195 
196 # endif // ! MLN_INCLUDE_ONLY
197 
198 } // end of namespace mln
199 
200 
201 #endif // ! MLN_CORE_CONCEPT_NEIGHBORHOOD_HH