5 use Math::Vector::Real;
8 use constant tau => pi*2;
14 my $N = 30; # around ellipse
15 my $M = 20; # around each circle
16 my @channeldistprops = (0, 1/3, 2/3);
20 // *** AUTOGENERATED - DO NOT EDIT ***
23 print "torusyup = ", ($circle / sqrt(2)), ";\n";
26 my $theta = tau * $_ / $N;
27 V( cos($theta) * $ellipse * $xscale, sin($theta) * $ellipse, 0 )
30 #print Dumper(\@ellipse);
34 $ellipse[ ($i+1) % $N ] - $ellipse[ ($i-1) % $N];
39 my $centre = $ellipse[$i];
40 my $axis = $alongs[$i]->versor();
41 my $rad0 = $axis x V(0,0,1);
42 my $rad1 = $rad0 x $axis;
44 my $theta = tau * $_ / $M;
45 $centre + $circle * ($rad0 * cos($theta) + $rad1 * sin($theta));
51 return "[ ".(join ", ", @$v)." ]"
55 print "module Torusy(){ polyhedron(points=[";
58 foreach my $i (0..$N-1) {
59 foreach my $j (0..$M-1) {
62 print " ",(scadvec $circles[$i][$j]);
63 $cirpt[$i][$j] = $ptix++;
69 foreach my $i (0..$N-1) {
71 foreach my $j (0..$M-1) {
73 print "," if $i || $j;
75 print " [ ", (join ", ",
80 print " [ ", (join ", ",
95 foreach my $i (1..$N) {
96 my $dist = $distances[ $i-1 ];
97 $dist += abs($ellipse[$i % $N] - $ellipse[$i-1]);
98 $distances[$i] = $dist;
101 sub infodistprop ($) {
104 # ( $ellipse_centreline_point,
106 my $dist = $distprop * $distances[$N];
107 foreach my $i (0..$N-1) {
109 ($dist - $distances[$i]) /
110 ($distances[$i+1] - $distances[$i]);
111 next unless 0 <= $prorata && $prorata <= 1;
112 print "// infodistprop $distprop => #$i=$ellipse[$i] $prorata $ellipse[$i+1]\n";
114 (1-$prorata) * $ellipse[$i] + ($prorata) * $ellipse[$i+1],
122 print "module Channels(){\n";
125 (map { 0.5 * $_ } @channeldistprops),
126 (map { 0.5 * ($_+1) } @channeldistprops),
128 my ($ctr, $along) = infodistprop($cdp);
129 my $angle = atan2(-$along->[0], $along->[1]);
130 print " translate(",scadvec($ctr),")\n";
131 print " rotate([0,0,$angle*360/",tau,"])\n";
132 print " rotate([0,90,0])\n";
133 print " translate([0,0, -2*$circle])\n";
134 print " cylinder(r=$channel, h=4*$circle, \$fn=$M);\n";
141 while (<DATA>) { print };
143 STDOUT->error and die $!;
144 STDOUT->flush or die $!;
149 translate([0,0,torusyup])
152 translate([-200,-200,-50])