00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef COORD_HPP
00026 #define COORD_HPP
00027
00028 #include <boost/operators.hpp>
00029 #include <iomanip>
00030 #include <sstream>
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