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