$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ball.hh
1 // Copyright (C) 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_BALL_HH
27 # define MLN_WIN_BALL_HH
28 
32 
33 # include <mln/core/internal/classical_window_base.hh>
34 # include <mln/core/dpoint.hh>
35 # include <mln/core/site_set/box.hh>
36 # include <mln/fun/i2v/all_to.hh>
37 
38 
39 namespace mln
40 {
41 
42  // Forward declaration.
43  namespace win { template <typename G, typename C> struct ball; }
44 
45 
46  namespace trait
47  {
48 
49  template <typename G, typename C>
50  struct window_< mln::win::ball<G,C> > : classical_window_
51  {
52  };
53 
54  } // end of namespace trait
55 
56 
57 
58  namespace win
59  {
60 
70  template <typename G, typename C>
71  struct ball : public internal::classical_window_base< dpoint<G,C>, ball<G,C> >
72  {
73 
77  ball(unsigned diameter);
78 
80  unsigned diameter() const;
81 
84  unsigned delta_() const;
85 
86  void print_(std::ostream& ostr) const;
87 
88  protected:
89  unsigned diameter_;
90  };
91 
92 
93 
94 # ifndef MLN_INCLUDE_ONLY
95 
96 
97  template <typename G, typename C>
98  inline
99  ball<G,C>::ball(unsigned diameter)
100  : diameter_(diameter)
101  {
102  mln_precondition(diameter % 2 == 1);
103  unsigned
104  radius = diameter / 2,
105  sqr_radius = radius * radius;
106  typedef point<G,C> P;
107  box<P> b(all_to(-radius), all_to(+radius));
108  P O = literal::origin;
109  mln_piter(box<P>) p(b);
110  for_all(p)
111  {
112  unsigned d = 0;
113  for (unsigned i = 0; i < P::dim; ++i)
114  d += p[i] * p[i];
115  if (d <= sqr_radius)
116  this->insert(p - O);
117  }
118  }
119 
120  template <typename G, typename C>
121  inline
122  unsigned ball<G,C>::diameter() const
123  {
124  return diameter_;
125  }
126 
127  template <typename G, typename C>
128  inline
129  unsigned ball<G,C>::delta_() const
130  {
131  return diameter_ / 2;
132  }
133 
134  template <typename G, typename C>
135  inline
136  void
137  ball<G,C>::print_(std::ostream& ostr) const
138  {
139  ostr << "[" << G::dim << "D ball: diameter=" << diameter_ << ']';
140  }
141 
142 # endif // ! MLN_INCLUDE_ONLY
143 
144  } // end of namespace mln::win
145 
146 } // end of namespace mln
147 
148 
149 
150 #endif // ! MLN_WIN_BALL_HH