-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy path3-link.scad
266 lines (226 loc) · 6.44 KB
/
3-link.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
// create 2D,2.5D amd 3D models based on
step=0.5; // step size in degrees
thickness=0.5; // width of the line
/* Engare 1
nodes=4;
cycles=1;
R=nodes/cycles;
link1=[1,10,0];
link2=[R,5.5,0];
link3=[4*R,1.5,0];
links=[link1,link2,link3];
reps=1;
*/
/*
// Engare 2
nodes=4;
cycles=1;
R=nodes/cycles;
R=10;
link1=[1,10,0];
link2=[-R,5,0]; // anticlockwise
link3=[3*R,1.75,0];
links=[link1,link2,link3];
reps=1;
*/
/*
// swirl
cycles=1;
nodes=6;
R=6;
link1=[1,8,0];
link2=[-R,5,0];
link3=[5*R,2.25,90];
links=[link1,link2,link3];
reps=1;
*/
/* Limaçon
nodes=1;
cycles=1;
R=nodes/cycles;
R=1;
link1=[1,10,0];
link2=[R,7,0]; // anticlockwise
link3=[3*R,0,0];
links=[link1,link2,link3];
reps=1;
*/
/*
$t=0.2;
// fridge magnet inset
nodes=15;
cycles=4;
R=nodes/cycles;
d=2;
link1=[1,14.5,0];
link2=[R,10.5-d,0]; // anticlockwise
link3=[2*R,d,0];
links=[link1,link2,link3];
reps=1;
*/
$t=0.2;
nodes=5;
cycles=1;
R=nodes/cycles;
R=5;
link1=[1,10,0];
link2=[R,5,0];
link3=[-4*R,3.33,-90];
links=[link1,link2,link3];
reps=1;
render="2D";
method="poly";
$fn=12;
//echo(f(90,links));
// overall scale
Scale=1;
path = path_points(step,0, 1*cycles*360,links);
color("red")
scale(Scale)
if (render=="2D") {
for(rep=[0:1:reps-1]) {
pmax= 360 / nodes;
rotate([0,0,pmax*rep/reps])
plane_path( path,thickness);
}
}
else if (render=="2.5D") {
height=2;
linear_extrude(height=height)
for(rep=[0:1:reps-1]) {
pmax= 360 / nodes;
rotate([0,0,pmax*rep/reps])
plane_path(path, thickness);
}
}
else if (render=="3D")
if( method=="hull")
if(reps==1) {
hull_path(path,thickness);
}
else {
for(rep=[0:1:reps-1])
rotate([0,0,pmax*rep/reps])
hull_path(path,thickness);
}
else if(method=="poly") {
Sides=12; // Sides of rope - must be a divisor of 360
Phase = 45; // phase angle for profile (maters for low Sides
Open = false; // true if knot is open or partial
Start=0; End=cycles*360; // change for partial path
if(reps==1) {
poly= poly_path(path,thickness,Sides,Phase,Open);
polyhedron(poly[0],poly[1]);
}
else {
poly= poly_path(path,thickness,Sides,Phase,Open);
for(rep=[0:1:reps-1]) {
pmax= 360 / nodes;
rotate([0,0,pmax*rep/reps])
polyhedron(poly[0],poly[1]);
}
}
}
// generative function
function f(t,param) =
let(p1=param[0],p2=param[1],p3=param[2])
let(r1=p1[0],a1=p1[1],t1=p1[2])
let(r2=p2[0]+r1,a2=p2[1],t2=p2[2])
let(r3=p3[0]+r2,a3=p3[1],t3=p3[2])
let(X=a1*cos(r1*t+t1)+a2*cos(r2*t+t2)+a3*cos(r3*t+t3))
let(Y=a1*sin(r1*t+t1)+a2*sin(r2*t+t2)+a3*sin(r3*t+t3))
let(r=sqrt(X*X+Y*Y))
let(Z=pow(r/7,3))
[X,Y,Z]
;
// utility functions
function m_translate(v) = [ [1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[v.x, v.y, v.z, 1 ] ];
function m_rotate(v) = [ [1, 0, 0, 0],
[0, cos(v.x), sin(v.x), 0],
[0, -sin(v.x), cos(v.x), 0],
[0, 0, 0, 1] ]
* [ [ cos(v.y), 0, -sin(v.y), 0],
[0, 1, 0, 0],
[ sin(v.y), 0, cos(v.y), 0],
[0, 0, 0, 1] ]
* [ [ cos(v.z), sin(v.z), 0, 0],
[-sin(v.z), cos(v.z), 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, 0, 1] ];
function vec3(v) = [v.x, v.y, v.z];
function transform(v, m) = vec3([v.x, v.y, v.z, 1] * m);
function orient_to(centre,normal, p) = m_rotate([0, atan2(sqrt(pow(normal.x, 2) + pow(normal.y, 2)), normal.z), 0])
* m_rotate([0, 0, atan2(normal[1], normal[0])])
* m_translate(centre);
// solid from path
function circle_points(r, sides,phase=45) =
let (delta = 360/sides)
[for (i=[0:sides-1]) [r * sin(i*delta + phase), r * cos(i*delta+phase), 0]];
function path_points(step,min=0,max=360,params) =
[for (t=[min:step:max-step]) f(t,params)];
function transform_points(list, matrix, i = 0) =
i < len(list)
? concat([ transform(list[i], matrix) ], transform_points(list, matrix, i + 1))
: [];
function tube_points(loop, circle_points, i = 0) =
(i < len(loop) - 1)
? concat(transform_points(circle_points, orient_to(loop[i], loop[i + 1] - loop[i] )),
tube_points(loop, circle_points, i + 1))
: transform_points(circle_points, orient_to(loop[i], loop[0] - loop[i] )) ;
function loop_faces(segs, sides, open=false) =
open
? concat(
[[for (j=[sides - 1:-1:0]) j ]],
[for (i=[0:segs-3])
for (j=[0:sides -1])
[ i * sides + j,
i * sides + (j + 1) % sides,
(i + 1) * sides + (j + 1) % sides,
(i + 1) * sides + j
]
] ,
[[for (j=[0:1:sides - 1]) (segs-2)*sides + j]]
)
: [for (i=[0:segs])
for (j=[0:sides -1])
[ i * sides + j,
i * sides + (j + 1) % sides,
((i + 1) % segs) * sides + (j + 1) % sides,
((i + 1) % segs) * sides + j
]
]
;
// create a 3D objec by hulling spheres
module hull_path(path,r) {
for (i = [0 : len(path) - 1 ]) {
hull() {
translate(path[i]) sphere(r);
translate(path[(i + 1) % len(path)]) sphere(r);
}
}
};
// create a polyhedron from a path
function poly_path(path,r,sides,phase=45,open=false) =
let(circle_points = circle_points(r,sides,phase))
let(tube_points = tube_points(path,circle_points))
let(loop_faces = loop_faces(len(path),sides,open))
[tube_points,loop_faces];
// create a 2d object from a path by hulling circles
module plane_path(path,thickness=0.5,open=0) {
for(i =[0:len(path)-1-open]) {
hull() {
translate(path[i]) circle(d=thickness);
translate(path[(i+1) % len(path)]) circle(d=thickness);
}
}
}
// object trimming
module ground(z=200) {
translate([0,0,-z]) cube(z*2,center=true);
}
module sky(z=200) {
rotate([0,180,0]) ground(z);
}