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/rename type ids, add sphere, isec fns
- Loading branch information
1 parent
2e6d196
commit 161199f
Showing
6 changed files
with
143 additions
and
19 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 |
---|---|---|
|
@@ -60,7 +60,7 @@ export const circleFrom3Points = | |
}; | ||
|
||
implementations( | ||
Type.CIRCLE2, | ||
Type.CIRCLE, | ||
|
||
null, | ||
|
||
|
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
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,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; | ||
// }; |