FVector.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GEOMETRY_FVECTOR
  24. #define DFPSR_GEOMETRY_FVECTOR
  25. #include "vectorMethods.h"
  26. namespace dsr {
  27. struct FVector2D {
  28. VECTOR_BODY_2D(FVector2D, float, 0.0f);
  29. };
  30. struct FVector3D {
  31. VECTOR_BODY_3D(FVector3D, float, 0.0f);
  32. };
  33. struct FVector4D {
  34. VECTOR_BODY_4D(FVector4D, float, 0.0f);
  35. };
  36. OPERATORS_2D(FVector2D, float);
  37. OPERATORS_3D(FVector3D, float);
  38. OPERATORS_4D(FVector4D, float);
  39. SIGNED_OPERATORS_2D(FVector2D, float);
  40. SIGNED_OPERATORS_3D(FVector3D, float);
  41. SIGNED_OPERATORS_4D(FVector4D, float);
  42. SERIALIZATION_2D(FVector2D);
  43. SERIALIZATION_3D(FVector3D);
  44. SERIALIZATION_4D(FVector4D);
  45. inline bool operator==(const FVector2D &left, const FVector2D &right) {
  46. return fabs(left.x - right.x) < 0.0001f && fabs(left.y - right.y) < 0.0001f;
  47. }
  48. inline bool operator==(const FVector3D &left, const FVector3D &right) {
  49. return fabs(left.x - right.x) < 0.0001f && fabs(left.y - right.y) < 0.0001f && fabs(left.z - right.z) < 0.0001f;
  50. }
  51. inline bool operator==(const FVector4D &left, const FVector4D &right) {
  52. return fabs(left.x - right.x) < 0.0001f && fabs(left.y - right.y) < 0.0001f && fabs(left.z - right.z) < 0.0001f && fabs(left.w - right.w) < 0.0001f;
  53. }
  54. inline bool operator!=(const FVector2D &left, const FVector2D &right) {
  55. return !(left == right);
  56. }
  57. inline bool operator!=(const FVector3D &left, const FVector3D &right) {
  58. return !(left == right);
  59. }
  60. inline bool operator!=(const FVector4D &left, const FVector4D &right) {
  61. return !(left == right);
  62. }
  63. inline float dotProduct(const FVector2D &a, const FVector2D &b) {
  64. return (a.x * b.x) + (a.y * b.y);
  65. }
  66. inline float dotProduct(const FVector3D &a, const FVector3D &b) {
  67. return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
  68. }
  69. inline float dotProduct(const FVector4D &a, const FVector4D &b) {
  70. return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
  71. }
  72. inline float squareLength(const FVector2D &v) {
  73. return v.x * v.x + v.y * v.y;
  74. }
  75. inline float squareLength(const FVector3D &v) {
  76. return v.x * v.x + v.y * v.y + v.z * v.z;
  77. }
  78. inline float squareLength(const FVector4D &v) {
  79. return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
  80. }
  81. inline float length(const FVector2D &v) {
  82. return sqrtf(squareLength(v));
  83. }
  84. inline float length(const FVector3D &v) {
  85. return sqrtf(squareLength(v));
  86. }
  87. inline float length(const FVector4D &v) {
  88. return sqrtf(squareLength(v));
  89. }
  90. inline FVector3D crossProduct(const FVector3D &a, const FVector3D &b) {
  91. return FVector3D(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  92. }
  93. inline FVector2D normalize(const FVector2D &v) {
  94. float l = length(v);
  95. if (l == 0.0f) {
  96. return FVector2D(0.0f, 1.0f);
  97. } else {
  98. return v / length(v);
  99. }
  100. }
  101. inline FVector3D normalize(const FVector3D &v) {
  102. float l = length(v);
  103. if (l == 0.0f) {
  104. return FVector3D(0.0f, 0.0f, 1.0f);
  105. } else {
  106. return v / length(v);
  107. }
  108. }
  109. inline FVector4D normalize(const FVector4D &v) {
  110. float l = length(v);
  111. if (l == 0.0f) {
  112. return FVector4D(0.0f, 0.0f, 0.0f, 1.0f);
  113. } else {
  114. return v / length(v);
  115. }
  116. }
  117. }
  118. #endif