Skip to content

Commit a13738a

Browse files
committed
Allow dividing vectors by numbers and add tests
1 parent e17e8b1 commit a13738a

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

packages/typegpu/src/std/numeric.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,21 @@ export const sqrt = createDualImpl(
471471

472472
export const div = createDualImpl(
473473
// CPU implementation
474-
<T extends AnyNumericVecInstance | number>(lhs: T, rhs: T): T => {
475-
if (typeof lhs === 'number') {
476-
return (lhs / (rhs as number)) as T;
474+
<T extends AnyNumericVecInstance | number>(lhs: T, rhs: T | number): T => {
475+
if (typeof lhs === 'number' && typeof rhs === 'number') {
476+
return (lhs / rhs) as T;
477477
}
478-
return VectorOps.div[lhs.kind](lhs, rhs as AnyNumericVecInstance) as T;
478+
if (typeof rhs === 'number') {
479+
return VectorOps.mulSxV[(lhs as AnyNumericVecInstance).kind](
480+
1 / rhs,
481+
lhs as AnyNumericVecInstance,
482+
) as T;
483+
}
484+
// Vector / Vector case
485+
return VectorOps.div[(lhs as AnyNumericVecInstance).kind](
486+
lhs as AnyNumericVecInstance,
487+
rhs as AnyNumericVecInstance,
488+
) as T;
479489
},
480490
// GPU implementation
481491
(lhs, rhs) => ({
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { describe, expect, it } from 'vitest';
2+
import {
3+
vec2f,
4+
vec2i,
5+
vec2u,
6+
vec3f,
7+
vec3i,
8+
vec3u,
9+
vec4f,
10+
vec4i,
11+
vec4u,
12+
} from '../../../src/data/index.ts';
13+
import { div } from '../../../src/std/index.ts';
14+
15+
describe('div', () => {
16+
it('computes quotient of a vec2f and a number', () => {
17+
expect(div(vec2f(0, 0), 17)).toEqual(vec2f(0, 0));
18+
expect(div(vec2f(0.24, 0), 0.4).x).toBeCloseTo(0.6);
19+
expect(div(vec2f(0, 0), 5)).toEqual(vec2f());
20+
});
21+
22+
it('computes quotient of a vec2u and a number', () => {
23+
expect(div(vec2u(0, 0), 2)).toEqual(vec2u(0, 0));
24+
expect(div(vec2u(3, 3), 3)).toEqual(vec2u(1));
25+
});
26+
27+
it('computes quotient of a vec2i and a number', () => {
28+
expect(div(vec2i(0, 0), 9)).toEqual(vec2i(0, 0));
29+
expect(div(vec2i(-3, -3), -3)).toEqual(vec2i(1));
30+
expect(div(vec2i(0, 0), 5)).toEqual(vec2i());
31+
});
32+
33+
it('computes quotient of a vec3f and a number', () => {
34+
expect(div(vec3f(-3, -4, -6), 2)).toEqual(vec3f(-1.5, -2, -3));
35+
expect(div(vec3f(-2, -2, -2), -2)).toEqual(vec3f(1));
36+
expect(div(vec3f(0, 0, 0), 5)).toEqual(vec3f());
37+
});
38+
39+
it('computes quotient of a vec3u and a number', () => {
40+
expect(div(vec3u(2, 2, 2), 2)).toEqual(vec3u(1));
41+
expect(div(vec3u(0, 0, 0), 5)).toEqual(vec3u());
42+
});
43+
44+
it('computes quotient of a vec3i and a number', () => {
45+
expect(div(vec3i(-1, -2, -3), 1)).toEqual(vec3i(-1, -2, -3));
46+
expect(div(vec3i(-4, -6, -8), -2)).toEqual(vec3i(2, 3, 4));
47+
expect(div(vec3i(2, 2, 2), 2)).toEqual(vec3i(1));
48+
});
49+
50+
it('computes quotient of a vec4f and a number', () => {
51+
expect(div(vec4f(1.5, 2, 3, 4), 2)).toEqual(vec4f(0.75, 1, 1.5, 2));
52+
expect(div(vec4f(4, 7, 8, 10), 2)).toEqual(vec4f(2, 3.5, 4, 5));
53+
expect(div(vec4f(0.09, 0.09, 0.09, 0.09), 0.3)).toEqual(vec4f(0.3));
54+
});
55+
56+
it('computes quotient of a vec4u and a number', () => {
57+
expect(div(vec4u(2, 2, 2, 2), 2)).toEqual(vec4u(1));
58+
expect(div(vec4u(16, 16, 16, 16), 8)).toEqual(vec4u(2));
59+
});
60+
61+
it('computes quotient of a vec4i and a number', () => {
62+
expect(div(vec4i(1, 2, 3, 4), -1)).toEqual(vec4i(-1, -2, -3, -4));
63+
expect(div(vec4i(0, 0, 0, 0), 1)).toEqual(vec4i());
64+
expect(div(vec4i(16, 16, 16, 16), 8)).toEqual(vec4i(2));
65+
});
66+
67+
it('computes quotient of a vec4f and a vec4f', () => {
68+
expect(div(vec4f(1.5, 2, 3, 4), vec4f(2, 2, 2, 2))).toEqual(
69+
vec4f(0.75, 1, 1.5, 2),
70+
);
71+
expect(div(vec4f(0.09, 0.09, 0.09, 0.09), vec4f(0.3))).toEqual(vec4f(0.3));
72+
});
73+
74+
it('computes quotient of a vec4u and a vec4u', () => {
75+
expect(div(vec4u(2, 2, 2, 2), vec4u(2))).toEqual(vec4u(1));
76+
expect(div(vec4u(16, 16, 16, 16), vec4u(8))).toEqual(vec4u(2));
77+
});
78+
79+
it('computes quotient of a vec4i and a vec4i', () => {
80+
expect(div(vec4i(1, 2, 3, 4), vec4i(-1))).toEqual(vec4i(-1, -2, -3, -4));
81+
expect(div(vec4i(0, 0, 0, 0), vec4i(1))).toEqual(vec4i());
82+
expect(div(vec4i(16, 16, 16, 16), vec4i(8))).toEqual(vec4i(2));
83+
});
84+
});

0 commit comments

Comments
 (0)