PolyVector3.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include <math.h>
  22. //#ifdef _WINDOWS
  23. #include <assert.h>
  24. //#endif
  25. namespace Polycode {
  26. /**
  27. * 3D Vector class.
  28. */
  29. class _PolyExport Vector3 : public PolyBase {
  30. public:
  31. /**
  32. * Create from x,y,z coordinates.
  33. * @param x X coordinate.
  34. * @param y Y coordinate.
  35. * @param z Z coordinate.
  36. */
  37. Vector3(Number x,Number y,Number z);
  38. /**
  39. * Create from single value for all coordinates
  40. * @param val Value for all coordinates
  41. */
  42. Vector3(Number val);
  43. /**
  44. * Default constructor.
  45. */
  46. Vector3();
  47. virtual ~Vector3();
  48. /**
  49. * Sets the vector from x,y,z coordinates.
  50. * @param x X coordinate.
  51. * @param y Y coordinate.
  52. * @param z Z coordinate.
  53. */
  54. void set(Number x, Number y, Number z);
  55. inline Vector3 operator - ( const Vector3& v2 ) const {
  56. return Vector3(x - v2.x, y - v2.y, z - v2.z);
  57. }
  58. /**
  59. * Returns the distance from this vector to another one.
  60. * @param vec2 Second vector.
  61. * @return Distance to the other vector.
  62. */
  63. inline Number distance(const Vector3& vec2) const {
  64. return (*this - vec2).length();
  65. }
  66. // ----------------------------------------------------------------------------------------------------------------
  67. /** @name Operators
  68. * Available vector operators.
  69. */
  70. //@{
  71. inline Vector3 operator * (const Number val) const {
  72. return Vector3(x * val, y * val, z * val);
  73. }
  74. inline Vector3 operator * (const Vector3 &v2) const {
  75. return Vector3(x * v2.x, y * v2.y, z * v2.z);
  76. }
  77. inline Vector3 operator / (const Number val) const {
  78. assert( val != 0.0 );
  79. return operator*(1/val);
  80. }
  81. inline Vector3& operator = ( const Vector3& v2) {
  82. x = v2.x;
  83. y = v2.y;
  84. z = v2.z;
  85. return *this;
  86. }
  87. inline Vector3& operator += ( const Vector3& v2) {
  88. x += v2.x;
  89. y += v2.y;
  90. z += v2.z;
  91. return *this;
  92. }
  93. inline Vector3& operator -= ( const Vector3& v2) {
  94. x -= v2.x;
  95. y -= v2.y;
  96. z -= v2.z;
  97. return *this;
  98. }
  99. inline Vector3 operator + ( const Vector3& v2 ) const {
  100. return Vector3(x + v2.x, y + v2.y, z + v2.z);
  101. }
  102. inline Vector3 operator - () {
  103. return Vector3(-x, -y, -z);
  104. }
  105. inline bool operator == ( const Vector3& v2) {
  106. return (v2.x == x && v2.y == y && v2.z == z);
  107. }
  108. inline bool operator != ( const Vector3& v2) {
  109. return (v2.x != x || v2.y != y || v2.z != z);
  110. }
  111. //@}
  112. // ----------------------------------------------------------------------------------------------------------------
  113. /**
  114. * Returns the angle between this and the specified vectors.
  115. * @return Angle between the vectors
  116. */
  117. inline Number angleBetween(const Vector3& dest) {
  118. Number lenProduct = length() * dest.length();
  119. if(lenProduct < 1e-6f)
  120. lenProduct = 1e-6f;
  121. Number f = dot(dest) / lenProduct;
  122. f = clampf(f, -1.0, 1.0);
  123. return acosf(f);
  124. }
  125. /**
  126. * Returns the vector length.
  127. * @return Length of the vector.
  128. */
  129. inline Number length () const {
  130. return sqrtf( x * x + y * y + z * z );
  131. }
  132. /**
  133. * Returns square of the length of the vector.
  134. * Cheaper to execute than length(), for use when you're just e.g. comparing vector lengths.
  135. * @return Square length of the vector.
  136. */
  137. inline Number lengthSquared() const {
  138. return dot(*this);
  139. }
  140. inline Vector3 setLength(const Number newLength) {
  141. Number oldLength = length();
  142. if(oldLength != 0 && newLength != oldLength) {
  143. (*this) = (*this) * (newLength / oldLength);
  144. }
  145. return (*this);
  146. }
  147. /**
  148. * Returns the dot product with another vector.
  149. * @return Dor product with the vector.
  150. */
  151. inline Number dot(const Vector3 &u) const {
  152. return x * u.x + y * u.y + z * u.z;
  153. }
  154. /**
  155. * Returns the cross product with another vector.
  156. * @param vec2 Second vector.
  157. * @return Cross product with the vector.
  158. */
  159. inline Vector3 crossProduct( const Vector3& vec2 ) const {
  160. return Vector3(
  161. y * vec2.z - z * vec2.y,
  162. z * vec2.x - x * vec2.z,
  163. x * vec2.y - y * vec2.x);
  164. }
  165. /**
  166. * Normalizes the vector.
  167. */
  168. void Normalize();
  169. /**
  170. * X coordinate.
  171. */
  172. Number x;
  173. /**
  174. * Y coordinate.
  175. */
  176. Number y;
  177. /**
  178. * Z coordinate.
  179. */
  180. Number z;
  181. protected:
  182. };
  183. }