set_level.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library 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
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_LEVEL_SET_LEVEL_HH
00029 # define OLENA_LEVEL_SET_LEVEL_HH
00030 
00031 # include <oln/core/box.hh>
00032 # include <oln/core/dpoint2d.hh>
00033 
00034 namespace oln {
00035 
00036   namespace level {
00037 
00038 
00081     template <class I>
00082     void
00083     set_level(abstract::image_with_dim<2,I>& inout,
00084               const oln_point_type(I)& p1,
00085               const oln_point_type(I)& p2,
00086               oln_value_type(I) level)
00087     {
00088       int iRow1 = p1.row();
00089       int iCol1 = p1.col();
00090       int iRow2 = p2.row();
00091       int iCol2 = p2.col();
00092       int dRow = ( iRow2 > iRow1 ? iRow2 - iRow1 : iRow1 - iRow2 );
00093       int ddRow = 2 * dRow;
00094       int dCol = ( iCol2 > iCol1 ? iCol2 - iCol1 : iCol1 - iCol2 );
00095       int ddCol = 2 * dCol;
00096       int sRow = ( iRow2 != iRow1 ? ( iRow2 > iRow1 ? 1 : -1 ) : 0 );
00097       int sCol = ( iCol2 != iCol1 ? ( iCol2 > iCol1 ? 1 : -1 ) : 0 );
00098       int i, e;
00099       int iRow = iRow1;
00100       int iCol = iCol1;
00101 
00102       if ( dCol > dRow )
00103         {
00104           e = ddRow - dCol;
00105           for ( i = 0; i < dCol; ++i )
00106             {
00107               inout(iRow, iCol) = level;
00108               while ( e >= 0 )
00109                 {
00110                   iRow += sRow;
00111                   e -= ddCol;
00112                 }
00113               iCol += sCol;
00114               e += ddRow;
00115             }
00116         }
00117       else
00118         {
00119           e = ddCol - dRow;
00120           for ( i = 0; i < dRow; ++i )
00121             {
00122               inout(iRow,iCol) = level;
00123               while ( e >= 0 )
00124                 {
00125                   iCol += sCol;
00126                   e -= ddRow;
00127                 }
00128               iRow += sRow;
00129               e += ddCol;
00130             }
00131         }
00132       inout(iRow,iCol) = level;
00133     }
00134 
00139     template <class I, class BoxType>
00140     void
00141     set_level(abstract::image_with_dim<2,I>& inout,
00142               BoxType& box,
00143               const oln_value_type(I)& level)
00144     {
00145       if (box.card() != 0)
00146         {
00147           dpoint2d drows(box.top().row() - box.bottom().row(), 0);
00148           dpoint2d dcols(0, box.top().col() - box.bottom().col());
00149           set_level(inout, box.top(), box.top() - drows, level);
00150           set_level(inout, box.top(), box.top() - dcols, level);
00151           set_level(inout, box.bottom(), box.bottom() + drows, level);
00152           set_level(inout, box.bottom(), box.bottom() + dcols, level);
00153         }
00154     }
00155 
00156   } // end of namespace level
00157 
00158 } // end of namespace oln
00159 
00160 #endif // ! OLENA_LEVEL_SET_LEVEL_HH

Generated on Thu Apr 15 20:13:14 2004 for Olena by doxygen 1.3.6-20040222