1/*
2---------------------------------------------------------------------------
3Open Asset Import Library (assimp)
4---------------------------------------------------------------------------
5
6Copyright (c) 2006-2019, assimp team
7
8
9
10All rights reserved.
11
12Redistribution and use of this software in source and binary forms,
13with or without modification, are permitted provided that the following
14conditions are met:
15
16* Redistributions of source code must retain the above
17 copyright notice, this list of conditions and the
18 following disclaimer.
19
20* Redistributions in binary form must reproduce the above
21 copyright notice, this list of conditions and the
22 following disclaimer in the documentation and/or other
23 materials provided with the distribution.
24
25* Neither the name of the assimp team, nor the names of its
26 contributors may be used to endorse or promote products
27 derived from this software without specific prior
28 written permission of the assimp team.
29
30THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41---------------------------------------------------------------------------
42*/
43
44/** @file vector3.inl
45 * @brief Inline implementation of aiVector3t<TReal> operators
46 */
47#pragma once
48#ifndef AI_VECTOR3D_INL_INC
49#define AI_VECTOR3D_INL_INC
50
51#ifdef __cplusplus
52#include "vector3.h"
53
54#include <cmath>
55
56// ------------------------------------------------------------------------------------------------
57/** Transformation of a vector by a 3x3 matrix */
58template <typename TReal>
59AI_FORCE_INLINE
60aiVector3t<TReal> operator * (const aiMatrix3x3t<TReal>& pMatrix, const aiVector3t<TReal>& pVector) {
61 aiVector3t<TReal> res;
62 res.x = pMatrix.a1 * pVector.x + pMatrix.a2 * pVector.y + pMatrix.a3 * pVector.z;
63 res.y = pMatrix.b1 * pVector.x + pMatrix.b2 * pVector.y + pMatrix.b3 * pVector.z;
64 res.z = pMatrix.c1 * pVector.x + pMatrix.c2 * pVector.y + pMatrix.c3 * pVector.z;
65 return res;
66}
67
68// ------------------------------------------------------------------------------------------------
69/** Transformation of a vector by a 4x4 matrix */
70template <typename TReal>
71AI_FORCE_INLINE
72aiVector3t<TReal> operator * (const aiMatrix4x4t<TReal>& pMatrix, const aiVector3t<TReal>& pVector) {
73 aiVector3t<TReal> res;
74 res.x = pMatrix.a1 * pVector.x + pMatrix.a2 * pVector.y + pMatrix.a3 * pVector.z + pMatrix.a4;
75 res.y = pMatrix.b1 * pVector.x + pMatrix.b2 * pVector.y + pMatrix.b3 * pVector.z + pMatrix.b4;
76 res.z = pMatrix.c1 * pVector.x + pMatrix.c2 * pVector.y + pMatrix.c3 * pVector.z + pMatrix.c4;
77 return res;
78}
79// ------------------------------------------------------------------------------------------------
80template <typename TReal>
81template <typename TOther>
82aiVector3t<TReal>::operator aiVector3t<TOther> () const {
83 return aiVector3t<TOther>(static_cast<TOther>(x),static_cast<TOther>(y),static_cast<TOther>(z));
84}
85// ------------------------------------------------------------------------------------------------
86template <typename TReal>
87AI_FORCE_INLINE
88void aiVector3t<TReal>::Set( TReal pX, TReal pY, TReal pZ) {
89 x = pX;
90 y = pY;
91 z = pZ;
92}
93// ------------------------------------------------------------------------------------------------
94template <typename TReal>
95AI_FORCE_INLINE
96TReal aiVector3t<TReal>::SquareLength() const {
97 return x*x + y*y + z*z;
98}
99// ------------------------------------------------------------------------------------------------
100template <typename TReal>
101AI_FORCE_INLINE
102TReal aiVector3t<TReal>::Length() const {
103 return std::sqrt( SquareLength());
104}
105// ------------------------------------------------------------------------------------------------
106template <typename TReal>
107AI_FORCE_INLINE
108aiVector3t<TReal>& aiVector3t<TReal>::Normalize() {
109 *this /= Length();
110
111 return *this;
112}
113// ------------------------------------------------------------------------------------------------
114template <typename TReal>
115AI_FORCE_INLINE
116aiVector3t<TReal>& aiVector3t<TReal>::NormalizeSafe() {
117 TReal len = Length();
118 if ( len > static_cast< TReal >( 0 ) ) {
119 *this /= len;
120 }
121 return *this;
122}
123// ------------------------------------------------------------------------------------------------
124template <typename TReal>
125AI_FORCE_INLINE
126const aiVector3t<TReal>& aiVector3t<TReal>::operator += (const aiVector3t<TReal>& o) {
127 x += o.x;
128 y += o.y;
129 z += o.z;
130
131 return *this;
132}
133// ------------------------------------------------------------------------------------------------
134template <typename TReal>
135AI_FORCE_INLINE
136const aiVector3t<TReal>& aiVector3t<TReal>::operator -= (const aiVector3t<TReal>& o) {
137 x -= o.x;
138 y -= o.y;
139 z -= o.z;
140
141 return *this;
142}
143// ------------------------------------------------------------------------------------------------
144template <typename TReal>
145AI_FORCE_INLINE
146const aiVector3t<TReal>& aiVector3t<TReal>::operator *= (TReal f) {
147 x *= f;
148 y *= f;
149 z *= f;
150
151 return *this;
152}
153// ------------------------------------------------------------------------------------------------
154template <typename TReal>
155AI_FORCE_INLINE
156const aiVector3t<TReal>& aiVector3t<TReal>::operator /= (TReal f) {
157 const TReal invF = (TReal) 1.0 / f;
158 x *= invF;
159 y *= invF;
160 z *= invF;
161
162 return *this;
163}
164// ------------------------------------------------------------------------------------------------
165template <typename TReal>
166AI_FORCE_INLINE
167aiVector3t<TReal>& aiVector3t<TReal>::operator *= (const aiMatrix3x3t<TReal>& mat){
168 return (*this = mat * (*this));
169}
170// ------------------------------------------------------------------------------------------------
171template <typename TReal>
172AI_FORCE_INLINE
173aiVector3t<TReal>& aiVector3t<TReal>::operator *= (const aiMatrix4x4t<TReal>& mat){
174 return (*this = mat * (*this));
175}
176// ------------------------------------------------------------------------------------------------
177template <typename TReal>
178AI_FORCE_INLINE
179TReal aiVector3t<TReal>::operator[](unsigned int i) const {
180 switch (i) {
181 case 0:
182 return x;
183 case 1:
184 return y;
185 case 2:
186 return z;
187 default:
188 break;
189 }
190 return x;
191}
192// ------------------------------------------------------------------------------------------------
193template <typename TReal>
194AI_FORCE_INLINE
195TReal& aiVector3t<TReal>::operator[](unsigned int i) {
196// return *(&x + i);
197 switch (i) {
198 case 0:
199 return x;
200 case 1:
201 return y;
202 case 2:
203 return z;
204 default:
205 break;
206 }
207 return x;
208}
209// ------------------------------------------------------------------------------------------------
210template <typename TReal>
211AI_FORCE_INLINE
212bool aiVector3t<TReal>::operator== (const aiVector3t<TReal>& other) const {
213 return x == other.x && y == other.y && z == other.z;
214}
215// ------------------------------------------------------------------------------------------------
216template <typename TReal>
217AI_FORCE_INLINE
218bool aiVector3t<TReal>::operator!= (const aiVector3t<TReal>& other) const {
219 return x != other.x || y != other.y || z != other.z;
220}
221// ---------------------------------------------------------------------------
222template<typename TReal>
223AI_FORCE_INLINE
224bool aiVector3t<TReal>::Equal(const aiVector3t<TReal>& other, TReal epsilon) const {
225 return
226 std::abs(x - other.x) <= epsilon &&
227 std::abs(y - other.y) <= epsilon &&
228 std::abs(z - other.z) <= epsilon;
229}
230// ------------------------------------------------------------------------------------------------
231template <typename TReal>
232AI_FORCE_INLINE
233bool aiVector3t<TReal>::operator < (const aiVector3t<TReal>& other) const {
234 return x != other.x ? x < other.x : y != other.y ? y < other.y : z < other.z;
235}
236// ------------------------------------------------------------------------------------------------
237template <typename TReal>
238AI_FORCE_INLINE
239const aiVector3t<TReal> aiVector3t<TReal>::SymMul(const aiVector3t<TReal>& o) {
240 return aiVector3t<TReal>(x*o.x,y*o.y,z*o.z);
241}
242// ------------------------------------------------------------------------------------------------
243// symmetric addition
244template <typename TReal>
245AI_FORCE_INLINE
246aiVector3t<TReal> operator + (const aiVector3t<TReal>& v1, const aiVector3t<TReal>& v2) {
247 return aiVector3t<TReal>( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
248}
249// ------------------------------------------------------------------------------------------------
250// symmetric subtraction
251template <typename TReal>
252AI_FORCE_INLINE
253aiVector3t<TReal> operator - (const aiVector3t<TReal>& v1, const aiVector3t<TReal>& v2) {
254 return aiVector3t<TReal>( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
255}
256// ------------------------------------------------------------------------------------------------
257// scalar product
258template <typename TReal>
259AI_FORCE_INLINE
260TReal operator * (const aiVector3t<TReal>& v1, const aiVector3t<TReal>& v2) {
261 return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
262}
263// ------------------------------------------------------------------------------------------------
264// scalar multiplication
265template <typename TReal>
266AI_FORCE_INLINE
267aiVector3t<TReal> operator * ( TReal f, const aiVector3t<TReal>& v) {
268 return aiVector3t<TReal>( f*v.x, f*v.y, f*v.z);
269}
270// ------------------------------------------------------------------------------------------------
271// and the other way around
272template <typename TReal>
273AI_FORCE_INLINE
274aiVector3t<TReal> operator * ( const aiVector3t<TReal>& v, TReal f) {
275 return aiVector3t<TReal>( f*v.x, f*v.y, f*v.z);
276}
277// ------------------------------------------------------------------------------------------------
278// scalar division
279template <typename TReal>
280AI_FORCE_INLINE
281aiVector3t<TReal> operator / ( const aiVector3t<TReal>& v, TReal f) {
282 return v * (1/f);
283}
284// ------------------------------------------------------------------------------------------------
285// vector division
286template <typename TReal>
287AI_FORCE_INLINE
288aiVector3t<TReal> operator / ( const aiVector3t<TReal>& v, const aiVector3t<TReal>& v2) {
289 return aiVector3t<TReal>(v.x / v2.x,v.y / v2.y,v.z / v2.z);
290}
291// ------------------------------------------------------------------------------------------------
292// cross product
293template<typename TReal>
294AI_FORCE_INLINE
295aiVector3t<TReal> operator ^ ( const aiVector3t<TReal>& v1, const aiVector3t<TReal>& v2) {
296 return aiVector3t<TReal>( v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
297}
298// ------------------------------------------------------------------------------------------------
299// vector negation
300template<typename TReal>
301AI_FORCE_INLINE
302aiVector3t<TReal> operator - ( const aiVector3t<TReal>& v) {
303 return aiVector3t<TReal>( -v.x, -v.y, -v.z);
304}
305
306// ------------------------------------------------------------------------------------------------
307
308#endif // __cplusplus
309#endif // AI_VECTOR3D_INL_INC
310

source code of qt3d/src/3rdparty/assimp/src/include/assimp/vector3.inl