Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
window2d.hh
1 // Copyright (C) 2007, 2008, 2009, 2010 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_CORE_ALIAS_WINDOW2D_HH
28 # define MLN_CORE_ALIAS_WINDOW2D_HH
29 
34 
35 # include <mln/core/window.hh>
36 # include <mln/core/alias/dpoint2d.hh>
37 # include <mln/metal/math/sqrt.hh>
38 # include <mln/convert/from_to.hxx>
39 
40 
41 namespace mln
42 {
43 
48  //
50 
53 
65  const window2d& win_c4p();
66 
69 
81  const window2d& win_c8p();
82 
83 
84  namespace convert
85  {
86 
87  namespace over_load
88  {
89 
90  template <unsigned S>
91  void from_to_(const bool (&values)[S], window2d& win);
92 
93  template <unsigned R, unsigned C>
94  void from_to_(const bool (&values)[R][C], window2d& win);
95 
96  } // end of namespace mln::convert::over_load
97 
98  } // end of namespace mln::convert
99 
100 
101 
102 # ifndef MLN_INCLUDE_ONLY
103 
104  inline const window2d&
106  {
107  static window2d it;
108  if (it.size() == 0)
109  {
110  it
111  .insert( 0, -1)
112  .insert(-1, 0)
113  .insert( 0, 0)
114  .insert(+1, 0)
115  .insert( 0, +1);
116  }
117  return it;
118  }
119 
120  inline const window2d&
122  {
123  static window2d it;
124  if (it.size() == 0)
125  {
126  it
127  .insert(-1, -1)
128  .insert( 0, -1)
129  .insert(+1, -1)
130  .insert(-1, 0)
131  .insert( 0, 0)
132  .insert(+1, 0)
133  .insert(-1, +1)
134  .insert( 0, +1)
135  .insert(+1, +1);
136  }
137  return it;
138  }
139 
140 
141  namespace convert
142  {
143 
144  namespace over_load
145  {
146 
147  template <unsigned S>
148  void
149  from_to_(const bool (&values)[S], window2d& win)
150  {
151  enum { H = mlc_sqrt_int(S) / 2 };
152  mlc_bool((2 * H + 1) * (2 * H + 1) == S)::check();
153  win.clear();
154  unsigned i = 0;
155  const def::coord
156  h = static_cast<def::coord>(H),
157  _h = static_cast<def::coord>(-h);
158  for (def::coord row = _h; row <= h; ++row)
159  for (def::coord col = _h; col <= h; ++col)
160  if (values[i++])
161  win.insert(row, col);
162  }
163 
164  template <unsigned R, unsigned C>
165  void
166  from_to_(const bool (&values)[R][C], window2d& win)
167  {
168  mlc_bool(R % 2 == 1)::check();
169  mlc_bool(C % 2 == 1)::check();
170  win.clear();
171  const def::coord
172  drow = static_cast<def::coord>(R / 2),
173  _drow = static_cast<def::coord>(- drow),
174  dcol = static_cast<def::coord>(C / 2),
175  _dcol = static_cast<def::coord>(- dcol);
176  for (def::coord row = _drow; row <= drow; ++row)
177  for (def::coord col = _dcol; col <= dcol; ++col)
178  if (values[row + drow][col + dcol])
179  win.insert(row, col);
180  }
181 
182  } // end of namespace mln::convert::over_load
183 
184  } // end of namespace mln::convert
185 
186 # endif // ! MLN_INCLUDE_ONLY
187 
188 } // end of namespace mln
189 
190 
191 #endif // ! MLN_CORE_ALIAS_WINDOW2D_HH