aiVector2D.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, ASSIMP Development 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 Development 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.h
  35. * @brief 2D vector structure, including operators when compiling in C++
  36. */
  37. #ifndef AI_VECTOR2D_H_INC
  38. #define AI_VECTOR2D_H_INC
  39. #include <math.h>
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. #include "./Compiler/pushpack1.h"
  44. // ----------------------------------------------------------------------------------
  45. /** Represents a two-dimensional vector.
  46. */
  47. struct aiVector2D
  48. {
  49. #ifdef __cplusplus
  50. aiVector2D () : x(0.0f), y(0.0f) {}
  51. aiVector2D (float _x, float _y) : x(_x), y(_y) {}
  52. aiVector2D (float _xyz) : x(_xyz), y(_xyz) {}
  53. aiVector2D (const aiVector2D& o) : x(o.x), y(o.y) {}
  54. void Set( float pX, float pY) {
  55. x = pX; y = pY;
  56. }
  57. float SquareLength() const {
  58. return x*x + y*y;
  59. }
  60. float Length() const {
  61. return ::sqrt( SquareLength());
  62. }
  63. aiVector2D& Normalize() {
  64. *this /= Length(); return *this;
  65. }
  66. const aiVector2D& operator += (const aiVector2D& o) {
  67. x += o.x; y += o.y; return *this;
  68. }
  69. const aiVector2D& operator -= (const aiVector2D& o) {
  70. x -= o.x; y -= o.y; return *this;
  71. }
  72. const aiVector2D& operator *= (float f) {
  73. x *= f; y *= f; return *this;
  74. }
  75. const aiVector2D& operator /= (float f) {
  76. x /= f; y /= f; return *this;
  77. }
  78. float operator[](unsigned int i) const {
  79. return *(&x + i);
  80. }
  81. float& operator[](unsigned int i) {
  82. return *(&x + i);
  83. }
  84. bool operator== (const aiVector2D& other) const {
  85. return x == other.x && y == other.y;
  86. }
  87. bool operator!= (const aiVector2D& other) const {
  88. return x != other.x || y != other.y;
  89. }
  90. aiVector2D& operator= (float f) {
  91. x = y = f;return *this;
  92. }
  93. const aiVector2D SymMul(const aiVector2D& o) {
  94. return aiVector2D(x*o.x,y*o.y);
  95. }
  96. #endif // __cplusplus
  97. float x, y;
  98. } PACK_STRUCT;
  99. #include "./Compiler/poppack1.h"
  100. #ifdef __cplusplus
  101. } // end extern "C"
  102. // ----------------------------------------------------------------------------------
  103. // symmetric addition
  104. inline aiVector2D operator + (const aiVector2D& v1, const aiVector2D& v2)
  105. {
  106. return aiVector2D( v1.x + v2.x, v1.y + v2.y);
  107. }
  108. // ----------------------------------------------------------------------------------
  109. // symmetric subtraction
  110. inline aiVector2D operator - (const aiVector2D& v1, const aiVector2D& v2)
  111. {
  112. return aiVector2D( v1.x - v2.x, v1.y - v2.y);
  113. }
  114. // ----------------------------------------------------------------------------------
  115. // scalar product
  116. inline float operator * (const aiVector2D& v1, const aiVector2D& v2)
  117. {
  118. return v1.x*v2.x + v1.y*v2.y;
  119. }
  120. // ----------------------------------------------------------------------------------
  121. // scalar multiplication
  122. inline aiVector2D operator * ( float f, const aiVector2D& v)
  123. {
  124. return aiVector2D( f*v.x, f*v.y);
  125. }
  126. // ----------------------------------------------------------------------------------
  127. // and the other way around
  128. inline aiVector2D operator * ( const aiVector2D& v, float f)
  129. {
  130. return aiVector2D( f*v.x, f*v.y);
  131. }
  132. // ----------------------------------------------------------------------------------
  133. // scalar division
  134. inline aiVector2D operator / ( const aiVector2D& v, float f)
  135. {
  136. return v * (1/f);
  137. }
  138. // ----------------------------------------------------------------------------------
  139. // vector division
  140. inline aiVector2D operator / ( const aiVector2D& v, const aiVector2D& v2)
  141. {
  142. return aiVector2D(v.x / v2.x,v.y / v2.y);
  143. }
  144. // ----------------------------------------------------------------------------------
  145. // vector inversion
  146. inline aiVector2D operator - ( const aiVector2D& v)
  147. {
  148. return aiVector2D( -v.x, -v.y);
  149. }
  150. #endif // __cplusplus
  151. #endif // AI_VECTOR2D_H_INC