-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquads.scad
More file actions
587 lines (485 loc) · 15.2 KB
/
Copy pathquads.scad
File metadata and controls
587 lines (485 loc) · 15.2 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* quads.scad -
*
* Some quadrilaterals that are perturbed rectangles, there are lots of settings
* and options.
*
* HeightMode - Method used to set height of each quad, and must be one of:
*
* HM_FIXED- All quads are the same height, BaseHeight.
*
* HM_RANDOM - Each quad is BaseHeight + one of Heights
* random values, stepped by HeightInc.
*
* QuadType - Type of each quad, and must be one of:
*
* QT_VERT - Polygon with vertical sides.
*
* QT_TAPER - Polygon with tapered sides.
*
* QT_TOPO - Polyhedrons that meet at the edges, works best when
* HeightMode is HM_RANDOM.
*
*
* ColorMode - Color mode for quads:
*
* CM_RANDOM - Random colors in range [FirstExtruder, LastExtruder].
*
* CM_RADIAL - Module radial colors in range [FirstExtruder, LastExtruder].
* This seems to look best when _Rows and _Cols are odd.
*
* TODO:
* - Update documentation
* - See if MultiExtruder is really needed
*/
/*
* Each x in the G grid is a point that will be perturbed (in x and y)
* to make slightly irregular quadrilaterals. The edges remain untouched
* to create a rectangle.
*
* 0 1 2 3 4 5 6 7 8
* 0 +---+---+---+---+---+---+---+---+
* | A | | | | | | | |
* 1 +---x---x---x---x---x---x---x---+
* | | | | | | | | |
* 2 +---x---x---x---x---x---x---x---+
* | | | | | | | | |
* 3 +---x---x---x---x---x---x---x---+
* | | | | | | | | |
* 4 +---+---+---+---+---+---+---+---+
*
* A is the first rectangle (defined by the points around it, and so forth.
*
* The H grid is used only when QuadType is QT_TOPO, and has the same basic
* layout as the G grid. Each x in the grid represents the height of the
* polyhedron where the adjacent quadrilaterals meet. Each element of the
* H grid is a height value.
*/
/*
* Define the grid:
*/
// Number of rows in the grid
_Rows = 12;
// Number of columns in the grid
_Cols = 12;
/*
* Define size of each rectangle in the grid:
*/
// Width of each rectangle
_RectWidth = 15;
// Depth of each rectangle
_RectDepth = 15;
/*
* Define gap between rectangles:
*/
// Gap between each row of rectangles
_RectRowGap = 1.0; // [0.1 : 0.1 : 10]
// Gap between each column of rectangles
_RectColGap = 2; // [0.1 : 0.1 : 10]
/*
* Define amount of perturbation per rectangle, this is -/+, so the
* perturbation can be from (-RowPert) to (RowPert), and the same
* for ColPert.
*/
// Max perturbation between rows
_RowPert = 8;
// Max perturbation between columns
_ColPert = 8;
// Scale row perturbation
_ScaleRowPert = "None"; // ["None", "Start", "Center"]
// Scale columm perturbation
_ScaleColPert = "None"; // ["None", "Start", "Center"]
/*
* Set height mode:
*/
// Height mode
_HeightMode = "HM_FIXED"; // [HM_FIXED, HM_RANDOM]
/*
* Define height parameters:
*/
// Quad height
_QuadHeight = 3; // [0.2 : 10]
// Height increment
_HeightInc = 0.4; // [0.2 : 0.2 : 10]
// Number of random heights
_Heights = 7; // [1 : 20]
// Scale at top of QT_TAPER quad
_TaperTopScale = 0.5; // [0.0 : 0.1 : 1.0]
/*
* Set quad type:
*/
// Quad type
_QuadType = "QT_VERT"; // [QT_VERT, QT_TAPER, QT_TOPO]
// Random seed
_RandomSeed = 1313;
/* [Rim] */
// Render rim
_RenderRim = true;
// Additional Rim Height
_RimHeight = 0.4;
// Rim Thickness
_RimThickness = 0.5;
/* [Base] */
// Render base
_RenderBase = false;
// Base height
_BaseHeight = 0.2; // [0.2 : 0.2 : 10]
// Base margin
_BaseMargin = 1.0;
/* [Lead] */
// Render lead
_RenderLead = false;
// Lead height
_LeadHeight = 0.4; // [0.2 : 0.2 : 10]
/* [Extruders] */
// Multiple extruder
_MultiExtruder = false;
// Color mode
_ColorMode = "Random"; // ["Random", "Radial"]
// first extruder
_FirstExtruder = 1;
// Last extruder
_LastExtruder = 4;
// Rim extruder
_RimExtruder = 5;
// Base extruder
_BaseExtruder = 5;
// Lead extruder
_LeadExtruder = 5;
// [Extruder to render]
_WhichExtruder = "All"; // ["All", 1, 2, 3, 4, 5]
/* End of customization */
module __Customizer_Limit__ () {}
/* Perform sanity checks */
assert(_Rows > 0);
assert(_Cols > 0);
assert(_RectWidth > 0);
assert(_RectDepth > 0);
assert(_RectRowGap >= 0);
assert(_RectColGap >= 0);
assert(_RowPert >= 0);
assert(_ColPert >= 0);
assert((_HeightMode == "HM_FIXED") || (_HeightMode == "HM_RANDOM"));
assert((_QuadType == "QT_VERT") || (_QuadType == "QT_TAPER") || (_QuadType == "QT_TOPO"));
// Map a value of _WhichExtruder to an OpenSCAD color
function ExtruderColor(Extruder) =
(Extruder == 1 ) ? "red" :
(Extruder == 2 ) ? "green" :
(Extruder == 3 ) ? "blue" :
(Extruder == 4 ) ? "pink" :
(Extruder == 5 ) ? "yellow" :
(Extruder == "All") ? "orange" :
"purple" ;
// If _WhichExtruder is "All" or is not "All" and matches the
// requested extruder, render the child nodes.
module Extruder(DoExtruder)
{
color(ExtruderColor(DoExtruder))
{
if (_WhichExtruder == "All" || DoExtruder == _WhichExtruder || DoExtruder == "All")
{
children();
}
}
}
/* Seed the Random number generator */
X = rands(0, 100, 1, _RandomSeed);
/* Compute overall size */
_OverallWidth = _BaseMargin + (_Cols * _RectWidth) + ((_Cols - 1) * _RectColGap) + _BaseMargin;
_OverallDepth = _BaseMargin + (_Cols * _RectDepth) + ((_Rows - 1) * _RectRowGap) + _BaseMargin;
echo ("Overall size: ", _OverallWidth, _OverallDepth);
/*
* GridRowPert - Return a value that will be used to perturb a row coordinate in the G grid
* GridColPert - Return a value that will be used to perturb a column coordinate in the G grid
*/
function GridRowPert() = rands(-_RowPert, _RowPert, 1)[0];
function GridColPert() = rands(-_ColPert, _ColPert, 1)[0];
/*
* The following two functions return scaling (0 - 1) for row or column
* perturbation to allow for gradually increasing amounts of disorder. There
* are three options, based on _ScaleRowPert/_ScaleColPert:
*
* "None" - No scaling.
*
* "Start" - 0 1 as row or col goes from 0 to end of row (_Rows) or column (_Cols)
*
* "Center" - 0 to 1 starting from center/midpoint.
*
*/
function GridRowPertScale(r, c) =
(_ScaleRowPert == "None") ? 1.0 :
(_ScaleRowPert == "Start") ? (r - 1) / _Rows :
(_ScaleRowPert == "Center") ? abs(r - 1 - _Rows / 2) / (_Rows / 2) :
0;
function GridColPertScale(r, c) =
(_ScaleColPert == "None") ? 1.0 :
(_ScaleColPert == "Start") ? (c - 1)/ _Cols :
(_ScaleColPert == "Center") ? abs(c - 1 - _Cols / 2) / (_Cols / 2) :
0;
/* Build the G grid */
G =
[
[for (c = [0 : _Cols]) [ 0, 0]],
for (r = [1 : _Rows - 1])
[
[0, 0],
for (c = [1 : _Cols - 1])
[
GridRowPert() * GridRowPertScale(r, c), GridColPert() * GridColPertScale(r, c)
],
[0, 0]
],
[for (c = [0 : _Cols]) [ 0, 0]],
];
echo("G Grid:");
for (r = [0 : _Rows])
{
echo(" Row: ", r);
echo(" ", G[r], "\n");
}
/* Build the H grid */
H =
[
[for (c = [0 : _Cols]) _QuadHeight],
for (r = [1 : _Rows - 1])
[
_QuadHeight,
for (c = [1 : _Cols - 1]) _QuadHeight + floor(rands(0, _Heights, 1)[0]) * _HeightInc,
_QuadHeight
],
[for (c = [0 : _Cols]) _QuadHeight],
];
echo("H Grid:");
for (r = [0 : _Rows])
{
echo(" Row: ", r);
echo(" ", H[r], "\n");
}
/* Generate random extruders/colors */
_ExtruderGridRandom =
[
for (r = [1 : _Rows])
[
for (c = [1 : _Cols])
floor(rands(0, 1, _RandomSeed)[0] * (_LastExtruder - _FirstExtruder + 1)) + _FirstExtruder
]
];
echo("Extruder Grid Random:");
echo(_ExtruderGridRandom);
_CenterRow = floor(_Rows / 2);
_CenterCol = floor(_Cols / 2);
echo("CenterRow=", _CenterRow, ", _CenterCol=", _CenterCol);
function Distance(X0, Y0, X1, Y1) = sqrt((X1 - X0) ^ 2 + (Y1 - Y0) ^ 2);
/* Generate radial extruders/colors */
_ExtruderGridRadial =
[
for (r = [0 : _Rows - 1])
[
for (c = [0 : _Cols - 1])
(round(Distance(_CenterRow, _CenterCol, r, c)) % (_LastExtruder - _FirstExtruder + 1)) + _FirstExtruder
]
];
/*
* Names of the X_BL (Bottom Left) define the four corners of the rectangle:
*
* [X_TL, Y_TL] [X_TR, Y_TR]
* +-------------...-------------+
* | |
* . .
* | |
* +-------------...-------------+
* [X_BL, Y_BL] [X_BR, Y_BR]
*
* These values are then perturbed via the G grid to define the corners of the
* quadrilateral, with names suffixed by _G.
*/
//translate([-Width / 2, -Depth / 2, 0])
/* Construct polygons */
/* Render a quadrilateral of type QT_VERT */
module RenderQuadVert(QuadPoly, QuadExtruder, QuadHeight, RenderRim, RimExtruder, RimHeight, RimThickness)
{
Extruder(QuadExtruder)
{
linear_extrude(QuadHeight)
{
polygon(QuadPoly);
}
}
if (RenderRim)
{
translate([0, 0, QuadHeight])
{
Extruder(RimExtruder)
{
linear_extrude(RimHeight)
{
difference()
{
polygon(QuadPoly);
offset(delta=-RimThickness)
{
polygon(QuadPoly);
}
}
}
}
}
}
}
/* Render a quadrilateral of type QT_TAPER */
module RenderQuadTaper(X_Center, Y_Center, TaperPoly, QuadExtruder, Height, TaperTopScale)
{
Extruder(QuadExtruder)
{
translate([X_Center, Y_Center, 0])
{
linear_extrude(Height, scale=TaperTopScale)
{
translate([-X_Center, -Y_Center, 0])
{
polygon(TaperPoly);
}
}
}
}
}
/* Render a quadrilateral of type QT_TOPO */
module RenderQuadTopo(PolyPoints, PolyFaces, QuadExtruder)
{
Extruder(QuadExtruder)
{
polyhedron(points=PolyPoints,faces=PolyFaces);
}
}
/* Render the grid of quadrilaterals */
module RenderQuadGrid(Rows, Cols, QuadType, HeightMode, RectWidth, RectDepth, RectRowGap, RectColGap, RowPert, ColPert, QuadHeight, HeightInc, Heights, TaperTopScale, ColorMode, MultiExtruder, SingleExtruder, RimExtruder, RenderRim, RimHeight, RimThickness)
{
for (r = [0 : Rows - 1])
{
for (c = [0 : Cols - 1])
{
/* Compute coordinates of each rectangle */
X_BL = (c * (RectWidth + RectColGap));
X_BR = X_BL + RectWidth;
X_TL = X_BL;
X_TR = X_BR;
Y_BL = (r * (RectDepth + RectRowGap));
Y_TL = Y_BL + RectDepth;
Y_BR = Y_BL;
Y_TR = Y_TL;
/* Adjust by the per-corner perturbation in the grid */
X_BL_G = X_BL + G[r][c][1];
Y_BL_G = Y_BL + G[r][c][0];
X_BR_G = X_BR + G[r][c+1][1];
Y_BR_G = Y_BR + G[r][c+1][0];
X_TL_G = X_TL + G[r+1][c][1];
Y_TL_G = Y_TL + G[r+1][c][0];
X_TR_G = X_TR + G[r+1][c+1][1];
Y_TR_G = Y_TR + G[r+1][c+1][0];
/* Pick a height based on HeightMode (this value is not used for QT_TOPO, since each corner has a distinct height) */
Height =
(HeightMode == "HM_FIXED") ? QuadHeight :
(HeightMode == "HM_RANDOM") ? QuadHeight + floor(rands(0, Heights, 1)[0]) * HeightInc :
0;
// If MultiExtruder is specified use the pert-quad color from _ExtruderGridRandom or _ExtruderGridRadial per
// ColorMode, otherwise use the given single extruder:
QuadExtruder =
(MultiExtruder && ColorMode == "Random") ? _ExtruderGridRandom[r][c] :
(MultiExtruder && ColorMode == "Radial") ? _ExtruderGridRadial[r][c] :
SingleExtruder;
/* Generate quad based on QuadType */
if (QuadType == "QT_VERT")
{
QuadPoly = [[X_BL_G, Y_BL_G], [X_BR_G, Y_BR_G],
[X_TR_G, Y_TR_G], [X_TL_G, Y_TL_G]];
RenderQuadVert(QuadPoly, QuadExtruder, Height, RenderRim, RimExtruder, RimHeight, RimThickness);
}
else
if (QuadType == "QT_TAPER")
{
X_Center = X_BL + RectWidth / 2;
Y_Center = Y_BL + RectDepth / 2;
TaperPoly = [[X_BL_G, Y_BL_G], [X_BR_G, Y_BR_G], [X_TR_G, Y_TR_G], [X_TL_G, Y_TL_G]];
RenderQuadTaper(X_Center, Y_Center, TaperPoly, QuadExtruder, Height, TaperTopScale);
}
else if (QuadType == "QT_TOPO")
{
/* Get height of each corner */
Z_BL = H[r][c];
Z_BR = H[r][c+1];
Z_TL = H[r+1][c];
Z_TR = H[r+1][c+1];
echo("[", r, ", ", c, "]: ", Z_TL, ", ", Z_TR, ", ", Z_BL, ", ", Z_BR);
/* Create points for polyhedron */
PolyPoints =
[
[X_BL_G, Y_BL_G, 0], // 0
[X_BR_G, Y_BR_G, 0], // 1
[X_TR_G, Y_TR_G, 0], // 2
[X_TL_G, Y_TL_G, 0], // 3
[X_BL_G, Y_BL_G, Z_BL], // 4
[X_BR_G, Y_BR_G, Z_BR], // 5
[X_TR_G, Y_TR_G, Z_TR], // 6
[X_TL_G, Y_TL_G, Z_TL], // 7
];
/* Create polyhedron */
PolyFaces =
[
[0,1,2,3], // bottom
[4,5,1,0], // front
[7,6,5,4], // top
[5,6,2,1], // right
[6,7,3,2], // back
[7,4,0,3] // left
];
RenderQuadTopo(PolyPoints, PolyFaces, QuadExtruder);
}
}
}
}
/* Render base below quads */
module RenderQuadBase(Rows, Cols, RectWidth, RectDepth, RectRowGap, RectColGap, BaseHeight, BaseMargin, BaseExtruder)
{
translate([-BaseMargin, -BaseMargin, 0])
{
Extruder(BaseExtruder)
{
linear_extrude(BaseHeight)
{
square([_OverallWidth, _OverallDepth], center=false);
}
}
}
}
module main(Rows, Cols, QuadType, HeightMode, RectWidth, RectDepth, RectRowGap, RectColGap, RowPert, ColPert, QuadHeight, HeightInc, Heights, TaperTopScale, ColorMode, MultiExtruder, RimExtruder, RenderRim, RimHeight, RimThickness, RenderBase, BaseHeight, BaseMargin, BaseExtruder, RenderLead, LeadHeight, LeadExtruder)
{
RenderQuadGrid(Rows, Cols, QuadType, HeightMode, RectWidth, RectDepth, RectRowGap, RectColGap, RowPert, ColPert, QuadHeight, HeightInc, Heights, TaperTopScale, ColorMode, MultiExtruder, 1, RimExtruder, RenderRim, RimHeight, RimThickness);
if (RenderBase)
{
translate([0, 0, -BaseHeight])
{
RenderQuadBase(Rows, Cols, RectWidth, RectDepth, RectRowGap, RectColGap, BaseHeight, BaseMargin, BaseExtruder);
}
}
if (RenderLead)
{
difference()
{
// A full plate of lead
translate([-BaseMargin, -BaseMargin, 0])
{
Extruder(LeadExtruder)
{
linear_extrude(LeadHeight - 0.001)
{
square([_OverallWidth, _OverallDepth], center=false);
}
}
}
// With holes cut out for the quad grid
{
RenderQuadGrid(Rows, Cols, QuadType, "HM_FIXED", RectWidth, RectDepth, RectRowGap, RectColGap, RowPert, ColPert, LeadHeight, HeightInc, Heights, TaperTopScale, ColorMode, false, "All", RimExtruder, RenderRim, RimHeight, RimThickness);
}
}
}
}
main(_Rows, _Cols, _QuadType, _HeightMode, _RectWidth, _RectDepth, _RectRowGap, _RectColGap, _RowPert, _ColPert, _QuadHeight, _HeightInc, _Heights, _TaperTopScale, _ColorMode, _MultiExtruder, _RimExtruder, _RenderRim, _RimHeight, _RimThickness, _RenderBase, _BaseHeight, _BaseMargin, _BaseExtruder, _RenderLead, _LeadHeight, _LeadExtruder);