• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

h_vec.hh

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_ALGEBRA_H_VEC_HH
00027 # define MLN_ALGEBRA_H_VEC_HH
00028 
00032 
00033 # include <mln/algebra/vec.hh>
00034 # include <mln/literal/one.hh>
00035 
00036 
00037 namespace mln
00038 {
00039 
00040   // Forward declaration.
00041   namespace algebra {
00042     template <unsigned d, typename C> struct h_vec;
00043   }
00044 
00045 
00046   namespace trait
00047   {
00048 
00049     // For unary traits.
00050 
00051     template < template <class> class Name,
00052                unsigned d, typename C >
00053     struct set_precise_unary_< Name, algebra::h_vec<d, C> >
00054     {
00055       typedef mln_trait_unary(Name, C) V;
00056       typedef algebra::h_vec<d, V> ret;
00057     };
00058 
00059     // For binary traits.
00060 
00061     template < template <class, class> class Name,
00062                unsigned d, typename C,
00063                typename Q >
00064     struct set_precise_binary_< Name,
00065                                 algebra::h_vec<d, C>, algebra::h_vec<d, Q> >
00066     {
00067       typedef mln_trait_binary(Name, C, Q) V;
00068       typedef algebra::h_vec<d, V> ret;
00069     };
00070 
00071     template < template <class, class> class Name,
00072                unsigned d, typename C,
00073                typename S >
00074     struct set_precise_binary_< Name,
00075                                 algebra::h_vec<d, C>, mln::value::scalar_<S> >
00076     {
00077       typedef mln_trait_binary(Name, C, S) V;
00078       typedef algebra::h_vec<d, V> ret;
00079     };
00080 
00081   } // end of namespace mln::trait
00082 
00083 
00084 
00085   namespace algebra
00086   {
00087 
00088 
00092     template <unsigned d, typename C>
00093     struct h_vec : public vec<d + 1, C>
00094     {
00096       enum { dim = d };
00097 
00099       h_vec();
00101       h_vec(const vec<d+1, C>& other);
00102 
00103       h_vec& operator=(const vec<d+1, C>& rhs);
00104 
00106       vec<d,C> to_vec() const;
00107     };
00108 
00109 
00110 
00111 # ifndef MLN_INCLUDE_ONLY
00112 
00113     template <unsigned d, typename C>
00114     inline
00115     h_vec<d,C>::h_vec()
00116     {
00117       /* Safety measure: set the last component to the unit (1).  This
00118          way, converting an unitialized h_vec to a vector won't trigger
00119          division-by-zero errors if this last component were randomly
00120          initialized to 0.  */
00121       this->data_[d] = literal::one;
00122     }
00123 
00124     template <unsigned d, typename C>
00125     inline
00126     h_vec<d,C>::h_vec(const vec<d+1, C>& other)
00127       : vec<d+1, C>(other)
00128     {
00129     }
00130 
00131     template <unsigned d, typename C>
00132     inline
00133     h_vec<d,C>& h_vec<d,C>::operator=(const vec<d+1, C>& rhs)
00134     {
00135       if (& rhs == this)
00136         return *this;
00137       this->vec<d+1, C>::operator=(rhs);
00138       return *this;
00139     }
00140 
00141     template <unsigned d, typename C>
00142     inline
00143     vec<d,C> h_vec<d,C>::to_vec() const
00144     {
00145       const C w = this->data_[d];
00146       mln_assertion(w != 0);
00147 
00148       vec<d,C> tmp;
00149       for (unsigned i = 0; i < d; ++i)
00150         tmp[i] = static_cast<C>(this->data_[i] / w);
00151       return tmp;
00152     }
00153 
00154     // Immersion of a vector in its homogeneous space.
00155 
00156     template <unsigned n, typename T>
00157     inline
00158     h_vec<n, T>
00159     vec<n,T>::to_h_vec() const
00160     {
00161       h_vec<n, T> tmp;
00162       for (unsigned i = 0; i < n; ++i)
00163         tmp[i] = this->data_[i];
00164       tmp[n] = literal::one;
00165       return tmp;
00166     }
00167 
00168 # endif // ! MLN_INCLUDE_ONLY
00169 
00170   } // end of namespace mln::algebra
00171 
00172 } // end of namespace mln
00173 
00174 
00175 #endif // ! MLN_ALGEBRA_H_VEC_HH

Generated on Thu Sep 8 2011 18:31:57 for Milena (Olena) by  doxygen 1.7.1