Skip to content

Commit

Permalink
feat(geom): add/rename type ids, add sphere, isec fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 2, 2019
1 parent 2e6d196 commit 161199f
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 19 deletions.
70 changes: 54 additions & 16 deletions packages/geom2/src/api.ts
Expand Up @@ -29,19 +29,25 @@ import { subdivKernel3 } from "./internal/subdiv-curve";
import { warpPoints } from "./internal/warp";

export const enum Type {
AABB = "aabb",
ARC2 = "arc",
CIRCLE2 = "circle",
CIRCLE = "circle",
CUBIC2 = "cubic",
ELLIPSE2 = "ellipse",
ELLIPSE = "ellipse",
GROUP = "g",
LINE2 = "line",
LINE3 = "line3",
PATH2 = "path",
POINTS2 = "points",
POINTS3 = "points3",
POLYGON2 = "polygon",
POLYGON3 = "poly3",
POLYLINE2 = "polyline",
QUAD2 = "quad",
QUAD2 = "quad2",
QUAD3 = "quad3",
QUADRATIC2 = "quadratic",
RECT2 = "rect",
RECT = "rect",
SPHERE = "sphere",
TRIANGLE2 = "triangle",
RAY = "ray"
}
Expand Down Expand Up @@ -469,7 +475,7 @@ export class Circle2 implements
}

get type() {
return Type.CIRCLE2;
return Type.CIRCLE;
}

copy() {
Expand Down Expand Up @@ -531,7 +537,7 @@ export class Ellipse2 implements
}

get type() {
return Type.ELLIPSE2;
return Type.ELLIPSE;
}

copy() {
Expand Down Expand Up @@ -596,6 +602,14 @@ export class Line2 extends PointContainer implements
super(Type.LINE2, points, attribs);
}

get a() {
return this.points[0];
}

get b() {
return this.points[1];
}

copy() {
return new Line2(this._copy(), { ...this.attribs });
}
Expand All @@ -607,17 +621,9 @@ export class Line2 extends PointContainer implements
["V", b[1]] :
a[1] === b[1] ?
["H", b[0]] :
["L", this.points[1]]
["L", b]
];
}

get a() {
return this.points[0];
}

get b() {
return this.points[1];
}
}

export class Path2 implements IShape {
Expand Down Expand Up @@ -799,7 +805,7 @@ export class Rect2 implements
}

get type() {
return Type.RECT2;
return Type.RECT;
}

copy() {
Expand All @@ -817,6 +823,38 @@ export class Rect2 implements
}
}

export class Sphere implements
IShape {

pos: Vec;
r: number;
attribs: Attribs;

constructor(pos: Vec, r: number, attribs?: Attribs) {
this.pos = pos;
this.r = r;
this.attribs = attribs;
}

get type() {
return Type.SPHERE;
}

copy() {
return new Sphere(copy(this.pos), this.r, { ...this.attribs });
}

equiv(o: any) {
return o instanceof Sphere &&
equiv(this.pos, o.pos) &&
this.r === o.r;
}

toHiccup() {
return "";
}
}

export class Triangle2 extends PointContainer {

constructor(points: Vec[], attribs?: Attribs) {
Expand Down
2 changes: 1 addition & 1 deletion packages/geom2/src/circle.ts
Expand Up @@ -60,7 +60,7 @@ export const circleFrom3Points =
};

implementations(
Type.CIRCLE2,
Type.CIRCLE,

null,

Expand Down
2 changes: 1 addition & 1 deletion packages/geom2/src/ellipse.ts
Expand Up @@ -34,7 +34,7 @@ export function ellipse(pos: Vec, r = [1, 1], attribs?: Attribs): Ellipse2 {
}

implementations(
Type.ELLIPSE2,
Type.ELLIPSE,

null,

Expand Down
1 change: 1 addition & 0 deletions packages/geom2/src/index.ts
Expand Up @@ -12,6 +12,7 @@ export * from "./polyline";
export * from "./quad";
export * from "./ray";
export * from "./rect";
export * from "./sphere";
export * from "./triangle";

export * from "./svg";
Expand Down
28 changes: 27 additions & 1 deletion packages/geom2/src/rect.ts
Expand Up @@ -13,9 +13,11 @@ import {
Attribs,
bounds,
centroid,
Circle2,
clipConvex,
ClipMode,
edges,
intersectShape,
IShape,
mapPoint,
perimeter,
Expand Down Expand Up @@ -51,7 +53,7 @@ export const rectFromMinMax = (min: Vec, max: Vec, attribs?: Attribs) =>
Rect2.fromMinMax(min, max, attribs);

implementations(
Type.RECT2,
Type.RECT,

{
[Type.POLYGON2]: [
Expand Down Expand Up @@ -144,3 +146,27 @@ implementations(
verts;
},
);

intersectShape.addAll({

[`${Type.RECT}-${Type.RECT}`]:
({ pos: { 0: ax, 1: ay }, size: { 0: aw, 1: ah } }: Rect2,
{ pos: { 0: bx, 1: by }, size: { 0: bw, 1: bh } }: Rect2) =>
!((ax > bx + bw) ||
(bx > ax + aw) ||
(ay > by + bh) ||
(by > ay + ah)),

[`${Type.RECT}-${Type.CIRCLE}`]:
({ pos: rp, size }: Rect2, { pos: cp, r }: Circle2) =>
rcAxis(cp[0], rp[0], size[0]) +
rcAxis(cp[1], rp[1], size[1]) <= r * r,

});

const rcAxis = (a: number, b: number, c: number) =>
a < b ?
Math.pow(a - b, 2) :
a > b + c ?
Math.pow(a - b - c, 2) :
0;
59 changes: 59 additions & 0 deletions packages/geom2/src/sphere.ts
@@ -0,0 +1,59 @@
import { Vec } from "@thi.ng/vectors3/api";
import { dot3 } from "@thi.ng/vectors3/dot";
import { maddN3 } from "@thi.ng/vectors3/maddn";
import { magSq3 } from "@thi.ng/vectors3/magsq";
import { sub3 } from "@thi.ng/vectors3/sub";
import {
intersectShape,
Ray,
Sphere,
Type,
} from "./api";
import { distSq3 } from "@thi.ng/vectors3/distsq";

export const sphere =
(pos: Vec, r = 1) => new Sphere(pos, r);

intersectShape.addAll({

[`${Type.SPHERE}-${Type.SPHERE}`]:
(a: Sphere, b: Sphere) =>
distSq3(a.pos, b.pos) <= Math.pow(a.r + b.r, 2),

[`${Type.SPHERE}-${Type.RAY}`]:
({ pos: spos, r: r }: Sphere, { pos: rpos, dir }: Ray) => {
const delta = sub3([], spos, rpos);
const w = dot3(delta, dir);
let d = r * r + w * w - magSq3(delta);
if (d >= 0) {
d = Math.sqrt(d);
const a = w + d;
const b = w - d;
d = a >= 0 ?
b >= 0 ?
a > b ?
b :
a :
a :
b >= 0 ?
b :
undefined;
// reuse delta as out
return d !== undefined ?
maddN3(delta, rpos, dir, d) :
d;
}
},
});

// export const isecRaySphere = (ray: Ray, sphere: Sphere) => {
// const w = sub3([], ray.pos, sphere.pos);
// const rd = ray.dir;
// const b = 2 * dot3(w, rd);
// const a = magSq3(rd);
// const c = magSq3(w) - sphere.r * sphere.r;
// let d = b * b - 4 * a * c;
// return d >= 0 && ((d = (-b - Math.sqrt(d)) / (2 * a)) >= 0) ?
// maddN3([], ray.pos, rd, d) :
// undefined;
// };

0 comments on commit 161199f

Please sign in to comment.