Skip to content

Commit

Permalink
feat(geom-isec): add ray-plane, plane-plane fns, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 22, 2019
1 parent 74dbcb0 commit 40a8bff
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
27 changes: 14 additions & 13 deletions packages/geom-isec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ This project is part of the

### 2D tests

| Type | Circle | Line | Poly | Rect | Tri |
|--------|:------:|:----:|:----:|:----:|:---:|
| Circle || | | | |
| Line | || | | |
| Point ||||||
| Ray ||||| |
| Rect || | || |
| Type | Circle | Line | Poly | Ray | Rect | Tri |
|--------|:------:|:----:|:----:|:---:|:----:|:---:|
| Circle || | | | | |
| Line | || | | | |
| Point |||| | ||
| Ray |||| | | |
| Rect || | | | | |

### 3D tests

| Type | AABB | Sphere |
|--------|:----:|:------:|
| AABB |||
| Point |||
| Ray |||
| Sphere | ||
| Type | AABB | Plane | Point | Ray | Sphere |
|--------|:----:|:-----:|:-----:|:---:|:------:|
| AABB || | | ||
| Plane | || | | |
| Point || | | ||
| Ray ||| | ||
| Sphere | | | | ||

## Installation

Expand Down
2 changes: 2 additions & 0 deletions packages/geom-isec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ export * from "./point";

export * from "./circle-circle";
export * from "./line-line";
export * from "./plane-plane";
export * from "./ray-circle";
export * from "./ray-line";
export * from "./ray-plane";
export * from "./ray-poly";
export * from "./ray-rect";
export * from "./rect-circle";
Expand Down
32 changes: 32 additions & 0 deletions packages/geom-isec/src/plane-plane.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IntersectionType } from "@thi.ng/geom-api";
import { eqDelta, eqDeltaFixed } from "@thi.ng/math";
import {
add3,
cross3,
dot3,
mulN3,
ReadonlyVec
} from "@thi.ng/vectors";
import { NONE } from "./api";

export const intersectPlanePlane = (
na: ReadonlyVec,
wa: number,
nb: ReadonlyVec,
wb: number
) => {
const dn = dot3(na, nb);
if (eqDeltaFixed(dn, 1)) {
return eqDelta(wa, wb) ? { type: IntersectionType.COINCIDENT } : NONE;
}
const det = 1 / (1 - dn * dn);
const da = (wa - wb * dn) * det;
const db = (wb - wa * dn) * det;
return {
type: IntersectionType.INTERSECT,
isec: [
add3(null, mulN3([], na, da), mulN3([], nb, db)),
cross3([], na, nb)
]
};
};
2 changes: 1 addition & 1 deletion packages/geom-isec/src/ray-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const intersectRayLine = (
const bay = b[1] - a[1];
const d = dir[0] * bay - dir[1] * bax;
if (eqDeltaFixed(d, 0)) {
return { type: IntersectionType.NONE };
return NONE;
}
const arx = a[0] - rpos[0];
const ary = a[1] - rpos[1];
Expand Down
37 changes: 37 additions & 0 deletions packages/geom-isec/src/ray-plane.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IntersectionType } from "@thi.ng/geom-api";
import { EPS, sign } from "@thi.ng/math";
import {
copy,
dot,
maddN,
mulN,
ReadonlyVec,
sub
} from "@thi.ng/vectors";
import { NONE } from "./api";

export const intersectRayPlane = (
rpos: ReadonlyVec,
dir: ReadonlyVec,
normal: ReadonlyVec,
w: number,
eps = EPS
) => {
const d = dot(normal, dir);
const cp = sign(dot(normal, rpos) - w, eps);
if ((d > eps && cp < 0) || (d < -eps && cp > 0)) {
const isec = sub(null, mulN([], normal, w), rpos);
const alpha = dot(normal, isec) / d;
return {
type: IntersectionType.INTERSECT,
isec: maddN(isec, rpos, dir, alpha),
alpha
};
}
return cp === 0
? {
type: IntersectionType.COINCIDENT,
isec: copy(rpos)
}
: NONE;
};

0 comments on commit 40a8bff

Please sign in to comment.