chiark / gitweb /
bbe70d93ad7c195288fdbf1066ceedc62c748333
[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 = 4;
20 my $sine_size = 10;
21 my $sine_angle = 1.2 * 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 20
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, $i_outward);
53
54     $i_outward = V( 0,
55                     ($ip =~ m/0$/ ? -1 : +1),
56                     0 );
57
58     my $i_thickscale = 1.0;
59     my $sine_len = $sine_size * sin($sine_angle);
60     my $sine_height = $sine_size * (1 - cos($sine_angle));
61
62     if ($ip =~ m/^lin[01]$/) {
63         $i_offset = V( -$lin_len * $it,
64                        0,
65                        0 );
66     } elsif ($ip =~ m/^circle$/) {
67         $i_offset = V( 0,0,0 );
68         $i_outward = V(  sin($it * TAU/2),
69                          -cos($it * TAU/2),
70                          0 );
71     } elsif ($ip =~ m/^sine[01]$/) {
72         my $angle = $it * $sine_angle;
73         $i_offset = V( -$sine_size * sin($angle) -$lin_len,
74                        0,
75                        +$sine_size * (1 - cos($angle))
76                      );
77     } elsif ($ip =~ m/^ball[02]$/) {
78         my $angle = $it * TAU/4;
79         my $dx = sin($angle) * $ballend_xr;
80         $i_offset = V( -$lin_len -$sine_len - $dx,
81                        0,
82                        +$sine_height + $dx * tan($sine_angle)
83                      );
84         $i_thickscale = cos($angle);
85     } else {
86         die "$ip ?";
87     }
88
89     my $j_plus_th = $jp =~ m/2$/ ? $thick : 0;
90
91     my $i_thick = $thick * $i_thickscale;
92     my $j_p_x = $small_dia/2 + $thick/2;
93     my $j_rs_x = $large_dia/2 + $thick/2;
94     my $j_dqr_x = (1-cos($slope_angle)) * $jcurverad;
95     my $j_q_x = $j_rs_x - $j_dqr_x;
96     my $j_dpq = ($j_q_x - $j_p_x) / asin($slope_angle);
97     #print STDERR "($j_q_x - $j_p_x) / asin($slope_angle); => $j_dpq\n";
98     my $j_p_y = 0;
99     my $j_q_y = $j_p_y + $j_dpq * cos($slope_angle);
100     my $j_r_y = $j_q_y + sin($slope_angle) * $jcurverad;
101     my $j_s_y = $tall;
102     my $j_qrc_x = $j_rs_x - $jcurverad;
103     my $j_qrc_y = $j_r_y;
104
105     my $j_x;
106     my $j_y;
107
108     if ($jp =~ m/^curveE$/) {
109         my $angle = ($jt + 1) * TAU/2 - $slope_angle;
110         $j_x = $j_p_x + $i_thick/2 * cos($angle);
111         $j_y = $j_p_y + $i_thick/2 * sin($angle);
112     } elsif ($jp =~ m/^curve[12]$/) {
113         my $angle = $slope_angle * $jt;
114         my $outwards = $jp =~ m/1/ ? -1 : +1;
115         $j_x = $j_qrc_x + cos($angle) * ($jcurverad + $outwards * $i_thick/2);
116         $j_y = $j_qrc_y - sin($angle) * ($jcurverad + $outwards * $i_thick/2);
117     } elsif ($jp =~ m/^lin0$/) {
118         $j_x = $j_rs_x + $i_thick * (+0.5 - $jt);
119         $j_y = $j_s_y;
120         $i_offset->[2] = 0;
121     } else {
122         die "$jp ?";
123     }
124
125     if ($i_offset->[0] < 0) {
126         my $base = $large_dia/2 + 3.0*($large_dia - $small_dia)/2;
127         $i_offset->[0] *=
128             ($base - $j_x) /
129             ($base - $small_dia/2);
130     }
131
132 #    print STDERR "@_ $j_x $j_y $i_offset $i_outward\n";
133     return
134         $i_offset +
135         $j_x * $i_outward +
136         V(0,0,1) * $j_y +
137         V(0,0,-$tall) ;
138 }
139
140 sub get_sections_ptvals {
141     my $last_ptval;
142     my @out;
143     while (my $name = shift @_) {
144         if ($name eq '-') {
145             push @out, $last_ptval;
146         } else {
147             my $count = shift @_;
148             my $neg = sub { $_[0] };
149             if ($count < 0) {
150                 $count = -$count;
151                 $neg = sub { 1- $_[0] };
152             }
153             foreach (my $ix = 0; $ix < $count; $ix++) {
154                 push @out, [ $name, $neg->($ix/$count) ];
155             }
156             $last_ptval = [ $name, $neg->(1.0) ];
157         }
158     }
159     return @out;
160 }
161
162 our @points;
163 our %point_indices;
164 our @triangles;
165
166 sub triangle {
167     my @pixs;
168     foreach my $pval (@_) {
169         my $pix = $point_indices{$pval}
170             //= ((push @points, $pval), $#points);
171         return if grep { $pix eq $_ } @pixs;
172         push @pixs, $pix;
173     }
174     push @triangles, \@pixs;
175 }
176
177 sub make_sheet () {
178     my @ipts = get_sections_ptvals(@i_sections);
179     my @jpts = get_sections_ptvals(@j_sections);
180     my @sheet;
181     foreach my $ipt (@ipts) {
182         my @row = ();
183         foreach my $jpt (@jpts) {
184             push @row, &point(@$ipt, @$jpt);
185         }
186         push @sheet, \@row;
187     }
188     foreach (my $qi=0; $qi<$#ipts; $qi++) { # i direction does not wrap
189         my $qi2 = $qi+1;
190         foreach (my $qj=0; $qj<@jpts; $qj++) { # j direction does wrap
191             my $qj2 = ($qj+1) % @jpts;
192             my $p0 = $sheet[$qi][$qj];
193             triangle($p0, $sheet[$qi2][$qj], $sheet[$qi2][$qj2]);
194             triangle($p0, $sheet[$qi2][$qj2], $sheet[$qi][$qj2]);
195         }
196     }
197 }
198
199 sub pv ($) {
200     my $v = shift @_;
201     return "[".(join ',', @$v)."]";
202 }
203
204 sub write_out () {
205     print "module ImplHeadCup(){ polyhedron(points=[\n" or die $!;
206     print pv($_),",\n" or die $! foreach @points;
207     print "],faces=[\n" or die $!;
208     print pv($_),",\n" or die $! foreach @triangles;
209     print "],convexity=10); }\n" or die $!;
210     print <<END or die $!;
211 implheadcup_large_dia = $large_dia;
212 implheadcup_thick     = $thick;
213 END
214 }
215
216 make_sheet();
217 write_out();