forked from tobbelobb/hangprinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep.scad
183 lines (148 loc) · 4.82 KB
/
sweep.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
// sweep.scad
//
// This code is copied from
// https://github.com/openscad/list-comprehension-demos
// and
// https://github.com/openscad/scad-utils
// to make hangprinter repo self-contained
// It was included in order to make the worm drive parametric
// In future OpenSCAD (or other script CAD programs)
// worm drives should be will be more easily constructed without
// this user space defined sweep operation.
// See OpenSCAD discussion at:
// https://github.com/openscad/openscad/issues/114
// Look into files
// lists.scad
// X transformations.scad
// linalg.scad
// se3.scad
// so3.scad
// Include modules and fcts:
/// linalg.scad
function vec3(p) = len(p) < 3 ? concat(p,0) : p;
function vec4(p) = let (v3=vec3(p)) len(v3) < 4 ? concat(v3,1) : v3;
function take3(v) = [v[0],v[1],v[2]];
function tail3(v) = [v[3],v[4],v[5]];
function construct_Rt(R,t) = [concat(R[0],t[0]),concat(R[1],t[1]),concat(R[2],t[2]),[0,0,0,1]];
/// so3.scad
function so3_exp_2(theta_sq) = [
1.0 - theta_sq * (1.0 - theta_sq/20) / 6,
0.5 - 0.25/6 * theta_sq
];
// Taylor series expansions close to 0
function so3_exp_1(theta_sq) = [
1 - 1/6*theta_sq,
0.5
];
function rodrigues_so3_exp(w, A, B) = [
[1.0 - B*(w[1]*w[1] + w[2]*w[2]), B*(w[0]*w[1]) - A*w[2], B*(w[0]*w[2]) + A*w[1]],
[B*(w[0]*w[1]) + A*w[2], 1.0 - B*(w[0]*w[0] + w[2]*w[2]), B*(w[1]*w[2]) - A*w[0]],
[B*(w[0]*w[2]) - A*w[1], B*(w[1]*w[2]) + A*w[0], 1.0 - B*(w[0]*w[0] + w[1]*w[1])]
];
function so3_exp_3_0(theta_deg, inv_theta) = [
sin(theta_deg) * inv_theta,
(1 - cos(theta_deg)) * (inv_theta * inv_theta)
];
/// se3.scad
function se3_exp_2(t,w) = se3_exp_2_0(t,w,w*w);
function se3_exp_2_0(t,w,theta_sq) =
se3_exp_23(
so3_exp_2(theta_sq),
C = (1.0 - theta_sq/20) / 6,
t=t,w=w);
function combine_se3_exp(w, ABt) = construct_Rt(rodrigues_so3_exp(w, ABt[0], ABt[1]), ABt[2]);
function se3_exp_1(t,w) = concat(
so3_exp_1(w*w),
[t + 0.5 * cross(w,t)]
);
function se3_exp_3(t,w) = se3_exp_3_0(t,w,sqrt(w*w)*180/PI,1/sqrt(w*w));
function se3_exp_23(AB,C,t,w) =
[AB[0], AB[1], t + AB[1] * cross(w,t) + C * cross(w,cross(w,t)) ];
function se3_exp_3_0(t,w,theta_deg,inv_theta) =
se3_exp_23(
so3_exp_3_0(theta_deg = theta_deg, inv_theta = inv_theta),
C = (1 - sin(theta_deg) * inv_theta) * (inv_theta * inv_theta),
t=t,w=w);
function se3_exp(mu) = se3_exp_0(t=take3(mu),w=tail3(mu)/180*PI);
function se3_exp_0(t,w) =
combine_se3_exp(w,
// Evaluate by Taylor expansion when near 0
w*w < 1e-8
? se3_exp_1(t,w)
: w*w < 1e-6
? se3_exp_2(t,w)
: se3_exp_3(t,w)
);
/// lists.scad
/*!
Flattens a list one level:
flatten([[0,1],[2,3]]) => [0,1,2,3]
*/
function flatten(list) = [ for (i = list, v = i) v ];
/*!
Extracts a subarray from index begin (inclusive) to end (exclusive)
subarray([1,2,3,4], 1, 2) => [2,3]
*/
function subarray(list,begin=0,end=-1) = [
let(end = end < 0 ? len(list) : end)
for (i = [begin : 1 : end-1])
list[i]
];
/// transformations.scad
//********* Create modifying matrices ***************//
/*!
Creates a rotation matrix
xyz = euler angles = rz * ry * rx
axis = rotation_axis * rotation_angle
*/
function rotation(xyz=undef, axis=undef) =
xyz != undef && axis != undef ? undef :
xyz == undef ? se3_exp([0,0,0,axis[0],axis[1],axis[2]]) :
len(xyz) == undef ? rotation(axis=[0,0,xyz]) :
(len(xyz) >= 3 ? rotation(axis=[0,0,xyz[2]]) : identity4()) *
(len(xyz) >= 2 ? rotation(axis=[0,xyz[1],0]) : identity4()) *
(len(xyz) >= 1 ? rotation(axis=[xyz[0],0,0]) : identity4());
/*!
Creates a scaling matrix
*/
function scaling(v) = [
[v[0],0,0,0],
[0,v[1],0,0],
[0,0,v[2],0],
[0,0,0,1],
];
/*!
Creates a translation matrix
*/
function translation(v) = [
[1,0,0,v[0]],
[0,1,0,v[1]],
[0,0,1,v[2]],
[0,0,0,1],
];
function project(x) = subarray(x,end=len(x)-1) / x[len(x)-1];
function transform(m, list) = [for (p=list) project(m * vec4(p))];
function to_3d(list) = [ for(v = list) vec3(v) ];
module sweep(shape, path_transforms, closed=false) {
pathlen = len(path_transforms);
segments = pathlen + (closed ? 0 : -1);
shape3d = to_3d(shape);
function sweep_points() =
flatten([for (i=[0:pathlen-1])
transform(path_transforms[i], shape3d)]);
function loop_faces() = [let (facets=len(shape3d))
for(s=[0:segments-1], i=[0:facets-1])
[(s%pathlen) * facets + i,
(s%pathlen) * facets + (i + 1) % facets,
((s + 1) % pathlen) * facets + (i + 1) % facets,
((s + 1) % pathlen) * facets + i]];
bottom_cap = closed ? [] : [[for (i=[len(shape3d)-1:-1:0]) i]];
top_cap = closed ? [] : [
[for (i=[0:len(shape3d)-1])
i+len(shape3d)*(pathlen-1)]];
polyhedron(points = sweep_points(),
faces = concat(loop_faces(),
bottom_cap,
top_cap),
convexity=5);
}