Vector3D.lua

Vector3D.lua (9.1.0.39617; unchanged since 8.3.0.33062)
  1. -- Protecting from addons since we use this in secure code.
  2. local cos = math.cos;
  3. local sin = math.sin;
  4. local atan2 = math.atan2;
  5. local asin = math.asin;
  6. local sqrt = math.sqrt;
  7. function Vector3D_ScaleBy(scalar, x, y, z)
  8. return x * scalar, y * scalar, z * scalar;
  9. end
  10. function Vector3D_DivideBy(divisor, x, y, z)
  11. return x / divisor, y / divisor, z / divisor;
  12. end
  13. function Vector3D_Add(leftX, leftY, leftZ, rightX, rightY, rightZ)
  14. return leftX + rightX, leftY + rightY, leftZ + rightZ;
  15. end
  16. function Vector3D_Subtract(leftX, leftY, leftZ, rightX, rightY, rightZ)
  17. return leftX - rightX, leftY - rightY, leftZ - rightZ;
  18. end
  19. function Vector3D_Cross(leftX, leftY, leftZ, rightX, rightY, rightZ)
  20. return leftY * rightZ - leftZ * rightY, leftZ * rightX - leftX * rightZ, leftX * rightY - leftY * rightX;
  21. end
  22. function Vector3D_Dot(leftX, leftY, leftZ, rightX, rightY, rightZ)
  23. return leftX * rightX + leftY * rightY + leftZ * rightZ;
  24. end
  25. function Vector3D_GetLengthSquared(x, y, z)
  26. return Vector3D_Dot(x, y, z, x, y, z);
  27. end
  28. function Vector3D_GetLength(x, y, z)
  29. return sqrt(Vector3D_GetLengthSquared(x, y, z));
  30. end
  31. function Vector3D_Normalize(x, y, z)
  32. return Vector3D_DivideBy(Vector3D_GetLength(x, y, z), x, y, z);
  33. end
  34. function Vector3D_AddVector(left, right)
  35. local clone = left:Clone();
  36. clone:Add(right);
  37. return clone;
  38. end
  39. function Vector3D_SubtractVector(left, right)
  40. local clone = left:Clone();
  41. clone:Subtract(right);
  42. return clone;
  43. end
  44. function Vector3D_NormalizeVector(vector)
  45. local clone = vector:Clone();
  46. clone:Normalize();
  47. return clone;
  48. end
  49. function Vector3D_ScaleVector(scalar, vector)
  50. local clone = vector:Clone();
  51. clone:ScaleBy(scalar);
  52. return clone;
  53. end
  54. function Vector3D_CalculateNormalFromYawPitch(yaw, pitch)
  55. return cos(-pitch) * cos(yaw),
  56. cos(-pitch) * sin(yaw),
  57. sin(-pitch);
  58. end
  59. function Vector3D_CalculateYawPitchFromNormal(x, y, z)
  60. if x ~= 0 or y ~= 0 then
  61. return atan2(y, x), asin(-z);
  62. end
  63. return 0, asin(-z);
  64. end
  65. function Vector3D_CalculateYawPitchFromNormalVector(vector)
  66. return Vector3D_CalculateYawPitchFromNormal(vector:GetXYZ());
  67. end
  68. function Vector3D_CreateNormalVectorFromYawPitch(yawRadians, pitchRadians)
  69. return CreateVector3D(Vector3D_CalculateNormalFromYawPitch(yawRadians, pitchRadians));
  70. end
  71. Vector3DMixin = {};
  72. function CreateVector3D(x, y, z)
  73. local vector = CreateFromMixins(Vector3DMixin);
  74. vector:OnLoad(x, y, z);
  75. return vector;
  76. end
  77. function AreVector3DEqual(left, right)
  78. if left and right then
  79. return left:IsEqualTo(right);
  80. end
  81. return left == right;
  82. end
  83. function Vector3DMixin:OnLoad(x, y, z)
  84. self:SetXYZ(x, y, z);
  85. end
  86. function Vector3DMixin:IsEqualTo(otherVector)
  87. return self.x == otherVector.x
  88. and self.y == otherVector.y
  89. and self.z == otherVector.z;
  90. end
  91. function Vector3DMixin:GetXYZ()
  92. return self.x, self.y, self.z;
  93. end
  94. function Vector3DMixin:SetXYZ(x, y, z)
  95. self.x = x;
  96. self.y = y;
  97. self.z = z;
  98. end
  99. function Vector3DMixin:ScaleBy(scalar)
  100. self:SetXYZ(Vector3D_ScaleBy(scalar, self:GetXYZ()));
  101. end
  102. function Vector3DMixin:DivideBy(scalar)
  103. self:SetXYZ(Vector3D_DivideBy(scalar, self:GetXYZ()));
  104. end
  105. function Vector3DMixin:Add(other)
  106. self:SetXYZ(Vector3D_Add(self.x, self.y, self.z, other:GetXYZ()));
  107. end
  108. function Vector3DMixin:Subtract(other)
  109. self:SetXYZ(Vector3D_Subtract(self.x, self.y, self.z, other:GetXYZ()));
  110. end
  111. function Vector3DMixin:Cross(other)
  112. self:SetXYZ(Vector3D_Cross(self.x, self.y, self.z, other:GetXYZ()));
  113. end
  114. function Vector3DMixin:Dot(other)
  115. return Vector3D_Dot(self.x, self.y, self.z, other:GetXYZ());
  116. end
  117. function Vector3DMixin:GetLengthSquared()
  118. return Vector3D_GetLengthSquared(self:GetXYZ());
  119. end
  120. function Vector3DMixin:GetLength()
  121. return Vector3D_GetLength(self:GetXYZ());
  122. end
  123. function Vector3DMixin:Normalize()
  124. self:SetXYZ(Vector3D_Normalize(self:GetXYZ()));
  125. end
  126. function Vector3DMixin:Clone()
  127. return CreateVector3D(self:GetXYZ());
  128. end