PolyVector4.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * 4D Vector class.
  28. */
  29. class _PolyExport Vector4 : 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. * @param w W coordinate.
  37. */
  38. Vector4(Number x,Number y,Number z, Number w);
  39. /**
  40. * Create from single value for all coordinates
  41. * @param val Value for all coordinates
  42. */
  43. Vector4(Number val);
  44. /**
  45. * Default constructor.
  46. */
  47. Vector4();
  48. virtual ~Vector4();
  49. /**
  50. * Sets the vector from x,y,z coordinates.
  51. * @param x X coordinate.
  52. * @param y Y coordinate.
  53. * @param z Z coordinate.
  54. * @param w W coordinate
  55. */
  56. void set(Number x, Number y, Number z, Number w);
  57. inline Vector4 operator - ( const Vector4& v2 ) const {
  58. return Vector4(x - v2.x, y - v2.y, z - v2.z, w - v2.w);
  59. }
  60. // ----------------------------------------------------------------------------------------------------------------
  61. /** @name Operators
  62. * Available vector operators.
  63. */
  64. //@{
  65. inline Vector4 operator * (const Number val) const {
  66. return Vector4(x * val, y * val, z * val, w * val);
  67. }
  68. inline Vector4 operator * (const Vector4 &v2) const {
  69. return Vector4(x * v2.x, y * v2.y, z * v2.z, w * v2.w);
  70. }
  71. inline Vector4 operator / (const Number val) const {
  72. assert( val != 0.0 );
  73. return operator*(1/val);
  74. }
  75. inline Vector4& operator = ( const Vector4& v2) {
  76. x = v2.x;
  77. y = v2.y;
  78. z = v2.z;
  79. w = v2.w;
  80. return *this;
  81. }
  82. inline Vector4& operator += ( const Vector4& v2) {
  83. x += v2.x;
  84. y += v2.y;
  85. z += v2.z;
  86. w += v2.w;
  87. return *this;
  88. }
  89. inline Vector4& operator -= ( const Vector4& v2) {
  90. x -= v2.x;
  91. y -= v2.y;
  92. z -= v2.z;
  93. w -= v2.w;
  94. return *this;
  95. }
  96. inline Vector4 operator + ( const Vector4& v2 ) const {
  97. return Vector4(x + v2.x, y + v2.y, z + v2.z, w + v2.w);
  98. }
  99. inline Vector4 operator - () {
  100. return Vector4(-x, -y, -z, -w);
  101. }
  102. inline bool operator == ( const Vector4& v2) {
  103. return (v2.x == x && v2.y == y && v2.z == z && v2.w == w);
  104. }
  105. inline bool operator != ( const Vector4& v2) {
  106. return (v2.x != x || v2.y != y || v2.z != z || v2.w != w);
  107. }
  108. //@}
  109. // ----------------------------------------------------------------------------------------------------------------
  110. /**
  111. * Returns the dot product with another vector.
  112. * @return Dor product with the vector.
  113. */
  114. inline Number dot(const Vector4 &u) const {
  115. return x * u.x + y * u.y + z * u.z + w * u.w;
  116. }
  117. /**
  118. * X coordinate.
  119. */
  120. Number x;
  121. /**
  122. * Y coordinate.
  123. */
  124. Number y;
  125. /**
  126. * Z coordinate.
  127. */
  128. Number z;
  129. /**
  130. * W coordinate.
  131. */
  132. Number w;
  133. };
  134. }