-
Notifications
You must be signed in to change notification settings - Fork 41
/
pei-1.scad
186 lines (151 loc) · 5.6 KB
/
pei-1.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
// create a tube as a polyhedron
// tube can be open or closed
// polyhedron constructor
function poly(name,vertices,faces,debug=[],partial=false) =
[name,vertices,faces,debug,partial];
function p_name(obj) = obj[0];
function p_vertices(obj) = obj[1];
function p_faces(obj) = obj[2];
module show_solid(obj) {
polyhedron(p_vertices(obj),p_faces(obj),convexity=10);
};
// 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 loop_points(step,min=0,max=360) =
[for (t=[min:step:max-step]) f(t)];
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
]
]
;
// path with hulls
module hulled_path(path,r) {
for (i = [0 : 1 : len(path) - 1 ]) {
hull() {
translate(path[i]) sphere(r);
translate(path[(i + 1) % len(path)]) sphere(r);
}
}
};
// smoothed path by interpolate between points
weight = [-1, 9, 9, -1] / 16;
function interpolate(path,n,i) =
path[(i + n - 1) %n] * weight[0] +
path[i] * weight[1] +
path[(i + 1) %n] * weight[2] +
path[(i + 2) %n] * weight[3] ;
function subdivide(path,i=0) =
i < len(path)
? concat([path[i]],
[interpolate(path,len(path),i)],
subdivide(path, i+1))
: [];
function smooth(path,n) =
n == 0
? path
: smooth(subdivide(path),n-1);
function path_segment(path,start,end) =
let (l = len(path))
let (s = max(floor(start * 360 / l),0),
e = min(ceil(end * 360 / l),l - 1))
[for (i=[s:e]) path[i]];
function scale(path,scale,i=0) =
i < len(path)
? concat(
[[path[i][0]*scale[0],path[i][1]*scale[1],path[i][2]*scale[2]]],
scale(path,scale,i+1)
)
: [];
function curve_length(step,t=0) =
t < 360
? norm(f(t+step) - f(t)) + curve_length(step,t+step)
: 0;
function map(t, min, max) =
min + t* (max-min)/360;
function radians (deg) = deg* 3.14159/180;
function degrees (rad) = rad*180/ 3.14159;
// create a knot from a path
function path_knot(path,r,sides,kscale,phase=45,open=false) =
let(loop_points = scale(path,kscale))
let(circle_points = circle_points(r,sides,phase))
let(tube_points = tube_points(loop_points,circle_points))
let(loop_faces = loop_faces(len(loop_points),sides,open))
poly(name="Knot",
vertices = tube_points,
faces = loop_faces);
function f(t) =
let (r = 1/(1.3 + sin(7*t)))
let (theta =degrees(radians(t)+ sin(7 *t )))
[r * cos(theta),
r* sin(theta),
r*r/5
];
Scale=40;
Sides=20; // Sides of rope - must be a divisor of 360
Phase = 45; // phase angle for profile (maters for low Sides
Kscale=[1,1,1]; // x,y,z scaling
R=0.07; // Rope diameter
Step=0.5 ; // decrease for finer details
Open = false; // true if knot is open or partial
Start=0; End=360; // change for partial path
colours = ["red","green","blue","yellow","pink"];
Paths = [loop_points(Step,Start,End)];
scale(Scale)
for (k =[0:len(Paths) - 1]) {
path = Paths[k];
knot= path_knot(path,R,Sides,Kscale,Phase,Open);
solid1=knot; // apply transformations here
color(colours[k])
show_solid(solid1);
};