Skip to content

Commit

Permalink
feat(geom): add AABB impls for vertices() & volume()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 10, 2019
1 parent c165268 commit a9ba010
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/geom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export * from "./ops/translate";
export * from "./ops/union";
export * from "./ops/unmap-point";
export * from "./ops/vertices";
export * from "./ops/volume";
export * from "./ops/warp-points";
export * from "./ops/with-attribs";

Expand Down
38 changes: 33 additions & 5 deletions packages/geom/src/ops/vertices.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { isNumber } from "@thi.ng/checks";
import { defmulti, MultiFn1O } from "@thi.ng/defmulti";
import { DEFAULT_SAMPLES, IShape, SamplingOpts, Type } from "@thi.ng/geom-api";
import {
DEFAULT_SAMPLES,
IShape,
SamplingOpts,
Type
} from "@thi.ng/geom-api";
import { sample as _arcVertices } from "@thi.ng/geom-arc";
import { resample } from "@thi.ng/geom-resample";
import { sampleCubic, sampleQuadratic } from "@thi.ng/geom-splines";
import { cossin, TAU } from "@thi.ng/math";
import { add2, cartesian2, madd2, set2, Vec } from "@thi.ng/vectors";
import { dispatch } from "../internal/dispatch";
import {
add2,
add3,
cartesian2,
madd2,
set2,
Vec
} from "@thi.ng/vectors";
import {
AABB,
Arc,
Circle,
Cubic,
Expand All @@ -20,6 +32,7 @@ import {
Quadratic,
Rect
} from "../api";
import { dispatch } from "../internal/dispatch";

export const vertices: MultiFn1O<
IShape,
Expand All @@ -28,6 +41,21 @@ export const vertices: MultiFn1O<
> = defmulti(dispatch);

vertices.addAll({
[Type.AABB]: ({ pos, size }: AABB) => {
const [px, py, pz] = pos;
const [qx, qy, qz] = add3([], pos, size);
return [
[px, py, pz],
[px, py, qz],
[qx, py, qz],
[qx, py, pz],
[px, qy, pz],
[px, qy, qz],
[qx, qy, qz],
[qx, qy, pz]
];
},

[Type.ARC]: ($: Arc, opts?: number | Partial<SamplingOpts>): Vec[] =>
_arcVertices($.pos, $.r, $.axis, $.start, $.end, opts),

Expand Down Expand Up @@ -108,7 +136,7 @@ const circleOpts = (
opts.theta
? Math.floor(TAU / opts.theta)
: opts.dist
? Math.floor(TAU / (opts.dist / r))
: opts.num || DEFAULT_SAMPLES,
? Math.floor(TAU / (opts.dist / r))
: opts.num || DEFAULT_SAMPLES,
opts.last === true
];
22 changes: 22 additions & 0 deletions packages/geom/src/ops/volume.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DEFAULT, defmulti, MultiFn1 } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { PI } from "@thi.ng/math";
import { AABB, Sphere } from "../api";
import { dispatch } from "../internal/dispatch";

/**
* Returns the volume of given 3D shape. Returns 0 for all others.
*
* Currently only implemented for:
*
* - AABB
* - Sphere
*/
export const volume: MultiFn1<IShape, number> = defmulti(dispatch);
volume.add(DEFAULT, () => 0);

volume.addAll({
[Type.AABB]: ({ size }: AABB) => size[0] * size[1] * size[2],

[Type.SPHERE]: ($: Sphere) => (4 / 3) * PI * Math.pow($.r, 3)
});

0 comments on commit a9ba010

Please sign in to comment.