5 use Math::Vector::Real;
8 use constant tau => pi*2;
14 my $N = 180; # around ellipse
15 my $M = 80; # around each circle
16 my @channeldistprops = (0, 1/3, 2/3);
18 my $NMdiv = $ENV{'LEMONSTAND_COARSE'} || 1;
25 // *** AUTOGENERATED - DO NOT EDIT ***
28 print "torusyup = ", ($circle / sqrt(2)), ";\n";
31 my $theta = tau * $_ / $N;
32 V( cos($theta) * $ellipse * $xscale, sin($theta) * $ellipse, 0 )
35 #print Dumper(\@ellipse);
39 $ellipse[ ($i+1) % $N ] - $ellipse[ ($i-1) % $N];
44 my $centre = $ellipse[$i];
45 my $axis = $alongs[$i]->versor();
46 my $rad0 = $axis x V(0,0,1);
47 my $rad1 = $rad0 x $axis;
49 my $theta = tau * $_ / $M;
50 $centre + $circle * ($rad0 * cos($theta) + $rad1 * sin($theta));
56 return "[ ".(join ", ", @$v)." ]"
60 print "module Torusy(){ polyhedron(points=[";
63 foreach my $i (0..$N-1) {
64 foreach my $j (0..$M-1) {
67 print " ",(scadvec $circles[$i][$j]);
68 $cirpt[$i][$j] = $ptix++;
74 foreach my $i (0..$N-1) {
76 foreach my $j (0..$M-1) {
78 print "," if $i || $j;
80 print " [ ", (join ", ",
85 print " [ ", (join ", ",
100 foreach my $i (1..$N) {
101 my $dist = $distances[ $i-1 ];
102 $dist += abs($ellipse[$i % $N] - $ellipse[$i-1]);
103 $distances[$i] = $dist;
106 sub infodistprop ($) {
109 # ( $ellipse_centreline_point,
111 my $dist = $distprop * $distances[$N];
112 foreach my $i (0..$N-1) {
114 ($dist - $distances[$i]) /
115 ($distances[$i+1] - $distances[$i]);
116 next unless 0 <= $prorata && $prorata <= 1;
117 print "// infodistprop $distprop => #$i=$ellipse[$i] $prorata $ellipse[$i+1]\n";
119 (1-$prorata) * $ellipse[$i] + ($prorata) * $ellipse[$i+1],
127 print "module Channels(){\n";
130 (map { 0.5 * $_ } @channeldistprops),
131 (map { 0.5 * ($_+1) } @channeldistprops),
133 my ($ctr, $along) = infodistprop($cdp);
134 my $angle = atan2(-$along->[0], $along->[1]);
135 print " translate(",scadvec($ctr),")\n";
136 print " rotate([0,0,$angle*360/",tau,"])\n";
137 print " rotate([0,90,0])\n";
138 print " translate([0,0, -2*$circle])\n";
139 print " rotate([0,0,360/8/2])\n";
140 print " cylinder(r=$channel, h=4*$circle, \$fn=8);\n";
147 while (<DATA>) { print };
149 STDOUT->error and die $!;
150 STDOUT->flush or die $!;
155 translate([0,0,torusyup])
158 translate([-200,-200,-50])