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