[207] | 1 | // -*- c++ -*- |
---|
[201] | 2 | #ifndef HUGO_XY_H |
---|
| 3 | #define HUGO_XY_H |
---|
| 4 | |
---|
| 5 | #include <iostream> |
---|
| 6 | |
---|
[207] | 7 | namespace hugo { |
---|
[201] | 8 | |
---|
[242] | 9 | /** \brief |
---|
| 10 | 2 dimensional vector (plainvector) implementation |
---|
| 11 | |
---|
| 12 | */ |
---|
[207] | 13 | template<typename T> |
---|
| 14 | class xy { |
---|
[201] | 15 | |
---|
[207] | 16 | public: |
---|
[240] | 17 | |
---|
| 18 | T x,y; |
---|
[207] | 19 | |
---|
| 20 | ///Default constructor: both coordinates become 0 |
---|
[240] | 21 | xy() : x(0), y(0) {} |
---|
[201] | 22 | |
---|
[240] | 23 | ///Constructing the instance from coordinates |
---|
| 24 | xy(T a, T b) : x(a), y(a) { } |
---|
[201] | 25 | |
---|
| 26 | |
---|
[207] | 27 | ///Gives back the square of the norm of the vector |
---|
| 28 | T normSquare(){ |
---|
[240] | 29 | return x*x+y*y; |
---|
[207] | 30 | }; |
---|
[201] | 31 | |
---|
[207] | 32 | ///Increments the left hand side by u |
---|
| 33 | xy<T>& operator +=(const xy<T>& u){ |
---|
[240] | 34 | x += u.x; |
---|
| 35 | y += u.y; |
---|
[207] | 36 | return *this; |
---|
| 37 | }; |
---|
[201] | 38 | |
---|
[207] | 39 | ///Decrements the left hand side by u |
---|
| 40 | xy<T>& operator -=(const xy<T>& u){ |
---|
[240] | 41 | x -= u.x; |
---|
| 42 | y -= u.y; |
---|
[207] | 43 | return *this; |
---|
| 44 | }; |
---|
[201] | 45 | |
---|
[207] | 46 | ///Multiplying the left hand side with a scalar |
---|
| 47 | xy<T>& operator *=(const T &u){ |
---|
[240] | 48 | x *= u; |
---|
| 49 | y *= u; |
---|
[207] | 50 | return *this; |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | ///Dividing the left hand side by a scalar |
---|
| 54 | xy<T>& operator /=(const T &u){ |
---|
[240] | 55 | x /= u; |
---|
| 56 | y /= u; |
---|
[207] | 57 | return *this; |
---|
| 58 | }; |
---|
[201] | 59 | |
---|
[207] | 60 | ///Returns the scalar product of two vectors |
---|
| 61 | T operator *(const xy<T>& u){ |
---|
[240] | 62 | return x*u.x+y*u.y; |
---|
[207] | 63 | }; |
---|
[201] | 64 | |
---|
[207] | 65 | ///Returns the sum of two vectors |
---|
| 66 | xy<T> operator+(const xy<T> &u) const { |
---|
| 67 | xy<T> b=*this; |
---|
| 68 | return b+=u; |
---|
| 69 | }; |
---|
[201] | 70 | |
---|
[207] | 71 | ///Returns the difference of two vectors |
---|
| 72 | xy<T> operator-(const xy<T> &u) const { |
---|
| 73 | xy<T> b=*this; |
---|
| 74 | return b-=u; |
---|
| 75 | }; |
---|
[201] | 76 | |
---|
[207] | 77 | ///Returns a vector multiplied by a scalar |
---|
| 78 | xy<T> operator*(const T &u) const { |
---|
| 79 | xy<T> b=*this; |
---|
| 80 | return b*=u; |
---|
| 81 | }; |
---|
[201] | 82 | |
---|
[207] | 83 | ///Returns a vector divided by a scalar |
---|
| 84 | xy<T> operator/(const T &u) const { |
---|
| 85 | xy<T> b=*this; |
---|
| 86 | return b/=u; |
---|
| 87 | }; |
---|
[201] | 88 | |
---|
[207] | 89 | ///Testing equality |
---|
| 90 | bool operator==(const xy<T> &u){ |
---|
[240] | 91 | return (x==u.x) && (y==u.y); |
---|
[207] | 92 | }; |
---|
[201] | 93 | |
---|
[207] | 94 | ///Testing inequality |
---|
| 95 | bool operator!=(xy u){ |
---|
[240] | 96 | return (x!=u.x) || (y!=u.y); |
---|
[207] | 97 | }; |
---|
[201] | 98 | |
---|
[207] | 99 | }; |
---|
[201] | 100 | |
---|
[207] | 101 | ///Reading a plainvector from a stream |
---|
| 102 | template<typename T> |
---|
| 103 | inline |
---|
| 104 | std::istream& operator>>(std::istream &is, xy<T> &z) |
---|
| 105 | { |
---|
[240] | 106 | |
---|
| 107 | is >> z.x >> z.y; |
---|
[207] | 108 | return is; |
---|
| 109 | } |
---|
[201] | 110 | |
---|
[207] | 111 | ///Outputting a plainvector to a stream |
---|
| 112 | template<typename T> |
---|
| 113 | inline |
---|
| 114 | std::ostream& operator<<(std::ostream &os, xy<T> z) |
---|
| 115 | { |
---|
[240] | 116 | os << "(" << z.x << ", " << z.y << ")"; |
---|
[207] | 117 | return os; |
---|
| 118 | } |
---|
| 119 | |
---|
[244] | 120 | |
---|
| 121 | /** \brief |
---|
| 122 | Implementation of a bounding box of plainvectors. |
---|
| 123 | |
---|
| 124 | */ |
---|
| 125 | template<typename T> |
---|
| 126 | class BoundingBox { |
---|
| 127 | xy<T> bottom_left, top_right; |
---|
| 128 | bool _empty; |
---|
| 129 | public: |
---|
| 130 | |
---|
| 131 | ///Default constructor: an empty bounding box |
---|
| 132 | BoundingBox() { _empty = true; } |
---|
| 133 | |
---|
| 134 | ///Constructing the instance from one point |
---|
| 135 | BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; } |
---|
| 136 | |
---|
| 137 | ///Is there any point added |
---|
| 138 | bool empty() const { |
---|
| 139 | return _empty; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) |
---|
| 143 | xy<T> bottomLeft() const { |
---|
| 144 | return bottom_left; |
---|
| 145 | }; |
---|
| 146 | |
---|
| 147 | ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) |
---|
| 148 | xy<T> topRight() const { |
---|
| 149 | return top_right; |
---|
| 150 | }; |
---|
| 151 | |
---|
| 152 | ///Checks whether a point is inside a bounding box |
---|
| 153 | bool inside(const xy<T>& u){ |
---|
| 154 | if (_empty) |
---|
| 155 | return false; |
---|
| 156 | else{ |
---|
| 157 | return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 && |
---|
| 158 | (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 ); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | ///Increments a bounding box with a point |
---|
| 163 | BoundingBox& operator +=(const xy<T>& u){ |
---|
| 164 | if (_empty){ |
---|
| 165 | bottom_left=top_right=u; |
---|
| 166 | _empty = false; |
---|
| 167 | } |
---|
| 168 | else{ |
---|
| 169 | if (bottom_left.x > u.x) bottom_left.x = u.x; |
---|
| 170 | if (bottom_left.y > u.y) bottom_left.y = u.y; |
---|
| 171 | if (top_right.x < u.x) top_right.x = u.x; |
---|
| 172 | if (top_right.y < u.y) top_right.y = u.y; |
---|
| 173 | } |
---|
| 174 | return *this; |
---|
| 175 | }; |
---|
| 176 | |
---|
| 177 | ///Sums a bounding box and a point |
---|
| 178 | BoundingBox operator +(const xy<T>& u){ |
---|
| 179 | BoundingBox b = *this; |
---|
| 180 | return b += u; |
---|
| 181 | }; |
---|
| 182 | |
---|
| 183 | ///Increments a bounding box with an other bounding box |
---|
| 184 | BoundingBox& operator +=(const BoundingBox &u){ |
---|
| 185 | if ( !u.empty() ){ |
---|
| 186 | *this += u.bottomLeft(); |
---|
| 187 | *this += u.topRight(); |
---|
| 188 | } |
---|
| 189 | return *this; |
---|
| 190 | }; |
---|
| 191 | |
---|
| 192 | ///Sums two bounding boxes |
---|
| 193 | BoundingBox operator +(const BoundingBox& u){ |
---|
| 194 | BoundingBox b = *this; |
---|
| 195 | return b += u; |
---|
| 196 | }; |
---|
| 197 | |
---|
| 198 | };//class Boundingbox |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | |
---|
[207] | 203 | } //namespace hugo |
---|
[201] | 204 | |
---|
| 205 | #endif //HUGO_XY_H |
---|