Skip to content

Commit

Permalink
fix(geom-isec): fix #269 update rayBox()
Browse files Browse the repository at this point in the history
- fix Y/Z-axis handling
- add tests
  • Loading branch information
postspectacular committed Dec 27, 2020
1 parent 1c0e284 commit 441cddb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/geom-isec/src/ray-rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ const rayBox: FnU4<ReadonlyVec, [number, number]> = (rpos, dir, bmin, bmax) => {
d = 1 / dir[1];
t1 = (bmin[1] - p) * d;
t2 = (bmax[1] - p) * d;
tmin = max(tmin, min(t1, t2));
tmax = min(tmax, max(t1, t2));
p = rpos[2];
d = 1 / dir[2];
t1 = (bmin[2] - p) * d;
t2 = (bmax[2] - p) * d;
tmin = max(tmin, min(t1, t2));
tmax = min(tmax, max(t1, t2));
return [max(tmin, min(t1, t2)), min(tmax, max(t1, t2))];
};

Expand All @@ -73,9 +73,9 @@ const intersectWith = (
? inside
? {
type: IntersectionType.INTERSECT,
inside,
isec: [maddN([], dir, tmax, rpos)],
alpha: tmax,
inside,
}
: {
type: IntersectionType.INTERSECT,
Expand Down
71 changes: 71 additions & 0 deletions packages/geom-isec/test/ray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { eqDelta, maddN3, mulN3, normalize, Vec } from "@thi.ng/vectors";
import * as assert from "assert";
import { intersectRayAABB } from "../src";

describe("ray intersection", () => {
it("rayBox inside", () => {
const dirs: Vec[] = [
[-1, -1, -1],
[-1, -1, 0],
[-1, -1, 1],
[-1, 0, -1],
[-1, 0, 0],
[-1, 0, 1],
[-1, 1, -1],
[-1, 1, 0],
[-1, 1, 1],
[0, -1, -1],
[0, -1, 0],
[0, -1, 1],
[0, 0, -1],
[0, 0, 1],
[0, 1, -1],
[0, 1, 0],
[0, 1, 1],
[1, -1, -1],
[1, -1, 0],
[1, -1, 1],
[1, 0, -1],
[1, 0, 0],
[1, 0, 1],
[1, 1, -1],
[1, 1, 0],
[1, 1, 1],
];
for (let d of dirs) {
const n = normalize([], d);
const i = intersectRayAABB([5, 5, 5], n, [0, 0, 0], [10, 10, 10]);
const expected = maddN3([], n, i.alpha!, [5, 5, 5]);
assert(i.inside, `inside d=${d}`);
assert(
eqDelta(expected, <Vec>i.isec![0]),
`d=${d} isec=${i.isec}, exp=${expected}`
);
}
});

it("rayBox outside", () => {
const dirs: Vec[] = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
];
for (let d of dirs) {
let o = mulN3([], d, -10);
let i = intersectRayAABB(o, d, [-5, -5, -5], [5, 5, 5]);
let expected = maddN3([], d, i.alpha!, o);
assert(
eqDelta(expected, <Vec>i.isec![0]),
`d=${d} isec=${i.isec}, exp=${expected}`
);
d = mulN3(d, d, -1);
o = mulN3([], d, -10);
i = intersectRayAABB(o, d, [-5, -5, -5], [5, 5, 5]);
expected = maddN3([], d, i.alpha!, o);
assert(
eqDelta(expected, <Vec>i.isec![0]),
`d=${d} isec=${i.isec}, exp=${expected}`
);
}
});
});

0 comments on commit 441cddb

Please sign in to comment.