ACF $AcfVersion:0$
CVector2d.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
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
21namespace i2d
22{
23
24
28class CVector2d: public imath::TVector<2>
29{
30public:
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;
103
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
140inline CVector2d::CVector2d(const QPointF& point)
141{
142 operator[](0) = point.x();
143 operator[](1) = point.y();
144}
145
146
148{
149}
150
151
152inline 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
172inline double CVector2d::GetX() const
173{
174 return operator[](0);
175}
176
177
178inline void CVector2d::SetX(double x)
179{
180 operator[](0) = x;
181}
182
183
184inline double CVector2d::GetY() const
185{
186 return operator[](1);
187}
188
189
190inline void CVector2d::SetY(double y)
191{
192 operator[](1) = y;
193}
194
195
197{
198 return *this + vector;
199}
200
201
203{
204 return CVector2d(GetX() + offsetX, GetY());
205}
206
207
208inline CVector2d CVector2d::GetVerticalTranslated(double offsetY) const
209{
210 return CVector2d(GetX(), GetY() + offsetY);
211}
212
213
214inline double CVector2d::GetCrossProductZ(const imath::TVector<2>& vector) const
215{
216 return GetX() * vector[1] - GetY() * vector[0];
217}
218
219
220inline double CVector2d::GetDotProduct(const CVector2d& vector) const
221{
222 return GetX() * vector.GetX() + GetY() * vector.GetY();
223}
224
225
226inline 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
250inline CVector2d CVector2d::operator*(double scalar) const
251{
252 return CVector2d(GetX() * scalar, GetY() * scalar);
253}
254
255
256inline 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
282inline CVector2d& CVector2d::operator*=(double scalar)
283{
284 BaseClass::operator*=(scalar);
285
286 return *this;
287}
288
289
290inline CVector2d& CVector2d::operator/=(double scalar)
291{
292 BaseClass::operator/=(scalar);
293
294 return *this;
295}
296
297
298inline 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
314inline 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
326
Definition of position or mathematical vector on 2D plane.
Definition CVector2d.h:29
double GetY() const
Get Y position of this vector.
Definition CVector2d.h:184
static const CVector2d & GetZero()
Definition CVector2d.h:306
double GetX() const
Get X position of this vector.
Definition CVector2d.h:172
CVector2d & operator-=(const imath::TVector< 2 > &vector)
Definition CVector2d.h:274
CVector2d operator/(double scalar) const
Definition CVector2d.h:256
void SetY(double y)
Set Y position of this vector.
Definition CVector2d.h:190
CVector2d & operator+=(const imath::TVector< 2 > &vector)
Definition CVector2d.h:266
CVector2d & operator*=(double scalar)
Definition CVector2d.h:282
CVector2d operator-() const
Definition CVector2d.h:232
CVector2d()
Default constructor.
Definition CVector2d.h:147
double GetDotProduct(const CVector2d &vector) const
Definition CVector2d.h:220
istd::CIndex2d ToIndex2d() const
Get vector converted to 2D index.
bool Serialize(iser::IArchive &archive)
Serialize this vector to specified archive.
CVector2d & operator/=(double scalar)
Definition CVector2d.h:290
CVector2d GetHorizontalTranslated(double offsetX) const
Get horizontal translated point.
Definition CVector2d.h:202
imath::TVector< 2 > BaseClass
Definition CVector2d.h:31
double GetCrossProductZ(const imath::TVector< 2 > &vector) const
Return Z coordinate of two vectors cross product.
Definition CVector2d.h:214
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.
double GetAngle() const
Get angle of this vector.
Definition CVector2d.h:226
void SetX(double x)
Set X position of this vector.
Definition CVector2d.h:178
CVector2d GetVerticalTranslated(double offsetY) const
Get vertical translated point.
Definition CVector2d.h:208
CVector2d GetTranslated(const imath::TVector< 2 > &vector)
Get translated point.
Definition CVector2d.h:196
void Init(double angle, double length=1.0)
Init this vector using angle and vector length.
CVector2d GetOrthogonal() const
Get orthogonal vector.
CVector2d operator*(double scalar) const
Definition CVector2d.h:250
Implementation of fixed-size mathematical vector with specified type of elements.
Definition TVector.h:95
const double & operator[](int i) const
Definition TVector.h:882
TVector< Size, double > & operator+=(const TVector< Size, double > &vector)
Definition TVector.h:773
TVector< Size, double > & operator/=(double scalar)
Definition TVector.h:806
TVector< Size, double > & operator-=(const TVector< Size, double > &vector)
Definition TVector.h:784
TVector< Size, double > & operator*=(double scalar)
Definition TVector.h:795
TVector< Size, double > GetTranslated(const TVector< Size, double > &vector)
Returns a new vector that is the translation of this vector.
Definition TVector.h:617
static const TVector< Size, double > & GetZero()
Get vector with all coordinates set to 0.
Definition TVector.h:918
bool GetNormalized(TVector< Size, double > &result, double length=1.0) const
Returns a normalized copy of this vector with specified length.
Definition TVector.h:960
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
Index implementation for addressing elements in 2D-space.
Definition CIndex2d.h:21
int GetX() const
Definition CIndex2d.h:96
int GetY() const
Definition CIndex2d.h:108
static const double I_BIG_EPSILON
Definition istd.h:26
Contains the 2D objects.
Definition CAffine2d.h:11
uint qHash(const CLine2d &key, uint seed=0)
Definition CLine2d.h:433