You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vector3.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * encantar.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022-2024 Alexandre Martins <alemartf(at)gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * vector3.ts
  20. * 3D vectors
  21. */
  22. /** Small number */
  23. const EPSILON = 1e-6;
  24. // public / non-internal methods do not change the contents of the quaternion
  25. /**
  26. * A vector in 3D space
  27. */
  28. export class Vector3
  29. {
  30. /** x coordinate */
  31. public x: number;
  32. /** y coordinate */
  33. public y: number;
  34. /** z coordinate */
  35. public z: number;
  36. /**
  37. * Constructor
  38. */
  39. constructor(x: number = 0, y: number = 0, z: number = 0)
  40. {
  41. this.x = +x;
  42. this.y = +y;
  43. this.z = +z;
  44. }
  45. /**
  46. * Instantiate a zero vector
  47. * @returns a new zero vector
  48. */
  49. static Zero(): Vector3
  50. {
  51. return new Vector3(0, 0, 0);
  52. }
  53. /**
  54. * The length of this vector
  55. * @returns sqrt(x^2 + y^2 + z^2)
  56. */
  57. length(): number
  58. {
  59. const x = this.x;
  60. const y = this.y;
  61. const z = this.z;
  62. return Math.sqrt(x*x + y*y + z*z);
  63. }
  64. /**
  65. * Check if this and v have the same coordinates
  66. * @param v a vector
  67. * @returns true if this and v have the same coordinates
  68. */
  69. equals(v: Vector3): boolean
  70. {
  71. return this.x === v.x && this.y === v.y && this.z === v.z;
  72. }
  73. /**
  74. * Convert to string
  75. * @returns a string
  76. */
  77. toString(): string
  78. {
  79. const x = this.x.toFixed(5);
  80. const y = this.y.toFixed(5);
  81. const z = this.z.toFixed(5);
  82. return `Vector3(${x},${y},${z})`;
  83. }
  84. /**
  85. * Normalize this vector
  86. * @returns this vector, normalized
  87. * @internal
  88. */
  89. _normalize(): Vector3
  90. {
  91. const length = this.length();
  92. if(length < EPSILON) // zero?
  93. return this;
  94. this.x /= length;
  95. this.y /= length;
  96. this.z /= length;
  97. return this;
  98. }
  99. /**
  100. * Copy v to this
  101. * @param v a vector
  102. * @returns this vector
  103. * @internal
  104. */
  105. _copyFrom(v: Vector3): Vector3
  106. {
  107. this.x = v.x;
  108. this.y = v.y;
  109. this.z = v.z;
  110. return this;
  111. }
  112. /**
  113. * Set the coordinates of this vector
  114. * @param x x-coordinate
  115. * @param y y-coordinate
  116. * @param z z-coordinate
  117. * @returns this vector
  118. * @internal
  119. */
  120. _set(x: number, y: number, z: number): Vector3
  121. {
  122. this.x = +x;
  123. this.y = +y;
  124. this.z = +z;
  125. return this;
  126. }
  127. }