plunkerでopenjscad その13

# 概要
plunkerでopenjscadやってみる。
練習問題やってみた。

# 練習問題
ROKをパースして、描画せよ。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/b03bf5ac-4a18-4f7a-44cf-97476d961e7a.png)


# サンプルコード

```

String.prototype.startsWith = function(ss) {
	return this.slice(0, ss.length) === ss;
};
String.prototype.ltrim = function() {
	return this.replace(/^\s+/, "");
};
String.prototype.rtrim = function() {
	return this.replace(/\s+$/, "");
};
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
};

function main() {
	var rok = 
"ROKU4\n" +
"    -1.139258874\n" +    
"    -0.184799568\n" +
"     0.000000000\n" +
"     0.000000000\n" +
"     0.000000000\n" +
"     1.500000000\n" +
"     1.000000000\n" +
"     1.000000000\n" +
"    1\n" +
"    0\n" +
"   400.000000000\n" +
"   400.000000000\n" +
"   343.000000000\n" +
"   160.000000000\n" +
"POINT\n" +
"    1\n" +
"    0\n" +
"    0\n" +
"    0\n" +
"    1\n" +
"     0.005691000\n" +
"     0.500000000\n" +
"     0.177817000\n" +
"    2\n" +
"    0\n" +
"    0\n" +
"    0\n" +
"    2\n" +
"     0.005691000\n" +
"     0.500000000\n" +
"     0.361567000\n" +
"    3\n" +
"    0\n" +
"    0\n" +
"    0\n" +
"    3\n" +
"     0.189441000\n" +
"     0.500000000\n" +
"     0.361567000\n" +
"    4\n" +
"    0\n" +
"    0\n" +
"    0\n" +
"    4\n" +
"     0.189441000\n" +
"     0.500000000\n" +
"     0.177817000\n" +
"LINE0\n" +
"    2\n" +
"    1\n" +
"    0\n" +
"    0\n" +
"    1\n" +
"    4\n" +
"    0\n" +
"    0\n" +
"    4\n" +
"    3\n" +
"    0\n" +
"    0\n" +
"    3\n" +
"    2\n" +
"    0\n" +
"    0\n" +
"FACE0\n" +
"    4\n" +
"    0\n" +
"    1\n" +
"    1\n" +
"    2\n" +
"    3\n" +
"    4\n" +
"END00";
	var points = [];
	var faces = [];
	var state = 0;
	var lines = rok.split('\n');
	var vnam = 0;
	var lnam = 0;
	var fmod = 0;
	var verts = 0;
	var x = 0,
		y = 0,
		z = 0,
		s = 0,
		e = 0;
	var c = 0;
	var v = [];
	var l = [];
	var fnam = 0;
	for (var i = 0; i < lines.length; i++)
	{
		var tline = lines[i].trim();
		if (tline.startsWith('POINT'))
		{
			state = 1;
		}
		else if (tline.startsWith('LINE0'))
		{
			state = 2;
		}
		else if (tline.startsWith('FACE0'))
		{
			state = 3;
		}
		else if (tline.startsWith('END00'))
		{
			state = 4;
		}
		else
		{
			var line = tline;
			if (state > 0)
			{
				c += 1;
			}
			if (state == 1)
			{
				if (c == 1)
				{
					vnam = parseInt(line);
				}
				if (c == 6)
				{
					x = parseFloat(line);
				}
				if (c == 7)
				{
					y = parseFloat(line);
				}
				if (c == 8)
				{
					z = parseFloat(line);
					v[vnam] = [x * 100, -y * 100, -z * 100];
					c = 0;
				}
			}
			else if (state == 2)
			{
				if (c == 1)
				{
					s = parseInt(line);
				}
				if (c == 2)
				{
					e = parseInt(line);
				}
				if (c == 3)
				{
					lnam += 1;
				}
				if (c == 4)
				{
					l[lnam] = [s, e];
					c = 0;
				}
			}
			else if (state == 3)
			{
				if (c == 1)
				{
					fmod = parseInt(line);
				}
				if (c == 2)
				{
					f = [];
				}
				if (c == 4)
				{
					f[0] = parseInt(line);
				}
				if (c == 5)
				{
					f[1] = parseInt(line);
				}
				if (c == 6)
				{
					f[2] = parseInt(line);
					if (fmod == 3)
					{
						points.push([v[l[f[0]][0]][0], v[l[f[0]][0]][1], v[l[f[0]][0]][2]]);
						points.push([v[l[f[1]][0]][0], v[l[f[1]][0]][1], v[l[f[1]][0]][2]]);
						points.push([v[l[f[2]][0]][0], v[l[f[2]][0]][1], v[l[f[2]][0]][2]]);
						faces.push([fnam,	fnam + 2, fnam + 1]);
						fnam += 3;
						c = 0;	
					}
				}
				if (c == 7)
				{
					f[3] = parseInt(line);
					if (fmod == 4)
					{
						points.push([v[l[f[0]][0]][0], v[l[f[0]][0]][1], v[l[f[0]][0]][2]]);
						points.push([v[l[f[1]][0]][0], v[l[f[1]][0]][1], v[l[f[1]][0]][2]]);
						points.push([v[l[f[2]][0]][0], v[l[f[2]][0]][1], v[l[f[2]][0]][2]]);
						points.push([v[l[f[3]][0]][0], v[l[f[3]][0]][1], v[l[f[3]][0]][2]]);
						faces.push([fnam, fnam + 2, fnam + 1]);
						faces.push([fnam + 2,	fnam,	fnam + 3]);
						fnam += 4;
						c = 0;
					}
				}
			}
		}
	}
	return CSG.polyhedron({
		points: points,
		faces: faces
	});
}

```

# 成果物

https://embed.plnkr.co/plunk/QW5lczreTCjy9sHa


以上。