]> git.scottworley.com Git - nt3d/blob - nt3d.js
fbd8f50aa34256add50d21c586c0e83a8bd56fea
[nt3d] / nt3d.js
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
18 nt3d = {
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 },
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 },
33 closed_trianglefan: function(fan) {
34 return this.trianglefan(fan.concat([fan[1]]));
35 },
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) {
42 result = result.concat(this.quad(strip[i-2], strip[i-1], strip[i+1], strip[i]));
43 }
44 return result;
45 },
46 closed_quadstrip: function(strip) {
47 return this.quadstrip(strip.concat([strip[0], strip[1]]));
48 },
49 sub: function(a, b) {
50 return [a[0] - b[0],
51 a[1] - b[1],
52 a[2] - b[2]];
53 },
54 cross: function(a, b) {
55 return [a[1]*b[2] - a[2]*b[1],
56 a[2]*b[0] - a[0]*b[2],
57 a[0]*b[1] - a[1]*b[0]];
58 },
59 normal: function(a, b, c) {
60 return this.cross(this.sub(a, b), this.sub(b, c));
61 },
62 go: function() {
63 // Get params from form
64 var params = [];
65 for (var i = 0; i < this.user_params.length; i++) {
66 params[i] = this.form.elements["param"+i].value;
67 }
68
69 // Run user_function
70 this.points = this.user_function.apply(null, params);
71 if (this.points.length % 3 != 0) {
72 alert("Points list length not divisble by 3!");
73 }
74 var n = this.points.length / 3;
75
76 // Make STL
77 this.stl = "solid " + this.user_function.name + "\n";
78 for (var i = 0; i < n; i++) {
79 var a = this.points[i*3+0];
80 var b = this.points[i*3+1];
81 var c = this.points[i*3+2];
82 var normal = this.normal(a, b, c);
83 this.stl += "facet normal " + normal[0] + " " + normal[1] + " " + normal[2] + "\n" +
84 "outer loop\n" +
85 "vertex " + a[0] + " " + a[1] + " " + a[2] + "\n"+
86 "vertex " + b[0] + " " + b[1] + " " + b[2] + "\n"+
87 "vertex " + c[0] + " " + c[1] + " " + c[2] + "\n"+
88 "endloop\n" +
89 "endfacet\n";
90 }
91 this.stl += "endsolid " + this.user_function.name + "\n";
92
93 // Remove any previous download links
94 var old_download_link = document.getElementById("nt3d_download");
95 if (old_download_link) {
96 old_download_link.parentNode.removeChild(old_download_link);
97 }
98
99 // Offer result as download
100 var download_link = document.createElement("a");
101 download_link.appendChild(document.createTextNode("Download!"));
102 download_link.setAttribute("id", "nt3d_download");
103 download_link.setAttribute("style", "background-color: blue");
104 download_link.setAttribute("download", this.user_function.name + ".stl");
105 download_link.setAttribute("href", "data:application/sla," + encodeURIComponent(this.stl));
106 this.ui.appendChild(download_link);
107 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);
108 },
109 framework: function (f, params) {
110 this.user_function = f;
111 this.user_params = params;
112
113 // Make the UI
114 this.ui = document.getElementById("nt3dui");
115 if (!this.ui) {
116 this.ui = document.createElement("div");
117 this.ui.setAttribute("id", "nt3dui");
118 document.body.appendChild(this.ui);
119 }
120 this.form = document.createElement("form");
121 this.form.setAttribute("onsubmit", "nt3d.go(); return false");
122 this.ui.appendChild(this.form);
123 var table = document.createElement("table");
124 this.form.appendChild(table);
125 var tr = document.createElement("tr");
126 table.appendChild(tr);
127 var th = document.createElement("th");
128 th.appendChild(document.createTextNode("Variable"));
129 tr.appendChild(th);
130 th = document.createElement("th");
131 th.appendChild(document.createTextNode("Value"));
132 tr.appendChild(th);
133 for (var i = 0; i < params.length; i++) {
134 tr = document.createElement("tr");
135 table.appendChild(tr);
136 var td = document.createElement("td");
137 td.appendChild(document.createTextNode(params[i][0]));
138 tr.appendChild(td);
139 td = document.createElement("td");
140 var input = document.createElement("input");
141 input.setAttribute("name", "param" + i);
142 input.setAttribute("value", params[i][1]);
143 td.appendChild(input);
144 tr.appendChild(td);
145 }
146 var go = document.createElement("input");
147 go.setAttribute("type", "button");
148 go.setAttribute("value", "Go!");
149 go.setAttribute("onclick", "nt3d.go()");
150 this.form.appendChild(go);
151 }
152 };