Skip to main content

graphics - On drilling holes with minimal redundancy (and with colors!)


The old Mathematica package Graphics`Shapes` featured the function PerforatePolygons[], which drilled a hole in any Polygon[] primitive present in a Graphics3D[] object. One would usually use it on the output of ParametricPlot3D[] or Polyhedron[] from the Graphics`Polyhedra` package, like so:


objects with holes


Here is a slightly simplified implementation of PerforatePolygons[] which I used in generating the pictures above:


perforateaux[points_, ratio_] := Block[{test = TrueQ[First[points] == Last[points]],
center, q},
center = Mean[If[test, Most, Identity][points]]; q = 1 - 2 Boole[test];

MapThread[Polygon[Join[#1, Reverse[#2]]] &,
Partition[#, 2, 1, {1, q}] & /@ {points, (center + ratio (# - center)) & /@ points}]]

PerforatePolygons[shape_, ratio_: 0.5] :=
shape /. Polygon[p_] :> perforateaux[p, ratio]

(I elected to remove the additional EdgeForm[] directive in the original implementation so that the images clearly show what is going on with each polygon. A proper implementation would have it, of course.)


Nowadays, both PolyhedronData[] and ParametricPlot3D[] return GraphicsComplex[] objects within Graphics3D[]. The big benefit of this new representation, among other things, is that it minimizes redundant storage; instead of having a point being stored on three or four Polygon[] primitives, all the points in the object are stored in the first component of GraphicsComplex[], and the Polygon[] objects only need to store the index corresponding to the point they need. For instance, compare the output of PolyhedronData["Tetrahedron", "Faces"] and Normal[PolyhedronData["Tetrahedron", "Faces"]].


The problem with this efficient representation is that it no longer works nicely with polygon replacement rules like the one used by PerforatePolygons[]. Of course, there is the obvious solution of applying Normal[] to any GraphicsComplex[] object generated before applying PerforatePolygons[], but you lose out on the storage efficiency afforded by GraphicsComplex[].


Here now is my question:




Is it possible to improve PerforatePolygons[] so that it works onGraphicsComplex[] objects, with the output still retaining the GraphicsComplex[] characteristic of storage with minimum redundancy?





If the question above is not sufficiently challenging for you, consider the following wrinkle.


Polygon[] objects in Mathematica are currently able to take a VertexColors option that sets how the things are colored, with proper color interpolation within the polygon.



Is it possible to implement a version of PerforatePolygons[] that does its best to have the new polygons inherit the coloring used by the old polygons?



As an example of what is expected:



colored polygon, with and without hole


The improved PerforatePolygons[] should be able to produce an image like the one on the right from the one on the left. For triangular polygons, simple bilinear interpolation works nicely, but how about more complicated polygon objects? Again, it is important that a GraphicsComplex[] object still be one after the perforation.



Answer



Here's a start:


perforateaux[pts_, ratio_, indices : {__Integer}] :=
Module[
{vertices, center, newPts, ind},
vertices = Replace[indices, {{p_, b___, p_} :> {p, b}}];
center = Mean[pts[[vertices]]];
newPts = ratio (# - center) + center & /@ pts[[ vertices]];

ind = MapThread[Flatten[{#1, Reverse[#2]}] &,
{Partition[vertices, 2, 1, {1, 1}],
Partition[Range[Length[newPts]] + Length[pts], 2, 1, {1, 1}]}];
{Join[pts, newPts], ind}];

perforateaux[pts_, ratio_, indices : {{__Integer} ..}] :=
{#[[1]], Flatten[#[[2, 1]], 1]} &@
Reap[Fold[(Sow[#2]; #1) &@@ perforateaux[#, ratio, #2] &, pts, indices]]

PerforatePolygons[graphics3D_, ratio_: 0.5] :=

graphics3D /. GraphicsComplex[pts_, shape_, opt___] :>
Module[{newshapes},
newshapes = Flatten[Cases[{shape}, Polygon[a_, b___] :>
(If[Depth[a] == 2, {a}, a]), Infinity], 1];
GraphicsComplex[#1, Polygon[#2]] & @@ perforateaux[pts, ratio, newshapes, opt]]

Example


PerforatePolygons[PolyhedronData["Dodecahedron"]]

Mathematica graphics




Here's a way to preserve the colouring in a plot. This assumes that the plot is of the form Graphics3D[...GraphicsComplex[pts, {shapes}, ... , VertexColors -> colours, ...], ... ]. I should note that it's not very fast, so I think there is still room for optimisation of the code.


newCols[pts_, collst_, ratio_] := Module[{normal, center, colc},
center = Mean[pts];
colc = Blend[collst, Norm[# - center] & /@ pts];
Blend[{colc, #}, ratio] & /@ collst]

perforateauxCol[pts_, collst_, ratio_, indices : {__Integer}] :=
Module[
{vertices, center, newPts, ind, newcol},

vertices = Replace[indices, {{p_, b___, p_} :> {p, b}}];
center = Mean[pts[[vertices]]];
newPts = ratio (# - center) + center & /@ pts[[ vertices]];
newcol = newCols[pts[[vertices]], collst[[vertices]], ratio];
ind = MapThread[Flatten[{##}] &,
{Partition[vertices, 2, 1, {1, 1}],
Reverse /@
Partition[Range[Length[newPts]] + Length[pts], 2,
1, {1, 1}]}];
{Join[pts, newPts], Join[collst, newcol], ind}];


perforateauxCol[pts_, collst_, ratio_,
indices : {{__Integer} ..}] :=
{#[[1, 1]], #[[1, 2]],
Flatten[#[[2, 1]], 1]} &@
Reap[Fold[(Sow[#[[3]]]; #[[{1,
2}]]) &@(perforateauxCol[#[[1]], #[[2]],
ratio, #2]) &, {pts, collst}, indices]]

PerforatePolygonsCol[graphics3D_, ratio_: 0.5] := graphics3D /.

GraphicsComplex[pts_, shape_, opt1___, VertexColors -> collst_,
opt2___] :>
Module[{newshapes},
newshapes = Flatten[Cases[{shape}, Polygon[a_, b___] :>
(If[Depth[a] == 2, {a}, a]), Infinity], 1];
GraphicsComplex[#1, Polygon[#3], opt1, VertexColors -> #2,
opt2] & @@
perforateauxCol[N[pts],
If[Head[collst] === List, N[RGBColor @@@ collst], N[collst]],
ratio, newshapes]]


Example


pl = Plot3D[Sin[x^2 - 4 Pi y (1 - y)], {x, 0, Pi}, {y, 0, 1}, 
PlotPoints -> 20, MaxRecursion -> 1, Mesh -> All,
ColorFunction -> (ColorData["GrayYellowTones"][#3] &)]

Mathematica graphics


With holes


pl1 = PerforatePolygonsCol[pl]


Mathematica graphics


To remove the edges you can do something like


Show[pl1 /. a_Polygon :> {EdgeForm[], a}]

Mathematica graphics


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...