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