vec3.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Defines a 3-element vector class that can be used for
  16. * coordinate math, useful for animation systems and point manipulation.
  17. *
  18. * Based heavily on code originally by:
  19. * @author brenneman@google.com (Shawn Brenneman)
  20. */
  21. goog.provide('goog.math.Vec3');
  22. goog.require('goog.math');
  23. goog.require('goog.math.Coordinate3');
  24. /**
  25. * Class for a three-dimensional vector object and assorted functions useful for
  26. * manipulation.
  27. *
  28. * Inherits from goog.math.Coordinate3 so that a Vec3 may be passed in to any
  29. * function that requires a Coordinate.
  30. *
  31. * @param {number} x The x value for the vector.
  32. * @param {number} y The y value for the vector.
  33. * @param {number} z The z value for the vector.
  34. * @struct
  35. * @constructor
  36. * @extends {goog.math.Coordinate3}
  37. */
  38. goog.math.Vec3 = function(x, y, z) {
  39. /**
  40. * X-value
  41. * @type {number}
  42. */
  43. this.x = x;
  44. /**
  45. * Y-value
  46. * @type {number}
  47. */
  48. this.y = y;
  49. /**
  50. * Z-value
  51. * @type {number}
  52. */
  53. this.z = z;
  54. };
  55. goog.inherits(goog.math.Vec3, goog.math.Coordinate3);
  56. /**
  57. * Generates a random unit vector.
  58. *
  59. * http://mathworld.wolfram.com/SpherePointPicking.html
  60. * Using (6), (7), and (8) to generate coordinates.
  61. * @return {!goog.math.Vec3} A random unit-length vector.
  62. */
  63. goog.math.Vec3.randomUnit = function() {
  64. var theta = Math.random() * Math.PI * 2;
  65. var phi = Math.random() * Math.PI * 2;
  66. var z = Math.cos(phi);
  67. var x = Math.sqrt(1 - z * z) * Math.cos(theta);
  68. var y = Math.sqrt(1 - z * z) * Math.sin(theta);
  69. return new goog.math.Vec3(x, y, z);
  70. };
  71. /**
  72. * Generates a random vector inside the unit sphere.
  73. *
  74. * @return {!goog.math.Vec3} A random vector.
  75. */
  76. goog.math.Vec3.random = function() {
  77. return goog.math.Vec3.randomUnit().scale(Math.random());
  78. };
  79. /**
  80. * Returns a new Vec3 object from a given coordinate.
  81. *
  82. * @param {goog.math.Coordinate3} a The coordinate.
  83. * @return {!goog.math.Vec3} A new vector object.
  84. */
  85. goog.math.Vec3.fromCoordinate3 = function(a) {
  86. return new goog.math.Vec3(a.x, a.y, a.z);
  87. };
  88. /**
  89. * Creates a new copy of this Vec3.
  90. *
  91. * @return {!goog.math.Vec3} A new vector with the same coordinates as this one.
  92. * @override
  93. */
  94. goog.math.Vec3.prototype.clone = function() {
  95. return new goog.math.Vec3(this.x, this.y, this.z);
  96. };
  97. /**
  98. * Returns the magnitude of the vector measured from the origin.
  99. *
  100. * @return {number} The length of the vector.
  101. */
  102. goog.math.Vec3.prototype.magnitude = function() {
  103. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  104. };
  105. /**
  106. * Returns the squared magnitude of the vector measured from the origin.
  107. * NOTE(brenneman): Leaving out the square root is not a significant
  108. * optimization in JavaScript.
  109. *
  110. * @return {number} The length of the vector, squared.
  111. */
  112. goog.math.Vec3.prototype.squaredMagnitude = function() {
  113. return this.x * this.x + this.y * this.y + this.z * this.z;
  114. };
  115. /**
  116. * Scales the current vector by a constant.
  117. *
  118. * @param {number} s The scale factor.
  119. * @return {!goog.math.Vec3} This vector, scaled.
  120. */
  121. goog.math.Vec3.prototype.scale = function(s) {
  122. this.x *= s;
  123. this.y *= s;
  124. this.z *= s;
  125. return this;
  126. };
  127. /**
  128. * Reverses the sign of the vector. Equivalent to scaling the vector by -1.
  129. *
  130. * @return {!goog.math.Vec3} This vector, inverted.
  131. */
  132. goog.math.Vec3.prototype.invert = function() {
  133. this.x = -this.x;
  134. this.y = -this.y;
  135. this.z = -this.z;
  136. return this;
  137. };
  138. /**
  139. * Normalizes the current vector to have a magnitude of 1.
  140. *
  141. * @return {!goog.math.Vec3} This vector, normalized.
  142. */
  143. goog.math.Vec3.prototype.normalize = function() {
  144. return this.scale(1 / this.magnitude());
  145. };
  146. /**
  147. * Adds another vector to this vector in-place.
  148. *
  149. * @param {goog.math.Vec3} b The vector to add.
  150. * @return {!goog.math.Vec3} This vector with {@code b} added.
  151. */
  152. goog.math.Vec3.prototype.add = function(b) {
  153. this.x += b.x;
  154. this.y += b.y;
  155. this.z += b.z;
  156. return this;
  157. };
  158. /**
  159. * Subtracts another vector from this vector in-place.
  160. *
  161. * @param {goog.math.Vec3} b The vector to subtract.
  162. * @return {!goog.math.Vec3} This vector with {@code b} subtracted.
  163. */
  164. goog.math.Vec3.prototype.subtract = function(b) {
  165. this.x -= b.x;
  166. this.y -= b.y;
  167. this.z -= b.z;
  168. return this;
  169. };
  170. /**
  171. * Compares this vector with another for equality.
  172. *
  173. * @param {goog.math.Vec3} b The other vector.
  174. * @return {boolean} True if this vector's x, y and z equal the given vector's
  175. * x, y, and z, respectively.
  176. */
  177. goog.math.Vec3.prototype.equals = function(b) {
  178. return this == b || !!b && this.x == b.x && this.y == b.y && this.z == b.z;
  179. };
  180. /**
  181. * Returns the distance between two vectors.
  182. *
  183. * @param {goog.math.Vec3} a The first vector.
  184. * @param {goog.math.Vec3} b The second vector.
  185. * @return {number} The distance.
  186. */
  187. goog.math.Vec3.distance = goog.math.Coordinate3.distance;
  188. /**
  189. * Returns the squared distance between two vectors.
  190. *
  191. * @param {goog.math.Vec3} a The first vector.
  192. * @param {goog.math.Vec3} b The second vector.
  193. * @return {number} The squared distance.
  194. */
  195. goog.math.Vec3.squaredDistance = goog.math.Coordinate3.squaredDistance;
  196. /**
  197. * Compares vectors for equality.
  198. *
  199. * @param {goog.math.Vec3} a The first vector.
  200. * @param {goog.math.Vec3} b The second vector.
  201. * @return {boolean} True if the vectors have equal x, y, and z coordinates.
  202. */
  203. goog.math.Vec3.equals = goog.math.Coordinate3.equals;
  204. /**
  205. * Returns the sum of two vectors as a new Vec3.
  206. *
  207. * @param {goog.math.Vec3} a The first vector.
  208. * @param {goog.math.Vec3} b The second vector.
  209. * @return {!goog.math.Vec3} The sum vector.
  210. */
  211. goog.math.Vec3.sum = function(a, b) {
  212. return new goog.math.Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
  213. };
  214. /**
  215. * Returns the difference of two vectors as a new Vec3.
  216. *
  217. * @param {goog.math.Vec3} a The first vector.
  218. * @param {goog.math.Vec3} b The second vector.
  219. * @return {!goog.math.Vec3} The difference vector.
  220. */
  221. goog.math.Vec3.difference = function(a, b) {
  222. return new goog.math.Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
  223. };
  224. /**
  225. * Returns the dot-product of two vectors.
  226. *
  227. * @param {goog.math.Vec3} a The first vector.
  228. * @param {goog.math.Vec3} b The second vector.
  229. * @return {number} The dot-product of the two vectors.
  230. */
  231. goog.math.Vec3.dot = function(a, b) {
  232. return a.x * b.x + a.y * b.y + a.z * b.z;
  233. };
  234. /**
  235. * Returns the cross-product of two vectors.
  236. *
  237. * @param {goog.math.Vec3} a The first vector.
  238. * @param {goog.math.Vec3} b The second vector.
  239. * @return {!goog.math.Vec3} The cross-product of the two vectors.
  240. */
  241. goog.math.Vec3.cross = function(a, b) {
  242. return new goog.math.Vec3(
  243. a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  244. };
  245. /**
  246. * Returns a new Vec3 that is the linear interpolant between vectors a and b at
  247. * scale-value x.
  248. *
  249. * @param {goog.math.Vec3} a Vector a.
  250. * @param {goog.math.Vec3} b Vector b.
  251. * @param {number} x The proportion between a and b.
  252. * @return {!goog.math.Vec3} The interpolated vector.
  253. */
  254. goog.math.Vec3.lerp = function(a, b, x) {
  255. return new goog.math.Vec3(
  256. goog.math.lerp(a.x, b.x, x), goog.math.lerp(a.y, b.y, x),
  257. goog.math.lerp(a.z, b.z, x));
  258. };