Advertisement
49xur

bagel_0103a

Jun 13th, 2022
97
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 KB | None | 0 0
  1. /**
  2. * based off example from https://openhome.cc/eGossip/OpenSCAD/lib3x-path_extrude.html
  3. **/
  4.  
  5. /*
  6. *
  7. * Version 0102a: First release with usability improvements
  8. * 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.
  9. */
  10.  
  11. use <dotSCAD/src/shape_circle.scad>;
  12. use <dotSCAD/src/path_extrude.scad>;
  13. use <dotSCAD/src/util/degrees.scad>;
  14.  
  15. function length_3d(lt) =
  16. let(end = len(lt) - 1)
  17. end == 0 ? lt[0] :
  18. let(
  19. cum_total = [
  20. for(i = 0, s = 0, is_continue = i < end;
  21. is_continue;
  22. 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]
  23. )
  24. cum_total[end];
  25.  
  26. function single_torus_knot(q, rad, dir, phi_step) =
  27. [
  28. for(phi = 0; phi < 6.283185307179586; phi = phi + phi_step)
  29. let(
  30. phi_deg = degrees(phi),
  31. l0_deg = phi_deg,
  32. l1_deg = phi_deg/q[0],
  33. l0_pos_x = rad[0]*cos(l0_deg),
  34. l0_pos_y = 0,
  35. l0_pos_z = rad[0]*sin(l0_deg)
  36. )
  37. [l0_pos_x,
  38. l0_pos_y,
  39. l0_pos_z]
  40. ];
  41.  
  42. function double_torus_knot(q, rad, dir, phi_step) =
  43. [
  44. for(phi = 0; phi < 6.283185307179586*q[0]; phi = phi + phi_step)
  45. let(
  46. phi_deg = degrees(phi),
  47. l0_deg = -dir[0]*phi_deg,
  48. l1_deg = phi_deg/q[0],
  49. l0_pos_x = rad[0]*cos(l0_deg),
  50. l0_pos_y = 0,
  51. l0_pos_z = rad[0]*sin(l0_deg),
  52. l1_pos_x = (l0_pos_x+rad[1])*cos(l1_deg)-l0_pos_y*sin(l1_deg),
  53. l1_pos_y = (l0_pos_x+rad[1])*sin(l1_deg)+l0_pos_y*cos(l1_deg),
  54. l1_pos_z = l0_pos_z
  55. )
  56. [l1_pos_x,
  57. l1_pos_y,
  58. l1_pos_z]
  59. ];
  60.  
  61. function triple_torus_knot(q, rad, dir, phi_step) =
  62. [
  63. for(phi = 0; phi < 6.283185307179586*q[0]*q[1]; phi = phi + phi_step)
  64. let(
  65. phi_deg = degrees(phi),
  66. l0_deg = -dir[0]*phi_deg,
  67. l1_deg = phi_deg/q[0],
  68. l0_pos_x = rad[0]*cos(l0_deg),
  69. l0_pos_y = 0,
  70. l0_pos_z = rad[0]*sin(l0_deg),
  71. l1_pos_x = (l0_pos_x+rad[1])*cos(l1_deg)-l0_pos_y*sin(l1_deg),
  72. l1_pos_y = (l0_pos_x+rad[1])*sin(l1_deg)+l0_pos_y*cos(l1_deg),
  73. l1_pos_z = l0_pos_z,
  74. l2_deg = dir[1]*phi_deg/q[0]/q[1],
  75. l2_pos_x = l1_pos_x,
  76. l2_pos_y = (l1_pos_y+rad[2])*cos(l2_deg)-l1_pos_z*sin(l2_deg),
  77. l2_pos_z = (l1_pos_y+rad[2])*sin(l2_deg)+l1_pos_z*cos(l2_deg)
  78. )
  79. [l2_pos_x,
  80. l2_pos_y,
  81. l2_pos_z]
  82. ];
  83.  
  84. function quad_torus_knot(q, rad, dir, phi_step) =
  85. [
  86. for(phi = 0; phi < 6.283185307179586*q[0]*q[1]*q[2]; phi = phi + phi_step)
  87. let(
  88. phi_deg = degrees(phi),
  89. l0_deg = -dir[0]*phi_deg,
  90. l1_deg = phi_deg/q[0],
  91. l0_pos_x = rad[0]*cos(l0_deg),
  92. l0_pos_y = 0,
  93. l0_pos_z = rad[0]*sin(l0_deg),
  94. l1_pos_x = (l0_pos_x+rad[1])*cos(l1_deg)-l0_pos_y*sin(l1_deg),
  95. l1_pos_y = (l0_pos_x+rad[1])*sin(l1_deg)+l0_pos_y*cos(l1_deg),
  96. l1_pos_z = l0_pos_z,
  97. l2_deg = dir[1]*phi_deg/q[0]/q[1],
  98. l2_pos_x = l1_pos_x,
  99. l2_pos_y = (l1_pos_y+rad[2])*cos(l2_deg)-l1_pos_z*sin(l2_deg),
  100. l2_pos_z = (l1_pos_y+rad[2])*sin(l2_deg)+l1_pos_z*cos(l2_deg),
  101. l3_deg = -dir[2]*phi_deg/q[0]/q[1]/q[2],
  102. l3_pos_x = l2_pos_x*cos(l3_deg)-(l2_pos_y+rad[3])*sin(l3_deg),
  103. l3_pos_y = l2_pos_x*sin(l3_deg)+(l2_pos_y+rad[3])*cos(l3_deg),
  104. l3_pos_z = l2_pos_z
  105. )
  106. [l3_pos_x,
  107. l3_pos_y,
  108. l3_pos_z]
  109. ];
  110.  
  111.  
  112. module single_torus(q, rad_p, rad_c, dir, show_cores, phi_step, base_seg) {
  113. wire_1_cross_section_radius = rad_c[0];
  114. wire_1_cross_section_pts = shape_circle(wire_1_cross_section_radius,$fn=base_seg*rad_p[0]);
  115. pts_1 = single_torus_knot(q, rad_p, dir, phi_step*rad_p[0]);
  116. echo(pts_1);
  117. length = length_3d(pts_1);
  118. echo("Length Wire", length);
  119. echo("Approx Volume Wire (length*crosssection)", length*3.141592654*rad_c[0]^2);
  120. path_extrude(
  121. wire_1_cross_section_pts,
  122. [each pts_1, pts_1[0]],
  123. closed = true,
  124. twist = 0,
  125. method = "AXIS_ANGLE"
  126. );
  127. }
  128.  
  129.  
  130. module double_torus(q, rad_p, rad_c, dir, show_cores, phi_step, base_seg) {
  131. wire_1_cross_section_radius = rad_c[0];
  132. wire_1_cross_section_pts = shape_circle(wire_1_cross_section_radius,$fn=base_seg*rad_p[0]);
  133. pts_1 = double_torus_knot(q, rad_p, dir, phi_step*rad_p[0]);
  134. length = length_3d(pts_1);
  135. echo("Length Wire", length);
  136. echo("Approx Volume Wire (length*crosssection)", length*3.141592654*rad_c[0]^2);
  137. path_extrude(
  138. wire_1_cross_section_pts,
  139. [each pts_1, pts_1[0]],
  140. closed = true,
  141. twist = 0,
  142. method = "AXIS_ANGLE"
  143. );
  144. if (show_cores > 0) {
  145. wire_2_cross_section_radius = rad_c[1];
  146. wire_2_cross_section_pts = shape_circle(wire_2_cross_section_radius,$fn=base_seg*rad_p[1]);
  147. pts_2 = double_torus_knot(q, [0,rad_p[1]], dir, phi_step*rad_p[1]);
  148. length = length_3d(pts_2);
  149. echo("Length Core 1", length);
  150. echo("Approx Volume Core 1 (length*crosssection)", length*3.141592654*rad_c[1]^2);
  151. color([1,0,0])
  152. path_extrude(
  153. wire_2_cross_section_pts,
  154. [each pts_2, pts_2[0]],
  155. closed = true,
  156. twist = 0,
  157. method = "AXIS_ANGLE"
  158. );
  159. }
  160. }
  161.  
  162.  
  163. module triple_torus(q, rad_p, rad_c, dir, show_cores, phi_step, base_seg) {
  164. wire_1_cross_section_radius = rad_c[0];
  165. wire_1_cross_section_pts = shape_circle(wire_1_cross_section_radius,$fn=base_seg*rad_p[0]);
  166. pts_1 = triple_torus_knot(q, rad_p, dir, phi_step*rad_p[0]);
  167. length = length_3d(pts_1);
  168. echo("Length Wire", length);
  169. echo("Approx Volume Wire (length*crosssection)", length*3.141592654*rad_c[0]^2);
  170. path_extrude(
  171. wire_1_cross_section_pts,
  172. [each pts_1, pts_1[0]],
  173. closed = true,
  174. twist = 0,
  175. method = "AXIS_ANGLE"
  176. );
  177. if (show_cores > 0) {
  178. wire_2_cross_section_radius = rad_c[1];
  179. wire_2_cross_section_pts = shape_circle(wire_2_cross_section_radius,$fn=base_seg*rad_p[1]);
  180. pts_2 = triple_torus_knot(q, [0,rad_p[1],rad_p[2]], dir, phi_step*rad_p[1]);
  181. length = length_3d(pts_2);
  182. echo("Length Core 1", length);
  183. echo("Approx Volume Core 1 (length*crosssection)", length*3.141592654*rad_c[1]^2);
  184. color([1,0,0])
  185. path_extrude(
  186. wire_2_cross_section_pts,
  187. [each pts_2, pts_2[0]],
  188. closed = true,
  189. twist = 0,
  190. method = "AXIS_ANGLE"
  191. );
  192. if (show_cores > 1) {
  193. wire_3_cross_section_radius = rad_c[2];
  194. wire_3_cross_section_pts = shape_circle(wire_3_cross_section_radius,$fn=base_seg*rad_p[2]);
  195. pts_3 = triple_torus_knot(q, [0,0,rad_p[2]], dir, phi_step*rad_p[2]);
  196. length = length_3d(pts_3);
  197. echo("Length Core 2", length);
  198. echo("Approx Volume Core 2 (length*crosssection)", length*3.141592654*rad_c[2]^2);
  199. color([0,1,0])
  200. path_extrude(
  201. wire_3_cross_section_pts,
  202. [each pts_3, pts_3[0]],
  203. closed = true,
  204. twist = 0,
  205. method = "AXIS_ANGLE"
  206. );
  207. }
  208. }
  209. }
  210.  
  211. module quad_torus(q, rad_p, rad_c, dir, show_cores, phi_step, base_seg) {
  212. wire_1_cross_section_radius = rad_c[0];
  213. wire_1_cross_section_pts = shape_circle(wire_1_cross_section_radius,$fn=base_seg*rad_p[0]);
  214. pts_1 = quad_torus_knot(q, rad_p, dir, phi_step*rad_p[0]);
  215. length = length_3d(pts_1);
  216. echo("Length Wire", length);
  217. echo("Approx Volume Wire (length*crosssection)", length*3.141592654*rad_c[0]^2);
  218. path_extrude(
  219. wire_1_cross_section_pts,
  220. [each pts_1, pts_1[0]],
  221. closed = true,
  222. twist = 0,
  223. method = "AXIS_ANGLE"
  224. );
  225. if (show_cores > 0) {
  226. wire_2_cross_section_radius = rad_c[1];
  227. wire_2_cross_section_pts = shape_circle(wire_2_cross_section_radius,$fn=base_seg*rad_p[1]);
  228. pts_2 = quad_torus_knot(q, [0,rad_p[1],rad_p[2],rad_p[3]], dir, phi_step*rad_p[1]);
  229. length = length_3d(pts_2);
  230. echo("Length Core 1", length);
  231. echo("Approx Volume Core 1 (length*crosssection)", length*3.141592654*rad_c[1]^2);
  232. color([1,0,0])
  233. path_extrude(
  234. wire_2_cross_section_pts,
  235. [each pts_2, pts_2[0]],
  236. closed = true,
  237. twist = 0,
  238. method = "AXIS_ANGLE"
  239. );
  240. if (show_cores > 1) {
  241. wire_3_cross_section_radius = rad_c[2];
  242. wire_3_cross_section_pts = shape_circle(wire_3_cross_section_radius,$fn=base_seg*rad_p[2]);
  243. pts_3 = quad_torus_knot(q, [0,0,rad_p[2],rad_p[3]], dir, phi_step*rad_p[2]);
  244. length = length_3d(pts_3);
  245. echo("Length Core 2", length);
  246. echo("Approx Volume Core 2 (length*crosssection)", length*3.141592654*rad_c[2]^2);
  247. color([0,1,0])
  248. path_extrude(
  249. wire_3_cross_section_pts,
  250. [each pts_3, pts_3[0]],
  251. closed = true,
  252. twist = 0,
  253. method = "AXIS_ANGLE"
  254. );
  255. if (show_cores > 2) {
  256. wire_4_cross_section_radius = rad_c[3];
  257. wire_4_cross_section_pts = shape_circle(wire_4_cross_section_radius,$fn=base_seg*rad_p[3]);
  258. pts_4 = quad_torus_knot(q, [0,0,0,rad_p[3]], dir, phi_step*rad_p[3]);
  259. length = length_3d(pts_4);
  260. echo("Length Core 3", length);
  261. echo("Approx Volume Core 3 (length*crosssection)", length*3.141592654*rad_c[3]^2);
  262. color([0,0,1])
  263. path_extrude(
  264. wire_4_cross_section_pts,
  265. [each pts_4, pts_4[0]],
  266. closed = true,
  267. twist = 0,
  268. method = "AXIS_ANGLE"
  269. );
  270. }
  271. }
  272. }
  273. }
  274.  
  275. /**
  276. * q - number of loops per level
  277. * 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)
  278. * rad_c - radius of wire/cores (this is used for the wire/core model radius)
  279. * dir - rotation direction 1 clockwise (right-handed?), -1 counter-clockwise (left-handed?)
  280. * show_cores - number of cores to show
  281. * phi_step - length of longitudinal wire segment in radians (this is then multiplied by the rad_p radius to reduce data in cores)
  282. * base_seg - segments for wire diameter (this is then multiplied by the rad_p radius to reduce data in cores)
  283. **/
  284.  
  285. // D-4D Samples
  286. //single_torus([], [2-0.5], [0.5], [], 0, 6.283185307179586/20, 40);
  287. //double_torus([12], [2-0.5,8-2], [0.5,1], [1], 1, 6.283185307179586/20, 20);
  288. //triple_torus([12,18], [2-0.5,8-2,32-8], [0.5,1,4], [1,1], 2, 6.283185307179586/20, 20);
  289. 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
  • 49xur
    1 year
    # text 0.08 KB | 0 0
    1. Latest version at https://pastebin.com/kbwK5EdL or referenced from that newer version.
Add Comment
Please, Sign In to add comment
Advertisement