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