2014-10-05 48 views
0

我正試圖對一個標量場進行polygonise。網格上的每個點只能具有1或0的值。我需要創建一個包含256個多邊形數組的8個點的數組,但與行進多維數據集不同,這些多邊形的邊只能位於網格的點上。水密行軍立方體像查找表?

回答

0
它返回陣列

Javascript函數,需要GL-matrix.js與vec3.ceil

function() { 
     var polyConds = []; 

     var b = function() { 
      var v = 0; 
      for (var i = 0; i < arguments.length; i++) { 
       v |= 1 << (arguments[i] - 1); 
      } 
      return v; 
     }; 

     /* 0 */ 
     polyConds[b()] = []; 

     /* 1 */ 
     polyConds[b(1)] = []; 

     /* 2 */ 
     polyConds[b(1, 2)] = []; 
     polyConds[b(1, 4)] = []; 
     polyConds[b(1, 8)] = []; 

     /* 3 */ 
     polyConds[b(1, 2, 3)] = [1, 2, 3]; 
     polyConds[b(1, 2, 7)] = [1, 2, 7, 1, 7, 2]; 
     polyConds[b(1, 4, 6)] = [1, 4, 6, 1, 6, 4]; 

     /* 4 */ 
     polyConds[b(1, 2, 3, 4)] = [1, 2, 3, 2, 4, 3]; 
     polyConds[b(1, 2, 7, 8)] = [1, 2, 7, 7, 8, 2, 1, 7, 2, 7, 2, 8]; 
     polyConds[b(1, 2, 3, 6)] = [1, 6, 3, 2, 3, 6]; 
     polyConds[b(1, 2, 3, 8)] = [1, 2, 3, 2, 3, 8, 2, 8, 3]; 
     polyConds[b(1, 4, 6, 7)] = [1, 4, 7, 1, 7, 4, 1, 4, 6, 1, 6, 4]; 
     polyConds[b(1, 2, 3, 5)] = [2, 3, 5]; 
     polyConds[b(2, 3, 4, 6)] = [2, 6, 3, 3, 6, 4]; 

     /* 5 */ 
     polyConds[b(1, 2, 3, 4, 5)] = [2, 3, 5, 2, 4, 3]; 
     polyConds[b(1, 3, 4, 5, 6)] = [1, 4, 6, 3, 5, 4, 4, 5, 6]; 
     polyConds[b(1, 4, 5, 6, 7)] = [1, 6, 7]; 

     /* 6 */ 
     polyConds[b(1, 2, 3, 4, 5, 6)] = [3, 5, 4, 4, 5, 6]; 
     polyConds[b(1, 2, 4, 5, 6, 7)] = [1, 7, 4, 4, 7, 6]; 
     polyConds[b(2, 3, 4, 5, 6, 7)] = [2, 5, 3, 4, 7, 6]; 

     /* 7 */ 
     polyConds[b(1, 2, 3, 4, 5, 6, 7)] = [4, 7, 6]; 

     /* 8 */ 
     polyConds[b(1, 2, 3, 4, 5, 6, 7, 8)] = []; 

     for (var k in polyConds) { 
      var points = [ 
       [0, 0, 0], 
       [1, 0, 0], 
       [0, 1, 0], 
       [1, 1, 0], 
       [0, 0, 1], 
       [1, 0, 1], 
       [0, 1, 1], 
       [1, 1, 1], 
      ]; 

      polyConds[k] = polyConds[k].map(function (v) {return points[v - 1]}); 
     } 

     var rotations = [ 
      [ 
       quat.setAxisAngle(quat.create(), vec3.fromValues(1, 0, 0), 0), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(1, 0, 0), Math.PI/2), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(1, 0, 0), Math.PI), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(1, 0, 0), Math.PI/2*3), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 1, 0), Math.PI/2), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 1, 0), Math.PI/2*3), 
      ], 
      [ 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 0, 1), 0), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 0, 1), Math.PI/2), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 0, 1), Math.PI), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 0, 1), Math.PI/2*3), 
      ], 
     ]; 

     var unrotations = [ 
      [ 
       quat.setAxisAngle(quat.create(), vec3.fromValues(1, 0, 0), 0), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(1, 0, 0), -Math.PI/2), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(1, 0, 0), -Math.PI), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(1, 0, 0), -Math.PI/2*3), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 1, 0), -Math.PI/2), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 1, 0), -Math.PI/2*3), 
      ], 
      [ 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 0, 1), 0), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 0, 1), -Math.PI/2), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 0, 1), -Math.PI), 
       quat.setAxisAngle(quat.create(), vec3.fromValues(0, 0, 1), -Math.PI/2*3), 
      ], 
     ]; 

     for (var i = 0; i < 256; i++) 
     for (var a = 0, al = rotations[0].length; a < al; a++) 
     for (var b = 0, bl = rotations[1].length; b < bl; b++) { 

      var points = [ 
       [0, 0, 0], 
       [1, 0, 0], 
       [0, 1, 0], 
       [1, 1, 0], 
       [0, 0, 1], 
       [1, 0, 1], 
       [0, 1, 1], 
       [1, 1, 1], 
      ]; 

      var vertices = []; 
      for (var j = 0; j < 8; j++) { 
       if (i >> j & 1) { 
        vertices.push(points[j]); 
       } 
      } 

      for (var j = 0, jl = vertices.length; j < jl; j++) { 
       var v = vertices[j]; 

       vec3.sub(v, v, vec3.fromValues(0.5, 0.5, 0.5)); 
       vec3.transformQuat(v, v, rotations[0][a]); 
       vec3.transformQuat(v, v, rotations[1][b]); 
       vec3.ceil(v, v); 
      } 


      var bits = 0; 
      for (var j = 0, jl = vertices.length; j < jl; j++) { 
       bits |= 1 << (vertices[j][0] + vertices[j][1] * 2 + vertices[j][2] * 4); 
      } 

      if (polyConds[bits]) { 
       vertices = JSON.parse(JSON.stringify(polyConds[bits])); 

       for (var j = 0, jl = vertices.length; j < jl; j++) { 
        var v = vertices[j]; 

        vec3.sub(v, v, vec3.fromValues(0.5, 0.5, 0.5)); 
        vec3.transformQuat(v, v, unrotations[1][b]); 
        vec3.transformQuat(v, v, unrotations[0][a]); 
        vec3.ceil(v, v); 
       } 

       polyConds[i] = vertices; 

       a = Infinity; // break to first loop 
       b = Infinity; 
      } 
     } 

     return polyConds; 
    }