@thi.ng/geom-closest-point
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -6,2 +6,10 @@ # Change Log | ||
## [0.3.3](https://github.com/thi-ng/umbrella/compare/@thi.ng/geom-closest-point@0.3.2...@thi.ng/geom-closest-point@0.3.3) (2019-07-31) | ||
**Note:** Version bump only for package @thi.ng/geom-closest-point | ||
## [0.3.2](https://github.com/thi-ng/umbrella/compare/@thi.ng/geom-closest-point@0.3.1...@thi.ng/geom-closest-point@0.3.2) (2019-07-12) | ||
@@ -8,0 +16,0 @@ |
@@ -1,133 +0,1 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@thi.ng/math'), require('@thi.ng/vectors')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@thi.ng/math', '@thi.ng/vectors'], factory) : | ||
(global = global || self, factory((global.thi = global.thi || {}, global.thi.ng = global.thi.ng || {}, global.thi.ng.geomClosestPoint = {}), global.thi.ng.math, global.thi.ng.vectors)); | ||
}(this, function (exports, math, vectors) { 'use strict'; | ||
const closestT = (p, a, b) => { | ||
const d = vectors.sub([], b, a); | ||
const l = vectors.magSq(d); | ||
return l > 1e-6 ? vectors.dot(vectors.sub([], p, a), d) / l : undefined; | ||
}; | ||
const closestPointLine = (p, a, b) => vectors.mixN([], a, b, closestT(p, a, b) || 0); | ||
const distToLine = (p, a, b) => vectors.dist(p, closestPointLine(p, a, b) || a); | ||
const closestPointSegment = (p, a, b, out, insideOnly = false, eps = 0) => { | ||
const t = closestT(p, a, b); | ||
if (t !== undefined && (!insideOnly || (t >= eps && t <= 1 - eps))) { | ||
out = out || vectors.empty(p); | ||
return t <= 0 ? vectors.set(out, a) : t >= 1 ? vectors.set(out, b) : vectors.mixN(out, a, b, t); | ||
} | ||
}; | ||
const distToSegment = (p, a, b) => vectors.dist(p, closestPointSegment(p, a, b) || a); | ||
const closestPointPolyline = (p, pts, closed = false, out = []) => { | ||
if (!pts.length) | ||
return; | ||
const tmp = []; | ||
const n = pts.length - 1; | ||
let minD = Infinity, i, j; | ||
if (closed) { | ||
i = n; | ||
j = 0; | ||
} | ||
else { | ||
i = 0; | ||
j = 1; | ||
} | ||
for (; j <= n; i = j, j++) { | ||
if (closestPointSegment(p, pts[i], pts[j], tmp)) { | ||
const d = vectors.distSq(p, tmp); | ||
if (d < minD) { | ||
minD = d; | ||
vectors.set(out, tmp); | ||
} | ||
} | ||
} | ||
return out; | ||
}; | ||
const farthestPointSegment = (a, b, points, from = 0, to = points.length) => { | ||
let maxD = -1; | ||
let maxIdx = -1; | ||
const tmp = vectors.empty(a); | ||
for (let i = from; i < to; i++) { | ||
const p = points[i]; | ||
const d = vectors.distSq(p, closestPointSegment(p, a, b, tmp) || a); | ||
if (d > maxD) { | ||
maxD = d; | ||
maxIdx = i; | ||
} | ||
} | ||
return [maxIdx, Math.sqrt(maxD)]; | ||
}; | ||
const closestPointArray = (p, pts, out = []) => { | ||
let minD = Infinity; | ||
let closest; | ||
for (let i = pts.length; --i >= 0;) { | ||
const d = vectors.distSq(pts[i], p); | ||
if (d < minD) { | ||
minD = d; | ||
closest = pts[i]; | ||
} | ||
} | ||
return closest ? vectors.set(out, closest) : undefined; | ||
}; | ||
const distToPlane = (p, n, w) => vectors.dot(n, p) - w; | ||
const closestPointPlane = (p, normal, w, out = []) => vectors.sub(out, p, vectors.normalize(out, normal, distToPlane(p, normal, w))); | ||
const closestPointCircle = (p, c, r, out = []) => vectors.add(out, c, vectors.normalize(out, vectors.sub(out, p, c), r)); | ||
const closestPointSphere = closestPointCircle; | ||
const closestPointRect = (p, bmin, bmax, out = []) => { | ||
let minD = Infinity; | ||
let minID; | ||
let minW; | ||
for (let i = 0; i < 4; i++) { | ||
const j = i >> 1; | ||
const w = (i & 1 ? bmax : bmin)[j]; | ||
const d = Math.abs(p[j] - w); | ||
if (d < minD) { | ||
minD = d; | ||
minID = j; | ||
minW = w; | ||
} | ||
} | ||
return minID === 0 | ||
? vectors.setC2(out, minW, math.clamp(p[1], bmin[1], bmax[1])) | ||
: vectors.setC2(out, math.clamp(p[0], bmin[0], bmax[0]), minW); | ||
}; | ||
const closestPointAABB = (p, bmin, bmax, out = []) => { | ||
let minD = Infinity; | ||
let minID; | ||
let minW; | ||
for (let i = 0; i < 6; i++) { | ||
const j = i >> 1; | ||
const w = (i & 1 ? bmax : bmin)[j]; | ||
const d = Math.abs(p[j] - w); | ||
if (d < minD) { | ||
minD = d; | ||
minID = j; | ||
minW = w; | ||
} | ||
} | ||
return minID === 0 | ||
? vectors.setC3(out, minW, math.clamp(p[1], bmin[1], bmax[1]), math.clamp(p[2], bmin[2], bmax[2])) | ||
: minID === 1 | ||
? vectors.setC3(out, math.clamp(p[0], bmin[0], bmax[0]), minW, math.clamp(p[2], bmin[2], bmax[2])) | ||
: vectors.setC3(out, math.clamp(p[0], bmin[0], bmax[0]), math.clamp(p[1], bmin[1], bmax[1]), minW); | ||
}; | ||
exports.closestPointAABB = closestPointAABB; | ||
exports.closestPointArray = closestPointArray; | ||
exports.closestPointCircle = closestPointCircle; | ||
exports.closestPointLine = closestPointLine; | ||
exports.closestPointPlane = closestPointPlane; | ||
exports.closestPointPolyline = closestPointPolyline; | ||
exports.closestPointRect = closestPointRect; | ||
exports.closestPointSegment = closestPointSegment; | ||
exports.closestPointSphere = closestPointSphere; | ||
exports.closestT = closestT; | ||
exports.distToLine = distToLine; | ||
exports.distToPlane = distToPlane; | ||
exports.distToSegment = distToSegment; | ||
exports.farthestPointSegment = farthestPointSegment; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@thi.ng/math"),require("@thi.ng/vectors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/math","@thi.ng/vectors"],e):e(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.geomClosestPoint={}),t.thi.ng.math,t.thi.ng.vectors)}(this,function(t,e,s){"use strict";const o=(t,e,o)=>{const n=s.sub([],o,e),i=s.magSq(n);return i>1e-6?s.dot(s.sub([],t,e),n)/i:void 0},n=(t,e,n)=>s.mixN([],e,n,o(t,e,n)||0),i=(t,e,n,i,l=!1,r=0)=>{const c=o(t,e,n);if(void 0!==c&&(!l||c>=r&&c<=1-r))return i=i||s.empty(t),c<=0?s.set(i,e):c>=1?s.set(i,n):s.mixN(i,e,n,c)},l=(t,e,o)=>s.dot(e,t)-o,r=(t,e,o,n=[])=>s.add(n,e,s.normalize(n,s.sub(n,t,e),o)),c=r;t.closestPointAABB=(t,o,n,i=[])=>{let l,r,c=1/0;for(let e=0;e<6;e++){const s=e>>1,i=(1&e?n:o)[s],a=Math.abs(t[s]-i);a<c&&(c=a,l=s,r=i)}return 0===l?s.setC3(i,r,e.clamp(t[1],o[1],n[1]),e.clamp(t[2],o[2],n[2])):1===l?s.setC3(i,e.clamp(t[0],o[0],n[0]),r,e.clamp(t[2],o[2],n[2])):s.setC3(i,e.clamp(t[0],o[0],n[0]),e.clamp(t[1],o[1],n[1]),r)},t.closestPointArray=(t,e,o=[])=>{let n,i=1/0;for(let o=e.length;--o>=0;){const l=s.distSq(e[o],t);l<i&&(i=l,n=e[o])}return n?s.set(o,n):void 0},t.closestPointCircle=r,t.closestPointLine=n,t.closestPointPlane=(t,e,o,n=[])=>s.sub(n,t,s.normalize(n,e,l(t,e,o))),t.closestPointPolyline=(t,e,o=!1,n=[])=>{if(!e.length)return;const l=[],r=e.length-1;let c,a,d=1/0;for(o?(c=r,a=0):(c=0,a=1);a<=r;c=a,a++)if(i(t,e[c],e[a],l)){const e=s.distSq(t,l);e<d&&(d=e,s.set(n,l))}return n},t.closestPointRect=(t,o,n,i=[])=>{let l,r,c=1/0;for(let e=0;e<4;e++){const s=e>>1,i=(1&e?n:o)[s],a=Math.abs(t[s]-i);a<c&&(c=a,l=s,r=i)}return 0===l?s.setC2(i,r,e.clamp(t[1],o[1],n[1])):s.setC2(i,e.clamp(t[0],o[0],n[0]),r)},t.closestPointSegment=i,t.closestPointSphere=c,t.closestT=o,t.distToLine=(t,e,o)=>s.dist(t,n(t,e,o)||e),t.distToPlane=l,t.distToSegment=(t,e,o)=>s.dist(t,i(t,e,o)||e),t.farthestPointSegment=(t,e,o,n=0,l=o.length)=>{let r=-1,c=-1;const a=s.empty(t);for(let d=n;d<l;d++){const n=o[d],l=s.distSq(n,i(n,t,e,a)||t);l>r&&(r=l,c=d)}return[c,Math.sqrt(r)]},Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "@thi.ng/geom-closest-point", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Closest point / proximity helpers", | ||
@@ -17,22 +17,23 @@ "module": "./index.js", | ||
"scripts": { | ||
"build": "yarn clean && yarn build:es6 && yarn build:bundle", | ||
"build": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module", | ||
"build:release": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module all", | ||
"build:es6": "tsc --declaration", | ||
"build:bundle": "../../scripts/bundle-module", | ||
"test": "rimraf build && tsc -p test/tsconfig.json && nyc mocha build/test/*.js", | ||
"build:test": "rimraf build && tsc -p test/tsconfig.json", | ||
"test": "yarn build:test && mocha build/test/*.js", | ||
"cover": "yarn build:test && nyc mocha build/test/*.js && nyc report --reporter=lcov", | ||
"clean": "rimraf *.js *.d.ts .nyc_output build coverage doc lib", | ||
"cover": "yarn test && nyc report --reporter=lcov", | ||
"doc": "node_modules/.bin/typedoc --mode modules --out doc --ignoreCompilerErrors src", | ||
"pub": "yarn build && yarn publish --access public" | ||
"pub": "yarn build:release && yarn publish --access public" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^12.0.8", | ||
"@types/node": "^12.6.3", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.0.0", | ||
"typedoc": "^0.14.2", | ||
"typescript": "^3.5.2" | ||
"typescript": "^3.5.3" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/math": "^1.4.1", | ||
"@thi.ng/vectors": "^3.0.2" | ||
"@thi.ng/math": "^1.4.2", | ||
"@thi.ng/vectors": "^3.0.3" | ||
}, | ||
@@ -54,3 +55,3 @@ "keywords": [ | ||
"sideEffects": false, | ||
"gitHead": "47075afc37f3a16adee7c903a2935304c7cf5daf" | ||
"gitHead": "53eec7988c378fc37ae140e7174f36ef9b6208fe" | ||
} |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
48489
421
2
Updated@thi.ng/math@^1.4.2
Updated@thi.ng/vectors@^3.0.3