5 use Math::Vector::Real;
8 use constant tau => pi*2;
14 my $N = 30; #180; # around ellipse
15 my $M = 20; #80; # around each circle
16 my @channeldistprops = (0, 1/3, 2/3);
20 // *** AUTOGENERATED - DO NOT EDIT ***
23 print "torusyup = ", ($circle / sqrt(2)), ";\n";
24 print "channelstopup = ", ($channel / sqrt(2)), ";\n";
27 my $theta = tau * $_ / $N;
28 V( cos($theta) * $ellipse * $xscale, sin($theta) * $ellipse, 0 )
31 #print Dumper(\@ellipse);
35 $ellipse[ ($i+1) % $N ] - $ellipse[ ($i-1) % $N];
40 my $centre = $ellipse[$i];
41 my $axis = $alongs[$i]->versor();
42 my $rad0 = $axis x V(0,0,1);
43 my $rad1 = $rad0 x $axis;
45 my $theta = tau * $_ / $M;
46 $centre + $circle * ($rad0 * cos($theta) + $rad1 * sin($theta));
52 return "[ ".(join ", ", @$v)." ]"
56 print "module Torusy(){ polyhedron(points=[";
59 foreach my $i (0..$N-1) {
60 foreach my $j (0..$M-1) {
63 print " ",(scadvec $circles[$i][$j]);
64 $cirpt[$i][$j] = $ptix++;
70 foreach my $i (0..$N-1) {
72 foreach my $j (0..$M-1) {
74 print "," if $i || $j;
76 print " [ ", (join ", ",
81 print " [ ", (join ", ",
96 foreach my $i (1..$N) {
97 my $dist = $distances[ $i-1 ];
98 $dist += abs($ellipse[$i % $N] - $ellipse[$i-1]);
99 $distances[$i] = $dist;
102 sub infodistprop ($) {
105 # ( $ellipse_centreline_point,
107 my $dist = $distprop * $distances[$N];
108 foreach my $i (0..$N-1) {
110 ($dist - $distances[$i]) /
111 ($distances[$i+1] - $distances[$i]);
112 next unless 0 <= $prorata && $prorata <= 1;
113 print "// infodistprop $distprop => #$i=$ellipse[$i] $prorata $ellipse[$i+1]\n";
115 (1-$prorata) * $ellipse[$i] + ($prorata) * $ellipse[$i+1],
123 print "module Channels(){\n";
126 (map { 0.5 * $_ } @channeldistprops),
127 (map { 0.5 * ($_+1) } @channeldistprops),
129 my ($ctr, $along) = infodistprop($cdp);
130 my $angle = atan2(-$along->[0], $along->[1]);
131 print " translate(",scadvec($ctr),")\n";
132 print " rotate([0,0,$angle*360/",tau,"])\n";
133 print " rotate([0,90,0])\n";
134 print " translate([0,0, -2*$circle])\n";
135 print " cylinder(r=$channel, h=4*$circle, \$fn=$M);\n";
142 while (<DATA>) { print };
144 STDOUT->error and die $!;
145 STDOUT->flush or die $!;
150 translate([0,0,torusyup])
154 translate([-200,-200,-50])
155 cube([400,400, 50+channelstopup]);
157 translate([-200,-200,-50])