5 use Math::Vector::Real;
8 use constant tau => pi*2;
15 my $N = 180; # around ellipse
16 my $M = 80; # around each circle
17 my @channeldistprops = (0, 1/3, 2/3);
19 my $NMdiv = $ENV{'LEMONSTAND_COARSE'} || 1;
26 // *** AUTOGENERATED - DO NOT EDIT ***
29 print "torusyup = ", ($circle / sqrt(2)), ";\n";
32 my $theta = tau * $_ / $N;
33 V( cos($theta) * $ellipse * $xscale, sin($theta) * $ellipse, 0 )
36 #print Dumper(\@ellipse);
40 $ellipse[ ($i+1) % $N ] - $ellipse[ ($i-1) % $N];
45 my $centre = $ellipse[$i];
46 my $axis = $alongs[$i]->versor();
47 my $rad0 = $axis x V(0,0,1);
48 my $rad1 = $rad0 x $axis;
50 my $theta = tau * $_ / $M;
51 $centre + $circle * ($rad0 * cos($theta) + $rad1 * sin($theta));
57 return "[ ".(join ", ", @$v)." ]"
61 print "module Torusy(){ polyhedron(points=[";
64 foreach my $i (0..$N-1) {
65 foreach my $j (0..$M-1) {
68 print " ",(scadvec $circles[$i][$j]);
69 $cirpt[$i][$j] = $ptix++;
75 foreach my $i (0..$N-1) {
77 foreach my $j (0..$M-1) {
79 print "," if $i || $j;
81 print " [ ", (join ", ",
86 print " [ ", (join ", ",
101 foreach my $i (1..$N) {
102 my $dist = $distances[ $i-1 ];
103 $dist += abs($ellipse[$i % $N] - $ellipse[$i-1]);
104 $distances[$i] = $dist;
107 sub infodistprop ($) {
110 # ( $ellipse_centreline_point,
112 my $dist = $distprop * $distances[$N];
113 foreach my $i (0..$N-1) {
115 ($dist - $distances[$i]) /
116 ($distances[$i+1] - $distances[$i]);
117 next unless 0 <= $prorata && $prorata <= 1;
118 print "// infodistprop $distprop => #$i=$ellipse[$i] $prorata $ellipse[$i+1]\n";
120 (1-$prorata) * $ellipse[$i] + ($prorata) * $ellipse[$i+1],
128 print "module Channels(){\n";
131 (map { 0.5 * $_ } @channeldistprops),
132 (map { 0.5 * ($_+1) } @channeldistprops),
134 my ($ctr, $along) = infodistprop($cdp);
135 my $angle = atan2(-$along->[0], $along->[1]);
136 print " translate(",scadvec($ctr),")\n";
137 print " rotate([0,0,$angle*360/",tau,"])\n";
138 print " rotate([0,90,0])\n";
139 print " translate([0,0, -2*$circle])\n";
140 print " scale([1, $channelw/$channelh/2, 1])\n";
141 print " rotate([0,0,360/8/2])\n";
142 print " cylinder(r=$channelh, h=4*$circle, \$fn=8);\n";
149 while (<DATA>) { print };
151 STDOUT->error and die $!;
152 STDOUT->flush or die $!;
157 translate([0,0,torusyup])
160 translate([-200,-200,-50])