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