Milena (Olena)  User documentation 2.0a Id
snake_generic.hh
00001 // Copyright (C) 2007, 2008, 2009, 2010, 2011 EPITA Research and
00002 // Development Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_CANVAS_BROWSING_SNAKE_GENERIC_HH
00028 # define MLN_CANVAS_BROWSING_SNAKE_GENERIC_HH
00029 
00033 
00034 # include <vector>
00035 # include <mln/core/concept/browsing.hh>
00036 
00037 
00038 namespace mln
00039 {
00040 
00041   namespace canvas
00042   {
00043 
00044     namespace browsing
00045     {
00046 
00048 
00076       struct snake_generic_t : public Browsing< snake_generic_t >
00077       {
00078 
00079         // This default constructor is needed for compilation with gcc
00080         // 4.6.0, gcc 4.6.1 and Clang.
00081         snake_generic_t();
00082 
00083         template <typename F>
00084         void operator()(F& f) const;
00085 
00086       };
00087 
00088       extern const snake_generic_t snake_generic;
00089 
00090 # ifndef MLN_INCLUDE_ONLY
00091 
00092 #  ifndef MLN_WO_GLOBAL_VARS
00093 
00094       const snake_generic_t snake_generic;
00095 
00096 #  endif // ! MLN_WO_GLOBAL_VARS
00097 
00098       inline
00099       snake_generic_t::snake_generic_t()
00100       {
00101       }
00102 
00103       template <typename F>
00104       inline
00105       void
00106       snake_generic_t::operator()(F& f) const
00107       {
00108         trace::entering("canvas::browsing::snake_generic");
00109         mln_precondition(f.input.is_valid());
00110 
00111         // p init
00112         f.p = f.input.bbox().pmin();// - f.dps[0];
00113 
00114         std::vector< int > directions(f.moves.size(), 0);
00115         unsigned deph = 0;
00116         unsigned total_deph = f.moves.size() / 2 + 1;
00117 
00118         // initialization
00119         f.init();
00120 
00121         bool first = true;
00122         directions[deph] = 1;
00123         deph = total_deph - 1;
00124 
00125         // Call the move function (for the first point)
00126         (f.*(f.moves[(deph - 1) * 2 - 1 + directions[deph - 1]])) ();
00127         while (deph > 0) // If direction is empty, break
00128         {
00129           mln_assertion(deph <= total_deph);
00130           mln_assertion(deph > 0);
00131           // If f.p is near the border (we ended a direction) -> next child
00132           if (!f.input.domain().has(f.p +
00133                                     f.dps[(deph - 1) * 2 - 1 + directions[deph - 1]]))
00134           {
00135             // Go up the tree
00136             deph--;
00137             if (deph >= 1)
00138               // Change directions
00139               directions[deph] = directions[deph] == 1 ? 0 : 1;
00140             continue;
00141           }
00142 
00143           if (!first)
00144           {
00145             // Move f.p
00146             f.p += f.dps[(deph - 1) * 2 - 1 + directions[deph - 1]];
00147             // Call the move function
00148             (f.*(f.moves[(deph - 1) * 2 - 1 + directions[deph - 1]])) ();
00149           }
00150           else
00151             first = false;
00152 
00153           if (deph != total_deph)
00154           {
00155             // Go down the tree
00156             deph++;
00157             first = true;
00158           }
00159         }
00160 
00161         trace::exiting("canvas::browsing::snake_generic");
00162       }
00163 
00164 # endif // ! MLN_INCLUDE_ONLY
00165 
00166     } // end of namespace mln::canvas::browsing
00167 
00168   } // end of namespace mln::canvas
00169 
00170 } // end of namespace mln
00171 
00172 
00173 #endif // ! MLN_CANVAS_BROWSING_SNAKE_GENERIC_HH
 All Classes Namespaces Functions Variables Typedefs Enumerator