border_morpher.hh

00001 // Copyright (C) 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, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, 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 BORDER_MORPHER_HH
00029 # define BORDER_MORPHER_HH
00030 
00031 # include <oln/morpher/generic_morpher.hh>
00032 # include <oln/core/abstract/behavior.hh>
00033 # include <oln/core/behavior.hh>
00034 
00035 namespace oln {
00036 
00037   namespace morpher {
00038 
00039     template <class I, class BehaviorType, class Exact = mlc::final>
00040     struct border_morpher;
00041     template <class I, class BehaviorType, class Exact = mlc::final>
00042     struct super_border_morpher;
00043 
00044   } // end of namespace morpher
00045 
00047   template <class SrcType, class BehaviorType, class Exact>
00048   struct image_id< morpher::super_border_morpher<SrcType, BehaviorType, Exact> >
00049   {
00050     typedef typename mlc::exact_vt<
00051       morpher::super_border_morpher<SrcType, BehaviorType, Exact>,
00052       Exact>::ret
00053     exact_type;
00055   };
00056 
00058   template <class SrcType, class BehaviorType, class Exact>
00059   struct image_id< morpher::border_morpher<SrcType, BehaviorType, Exact> >
00060   {
00061     enum {dim = SrcType::dim}; 
00062     typedef oln_impl_type(SrcType) impl_type;
00064     typedef oln_value_type(SrcType) value_type;
00066     typedef typename mlc::exact_vt<morpher::border_morpher<SrcType, BehaviorType, Exact>,
00067                                    Exact>::ret exact_type;
00069     typedef oln_point_type(SrcType) point_type;
00070     typedef oln_dpoint_type(SrcType) dpoint_type;
00071     typedef oln_size_type(SrcType) size_type;
00072     typedef oln_iter_type(SrcType) iter_type;
00073   };
00074 
00076   template <class SrcType, class BehaviorType, class Exact>
00077   struct image_traits < morpher::border_morpher<SrcType, BehaviorType, Exact> >
00078     : public
00079   image_traits<
00080     morpher::abstract::generic_morpher<
00081       SrcType,
00082       typename image_id<morpher::border_morpher<SrcType,
00083                                                 BehaviorType,
00084                                                 Exact> >::exact_type
00085     >
00086   >
00087   {};
00088 
00089 
00090   namespace morpher {
00091 
00093     template <class SrcType, class BehaviorType, class Exact>
00094     class super_border_morpher
00095       : public abstract::generic_morpher<SrcType, Exact>
00096     {
00097 
00098     public:
00099 
00100       typedef super_border_morpher<SrcType, BehaviorType, Exact> self_type;
00101       typedef typename image_id<self_type>::exact_type exact_type;
00102       typedef abstract::generic_morpher<SrcType, Exact> super_type;
00103 
00104       typedef typename image_id<exact_type>::dpoint_type dpoint_type;
00105       typedef typename image_id<exact_type>::size_type size_type;
00106 
00107     protected:
00108 
00115       super_border_morpher(const SrcType &ima,
00116                            const coord width,
00117                            const BehaviorType& be)
00118         : super_type(ima), width(width), be(be)
00119       {
00120         be.adapt_border(ima, width + this->get_ima().border());
00121         for (unsigned i = 0; i < image_id<exact_type>::dim; ++i)
00122         {
00123           size_.nth(i) = this->get_ima().size().nth(i) + 2 * width;
00124           dp_.nth(i) = -width;
00125         }
00126         size_.border() = this->get_ima().size().border();
00127       }
00128 
00129       const coord width; 
00130       const BehaviorType be; 
00131       size_type size_;
00132       dpoint_type dp_;
00133 
00139       super_border_morpher()
00140         : super_type(), width(0), be()
00141       {}
00142 
00143     public:
00144 
00146       const size_type
00147       size() const
00148       {
00149         return size_;
00150       }
00151 
00153       const dpoint_type
00154       get_dp() const
00155       {
00156         return dp_;
00157       }
00158 
00160       const BehaviorType
00161       get_behavior() const
00162       {
00163         return be;
00164       }
00165 
00167       const coord
00168       get_width() const
00169       {
00170         return width;
00171       }
00172 
00174       static std::string
00175       name()
00176       {
00177         return "super_border_morpher<" + super_type::name() + ">";
00178       }
00179 
00180     };
00181 
00190     template <class SrcType, class BehaviorType, class Exact>
00191     struct border_morpher
00192       : public super_border_morpher<
00193           SrcType,
00194           BehaviorType,
00195           typename image_id<border_morpher<SrcType, BehaviorType, Exact> >::exact_type
00196         >
00197     {
00198       typedef border_morpher<SrcType, BehaviorType, Exact> self_type;
00199       typedef typename image_id<self_type>::exact_type exact_type;
00200       typedef super_border_morpher<SrcType, BehaviorType, exact_type> super_type;
00201 
00202       typedef typename image_id<exact_type>::point_type point_type;
00203       typedef typename image_id<exact_type>::dpoint_type dpoint_type;
00204       typedef typename image_id<exact_type>::size_type size_type;
00205       typedef typename image_id<exact_type>::value_type value_type;
00206 
00208       border_morpher(const SrcType &ima,
00209                      const coord width,
00210                      const BehaviorType& be)
00211         : super_type(ima, width, be)
00212       {}
00213 
00215       border_morpher(const self_type& r)
00216         : super_type(r.get_ima(), r.get_width(), r.get_behavior())
00217       {}
00218 
00224       border_morpher()
00225       {}
00226 
00232       value_type&
00233       at(const point_type& p)
00234       {
00235         return const_cast<value_type &>
00236           ( const_cast<SrcType &>(this->ima_)[p + this->get_dp()] );
00237       }
00238 
00244       const value_type
00245       at(const point_type& p) const
00246       {
00247         return this->ima_[p + this->get_dp()];
00248       }
00249 
00254       self_type&
00255       assign(self_type& rhs)
00256       {
00257         oln_iter_type(SrcType)  it(rhs);
00258 
00259         for_all(it)
00260           this->at(it) = rhs[it];
00261         return this->exact();
00262       }
00263 
00267       self_type&
00268       operator=(SrcType& rhs)
00269       {
00270         return this->exact().assign(rhs);
00271       }
00272 
00274       static std::string
00275       name()
00276       {
00277         return "border_morpher<" + super_type::name() + ">";
00278       }
00279 
00280     };
00281 
00283     template <class SrcType, class BehaviorType, class Exact>
00284     struct border_morpher<const SrcType, BehaviorType, Exact>
00285       : public super_border_morpher<
00286           const SrcType,
00287           BehaviorType,
00288           typename image_id<border_morpher<const SrcType,
00289                                            BehaviorType,
00290                                            Exact> >::exact_type
00291         >
00292     {
00293       typedef border_morpher<const SrcType, BehaviorType, Exact> self_type;
00294       typedef typename image_id<self_type>::exact_type exact_type;
00295       typedef super_border_morpher<const SrcType, BehaviorType, exact_type> super_type;
00296 
00297       typedef typename image_id<exact_type>::point_type point_type;
00298       typedef typename image_id<exact_type>::dpoint_type dpoint_type;
00299       typedef typename image_id<exact_type>::size_type size_type;
00300       typedef typename image_id<exact_type>::value_type value_type;
00301 
00307       border_morpher(const SrcType &ima,
00308                      const coord width,
00309                      const BehaviorType& be)
00310         : super_type(ima, width, be)
00311       {}
00312 
00314       border_morpher(const self_type& r)
00315         : super_type(r.get_ima(), r.get_width(), r.get_behavior())
00316       {}
00317 
00323       border_morpher() {}
00324 
00330       const value_type
00331       at(const point_type &p) const
00332       {
00333         return this->ima_[p + this->get_dp()];
00334       }
00335 
00337       static std::string
00338       name()
00339       {
00340         return "border_morpher<" + super_type::name() + ">";
00341       }
00342 
00343     };
00344 
00345 
00371     template <class I, class BehaviorType>
00372     const border_morpher<I, BehaviorType>
00373     border_morph(I &ima,
00374                  const coord width,
00375                  const BehaviorType& be)
00376     {
00377       return border_morpher<I, BehaviorType>(ima, width, be);
00378     }
00379 
00380 
00381   } // end namespace morpher
00382 
00383 } // end namespace oln
00384 
00385 #endif // !BORDER_MORPHER

Generated on Tue Feb 20 20:18:35 2007 for Olena by  doxygen 1.5.1