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