Skip to content

Commit

Permalink
Allow passing quaternions to vec4
Browse files Browse the repository at this point in the history
  • Loading branch information
jespertheend committed Aug 12, 2024
1 parent e162c77 commit 52ee7c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/math/Vec4.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Vec2 } from "./Vec2.js";
import { Vec3 } from "./Vec3.js";
import { Mat4 } from "./Mat4.js";
import { Quat } from "./Quat.js";

/**
* @typedef {() => Vec4} vec4SetEmptySignature
* @typedef {(vec: Vec2) => Vec4} vec4SetVec2Signature
* @typedef {(vec: Vec3) => Vec4} vec4SetVec3Signature
* @typedef {(vec: Vec4) => Vec4} vec4SetVec4Signature
* @typedef {(vec: Quat) => Vec4} vec4SetQuatSignature
* @typedef {(x?: number, y?: number, z?: number, w?: number) => Vec4} vec4SetNumNumSignature
* @typedef {(xyzw: number[]) => Vec4} vec4SetArraySignature
* @typedef {import("./MathTypes.js").MergeParameters<vec4SetEmptySignature | vec4SetVec2Signature | vec4SetVec3Signature | vec4SetVec4Signature | vec4SetNumNumSignature | vec4SetArraySignature>} Vec4Parameters
* @typedef {import("./MathTypes.js").MergeParameters<vec4SetEmptySignature | vec4SetVec2Signature | vec4SetVec3Signature | vec4SetVec4Signature | vec4SetQuatSignature | vec4SetNumNumSignature | vec4SetArraySignature>} Vec4Parameters
*/

/**
Expand Down Expand Up @@ -73,7 +75,7 @@ export class Vec4 {

if (args.length == 1) {
const arg = args[0];
if (arg instanceof Vec4) {
if (arg instanceof Vec4 || arg instanceof Quat) {
this._x = arg.x;
this._y = arg.y;
this._z = arg.z;
Expand Down
23 changes: 22 additions & 1 deletion test/unit/src/math/Vec4.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals, assertNotStrictEquals } from "std/testing/asserts.ts";
import { Mat4, Vec2, Vec3, Vec4 } from "../../../../src/mod.js";
import { Mat4, Quat, Vec2, Vec3, Vec4 } from "../../../../src/mod.js";
import { assertAlmostEquals, assertVecAlmostEquals } from "../../../../src/util/asserts.js";

Deno.test({
Expand Down Expand Up @@ -41,6 +41,16 @@ Deno.test({
},
});

Deno.test({
name: "Create with Quat",
fn() {
const quat = new Quat([1, 2, 3, 4]);
const vec = new Vec4(quat);

assertEquals(vec.toArray(), [1, 2, 3, 4]);
},
});

Deno.test({
name: "Create with one number",
fn() {
Expand Down Expand Up @@ -155,6 +165,17 @@ Deno.test({
},
});

Deno.test({
name: "Set with Quat",
fn() {
const quat = new Quat([1, 2, 3, 4]);
const vec = new Vec4();
vec.set(quat);

assertEquals(vec.toArray(), [1, 2, 3, 4]);
},
});

Deno.test({
name: "Set with two numbers",
fn() {
Expand Down

0 comments on commit 52ee7c5

Please sign in to comment.