]>
git.scottworley.com Git - nt3d/blob - nt3d.js
fbd8f50aa34256add50d21c586c0e83a8bd56fea
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
));
26 trianglefan: function(fan
) {
28 for (var i
= 2; i
< fan
.length
; i
++) {
29 result
.push(fan
[0], fan
[i
-1], fan
[i
]);
33 closed_trianglefan: function(fan
) {
34 return this.trianglefan(fan
.concat([fan
[1]]));
36 quadstrip: function(strip
) {
37 if (strip
.length
% 2 != 0) {
38 alert("quadstrip length not divisble by 2!");
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
]));
46 closed_quadstrip: function(strip
) {
47 return this.quadstrip(strip
.concat([strip
[0], strip
[1]]));
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]];
59 normal: function(a
, b
, c
) {
60 return this.cross(this.sub(a
, b
), this.sub(b
, c
));
63 // Get params from form
65 for (var i
= 0; i
< this.user_params
.length
; i
++) {
66 params
[i
] = this.form
.elements
["param"+i
].value
;
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!");
74 var n
= this.points
.length
/ 3;
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" +
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"+
91 this.stl
+= "endsolid " + this.user_function
.name
+ "\n";
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
);
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);
109 framework: function (f
, params
) {
110 this.user_function
= f
;
111 this.user_params
= params
;
114 this.ui
= document
.getElementById("nt3dui");
116 this.ui
= document
.createElement("div");
117 this.ui
.setAttribute("id", "nt3dui");
118 document
.body
.appendChild(this.ui
);
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"));
130 th
= document
.createElement("th");
131 th
.appendChild(document
.createTextNode("Value"));
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]));
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
);
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
);