概要
plunkerでopenjscadやってみる。
練習問題やってみた。
練習問題
MQOをパースして、描画せよ。
写真
サンプルコード
function main() {
var mqo =
"Metasequoia Document\n" +
"Format Text Ver 1.0\n" +
"\n" +
"Scene {\n" +
" pos 0.0000 0.0000 1500.0000\n" +
" lookat 0.0000 0.0000 0.0000\n" +
" head 3.7864\n" +
" pich 0.2392\n" +
" ortho 0\n" +
" zoom2 5.0000\n" +
" amb 0.250 0.250 0.250\n" +
"}\n" +
"Material 2 {\n" +
' "mat1" col(1.000 0.000 0.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)\n' +
' "mat2" col(1.000 0.647 0.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)\n' +
"}\n" +
'Object "obj1" {\n' +
" visible 15\n" +
" locking 0\n" +
" shading 0\n" +
" facet 59.5\n" +
" color 0.898 0.498 0.698\n" +
" color_type 0\n" +
" vertex 8 {\n" +
" -100.0000 100.0000 100.0000\n" +
" -100.0000 -100.0000 100.0000\n" +
" 100.0000 100.0000 100.0000\n" +
" 100.0000 -100.0000 100.0000\n" +
" 100.0000 100.0000 -100.0000\n" +
" 100.0000 -100.0000 -100.0000\n" +
" -100.0000 100.0000 -100.0000\n" +
" -100.0000 -100.0000 -100.0000\n" +
" }\n" +
" face 6 {\n" +
" 4 V(0 2 3 1) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\n" +
" 4 V(2 4 5 3) M(1) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\n" +
" 4 V(4 6 7 5) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\n" +
" 4 V(6 0 1 7) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\n" +
" 4 V(6 4 2 0) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\n" +
" 4 V(1 3 5 7) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)\n" +
" }\n" +
"}\n" +
"Eof";
var points = [];
var faces = [];
var textArray = mqo.split(/\r\n|\r|\n/);
for (var i = 0; i < textArray.length; i++)
{
var line = textArray[i];
if (line.indexOf(' 4') === 0)
{
line = line.replace(' 4 V(', '');
var tmp = line.replace(')', '');
var p = tmp.split(' ');
faces.push([p[0], p[1], p[2]]);
faces.push([p[0], p[2], p[3]]);
}
else if (line.indexOf(' ') === 0)
{
line = line.trim();
var tmp = line.split(' ');
points.push([tmp[0], tmp[1], tmp[2]]);
}
}
return CSG.polyhedron({
points: points,
faces: faces
});
}
成果物
以上。