00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 00025 00026 #ifndef MLN_CONVERT_TO_WINDOW_HH 00027 # define MLN_CONVERT_TO_WINDOW_HH 00028 00032 00033 # include <set> 00034 00035 # include <mln/core/concept/gdpoint.hh> 00036 # include <mln/core/concept/neighborhood.hh> 00037 # include <mln/core/window.hh> 00038 # include <mln/pw/image.hh> 00039 # include <mln/pw/cst.hh> 00040 # include <mln/metal/is_a.hh> 00041 00042 00043 namespace mln 00044 { 00045 00046 namespace convert 00047 { 00048 00050 template <typename N> 00051 mln_window(N) to_window(const Neighborhood<N>& nbh); 00052 00054 template <typename I> 00055 window<mln_site(I)::dpsite> to_window(const Image<I>& ima); 00056 00058 template <typename S> 00059 window<mln_site(S)::dpsite> to_window(const Site_Set<S>& pset); 00060 00062 template <typename D, typename C> 00063 window<D> to_window(const std::set<D, C>& s); 00064 00065 00066 # ifndef MLN_INCLUDE_ONLY 00067 00068 /*-------------------------------------. 00069 | Neighborhood-to-window conversions. | 00070 `-------------------------------------*/ 00071 00072 template <typename N> 00073 inline 00074 mln_window(N) to_window(const Neighborhood<N>& nbh) 00075 { 00076 return exact(nbh).win(); 00077 } 00078 00079 /*------------------------------. 00080 | Image-to-window conversions. | 00081 `------------------------------*/ 00082 00083 /* FIXME: According to milena/core/concepts/README, windows are 00084 not necessarily based on a set of dpsites. So the current 00085 algorithm won't work on non dpsite-set-based windows. In the 00086 general case (of windows not being a set of dpsites), the 00087 window resulting from this conversion (as well as the iterators 00088 based on such windows!) should depend on the initial 00089 neighborhood (i.e., delegate the actual iteration to the 00090 aggregated neighborhood). When this is fixed, document this in 00091 depth in milena/core/concepts/README. */ 00092 template <typename I> 00093 inline 00094 window<mln_site(I)::dpsite> to_window(const Image<I>& ima_) 00095 { 00096 const I& ima = exact(ima_); 00097 mln_precondition(ima.is_valid()); 00098 // FIXME: Check that ima is binary! 00099 typedef mln_site(I) P; 00100 typedef mln_dpsite(P) D; 00101 00102 window<D> win; 00103 mln_piter(I) p(ima.domain()); 00104 for_all(p) 00105 if (ima(p)) 00106 win.insert(p - P::origin); 00107 return win; 00108 } 00109 00110 00111 /*----------------------------------. 00112 | Point set-to-window conversions. | 00113 `----------------------------------*/ 00114 00115 template <typename S> 00116 inline 00117 window<mln_site(S)::dpsite> to_window(const Site_Set<S>& pset) 00118 { 00119 return to_window(pw::cst(true) | pset); 00120 } 00121 00122 /*---------------------------------. 00123 | std::set-to-window conversions. | 00124 `---------------------------------*/ 00125 00126 template <typename D, typename C> 00127 inline 00128 window<D> to_window(const std::set<D, C>& s) 00129 { 00130 // FIXME: Was: mln::metal::is_a<D, Dpoint>::check(); 00131 // OR mln::metal::is_a<D, Delta_Point_Site>::check(); 00132 mln::metal::is_a<D, Gdpoint>::check(); 00133 window<D> win; 00134 for (typename std::set<D, C>::const_iterator i = s.begin(); 00135 i != s.end(); ++i) 00136 win.insert(*i); 00137 return win; 00138 } 00139 00140 # endif // ! MLN_INCLUDE_ONLY 00141 00142 } // end of namespace mln::convert 00143 00144 } // end of namespace mln 00145 00146 00147 #endif // ! MLN_CONVERT_TO_WINDOW_HH