]> git.scottworley.com Git - nt3d/blame - nt3d.js
Pass numeric parameters as numbers
[nt3d] / nt3d.js
CommitLineData
d82d3571
SW
1/* nt3d - Javascript library for doing some 3D stuff.
2 * Copyright (C) 2012 Scott Worley <ScottWorley@ScottWorley.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18nt3d = {
19 triangle: function(a, b, c) {
20 return [a, b, c];
21 },
22 quad: function(a, b, c, d) {
23 return this.triangle(a, b, c).concat(
24 this.triangle(c, d, a));
25 },
0ab5ca18
SW
26 trianglefan: function(fan) {
27 var result = [];
28 for (var i = 2; i < fan.length; i++) {
29 result.push(fan[0], fan[i-1], fan[i]);
30 }
31 return result;
32 },
a9f371ff 33 closed_trianglefan: function(fan) {
70650839 34 return this.trianglefan(fan.concat([fan[1]]));
a9f371ff 35 },
4e2bdb62
SW
36 quadstrip: function(strip) {
37 if (strip.length % 2 != 0) {
38 alert("quadstrip length not divisble by 2!");
39 }
40 var result = [];
41 for (var i = 2; i < strip.length; i += 2) {
70650839 42 result = result.concat(this.quad(strip[i-2], strip[i-1], strip[i+1], strip[i]));
4e2bdb62
SW
43 }
44 return result;
45 },
46 closed_quadstrip: function(strip) {
70650839 47 return this.quadstrip(strip.concat([strip[0], strip[1]]));
4e2bdb62 48 },
94faa4e5
SW
49 extrude: function(shape, path, shapenormals, pathnormals) {
50 var guts_result = nt3d._extrude_guts(shape, path, shapenormals, pathnormals);
51 // Add the end-caps
52 // XXX: This doesn't work if shape is not convex
53 return guts_result.points.concat(
54 nt3d.trianglefan(guts_result.first_loop.reverse()),
55 nt3d.trianglefan(guts_result.last_loop));
56
57 },
58 closed_extrude: function(shape, path, shapenormals, pathnormals) {
59 var guts_result = nt3d._extrude_guts(shape, path, shapenormals, pathnormals);
60 // Stitch the ends together
61 return guts_result.points.concat(
62 nt3d.closed_quadstrip(nt3d.zip(guts_result.first_loop, guts_result.last_loop)));
63 },
64 _fix_pathnormals: function(shapenormals, pathnormals) {
65 // Fix pathnormals[i] to be perfectly perpendicular to
66 // shapenormals[i]. This lets extrude callers be sloppy
67 // with pathnormals, which can greatly simplify things.
68 var fixedpathnormals = [];
69 for (var i = 0; i < pathnormals.length; i++) {
70 var proj = this.project(shapenormals[i], pathnormals[i]);
71 fixedpathnormals[i] = this.sub(pathnormals[i], proj);
72 }
73 return fixedpathnormals;
74 },
75 _extrude_guts: function(shape, path, shapenormals, pathnormals) {
76 var fixedpathnormals = this._fix_pathnormals(shapenormals, pathnormals);
77 var result = { points: [] };
78 var prev_loop;
79 for (var i = 0; i < path.length; i++) {
80 // loop is shape in 3d with (0,0) at path[i], shape's
81 // z axis in the direction of shapenormals[i], and
82 // shape's x axis in the direction of pathnormals[i].
2f6b97f8
SW
83 var loop = shape;
84
94faa4e5
SW
85 // This is done in three steps:
86 // 1. Rotate shape out of the xy plane so that [0,0,1]
87 // becomes shapenormals[i] by crossing [0,0,1] and
88 // shapenormals[i] to get a rotation axis and taking
89 // their dot product to get a rotation angle. This
90 // puts the shape in the correct plane, but does not
91 // constrain its rotation about shapenormals[i].
92 var rot1axis = this.unit(this.cross([0,0,1], shapenormals[i]));
93 var rot1angle = this.angle_between([0,0,1], this.unit(shapenormals[i]));
2f6b97f8 94 if (rot1angle > 1e-7) {
1e62819c 95 loop = this.rotate_about_origin(loop, rot1axis, rot1angle);
2f6b97f8
SW
96 }
97
94faa4e5
SW
98 // 2. Rotate around shapenormals[i] so that [1,0,0]
99 // becomes fixedpathnormals[i].
100 var rot2axis = this.unit(shapenormals[i]);
101 var rot2angle = this.angle_between([1,0,0], this.unit(fixedpathnormals[i]));
2f6b97f8 102 if (rot2angle > 1e-7) {
1e62819c 103 loop = this.rotate_about_origin(loop, rot2axis, rot2angle);
2f6b97f8 104 }
94faa4e5
SW
105 // This would probably be faster and more numerically stable
106 // if the two rotations were applied as one combined operation
107 // rather than separate steps.
2f6b97f8
SW
108
109 // 3. Translate to path[i].
110 loop = this.translate(loop, path[i]);
111
94faa4e5
SW
112 if (i == 0) {
113 result.first_loop = loop;
114 } else {
115 result.points = result.points.concat(nt3d.closed_quadstrip(nt3d.zip(loop, prev_loop)));
116 }
117 prev_loop = loop;
118 }
119 result.last_loop = prev_loop;
120 return result;
121 },
122 zip: function(a, b) {
123 var result = [];
124 if (a.length != b.length) {
125 alert("Zip over different-sized inputs");
126 }
127 for (var i = 0; i < a.length; i++) {
128 result.push(a[i], b[i]);
129 }
130 return result;
131 },
132 magnitude: function(a) {
133 return Math.sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
134 },
135 unit: function(a) {
136 return this.scale(a, 1 / this.magnitude(a));
137 },
d82d3571
SW
138 sub: function(a, b) {
139 return [a[0] - b[0],
140 a[1] - b[1],
141 a[2] - b[2]];
142 },
1e62819c
SW
143 neg: function(a) {
144 return [-a[0], -a[1], -a[2]];
145 },
94faa4e5
SW
146 dot: function(a, b) {
147 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
148 },
149 scale: function(v, s) { // Scale vector v by scalar s
150 return [s*v[0], s*v[1], s*v[2]];
151 },
d82d3571
SW
152 cross: function(a, b) {
153 return [a[1]*b[2] - a[2]*b[1],
154 a[2]*b[0] - a[0]*b[2],
155 a[0]*b[1] - a[1]*b[0]];
156 },
157 normal: function(a, b, c) {
158 return this.cross(this.sub(a, b), this.sub(b, c));
159 },
94faa4e5
SW
160 project: function(a, b) { // Project b onto a
161 var a_magnitude = this.magnitude(a);
162 return this.scale(a, this.dot(a, b) / a_magnitude * a_magnitude);
163 },
2f6b97f8
SW
164 translate: function(points, offset) {
165 var translated = [];
166 for (var i = 0; i < points.length; i++) {
167 translated[i] = [points[i][0] + offset[0],
168 points[i][1] + offset[1],
169 points[i][2] + offset[2]];
170 }
171 return translated;
94faa4e5
SW
172 },
173 angle_between: function(a, b) { // a and b must be unit vectors
174 return Math.acos(this.dot(a, b));
175 },
1e62819c 176 rotate_about_origin: function(points, axis, angle) { // axis must be a unit vector
94faa4e5
SW
177 // From http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/
178 var cosangle = Math.cos(angle);
179 var sinangle = Math.sin(angle);
2f6b97f8
SW
180 var rotated = [];
181 for (var i = 0; i < points.length; i++) {
182 var p = points[i];
183 var tmp = this.dot(p, axis) * (1 - cosangle);
184 rotated[i] = [
185 axis[0]*tmp + p[0]*cosangle + (-axis[2]*p[1] + axis[1]*p[2])*sinangle,
186 axis[1]*tmp + p[1]*cosangle + ( axis[2]*p[0] - axis[0]*p[2])*sinangle,
187 axis[2]*tmp + p[2]*cosangle + (-axis[1]*p[0] + axis[0]*p[1])*sinangle];
188 }
189 return rotated;
94faa4e5 190 },
1e62819c
SW
191 rotate: function(points, center, axis, angle) { // axis must be a unit vector
192 return this.translate(
193 this.rotate_about_origin(
194 this.translate(points, this.neg(center)),
195 axis,
196 angle),
197 center);
198 },
d82d3571
SW
199 go: function() {
200 // Get params from form
201 var params = [];
202 for (var i = 0; i < this.user_params.length; i++) {
ec5f883d
SW
203 var as_string = this.form.elements["param"+i].value;
204 var as_num = +as_string;
205 params[i] = isNaN(as_num) ? as_string : as_num;
d82d3571
SW
206 }
207
208 // Run user_function
209 this.points = this.user_function.apply(null, params);
210 if (this.points.length % 3 != 0) {
211 alert("Points list length not divisble by 3!");
212 }
213 var n = this.points.length / 3;
214
215 // Make STL
216 this.stl = "solid " + this.user_function.name + "\n";
217 for (var i = 0; i < n; i++) {
218 var a = this.points[i*3+0];
219 var b = this.points[i*3+1];
220 var c = this.points[i*3+2];
221 var normal = this.normal(a, b, c);
222 this.stl += "facet normal " + normal[0] + " " + normal[1] + " " + normal[2] + "\n" +
223 "outer loop\n" +
224 "vertex " + a[0] + " " + a[1] + " " + a[2] + "\n"+
225 "vertex " + b[0] + " " + b[1] + " " + b[2] + "\n"+
226 "vertex " + c[0] + " " + c[1] + " " + c[2] + "\n"+
227 "endloop\n" +
228 "endfacet\n";
229 }
230 this.stl += "endsolid " + this.user_function.name + "\n";
231
232 // Remove any previous download links
233 var old_download_link = document.getElementById("nt3d_download");
234 if (old_download_link) {
235 old_download_link.parentNode.removeChild(old_download_link);
236 }
237
238 // Offer result as download
239 var download_link = document.createElement("a");
240 download_link.appendChild(document.createTextNode("Download!"));
241 download_link.setAttribute("id", "nt3d_download");
4eea5cbf 242 download_link.setAttribute("style", "background-color: blue");
d82d3571
SW
243 download_link.setAttribute("download", this.user_function.name + ".stl");
244 download_link.setAttribute("href", "data:application/sla," + encodeURIComponent(this.stl));
245 this.ui.appendChild(download_link);
4eea5cbf 246 setTimeout(function() { download_link.setAttribute("style", "-webkit-transition: background-color 0.4s; -moz-transition: background-color 0.4s; -o-transition: background-color 0.4s; -ms-transition: background-color 0.4s; transition: background-color 0.4s; background-color: inherit"); }, 0);
d82d3571
SW
247 },
248 framework: function (f, params) {
249 this.user_function = f;
250 this.user_params = params;
251
252 // Make the UI
253 this.ui = document.getElementById("nt3dui");
254 if (!this.ui) {
255 this.ui = document.createElement("div");
256 this.ui.setAttribute("id", "nt3dui");
257 document.body.appendChild(this.ui);
258 }
259 this.form = document.createElement("form");
a26d0960 260 this.form.setAttribute("onsubmit", "nt3d.go(); return false");
d82d3571
SW
261 this.ui.appendChild(this.form);
262 var table = document.createElement("table");
263 this.form.appendChild(table);
264 var tr = document.createElement("tr");
265 table.appendChild(tr);
266 var th = document.createElement("th");
267 th.appendChild(document.createTextNode("Variable"));
268 tr.appendChild(th);
269 th = document.createElement("th");
270 th.appendChild(document.createTextNode("Value"));
271 tr.appendChild(th);
272 for (var i = 0; i < params.length; i++) {
273 tr = document.createElement("tr");
274 table.appendChild(tr);
275 var td = document.createElement("td");
276 td.appendChild(document.createTextNode(params[i][0]));
277 tr.appendChild(td);
278 td = document.createElement("td");
279 var input = document.createElement("input");
280 input.setAttribute("name", "param" + i);
281 input.setAttribute("value", params[i][1]);
282 td.appendChild(input);
283 tr.appendChild(td);
284 }
285 var go = document.createElement("input");
286 go.setAttribute("type", "button");
287 go.setAttribute("value", "Go!");
288 go.setAttribute("onclick", "nt3d.go()");
289 this.form.appendChild(go);
290 }
291};