summaryrefslogtreecommitdiffstats
path: root/src/system/Qt3DSVector3.cpp
blob: 2d790f95f9b26205fb22a6bfea0b555a3da57374 (plain)
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
/****************************************************************************
**
** Copyright (C) 1993-2009 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "SystemPrefix.h"

//==============================================================================
// Includes
//==============================================================================
#include "Qt3DSVector3.h"
#include "Qt3DSMatrix.h"
#include "Qt3DSDataLogger.h"
#include <math.h>

//==============================================================================
//	Namespace
//==============================================================================
namespace Q3DStudio {

//==============================================================================
//	Constants
//==============================================================================
extern const float RUNTIME_EPSILON = 0.0001f;
const float RUNTIME_SQUARE_EPSILON = 0.000001f;

//==============================================================================
/**
 *	Simple constructor - creates a NULL vector.
 */
RuntimeVector3::RuntimeVector3()
    : m_X(0.0f)
    , m_Y(0.0f)
    , m_Z(0.0f)
{
}

//==============================================================================
/**
 *	Construction from three array value.
 *	@param inVector		the source array
 */
RuntimeVector3::RuntimeVector3(const float inVector[3])
    : m_X(inVector[0])
    , m_Y(inVector[1])
    , m_Z(inVector[2])
{
}

//==============================================================================
/**
 *	Construction from three coordinates.
 *	@param inX		the m_X coordinate
 *	@param inY		the m_Y coordinate
 *	@param inZ		the m_Z coordinate
 */
RuntimeVector3::RuntimeVector3(const float inX, const float inY, const float inZ)
    : m_X(inX)
    , m_Y(inY)
    , m_Z(inZ)
{
}

//==============================================================================
/**
 *	Copy constructor
 *	@param inVector		the source vector
 */
RuntimeVector3::RuntimeVector3(const RuntimeVector3 &inVector)
    : m_X(inVector.m_X)
    , m_Y(inVector.m_Y)
    , m_Z(inVector.m_Z)
{
}

//==============================================================================
/**
 *	Assignment of coordinates.
 *	@param inX		the m_X coordinate
 *	@param inY		the m_Y coordinate
 *	@param inZ		the m_Z coordinate
 *	@return a reference to this modified vector
 */
RuntimeVector3 &RuntimeVector3::Set(const float inX, const float inY, const float inZ)
{
    m_X = inX;
    m_Y = inY;
    m_Z = inZ;
    return *this;
}

//==============================================================================
/**
 *	Assignment of coordinates using another vector.
 *	@param inVector		the vector to be copied.
 *	@return a reference to this modified vector
 */
RuntimeVector3 &RuntimeVector3::Set(const RuntimeVector3 &inVector)
{
    m_X = inVector.m_X;
    m_Y = inVector.m_Y;
    m_Z = inVector.m_Z;
    return *this;
}

//==============================================================================
/**
 *	Compare this vector with another. Exact match is not needed but instead
 *	an error of EPSILON is allowed.
 *	@param inVector	the vector we are compared with
 *	@return	true if the vectors are close to identical
 *	@see	EPSILON
 */
bool RuntimeVector3::operator==(const RuntimeVector3 &inVector) const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);

    return (::fabsf(m_X - inVector.m_X) < RUNTIME_EPSILON) && (::fabsf(m_Y - inVector.m_Y) < RUNTIME_EPSILON)
        && (::fabsf(m_Z - inVector.m_Z) < RUNTIME_EPSILON);
}

//==============================================================================
/**
 *	Compare this vector with another. Exact match is not needed but instead
 *	an error of EPSILON is allowed.
 *	@param inVector		the vector we are compared with
 *	@return	true if the vectors are not even close to identical
 *	@see	EPSILON
 */
bool RuntimeVector3::operator!=(const RuntimeVector3 &inVector) const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);

    return (::fabsf(m_X - inVector.m_X) > RUNTIME_EPSILON) || (::fabsf(m_Y - inVector.m_Y) > RUNTIME_EPSILON)
        || (::fabsf(m_Z - inVector.m_Z) > RUNTIME_EPSILON);
}

//==============================================================================
/**
 *	Add this vector with another but do not modify this vector.
 *	@param inVector		the second vector being added
 *	@return a new vector
 */
RuntimeVector3 RuntimeVector3::operator+(const RuntimeVector3 &inVector) const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);

    return RuntimeVector3(m_X + inVector.m_X, m_Y + inVector.m_Y, m_Z + inVector.m_Z);
}

//==============================================================================
/**
 *	Add two vectors but do not modify this vector.
 *	@param inVector		vector being subtracted
 *	@return a new vector
 */
RuntimeVector3 RuntimeVector3::operator-(const RuntimeVector3 &inVector) const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    return RuntimeVector3(m_X - inVector.m_X, m_Y - inVector.m_Y, m_Z - inVector.m_Z);
}

//==============================================================================
/**
 *	Get a scaled copy of this vector.
 *	@param inFactor		the factor that scales each coordinate
 *	@return a new scaled vector
 */
RuntimeVector3 RuntimeVector3::operator*(float inFactor) const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    return RuntimeVector3(m_X * inFactor, m_Y * inFactor, m_Z * inFactor);
}

//==============================================================================
/**
 *	Invert the vector.
 *	@return	the inverted copy of this vector
 */
RuntimeVector3 RuntimeVector3::operator-() const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    return RuntimeVector3(-m_X, -m_Y, -m_Z);
}

//==============================================================================
/**
 *	Simple assignment.
 *	@param inVector		the new vector
 *	@return	a reference to this modified vector
 */
RuntimeVector3 &RuntimeVector3::operator=(const RuntimeVector3 &inVector)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    if (&inVector != this) {
        m_X = inVector.m_X;
        m_Y = inVector.m_Y;
        m_Z = inVector.m_Z;
    }
    return *this;
}

//==============================================================================
/**
 *	Increment this vector.
 *	@param inVector		has the coordinates by which this vector will be incremented
 *	@return	a reference to this modified vector
 */
RuntimeVector3 &RuntimeVector3::operator+=(const RuntimeVector3 &inVector)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    m_X += inVector.m_X;
    m_Y += inVector.m_Y;
    m_Z += inVector.m_Z;
    return *this;
}

//==============================================================================
/**
 *	Decrement this vector.
 *	@param inVector		has the coordinates by which this vector will be decremented
 *	@return	a reference to this modified vector
 */
RuntimeVector3 &RuntimeVector3::operator-=(const RuntimeVector3 &inVector)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    m_X -= inVector.m_X;
    m_Y -= inVector.m_Y;
    m_Z -= inVector.m_Z;
    return *this;
}

//==============================================================================
/**
 *	Scale this vector by a factor.
 *	@param inFactor		the scale factor
 *	@return	a reference to this modified vector
 */
RuntimeVector3 &RuntimeVector3::operator*=(float inFactor)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    m_X *= inFactor;
    m_Y *= inFactor;
    m_Z *= inFactor;
    return *this;
}

//==============================================================================
/**
 *	Calculates the squared distance between this vector and another.
 *	This is often used in sorting situations where the real distance isn't needed.
 *	@param inVector		vector to which the distance is being calculated
 *	@return	the squared distance between vectors
 */
float RuntimeVector3::DistanceSquared(const RuntimeVector3 &inVector) const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    return (m_X - inVector.m_X) * (m_X - inVector.m_X) + (m_Y - inVector.m_Y) * (m_Y - inVector.m_Y)
        + (m_Z - inVector.m_Z) * (m_Z - inVector.m_Z);
}

//==============================================================================
/**
 *	Calculates the distance between this vector and another.
 *	@param inVector		vector to which the distance is being calculated
 *	@return	the distance between vectors
 */
float RuntimeVector3::Distance(const RuntimeVector3 &inVector) const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    return ::sqrtf((m_X - inVector.m_X) * (m_X - inVector.m_X)
                   + (m_Y - inVector.m_Y) * (m_Y - inVector.m_Y)
                   + (m_Z - inVector.m_Z) * (m_Z - inVector.m_Z));
}

//==============================================================================
/**
 *	Calculates the squared length (squared magnitude) of the current vector.
 *	@return	the squared length of this vector
 */
float RuntimeVector3::LengthSquared() const
{
    return m_X * m_X + m_Y * m_Y + m_Z * m_Z;
}

//==============================================================================
/**
 *	Calculates the length (magnitude) of the current vector.
 *	@return	the length of this vector
 */
float RuntimeVector3::Length() const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    return ::sqrtf(m_X * m_X + m_Y * m_Y + m_Z * m_Z);
}

//==============================================================================
/**
 *	Calculates the dot product of this vector and another.
 *	@param inVector		vector measuring with
 *	@return	the dot product
 *	@see	operator*
 */
float RuntimeVector3::DotProduct(const RuntimeVector3 &inVector) const
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    return m_X * inVector.m_X + m_Y * inVector.m_Y + m_Z * inVector.m_Z;
}

//==============================================================================
/**
 *	Calculates the cross product of this vector and another and modifies this vector
 *	@param inVector		other vector
 *	@return this cross product vector
 *	@see	operator%
 *	@see	operator%=
 */
RuntimeVector3 &RuntimeVector3::CrossProduct(const RuntimeVector3 &inVector)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    float theX = m_Y * inVector.m_Z - m_Z * inVector.m_Y;
    float theY = m_Z * inVector.m_X - m_X * inVector.m_Z;
    float theZ = m_X * inVector.m_Y - m_Y * inVector.m_X;

    m_X = theX;
    m_Y = theY;
    m_Z = theZ;

    return *this;
}

//==============================================================================
/**
 *	Normalizes the current vector making it a unit vector.
 *	The normalized vector is defined to be: V_norm = V / Magnitude(V).
  *	@return	this normalized vector
*/
RuntimeVector3 &RuntimeVector3::Normalize()
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    float theLengthSquared = LengthSquared();
    if ((theLengthSquared > RUNTIME_SQUARE_EPSILON) && ::fabsf(theLengthSquared - 1.0f) > RUNTIME_SQUARE_EPSILON) {
        float theInvLength = 1.0f / ::sqrtf(theLengthSquared);
        m_X *= theInvLength;
        m_Y *= theInvLength;
        m_Z *= theInvLength;
    }
    return *this;
}

//==============================================================================
/**
 *	Modifies the current vector to contain elements that are the minimum between
 *	this vector and another.
 *	@param inVector		vector that whose elements are tested against the
 *						current vector to build the minimum.
 *	@return	this minimized vector
*/
RuntimeVector3 &RuntimeVector3::Minimize(const RuntimeVector3 &inVector)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    m_X = Q3DStudio_min<float>(m_X, inVector.m_X);
    m_Y = Q3DStudio_min<float>(m_Y, inVector.m_Y);
    m_Z = Q3DStudio_min<float>(m_Z, inVector.m_Z);
    return *this;
}

//==============================================================================
/**
 *	Modifies the current vector to contain elements that are the maximum between
 *	this vector and another.
 *	@param inVector		vector that whose elements are tested against the
 *						current vector to build the maximum.
 *	@return	this maximized vector
*/
RuntimeVector3 &RuntimeVector3::Maximize(const RuntimeVector3 &inVector)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    m_X = Q3DStudio_max<float>(m_X, inVector.m_X);
    m_Y = Q3DStudio_max<float>(m_Y, inVector.m_Y);
    m_Z = Q3DStudio_max<float>(m_Z, inVector.m_Z);
    return *this;
}

//==============================================================================
/**
 *	Modifies the current vector by performing a linear interpolation
 *	between the current vector and the given vector.
 *
 *	If inFactor is 0.0 then this vector remains unchanged.  If inFactor is
 *	1.0 then this vector becomes inDestVector. If inFactor is between 0.0-1.0
 *	then this vector becomes a vector between this old vector and inDestVector
 *	proportionally interpolated based in inFactor.  If inFactor is less than 0
 *	or more than 1 then this vector is extrapolated.
 *
 *	@param inDestVector		Second vector for the interpolation
 *	@param inFactor			Weight for the interpolation
 *	@return	this interpolated vector
 */
RuntimeVector3 &RuntimeVector3::InterpolateLinear(const RuntimeVector3 &inDestVector, float inFactor)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    m_X += inFactor * (inDestVector.m_X - m_X);
    m_Y += inFactor * (inDestVector.m_Y - m_Y);
    m_Z += inFactor * (inDestVector.m_Z - m_Z);
    return *this;
}

//==============================================================================
/**
 *	Transforms this vector by the given matrix.
 *
 *	The matrix is row column of the form Matrix[row][column]:
@code
        | m0	m1	m2	w |
        | m4	m5	m6	w |
        | m8	m9	m10	w |
        | tX	tY	tZ	w |
@endcode
 *	@param inMatrix		transform matrix
 *	@return	this transformed vector
 */
RuntimeVector3 &RuntimeVector3::Transform(const RuntimeMatrix &inMatrix)
{
    PerfLogMathEvent1(DATALOGGER_VECTOR);
    float theX = m_X;
    float theY = m_Y;
    float theZ = m_Z;

    m_X = theX * inMatrix.Get(0, 0) + theY * inMatrix.Get(1, 0) + theZ * inMatrix.Get(2, 0);
    m_Y = theX * inMatrix.Get(0, 1) + theY * inMatrix.Get(1, 1) + theZ * inMatrix.Get(2, 1);
    m_Z = theX * inMatrix.Get(0, 2) + theY * inMatrix.Get(1, 2) + theZ * inMatrix.Get(2, 2);

    m_X += inMatrix.Get(3, 0);
    m_Y += inMatrix.Get(3, 1);
    m_Z += inMatrix.Get(3, 2);

    return *this;
}

} // namespace Q3DStudio