1 | /* |
2 | --------------------------------------------------------------------------- |
3 | Open Asset Import Library (assimp) |
4 | --------------------------------------------------------------------------- |
5 | |
6 | Copyright (c) 2006-2019, assimp team |
7 | |
8 | |
9 | |
10 | All rights reserved. |
11 | |
12 | Redistribution and use of this software in source and binary forms, |
13 | with or without modification, are permitted provided that the following |
14 | conditions 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 | |
30 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
31 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
32 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
33 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
34 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
35 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
36 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
37 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
38 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
39 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
40 | OF 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 */ |
58 | template <typename TReal> |
59 | AI_FORCE_INLINE |
60 | aiVector3t<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 */ |
70 | template <typename TReal> |
71 | AI_FORCE_INLINE |
72 | aiVector3t<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 | // ------------------------------------------------------------------------------------------------ |
80 | template <typename TReal> |
81 | template <typename TOther> |
82 | aiVector3t<TReal>::operator aiVector3t<TOther> () const { |
83 | return aiVector3t<TOther>(static_cast<TOther>(x),static_cast<TOther>(y),static_cast<TOther>(z)); |
84 | } |
85 | // ------------------------------------------------------------------------------------------------ |
86 | template <typename TReal> |
87 | AI_FORCE_INLINE |
88 | void aiVector3t<TReal>::Set( TReal pX, TReal pY, TReal pZ) { |
89 | x = pX; |
90 | y = pY; |
91 | z = pZ; |
92 | } |
93 | // ------------------------------------------------------------------------------------------------ |
94 | template <typename TReal> |
95 | AI_FORCE_INLINE |
96 | TReal aiVector3t<TReal>::SquareLength() const { |
97 | return x*x + y*y + z*z; |
98 | } |
99 | // ------------------------------------------------------------------------------------------------ |
100 | template <typename TReal> |
101 | AI_FORCE_INLINE |
102 | TReal aiVector3t<TReal>::Length() const { |
103 | return std::sqrt( SquareLength()); |
104 | } |
105 | // ------------------------------------------------------------------------------------------------ |
106 | template <typename TReal> |
107 | AI_FORCE_INLINE |
108 | aiVector3t<TReal>& aiVector3t<TReal>::Normalize() { |
109 | *this /= Length(); |
110 | |
111 | return *this; |
112 | } |
113 | // ------------------------------------------------------------------------------------------------ |
114 | template <typename TReal> |
115 | AI_FORCE_INLINE |
116 | aiVector3t<TReal>& aiVector3t<TReal>::NormalizeSafe() { |
117 | TReal len = Length(); |
118 | if ( len > static_cast< TReal >( 0 ) ) { |
119 | *this /= len; |
120 | } |
121 | return *this; |
122 | } |
123 | // ------------------------------------------------------------------------------------------------ |
124 | template <typename TReal> |
125 | AI_FORCE_INLINE |
126 | const 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 | // ------------------------------------------------------------------------------------------------ |
134 | template <typename TReal> |
135 | AI_FORCE_INLINE |
136 | const 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 | // ------------------------------------------------------------------------------------------------ |
144 | template <typename TReal> |
145 | AI_FORCE_INLINE |
146 | const aiVector3t<TReal>& aiVector3t<TReal>::operator *= (TReal f) { |
147 | x *= f; |
148 | y *= f; |
149 | z *= f; |
150 | |
151 | return *this; |
152 | } |
153 | // ------------------------------------------------------------------------------------------------ |
154 | template <typename TReal> |
155 | AI_FORCE_INLINE |
156 | const 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 | // ------------------------------------------------------------------------------------------------ |
165 | template <typename TReal> |
166 | AI_FORCE_INLINE |
167 | aiVector3t<TReal>& aiVector3t<TReal>::operator *= (const aiMatrix3x3t<TReal>& mat){ |
168 | return (*this = mat * (*this)); |
169 | } |
170 | // ------------------------------------------------------------------------------------------------ |
171 | template <typename TReal> |
172 | AI_FORCE_INLINE |
173 | aiVector3t<TReal>& aiVector3t<TReal>::operator *= (const aiMatrix4x4t<TReal>& mat){ |
174 | return (*this = mat * (*this)); |
175 | } |
176 | // ------------------------------------------------------------------------------------------------ |
177 | template <typename TReal> |
178 | AI_FORCE_INLINE |
179 | TReal 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 | // ------------------------------------------------------------------------------------------------ |
193 | template <typename TReal> |
194 | AI_FORCE_INLINE |
195 | TReal& 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 | // ------------------------------------------------------------------------------------------------ |
210 | template <typename TReal> |
211 | AI_FORCE_INLINE |
212 | bool aiVector3t<TReal>::operator== (const aiVector3t<TReal>& other) const { |
213 | return x == other.x && y == other.y && z == other.z; |
214 | } |
215 | // ------------------------------------------------------------------------------------------------ |
216 | template <typename TReal> |
217 | AI_FORCE_INLINE |
218 | bool aiVector3t<TReal>::operator!= (const aiVector3t<TReal>& other) const { |
219 | return x != other.x || y != other.y || z != other.z; |
220 | } |
221 | // --------------------------------------------------------------------------- |
222 | template<typename TReal> |
223 | AI_FORCE_INLINE |
224 | bool 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 | // ------------------------------------------------------------------------------------------------ |
231 | template <typename TReal> |
232 | AI_FORCE_INLINE |
233 | bool 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 | // ------------------------------------------------------------------------------------------------ |
237 | template <typename TReal> |
238 | AI_FORCE_INLINE |
239 | const 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 |
244 | template <typename TReal> |
245 | AI_FORCE_INLINE |
246 | aiVector3t<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 |
251 | template <typename TReal> |
252 | AI_FORCE_INLINE |
253 | aiVector3t<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 |
258 | template <typename TReal> |
259 | AI_FORCE_INLINE |
260 | TReal 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 |
265 | template <typename TReal> |
266 | AI_FORCE_INLINE |
267 | aiVector3t<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 |
272 | template <typename TReal> |
273 | AI_FORCE_INLINE |
274 | aiVector3t<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 |
279 | template <typename TReal> |
280 | AI_FORCE_INLINE |
281 | aiVector3t<TReal> operator / ( const aiVector3t<TReal>& v, TReal f) { |
282 | return v * (1/f); |
283 | } |
284 | // ------------------------------------------------------------------------------------------------ |
285 | // vector division |
286 | template <typename TReal> |
287 | AI_FORCE_INLINE |
288 | aiVector3t<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 |
293 | template<typename TReal> |
294 | AI_FORCE_INLINE |
295 | aiVector3t<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 |
300 | template<typename TReal> |
301 | AI_FORCE_INLINE |
302 | aiVector3t<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 | |