-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
225 lines (212 loc) · 7.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const SVG_NS = "http://www.w3.org/2000/svg";
var svg = document.createElementNS(SVG_NS,'svg');
svg.setAttribute('xmlns',SVG_NS)
svg.setAttribute('id','svg')
// svg.style.border = "1px solid red"
document.body.appendChild(svg)
const im1 = new Image();
im1.src="docs/archimedean-solid-radii.png"
document.body.appendChild(im1)
const im2 = new Image();
im2.src="docs/platonic-solid-radii.png"
document.body.appendChild(im2)
const margin=10;
const mmppx=3.7796;
// const MMPPX=3.7796;
var faceRadius = -1;
var gr;
// svg.setAttribute('width','900')
// svg.setAttribute('height','600')
var r;
var cloneTx = -1;
var cloneTy = -1;
////////////////////////
const gui = new dat.GUI()
const fileFolder = gui.addFolder('File');
const viewFolder = gui.addFolder('View');
const polyhedraFolder = gui.addFolder('Polyhedra');
const ArchimedeanFolder = polyhedraFolder.addFolder('Archimedean');
const PlatonicFolder = polyhedraFolder.addFolder('Platonic');
polyhedraFolder.open()
gui.width = 435;
if(ISNW){
fileFolder.add(this,'exportSVG');
fileFolder.add(location,'reload');
const folderDocs = gui.addFolder('docs');
folderDocs.add({f(){shell.openItem(path.join(CWD,'./docs/dihedralAngle-hinge-formulae.svg'))}},'f').name('dihedralAngle-hinge-formulae.svg')
folderDocs.add({f(){shell.openItem(path.join(CWD,'./docs/notch-distance.svg'))}},'f').name('notch-distance.svg')
folderDocs.add({f(){shell.openItem(path.join(CWD,'./docs/side.svg'))}},'f').name('side.svg')
}
viewFolder.add(this,'setNaturalSize').name("1:1");
viewFolder.add(this,'onResize').name("fit to view");
var isResizing = true;
var isMarkingHinges;
isMarkingHinges = true;
isMarkingHinges = false;
viewFolder.add(this,'isResizing').name('is fitting').onChange((value)=>{
console.log('value: ', value);
if(!value){
setNaturalSize()
}else{
onResize()
}
})
const valueController = viewFolder.add({f:400},'f',300,1000,1).onChange((value)=>{
svg.setAttribute("height", `${value}`)
})
// gui.add(this,'isMarkingHinges')
let Polyhedron;
Polyhedron = Archimedean.TruncatedTetrahedron;
Polyhedron = Archimedean.TruncatedCube;
Polyhedron = Archimedean.TruncatedOctahedron;
Polyhedron = Archimedean.Rhombicuboctahedron;
Polyhedron = Archimedean.TruncatedCuboctahedron;
Polyhedron = Archimedean.Cuboctahedron;
Polyhedron = Platonic.Dodecahedron;
Polyhedron = Platonic.Icosahedron;
const sideLength = 60;//mm
////////////////////////////////////////////////////////////
let hingedPolyhedron = new HingedPolyhedron({svg, gui, sideLength, Polyhedron,isMarkingHinges});
hingedPolyhedron.addEventListener('update',onResize);
/////////////////////////////////////////
const testsFolder = gui.addFolder("pseudo-tests");
// testsFolder.open();
testsFolder.add({f:function () {
location.href = "pseudo-tests/hinge-test.html"
}},"f").name("hinge-test.html")
testsFolder.add({f:function () {
location.href = "pseudo-tests/hingesManager-test.html"
}},"f").name("hingesManager-test.html")
testsFolder.add({f:function () {
location.href = "pseudo-tests/notched-face-test.html"
}},"f").name("notched-face-test.html")
testsFolder.add({f:function () {
location.href = "pseudo-tests/popup-dodecahedron.html"
}},"f").name("popup dodecahedron")
/////////////////////////////////////////
function doUpdate(params) {
console.trace()
}
function to_mm(value) {
return mmppx*value
return 3.7795*value
}
function to_px(value) {
return value/mmppx
return 3.7795*value
}
function onResize(params) {
if(!isResizing)return
const proportions = innerWidth / innerHeight;
const isWider = proportions>1;
// const bbox = svg.getBBox();
// const tw = bbox.width + Math.abs(bbox.x);
// const th = bbox.height + Math.abs(bbox.y);
const {tw, th} = getSVGsize();
const viewBoxString = `0 0 ${tw} ${th}`;
svg.setAttribute("viewBox",viewBoxString)
svg.removeAttribute("height")
const h = isWider? innerHeight:innerWidth;
// console.log('h: ', h);
svg.setAttribute("height", `${h}`)
valueController.setValue(h)
}
function setNaturalSize() {
const proportions = innerWidth / innerHeight;
const isWider = proportions>1;
// const bbox = svg.getBBox();
// const tw = bbox.width + Math.abs(bbox.x);
// const th = bbox.height + Math.abs(bbox.y);
const viewBoxString = `0 0 ${innerWidth} ${innerHeight}`;
svg.setAttribute("viewBox",viewBoxString)
svg.removeAttribute("height")
const h = isWider? innerHeight:innerWidth;
svg.setAttribute("height", `${h}`)
}
window.addEventListener('resize',onResize)
onResize()
// isResizing = false;
// setNaturalSize();
function getSVGsize(params) {
const bbox = svg.getBBox();
const tw = bbox.width + Math.abs(bbox.x);
const th = bbox.height + Math.abs(bbox.y);
return {tw,th}
}
function setBin() {
const bin = document.createElementNS(SVG_NS, 'rect');
let {tw, th} = getSVGsize();
// console.log('th: ', th);
// console.log('tw: ', tw);
const margin = 3;
th = to_mm(250-margin);
tw = to_mm(350-margin);
bin.setAttribute('id','bin')
bin.setAttribute('width',tw)
bin.setAttribute('height',th)
bin.setAttribute('fill','none')
bin.setAttribute('stroke','black')
svg.appendChild(bin)
}
function setSvgNestClient(){
var bins = document.getElementById('bins');
const nester = new SvgNestClient(gui,bins);
nester.addEventListener('nested',onNested)
// nester.start()
}
setBin()
setSvgNestClient()
function onNested(params) {
// console.log('params: ', params);
// const files = pre_exportSVG();
// console.log('files: ', files);
// exportSVG()
//["dihedral0.svg", "dihedral1.svg"]
}
//https://www.rapidtables.com/convert/number/degrees-minutes-seconds-to-degrees.html
for (const polyhedronName in Archimedean) {
if (Archimedean.hasOwnProperty(polyhedronName)) {
const Polyhedron = Archimedean[polyhedronName];
ArchimedeanFolder.add({f:()=>{
pickPolyhedron(Polyhedron);
}},'f').name(polyhedronName)
}
}
for (const polyhedronName in Platonic) {
if (Platonic.hasOwnProperty(polyhedronName)) {
const Polyhedron = Platonic[polyhedronName];
PlatonicFolder.add({f:()=>{
pickPolyhedron(Polyhedron);
}},'f').name(polyhedronName)
}
}
function pickPolyhedron(Polyhedron) {
svg.innerHTML = '';
setBin();
gui.removeFolder(hingedPolyhedron.guiFolder);
hingedPolyhedron.removeEventListener('update', onResize);
hingedPolyhedron = new HingedPolyhedron({ svg, gui, sideLength, Polyhedron ,isMarkingHinges});
hingedPolyhedron.addEventListener('update', onResize);
onResize();
}
const polyhedraNames = Object.keys(Archimedean);
var polyhedronIndex = 1;
setInterval(()=>{
return
const polyhedronName = polyhedraNames[polyhedronIndex];
console.log('polyhedronName: ', polyhedronName);
pickPolyhedron(Archimedean[polyhedronName])
polyhedronIndex++
if(polyhedronIndex>=polyhedraNames.length)
polyhedronIndex=0;
},2000)
function closeAllGuiFolders() {
const folders = gui.__folders;
for (const folderKey in folders) {
if (folders.hasOwnProperty(folderKey)) {
const folder = folders[folderKey];
folder.close()
}
}
}
closeAllGuiFolders()