vector2.inl 8.0 KB

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