-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geom): add/update edges(), pointInside() & classifyPoint() impls
- Loading branch information
1 parent
f5a53ca
commit e834597
Showing
11 changed files
with
122 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ReadonlyVec, signedArea2 } from "@thi.ng/vectors3"; | ||
|
||
export const clockwise2 = | ||
(a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec) => | ||
signedArea2(a, b, c) < 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,6 @@ | ||
import { EPS, sign } from "@thi.ng/math"; | ||
import { ReadonlyVec, signedArea2 } from "@thi.ng/vectors3"; | ||
|
||
export const classify = | ||
export const corner = | ||
(a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec, eps = EPS) => | ||
sign(signedArea2(a, b, c), eps); | ||
|
||
export const clockwise2 = | ||
(a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec) => | ||
signedArea2(a, b, c) < 0; | ||
|
||
export const classifyPointInTriangle2 = | ||
(p: ReadonlyVec, a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec) => { | ||
const s = clockwise2(a, b, c) ? 1 : -1; | ||
return sign( | ||
Math.min( | ||
s * signedArea2(a, c, p), | ||
s * signedArea2(b, a, p), | ||
s * signedArea2(c, b, p) | ||
) | ||
); | ||
}; | ||
|
||
export const pointInTriangle2 = | ||
(p: ReadonlyVec, a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec) => { | ||
const s = clockwise2(a, b, c) ? 1 : -1; | ||
return s * signedArea2(a, c, p) >= 0 && | ||
s * signedArea2(b, a, p) >= 0 && | ||
s * signedArea2(c, b, p) >= 0; | ||
}; | ||
sign(signedArea2(a, b, c), eps); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { partition, wrap } from "@thi.ng/transducers"; | ||
import { ReadonlyVec } from "@thi.ng/vectors3"; | ||
import { VecPair } from "../api"; | ||
|
||
export const edgeIterator = | ||
(vertices: Iterable<ReadonlyVec>, closed = false) => | ||
<IterableIterator<VecPair>>partition( | ||
2, 1, | ||
closed ? | ||
wrap(vertices, 1, false, true) : | ||
vertices | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { EPS } from "@thi.ng/math"; | ||
import { eqDelta, ReadonlyVec } from "@thi.ng/vectors3"; | ||
|
||
export const pointInArray = | ||
(points: ReadonlyVec[], p: ReadonlyVec, eps = EPS) => { | ||
for (let i = points.length; --i >= 0;) { | ||
if (eqDelta(p, points[i], eps)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { EPS, sign } from "@thi.ng/math"; | ||
import { ReadonlyVec, signedArea2 } from "@thi.ng/vectors3"; | ||
import { clockwise2 } from "./clockwise"; | ||
|
||
export const classifyPointInTriangle2 = | ||
(p: ReadonlyVec, a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec, eps = EPS) => { | ||
const s = clockwise2(a, b, c) ? 1 : -1; | ||
return sign( | ||
Math.min( | ||
s * signedArea2(a, c, p), | ||
s * signedArea2(b, a, p), | ||
s * signedArea2(c, b, p) | ||
), | ||
eps | ||
); | ||
}; | ||
|
||
export const pointInTriangle2 = | ||
(p: ReadonlyVec, a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec) => { | ||
const s = clockwise2(a, b, c) ? 1 : -1; | ||
return s * signedArea2(a, c, p) >= 0 && | ||
s * signedArea2(b, a, p) >= 0 && | ||
s * signedArea2(c, b, p) >= 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
import { defmulti } from "@thi.ng/defmulti"; | ||
import { defmulti, MultiFn2O } from "@thi.ng/defmulti"; | ||
import { EPS, sign } from "@thi.ng/math"; | ||
import { dist, ReadonlyVec } from "@thi.ng/vectors3"; | ||
import { Circle, IShape, Type } from "../api"; | ||
import { Circle, IShape, Type, Triangle } from "../api"; | ||
import { dispatch } from "../internal/dispatch"; | ||
import { classifyPointInTriangle2 } from "../internal/triangle-point-inside"; | ||
|
||
export const classifyPoint = defmulti<IShape, ReadonlyVec, number>(dispatch); | ||
export const classifyPoint: MultiFn2O<IShape, ReadonlyVec, number, number> = defmulti(dispatch); | ||
|
||
classifyPoint.addAll({ | ||
|
||
[Type.CIRCLE]: | ||
($: Circle, p: ReadonlyVec, eps = EPS) => | ||
sign($.r - dist($.pos, p), eps), | ||
|
||
[Type.TRIANGLE]: | ||
({ points }: Triangle, p: ReadonlyVec, eps = EPS) => | ||
classifyPointInTriangle2(p, points[0], points[1], points[2], eps), | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { defmulti, MultiFn1O } from "@thi.ng/defmulti"; | ||
import { | ||
IShape, | ||
Polygon, | ||
Polyline, | ||
SamplingOpts, | ||
Type, | ||
VecPair, | ||
Rect | ||
} from "../api"; | ||
import { dispatch } from "../internal/dispatch"; | ||
import { edgeIterator } from "../internal/edges"; | ||
import { vertices } from "./vertices"; | ||
|
||
export const edges: MultiFn1O<IShape, number | Partial<SamplingOpts>, Iterable<VecPair>> = defmulti(dispatch); | ||
|
||
edges.addAll({ | ||
|
||
[Type.POLYGON]: | ||
($: Polygon) => | ||
edgeIterator($.points, true), | ||
|
||
[Type.POLYLINE]: | ||
($: Polyline) => | ||
edgeIterator($.points), | ||
|
||
[Type.RECT]: | ||
($: Rect) => | ||
edgeIterator(vertices($), true), | ||
|
||
}); | ||
|
||
edges.isa(Type.LINE, Type.POLYLINE); | ||
edges.isa(Type.QUAD, Type.POLYGON); | ||
edges.isa(Type.TRIANGLE, Type.POLYGON); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters