macros.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_MATH_MACROS_HH
00029 # define OLENA_MATH_MACROS_HH
00030 
00031 # include <ntg/basics.hh>
00032 # include <oln/basics.hh>
00033 
00034 namespace oln {
00035 
00045   namespace math {
00046 
00048     template<class T>
00049     struct f_sqr
00050     {
00051       typedef T output_t;
00052       const T operator()(const T& val) const
00053       {
00054         return val * val;
00055       }
00056     };
00057 
00059     template<class T>
00060     const T sqr(const T& val)
00061     {
00062       f_sqr<T> f;
00063       return f(val);
00064     }
00065 
00067     template<class T>
00068     struct f_abs
00069     {
00070       typedef T output_t;
00071 
00072       const T
00073       operator()(const T& val) const
00074       {
00075         return ::abs(val);
00076       }
00077     };
00078 
00080     template<class T>
00081     const T
00082     abs(const T& val)
00083     {
00084       f_abs<T> f;
00085       return f(val);
00086     }
00087 
00089     namespace internal {
00091       template<typename DestValue, typename I, typename J>
00092       struct f_dot_product_nv
00093       {
00094         typedef typename ntg_is_a(I, ntg::non_vectorial)::ensure_type t;
00095 
00096         static DestValue
00097         product(const I &i, const J &j)
00098         {
00099           return i * j;
00100         }
00101       };
00102 
00104       template<typename DestValue, typename I, typename J>
00105       struct f_dot_product_v
00106       {
00107         typedef typename ntg_is_a(I, ntg::vectorial)::ensure_type t;
00108         //typedef typename mlc::eq<ntg_nb_comp(I), ntg_nb_comp(J)>::ensure_type t;
00109 
00110         static DestValue
00111         product(const I &i, const J &j)
00112         {
00113           DestValue d(ntg_zero_val(DestValue));
00114 
00115           for (unsigned k = 0; k < ntg_nb_comp(I); ++k)
00116             d += i[k] * j[k];
00117           return d;
00118         }
00119       };
00120     }
00121 
00140     template<typename DestValue, typename I, typename J>
00141     DestValue
00142     dot_product(const I &i, const J &j)
00143     {
00144       typedef typename mlc::if_<ntg_is_a(I, ntg::vectorial)::ret,
00145         internal::f_dot_product_v<DestValue, I, J>,
00146         internal::f_dot_product_nv<DestValue, I, J> >::ret fctor;
00147       return fctor::product(i, j);
00148     }
00149 
00150   } // end of math
00151 
00152 } // end of oln
00153 
00154 #endif // ! OLENA_MATH_MACROS_HH

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