1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qvector4d.h"
#include "qvector3d.h"
#include "qvector2d.h"
#include <QtCore/qdatastream.h>
#include <QtCore/qdebug.h>
#include <QtCore/qvariant.h>
#include <QtCore/qmath.h>
QT_BEGIN_NAMESPACE
#ifndef QT_NO_VECTOR4D
/*!
\class QVector4D
\brief The QVector4D class represents a vector or vertex in 4D space.
\since 4.6
\ingroup painting-3D
\inmodule QtGui
The QVector4D class can also be used to represent vertices in 4D space.
We therefore do not need to provide a separate vertex class.
\sa QQuaternion, QVector2D, QVector3D
*/
/*!
\fn QVector4D::QVector4D()
Constructs a null vector, i.e. with coordinates (0, 0, 0, 0).
*/
/*!
\fn QVector4D::QVector4D(Qt::Initialization)
\since 5.5
\internal
Constructs a vector without initializing the contents.
*/
/*!
\fn QVector4D::QVector4D(float xpos, float ypos, float zpos, float wpos)
Constructs a vector with coordinates (\a xpos, \a ypos, \a zpos, \a wpos).
*/
/*!
\fn QVector4D::QVector4D(const QPoint& point)
Constructs a vector with x and y coordinates from a 2D \a point, and
z and w coordinates of 0.
*/
/*!
\fn QVector4D::QVector4D(const QPointF& point)
Constructs a vector with x and y coordinates from a 2D \a point, and
z and w coordinates of 0.
*/
#ifndef QT_NO_VECTOR2D
/*!
Constructs a 4D vector from the specified 2D \a vector. The z
and w coordinates are set to zero.
\sa toVector2D()
*/
QVector4D::QVector4D(const QVector2D& vector)
{
xp = vector.xp;
yp = vector.yp;
zp = 0.0f;
wp = 0.0f;
}
/*!
Constructs a 4D vector from the specified 2D \a vector. The z
and w coordinates are set to \a zpos and \a wpos respectively.
\sa toVector2D()
*/
QVector4D::QVector4D(const QVector2D& vector, float zpos, float wpos)
{
xp = vector.xp;
yp = vector.yp;
zp = zpos;
wp = wpos;
}
#endif
#ifndef QT_NO_VECTOR3D
/*!
Constructs a 4D vector from the specified 3D \a vector. The w
coordinate is set to zero.
\sa toVector3D()
*/
QVector4D::QVector4D(const QVector3D& vector)
{
xp = vector.xp;
yp = vector.yp;
zp = vector.zp;
wp = 0.0f;
}
/*!
Constructs a 4D vector from the specified 3D \a vector. The w
coordinate is set to \a wpos.
\sa toVector3D()
*/
QVector4D::QVector4D(const QVector3D& vector, float wpos)
{
xp = vector.xp;
yp = vector.yp;
zp = vector.zp;
wp = wpos;
}
#endif
/*!
\fn bool QVector4D::isNull() const
Returns \c true if the x, y, z, and w coordinates are set to 0.0,
otherwise returns \c false.
*/
/*!
\fn float QVector4D::x() const
Returns the x coordinate of this point.
\sa setX(), y(), z(), w()
*/
/*!
\fn float QVector4D::y() const
Returns the y coordinate of this point.
\sa setY(), x(), z(), w()
*/
/*!
\fn float QVector4D::z() const
Returns the z coordinate of this point.
\sa setZ(), x(), y(), w()
*/
/*!
\fn float QVector4D::w() const
Returns the w coordinate of this point.
\sa setW(), x(), y(), z()
*/
/*!
\fn void QVector4D::setX(float x)
Sets the x coordinate of this point to the given \a x coordinate.
\sa x(), setY(), setZ(), setW()
*/
/*!
\fn void QVector4D::setY(float y)
Sets the y coordinate of this point to the given \a y coordinate.
\sa y(), setX(), setZ(), setW()
*/
/*!
\fn void QVector4D::setZ(float z)
Sets the z coordinate of this point to the given \a z coordinate.
\sa z(), setX(), setY(), setW()
*/
/*!
\fn void QVector4D::setW(float w)
Sets the w coordinate of this point to the given \a w coordinate.
\sa w(), setX(), setY(), setZ()
*/
/*! \fn float &QVector4D::operator[](int i)
\since 5.2
Returns the component of the vector at index position \a i
as a modifiable reference.
\a i must be a valid index position in the vector (i.e., 0 <= \a i
< 4).
*/
/*! \fn float QVector4D::operator[](int i) const
\since 5.2
Returns the component of the vector at index position \a i.
\a i must be a valid index position in the vector (i.e., 0 <= \a i
< 4).
*/
/*!
Returns the length of the vector from the origin.
\sa lengthSquared(), normalized()
*/
float QVector4D::length() const
{
// Need some extra precision if the length is very small.
double len = double(xp) * double(xp) +
double(yp) * double(yp) +
double(zp) * double(zp) +
double(wp) * double(wp);
return float(sqrt(len));
}
/*!
Returns the squared length of the vector from the origin.
This is equivalent to the dot product of the vector with itself.
\sa length(), dotProduct()
*/
float QVector4D::lengthSquared() const
{
return xp * xp + yp * yp + zp * zp + wp * wp;
}
/*!
Returns the normalized unit vector form of this vector.
If this vector is null, then a null vector is returned. If the length
of the vector is very close to 1, then the vector will be returned as-is.
Otherwise the normalized form of the vector of length 1 will be returned.
\sa length(), normalize()
*/
QVector4D QVector4D::normalized() const
{
// Need some extra precision if the length is very small.
double len = double(xp) * double(xp) +
double(yp) * double(yp) +
double(zp) * double(zp) +
double(wp) * double(wp);
if (qFuzzyIsNull(len - 1.0f)) {
return *this;
} else if (!qFuzzyIsNull(len)) {
double sqrtLen = sqrt(len);
return QVector4D(float(double(xp) / sqrtLen),
float(double(yp) / sqrtLen),
float(double(zp) / sqrtLen),
float(double(wp) / sqrtLen));
} else {
return QVector4D();
}
}
/*!
Normalizes the currect vector in place. Nothing happens if this
vector is a null vector or the length of the vector is very close to 1.
\sa length(), normalized()
*/
void QVector4D::normalize()
{
// Need some extra precision if the length is very small.
double len = double(xp) * double(xp) +
double(yp) * double(yp) +
double(zp) * double(zp) +
double(wp) * double(wp);
if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
return;
len = sqrt(len);
xp = float(double(xp) / len);
yp = float(double(yp) / len);
zp = float(double(zp) / len);
wp = float(double(wp) / len);
}
/*!
\fn QVector4D &QVector4D::operator+=(const QVector4D &vector)
Adds the given \a vector to this vector and returns a reference to
this vector.
\sa operator-=()
*/
/*!
\fn QVector4D &QVector4D::operator-=(const QVector4D &vector)
Subtracts the given \a vector from this vector and returns a reference to
this vector.
\sa operator+=()
*/
/*!
\fn QVector4D &QVector4D::operator*=(float factor)
Multiplies this vector's coordinates by the given \a factor, and
returns a reference to this vector.
\sa operator/=()
*/
/*!
\fn QVector4D &QVector4D::operator*=(const QVector4D &vector)
Multiplies the components of this vector by the corresponding
components in \a vector.
*/
/*!
\fn QVector4D &QVector4D::operator/=(float divisor)
Divides this vector's coordinates by the given \a divisor, and
returns a reference to this vector.
\sa operator*=()
*/
/*!
\fn QVector4D &QVector4D::operator/=(const QVector4D &vector)
\since 5.5
Divides the components of this vector by the corresponding
components in \a vector.
\sa operator*=()
*/
/*!
Returns the dot product of \a v1 and \a v2.
*/
float QVector4D::dotProduct(const QVector4D& v1, const QVector4D& v2)
{
return v1.xp * v2.xp + v1.yp * v2.yp + v1.zp * v2.zp + v1.wp * v2.wp;
}
/*!
\fn bool operator==(const QVector4D &v1, const QVector4D &v2)
\relates QVector4D
Returns \c true if \a v1 is equal to \a v2; otherwise returns \c false.
This operator uses an exact floating-point comparison.
*/
/*!
\fn bool operator!=(const QVector4D &v1, const QVector4D &v2)
\relates QVector4D
Returns \c true if \a v1 is not equal to \a v2; otherwise returns \c false.
This operator uses an exact floating-point comparison.
*/
/*!
\fn const QVector4D operator+(const QVector4D &v1, const QVector4D &v2)
\relates QVector4D
Returns a QVector4D object that is the sum of the given vectors, \a v1
and \a v2; each component is added separately.
\sa QVector4D::operator+=()
*/
/*!
\fn const QVector4D operator-(const QVector4D &v1, const QVector4D &v2)
\relates QVector4D
Returns a QVector4D object that is formed by subtracting \a v2 from \a v1;
each component is subtracted separately.
\sa QVector4D::operator-=()
*/
/*!
\fn const QVector4D operator*(float factor, const QVector4D &vector)
\relates QVector4D
Returns a copy of the given \a vector, multiplied by the given \a factor.
\sa QVector4D::operator*=()
*/
/*!
\fn const QVector4D operator*(const QVector4D &vector, float factor)
\relates QVector4D
Returns a copy of the given \a vector, multiplied by the given \a factor.
\sa QVector4D::operator*=()
*/
/*!
\fn const QVector4D operator*(const QVector4D &v1, const QVector4D& v2)
\relates QVector4D
Returns the vector consisting of the multiplication of the
components from \a v1 and \a v2.
\sa QVector4D::operator*=()
*/
/*!
\fn const QVector4D operator-(const QVector4D &vector)
\relates QVector4D
\overload
Returns a QVector4D object that is formed by changing the sign of
all three components of the given \a vector.
Equivalent to \c {QVector4D(0,0,0,0) - vector}.
*/
/*!
\fn const QVector4D operator/(const QVector4D &vector, float divisor)
\relates QVector4D
Returns the QVector4D object formed by dividing all four components of
the given \a vector by the given \a divisor.
\sa QVector4D::operator/=()
*/
/*!
\fn const QVector4D operator/(const QVector4D &vector, const QVector4D &divisor)
\relates QVector4D
\since 5.5
Returns the QVector4D object formed by dividing components of the given
\a vector by a respective components of the given \a divisor.
\sa QVector4D::operator/=()
*/
/*!
\fn bool qFuzzyCompare(const QVector4D& v1, const QVector4D& v2)
\relates QVector4D
Returns \c true if \a v1 and \a v2 are equal, allowing for a small
fuzziness factor for floating-point comparisons; false otherwise.
*/
#ifndef QT_NO_VECTOR2D
/*!
Returns the 2D vector form of this 4D vector, dropping the z and w coordinates.
\sa toVector2DAffine(), toVector3D(), toPoint()
*/
QVector2D QVector4D::toVector2D() const
{
return QVector2D(xp, yp);
}
/*!
Returns the 2D vector form of this 4D vector, dividing the x and y
coordinates by the w coordinate and dropping the z coordinate.
Returns a null vector if w is zero.
\sa toVector2D(), toVector3DAffine(), toPoint()
*/
QVector2D QVector4D::toVector2DAffine() const
{
if (qIsNull(wp))
return QVector2D();
return QVector2D(xp / wp, yp / wp);
}
#endif
#ifndef QT_NO_VECTOR3D
/*!
Returns the 3D vector form of this 4D vector, dropping the w coordinate.
\sa toVector3DAffine(), toVector2D(), toPoint()
*/
QVector3D QVector4D::toVector3D() const
{
return QVector3D(xp, yp, zp);
}
/*!
Returns the 3D vector form of this 4D vector, dividing the x, y, and
z coordinates by the w coordinate. Returns a null vector if w is zero.
\sa toVector3D(), toVector2DAffine(), toPoint()
*/
QVector3D QVector4D::toVector3DAffine() const
{
if (qIsNull(wp))
return QVector3D();
return QVector3D(xp / wp, yp / wp, zp / wp);
}
#endif
/*!
\fn QPoint QVector4D::toPoint() const
Returns the QPoint form of this 4D vector. The z and w coordinates
are dropped.
\sa toPointF(), toVector2D()
*/
/*!
\fn QPointF QVector4D::toPointF() const
Returns the QPointF form of this 4D vector. The z and w coordinates
are dropped.
\sa toPoint(), toVector2D()
*/
/*!
Returns the 4D vector as a QVariant.
*/
QVector4D::operator QVariant() const
{
return QVariant(QVariant::Vector4D, this);
}
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QVector4D &vector)
{
dbg.nospace() << "QVector4D("
<< vector.x() << ", " << vector.y() << ", "
<< vector.z() << ", " << vector.w() << ')';
return dbg.space();
}
#endif
#ifndef QT_NO_DATASTREAM
/*!
\fn QDataStream &operator<<(QDataStream &stream, const QVector4D &vector)
\relates QVector4D
Writes the given \a vector to the given \a stream and returns a
reference to the stream.
\sa {Serializing Qt Data Types}
*/
QDataStream &operator<<(QDataStream &stream, const QVector4D &vector)
{
stream << vector.x() << vector.y()
<< vector.z() << vector.w();
return stream;
}
/*!
\fn QDataStream &operator>>(QDataStream &stream, QVector4D &vector)
\relates QVector4D
Reads a 4D vector from the given \a stream into the given \a vector
and returns a reference to the stream.
\sa {Serializing Qt Data Types}
*/
QDataStream &operator>>(QDataStream &stream, QVector4D &vector)
{
float x, y, z, w;
stream >> x;
stream >> y;
stream >> z;
stream >> w;
vector.setX(x);
vector.setY(y);
vector.setZ(z);
vector.setW(w);
return stream;
}
#endif // QT_NO_DATASTREAM
#endif // QT_NO_VECTOR4D
QT_END_NAMESPACE
|