"use strict";function cofactor(r,t){for(var n=new Array(r.length-1),e=1;e<r.length;++e)for(var o=n[e-1]=new Array(r.length-1),a=0,u=0;a<r.length;++a)a!==t&&(o[u++]=r[e][a]);return n}function matrix(r){for(var t=new Array(r),n=0;r>n;++n){t[n]=new Array(r);for(var e=0;r>e;++e)t[n][e]=["m",e,"[",r-n-1,"]"].join("")}return t}function sign(r){return 1&r?"-":""}function generateSum(r){if(1===r.length)return r[0];if(2===r.length)return["sum(",r[0],",",r[1],")"].join("");var t=r.length>>1;return["sum(",generateSum(r.slice(0,t)),",",generateSum(r.slice(t)),")"].join("")}function determinant(r){if(2===r.length)return[["sum(prod(",r[0][0],",",r[1][1],"),prod(-",r[0][1],",",r[1][0],"))"].join("")];for(var t=[],n=0;n<r.length;++n)t.push(["scale(",generateSum(determinant(cofactor(r,n))),",",sign(n),r[0][n],")"].join(""));return t}function orientation(r){for(var t=[],n=[],e=matrix(r),o=[],a=0;r>a;++a)0===(1&a)?t.push.apply(t,determinant(cofactor(e,a))):n.push.apply(n,determinant(cofactor(e,a))),o.push("m"+a);var u=generateSum(t),i=generateSum(n),s="orientation"+r+"Exact",c=["function ",s,"(",o.join(),"){var p=",u,",n=",i,",d=sub(p,n);return d[d.length-1];};return ",s].join(""),h=new Function("sum","prod","scale","sub",c);return h(robustSum,twoProduct,robustScale,robustSubtract)}function slowOrient(r){var t=CACHED[r.length];return t||(t=CACHED[r.length]=orientation(r.length)),t.apply(void 0,r)}function generateOrientationProc(){for(;CACHED.length<=NUM_EXPAND;)CACHED.push(orientation(CACHED.length));for(var r=[],t=["slow"],n=0;NUM_EXPAND>=n;++n)r.push("a"+n),t.push("o"+n);for(var e=["function getOrientation(",r.join(),"){switch(arguments.length){case 0:case 1:return 0;"],n=2;NUM_EXPAND>=n;++n)e.push("case ",n,":return o",n,"(",r.slice(0,n).join(),");");e.push("}var s=new Array(arguments.length);for(var i=0;i<arguments.length;++i){s[i]=arguments[i]};return slow(s);}return getOrientation"),t.push(e.join(""));var o=Function.apply(void 0,t);module.exports=o.apply(void 0,[slowOrient].concat(CACHED));for(var n=0;NUM_EXPAND>=n;++n)module.exports[n]=CACHED[n]}var twoProduct=require("two-product"),robustSum=require("robust-sum"),robustScale=require("robust-scale"),robustSubtract=require("robust-subtract"),NUM_EXPAND=5,EPSILON=1.1102230246251565e-16,ERRBOUND3=(3+16*EPSILON)*EPSILON,ERRBOUND4=(7+56*EPSILON)*EPSILON,orientation3Exact=orientation(3),orientation4Exact=orientation(4),CACHED=[function(){return 0},function(){return 0},function(r,t){return t[0]-r[0]},function(r,t,n){var e,o=(r[1]-n[1])*(t[0]-n[0]),a=(r[0]-n[0])*(t[1]-n[1]),u=o-a;if(o>0){if(0>=a)return u;e=o+a}else{if(!(0>o))return u;if(a>=0)return u;e=-(o+a)}var i=ERRBOUND3*e;return u>=i||-i>=u?u:orientation3Exact(r,t,n)},function(r,t,n,e){var o=r[0]-e[0],a=t[0]-e[0],u=n[0]-e[0],i=r[1]-e[1],s=t[1]-e[1],c=n[1]-e[1],h=r[2]-e[2],l=t[2]-e[2],f=n[2]-e[2],g=a*c,p=u*s,m=u*i,E=o*c,b=o*s,v=a*i,N=h*(g-p)+l*(m-E)+f*(b-v),d=(Math.abs(g)+Math.abs(p))*Math.abs(h)+(Math.abs(m)+Math.abs(E))*Math.abs(l)+(Math.abs(b)+Math.abs(v))*Math.abs(f),A=ERRBOUND4*d;return N>A||-N>A?N:orientation4Exact(r,t,n,e)}];generateOrientationProc();
Example #2
0
tape("sphere-test", function(t) {
  var shape = []
  var size = 1
  var fact = 1
  var funcs = [
    function(x) {
      return Math.pow(x-8,2)
    },
    function(x,y) {
      return Math.pow(x-8,2) + Math.pow(y-8,2)
    },
    function(x,y,z) {
      return Math.pow(x-8,2) + Math.pow(y-8,2) + Math.pow(z-8,2)
    },
    function(x,y,z,w) {
      return Math.pow(x-8,2) + Math.pow(y-8,2) + Math.pow(z-8,2) + Math.pow(w-8,2)
    }
  ]
  for(var d=1; d<=4; ++d) {
    shape.push(16)
    size *= 16
    fact *= d    

    var x = ndarray(new Float32Array(size), shape)
    fill(x, funcs[d-1])
    var surf = surfaceNets(x, 36)
    for(var k=0; k<surf.positions.length; ++k) {
      var p = surf.positions[k]
      var u = 0.0
      for(var j=0; j<d; ++j) {
        u += Math.pow(p[j]-8,2)
      }
      t.ok(Math.abs(u-36) <= 1.2, "test: " + d + "d - " + u)
    }

    if(d > 1) {
      //Compute volume of surface
      var vol = 0.0
      for(var k=0; k<surf.cells.length; ++k) {
        var c = surf.cells[k]
        var m = []
        for(var i=0; i<c.length; ++i) {
          var pp = surf.positions[c[i]].slice()
          for(var j=0; j<d; ++j) {
            pp[j] -= 8
          }
          m.push(pp)
        }
        var r = det(m)
        t.ok(r[r.length-1] >= -1e-6, "check orientation: " + r)
        vol += r[r.length-1] / gamma(d+1)
      }
      var bvol = Math.pow(Math.PI, Math.floor(d/2)) * Math.pow(6.0, d)
      if((d % 2) === 0) {
        bvol /= gamma((d/2) + 1)
      } else {
        var kk = Math.floor(d/2)
        bvol *= Math.pow(2.0, d) * gamma(1+kk) / gamma(1+d)
      }
      t.ok(Math.abs(vol / bvol - 1.0) < 0.05, "check volume - vol=" + vol + " vs bvol=" + bvol)
    }
  }
  t.end()
})
"use strict";function cofactor(r,e){for(var t=new Array(r.length-1),n=1;n<r.length;++n)for(var o=t[n-1]=new Array(r.length-1),u=0,i=0;u<r.length;++u)u!==e&&(o[i++]=r[n][u]);return t}function matrix(r){for(var e=new Array(r),t=0;r>t;++t){e[t]=new Array(r);for(var n=0;r>n;++n)e[t][n]=["m[",t,"][",n,"]"].join("")}return e}function sign(r){return 1&r?"-":""}function generateSum(r){if(1===r.length)return r[0];if(2===r.length)return["sum(",r[0],",",r[1],")"].join("");var e=r.length>>1;return["sum(",generateSum(r.slice(0,e)),",",generateSum(r.slice(e)),")"].join("")}function determinant(r){if(2===r.length)return["sum(prod(",r[0][0],",",r[1][1],"),prod(-",r[0][1],",",r[1][0],"))"].join("");for(var e=[],t=0;t<r.length;++t)e.push(["scale(",determinant(cofactor(r,t)),",",sign(t),r[0][t],")"].join(""));return generateSum(e)}function compileDeterminant(r){var e=new Function("sum","scale","prod","compress",["function robustDeterminant",r,"(m){return compress(",determinant(matrix(r)),")};return robustDeterminant",r].join(""));return e(robustSum,robustScale,twoProduct,compress)}function generateDispatch(){for(;CACHE.length<NUM_EXPANDED;)CACHE.push(compileDeterminant(CACHE.length));for(var r=[],e=["function robustDeterminant(m){switch(m.length){"],t=0;NUM_EXPANDED>t;++t)r.push("det"+t),e.push("case ",t,":return det",t,"(m);");e.push("}var det=CACHE[m.length];if(!det)det=CACHE[m.length]=gen(m.length);return det(m);}return robustDeterminant"),r.push("CACHE","gen",e.join(""));var n=Function.apply(void 0,r);module.exports=n.apply(void 0,CACHE.concat([CACHE,compileDeterminant]));for(var t=0;t<CACHE.length;++t)module.exports[t]=CACHE[t]}var twoProduct=require("two-product"),robustSum=require("robust-sum"),robustScale=require("robust-scale"),compress=require("robust-compress"),NUM_EXPANDED=6,CACHE=[function(){return[0]},function(r){return[r[0][0]]}];generateDispatch();