-
-
Notifications
You must be signed in to change notification settings - Fork 143
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 new shape factories & impls
- add AABB, Plane, Sphere factories - add closestPoint() impls
- Loading branch information
1 parent
90e8b50
commit 1a5ead1
Showing
7 changed files
with
155 additions
and
20 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,41 @@ | ||
import { Attribs } from "@thi.ng/geom-api"; | ||
import { SQRT2_2 } from "@thi.ng/math"; | ||
import { | ||
ReadonlyVec, | ||
sub3, | ||
subN3, | ||
Vec | ||
} from "@thi.ng/vectors"; | ||
import { AABB, Sphere } from "../api"; | ||
import { argsVV } from "../internal/args"; | ||
|
||
export function aabb(pos: Vec, size: number | Vec, attribs?: Attribs): AABB; | ||
export function aabb(size: number | Vec, attribs?: Attribs): AABB; | ||
export function aabb(attribs?: Attribs): AABB; | ||
export function aabb(...args: any[]) { | ||
return new AABB(...argsVV(args)); | ||
} | ||
|
||
export const aabbFromMinMax = (min: Vec, max: Vec, attribs?: Attribs) => | ||
new AABB(min, sub3([], max, min), attribs); | ||
|
||
/** | ||
* Returns square inscribed in given circle instance. The circle can also be | ||
* given as centroid & radius. | ||
* | ||
* @param circle | ||
*/ | ||
export function inscribedAABB(sphere: Sphere): AABB; | ||
export function inscribedAABB(pos: ReadonlyVec, r: number): AABB; | ||
export function inscribedAABB(...args: any[]) { | ||
let pos: ReadonlyVec, r: number; | ||
if (args.length === 1) { | ||
const c: Sphere = args[0]; | ||
pos = c.pos; | ||
r = c.r; | ||
} else { | ||
[pos, r] = args; | ||
} | ||
r *= SQRT2_2; | ||
return aabb(subN3([], pos, r), r * 2); | ||
} |
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,20 @@ | ||
import { Attribs } from "@thi.ng/geom-api"; | ||
import { | ||
dot3, | ||
normalize, | ||
ReadonlyVec, | ||
Vec | ||
} from "@thi.ng/vectors"; | ||
import { Plane } from "../api"; | ||
|
||
export const plane = (normal: Vec, w: number, attribs?: Attribs) => | ||
new Plane(normalize(null, normal), w, attribs); | ||
|
||
export const planeWithPoint = ( | ||
normal: Vec, | ||
p: ReadonlyVec, | ||
attribs?: Attribs | ||
) => { | ||
normal = normalize(null, normal); | ||
return new Plane(normal, -dot3(normal, p), attribs); | ||
}; |
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,23 @@ | ||
import { Attribs } from "@thi.ng/geom-api"; | ||
import { | ||
dist, | ||
mixN3, | ||
ReadonlyVec, | ||
Vec | ||
} from "@thi.ng/vectors"; | ||
import { Sphere } from "../api"; | ||
import { argsVN } from "../internal/args"; | ||
|
||
export function sphere(pos: Vec, r: number, attribs?: Attribs): Sphere; | ||
export function sphere(pos: Vec, attribs?: Attribs): Sphere; | ||
export function sphere(r: number, attribs?: Attribs): Sphere; | ||
export function sphere(attribs?: Attribs): Sphere; | ||
export function sphere(...args: any[]) { | ||
return new Sphere(...argsVN(args)); | ||
} | ||
|
||
export const sphereFrom2Points = ( | ||
a: ReadonlyVec, | ||
b: ReadonlyVec, | ||
attribs?: Attribs | ||
) => new Sphere(mixN3([], a, b, 0.5), dist(a, b) / 2, attribs); |
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
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