alaCarte Maps
Renderer for OpenStreetMap tiles
point.hpp
Go to the documentation of this file.
1 
21 #pragma once
22 #ifndef VECTOR_HPP
23 #define VECTOR_HPP
24 
25 #include <vector>
26 #include <sstream>
27 
28 #include <boost/archive/text_oarchive.hpp>
29 #include <boost/archive/text_iarchive.hpp>
30 
31 template<typename T>
33 {
34 private:
35  friend class boost::serialization::access;
36  template<typename Archive>
37  void serialize(Archive &ar, const unsigned int version){
38  ar & x;
39  ar & y;
40  }
41 public:
42  basic_vector2() : x(T(0)), y(T(0)) {}
43  basic_vector2(const basic_vector2& _v) = default;
44  basic_vector2(basic_vector2&& _v) = default;
45  basic_vector2(const T _v) : x(_v), y(_v) {}
46  basic_vector2(const T _x, const T _y) : x(_x), y(_y) {}
47 
48  // Casting-Operatoren
49  operator float* () {return (float*)(c);}
50 
51  // Zuweisungsoperatoren
52  basic_vector2& operator = (const basic_vector2& v) = default;
53  basic_vector2& operator = (basic_vector2&& v) = default;
54  basic_vector2& operator += (const basic_vector2& v) {x += v.x; y += v.y; return (*this);}
55  basic_vector2& operator -= (const basic_vector2& v) {x -= v.x; y -= v.y; return (*this);}
56  basic_vector2& operator *= (const basic_vector2& v) {x *= v.x; y *= v.y; return (*this);}
57  basic_vector2& operator *= (const T f) {x *= f; y *= f; return (*this);}
58  basic_vector2& operator /= (const basic_vector2& v) {x /= v.x; y /= v.y; return (*this);}
59  basic_vector2& operator /= (const T f) {x /= f; y /= f; return (*this);}
60 
61  // Arithmetische Operatoren
62  inline basic_vector2 operator + (const basic_vector2& v) const {return basic_vector2(x + v.x, y + v.y);}
63  inline basic_vector2 operator - (const basic_vector2& v) const {return basic_vector2(x - v.x, y - v.y);}
64  inline basic_vector2 operator - () const {return basic_vector2(-x, -y);}
65  inline basic_vector2 operator * (const basic_vector2& v) const {return basic_vector2(x * v.x, y * v.y);}
66  inline basic_vector2 operator * (const T f) const {return basic_vector2(x * f, y * f);}
67  inline basic_vector2 operator / (const basic_vector2& v) const {return basic_vector2(x / v.x, y / v.y);}
68  inline basic_vector2 operator / (const T f) const {return basic_vector2(x / f, y / f);}
69 
70  // Vergleichsoperatoren
71  inline bool operator == (const basic_vector2& v) const {return (x == v.x && y == v.y);}
72  inline bool operator != (const basic_vector2& v) const {return (x != v.x || y != v.y);}
73 
74  inline double distance(const basic_vector2& v) const { return std::sqrt(distanceQuad(v));}
75  inline double distanceQuad(const basic_vector2& v) const { basic_vector2<double> delta(v - *this); delta*=delta; return delta.x + delta.y; }
76  inline bool isInRange(const basic_vector2& v, double epsilon = 0.000000001) const { return distanceQuad(v) < epsilon; }
77 
78  operator basic_vector2<int> () const {return basic_vector2<int>(static_cast<int>(x), static_cast<int>(y));}
79  operator basic_vector2<float> () const {return basic_vector2<float>(static_cast<float>(x), static_cast<float>(y));}
80  operator basic_vector2<double>() const {return basic_vector2<double>(static_cast<double>(x), static_cast<double>(y));}
81 
82  inline T area() const { return w * h; }
83 
84  union
85  {
86  struct
87  {
88  T x;
89  T y;
90  };
91 
92  struct
93  {
94  T lon;
95  T lat;
96  };
97 
98  struct
99  {
100  T w;
101  T h;
102  };
103 
104  T c[2];
105  };
106 };
107 
108 
109 // Arithmetische Operatoren
110 template<typename T>
111 inline basic_vector2<T> operator * (const T f, const basic_vector2<T>& v) {return basic_vector2<T>(v.x * f, v.y * f);}
112 template<typename T>
113 inline basic_vector2<T> operator / (const T f, const basic_vector2<T>& v) {return basic_vector2<T>(v.x / f, v.y / f);}
114 
115 
116 
117 
118 
119 template<typename T, typename C>
120 inline std::basic_istream<C>& operator >> (std::basic_istream<C>& is, basic_vector2<T>& v)
121 {
122  C op;
123 
124  is >> v.x;
125  is >> op;
126  is >> v.y;
127 
128  if((op != C(',') && op != C('x') && op != C('|')) || is.bad())
129  {
130  throw std::runtime_error("Not able to convert from stream to vector!");
131  }
132 
133  return is;
134 }
135 
136 template<typename T, typename C>
137 std::basic_ostream<C>& operator << (std::basic_ostream<C>& os, const basic_vector2<T>& v)
138 {
139  os << v.x << C(',') << C(' ') << v.y;
140  return os;
141 }
142 
143 
147 
148 #endif
basic_vector2 operator-() const
Definition: point.hpp:64
basic_vector2 & operator/=(const basic_vector2 &v)
Definition: point.hpp:58
double distance(const basic_vector2 &v) const
Definition: point.hpp:74
bool operator==(const basic_vector2 &v) const
Definition: point.hpp:71
void serialize(Archive &ar, const unsigned int version)
Definition: point.hpp:37
bool isInRange(const basic_vector2 &v, double epsilon=0.000000001) const
Definition: point.hpp:76
basic_vector2(const T _x, const T _y)
Definition: point.hpp:46
basic_vector2()
Definition: point.hpp:42
basic_vector2 & operator*=(const basic_vector2 &v)
Definition: point.hpp:56
basic_vector2 operator/(const basic_vector2 &v) const
Definition: point.hpp:67
basic_vector2 operator+(const basic_vector2 &v) const
Definition: point.hpp:62
basic_vector2 & operator=(const basic_vector2 &v)=default
basic_vector2< coord_t > FixedPoint
Definition: point.hpp:144
double distanceQuad(const basic_vector2 &v) const
Definition: point.hpp:75
basic_vector2 & operator+=(const basic_vector2 &v)
Definition: point.hpp:54
basic_vector2< double > FloatPoint
Definition: point.hpp:145
basic_vector2(const T _v)
Definition: point.hpp:45
basic_vector2< double > Vector
Definition: point.hpp:146
basic_vector2 operator*(const basic_vector2 &v) const
Definition: point.hpp:65
basic_vector2 & operator-=(const basic_vector2 &v)
Definition: point.hpp:55
bool operator!=(const basic_vector2 &v) const
Definition: point.hpp:72
std::basic_istream< C > & operator>>(std::basic_istream< C > &is, basic_vector2< T > &v)
Definition: point.hpp:120
T area() const
Definition: point.hpp:82