dpoint.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_CORE_ABSTRACT_DPOINT_HH
00029 # define OLENA_CORE_ABSTRACT_DPOINT_HH
00030 
00031 # include <mlc/type.hh>
00032 # include <oln/core/coord.hh>
00033 # include <ntg/float.hh>
00034 # include <cmath>
00035 
00036 namespace oln {
00037 
00038 
00039   namespace abstract {
00040     template<class Exact>
00041     struct dpoint; // fwd_decl
00042 
00043     template<class Exact>
00044     struct point; // fwd_decl
00045   } // end of abstract
00046 
00047   template<class Exact>
00048   struct dpoint_traits; // fwd_decl
00049 
00055   template<class Exact>
00056   struct dpoint_traits<abstract::dpoint<Exact> >
00057   {
00058 
00059   };
00060 
00061 
00062   namespace abstract {
00063 
00070     template<class Exact>
00071     struct dpoint : public mlc_hierarchy::any<Exact>
00072     {
00073 
00074       typedef Exact exact_type;
00075       typedef dpoint<Exact> self_type;
00076       enum { dim = dpoint_traits<exact_type>::dim };
00077       typedef typename dpoint_traits<exact_type>::point_type point_type;
00078 
00083       explicit dpoint(const abstract::point<point_type>& p)
00084       {
00085         for (unsigned i = 0; i < dim; ++i)
00086           nth(i) = p.exact().nth(i);
00087       }
00088 
00090 
00091       coord
00092       nth(const unsigned d) const
00093       {
00094         return coord_[d];
00095       }
00096 
00098       coord&
00099       nth(const unsigned d)
00100       {
00101         return coord_[d];
00102       }
00103 
00108       exact_type
00109       operator-() const
00110       {
00111         return this->exact().minus();
00112       }
00113 
00119       exact_type&
00120       operator+=(const self_type& dp)
00121       {
00122         return this->exact().plus_assign_dp(dp.exact());
00123       }
00124 
00130       exact_type&
00131       operator-=(const self_type& dp)
00132       {
00133         return this->exact().minus_assign_dp(dp.exact());
00134       }
00135 
00141       exact_type
00142       operator+(const self_type& dp) const
00143       {
00144         return this->exact().plus_dp(dp.exact());
00145       }
00146 
00152       exact_type
00153       operator-(const self_type& dp) const
00154       {
00155         return this->exact().minus_dp(dp.exact());
00156       }
00157 
00166       bool
00167       operator==(const self_type& dp) const
00168       {
00169         for (unsigned i = 0; i < dim; ++i)
00170           if (dp.nth(i) != nth(i))
00171             return false;
00172         return true;
00173       }
00174 
00183       bool
00184       operator!=(const self_type& dp) const
00185       {
00186         for (unsigned i = 0; i < dim; ++i)
00187           if (dp.nth(i) != nth(i))
00188             return true;
00189         return false;
00190       }
00191 
00198       bool
00199       is_centered(void) const
00200       {
00201         for (unsigned i = 0; i < dim; ++i)
00202           if (nth(i) != 0)
00203             return false;
00204         return true;
00205       }
00206 
00208 
00209       ntg::float_d
00210       norm2(void) const
00211       {
00212         double norm = 0;
00213 
00214         for (unsigned i = 0; i < dim; ++i)
00215           norm += nth(i) * nth(i);
00216         return sqrt(norm);
00217       }
00218 
00220 
00221       ntg::float_d
00222       sqr_norm2(void) const
00223       {
00224         double norm = 0;
00225 
00226         for (unsigned i = 0; i < dim; ++i)
00227           norm += nth(i) * nth(i);
00228         return norm;
00229       }
00230 
00231       static std::string
00232       name()
00233       {
00234         return std::string("dpoint<") +
00235           Exact::name() + ">";
00236       }
00237 
00238     protected:
00239 
00240       dpoint()
00241       {}
00242 
00243     private:
00244 
00245       coord coord_[dim];
00246     };
00247 
00248   } // end of abstract
00249 
00252   namespace internal
00253   {
00254 
00261     template<class Exact>
00262     struct default_less< abstract::dpoint<Exact> >
00263     {
00264 
00271       bool operator()(const abstract::dpoint<Exact>& l,
00272                       const abstract::dpoint<Exact>& r) const
00273       {
00274         for (unsigned i = 0; i < abstract::dpoint<Exact>::dim; ++i)
00275           if (l.nth(i) < r.nth(i))
00276             return true;
00277           else if (l.nth(i) > r.nth(i))
00278             return false;
00279         return false;
00280       }
00281     };
00282 
00283   } // internal
00284 
00285 
00286 } // end of oln
00287 
00288 
00289 #endif // ! OLENA_CORE_ABSTRACT_DPOINT_HH

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