aiVector2D.h 4.7 KB

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