chiark / gitweb /
019523ad1c73e2b482f8764f4ccbc3c27f01d25a
[reprap-play.git] / poster-tube-lid-parametric.scad.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 use Math::Vector::Real;
5 use Math::Trig qw(pi);
6 use POSIX;
7 use Data::Dumper;
8
9 sub TAU () { pi*2; }
10
11 my $thick = 2.5;
12
13 my $small_dia = 20;
14 my $large_dia = 30;
15 my $slope_angle = 45 * TAU/360;
16 my $jcurverad = 5;
17 my $tall = 50;
18
19 my $lin_len = 2;
20 my $sine_size = 5;
21 my $sine_angle = 1.20 * TAU/8;
22
23 my $ballend_xr = $thick/2;
24
25 my $skew_slope = 0.7;
26
27 my @i_sections = qw(ball0  -6
28                     sine0  -10
29                     -
30                     lin0    2
31                     circle 40
32                     lin1    2
33                     sine1  10
34                     -
35                     ball2   6
36                     -
37                     );
38
39 my @j_sections = qw(lin0    2
40                     -
41                     curve1 10
42                     -
43                     curveE 20
44                     -
45                     curve2 -10
46                     -
47                     );
48
49 sub point ($$$$) {
50     my ($ip,$it, $jp,$jt) = @_;
51
52     my ($i_offset);
53
54     my $i_outward = V( 0,
55                        ($ip =~ m/0$/ ? -1 : +1),
56                        0 );
57
58     my $i_j_y_angle = 0;
59
60     my $i_thickscale = 1.0;
61     my $sine_len = $sine_size * sin($sine_angle);
62     my $sine_height = $sine_size * (1 - cos($sine_angle));
63
64     if ($ip =~ m/^lin[01]$/) {
65         $i_offset = V( -$lin_len * $it,
66                        0,
67                        0 );
68     } elsif ($ip =~ m/^circle$/) {
69         $i_offset = V( 0,0,0 );
70         $i_outward = V(  sin($it * TAU/2),
71                          -cos($it * TAU/2),
72                          0 );
73     } elsif ($ip =~ m/^sine[01]$/) {
74         my $angle = $it * $sine_angle;
75         $i_offset = V( -$lin_len -$sine_size * sin($angle),
76                        0,
77                        +$sine_size * (1 - cos($angle))
78                      );
79         $i_j_y_angle = $angle;
80     } elsif ($ip =~ m/^ball[02]$/) {
81         $i_j_y_angle = $sine_angle;
82         my $angle = $it * TAU/4;
83         my $dx = sin($angle) * $ballend_xr;
84         $i_offset = V( -$lin_len -$sine_len - $dx * cos($sine_angle),
85                        0,
86                        +$sine_height + $dx * sin($sine_angle)
87                      );
88         $i_thickscale = cos($angle);
89     } else {
90         die "$ip ?";
91     }
92
93     my $i_j_y_vect = V( sin($i_j_y_angle),
94                         0,
95                         cos($i_j_y_angle ));
96
97     my $j_plus_th = $jp =~ m/2$/ ? $thick : 0;
98
99     my $i_thick = $thick * $i_thickscale;
100     my $j_p_x = $small_dia/2 + $thick/2;
101     my $j_rs_x = $large_dia/2 + $thick/2;
102     my $j_dqr_x = (1-cos($slope_angle)) * $jcurverad;
103     my $j_q_x = $j_rs_x - $j_dqr_x;
104     my $j_dpq = ($j_q_x - $j_p_x) / asin($slope_angle);
105     #print STDERR "($j_q_x - $j_p_x) / asin($slope_angle); => $j_dpq\n";
106     my $j_p_y = 0;
107     my $j_q_y = $j_p_y + $j_dpq * cos($slope_angle);
108     my $j_r_y = $j_q_y + sin($slope_angle) * $jcurverad;
109     my $j_s_y = $tall;
110     my $j_qrc_x = $j_rs_x - $jcurverad;
111     my $j_qrc_y = $j_r_y;
112
113     my $j_x;
114     my $j_y;
115
116     if ($jp =~ m/^curveE$/) {
117         my $angle = ($jt + 1) * TAU/2 - $slope_angle;
118         $j_x = $j_p_x + $i_thick/2 * cos($angle);
119         $j_y = $j_p_y + $i_thick/2 * sin($angle);
120     } elsif ($jp =~ m/^curve[12]$/) {
121         my $angle = $slope_angle * $jt;
122         my $outwards = $jp =~ m/1/ ? -1 : +1;
123         $j_x = $j_qrc_x + cos($angle) * ($jcurverad + $outwards * $i_thick/2);
124         $j_y = $j_qrc_y - sin($angle) * ($jcurverad + $outwards * $i_thick/2);
125     } elsif ($jp =~ m/^lin0$/) {
126         $j_x = $j_rs_x + $i_thick * (+0.5 - $jt);
127         $j_y = $j_s_y;
128         $i_offset->[2] = 0;
129     } else {
130         die "$jp ?";
131     }
132
133     $j_y -= $j_qrc_y;
134
135     if ($j_y > 0) {
136         $i_j_y_vect = V(0,0,1);
137     }
138
139 #    print STDERR "@_ $j_x $j_y $i_offset $i_outward\n";
140     return
141         $i_offset +
142         $j_x * $i_outward +
143         $i_j_y_vect * $j_y +
144         V(0,0,1) * $j_qrc_y +
145         V(0,0,-$tall) ;
146 }
147
148 sub get_sections_ptvals {
149     my $last_ptval;
150     my @out;
151     while (my $name = shift @_) {
152         if ($name eq '-') {
153             push @out, $last_ptval;
154         } else {
155             my $count = shift @_;
156             my $neg = sub { $_[0] };
157             if ($count < 0) {
158                 $count = -$count;
159                 $neg = sub { 1- $_[0] };
160             }
161             foreach (my $ix = 0; $ix < $count; $ix++) {
162                 push @out, [ $name, $neg->($ix/$count) ];
163             }
164             $last_ptval = [ $name, $neg->(1.0) ];
165         }
166     }
167     return @out;
168 }
169
170 our @points;
171 our %point_indices;
172 our @triangles;
173
174 sub triangle {
175     my @pixs;
176     foreach my $pval (@_) {
177         my $pix = $point_indices{$pval}
178             //= ((push @points, $pval), $#points);
179         return if grep { $pix eq $_ } @pixs;
180         push @pixs, $pix;
181     }
182     push @triangles, \@pixs;
183 }
184
185 sub make_sheet () {
186     my @ipts = get_sections_ptvals(@i_sections);
187     my @jpts = get_sections_ptvals(@j_sections);
188     my @sheet;
189     foreach my $ipt (@ipts) {
190         my @row = ();
191         foreach my $jpt (@jpts) {
192             push @row, &point(@$ipt, @$jpt);
193         }
194         push @sheet, \@row;
195     }
196     foreach (my $qi=0; $qi<$#ipts; $qi++) { # i direction does not wrap
197         my $qi2 = $qi+1;
198         foreach (my $qj=0; $qj<@jpts; $qj++) { # j direction does wrap
199             my $qj2 = ($qj+1) % @jpts;
200             my $p0 = $sheet[$qi][$qj];
201             triangle($p0, $sheet[$qi2][$qj], $sheet[$qi2][$qj2]);
202             triangle($p0, $sheet[$qi2][$qj2], $sheet[$qi][$qj2]);
203         }
204     }
205 }
206
207 sub pv ($) {
208     my $v = shift @_;
209     return "[".(join ',', @$v)."]";
210 }
211
212 sub write_out () {
213     print "module ImplHeadCup(){ polyhedron(points=[\n" or die $!;
214     print pv($_),",\n" or die $! foreach @points;
215     print "],faces=[\n" or die $!;
216     print pv($_),",\n" or die $! foreach @triangles;
217     print "],convexity=10); }\n" or die $!;
218     print <<END or die $!;
219 implheadcup_large_dia = $large_dia;
220 implheadcup_thick     = $thick;
221 END
222 }
223
224 make_sheet();
225 write_out();