Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. (* Code written by J.M., taken from http://mathematica.stackexchange.com/a/119886/9490 *)
  2.  
  3. makeCap[type : ("Butt" | "Round" | "Square"), s : (-1 | 1), path_?MatrixQ, tube_?ArrayQ,
  4. r_?NumericQ, h_?NumericQ] :=
  5. Module[{d = Take[path, 2 s], cs, p, t0, t1},
  6. cs = tube[[s]];
  7. t0 = h; t1 = 1 - h Boole[type =!= "Square"];
  8. If[s == -1, {t0, t1} = {t1, t0}];
  9. If[type === "Butt",
  10. {d[[s]],
  11. Table[ScalingTransform[{t, t, t}, d[[s]]] @ cs, {t, t0, t1, s h}]},
  12. p = (s r/(EuclideanDistance @@ d)) ({1, -1}.d);
  13. {d[[s]] + p, Switch[type,
  14. "Round",
  15. Table[Composition[TranslationTransform[p Cos[π t/2]],
  16. ScalingTransform[{1, 1, 1} Sin[π t/2], d[[s]]]][cs],
  17. {t, t0, t1, s h}],
  18. "Square",
  19. Table[Composition[TranslationTransform[p],
  20. ScalingTransform[{t, t, t}, d[[s]]]][cs],
  21. {t, t0, t1, s h}]]}]]
  22.  
  23. Options[TubeMesh] = {"CapForm" -> None, "CirclePoints" -> Automatic,
  24. "MeshType" -> Automatic, Tolerance -> Automatic};
  25.  
  26. TubeMesh[path_?MatrixQ, r_?NumericQ, opts : OptionsPattern[{TubeMesh, MeshRegion}]] :=
  27. Module[{c0, c1, cf, mt, dims, h, idx, m, n, p0, p1, t0, t1, tol, tube},
  28. cf = OptionValue["CapForm"]; mt = OptionValue["MeshType"];
  29. If[mt === Automatic,
  30. mt = If[MatchQ[cf, "Butt" | "Round" | "Square"],
  31. BoundaryMeshRegion, MeshRegion]];
  32. tol = OptionValue[Tolerance] /. Automatic -> 0.0015;
  33. n = OptionValue["CirclePoints"];
  34. If[n === Automatic, n = Round[17 tol^(-1/3)/5 - 57 tol^(1/3)/59];
  35. n += Boole[OddQ[n]]]; h = 2/n;
  36. tube = FoldList[Function[{p, t}, Module[{o = orthogonalDirections[t]},
  37. extend[#, t[[2]], t[[2]] - t[[1]], o] & /@ p]],
  38. crossSection[path, r, n], Partition[path, 3, 1, {1, 2}, {}]];
  39. If[MatchQ[cf, "Butt" | "Round" | "Square"],
  40. {p0, c0} = makeCap[cf, 1, path, tube, r, h];
  41. {p1, c1} = makeCap[cf, -1, path, tube, r, h];
  42. tube = Join[c0, tube, c1]];
  43. dims = Most[Dimensions[tube]]; tube = Apply[Join, tube];
  44. m = Times @@ dims; idx = Partition[Range[m], Last[dims]]; t0 = t1 = {};
  45. If[MatchQ[cf, "Butt" | "Round" | "Square"],
  46. PrependTo[tube, p0]; AppendTo[tube, p1]; idx += 1;
  47. t0 = PadLeft[Partition[First[idx], 2, 1], {Automatic, 3}, 1];
  48. t1 = PadRight[Reverse /@ Partition[Last[idx], 2, 1],
  49. {Automatic, 3}, m + 2]];
  50. mt[tube, Triangle[Join[t0,
  51. Flatten[Apply[{Append[Reverse[#1], Last[#2]],
  52. Prepend[#2, First[#1]]} &,
  53. Partition[idx, {2, 2}, {1, 1}], {2}], 2], t1]],
  54. FilterRules[{opts}, Options[MeshRegion]]]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement