Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

229 linhas
10 KiB

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