概要
plunkerでopenjscadやってみる。
練習問題やってみた。
練習問題
STLをパースして、描画せよ。
写真
サンプルコード
function main() {
var stl =
" solid square-ascii.SLDPRT\n" +
" facet normal 0.000000e+00 0.000000e+00 -1.000000e+00\n" +
" outer loop\n" +
" vertex 0.000000e+00 2.500000e+01 0.000000e+00\n" +
" vertex 2.500000e+01 2.500000e+01 0.000000e+00\n" +
" vertex 0.000000e+00 0.000000e+00 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
" facet normal 0.000000e+00 -0.000000e+00 -1.000000e+00\n" +
" outer loop\n" +
" vertex 2.500000e+01 2.500000e+01 0.000000e+00\n" +
" vertex 2.500000e+01 0.000000e+00 0.000000e+00\n" +
" vertex 0.000000e+00 0.000000e+00 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
" facet normal -1.000000e+00 0.000000e+00 0.000000e+00\n" +
" outer loop\n" +
" vertex 0.000000e+00 0.000000e+00 2.500000e+01\n" +
" vertex 0.000000e+00 2.500000e+01 0.000000e+00\n" +
" vertex 0.000000e+00 0.000000e+00 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
" facet normal -0.000000e+00 -1.000000e+00 0.000000e+00\n" +
" outer loop\n" +
" vertex 2.500000e+01 0.000000e+00 2.500000e+01\n" +
" vertex 0.000000e+00 0.000000e+00 2.500000e+01\n" +
" vertex 0.000000e+00 0.000000e+00 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
" facet normal 0.000000e+00 -1.000000e+00 0.000000e+00\n" +
" outer loop\n" +
" vertex 2.500000e+01 0.000000e+00 0.000000e+00\n" +
" vertex 2.500000e+01 0.000000e+00 2.500000e+01\n" +
" vertex 0.000000e+00 0.000000e+00 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
" facet normal 1.000000e+00 0.000000e+00 -0.000000e+00\n" +
" outer loop\n" +
" vertex 2.500000e+01 2.500000e+01 0.000000e+00\n" +
" vertex 2.500000e+01 2.500000e+01 2.500000e+01\n" +
" vertex 2.500000e+01 0.000000e+00 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
" facet normal 1.000000e+00 0.000000e+00 0.000000e+00\n" +
" outer loop\n" +
" vertex 2.500000e+01 2.500000e+01 2.500000e+01\n" +
" vertex 2.500000e+01 0.000000e+00 2.500000e+01\n" +
" vertex 2.500000e+01 0.000000e+00 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
" facet normal -1.000000e+00 -0.000000e+00 -0.000000e+00\n" +
" outer loop\n" +
" vertex 0.000000e+00 2.500000e+01 2.500000e+01\n" +
" vertex 0.000000e+00 2.500000e+01 0.000000e+00\n" +
" vertex 0.000000e+00 0.000000e+00 2.500000e+01\n" +
" endloop\n" +
" endfacet\n" +
" facet normal 0.000000e+00 0.000000e+00 1.000000e+00\n" +
" outer loop\n" +
" vertex 2.500000e+01 0.000000e+00 2.500000e+01\n" +
" vertex 0.000000e+00 2.500000e+01 2.500000e+01\n" +
" vertex 0.000000e+00 0.000000e+00 2.500000e+01\n" +
" endloop\n" +
" endfacet\n" +
" facet normal 0.000000e+00 0.000000e+00 1.000000e+00\n" +
" outer loop\n" +
" vertex 2.500000e+01 2.500000e+01 2.500000e+01\n" +
" vertex 0.000000e+00 2.500000e+01 2.500000e+01\n" +
" vertex 2.500000e+01 0.000000e+00 2.500000e+01\n" +
" endloop\n" +
" endfacet\n" +
" facet normal 0.000000e+00 1.000000e+00 0.000000e+00\n" +
" outer loop\n" +
" vertex 0.000000e+00 2.500000e+01 2.500000e+01\n" +
" vertex 2.500000e+01 2.500000e+01 0.000000e+00\n" +
" vertex 0.000000e+00 2.500000e+01 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
" facet normal -0.000000e+00 1.000000e+00 0.000000e+00\n" +
" outer loop\n" +
" vertex 0.000000e+00 2.500000e+01 2.500000e+01\n" +
" vertex 2.500000e+01 2.500000e+01 2.500000e+01\n" +
" vertex 2.500000e+01 2.500000e+01 0.000000e+00\n" +
" endloop\n" +
" endfacet\n" +
"endsolid";
var points = [];
var faces = [];
var t = 0;
var vertexes = stl.match(/facet\s+normal\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+outer\s+loop\s+vertex\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+vertex\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+vertex\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+endloop\s+endfacet/g);
vertexes.forEach(function(vert) {
vert.match(/vertex\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s/g).forEach(function(vertex, i) {
var tempVertex = vertex.replace('vertex', '').match(/[-+]?[0-9]*\.?[0-9]+/g);
points.push([tempVertex[0], tempVertex[2], tempVertex[4]]);
});
faces.push([t, t + 1, t + 2]);
t = t + 3;
})
return CSG.polyhedron({
points: points,
faces: faces
});
};
成果物
以上
Comments
Let's comment your feelings that are more than good