Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
pmin_pmax.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_GEOM_PMIN_PMAX_HH
27 # define MLN_GEOM_PMIN_PMAX_HH
28 
33 
34 # include <utility>
35 
36 # include <mln/core/concept/site_set.hh>
37 # include <mln/core/concept/box.hh>
38 
39 
40 
41 namespace mln
42 {
43 
44  namespace geom
45  {
46 
47 
49  template <typename S>
50  std::pair<mln_site(S), mln_site(S)>
51  pmin_pmax(const Site_Set<S>& s);
52 
53 
56  template <typename S>
57  void
58  pmin_pmax(const Site_Set<S>& s, mln_site(S)& pmin, mln_site(S)& pmax);
59 
60 
63  template <typename I>
64  std::pair<mln_site(I), mln_site(I)>
65  pmin_pmax(const Site_Iterator<I>& p);
66 
67 
70  template <typename I>
71  void
72  pmin_pmax(const Site_Iterator<I>& p, mln_site(I)& pmin, mln_site(I)& pmax);
73 
74 
75 
76 # ifndef MLN_INCLUDE_ONLY
77 
78 
79  // Versions with point iterator.
80 
81  template <typename I>
82  inline
83  void
84  pmin_pmax(const Site_Iterator<I>& p_, mln_site(I)& pmin, mln_site(I)& pmax)
85  {
86  I p = exact(p_); // a copy of p_
87 
88  // init with first point
89  p.start();
90  mln_precondition(p.is_valid());
91  pmin = pmax = p;
92 
93  // update with remaining points
94  typedef mln_site(I) P;
95  for_all_remaining(p)
96  for (unsigned i = 0; i < P::dim; ++i)
97  if (p[i] < pmin[i])
98  pmin[i] = p[i];
99  else if (p[i] > pmax[i])
100  pmax[i] = p[i];
101  }
102 
103  template <typename I>
104  inline
105  std::pair<mln_site(I), mln_site(I)>
107  {
108  typedef mln_site(I) P;
109  std::pair<P, P> tmp;
110  pmin_pmax(p, tmp.first, tmp.second); // Calls the previous version.
111  return tmp;
112  }
113 
114 
115  // Versions with point set.
116 
117  namespace impl
118  {
119 
120  // General case.
121 
122  template <typename S>
123  inline
124  void
125  pmin_pmax_(const Site_Set<S>& s, mln_site(S)& pmin, mln_site(S)& pmax)
126  {
127  mln_piter(S) it(exact(s));
128  pmin_pmax(it, pmin, pmax);
129  }
130 
131  // Box.
132 
133  template <typename B>
134  inline
135  void
136  pmin_pmax_(const Box<B>& b, mln_site(B)& pmin, mln_site(B)& pmax)
137  {
138  pmin = exact(b).pmin();
139  pmax = exact(b).pmax();
140  }
141 
142  } // end of namespace mln::geom::impl
143 
144 
145  template <typename S>
146  inline
147  void
148  pmin_pmax(const Site_Set<S>& s, mln_site(S)& pmin, mln_site(S)& pmax)
149  {
150  mln_precondition(exact(s).nsites() != 0);
151  impl::pmin_pmax_(exact(s), pmin, pmax);
152  }
153 
154  template <typename S>
155  inline
156  std::pair<mln_site(S), mln_site(S)>
158  {
159  mln_precondition(exact(s).nsites() != 0);
160  typedef mln_site(S) P;
161  std::pair<P, P> tmp;
162  pmin_pmax(s, tmp.first, tmp.second); // Calls the previous version.
163  return tmp;
164  }
165 
166 # endif // ! MLN_INCLUDE_ONLY
167 
168  } // end of namespace mln::geom
169 
170 } // end of namespace mln
171 
172 
173 #endif // ! MLN_GEOM_PMIN_PMAX_HH