CVector2d.h
Go to the documentation of this file.
1 #ifndef i2d_CVector2d_included
2 #define i2d_CVector2d_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtGlobal>
7 #if QT_VERSION >= 0x050000
8 #include <QtCore/QtMath>
9 #else
10 #include <QtCore/qmath.h>
11 #endif
12 #include <QtCore/QPointF>
13 
14 // ACF includes
15 #include <istd/CIndex2d.h>
16 #include <imath/TVector.h>
17 
18 #include <i2d/i2d.h>
19 
20 
21 namespace i2d
22 {
23 
24 
28 class CVector2d: public imath::TVector<2>
29 {
30 public:
32 
37  CVector2d();
38 
42  CVector2d(double x, double y);
43 
44  CVector2d(const istd::CIndex2d& index);
45 
46  CVector2d(const QPointF& point);
47 
51  CVector2d(const imath::TVector<2>& vector);
52 
56  double GetX() const;
60  void SetX(double x);
64  double GetY() const;
68  void SetY(double y);
69 
73  void Init(double angle, double length = 1.0);
74 
83  CVector2d GetHorizontalTranslated(double offsetX) const;
87  CVector2d GetVerticalTranslated(double offsetY) const;
88 
92  double GetCrossProductZ(const imath::TVector<2>& vector) const;
93 
94  double GetDotProduct(const CVector2d& vector) const;
98  double GetAngle() const;
102  CVector2d GetOrthogonal() const;
103 
107  istd::CIndex2d ToIndex2d() const;
108 
113  CVector2d GetNormalized(double length = 1.0) const;
114 
118  bool Serialize(iser::IArchive& archive);
119 
120  CVector2d operator-() const;
121 
122  CVector2d operator+(const imath::TVector<2>& vector) const;
123  CVector2d operator-(const imath::TVector<2>& vector) const;
124  CVector2d operator*(double scalar) const;
125  CVector2d operator/(double scalar) const;
126 
127  CVector2d& operator+=(const imath::TVector<2>& vector);
128  CVector2d& operator-=(const imath::TVector<2>& vector);
129  CVector2d& operator*=(double scalar);
130  CVector2d& operator/=(double scalar);
131 
132  operator QPointF() const;
133 
134  static const CVector2d& GetZero();
135 };
136 
137 
138 // inline methods
139 
140 inline CVector2d::CVector2d(const QPointF& point)
141 {
142  operator[](0) = point.x();
143  operator[](1) = point.y();
144 }
145 
146 
148 {
149 }
150 
151 
152 inline CVector2d::CVector2d(double x, double y)
153 {
154  operator[](0) = x;
155  operator[](1) = y;
156 }
157 
158 
160 : BaseClass(vector)
161 {
162 }
163 
164 
166 {
167  operator[](0) = index.GetX();
168  operator[](1) = index.GetY();
169 }
170 
171 
172 inline double CVector2d::GetX() const
173 {
174  return operator[](0);
175 }
176 
177 
178 inline void CVector2d::SetX(double x)
179 {
180  operator[](0) = x;
181 }
182 
183 
184 inline double CVector2d::GetY() const
185 {
186  return operator[](1);
187 }
188 
189 
190 inline void CVector2d::SetY(double y)
191 {
192  operator[](1) = y;
193 }
194 
195 
197 {
198  return *this + vector;
199 }
200 
201 
202 inline CVector2d CVector2d::GetHorizontalTranslated(double offsetX) const
203 {
204  return CVector2d(GetX() + offsetX, GetY());
205 }
206 
207 
208 inline CVector2d CVector2d::GetVerticalTranslated(double offsetY) const
209 {
210  return CVector2d(GetX(), GetY() + offsetY);
211 }
212 
213 
214 inline double CVector2d::GetCrossProductZ(const imath::TVector<2>& vector) const
215 {
216  return GetX() * vector[1] - GetY() * vector[0];
217 }
218 
219 
220 inline double CVector2d::GetDotProduct(const CVector2d& vector) const
221 {
222  return GetX() * vector.GetX() + GetY() * vector.GetY();
223 }
224 
225 
226 inline double CVector2d::GetAngle() const
227 {
228  return qAtan2(GetY(), GetX());
229 }
230 
231 
233 {
234  return CVector2d(-GetX(), -GetY());
235 }
236 
237 
239 {
240  return CVector2d(GetX() + vector[0], GetY() + vector[1]);
241 }
242 
243 
245 {
246  return CVector2d(GetX() - vector[0], GetY() - vector[1]);
247 }
248 
249 
250 inline CVector2d CVector2d::operator*(double scalar) const
251 {
252  return CVector2d(GetX() * scalar, GetY() * scalar);
253 }
254 
255 
256 inline CVector2d CVector2d::operator/(double scalar) const
257 {
258  if (qAbs(scalar) < I_BIG_EPSILON){
259  scalar = (scalar > 0)? I_BIG_EPSILON: -I_BIG_EPSILON;
260  }
261 
262  return CVector2d(GetX() / scalar, GetY() / scalar);
263 }
264 
265 
267 {
268  BaseClass::operator+=(vector);
269 
270  return *this;
271 }
272 
273 
275 {
276  BaseClass::operator-=(vector);
277 
278  return *this;
279 }
280 
281 
282 inline CVector2d& CVector2d::operator*=(double scalar)
283 {
284  BaseClass::operator*=(scalar);
285 
286  return *this;
287 }
288 
289 
290 inline CVector2d& CVector2d::operator/=(double scalar)
291 {
292  BaseClass::operator/=(scalar);
293 
294  return *this;
295 }
296 
297 
298 inline CVector2d::operator QPointF() const
299 {
300  return QPointF(qreal(GetX()), qreal(GetY()));
301 }
302 
303 
304 // static methods
305 
307 {
308  return reinterpret_cast<const CVector2d&>(BaseClass::GetZero());
309 }
310 
311 
312 // related global functions
313 
314 inline uint qHash(const CVector2d& key, uint seed = 0)
315 {
316  Q_UNUSED(seed);
317 
318  return int(key.GetX()) ^ int(key.GetY());
319 }
320 
321 
322 } // namespace i2d
323 
324 
325 #endif // !i2d_CVector2d_included
326 
327 
CVector2d & operator/=(double scalar)
Definition: CVector2d.h:290
double GetCrossProductZ(const imath::TVector< 2 > &vector) const
Return Z coordinate of two vectors cross product.
Definition: CVector2d.h:214
Index implementation for addressing elements in 2D-space.
Definition: CIndex2d.h:20
bool Serialize(iser::IArchive &archive)
Serialize this vector to specified archive.
double GetY() const
Get Y position of this vector.
Definition: CVector2d.h:184
imath::TVector< 2 > BaseClass
Definition: CVector2d.h:31
void Init(double angle, double length=1.0)
Init this vector using angle and vector length.
static const TVector< Size, double > & GetZero()
Get vector with all coordinates set to 0.
CVector2d operator*(double scalar) const
Definition: CVector2d.h:250
CVector2d()
Default constructor.
Definition: CVector2d.h:147
CVector2d GetTranslated(const imath::TVector< 2 > &vector)
Get translated point.
Definition: CVector2d.h:196
bool GetNormalized(TVector< Size, double > &result, doublelength=1.0) const
Return normalized vector with the same direction and specified length.
int GetX() const
Definition: CIndex2d.h:96
uint qHash(const CLine2d &key, uint seed=0)
Definition: CLine2d.h:424
void SetX(double x)
Set X position of this vector.
Definition: CVector2d.h:178
Represent input/output persistence archive.
Definition: IArchive.h:30
CVector2d operator-() const
Definition: CVector2d.h:232
Implementation of fixed-size mathematical vector with specified type of elements. ...
Definition: TVector.h:27
Definition of position or mathematical vector on 2D plane.
Definition: CVector2d.h:28
CVector2d GetOrthogonal() const
Get orthogonal vector.
void SetY(double y)
Set Y position of this vector.
Definition: CVector2d.h:190
TVector< Size, double > & operator-=(const TVector< Size, double > &vector)
CVector2d & operator-=(const imath::TVector< 2 > &vector)
Definition: CVector2d.h:274
TVector< Size, double > & operator/=(doublescalar)
double GetAngle() const
Get angle of this vector.
Definition: CVector2d.h:226
double GetDotProduct(const CVector2d &vector) const
Definition: CVector2d.h:220
int GetY() const
Definition: CIndex2d.h:108
static const CVector2d & GetZero()
Definition: CVector2d.h:306
CVector2d GetHorizontalTranslated(double offsetX) const
Get horizontal translated point.
Definition: CVector2d.h:202
TVector< Size, double > GetTranslated(const TVector< Size, double > &vector)
Get translated point.
static const double I_BIG_EPSILON
Definition: istd.h:26
CVector2d & operator*=(double scalar)
Definition: CVector2d.h:282
CVector2d operator+(const imath::TVector< 2 > &vector) const
Definition: CVector2d.h:238
CVector2d GetNormalized(double length=1.0) const
Return normalized vector with the same direction and specified length.
istd::CIndex2d ToIndex2d() const
Get vector converted to 2D index.
TVector< Size, double > & operator+=(const TVector< Size, double > &vector)
double GetX() const
Get X position of this vector.
Definition: CVector2d.h:172
TVector< Size, double > & operator*=(doublescalar)
CVector2d GetVerticalTranslated(double offsetY) const
Get vertical translated point.
Definition: CVector2d.h:208
const double & operator[](int i) const
CVector2d operator/(double scalar) const
Definition: CVector2d.h:256
CVector2d & operator+=(const imath::TVector< 2 > &vector)
Definition: CVector2d.h:266

© 2007-2017 Witold Gantzke and Kirill Lepskiy