Skip to content

Commit

Permalink
refactor(geom-api): split into separate files, add ISpatialAccel
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 24, 2019
1 parent 03352e1 commit 5c98ed7
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 153 deletions.
11 changes: 11 additions & 0 deletions packages/geom-api/src/accel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Pair } from "@thi.ng/api";

export interface ISpatialAccel<K, V> {
add(p: K, v: V, eps?: number): boolean;
addAll(pairs: Iterable<Pair<K, V>>, eps?: number): boolean;
addKeys(keys: Iterable<K>, eps?: number): boolean;
remove(p: K): boolean;

select(q: Readonly<K>, maxNum: number, maxDist?: number): Pair<K, V>[];
selectKeys(q: Readonly<K>, maxNum: number, maxDist?: number): K[];
}
160 changes: 7 additions & 153 deletions packages/geom-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,153 +1,7 @@
import { ICopy, IObjectOf, IToHiccup } from "@thi.ng/api";
import { ReadonlyVec, Vec } from "@thi.ng/vectors";

export const DEFAULT_SAMPLES = 20;

export const enum SegmentType {
MOVE,
LINE,
POLYLINE,
ARC,
CUBIC,
QUADRATIC,
CLOSE,
}

export const enum Type {
AABB = 1,
ARC,
CIRCLE,
CUBIC,
CUBIC3,
ELLIPSE,
GROUP,
LINE,
LINE3,
PATH,
POINTS,
POINTS3,
POLYGON,
POLYGON3,
POLYLINE,
POLYLINE3,
QUAD,
QUAD3,
QUADRATIC,
QUADRATIC3,
RECT,
SPHERE,
TRIANGLE,
TRIANGLE3,
RAY,
RAY3,
}

export type Attribs = IObjectOf<any>;

export type Tessellator = (points: ReadonlyVec[]) => Vec[][];

export interface IShape extends
ICopy<IShape> {

readonly type: number | string;
attribs?: Attribs;
}

export interface AABBLike extends IShape {
pos: Vec;
size: Vec;

max(): Vec;
}

export interface SphereLike extends IShape {
pos: Vec;
r: number;
}

export interface IHiccupShape extends IShape, IToHiccup {
}

export interface IHiccupPathSegment {
toHiccupPathSegments(): any[];
}

export interface PathSegment {
type: SegmentType;
point?: Vec;
geo?: IShape & IHiccupPathSegment;
}

export interface PCLike extends IShape {
points: Vec[];
}

export interface PCLikeConstructor {
new(pts: Vec[], attribs: Attribs): PCLike;
}

export interface SamplingOpts {
/**
* Number of points to sample & return. Defaults to the implementing
* type's `DEFAULT_RES` if neither this nor `theta` option is given.
*/
num: number;
/**
* Defines the target angle between sampled points. If greater than
* the actual range of the arc, only the two end points will be
* returned at most. This option is used to derive a `num` value and
* takes priority if `num` is given as well.
*
* This option is useful to adapt the sampling based on angular
* resolution, rather than a fixed number of samples.
*/
theta: number;
/**
* Approximate desired distance between sampled result points. If
* given, takes priority over the `num` option, but the latter MIGHT
* be used as part of the sampling process (implementation
* specific).
*/
dist: number;
/**
* If `true`, the shape's end point will be included in the result
* array. The default setting for open geometries is `true`, for
* closed ones `false`. This option has no influence on any internal
* resolution calculation.
*
* For open geometry this option is useful to when re-sampling paths
* of consecutive segments, where the end points of each segment
* coincide with the start points of the next segment. For all but
* the last segment, this option should be `false` and so can be
* used to avoid duplicate vertices in the concatenated result.
*
* When sampling closed shapes, enabling this option will include an
* extra point (start), i.e. if the `num` option was given, results
* in `num+1` points.
*/
last: boolean;
}

export interface SubdivKernel {
fn: (pts: ReadonlyVec[], i: number, nump: number) => Vec[];
iter?: (pts: ReadonlyVec[]) => Iterable<ReadonlyVec>;
size: number;
}

export const enum IntersectionType {
NONE,
PARALLEL,
COINCIDENT,
COINCIDENT_NO_INTERSECT,
INTERSECT,
INTERSECT_OUTSIDE,
}

export interface IntersectionResult {
type: IntersectionType;
isec?: Vec | Vec[];
det?: number;
alpha?: number;
beta?: number;
inside?: boolean;
}
export * from "./accel";
export * from "./isec";
export * from "./path";
export * from "./sample";
export * from "./shape";
export * from "./subdiv";
export * from "./tessel";
19 changes: 19 additions & 0 deletions packages/geom-api/src/isec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Vec } from "@thi.ng/vectors";

export const enum IntersectionType {
NONE,
PARALLEL,
COINCIDENT,
COINCIDENT_NO_INTERSECT,
INTERSECT,
INTERSECT_OUTSIDE,
}

export interface IntersectionResult {
type: IntersectionType;
isec?: Vec | Vec[];
det?: number;
alpha?: number;
beta?: number;
inside?: boolean;
}
22 changes: 22 additions & 0 deletions packages/geom-api/src/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Vec } from "@thi.ng/vectors";
import { IShape } from "./shape";

export const enum SegmentType {
MOVE,
LINE,
POLYLINE,
ARC,
CUBIC,
QUADRATIC,
CLOSE,
}

export interface PathSegment {
type: SegmentType;
point?: Vec;
geo?: IShape & IHiccupPathSegment;
}

export interface IHiccupPathSegment {
toHiccupPathSegments(): any[];
}
46 changes: 46 additions & 0 deletions packages/geom-api/src/sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export let DEFAULT_SAMPLES = 20;

export const setDefaultSamples = (n: number) =>
(DEFAULT_SAMPLES = n);

export interface SamplingOpts {
/**
* Number of points to sample & return. Defaults to the implementing
* type's `DEFAULT_RES` if neither this nor `theta` option is given.
*/
num: number;
/**
* Defines the target angle between sampled points. If greater than
* the actual range of the arc, only the two end points will be
* returned at most. This option is used to derive a `num` value and
* takes priority if `num` is given as well.
*
* This option is useful to adapt the sampling based on angular
* resolution, rather than a fixed number of samples.
*/
theta: number;
/**
* Approximate desired distance between sampled result points. If
* given, takes priority over the `num` option, but the latter MIGHT
* be used as part of the sampling process (implementation
* specific).
*/
dist: number;
/**
* If `true`, the shape's end point will be included in the result
* array. The default setting for open geometries is `true`, for
* closed ones `false`. This option has no influence on any internal
* resolution calculation.
*
* For open geometry this option is useful to when re-sampling paths
* of consecutive segments, where the end points of each segment
* coincide with the start points of the next segment. For all but
* the last segment, this option should be `false` and so can be
* used to avoid duplicate vertices in the concatenated result.
*
* When sampling closed shapes, enabling this option will include an
* extra point (start), i.e. if the `num` option was given, results
* in `num+1` points.
*/
last: boolean;
}
63 changes: 63 additions & 0 deletions packages/geom-api/src/shape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ICopy, IObjectOf, IToHiccup } from "@thi.ng/api";
import { Vec } from "@thi.ng/vectors";

export const enum Type {
AABB = 1,
ARC,
CIRCLE,
CUBIC,
CUBIC3,
ELLIPSE,
GROUP,
LINE,
LINE3,
PATH,
POINTS,
POINTS3,
POLYGON,
POLYGON3,
POLYLINE,
POLYLINE3,
QUAD,
QUAD3,
QUADRATIC,
QUADRATIC3,
RECT,
SPHERE,
TRIANGLE,
TRIANGLE3,
RAY,
RAY3,
}

export type Attribs = IObjectOf<any>;

export interface IShape extends
ICopy<IShape> {

readonly type: number | string;
attribs?: Attribs;
}

export interface AABBLike extends IShape {
pos: Vec;
size: Vec;

max(): Vec;
}

export interface SphereLike extends IShape {
pos: Vec;
r: number;
}

export interface IHiccupShape extends IShape, IToHiccup {
}

export interface PCLike extends IShape {
points: Vec[];
}

export interface PCLikeConstructor {
new(pts: Vec[], attribs: Attribs): PCLike;
}
22 changes: 22 additions & 0 deletions packages/geom-api/src/subdiv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ReadonlyVec, Vec } from "@thi.ng/vectors";

export interface SubdivKernel {
/**
* Subdivision function. Called with an array of `size` consecutive
* points of the original curve and can produce any number of result
* points. Additionally is passed the index `i` of the processed
* point and `nump`, the total number of point in the
* curve/polyline. The latter 2 args are useful to implement custom
* behaviors for the start / end points of the curve.
*/
fn: (pts: ReadonlyVec[], i: number, nump: number) => Vec[];
/**
* Optional function to pre-process the original curve points prior to subdivision and yield a point iteratable (e.g. for closed curves / polygons to prepend the last point before the first).
*/
iter?: (pts: ReadonlyVec[]) => Iterable<ReadonlyVec>;
/**
* Kernal size. The subdivision function `fn` always receives `size`
* number consecutive points.
*/
size: number;
}
8 changes: 8 additions & 0 deletions packages/geom-api/src/tessel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReadonlyVec, Vec } from "@thi.ng/vectors";

/**
* Tessellation function. Receives a point array representing a polygon
* / cell and yields an array of point arrays, representing the
* subdivided cells.
*/
export type Tessellator = (points: ReadonlyVec[]) => Vec[][];

0 comments on commit 5c98ed7

Please sign in to comment.