Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * based off example from https://openhome.cc/eGossip/OpenSCAD/lib3x-path_extrude.html
- **/
- /*
- *
- * Version 0102a: First release with usability improvements
- * Version 0103a: Fix for level 0 torus radius was calculating incorrectly, also added correct examples for D-4D using the torus center line for rad_p properly.
- */
- use <dotSCAD/src/shape_circle.scad>;
- use <dotSCAD/src/path_extrude.scad>;
- use <dotSCAD/src/util/degrees.scad>;
- function length_3d(lt) =
- let(end = len(lt) - 1)
- end == 0 ? lt[0] :
- let(
- cum_total = [
- for(i = 0, s = 0, is_continue = i < end;
- is_continue;
- is_continue = i < end, s = is_continue ? s + sqrt((lt[i+1][0]-lt[i][0])^2+(lt[i+1][1]-lt[i][1])^2+(lt[i+1][2]-lt[i][2])^2) : undef, i = i + 1) s]
- )
- cum_total[end];
- function single_torus_knot(q, rad, dir, phi_step) =
- [
- for(phi = 0; phi < 6.283185307179586; phi = phi + phi_step)
- let(
- phi_deg = degrees(phi),
- l0_deg = phi_deg,
- l1_deg = phi_deg/q[0],
- l0_pos_x = rad[0]*cos(l0_deg),
- l0_pos_y = 0,
- l0_pos_z = rad[0]*sin(l0_deg)
- )
- [l0_pos_x,
- l0_pos_y,
- l0_pos_z]
- ];
- function double_torus_knot(q, rad, dir, phi_step) =
- [
- for(phi = 0; phi < 6.283185307179586*q[0]; phi = phi + phi_step)
- let(
- phi_deg = degrees(phi),
- l0_deg = -dir[0]*phi_deg,
- l1_deg = phi_deg/q[0],
- l0_pos_x = rad[0]*cos(l0_deg),
- l0_pos_y = 0,
- l0_pos_z = rad[0]*sin(l0_deg),
- l1_pos_x = (l0_pos_x+rad[1])*cos(l1_deg)-l0_pos_y*sin(l1_deg),
- l1_pos_y = (l0_pos_x+rad[1])*sin(l1_deg)+l0_pos_y*cos(l1_deg),
- l1_pos_z = l0_pos_z
- )
- [l1_pos_x,
- l1_pos_y,
- l1_pos_z]
- ];
- function triple_torus_knot(q, rad, dir, phi_step) =
- [
- for(phi = 0; phi < 6.283185307179586*q[0]*q[1]; phi = phi + phi_step)
- let(
- phi_deg = degrees(phi),
- l0_deg = -dir[0]*phi_deg,
- l1_deg = phi_deg/q[0],
- l0_pos_x = rad[0]*cos(l0_deg),
- l0_pos_y = 0,
- l0_pos_z = rad[0]*sin(l0_deg),
- l1_pos_x = (l0_pos_x+rad[1])*cos(l1_deg)-l0_pos_y*sin(l1_deg),
- l1_pos_y = (l0_pos_x+rad[1])*sin(l1_deg)+l0_pos_y*cos(l1_deg),
- l1_pos_z = l0_pos_z,
- l2_deg = dir[1]*phi_deg/q[0]/q[1],
- l2_pos_x = l1_pos_x,
- l2_pos_y = (l1_pos_y+rad[2])*cos(l2_deg)-l1_pos_z*sin(l2_deg),
- l2_pos_z = (l1_pos_y+rad[2])*sin(l2_deg)+l1_pos_z*cos(l2_deg)
- )
- [l2_pos_x,
- l2_pos_y,
- l2_pos_z]
- ];
- function quad_torus_knot(q, rad, dir, phi_step) =
- [
- for(phi = 0; phi < 6.283185307179586*q[0]*q[1]*q[2]; phi = phi + phi_step)
- let(
- phi_deg = degrees(phi),
- l0_deg = -dir[0]*phi_deg,
- l1_deg = phi_deg/q[0],
- l0_pos_x = rad[0]*cos(l0_deg),
- l0_pos_y = 0,
- l0_pos_z = rad[0]*sin(l0_deg),
- l1_pos_x = (l0_pos_x+rad[1])*cos(l1_deg)-l0_pos_y*sin(l1_deg),
- l1_pos_y = (l0_pos_x+rad[1])*sin(l1_deg)+l0_pos_y*cos(l1_deg),
- l1_pos_z = l0_pos_z,
- l2_deg = dir[1]*phi_deg/q[0]/q[1],
- l2_pos_x = l1_pos_x,
- l2_pos_y = (l1_pos_y+rad[2])*cos(l2_deg)-l1_pos_z*sin(l2_deg),
- l2_pos_z = (l1_pos_y+rad[2])*sin(l2_deg)+l1_pos_z*cos(l2_deg),
- l3_deg = -dir[2]*phi_deg/q[0]/q[1]/q[2],
- l3_pos_x = l2_pos_x*cos(l3_deg)-(l2_pos_y+rad[3])*sin(l3_deg),
- l3_pos_y = l2_pos_x*sin(l3_deg)+(l2_pos_y+rad[3])*cos(l3_deg),
- l3_pos_z = l2_pos_z
- )
- [l3_pos_x,
- l3_pos_y,
- l3_pos_z]
- ];
- module single_torus(q, rad_p, rad_c, dir, show_cores, phi_step, base_seg) {
- wire_1_cross_section_radius = rad_c[0];
- wire_1_cross_section_pts = shape_circle(wire_1_cross_section_radius,$fn=base_seg*rad_p[0]);
- pts_1 = single_torus_knot(q, rad_p, dir, phi_step*rad_p[0]);
- echo(pts_1);
- length = length_3d(pts_1);
- echo("Length Wire", length);
- echo("Approx Volume Wire (length*crosssection)", length*3.141592654*rad_c[0]^2);
- path_extrude(
- wire_1_cross_section_pts,
- [each pts_1, pts_1[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- }
- module double_torus(q, rad_p, rad_c, dir, show_cores, phi_step, base_seg) {
- wire_1_cross_section_radius = rad_c[0];
- wire_1_cross_section_pts = shape_circle(wire_1_cross_section_radius,$fn=base_seg*rad_p[0]);
- pts_1 = double_torus_knot(q, rad_p, dir, phi_step*rad_p[0]);
- length = length_3d(pts_1);
- echo("Length Wire", length);
- echo("Approx Volume Wire (length*crosssection)", length*3.141592654*rad_c[0]^2);
- path_extrude(
- wire_1_cross_section_pts,
- [each pts_1, pts_1[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- if (show_cores > 0) {
- wire_2_cross_section_radius = rad_c[1];
- wire_2_cross_section_pts = shape_circle(wire_2_cross_section_radius,$fn=base_seg*rad_p[1]);
- pts_2 = double_torus_knot(q, [0,rad_p[1]], dir, phi_step*rad_p[1]);
- length = length_3d(pts_2);
- echo("Length Core 1", length);
- echo("Approx Volume Core 1 (length*crosssection)", length*3.141592654*rad_c[1]^2);
- color([1,0,0])
- path_extrude(
- wire_2_cross_section_pts,
- [each pts_2, pts_2[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- }
- }
- module triple_torus(q, rad_p, rad_c, dir, show_cores, phi_step, base_seg) {
- wire_1_cross_section_radius = rad_c[0];
- wire_1_cross_section_pts = shape_circle(wire_1_cross_section_radius,$fn=base_seg*rad_p[0]);
- pts_1 = triple_torus_knot(q, rad_p, dir, phi_step*rad_p[0]);
- length = length_3d(pts_1);
- echo("Length Wire", length);
- echo("Approx Volume Wire (length*crosssection)", length*3.141592654*rad_c[0]^2);
- path_extrude(
- wire_1_cross_section_pts,
- [each pts_1, pts_1[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- if (show_cores > 0) {
- wire_2_cross_section_radius = rad_c[1];
- wire_2_cross_section_pts = shape_circle(wire_2_cross_section_radius,$fn=base_seg*rad_p[1]);
- pts_2 = triple_torus_knot(q, [0,rad_p[1],rad_p[2]], dir, phi_step*rad_p[1]);
- length = length_3d(pts_2);
- echo("Length Core 1", length);
- echo("Approx Volume Core 1 (length*crosssection)", length*3.141592654*rad_c[1]^2);
- color([1,0,0])
- path_extrude(
- wire_2_cross_section_pts,
- [each pts_2, pts_2[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- if (show_cores > 1) {
- wire_3_cross_section_radius = rad_c[2];
- wire_3_cross_section_pts = shape_circle(wire_3_cross_section_radius,$fn=base_seg*rad_p[2]);
- pts_3 = triple_torus_knot(q, [0,0,rad_p[2]], dir, phi_step*rad_p[2]);
- length = length_3d(pts_3);
- echo("Length Core 2", length);
- echo("Approx Volume Core 2 (length*crosssection)", length*3.141592654*rad_c[2]^2);
- color([0,1,0])
- path_extrude(
- wire_3_cross_section_pts,
- [each pts_3, pts_3[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- }
- }
- }
- module quad_torus(q, rad_p, rad_c, dir, show_cores, phi_step, base_seg) {
- wire_1_cross_section_radius = rad_c[0];
- wire_1_cross_section_pts = shape_circle(wire_1_cross_section_radius,$fn=base_seg*rad_p[0]);
- pts_1 = quad_torus_knot(q, rad_p, dir, phi_step*rad_p[0]);
- length = length_3d(pts_1);
- echo("Length Wire", length);
- echo("Approx Volume Wire (length*crosssection)", length*3.141592654*rad_c[0]^2);
- path_extrude(
- wire_1_cross_section_pts,
- [each pts_1, pts_1[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- if (show_cores > 0) {
- wire_2_cross_section_radius = rad_c[1];
- wire_2_cross_section_pts = shape_circle(wire_2_cross_section_radius,$fn=base_seg*rad_p[1]);
- pts_2 = quad_torus_knot(q, [0,rad_p[1],rad_p[2],rad_p[3]], dir, phi_step*rad_p[1]);
- length = length_3d(pts_2);
- echo("Length Core 1", length);
- echo("Approx Volume Core 1 (length*crosssection)", length*3.141592654*rad_c[1]^2);
- color([1,0,0])
- path_extrude(
- wire_2_cross_section_pts,
- [each pts_2, pts_2[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- if (show_cores > 1) {
- wire_3_cross_section_radius = rad_c[2];
- wire_3_cross_section_pts = shape_circle(wire_3_cross_section_radius,$fn=base_seg*rad_p[2]);
- pts_3 = quad_torus_knot(q, [0,0,rad_p[2],rad_p[3]], dir, phi_step*rad_p[2]);
- length = length_3d(pts_3);
- echo("Length Core 2", length);
- echo("Approx Volume Core 2 (length*crosssection)", length*3.141592654*rad_c[2]^2);
- color([0,1,0])
- path_extrude(
- wire_3_cross_section_pts,
- [each pts_3, pts_3[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- if (show_cores > 2) {
- wire_4_cross_section_radius = rad_c[3];
- wire_4_cross_section_pts = shape_circle(wire_4_cross_section_radius,$fn=base_seg*rad_p[3]);
- pts_4 = quad_torus_knot(q, [0,0,0,rad_p[3]], dir, phi_step*rad_p[3]);
- length = length_3d(pts_4);
- echo("Length Core 3", length);
- echo("Approx Volume Core 3 (length*crosssection)", length*3.141592654*rad_c[3]^2);
- color([0,0,1])
- path_extrude(
- wire_4_cross_section_pts,
- [each pts_4, pts_4[0]],
- closed = true,
- twist = 0,
- method = "AXIS_ANGLE"
- );
- }
- }
- }
- }
- /**
- * q - number of loops per level
- * rad_p - radius of torus center line per level (this is used for positioning of the wire and cores relative to the center of each parent flux loop)
- * rad_c - radius of wire/cores (this is used for the wire/core model radius)
- * dir - rotation direction 1 clockwise (right-handed?), -1 counter-clockwise (left-handed?)
- * show_cores - number of cores to show
- * phi_step - length of longitudinal wire segment in radians (this is then multiplied by the rad_p radius to reduce data in cores)
- * base_seg - segments for wire diameter (this is then multiplied by the rad_p radius to reduce data in cores)
- **/
- // D-4D Samples
- //single_torus([], [2-0.5], [0.5], [], 0, 6.283185307179586/20, 40);
- //double_torus([12], [2-0.5,8-2], [0.5,1], [1], 1, 6.283185307179586/20, 20);
- //triple_torus([12,18], [2-0.5,8-2,32-8], [0.5,1,4], [1,1], 2, 6.283185307179586/20, 20);
- quad_torus([12,18,24], [2-0.5,8-2,32-8,128-32], [0.5,1,4,16], [1,1,1], 2, 6.283185307179586/20, 10);
Advertisement
Comments
-
- Latest version at https://pastebin.com/kbwK5EdL or referenced from that newer version.
Add Comment
Please, Sign In to add comment
Advertisement