Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix atan2 implementation to behave like Javascript Math.atan2 #683

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/backend/web-gl/fragment-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ float asinh(float x) {
}

float atan2(float v1, float v2) {
if (v1 == 0.0 || v2 == 0.0) return 0.0;
return atan(v1 / v2);
if (v2 == 0.0) {
if (v1 == 0.0) return 0.0;
if (v1 > 0.0) return 1.5707963267948966;
if (v1 < 0.0) return -1.5707963267948966;
}
return atan(v1, v2);
}

float atanh(float x) {
Expand Down
8 changes: 6 additions & 2 deletions src/backend/web-gl2/fragment-shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ __CONSTANTS__;
in vec2 vTexCoord;

float atan2(float v1, float v2) {
if (v1 == 0.0 || v2 == 0.0) return 0.0;
return atan(v1 / v2);
if (v2 == 0.0) {
if (v1 == 0.0) return 0.0;
if (v1 > 0.0) return 1.5707963267948966;
if (v1 < 0.0) return -1.5707963267948966;
}
return atan(v1, v2);
}

float cbrt(float x) {
Expand Down
1 change: 1 addition & 0 deletions test/all.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<script type="module" src="issues/585-inaccurate-lookups.js"></script>
<script type="module" src="issues/586-unable-to-resize.js"></script>
<script type="module" src="issues/608-rewritten-arrays.js"></script>
<script type="module" src="issues/647-atan2-range.js"></script>
<script type="module" src="issues/91-create-kernel-map-array.js"></script>
<script type="module" src="issues/96-param-names.js"></script>
<script type="module" src="features/to-string/as-file.js"></script>
Expand Down
50 changes: 50 additions & 0 deletions test/issues/647-atan2-range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { assert, skip, test, module: describe } = require('qunit');
const { GPU } = require('../../src');

describe('issue #647');

function buildAtan2KernelResult(mode) {
const gpu = new GPU({ mode });
const kernel = gpu.createKernel(function (x, y) {
return Math.atan2(y[this.thread.x], x[this.thread.x]);
}, {
output: [5],
});

// test atan2 at center, E, N, W, S on unit circle
// [0,0] [1,0], [0, 1], [-1, 0], [0, -1]
const x = [0, 1, 0, -1, 0];
const y = [0, 0, 1, 0, -1];
const result = kernel(x, y);

assert.equal(result[0].toFixed(7), 0.0000000);
assert.equal(result[1].toFixed(7), 0.0000000);
assert.equal(result[2].toFixed(7), 1.5707964);
assert.equal(result[3].toFixed(7), 3.1415927);
assert.equal(result[4].toFixed(7), -1.5707964);
gpu.destroy();
}

test('Issue #647 atan2 - auto', () => {
buildAtan2KernelResult();
});

test('Issue #647 atan2 - gpu', () => {
buildAtan2KernelResult('gpu');
});

(GPU.isWebGLSupported ? test : skip)('Issue #647 atan2 - webgl', () => {
buildAtan2KernelResult('webgl');
});

(GPU.isWebGL2Supported ? test : skip)('Issue #647 atan2 - webgl2', () => {
buildAtan2KernelResult('webgl2');
});

(GPU.isHeadlessGLSupported ? test : skip)('Issue #647 atan2 - headlessgl', () => {
buildAtan2KernelResult('headlessgl');
});

test('Issue #647 atan2 - cpu', () => {
buildAtan2KernelResult('cpu');
});