$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
octagon2d.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_WIN_OCTAGON2D_HH
27 # define MLN_WIN_OCTAGON2D_HH
28 
32 
33 # include <mln/core/internal/classical_window_base.hh>
34 # include <mln/core/alias/dpoint2d.hh>
35 
36 
37 namespace mln
38 {
39 
41 
42 
43  namespace win
44  {
45 
67  struct octagon2d : public internal::classical_window_base< dpoint2d, octagon2d >
68  {
75  octagon2d(unsigned length);
76 
79  unsigned length() const;
80 
84  unsigned delta_() const;
85 
88  unsigned area() const;
89 
90  void print_(std::ostream& ostr) const;
91 
92  protected:
93  unsigned length_;
94  };
95 
96 
97 
98 # ifndef MLN_INCLUDE_ONLY
99 
100  inline
101  octagon2d::octagon2d(unsigned length)
102  : length_(length)
103  {
104  mln_precondition(length % 6 == 1);
105  const def::coord
106  y = static_cast<def::coord>(length / 6),
107  x = static_cast<def::coord>(y * 2),
108  z = static_cast<def::coord>(y + x);
109  insert(dpoint2d(0, 0));
110  for (def::coord a = 1; a <= x; ++a)
111  for (def::coord b = 0; b <= x; ++b)
112  {
113  def::coord
114  _a = static_cast<def::coord>(-a),
115  _b = static_cast<def::coord>(-b);
116  insert(dpoint2d(a, b));
117  insert(dpoint2d(_b, a));
118  insert(dpoint2d(b, _a));
119  insert(dpoint2d(_a, _b));
120  }
121  const def::coord a_min = static_cast<def::coord>(x + 1);
122  for (def::coord a = a_min; a <= z; ++a)
123  {
124  const def::coord
125  b_min = static_cast<def::coord>(-2 * x + a),
126  b_max = static_cast<def::coord>(2 * x - a);
127  for (def::coord b = b_min; b <= b_max; ++b)
128  {
129  def::coord
130  _a = static_cast<def::coord>(-a),
131  _b = static_cast<def::coord>(-b);
132  insert(dpoint2d(a, b));
133  insert(dpoint2d(a, _b));
134  insert(dpoint2d(_a, b));
135  insert(dpoint2d(_a, _b));
136  insert(dpoint2d(b, a));
137  insert(dpoint2d(b, _a));
138  insert(dpoint2d(_b, a));
139  insert(dpoint2d(_b, _a));
140  }
141  }
142  }
143 
144  inline
145  unsigned octagon2d::length() const
146  {
147  return length_;
148  }
149 
150  inline
151  unsigned octagon2d::delta_() const
152  {
153  return length_ / 2;
154  }
155 
156  inline
157  unsigned octagon2d::area() const
158  {
159  unsigned l = (length_ - 1) / 6;
160  return l * (28 * l + 8) + 4;
161  }
162 
163  inline
164  void octagon2d::print_(std::ostream& ostr) const
165  {
166  ostr << "[octagon2d: length=" << length_ << ']';
167  }
168 
169 # endif // ! MLN_INCLUDE_ONLY
170 
171  } // end of namespace mln::win
172 
173 } // end of namespace mln
174 
175 
176 
177 // When rectangle2d is involved, one surely also wants:
178 # include <mln/win/hline2d.hh>
179 # include <mln/win/vline2d.hh>
180 # include <mln/win/diag2d.hh>
181 # include <mln/win/backdiag2d.hh>
182 
183 #endif // ! MLN_WIN_OCTAGON2D_HH