-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWireframeGenerator.js
More file actions
438 lines (329 loc) · 10.8 KB
/
WireframeGenerator.js
File metadata and controls
438 lines (329 loc) · 10.8 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* @author wizard23 / http://www.wizards23.net
*/
///
function CreatePolyOutlineSCAD(geometry)
{
var eps = 0.001;
// geometry.faces
// geometry.vertices
// unifiedVertices = {0:0, 1:0, 2:2, 3:0, ...}
// array that maps the vertex-index given to the lowest vertex-index that is "equivalent" to the given vertex
// if vertices are connected by a face then they should not be unified write assert for that, also eps needs to be
// added
s = "";
// Find coinciding vertices, quadratic runtime
var vertices = geometry.vertices;
var vMatches = [];
for (var i = 0; i < vertices.length; i++)
{
vMatches[i] = i;
for (var j = 0; j < i; j++)
{
var delta = new THREE.Vector3();
delta.subVectors(vertices[i], vertices[j]);
var deltaD = delta.length();
// unify if close enough
if (deltaD < eps)
{
vMatches[i] = vMatches[j];
break;
}
}
}
// TODO: I think the above algorithm does the trick but we could implement assert that guarantees that vMatches define an equivalence relation
// specifically that: A ~ B and B ~ C implies A ~ C
var cleanedVertices = [];
var vRenumbered = [];
var vRenumberedReverse = [];
var nextVIndex = 0;
for (var i = 0; i < vertices.length; i++)
{
if (vMatches[i] == i)
{
vRenumbered[i] = nextVIndex;
vRenumberedReverse[nextVIndex] = i;
cleanedVertices[nextVIndex] = vertices[i];
nextVIndex++;
}
else
{
vRenumbered[i] = vRenumbered[vMatches[i]];
}
}
var nrCleanedVertices = nextVIndex;
var faces = geometry.faces;
var cleanedFaces = [];
var v2fTable = [];
for (var i = 0; i < nrCleanedVertices; i++)
{
v2fTable[i] = [];
}
for (var i = 0; i < faces.length; i++) {
// TODO: is this really a triangle???
var f = faces[i];
var ia = vRenumbered[vMatches[f.a]];
var ib = vRenumbered[vMatches[f.b]];
var ic = vRenumbered[vMatches[f.c]];
var fn = [ia, ib, ic];
cleanedFaces.push(fn);
v2fTable[ia].push(reorderFace(fn, 0));
v2fTable[ib].push(reorderFace(fn, 1));
v2fTable[ic].push(reorderFace(fn, 2));
}
// get an order for the faces aroud each vertex
// quadratic but who cares
var v2Oriented = [];
for (var i = 0; i < v2fTable.length; i++) {
v2Oriented[i] = [];
var fList = v2fTable[i];
first = fList[0];
startB = first[1];
currentB = startB;
var iterations = 0;
var maxIterations = 1000;
do
{
v2Oriented[i].push(currentB);
for (var j = 0; j < fList.length; j++)
{
if (fList[j][1] == currentB)
{
currentB = fList[j][2];
break;
}
}
iterations++;
} while (currentB != startB && iterations < maxIterations)
if (iterations == maxIterations) alert("max iterations reached");
}
/*
for (var i = 0; i < v2fTable.length; i++) {
var fList = v2fTable[i];
for (var fi = 0; fi < fList.length; fi++) {
var fn = fList[fi];
s += "f:[" + fn[0] + "|" + fn[1] + "|" + fn[2] + "]"
//s += "f:[" + f.a + "|" + f.b + "|" + f.c + "]"
//s += "f:[" + dV3(f.a) + "|" + dV3(f.b) + "|" + dV3(f.c) + "]"
}
break;
}*/
var sR = 1.5;
var wall = 1.6;
var noCutL=4;
var extraCutDepth=1; // to give slack
s+="use <PolyhedronOutlinerLib.scad>"
s+="forPrint=0; generateConnectors = 0; generateSticks=1;";
s+="sR=" + sR + "; sL = 6; cR=6; cL=5;\n";
s+="*edge0_1(1); *vertex0(1);";
s+="if (!forPrint) %mainShape();\n";
// generate connectorz
var vertexBaseFn = "";
var vertexFn = "";
var sticksFn = "";
var vPlatePosX = 0;
var vetrexPlate = "if (generateConnectors && forPrint) union() {";
var sPlatePosX = 0;
var sticksPlate = "if (generateSticks && forPrint) union() {";
var vertexAssembly = "if (generateConnectors && !forPrint) union() {";
var sticksAssembly = "if (generateSticks && !forPrint) union() {";
for (var i = 0; i < v2fTable.length; i++)
{
var fList = v2fTable[i];
var vList = v2Oriented[i];
//if (i != 4 && i != 3) continue;
if (fList.length == 0) alert("this should not happen");
var nSum = new THREE.Vector3();
var vA = cleanedVertices[i];
var cutSticks = "";
//alert(vList.length);
for (var vi = 0; vi < vList.length; vi++)
{
var bIdx = vList[vi];
var vP = cleanedVertices[vList[(vi+vList.length-1)%vList.length]];
var vB = cleanedVertices[bIdx];
var vC = cleanedVertices[vList[(vi+1)%vList.length]];
var pDir = new THREE.Vector3();
pDir.subVectors(vP, vA).normalize();
var bDir = new THREE.Vector3();
bDir.subVectors(vB, vA).normalize();
var cDir = new THREE.Vector3();
cDir.subVectors(vC, vA).normalize();
var pNormal = new THREE.Vector3();
pNormal.crossVectors( pDir, bDir );
var normal = new THREE.Vector3();
normal.crossVectors( bDir, cDir );
nSum.add(normal);
//var wall = 1;
//var noCutL=5;
//var extraCutDepth=1; // to give slack
var invRot = generateStickSCAD(vA, vP, vB, vC, 0, 0, 0, 0, true);
var mainStick = generateStickSCAD(vA, vP, vB, vC, -10, -10, 1, 0);
//var mainCutStick = generateStickSCAD(vA, vP, vB, vC, noCutL+smallCutL, noCutL+smallCutL, 1, 0);
var smallStick = generateStickSCAD(vA, vP, vB, vC, noCutL, noCutL, -wall, 0);
var cutStick = generateStickSCAD(vA, vP, vB, vC, noCutL-extraCutDepth, noCutL-extraCutDepth, -wall, 0.42);
cutStick = generateStickSCAD(vA, vP, vB, vC, -10, -10, 1, 0);
// this gets cut away from connector
cutSticks += cutStick;
if (i < bIdx)
{
var realE = "";
/*
realE += "intersection() { mainShape(); ";
realE += "union(){difference(){"+mainStick+"vertexBase"+i+"();vertexBase"+vList[vi]+"();}"+ smallStick + "}\n";
realE += "}"; // intersection end
*/
realE += "intersection() { mainShape(); ";
realE += "union(){"+mainStick+"}\n";
realE += "}"; // intersection end
sticksFn += "module edge" + i + "_" + bIdx + "(forPrint){"; // module start
sticksFn += "if (forPrint) " + invRot + "{" + realE + "}";
sticksFn += "if (!forPrint) {" + realE + "}";
sticksFn += "}"; // module end
sticksAssembly += "edge" + i + "_" + bIdx + "(0);";
sticksPlate += "translate([" + (sPlatePosX) + ",0,0]) edge" + i + "_" + bIdx + "(1);"
sPlatePosX += 3.2;
}
}
// true normal
var nSumVA = new THREE.Vector3();
nSumVA.addVectors(nSum, vA);
vertexBaseFn += "module vertexBase" + i + "() { intersection() { mainShape(); ";
vertexBaseFn += SphereSCAD("cR", vA, 24);
vertexBaseFn += LineSCAD(vA, nSumVA, "5*cR", "cL", "3");
vertexBaseFn += "}}";
var vRot = LineRotations(nSum);
vertexFn += "module vertex" + i + "(forPrint){if(forPrint) translate([0,0,cL]) rotate([" + (-vRot.x* 180/Math.PI) + ",0,0]) " +
"rotate([0,0,"+ (-vRot.z* 180/Math.PI) + "]) translate(-" + pV3(vA) + ") difference(){vertexBase" + i + "(); ";
vertexFn += cutSticks;
vertexFn += "}";
vertexFn += "if(!forPrint)difference(){vertexBase" + i + "(); ";
vertexFn += cutSticks;
vertexFn += "}";
vertexFn += "}";
vertexAssembly += "vertex" + i + "(0);";
vetrexPlate += "translate([" + (vPlatePosX) + ",0,0]) vertex" + i + "(1);";
vPlatePosX += 15;
//if (i > 1) break;
}
vertexAssembly += "}";
sticksAssembly += "}";
vetrexPlate += "}";
sticksPlate += "}";
// generate original poly in scad
var points = "";
var triangles = "";
for (var i = 0; i < cleanedFaces.length; i++) {
var fn = cleanedFaces[i];
if (i != 0) triangles += ", ";
triangles += "[" + fn[0] + "," + fn[1] + "," + fn[2] + "]"
}
for (var i = 0; i < cleanedVertices.length; i++) {
var vertex = cleanedVertices[i];
if (i != 0) points += ", ";
points += pV3(vertex);
}
s += vertexAssembly;
s += sticksAssembly;
s += vetrexPlate;
s += "translate([0,-18,0])" + sticksPlate;
s += vertexBaseFn;
s += sticksFn;
s += vertexFn;
s += "module mainShape() {polyhedron(points = [" + points + "], triangles = [" + triangles + "], convexity = 10);}\n";
return s;
}
function LineSCAD(a, b, r, l, type, child)
{
child = child || "";
return "PlaceLine([" + pV3(a) + "," + pV3(b) + "],r=" + r + ",l=" + l + ",type=" + type + ") {" + child + "}\n";
}
function SphereSCAD(r, translate, fn)
{
var s = "";
if (translate)
s += "translate(" + pV3(translate) + ")";
s += "sphere(" + r;
if (fn) s += ",$f=" + fn;
s += ");";
return s;
}
function reorderFace(list, i)
{
if (i == 0) return list;
if (i == 1) return [list[1], list[2], list[0]];
if (i == 2) return [list[2], list[0], list[1]];
alert("reorder unknown" + i);
}
function pV3(v3)
{
return "[" + v3.x + "," + v3.y + "," + v3.z + "]";
}
function dV3(v3)
{
return "x" + v3.x + "/y" + v3.y +"/z" + v3.z;
}
function copyToClipboard (text) {
window.prompt ("Copy to clipboard: Ctrl+C, Enter", text);
}
function generateStickSCAD(vA, vP, vB, vC, cutA, cutB, hDelta, slack, returnInvRot)
{
slack = slack || 0;
var s = "";
var pDir = new THREE.Vector3();
pDir.subVectors(vP, vA).normalize();
var bDir = new THREE.Vector3();
bDir.subVectors(vB, vA);
var edgeLen = bDir.length();
bDir.normalize();
var cDir = new THREE.Vector3();
cDir.subVectors(vC, vA).normalize();
var pNormal = new THREE.Vector3();
pNormal.crossVectors( pDir, bDir ).normalize();
var normal = new THREE.Vector3();
normal.crossVectors( bDir, cDir ).normalize();
var cosA = pNormal.dot(normal);
var angle = Math.acos(cosA);
// I dont understand that criterium but it works for me :)
if (angle < Math.PI/2) angle = -angle;
var bR = LineRotations(bDir).multiplyScalar(1);
var invR = new THREE.Matrix4().setRotationFromEuler(bR, "ZXY");
var m1 = new THREE.Matrix4();
var m3 = new THREE.Matrix4();
m1.makeRotationZ( -bR.z );
m3.makeRotationX( -bR.x );
revN = m1.multiplyVector3(normal);
revN = m3.multiplyVector3(revN);
var rA = (Math.atan2(revN.y, revN.x)) * 180/Math.PI;
rA = rA - ((180 - angle* 180/Math.PI)/2);
var xLen = 0.6;
var extraH = 1;
var edgeX = Math.cos(-angle/2);
var edgeY = Math.sin(-angle/2);
edgeY /= edgeX;
edgeY *= xLen
edgeX = xLen;
var bottomY = -extraH;
if (edgeY < 0)
bottomY = edgeY - extraH;
//var centerDir = new THREE.Vector3();
//centerDir.addVectors(pNormal, normal).normalize();
//var cosR = centerDir.dot(bDir);
if (returnInvRot)
{
return "translate([0,0," + (-bottomY) + "]) rotate([90,0,0]) rotate([0,0," + (-rA) + "]) rotate([" + (-bR.x* 180/Math.PI) + ",0,0]) " +
"rotate([0,0,"+ (-bR.z* 180/Math.PI) + "]) translate(-" + pV3(vA) + ")";
}
s += "rotate([0,0," + rA + "])";
if (slack != 0) s += "minkowski() { circle(" + slack + ", $fn=8);"
s += "polygon([[0," + (hDelta) + "],["+(edgeX)+","+(edgeY+hDelta)+"],["+(edgeX)+","+(bottomY)+"]," +
"[-"+(edgeX)+","+(bottomY)+"],[-"+(edgeX)+","+(edgeY+hDelta)+"]]);";
if (slack != 0) s += "}"
var cutSum = cutA + cutB;
s = LineSCAD(vA, vB, "sR", "sL", 0, "translate([0,0," + cutA + "]) linear_extrude(height=" + (edgeLen - cutSum) + ") " + s);
return s;
}
function LineRotations(v3) {
var r = new THREE.Vector3(Math.atan2(Math.sqrt(v3.x*v3.x+v3.y*v3.y), v3.z), 0, Math.atan2(v3.y, v3.x)+Math.PI/2);
return r;
}