From 4d40fd95a4741caa8ba808d51117a3afc25b20cf Mon Sep 17 00:00:00 2001 From: Tim Langlois Date: Mon, 10 Jul 2017 09:16:34 -0700 Subject: [PATCH 1/3] export the lineSegmentsIntersect function for external use. Also update package dependencies to newest version. --- Gruntfile.js | 2 +- build/decomp.js | 10 +++++----- build/decomp.min.js | 2 +- package.json | 12 ++++++------ src/index.js | 3 ++- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 74fc1ca..d3bcb0b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -8,7 +8,7 @@ module.exports = function(grunt) { src : ["src/index.js"], dest : 'build/decomp.js', options:{ - bundleOptions : { + browserifyOptions : { standalone : "decomp" } } diff --git a/build/decomp.js b/build/decomp.js index ffd33bd..dd1f907 100644 --- a/build/decomp.js +++ b/build/decomp.js @@ -1,11 +1,12 @@ -!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.decomp=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o=0&&1>=u&&p>=0&&1>=p}function i(e,f,o){return(f[0]-e[0])*(o[1]-e[1])-(o[0]-e[0])*(f[1]-e[1])}function t(e,f,o){return i(e,f,o)>0}function l(e,f,o){return i(e,f,o)>=0}function u(e,f,o){return i(e,f,o)<0}function p(e,f,o){return i(e,f,o)<=0}function s(e,f,o,n){if(n){var d=F,t=G;d[0]=f[0]-e[0],d[1]=f[1]-e[1],t[0]=o[0]-f[0],t[1]=o[1]-f[1];var l=d[0]*t[0]+d[1]*t[1],u=Math.sqrt(d[0]*d[0]+d[1]*d[1]),p=Math.sqrt(t[0]*t[0]+t[1]*t[1]),s=Math.acos(l/(u*p));return n>s}return 0===i(e,f,o)}function c(e,f){var o=f[0]-e[0],n=f[1]-e[1];return o*o+n*n}function y(e,f){var o=e.length;return e[0>f?f%o+o:f%o]}function a(e){e.length=0}function m(e,f,o,n){for(var d=o;n>d;d++)e.push(f[d])}function r(e){for(var f=0,o=e,n=1;no[f][0])&&(f=n);return t(y(e,f-1),y(e,f),y(e,f+1))?!1:(w(e),!0)}function w(e){for(var f=[],o=e.length,n=0;n!==o;n++)f.push(e.pop());for(var n=0;n!==o;n++)e[n]=f[n]}function b(e,f){return u(y(e,f-1),y(e,f),y(e,f+1))}function g(e,f,o){var d,i,t=H,u=I;if(l(y(e,f+1),y(e,f),y(e,o))&&p(y(e,f-1),y(e,f),y(e,o)))return!1;i=c(y(e,f),y(e,o));for(var s=0;s!==e.length;++s)if((s+1)%e.length!==f&&s!==f&&l(y(e,f),y(e,o),y(e,s+1))&&p(y(e,f),y(e,o),y(e,s))&&(t[0]=y(e,f),t[1]=y(e,o),u[0]=y(e,s),u[1]=y(e,s+1),d=n(t,u),c(y(e,f),d)f)for(var i=f;o>=i;i++)d.push(e[i]);else{for(var i=0;o>=i;i++)d.push(e[i]);for(var i=f;i0?k(e,f):[e]}function k(e,f){if(0===f.length)return[e];if(f instanceof Array&&f.length&&f[0]instanceof Array&&2===f[0].length&&f[0][0]instanceof Array){for(var o=[e],n=0;nn;n++)if(d(o[f],o[f+1],o[n],o[n+1]))return!1;for(f=1;fi)return console.warn("quickDecomp: max level ("+i+") reached."),f;for(var G=0;Gv&&(j=v,r=w,q=H))),t(y(E,G+1),y(E,G),y(E,H+1))&&p(y(E,G+1),y(E,G),y(E,H))&&(w=z(y(E,G+1),y(E,G),y(E,H),y(E,H+1)),t(y(E,G-1),y(E,G),w)&&(v=c(E[G],w),g>v&&(g=v,a=w,k=H)));if(q===(k+1)%e.length)w[0]=(r[0]+a[0])/2,w[1]=(r[1]+a[1])/2,n.push(w),k>G?(m(C,E,G,k+1),C.push(w),D.push(w),0!==q&&m(D,E,q,E.length),m(D,E,0,G+1)):(0!==G&&m(C,E,G,E.length),m(C,E,0,k+1),C.push(w),D.push(w),m(D,E,q,G+1));else{if(q>k&&(k+=e.length),h=Number.MAX_VALUE,q>k)return f;for(var H=q;k>=H;++H)l(y(E,G-1),y(E,G),y(E,H))&&p(y(E,G+1),y(E,G),y(E,H))&&(v=c(y(E,G),y(E,H)),h>v&&x(E,G,H)&&(h=v,B=H%e.length));B>G?(m(C,E,G,B+1),0!==B&&m(D,E,B,F.length),m(D,E,0,G+1)):(0!==G&&m(C,E,G,F.length),m(C,E,0,B+1),m(D,E,B,G+1))}return C.length3&&n>=0;--n)s(y(e,n-1),y(e,n),y(e,n+1),f)&&(e.splice(n%e.length,1),o++);return o}function C(e,f){for(var o=e.length-1;o>=1;--o)for(var n=e[o],d=o-1;d>=0;--d)E(n,e[d],f)&&e.splice(o,1)}function D(e,f,o){return o=o||0,Math.abs(e-f)<=o}function E(e,f,o){return D(e[0],f[0],o)&&D(e[1],f[1],o)}f.exports={decomp:h,quickDecomp:A,isSimple:q,removeCollinearPoints:B,removeDuplicatePoints:C,makeCCW:r};var F=[],G=[],H=[],I=[]},{}]},{},[1])(1)}); \ No newline at end of file +!function(r){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).decomp=r()}}(function(){return function f(o,i,a){function l(n,r){if(!i[n]){if(!o[n]){var e="function"==typeof require&&require;if(!r&&e)return e(n,!0);if(h)return h(n,!0);var t=new Error("Cannot find module '"+n+"'");throw t.code="MODULE_NOT_FOUND",t}var u=i[n]={exports:{}};o[n][0].call(u.exports,function(r){return l(o[n][1][r]||r)},u,u.exports,f,o,i,a)}return i[n].exports}for(var h="function"==typeof require&&require,r=0;re[n][0])&&(n=t);return!w(U(r,n-1),U(r,n),U(r,n+1))&&(function(r){for(var n=[],e=r.length,t=0;t!==e;t++)n.push(r.pop());for(var t=0;t!==e;t++)r[t]=n[t]}(r),!0)},lineSegmentsIntersect:u};var h=[],c=[];function f(r,n,e,t){if(t){var u=h,f=c;u[0]=n[0]-r[0],u[1]=n[1]-r[1],f[0]=e[0]-n[0],f[1]=e[1]-n[1];var o=u[0]*f[0]+u[1]*f[1],i=Math.sqrt(u[0]*u[0]+u[1]*u[1]),a=Math.sqrt(f[0]*f[0]+f[1]*f[1]);return Math.acos(o/(i*a))=3.44.2", + "grunt": ">=1.0.1", + "grunt-browserify": ">=2.0.8", + "grunt-contrib-nodeunit": ">=1.0.0", + "grunt-contrib-uglify": ">=0.4.1", + "nodeunit": ">=0.9.5" } } diff --git a/src/index.js b/src/index.js index cd7ea48..155e093 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,8 @@ module.exports = { isSimple: polygonIsSimple, removeCollinearPoints: polygonRemoveCollinearPoints, removeDuplicatePoints: polygonRemoveDuplicatePoints, - makeCCW: polygonMakeCCW + makeCCW: polygonMakeCCW, + lineSegmentsIntersect: lineSegmentsIntersect }; /** From fe7b656ac3cd13dd5bb95f7a7a62c91cdb61a8e3 Mon Sep 17 00:00:00 2001 From: Tim Langlois Date: Tue, 11 Jul 2017 11:33:18 -0700 Subject: [PATCH 2/3] remove console.warn in quickDecomp --- build/decomp.js | 2 +- build/decomp.min.js | 2 +- src/index.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build/decomp.js b/build/decomp.js index dd1f907..af97727 100644 --- a/build/decomp.js +++ b/build/decomp.js @@ -473,7 +473,7 @@ function polygonQuickDecomp(polygon, result,reflexVertices,steinerPoints,delta,m level++; if(level > maxlevel){ - console.warn("quickDecomp: max level ("+maxlevel+") reached."); + //console.warn("quickDecomp: max level ("+maxlevel+") reached."); return result; } diff --git a/build/decomp.min.js b/build/decomp.min.js index 3cd8a3b..dd910c4 100644 --- a/build/decomp.min.js +++ b/build/decomp.min.js @@ -1 +1 @@ -!function(r){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).decomp=r()}}(function(){return function f(o,i,a){function l(n,r){if(!i[n]){if(!o[n]){var e="function"==typeof require&&require;if(!r&&e)return e(n,!0);if(h)return h(n,!0);var t=new Error("Cannot find module '"+n+"'");throw t.code="MODULE_NOT_FOUND",t}var u=i[n]={exports:{}};o[n][0].call(u.exports,function(r){return l(o[n][1][r]||r)},u,u.exports,f,o,i,a)}return i[n].exports}for(var h="function"==typeof require&&require,r=0;re[n][0])&&(n=t);return!w(U(r,n-1),U(r,n),U(r,n+1))&&(function(r){for(var n=[],e=r.length,t=0;t!==e;t++)n.push(r.pop());for(var t=0;t!==e;t++)r[t]=n[t]}(r),!0)},lineSegmentsIntersect:u};var h=[],c=[];function f(r,n,e,t){if(t){var u=h,f=c;u[0]=n[0]-r[0],u[1]=n[1]-r[1],f[0]=e[0]-n[0],f[1]=e[1]-n[1];var o=u[0]*f[0]+u[1]*f[1],i=Math.sqrt(u[0]*u[0]+u[1]*u[1]),a=Math.sqrt(f[0]*f[0]+f[1]*f[1]);return Math.acos(o/(i*a))e[n][0])&&(n=t);return!w(_(r,n-1),_(r,n),_(r,n+1))&&(function(r){for(var n=[],e=r.length,t=0;t!==e;t++)n.push(r.pop());for(var t=0;t!==e;t++)r[t]=n[t]}(r),!0)},lineSegmentsIntersect:u};var h=[],c=[];function f(r,n,e,t){if(t){var u=h,f=c;u[0]=n[0]-r[0],u[1]=n[1]-r[1],f[0]=e[0]-n[0],f[1]=e[1]-n[1];var o=u[0]*f[0]+u[1]*f[1],i=Math.sqrt(u[0]*u[0]+u[1]*u[1]),a=Math.sqrt(f[0]*f[0]+f[1]*f[1]);return Math.acos(o/(i*a)) maxlevel){ - console.warn("quickDecomp: max level ("+maxlevel+") reached."); + //console.warn("quickDecomp: max level ("+maxlevel+") reached."); return result; } From 2e10259f102bfaa2b6f039b870285e104680ab9c Mon Sep 17 00:00:00 2001 From: Tim Langlois Date: Wed, 14 Nov 2018 12:06:31 -0800 Subject: [PATCH 3/3] clamp values to [0,1] before passing to acos function --- build/decomp.js | 2 +- build/decomp.min.js | 2 +- src/index.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build/decomp.js b/build/decomp.js index af97727..ca3095e 100644 --- a/build/decomp.js +++ b/build/decomp.js @@ -118,7 +118,7 @@ function collinear(a,b,c,thresholdAngle) { var dot = ab[0]*bc[0] + ab[1]*bc[1], magA = Math.sqrt(ab[0]*ab[0] + ab[1]*ab[1]), magB = Math.sqrt(bc[0]*bc[0] + bc[1]*bc[1]), - angle = Math.acos(dot/(magA*magB)); + angle = Math.acos( Math.min(1, Math.max(0, dot/(magA*magB))) ); return angle < thresholdAngle; } } diff --git a/build/decomp.min.js b/build/decomp.min.js index dd910c4..7a16f8d 100644 --- a/build/decomp.min.js +++ b/build/decomp.min.js @@ -1 +1 @@ -!function(r){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).decomp=r()}}(function(){return function f(o,i,a){function l(n,r){if(!i[n]){if(!o[n]){var e="function"==typeof require&&require;if(!r&&e)return e(n,!0);if(h)return h(n,!0);var t=new Error("Cannot find module '"+n+"'");throw t.code="MODULE_NOT_FOUND",t}var u=i[n]={exports:{}};o[n][0].call(u.exports,function(r){return l(o[n][1][r]||r)},u,u.exports,f,o,i,a)}return i[n].exports}for(var h="function"==typeof require&&require,r=0;re[n][0])&&(n=t);return!w(_(r,n-1),_(r,n),_(r,n+1))&&(function(r){for(var n=[],e=r.length,t=0;t!==e;t++)n.push(r.pop());for(var t=0;t!==e;t++)r[t]=n[t]}(r),!0)},lineSegmentsIntersect:u};var h=[],c=[];function f(r,n,e,t){if(t){var u=h,f=c;u[0]=n[0]-r[0],u[1]=n[1]-r[1],f[0]=e[0]-n[0],f[1]=e[1]-n[1];var o=u[0]*f[0]+u[1]*f[1],i=Math.sqrt(u[0]*u[0]+u[1]*u[1]),a=Math.sqrt(f[0]*f[0]+f[1]*f[1]);return Math.acos(o/(i*a))e[n][0])&&(n=t);return!w(_(r,n-1),_(r,n),_(r,n+1))&&(function(r){for(var n=[],e=r.length,t=0;t!==e;t++)n.push(r.pop());for(var t=0;t!==e;t++)r[t]=n[t]}(r),!0)},lineSegmentsIntersect:u};var l=[],c=[];function f(r,n,e,t){if(t){var u=l,f=c;u[0]=n[0]-r[0],u[1]=n[1]-r[1],f[0]=e[0]-n[0],f[1]=e[1]-n[1];var o=u[0]*f[0]+u[1]*f[1],i=Math.sqrt(u[0]*u[0]+u[1]*u[1]),a=Math.sqrt(f[0]*f[0]+f[1]*f[1]);return Math.acos(Math.min(1,Math.max(0,o/(i*a))))