Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Code written by J.M., taken from http://mathematica.stackexchange.com/a/119886/9490 *)
- makeCap[type : ("Butt" | "Round" | "Square"), s : (-1 | 1), path_?MatrixQ, tube_?ArrayQ,
- r_?NumericQ, h_?NumericQ] :=
- Module[{d = Take[path, 2 s], cs, p, t0, t1},
- cs = tube[[s]];
- t0 = h; t1 = 1 - h Boole[type =!= "Square"];
- If[s == -1, {t0, t1} = {t1, t0}];
- If[type === "Butt",
- {d[[s]],
- Table[ScalingTransform[{t, t, t}, d[[s]]] @ cs, {t, t0, t1, s h}]},
- p = (s r/(EuclideanDistance @@ d)) ({1, -1}.d);
- {d[[s]] + p, Switch[type,
- "Round",
- Table[Composition[TranslationTransform[p Cos[π t/2]],
- ScalingTransform[{1, 1, 1} Sin[π t/2], d[[s]]]][cs],
- {t, t0, t1, s h}],
- "Square",
- Table[Composition[TranslationTransform[p],
- ScalingTransform[{t, t, t}, d[[s]]]][cs],
- {t, t0, t1, s h}]]}]]
- Options[TubeMesh] = {"CapForm" -> None, "CirclePoints" -> Automatic,
- "MeshType" -> Automatic, Tolerance -> Automatic};
- TubeMesh[path_?MatrixQ, r_?NumericQ, opts : OptionsPattern[{TubeMesh, MeshRegion}]] :=
- Module[{c0, c1, cf, mt, dims, h, idx, m, n, p0, p1, t0, t1, tol, tube},
- cf = OptionValue["CapForm"]; mt = OptionValue["MeshType"];
- If[mt === Automatic,
- mt = If[MatchQ[cf, "Butt" | "Round" | "Square"],
- BoundaryMeshRegion, MeshRegion]];
- tol = OptionValue[Tolerance] /. Automatic -> 0.0015;
- n = OptionValue["CirclePoints"];
- If[n === Automatic, n = Round[17 tol^(-1/3)/5 - 57 tol^(1/3)/59];
- n += Boole[OddQ[n]]]; h = 2/n;
- tube = FoldList[Function[{p, t}, Module[{o = orthogonalDirections[t]},
- extend[#, t[[2]], t[[2]] - t[[1]], o] & /@ p]],
- crossSection[path, r, n], Partition[path, 3, 1, {1, 2}, {}]];
- If[MatchQ[cf, "Butt" | "Round" | "Square"],
- {p0, c0} = makeCap[cf, 1, path, tube, r, h];
- {p1, c1} = makeCap[cf, -1, path, tube, r, h];
- tube = Join[c0, tube, c1]];
- dims = Most[Dimensions[tube]]; tube = Apply[Join, tube];
- m = Times @@ dims; idx = Partition[Range[m], Last[dims]]; t0 = t1 = {};
- If[MatchQ[cf, "Butt" | "Round" | "Square"],
- PrependTo[tube, p0]; AppendTo[tube, p1]; idx += 1;
- t0 = PadLeft[Partition[First[idx], 2, 1], {Automatic, 3}, 1];
- t1 = PadRight[Reverse /@ Partition[Last[idx], 2, 1],
- {Automatic, 3}, m + 2]];
- mt[tube, Triangle[Join[t0,
- Flatten[Apply[{Append[Reverse[#1], Last[#2]],
- Prepend[#2, First[#1]]} &,
- Partition[idx, {2, 2}, {1, 1}], {2}], 2], t1]],
- FilterRules[{opts}, Options[MeshRegion]]]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement