]>
git.scottworley.com Git - nt3d/blob - nt3d.js
878ee43b34d9b66f32ddc13b14b682f6255498a9
1 /* nt3d - Javascript library for doing some 3D stuff.
2 * Copyright (C) 2012 Scott Worley <ScottWorley@ScottWorley.com>
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.
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.
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/>.
19 triangle: function(a
, b
, c
) {
22 quad: function(a
, b
, c
, d
) {
23 return this.triangle(a
, b
, c
).concat(
24 this.triangle(c
, d
, a
));
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]];
36 normal: function(a
, b
, c
) {
37 return this.cross(this.sub(a
, b
), this.sub(b
, c
));
40 // Get params from form
42 for (var i
= 0; i
< this.user_params
.length
; i
++) {
43 params
[i
] = this.form
.elements
["param"+i
].value
;
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!");
51 var n
= this.points
.length
/ 3;
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" +
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"+
68 this.stl
+= "endsolid " + this.user_function
.name
+ "\n";
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
);
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("download", this.user_function
.name
+ ".stl");
81 download_link
.setAttribute("href", "data:application/sla," + encodeURIComponent(this.stl
));
82 this.ui
.appendChild(download_link
);
84 framework: function (f
, params
) {
85 this.user_function
= f
;
86 this.user_params
= params
;
89 this.ui
= document
.getElementById("nt3dui");
91 this.ui
= document
.createElement("div");
92 this.ui
.setAttribute("id", "nt3dui");
93 document
.body
.appendChild(this.ui
);
95 this.form
= document
.createElement("form");
96 this.ui
.appendChild(this.form
);
97 var table
= document
.createElement("table");
98 this.form
.appendChild(table
);
99 var tr
= document
.createElement("tr");
100 table
.appendChild(tr
);
101 var th
= document
.createElement("th");
102 th
.appendChild(document
.createTextNode("Variable"));
104 th
= document
.createElement("th");
105 th
.appendChild(document
.createTextNode("Value"));
107 for (var i
= 0; i
< params
.length
; i
++) {
108 tr
= document
.createElement("tr");
109 table
.appendChild(tr
);
110 var td
= document
.createElement("td");
111 td
.appendChild(document
.createTextNode(params
[i
][0]));
113 td
= document
.createElement("td");
114 var input
= document
.createElement("input");
115 input
.setAttribute("name", "param" + i
);
116 input
.setAttribute("value", params
[i
][1]);
117 td
.appendChild(input
);
120 var go
= document
.createElement("input");
121 go
.setAttribute("type", "button");
122 go
.setAttribute("value", "Go!");
123 go
.setAttribute("onclick", "nt3d.go()");
124 this.form
.appendChild(go
);