Guest User

Untitled

a guest
Nov 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. data3C = RandomReal[1, {10, 6}];
  2. func3C = Nearest[{#, #2, #3} -> {##4, .03} & @@@ data3C];
  3. tbl3C = Table[ First[func3C[{x, y, z}]] // Quiet, {x, 0, 1, .01},
  4. {y, 0, 1, .01}, {z, 0, 1, .01}];
  5.  
  6. Row[Labeled[Graphics3D[Raster3D[tbl3C, ColorFunction -> #,
  7. Method -> {"InterpolateValues" -> True}],
  8. Background -> Black, ImageSize -> 400,
  9. SphericalRegion -> True], #, Top] & /@
  10. {Hue, RGBColor, (GrayLevel[#[[1]], .03] &)}, Spacer[5]]
  11.  
  12. colorRules = Thread[# -> (ColorData[1, "ColorList"][[;; Length@#]])] &[
  13. Flatten[tbl3C, 2] // DeleteDuplicates] /. RGBColor -> List;
  14. Row[Labeled[ Graphics3D[Raster3D[tbl3C /. colorRules, ColorFunction -> #,
  15. Method -> {"InterpolateValues" -> True}],
  16. Background -> Black, ImageSize -> 400,
  17. SphericalRegion -> True], #, Top] & /@
  18. {(RGBColor[#[[1]], #[[2]], #[[3]], .01] &),
  19. (RGBColor[#[[1]], #[[2]], #[[3]], .03] &),
  20. (RGBColor[#[[1]], #[[2]], #[[3]], .05] &)}, Spacer[5]]
  21.  
  22. data = RandomReal[1, {20, 4}];
  23. func = Nearest[{#, #2, #3} -> #4 & @@@ data];
  24. dta = Table[func[{x, y, z}] // Quiet, {x, 0, 1, .005}, {y, 0, 1, .005}, {z, 0, 1, .005}];
  25.  
  26. Grid[Partition[
  27. Image3D[dta,
  28. ImageSize -> 350, ColorFunction -> #, Background -> Black] & /@
  29. {"SunsetColorsOpacity", "RainbowOpacity", "WhiteBlackOpacity",
  30. (Append[Blend[{LightBlue, Blue, Yellow}, #], #] &)}, 2]]
  31.  
  32. data = RandomReal[1, {20, 4}];
  33.  
  34. func = Nearest[{#, #2, #3} -> #4 & @@@ data];
  35.  
  36. ContourPlot3D[
  37. func[{x, y, z}] // Quiet,
  38. {x, 0, 1}, {y, 0, 1}, {z, 0, 1},
  39. ColorFunction -> (Hue@#3 &)
  40. ]
  41.  
  42. BlockRandom[SeedRandom["voronoi"]; pts = RandomReal[{-1, 1}, {32, 3}]];
  43. nf = Nearest[pts];
  44.  
  45. (* gradient-normalized function *)
  46. vfun[x_?NumericQ, y_?NumericQ, z_?NumericQ] := With[{np = nf[{x, y, z}, 2]},
  47. (EuclideanDistance[{x, y, z}, np[[2]]] - EuclideanDistance[{x, y, z}, np[[1]]])/
  48. EuclideanDistance[Normalize[{x, y, z} - np[[2]]], Normalize[{x, y, z} - np[[1]]]]]
  49.  
  50. With[{ε = 1/100}, (* tiny number, increase if you want to see gaps between cells *)
  51. ContourPlot3D[vfun[x, y, z] == ε, {x, -1, 1}, {y, -1, 1}, {z, -1, 1},
  52. MaxRecursion -> 1, Mesh -> False, PlotPoints -> 35]]
  53.  
  54. $QHULL::usage="$QHULL should contain the name (as a string) of the folder that contains the QHULL binaries on your system.
  55. $QHULL = ToFileName[{$UserBaseDirectory, "Applications", "qhull", "src"}];
  56.  
  57. $QHULL = ToFileName[{NotebookDirectory[], "qhull", "bin"}];
  58.  
  59. SetDirectory[NotebookDirectory[]]
  60. << "mPower.m"
  61.  
  62. Warning: regtet binary not found. Expected location: /mPower-1.0-11-May-2008-1631/qhull/bin/
  63.  
  64. Warning: pwrvtx binary not found. Expected location: /mPower-1.0-11-May-2008-1631/qhull/bin/
  65.  
  66. (* random 3D points *)
  67. points = RandomReal[1, {40, 3}];
  68. (* construct 3D convexhull *)
  69. ch = convexHull[points, convexHullFormat -> {facetNormals -> True, facets -> True}];
  70. (* generate facets for 3D convexhull *)
  71. facetIndices = facets /. ch;
  72. loop[alist_] := Append[alist, alist[[1]]];
  73. loopedFacetIndices = loop /@ facetIndices;
  74. index2xyz[ijklist_] := points[[#]] & /@ ijklist;
  75. loopedFacetCoordinates = index2xyz /@ loopedFacetIndices;
  76. (* visulization 3D convexhull *)
  77. convexObject =
  78. Graphics3D[{PointSize[Large], Point[points], FaceForm[],
  79. EdgeForm[Blue], Polygon /@ loopedFacetCoordinates},
  80. ImageSize -> 500]
  81.  
  82. pad[δ_][{min_, max_}] := {min, max} + δ(max-min){-1, 1}
  83.  
  84. VoronoiCells[pts_] :=
  85. Block[{bds, dm, init, conn, adj, lc, pc, cpts, hpts, hns, hp, vcells},
  86. bds = pad[.1] /@ MinMax /@ Transpose[pts];
  87. dm = DelaunayMesh[pts];
  88. init = BoundaryMeshRegion[Tuples[bds], Polygon[{{1,2,4,3},{2,4,8,6},{8,6,5,7},{1,5,7,3},{1,5,6,2},{3,7,8,4}}]];
  89.  
  90. conn = dm["ConnectivityMatrix"[0, 1]];
  91. adj = conn . Transpose[conn];
  92.  
  93. lc = conn["MatrixColumns"];
  94. pc = adj["MatrixColumns"];
  95. cpts = MeshCoordinates[dm];
  96.  
  97. vcells = Table[
  98. hpts = PropertyValue[{dm, {1, lc[[i]]}}, MeshCellCentroid];
  99. hns = Transpose[Transpose[cpts[[DeleteCases[pc[[i]], i]]]] - cpts[[i]]];
  100. hp = MapThread[HalfSpace, {hns, hpts}];
  101. RegionIntersection @@ Prepend[BoundaryDiscretizeGraphics[Graphics3D[#, PlotRange -> bds]]& /@ hp, init],
  102. {i, MeshCellCount[dm, 0]}
  103. ];
  104.  
  105. AssociationThread[cpts, vcells]
  106. ]
  107.  
  108. SeedRandom[10000];
  109. pts = RandomReal[1, {10, 3}];
  110.  
  111. vc = VoronoiCells[pts]
  112.  
  113. Show[MapIndexed[
  114. BoundaryMeshRegion[#, MeshCellStyle -> {1 -> {Black, Thick}, 2 -> {ColorData[112][First[#2]]}}] &,
  115. Values[vc]
  116. ]]
  117.  
  118. Show[
  119. MapIndexed[
  120. BoundaryMeshRegion[#, MeshCellStyle -> {1 -> {Black, Thick}, 2 -> {ColorData[112][First[#2]]}}] &,
  121. Values[vc]
  122. ],
  123. Graphics3D[{PointSize[Large], Point[pts]}],
  124. Method -> {"RelieveDPZFighting" -> True}
  125. ]
Add Comment
Please, Sign In to add comment