|
1 // Naca4_sweep.scad - sweep library |
|
2 // Code: Rudolf Huttary, Berlin |
|
3 // June 2015 |
|
4 // commercial use prohibited |
|
5 |
|
6 use <Naca4.scad> |
|
7 |
|
8 //example1(); |
|
9 //rotate([80, 180, 130]) |
|
10 example(); |
|
11 |
|
12 // sweep from NACA1480 to NACA6480 (len = 230 mm, winding y,z = 80° |
|
13 // sweeps generates a single polyhedron from multiple datasets |
|
14 module example() |
|
15 { |
|
16 N = 40; |
|
17 sweep(gen_dat(N=5, dz=1,N=N)); |
|
18 |
|
19 // specific generator function |
|
20 function gen_dat(M=10,dz=.1,N=10) = [for (i=[1:dz:M]) |
|
21 let( L = length(i)) |
|
22 let( af = vec3D( |
|
23 airfoil_data([.1,.5,thickness(i)], L=length(i), N = N))) |
|
24 T_(-L/2, 0, (i+1)*2, af)]; // translate airfoil |
|
25 |
|
26 function thickness(i) = .5*sin(i*i)+.1; |
|
27 function length(i) = (60+sin(12*(i-3))*30); |
|
28 } |
|
29 |
|
30 module help() |
|
31 { |
|
32 echo(str("\n\nList of signatures in lib:\n=================\n", |
|
33 "sweep(dat, convexity = 5, showslices = false, plaincaps = true) // dat - vec of vec2, with vec1 = airfoil_data\n", |
|
34 "function vec3D(v, z=0) // expand vec2 to vec3", |
|
35 "function rot(w=0, p) // rotate vec2", |
|
36 "function T_(x=0, y=0, z=0, v) // translates vec of vec3\n", |
|
37 "function R_(x=0, y=0, z=0, v) // rotates vec of vec3\n", |
|
38 "function Rx_(x=0, v) // x-rotates vec of vec3\n", |
|
39 "function Ry_(y=0, v) // y-rotates vec of vec3\n", |
|
40 "function Rz_(z=0, v) // z-rotates vec of vec3\n", |
|
41 "function T_(x=0, y=0, z=0, v) // translates vec of vec3\n", |
|
42 "function Tx_(x=0, v) // x-translates vec of vec3\n", |
|
43 "function Ry_(y=0, v) // y-translates vec of vec3\n", |
|
44 "function Rz_(z=0, v) // z-translates vec of vec3\n", |
|
45 "function S_(x=0, y=0, z=0, v) // scales vec of vec3\n", |
|
46 "function Sx_(x=0, v) // x-translates vec of vec3\n", |
|
47 "function Sy_(x=0, v) // y-translates vec of vec3\n", |
|
48 "function Sz_(x=0, v) // z-translates vec of vec3\n", |
|
49 "=================\n")); |
|
50 } |
|
51 |
|
52 |
|
53 // generate polyhedron from multiple airfoil_datasets |
|
54 // dat - vec of vec1, with vec1 = simple polygon like airfoil_data, > 3 points per dataset expected |
|
55 module sweep(dat, convexity = 5, showslices = false, plaincaps = true) |
|
56 { |
|
57 n = len(dat); // # datasets |
|
58 l = len(dat[0]); // points per dataset |
|
59 if(l<=3) |
|
60 echo("ERROR: sweep() expects more than 3 points per dataset"); |
|
61 else |
|
62 { |
|
63 if(n==1) |
|
64 polyhedron(points = dat[0], faces = [count(l-1, 0)]); |
|
65 else{ |
|
66 first = plaincaps?[count(l-1, 0)]: |
|
67 faces_polygon(l, true); // triangulate first dataset |
|
68 last = plaincaps?[count((n-1)*l,(n)*l-1)]: |
|
69 faces_shift((n-2)*l, faces_polygon(l, false)); // triangulate last dataset |
|
70 if (showslices) |
|
71 for(i=[0:n-1]) |
|
72 sweep([dat[i]]); |
|
73 else |
|
74 if (n<2) // this case is also used recursively for showslices |
|
75 polyhedron(points = flat(), faces = last, convexity = 5); |
|
76 else |
|
77 { |
|
78 polyhedron(points = flat(), |
|
79 faces = concat(first, last, faces_sweep(l,n)), convexity = 5); |
|
80 } |
|
81 } |
|
82 } |
|
83 function count(a, b) = let(st = (a<b?1:-1))[for (i=[a:st:b]) i]; |
|
84 function faces_shift(d, dat) = [for (i=[0:len(dat)-1]) dat[i] + [d, d, d]]; |
|
85 function flat() = [for (i=[0:n-1], j=[0:l-1]) dat[i][j]]; |
|
86 } |
|
87 |
|
88 function del(A, n) = [for(i=[0:len(A)-1]) if (n!=i)A[i]]; |
|
89 |
|
90 //// composition stuff for polyhedron |
|
91 function faces_sweep(l, n=1) = let(M = n*l) |
|
92 concat([[0,l,l-1]], // first face |
|
93 [for (i=[0:l*(n-1)-2], j = [0,1]) |
|
94 j==0? [i, i+1, (i+l)] : [i+1, (i+l+1), i+l]], |
|
95 [[n*l-1, (n-1)*l-1, (n-1)*l]]) // last face |
|
96 ; |
|
97 |
|
98 function faces_polygon(l, first = true) = let(odd = (l%2==1), d=first?0:l) |
|
99 let(res = odd?concat([[d,d+1,d+l-1]], |
|
100 [for (i=[1:(l-3)/2], j=[0,1])(j==0)?[d+i,d+i+1,d+l-i]:[d+i+1,d+l-i-1, d+l-i]]): |
|
101 [for (i=[0:(l-4)/2], j=[0,1])(j==0)?[d+i,d+i+1,d+l-i-1]:[d+i+1,d+l-i-2, d+l-i-1]]) |
|
102 first?facerev(res):res; |
|
103 |
|
104 function facerev(dat) = [for (i=[0:len(dat)-1]) [dat[i][0],dat[i][2],dat[i][1]]]; |
|
105 |
|
106 |
|
107 |
|
108 //// vector and vector set operation stuff /////////////////////// |
|
109 //// Expand 2D vector into 3D |
|
110 function vec3D(v, z=0) = [for(i = [0:len(v)-1]) |
|
111 len(v[i])==2?[v[i][0], v[i][1], z]:v[i]+[0, 0, z]]; |
|
112 |
|
113 // Translation - 1D, 2D, 3D point vector ////////////////////////// |
|
114 // vector along all axes |
|
115 function T_(x=0, y=0, z=0, v) = let(x_ = (len(x)==3)?x:[x, y, z]) |
|
116 [for (i=[0:len(v)-1]) T__(x_[0], x_[1], x_[2], p=v[i])]; |
|
117 /// vector along one axis |
|
118 function Tx_(x=0, v) = T_(x=x, v=v); |
|
119 function Ty_(y=0, v) = T_(y=y, v=v); |
|
120 function Tz_(z=0, v) = T_(z=z, v=v); |
|
121 /// point along all axes 1D, 2D, 3D allowed |
|
122 function T__(x=0, y=0, z=0, p) = len(p)==3?p+[x, y, z]:len(p)==2?p+[x, y]:p+x; |
|
123 |
|
124 //// Rotation - 2D, 3D point vector /////////////////////////////////// |
|
125 // vector around all axes |
|
126 function R_(x=0, y=0, z=0, v) = // 2D vectors allowed |
|
127 let(x_ = (len(x)==3)?x:[x, y, z]) |
|
128 len(v[0])==3?Rx_(x_[0], Ry_(x_[1], Rz_(x_[2], v))): |
|
129 [for(i = [0:len(v)-1]) rot(x_[2], v[i])]; |
|
130 // vector around one axis |
|
131 function Rx_(w, A) = A*[[1, 0, 0], [0, cos(w), sin(w)], [0, -sin(w), cos(w)]]; |
|
132 function Ry_(w, A) = A*[[cos(w), 0, sin(w)], [0, 1, 0], [-sin(w), 0, cos(w)]]; |
|
133 function Rz_(w, A) = A*[[cos(w), sin(w), 0], [-sin(w), cos(w), 0], [0, 0, 1]]; |
|
134 |
|
135 |
|
136 //// Scale - 2D, 3D point vector /////////////////////////////////// |
|
137 // vector along all axes |
|
138 function S_(x=1, y=1, z=1, v) = |
|
139 [for (i=[0:len(v)-1]) S__(x,y,z, v[i])]; |
|
140 // vector along one axis |
|
141 function Sx_(x=0, v) = S_(x=x, v=v); |
|
142 function Sy_(y=0, v) = S_(y=y, v=v); |
|
143 function Sz_(z=0, v) = S_(z=z, v=v); |
|
144 // single point in 2D |
|
145 function S__(x=1, y=1, z=1, p) = |
|
146 len(p)==3?[p[0]*x, p[1]*y, p[2]*z]:len(p)==2?[p[0]*x+p[1]*y]:[p[0]*x]; |
|
147 |
|
148 |
|
149 |