glmaths 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/quat2.ts CHANGED
@@ -11,9 +11,9 @@ import { Mat4 } from './mat4'
11
11
  */
12
12
  export class Quat2 extends Float32Array {
13
13
 
14
- static get identity() { return new Quat2(0, 0, 0, 1, 0, 0, 0, 0) }
15
- static get Identity() { return new Quat2(0, 0, 0, 1, 0, 0, 0, 0) }
16
- static get IDENTITY() { return new Quat2(0, 0, 0, 1, 0, 0, 0, 0) }
14
+ static get identity() { return quat2(0, 0, 0, 1, 0, 0, 0, 0) }
15
+ static get Identity() { return quat2(0, 0, 0, 1, 0, 0, 0, 0) }
16
+ static get IDENTITY() { return quat2(0, 0, 0, 1, 0, 0, 0, 0) }
17
17
 
18
18
  /**
19
19
  * Creates a new dual quaternion
@@ -100,7 +100,7 @@ export class Quat2 extends Float32Array {
100
100
  * @param {Quat2} out the receiving dual quaternion, defaults to quat2()
101
101
  * @returns {Quat2} out
102
102
  */
103
- static fromRotationTranslation(q: Quat, t: Vec3, out = new Quat2()): Quat2 {
103
+ static fromRotationTranslation(q: Quat, t: Vec3, out = quat2()): Quat2 {
104
104
  const ax = t[0] * 0.5, ay = t[1] * 0.5, az = t[2] * 0.5
105
105
  const bx = q[0], by = q[1], bz = q[2], bw = q[3]
106
106
  out[0] = bx; out[1] = by; out[2] = bz; out[3] = bw
@@ -118,7 +118,7 @@ export class Quat2 extends Float32Array {
118
118
  * @param {Quat2} out the receiving dual quaternion, defaults to quat2()
119
119
  * @returns {Quat2} out
120
120
  */
121
- static fromTranslation(t: Vec3, out = new Quat2()): Quat2 {
121
+ static fromTranslation(t: Vec3, out = quat2()): Quat2 {
122
122
  out[0] = 0; out[1] = 0; out[2] = 0; out[3] = 1
123
123
  out[4] = t[0] * 0.5; out[5] = t[1] * 0.5; out[6] = t[2] * 0.5; out[7] = 0
124
124
  return out
@@ -131,7 +131,7 @@ export class Quat2 extends Float32Array {
131
131
  * @param {Quat2} out the receiving dual quaternion, defaults to quat2()
132
132
  * @returns {Quat2} out
133
133
  */
134
- static fromRotation(q: Quat, out = new Quat2()): Quat2 {
134
+ static fromRotation(q: Quat, out = quat2()): Quat2 {
135
135
  out[0] = q[0]; out[1] = q[1]; out[2] = q[2]; out[3] = q[3]
136
136
  out[4] = 0; out[5] = 0; out[6] = 0; out[7] = 0
137
137
  return out
@@ -144,7 +144,7 @@ export class Quat2 extends Float32Array {
144
144
  * @param {Quat2} out the receiving dual quaternion, defaults to quat2()
145
145
  * @returns {Quat2} out
146
146
  */
147
- static fromMat4(m: Mat4, out = new Quat2()): Quat2 {
147
+ static fromMat4(m: Mat4, out = quat2()): Quat2 {
148
148
  const r = m.getRotation()
149
149
  const t = m.getTranslation()
150
150
  return Quat2.fromRotationTranslation(r, t, out)
@@ -156,18 +156,18 @@ export class Quat2 extends Float32Array {
156
156
  * @returns {Quat2} a new dual quaternion
157
157
  */
158
158
  clone() {
159
- return new Quat2(this[0], this[1], this[2], this[3], this[4], this[5], this[6], this[7])
159
+ return quat2(this[0], this[1], this[2], this[3], this[4], this[5], this[6], this[7])
160
160
  }
161
161
 
162
162
  /**
163
163
  * Multiply two dual quaternions
164
164
  *
165
165
  * @param {Quat2} b the second operand
166
- * @param {Quat2} out the receiving dual quaternion, defaults to this
166
+ * @param {Quat2} out the receiving dual quaternion, defaults to new quat2()
167
167
  * @returns {Quat2} out
168
168
  */
169
169
  // @ts-ignore
170
- multiply = (b: Quat2, out = glmaths.ALWAYS_COPY ? new Quat2() : this): Quat2 => {
170
+ multiply = (b: Quat2, out = glmaths.ALWAYS_COPY ? quat2() : this): Quat2 => {
171
171
  const ax0 = this[0], ay0 = this[1], az0 = this[2], aw0 = this[3]
172
172
  const bx1 = b[4], by1 = b[5], bz1 = b[6], bw1 = b[7]
173
173
  const ax1 = this[4], ay1 = this[5], az1 = this[6], aw1 = this[7]
@@ -193,10 +193,10 @@ export class Quat2 extends Float32Array {
193
193
  * Translate by a Vec3
194
194
  *
195
195
  * @param {Vec3} v the translation vector
196
- * @param {Quat2} out the receiving dual quaternion, defaults to this
196
+ * @param {Quat2} out the receiving dual quaternion, defaults to new quat2()
197
197
  * @returns {Quat2} out
198
198
  */
199
- translate(v: Vec3, out = glmaths.ALWAYS_COPY ? new Quat2() : this): Quat2 {
199
+ translate(v: Vec3, out = glmaths.ALWAYS_COPY ? quat2() : this): Quat2 {
200
200
  const ax1 = this[0], ay1 = this[1], az1 = this[2], aw1 = this[3]
201
201
  const bx1 = v[0] * 0.5, by1 = v[1] * 0.5, bz1 = v[2] * 0.5
202
202
  const ax2 = this[4], ay2 = this[5], az2 = this[6], aw2 = this[7]
@@ -211,10 +211,10 @@ export class Quat2 extends Float32Array {
211
211
  /**
212
212
  * Calculates the conjugate of a dual quaternion
213
213
  *
214
- * @param {Quat2} out the receiving dual quaternion, defaults to this
214
+ * @param {Quat2} out the receiving dual quaternion, defaults to new quat2()
215
215
  * @returns {Quat2} out
216
216
  */
217
- conjugate(out = glmaths.ALWAYS_COPY ? new Quat2() : this): Quat2 {
217
+ conjugate(out = glmaths.ALWAYS_COPY ? quat2() : this): Quat2 {
218
218
  out[0] = -this[0]; out[1] = -this[1]; out[2] = -this[2]; out[3] = this[3]
219
219
  out[4] = -this[4]; out[5] = -this[5]; out[6] = -this[6]; out[7] = this[7]
220
220
  return out
@@ -223,10 +223,10 @@ export class Quat2 extends Float32Array {
223
223
  /**
224
224
  * Calculates the inverse of a dual quaternion
225
225
  *
226
- * @param {Quat2} out the receiving dual quaternion, defaults to this
226
+ * @param {Quat2} out the receiving dual quaternion, defaults to new quat2()
227
227
  * @returns {Quat2} out
228
228
  */
229
- invert(out = glmaths.ALWAYS_COPY ? new Quat2() : this): Quat2 {
229
+ invert(out = glmaths.ALWAYS_COPY ? quat2() : this): Quat2 {
230
230
  const sqlen = this.squaredLength()
231
231
  out[0] = -this[0] / sqlen; out[1] = -this[1] / sqlen
232
232
  out[2] = -this[2] / sqlen; out[3] = this[3] / sqlen
@@ -257,16 +257,17 @@ export class Quat2 extends Float32Array {
257
257
  /**
258
258
  * Normalize the dual quaternion
259
259
  *
260
- * @param {Quat2} out the receiving dual quaternion, defaults to this
260
+ * @param {Quat2} q the quaternion to normalize
261
+ * @param {Quat2} out the receiving dual quaternion, defaults to new quat2()
261
262
  * @returns {Quat2} out
262
263
  */
263
- normalize(out = glmaths.ALWAYS_COPY ? new Quat2() : this): Quat2 {
264
- let magnitude = this.squaredLength()
264
+ static normalize(q: Quat2, out = quat2()) {
265
+ let magnitude = q.squaredLength()
265
266
  if (magnitude > 0) {
266
267
  magnitude = Math.sqrt(magnitude)
267
- const a0 = this[0] / magnitude, a1 = this[1] / magnitude
268
- const a2 = this[2] / magnitude, a3 = this[3] / magnitude
269
- const b0 = this[4], b1 = this[5], b2 = this[6], b3 = this[7]
268
+ const a0 = q[0] / magnitude, a1 = q[1] / magnitude
269
+ const a2 = q[2] / magnitude, a3 = q[3] / magnitude
270
+ const b0 = q[4], b1 = q[5], b2 = q[6], b3 = q[7]
270
271
  const a_dot_b = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3
271
272
  out[0] = a0; out[1] = a1; out[2] = a2; out[3] = a3
272
273
  out[4] = (b0 - a0 * a_dot_b) / magnitude
@@ -277,6 +278,16 @@ export class Quat2 extends Float32Array {
277
278
  return out
278
279
  }
279
280
 
281
+ /**
282
+ * Normalize the dual quaternion
283
+ *
284
+ * @param {Quat2} out the receiving dual quaternion, defaults to new quat2()
285
+ * @returns {Quat2} out
286
+ */
287
+ normalize(out = glmaths.ALWAYS_COPY ? quat2() : this): Quat2 {
288
+ return Quat2.normalize(this, out)
289
+ }
290
+
280
291
  /**
281
292
  * Dot product of the real parts of two dual quaternions
282
293
  *
@@ -297,7 +308,7 @@ export class Quat2 extends Float32Array {
297
308
  * @param {Quat2} out the receiving dual quaternion, defaults to quat2()
298
309
  * @returns {Quat2} out
299
310
  */
300
- static lerp(a: Quat2, b: Quat2, t: number, out = new Quat2()): Quat2 {
311
+ static lerp(a: Quat2, b: Quat2, t: number, out = quat2()): Quat2 {
301
312
  const mt = 1 - t
302
313
  if (Quat2.dot(a, b) < 0) t = -t
303
314
  out[0] = a[0] * mt + b[0] * t
@@ -315,10 +326,10 @@ export class Quat2 extends Float32Array {
315
326
  * Adds two dual quaternions
316
327
  *
317
328
  * @param {Quat2} b the second operand
318
- * @param {Quat2} out the receiving dual quaternion, defaults to this
329
+ * @param {Quat2} out the receiving dual quaternion, defaults to new quat2()
319
330
  * @returns {Quat2} out
320
331
  */
321
- plus(b: Quat2, out = glmaths.ALWAYS_COPY ? new Quat2() : this): Quat2 {
332
+ plus(b: Quat2, out = glmaths.ALWAYS_COPY ? quat2() : this): Quat2 {
322
333
  out[0] = this[0] + b[0]; out[1] = this[1] + b[1]
323
334
  out[2] = this[2] + b[2]; out[3] = this[3] + b[3]
324
335
  out[4] = this[4] + b[4]; out[5] = this[5] + b[5]
@@ -330,10 +341,10 @@ export class Quat2 extends Float32Array {
330
341
  * Scales a dual quaternion by a scalar
331
342
  *
332
343
  * @param {Number} s the scalar to scale by
333
- * @param {Quat2} out the receiving dual quaternion, defaults to this
344
+ * @param {Quat2} out the receiving dual quaternion, defaults to new quat2()
334
345
  * @returns {Quat2} out
335
346
  */
336
- scale(s: number, out = glmaths.ALWAYS_COPY ? new Quat2() : this): Quat2 {
347
+ scale(s: number, out = glmaths.ALWAYS_COPY ? quat2() : this): Quat2 {
337
348
  out[0] = this[0] * s; out[1] = this[1] * s
338
349
  out[2] = this[2] * s; out[3] = this[3] * s
339
350
  out[4] = this[4] * s; out[5] = this[5] * s
@@ -399,14 +410,17 @@ export interface Quat2 {
399
410
  sqrLen: () => number
400
411
  str: () => string
401
412
  add: (b: Quat2, out?: Quat2) => Quat2
413
+ normalized: (out?: Quat2) => Quat2
402
414
  }
403
415
 
404
416
  // @aliases
405
417
  Quat2.prototype.sqrLen = Quat2.prototype.squaredLength
418
+ Quat2.prototype.normalized = Quat2.prototype.normalize
406
419
  Quat2.prototype.str = Quat2.prototype.toString
407
420
  Quat2.prototype.add = Quat2.prototype.plus
408
421
 
409
- export const quat2 = Object.assign(
410
- (x1 = 0, y1 = 0, z1 = 0, w1 = 1, x2 = 0, y2 = 0, z2 = 0, w2 = 0) => new Quat2(x1, y1, z1, w1, x2, y2, z2, w2),
411
- Quat2
412
- )
422
+ const createQuat2 =
423
+ (x1 = 0, y1 = 0, z1 = 0, w1 = 1, x2 = 0, y2 = 0, z2 = 0, w2 = 0) =>
424
+ new Quat2(x1, y1, z1, w1, x2, y2, z2, w2)
425
+ Object.setPrototypeOf(createQuat2, Quat2)
426
+ export const quat2 = createQuat2 as typeof createQuat2 & typeof Quat2