Guest User

Untitled

a guest
Mar 21st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. reg = BoundaryDiscretizeRegion[Ball[], PlotTheme -> "SmoothShading",
  2. PrecisionGoal -> 1, MaxCellMeasure -> 0.1]
  3.  
  4. gr = GraphicsComplex[MeshCoordinates[reg], MeshCells[reg, 2]];
  5.  
  6. Graphics3D[{EdgeForm[None], gr}]
  7.  
  8. reg = BoundaryDiscretizeRegion[Ball[], PlotTheme -> "SmoothShading",
  9. PrecisionGoal -> 1, MaxCellMeasure -> 0.1];
  10. Graphics3D[
  11. GraphicsComplex[
  12. MeshCoordinates[reg],
  13. {EdgeForm[], Thread[MeshCells[reg, 2], Polygon]},
  14. VertexNormals -> #]
  15. ] & /@ {Automatic, Region`Mesh`MeshCellNormals[reg, 0]} // GraphicsRow
  16.  
  17. rplot[reg_] := Graphics3D[
  18. GraphicsComplex[
  19. MeshCoordinates[reg],
  20. {EdgeForm[], Thread[MeshCells[reg, 2], Polygon]},
  21. VertexNormals -> #]
  22. ] &
  23.  
  24. reg = BoundaryDiscretizeRegion[
  25. RegionUnion[Ball[], Cylinder[{{0, 0, 0}, {1, 1, 1}}, 2]]];
  26. rplot[reg] /@ {Automatic, Region`Mesh`MeshCellNormals[reg, 0]} // GraphicsRow
  27.  
  28. reg = BoundaryDiscretizeRegion@
  29. ImplicitRegion[x^2 + y^2 + z^2 + (x^2 - y^2)^2 < 4, {x, y, z}];
  30. rplot[reg] /@ {Automatic, Region`Mesh`MeshCellNormals[reg, 0]} // GraphicsRow
  31.  
  32. reg = ConvexHullMesh[RandomPoint[Sphere[], 100]];
  33. rplot[reg] /@ {Automatic, Region`Mesh`MeshCellNormals[reg, 0]} // GraphicsRow
  34.  
  35. meshPoints = MeshPrimitives[reg, 0] /. Point -> Sequence;
  36.  
  37. polys = MeshPrimitives[reg, 2] /. Polygon -> Sequence;
  38. polysRotated = RotateRight /@ polys;
  39. polyVecs = polys - polysRotated;
  40.  
  41. surfaceNorms =
  42. MapThread[Cross, {polyVecs[[All, 1]], polyVecs[[All, 2]]}];
  43.  
  44. polyVertexes = MeshCells[reg, 2] /. Polygon -> Sequence;
  45.  
  46. sharedVerticeAssoc = (Flatten /@
  47. Merge[{PositionIndex[polyVertexes[[All, 1]]],
  48. PositionIndex[polyVertexes[[All, 2]]],
  49. PositionIndex[polyVertexes[[All, 3]]]}, Identity]);
  50.  
  51. SurfaceNormsAdjacentEachVertice =
  52. Table[Plus[meshPoints[[i]], #] & /@
  53. surfaceNorms[[sharedVerticeAssoc[i]]], {i, 1,
  54. Length[meshPoints]}];
  55.  
  56. surfaceVectorsatEachVertex =
  57. Table[Riffle[
  58. SurfaceNormsAdjacentEachVertice[[i]], {meshPoints[[i]]}, {1, -2,
  59. 2}], {i, 1, Length[meshPoints]}];
  60.  
  61. Graphics3D[{Table[
  62. Line /@ Partition[surfaceVectorsatEachVertex[[i]], 2], {i,
  63. 122}], {EdgeForm[None], gr}}]
  64.  
  65. Graphics3D[GraphicsComplex[MeshCoordinates[reg], {EdgeForm[],Thread[MeshCells[reg, 2], Polygon]},VertexNormals -> Total /@ SurfaceNormsAdjacentEachVertice]]
  66.  
  67. reg = BoundaryDiscretizeRegion[Ball[], PlotTheme -> "SmoothShading",
  68. PrecisionGoal -> 1, MaxCellMeasure -> 0.1];
  69.  
  70. pts = MeshCoordinates[reg]; tri = MeshCells[reg, 2];
  71.  
  72. idx = tri[[All, 1]];
  73.  
  74. (* get neighbors for each vertex *)
  75. nbrs = Table[DeleteDuplicates[Flatten[List @@@ First[FindCycle[
  76. Extract[idx, Drop[SparseArray[Unitize[idx - k],
  77. Automatic, 1]["NonzeroPositions"], None, -1],
  78. # /. {k, a_, b_} | {b_, k, a_} | {a_, b_, k} :> (a -> b) &]]]]],
  79. {k, Length[pts]}];
  80.  
  81. (* Max's method *)
  82. nrms = Table[Normalize[Total[With[{c = pts[[k]], vl = pts[[#]]},
  83. Cross[vl[[1]] - c, vl[[2]] - c]/
  84. ((#.# &[vl[[1]] - c]) (#.# &[vl[[2]] - c]))] & /@
  85. Partition[nbrs[[k]], 2, 1, 1],
  86. Method -> "CompensatedSummation"]], {k, Length[pts]}];
  87.  
  88. Graphics3D[{EdgeForm[], GraphicsComplex[pts, tri, VertexNormals -> nrms]}]
  89.  
  90. Graphics3D[{EdgeForm[], GraphicsComplex[pts, Triangle[tri], VertexNormals -> nrms],
  91. Arrowheads[Small],
  92. MapThread[Arrow[Tube[{#1, #1 + #2/5}, 0.01]] &, {pts, nrms}]}]
Add Comment
Please, Sign In to add comment