/home/andreas/src/svn/mapnik/include/mapnik/coord.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * 
00003  * This file is part of Mapnik (c++ mapping toolkit)
00004  *
00005  * Copyright (C) 2006 Artem Pavlenko
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  *
00021  *****************************************************************************/
00022 
00023 //$Id: coord.hpp 39 2005-04-10 20:39:53Z pavlenko $
00024 
00025 #ifndef COORD_HPP
00026 #define COORD_HPP
00027 
00028 #include <iomanip>
00029 #include <sstream>
00030 #include <boost/operators.hpp>
00031 
00032 namespace mapnik {   
00033     template <typename T,int dim>
00034     struct coord {
00035         typedef T type;
00036     };
00037     
00038     template <typename T>
00039     struct coord<T,2> 
00040         : boost::addable<coord<T,2>,
00041           boost::addable2<coord<T,2>,T,
00042           boost::subtractable<coord<T,2>,
00043           boost::subtractable2<coord<T,2>,T,
00044           boost::dividable2<coord<T,2>, T,
00045           boost::multipliable2<coord<T,2>, T > > > > > >
00046                     
00047     {
00048         typedef T type;
00049         T x;
00050         T y;
00051     public:
00052         coord()
00053             : x(),y() {}
00054         coord(T x,T y)
00055             : x(x),y(y) {}
00056         template <typename T2>
00057         coord (const coord<T2,2>& rhs)
00058             : x(type(rhs.x)),
00059               y(type(rhs.y)) {}
00060 
00061         template <typename T2>
00062         coord<T,2>& operator=(const coord<T2,2>& rhs)
00063         {
00064             if ((void*)this==(void*)&rhs)
00065             {
00066                 return *this;
00067             }
00068             x=type(rhs.x);
00069             y=type(rhs.y);
00070             return *this;
00071         }
00072         template <typename T2>
00073         bool operator==(coord<T2,2> const& rhs)
00074         {
00075             return x == rhs.x && y == rhs.y;
00076         }
00077 
00078         coord<T,2>& operator+=(coord<T,2> const& rhs)
00079         {
00080             x+=rhs.x;
00081             y+=rhs.y;
00082             return *this;
00083         }
00084         
00085         coord<T,2>& operator+=(T rhs)
00086         {
00087             x+=rhs;
00088             y+=rhs;
00089             return *this;
00090         }
00091         
00092         coord<T,2>& operator-=(coord<T,2> const& rhs)
00093         {
00094             x-=rhs.x;
00095             y-=rhs.y;
00096             return *this;
00097         }
00098         
00099         coord<T,2>& operator-=(T rhs)
00100         {
00101             x-=rhs;
00102             y-=rhs;
00103             return *this;
00104         }
00105         
00106         coord<T,2>& operator*=(T t)
00107         {
00108             x*=t;
00109             y*=t;
00110             return *this;
00111         }
00112         coord<T,2>& operator/=(T t)
00113         {
00114             x/=t;
00115             y/=t;
00116             return *this;
00117         }
00118     };
00119 
00120     template <typename T>
00121     struct coord<T,3> 
00122     {
00123         typedef T type;
00124         T x;
00125         T y;
00126         T z;
00127     public:
00128         coord()
00129             : x(),y(),z() {}
00130         coord(T x,T y,T z)
00131             : x(x),y(y),z(z) {}
00132         template <typename T2>
00133         coord (const coord<T2,3>& rhs)
00134             : x(type(rhs.x)),
00135               y(type(rhs.y)),
00136               z(type(rhs.z)) {}
00137 
00138         template <typename T2>
00139         coord<T,3>& operator=(const coord<T2,3>& rhs)
00140         {
00141             if ((void*)this==(void*)&rhs)
00142             {
00143                 return *this;
00144             }
00145             x=type(rhs.x);
00146             y=type(rhs.y);
00147             z=type(rhs.z);
00148             return *this;
00149         }
00150     };
00151 
00152     typedef coord<double,2> coord2d;
00153     typedef coord<int,2> coord2i;
00154 
00155      
00156     template <typename charT,typename traits,typename T ,int dim>
00157     inline std::basic_ostream<charT,traits>&
00158     operator << (std::basic_ostream<charT,traits>& out,
00159                  const coord<T,dim>& c);
00160     
00161     template <typename charT,typename traits,typename T>
00162     inline std::basic_ostream<charT,traits>&
00163     operator << (std::basic_ostream<charT,traits>& out,
00164                  const coord<T,2>& c)
00165     {
00166         std::basic_ostringstream<charT,traits> s;
00167         s.copyfmt(out);
00168         s.width(0);
00169         s << "coord2(" << std::setprecision(16) 
00170           << c.x << "," << c.y<< ")";
00171         out << s.str();
00172         return out;
00173     }
00174 
00175     template <typename charT,typename traits,typename T>
00176     inline std::basic_ostream<charT,traits>&
00177     operator << (std::basic_ostream<charT,traits>& out,
00178                  const coord<T,3>& c)
00179     {
00180         std::basic_ostringstream<charT,traits> s;
00181         s.copyfmt(out);
00182         s.width(0);
00183         s << "coord3(" << std::setprecision(16) 
00184           << c.x << "," << c.y<< "," << c.z<<")";
00185         out << s.str();
00186         return out;
00187     } 
00188 }
00189 
00190 #endif // COORD_HPP

Generated on Thu Jul 19 17:59:25 2007 for Mapnik by  doxygen 1.4.7