-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlib-spiral.scad
311 lines (296 loc) · 13 KB
/
lib-spiral.scad
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
/*
* Copyright (c) 2019, Gilles Bouissac
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* Description: Sweep an opened profile along a spiral to build a 3D surface without any manifold issue
* Author: Gilles Bouissac
*/
use <scad-utils/lists.scad>
use <scad-utils/transformations.scad>
use <list-comprehension-demos/skin.scad>
use <agentscad/extensions.scad>
use <agentscad/mesh.scad>
// ----------------------------------------
//
// API
//
// ----------------------------------------
//
// Build an external spiral mesh from a given 2D profile
// The spiraled profile is the external surface of the mesh
//
// It is assumes that the profile respects these conditions:
// - x is always positive,
// - First profile point Y is less than last profile Y
//
// In other cases the result is likely to give bad normal orientation
//
// Params:
// - profile: 2D array (x,y) of points
// - loops: Number of spiral rotation or fraction of rotation required
// - layer: (Optional): Spiral layers height
// computed from profile y min/max coordinates if not set
//min/max
function meshSpiralExternal ( profile, loops=1, layer=undef ) =
meshSpiralExternalImpl ( profile, loops, layer );
//
// Build an internal spiral mesh from a given 2D profile
// The spiraled profile is the internal surface of the mesh
// The external surface is a cylinder with given radius
//
// It is assumes that the profile respects these conditions:
// - x is always positive,
// - First profile point Y is less than last profile Y
//
// In other cases the result is likely to give bad normal orientation
//
// Params:
// - profile: 2D array (x,y) of points
// - loops: Number of spiral rotation or fraction of rotation required
// - layer: (Optional): Spiral layers height
// computed from profile y min/max coordinates if not set
// - radius: (Optional): Radius of cylinder around the profile:
// computed to be equal to profile x max coordinates if not set
//
function meshSpiralInternal ( profile, loops=1, layer=undef, radius=undef ) =
meshSpiralInternalImpl ( profile, loops, layer, radius );
// ----------------------------------------
//
// Implementation
//
// ----------------------------------------
//
// Implementation of meshSpiralExternal
//
function meshSpiralExternalImpl ( profile, loops=1, layer=undef ) = let(
bot_z = min ( [for(p=profile) p.y] ),
profile3d = [ for (i=[0:len(profile)-1]) [profile[i].x,0,profile[i].y-bot_z] ],
loop_h = is_undef(layer) ? abs(profile3d[0].z-profile3d[len(profile3d)-1].z) : layer,
step = getStep(),
step_h = step*loop_h,
nb_slice = floor ( loops/step ),
nb_slice_loop = floor ( 1/step ),
nb_slice_facets = len ( profile3d ),
nb_slice_cap = nb_slice<nb_slice_loop ? nb_slice:nb_slice_loop,
points_profiles = flatten([ for (i=[0:nb_slice])
transform (
translation([0,0,i*step_h])*rotation([0,0,i*step*360]),
profile3d )
]),
cap_pt_idx = len(points_profiles),
cap_sl_idx = nb_slice+nb_slice_loop-nb_slice_cap,
// One layer of first profile point for last seam
cap_points = flatten([ for (i=[0:nb_slice_cap]) let (is=cap_sl_idx+i)
transform (
translation([0,0,is*step_h])*rotation([0,0,is*step*360]),
[ profile3d[0] ] )
]),
// Bottom pole (index of first point: pole_bot_pt_idx)
pole_bot_pt_idx = cap_pt_idx + len(cap_points),
bottom_points = [
for ( i=[0:nb_slice_cap] ) [0,0,(i+1)*step_h]
,[0,0,loop_h] // Last point for complete Bottom face
],
// Top pole (index of first point: pole_top_pt_idx)
pole_top_pt_idx = pole_bot_pt_idx + len(bottom_points),
top_points = [
[0,0,nb_slice*step_h], // First point for complete Top face
for ( i=[0:nb_slice_cap] ) let (is=cap_sl_idx+i) [0,0,is*step_h]
],
points = concat( points_profiles, cap_points, bottom_points, top_points ),
faces = [
// Profile sweeping
for ( s=[0:nb_slice-1], f=[0:nb_slice_facets-2] ) [
( s % (nb_slice+1)) * (nb_slice_facets) + f
,( s % (nb_slice+1)) * (nb_slice_facets) + (f+1) % (nb_slice_facets)
,( (s+1) % (nb_slice+1)) * (nb_slice_facets) + (f+1) % (nb_slice_facets)
,( (s+1) % (nb_slice+1)) * (nb_slice_facets) + f
],
// Seams
if ( nb_slice>nb_slice_cap )
for ( s=[0:nb_slice-nb_slice_cap-1] ) [
(s+0) * (nb_slice_facets) + nb_slice_facets-1
, (s+nb_slice_loop+0) * (nb_slice_facets) + 0
, (s+nb_slice_loop+1) * (nb_slice_facets) + 0
, (s+1) * (nb_slice_facets) + nb_slice_facets-1
],
// Seams top cap
for ( s=[0:nb_slice_cap-1] ) [
(nb_slice-nb_slice_cap+s+0) * (nb_slice_facets) + nb_slice_facets-1
, cap_pt_idx + s + 0
, cap_pt_idx + s + 1
, (nb_slice-nb_slice_cap+s+1) * (nb_slice_facets) + nb_slice_facets-1
],
// Bottom cap
for ( s=[0:nb_slice_cap-1] ) [ (s+1) * nb_slice_facets, pole_bot_pt_idx+s+0, (s+0) * nb_slice_facets ],
for ( s=[0:nb_slice_cap-1-1] ) [ (s+1) * nb_slice_facets, pole_bot_pt_idx+s+1, pole_bot_pt_idx+s+0 ],
// Top cap
for ( s=[0:nb_slice_cap-1] ) [ cap_pt_idx+s+0, pole_top_pt_idx+s+1, cap_pt_idx+s+1 ],
for ( s=[0:nb_slice_cap-2] ) [ cap_pt_idx+s+1, pole_top_pt_idx+s+1, pole_top_pt_idx+s+2 ],
// Bottom wall
[
for ( i=[0:len(bottom_points)-1] ) pole_bot_pt_idx+i ,
nb_slice>nb_slice_cap ? nb_slice_loop*nb_slice_facets:cap_pt_idx,
for ( i=[nb_slice_facets-1:-1:0] ) i
],
// Top wall
[
for ( i=[len(top_points)-2:-1:0] ) pole_top_pt_idx+i ,
for ( i=[0:nb_slice_facets-1] ) len(points_profiles)-nb_slice_facets+i,
cap_pt_idx+nb_slice_cap,
]
]
) newMesh ( points, faces );
//
// Implementation of meshSpiralInternal
//
function meshSpiralInternalImpl ( profile, loops, layer, radius ) = let(
bot_z = min ( [for(p=profile) p.y] ),
profile3d = [ for (i=[0:len(profile)-1]) [profile[i].x,0,profile[i].y-bot_z] ],
loop_h = is_undef(layer) ? abs(profile3d[0].z-profile3d[len(profile3d)-1].z) : layer,
step = getStep(),
step_h = step*loop_h,
nb_slice = floor ( loops/step ),
nb_slice_loop = floor ( 1/step ),
nb_slice_facets = len ( profile3d ),
nb_slice_cap = nb_slice<nb_slice_loop ? nb_slice:nb_slice_loop,
overlaps = nb_slice>nb_slice_loop,
nb_slice_overlap = overlaps ? nb_slice%nb_slice_loop:0,
min_r = max ( [for(p=profile3d)p.x] ),
loc_r = is_undef(radius) || radius<min_r ? min_r : radius,
points_profiles = flatten([ for (i=[0:nb_slice])
transform (
translation([0,0,i*step_h])*rotation([0,0,i*step*360]),
profile3d )
]),
cap_sl_idx = nb_slice+nb_slice_loop-nb_slice_cap,
cap_pt_idx = len(points_profiles),
// One layer of first profile point for last seam
cap_points = flatten([ for (i=[0:nb_slice_cap]) let (is=cap_sl_idx+i)
transform (
translation([0,0,is*step_h])*rotation([0,0,is*step*360]),
[ profile3d[0] ] )
]),
// Cylinder bottom points
cyl_bot_pt_idx = cap_pt_idx + len(cap_points),
bottom_points = flatten([ for ( i=[0:nb_slice_cap] ) let (is=i)
transform ( translation([0,0,is*step_h])*rotation([0,0,is*step*360]), [[loc_r,0,0]])
]),
// Cylinder top points
cyl_ovl_pt_idx = cyl_bot_pt_idx + len(bottom_points),
overlap_points = flatten([
if (overlaps) for ( i=[-nb_slice_overlap:0] ) let (is=cap_sl_idx+nb_slice_loop+i)
transform ( translation([0,0,is*step_h])*rotation([0,0,is*step*360]), [[loc_r,0,0]])
]),
cyl_top_pt_idx = cyl_ovl_pt_idx + len(overlap_points),
top_points = flatten([
for ( i=[0:nb_slice_cap-nb_slice_overlap] ) let (is=cap_sl_idx+i)
transform ( translation([0,0,is*step_h])*rotation([0,0,is*step*360]), [[loc_r,0,0]])
]),
points = concat( points_profiles,cap_points,bottom_points,overlap_points,top_points ),
faces = [
// Profile sweeping
for ( s=[0:nb_slice-1], f=[0:nb_slice_facets-2] ) [
( (s+1) % (nb_slice+1)) * (nb_slice_facets) + f
,( (s+1) % (nb_slice+1)) * (nb_slice_facets) + (f+1) % (nb_slice_facets)
,( s % (nb_slice+1)) * (nb_slice_facets) + (f+1) % (nb_slice_facets)
,( s % (nb_slice+1)) * (nb_slice_facets) + f
],
// Seams
if ( nb_slice>nb_slice_cap )
for ( s=[0:nb_slice-nb_slice_cap-1] ) [
(s+1) * (nb_slice_facets) + nb_slice_facets-1
,(s+nb_slice_loop+1) * (nb_slice_facets) + 0
,(s+nb_slice_loop+0) * (nb_slice_facets) + 0
,(s+0) * (nb_slice_facets) + nb_slice_facets-1
],
// Seams top cap
for ( s=[0:nb_slice_cap-1] ) [
(nb_slice-nb_slice_cap+s+1) * (nb_slice_facets) + nb_slice_facets-1
,cap_pt_idx + s + 1
,cap_pt_idx + s + 0
,(nb_slice-nb_slice_cap+s+0) * (nb_slice_facets) + nb_slice_facets-1
],
// Cylinder
if (nb_slice_overlap>0)
for ( s=[0:nb_slice_overlap-1] ) let (
offs1=cyl_bot_pt_idx,
offs2=cyl_ovl_pt_idx,
is_first = s==0,
is_last = s==nb_slice_overlap-1
)
concat(
[offs1+s+1,offs1+s+0],
is_first ? [cyl_bot_pt_idx+nb_slice_cap]:[],
[offs2+s+0, offs2+s+1],
is_last ? [cyl_top_pt_idx]:[]
),
for ( s=[0:nb_slice_cap-nb_slice_overlap-1] ) let (
offs1=cyl_bot_pt_idx+nb_slice_overlap,
offs2=cyl_top_pt_idx,
is_first = nb_slice_overlap==0 && s==0,
is_last = nb_slice_overlap==0 && s==nb_slice_cap-1
)
concat(
[offs1+s+1,offs1+s+0],
is_first ? [cyl_bot_pt_idx+nb_slice_cap]:[],
[offs2+s+0,offs2+s+1],
is_last ? [cyl_top_pt_idx]:[]
),
// Bottom cap
for ( s=[0:nb_slice_cap-1] ) let ( mult1=nb_slice_facets, offs2=cyl_bot_pt_idx )
[ (s+1)*mult1, (s+0)*mult1, offs2+s+0, offs2+s+1 ],
// Top cap
for ( s=[0:nb_slice_cap-nb_slice_overlap-1] ) let ( offs1=cap_pt_idx, offs2=cyl_top_pt_idx )
[ offs1+s+0, offs1+s+1, offs2+s+1, offs2+s+0 ],
if ( nb_slice_overlap>0 )
for ( s=[0:nb_slice_overlap-1] ) let ( offs1=cap_pt_idx+nb_slice_loop-nb_slice_overlap, offs2=cyl_ovl_pt_idx)
[ offs1+s+0, offs1+s+1, offs2+s+1, offs2+s+0 ],
// Bottom wall
[
for ( i=[0:nb_slice_facets-1] ) i,
overlaps ? nb_slice_cap*nb_slice_facets : cap_pt_idx,
overlaps ? cyl_bot_pt_idx+nb_slice_cap : cyl_top_pt_idx,
cyl_bot_pt_idx+0
],
// Top wall
[
cap_pt_idx+nb_slice_cap,
for ( i=[nb_slice_facets-1:-1:0] ) len(points_profiles)-nb_slice_facets+i,
overlaps ? cyl_top_pt_idx : nb_slice_overlap==0?cyl_top_pt_idx:cyl_bot_pt_idx+nb_slice_cap,
overlaps ? cyl_ovl_pt_idx+len(overlap_points)-1: cyl_top_pt_idx+nb_slice_cap,
]
]
) newMesh ( points, faces );
// ----------------------------------------
//
// Showcase
//
// ----------------------------------------
module showcase() {
height = 4;
layer = 2;
loops = height/layer+0*getStep();
gap = 0.05;
profile = [ for ( i=[0:0.05:1]) [2+0.2*sin(i*360),0.05+(layer-0.05)*i] ];
// profile = [ [2,0.1], [1,1], [2,2] ];
meshe = meshSpiralExternal(profile=[for(p=profile)[p.x-gap,p.y]],loops=loops,layer=layer );
meshi = meshSpiralInternal(profile=[for(p=profile)[p.x+gap,p.y]],loops=loops,layer=layer,radius=4 );
difference() {
intersection() {
union() {
meshPolyhedron(meshi,convexity=5);
meshPolyhedron(meshe,convexity=5);
}
translate( [0,0,layer+layer/2] )
cube( [100,100,layer], true );
}
cube( [10,10,10] );
}
}
*
showcase ($fn=50);