vector2.inl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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 vector2.inl
  35. * @brief Inline implementation of aiVector2t<TReal> operators
  36. */
  37. #pragma once
  38. #ifndef AI_VECTOR2D_INL_INC
  39. #define AI_VECTOR2D_INL_INC
  40. #ifdef __GNUC__
  41. # pragma GCC system_header
  42. #endif
  43. #ifdef __cplusplus
  44. #include <assimp/vector2.h>
  45. #include <cmath>
  46. // ------------------------------------------------------------------------------------------------
  47. template <typename TReal>
  48. template <typename TOther>
  49. aiVector2t<TReal>::operator aiVector2t<TOther> () const {
  50. return aiVector2t<TOther>(static_cast<TOther>(x),static_cast<TOther>(y));
  51. }
  52. // ------------------------------------------------------------------------------------------------
  53. template <typename TReal>
  54. inline
  55. void aiVector2t<TReal>::Set( TReal pX, TReal pY) {
  56. x = pX; y = pY;
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. template <typename TReal>
  60. inline
  61. TReal aiVector2t<TReal>::SquareLength() const {
  62. return x*x + y*y;
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. template <typename TReal>
  66. inline
  67. TReal aiVector2t<TReal>::Length() const {
  68. return std::sqrt( SquareLength());
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. template <typename TReal>
  72. inline
  73. aiVector2t<TReal>& aiVector2t<TReal>::Normalize() {
  74. *this /= Length();
  75. return *this;
  76. }
  77. // ------------------------------------------------------------------------------------------------
  78. template <typename TReal>
  79. inline
  80. const aiVector2t<TReal>& aiVector2t<TReal>::operator += (const aiVector2t& o) {
  81. x += o.x; y += o.y;
  82. return *this;
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. template <typename TReal>
  86. inline
  87. const aiVector2t<TReal>& aiVector2t<TReal>::operator -= (const aiVector2t& o) {
  88. x -= o.x; y -= o.y;
  89. return *this;
  90. }
  91. // ------------------------------------------------------------------------------------------------
  92. template <typename TReal>
  93. inline
  94. const aiVector2t<TReal>& aiVector2t<TReal>::operator *= (TReal f) {
  95. x *= f; y *= f;
  96. return *this;
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. template <typename TReal>
  100. inline
  101. const aiVector2t<TReal>& aiVector2t<TReal>::operator /= (TReal f) {
  102. x /= f; y /= f;
  103. return *this;
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. template <typename TReal>
  107. inline
  108. TReal aiVector2t<TReal>::operator[](unsigned int i) const {
  109. switch (i) {
  110. case 0:
  111. return x;
  112. case 1:
  113. return y;
  114. default:
  115. break;
  116. }
  117. return x;
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. template <typename TReal>
  121. inline
  122. bool aiVector2t<TReal>::operator== (const aiVector2t& other) const {
  123. return x == other.x && y == other.y;
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. template <typename TReal>
  127. inline
  128. bool aiVector2t<TReal>::operator!= (const aiVector2t& other) const {
  129. return x != other.x || y != other.y;
  130. }
  131. // ---------------------------------------------------------------------------
  132. template<typename TReal>
  133. inline
  134. bool aiVector2t<TReal>::Equal(const aiVector2t& other, TReal epsilon) const {
  135. return
  136. std::abs(x - other.x) <= epsilon &&
  137. std::abs(y - other.y) <= epsilon;
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. template <typename TReal>
  141. inline
  142. aiVector2t<TReal>& aiVector2t<TReal>::operator= (TReal f) {
  143. x = y = f;
  144. return *this;
  145. }
  146. // ------------------------------------------------------------------------------------------------
  147. template <typename TReal>
  148. inline
  149. const aiVector2t<TReal> aiVector2t<TReal>::SymMul(const aiVector2t& o) {
  150. return aiVector2t(x*o.x,y*o.y);
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. // symmetric addition
  154. template <typename TReal>
  155. inline
  156. aiVector2t<TReal> operator + (const aiVector2t<TReal>& v1, const aiVector2t<TReal>& v2) {
  157. return aiVector2t<TReal>( v1.x + v2.x, v1.y + v2.y);
  158. }
  159. // ------------------------------------------------------------------------------------------------
  160. // symmetric subtraction
  161. template <typename TReal>
  162. inline
  163. aiVector2t<TReal> operator - (const aiVector2t<TReal>& v1, const aiVector2t<TReal>& v2) {
  164. return aiVector2t<TReal>( v1.x - v2.x, v1.y - v2.y);
  165. }
  166. // ------------------------------------------------------------------------------------------------
  167. // scalar product
  168. template <typename TReal>
  169. inline
  170. TReal operator * (const aiVector2t<TReal>& v1, const aiVector2t<TReal>& v2) {
  171. return v1.x*v2.x + v1.y*v2.y;
  172. }
  173. // ------------------------------------------------------------------------------------------------
  174. // scalar multiplication
  175. template <typename TReal>
  176. inline
  177. aiVector2t<TReal> operator * ( TReal f, const aiVector2t<TReal>& v) {
  178. return aiVector2t<TReal>( f*v.x, f*v.y);
  179. }
  180. // ------------------------------------------------------------------------------------------------
  181. // and the other way around
  182. template <typename TReal>
  183. inline
  184. aiVector2t<TReal> operator * ( const aiVector2t<TReal>& v, TReal f) {
  185. return aiVector2t<TReal>( f*v.x, f*v.y);
  186. }
  187. // ------------------------------------------------------------------------------------------------
  188. // scalar division
  189. template <typename TReal>
  190. inline
  191. aiVector2t<TReal> operator / ( const aiVector2t<TReal>& v, TReal f) {
  192. return v * (1/f);
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. // vector division
  196. template <typename TReal>
  197. inline
  198. aiVector2t<TReal> operator / ( const aiVector2t<TReal>& v, const aiVector2t<TReal>& v2) {
  199. return aiVector2t<TReal>(v.x / v2.x,v.y / v2.y);
  200. }
  201. // ------------------------------------------------------------------------------------------------
  202. // vector negation
  203. template <typename TReal>
  204. inline
  205. aiVector2t<TReal> operator - ( const aiVector2t<TReal>& v) {
  206. return aiVector2t<TReal>( -v.x, -v.y);
  207. }
  208. #endif
  209. #endif // AI_VECTOR2D_INL_INC