Thursday, December 22, 2016

The Colour of 3D Fractals

An interesting feature of 3D fractals compared to other shapes is that they have a real colour. By this I mean that, if you 3D printed a 3D fractal to an ultrafine scale (below about 100nm), using a white substance, the fractal would never-the-less appear to have some amount of colour. This is due to structural coloration, meaning that the actual shape of an object (rather than its chemistry / pigments) gives it colour.

Examples in nature include butterfly wings, peacock's tail feathers, the iridescent colours when cutting through meat (due to muscle fibre ends), and opals. These iridescent effects occur when the object has a regular structure at scales similar to light's wavelength, much like a diffraction grating. But it isn't just iridescent colours that are produced structurally, for instance the bright blue colours of parrots are due to the fine scale shape of the feathers.

So what colour are 3D fractals? What colour is a 3D Menger sponge, or a Mandelbox or Mandelbulb? The answer is that it is pretty difficult to find out. The equations of light scattering are quite complicated, and also difficult to simulate as calculations and reflections happening at the nanoscale effect the result at the macroscopic scale (to colour a 1cm fractal for instance). One needs to get statistical approximations of the light dynamics, luckily A Yu Cherny and E M Anitas from the Moscow Institute of Physics have some very recent papers on this subject. In this paper they look at the spectrum of light reflected from a cluster fractal, in this case a Cantor cluster:
gives the spectrum:
The horizontal axis qL is proportional to the frequency of light, and the vertical axis gives relative intensity of reflected light. The downwards slope suggests that this fractal would be a blueish sort of colour, since it has a similar slope to classic Rayleigh scattering which gives the sky its blue colour. 
They also work out the spectral response of a Koch snowflake-like 2D fractal (from in-plane light rays), with a similar power-law downward slope, but less steep in this case. And also for 3D Viscek fractsls (void trees):
giving the spectrum:
which again has a gentler slope, so I would expect it to be a less saturated blue, if I'm interpreting it correctly. 

For more complex fractals statistical plots are no good as the colour could well change with location. A good example is the Mandelbox fractal (I'm using scale -1.5), where some areas are clearly more rough than others. To render the fractal, I use the program Fragmentarium as the algorithm can be modified. There are several effects that could be simulated but I have tried just two:
  1. These fractals are rendered as a very high detail but smooth surface, which is controlled by the minDist bailout threshold, so long as the other iteration counts are high. I model light of different wavelengths as reacting to this surface of different minDist values respectively, consequently high wavelength light will only react to smoothed features and low wavelength light will reflect off all the smaller features. So I simply render the fractal three times using a different minDist, for red green and blue light respectively. 
  2. The Bragg's Law effect shows how a coherent light, when bouncing off two surfaces of different depths, interferes on the return journey, either destructively (darker in that wavelength) or constructively (brighter in that wavelength). This is the principle effect happening in the iridescent natural examples. I simulate it by taking nearby points in screen-space, and performing this interference based on their relative depths. 
The result is shown on this 1cm dimension Mandelbox, there appears to be no coloration:
However this may be largely expected on this fractal as most of it is very low roughness surfaces (even the rough parts have a lot of these flat box faces, larger than the wavelengths of light). But if we look closer: 

 here we see a brown colour on the thinnest cracks, and come blue colours amoungst the rougher areas.
This 'lilly' shows a brown coloration at its edge, and blue amoung the tiny encrusted boxes.
For a smaller (4mm) Mandelbox, the effect is a bit stronger, but the actual colours haven't changed. 

Examining the effect of the two simulated effects above, it appears that they have a very similar effect, both causing the redder and the bluer areas in roughly the same places. Since the 2nd effect is slower and less obvious in its correctness, I have chosen to exclude it, and can just do the first effect, but exaggerating the wavelength difference between red,green,blue to compensate. This is done in real-time using this Fragmentarium script

It is important when using this script to turn the 'pigment' colour off (colourIterations=0), antialiasScale=0, dither=0, and set maxRaySteps and (Mandelbox) Iterations to high. 

Here is the result for the 1cm Mandelbox:

How about the 1cm Mandelbulb? Well, the same code gives this:
One problem with this method is that, while the large minDist lighting gives the expected smoothing, it also enlarges the fractal. consequently, edges could become coloured as they are only seen in the lower wavelengths. The images below removed this effect, and I have also rendered in higher detail, with more colour channels (5), and adjusted the specularity to see what effect it had:
The results are very similar, and suggest that the colours in the images are not unwanted effects. Here are two areas of the -1.5 mandelbox that show consistent colds and warm colours respectively:
 


These results are all very primitive however, so my conclusions are really just conjectures at this point, here they are:

1. For a surface of increasing roughness, the surface goes from a mirror towards a reducing sharpness specular component and increasing diffuse component, the specular component is a warmer colour and the diffuse a cooler colour. This is very similar to the Tyndall effect, but rather than scattering of light going through a 'dust' it is scattered reflections off surfaces. The colour gets stronger as roughness increases.
2. For much rougher surfaces, areas in the light and protrusions have a warmer colour as the higher frequencies are scattered away. and areas out of the light and in hollows and away from the specular reflection have a bluer colour as greater scattering is needed to get light from these areas.
3. For materials that absorb some of the incident light, I would expect the average colour to be warmer, as blues scatter more and so have more surface hits where absorption can happen.
4. For complex fractals the structural colour can be quite subtle, because:
    a. the coloration varies so much that it averages to a greyish colour
    b. many surfaces are low roughness, such as the -1.5 mandelbox, or the smooth mandelbulb areas
5. For complex (multifractal, varying scale) fractals the structural colour doesn't effectively change with the scale of the fractal. It just effects how noticeable it is. 

Regarding point 1, a good avenue here may be the Cook-Torrence model of specular reflection off rough surfaces, it includes a Fresnel term, which includes a term to the power of the wavelength, such that larger wavelengths give a stronger, sharper specular reflection, consistent with this point 1.

Update:
There is a nice article on 3D printing transmission gratings at the micrometre level. This may be a reasonable model for the 2.5D fractal here:
as it is somewhat similar in its shape. If this is the case, then unfortunately the low pillar height in this fractal places it off the graph in figure 2, but the extrapolation hints at a pale or aqua blue, when the pillar height is 1 micrometre. Perhaps a better paper is this, which has a very similar local shape. For pillar height 120nm the perceived colour was 530nm which is aqua. Moreover, they claim that this lower pillar height (the same as the pillar width) keeps its colour up to 45 degrees angle, which, given the shape of the fractal above, suggest that the whole thing would keep an aqua colour, rather than be too iridescent. However it should be noted that both papers use transparent material and the result probably depends on the relative refraction indices... so it doesn't necessarily hold that the same coloration would work with just a reflective material like metal.

Also a lab has in fact made a nanoscale 'sponge' fractal, which they call the fractal nanotruss:
As you can see, the smallest scale is around 500nm, so just about into the light wavelengths. The lead of this project has clarified that it does exhibit structural colour, she says it shows opalescence. So it is good to hear that the effect does happen in real examples... though it remains a question what effect the material properties has on the resulting colour. 

Thursday, November 3, 2016

Semi-dimensional shapes and other curves

I just discovered that shapes with half-integer dimensions have a simple square construction by subdividing by 4. An example is labelled the quadratic type 2 curve (or sometimes Minkowski sausage) with dimension 1.5. However this curve comes in a left and right handed form. Another example I made here:

It is also possible to build 2.5D surfaces, an example is here, however this surface intersects. It is possible to make a non-intersecting surface, which is approximately a 3D version of the above:

And here's a 1.5D curve in 3D space:
A 2.5D curve in 3D space is a bit more of a challenge

Monday, September 19, 2016

Crumpled surface fractals

When we say a rope is 3m long, we have in mind a metre as a straight line, and when we say our garden is 60m^2, the m^2 is a solid flat square. The line and the square are archetype examples of the measures of length and area respectively. When we measure the size of a curve between dimension 1 and 2, a sensible archetype is the variable angle Koch curve. The bend angle exemplifies a rough curve of any dimension between 1 and 2.

When it comes to rough surfaces there appears to be no useful archetype, parametrised by dimension. I give some good candidates for such an archetype here.

An interesting alternative is a fractal surface that is a non-smooth developable surface, so has zero Gaussian curvature (or, technically, its discrete equivalent) everywhere and could be made out of paper (if self intersection were allowed). It is not immediately obvious how to generate fractals that have this property.

The trick is to note that a Koch curve itself can be built from a sequence of corrugations, each twice the size of the previous, and we can perform these corrugations using reflections on the surface, which maintain the curvature (or discrete equivalent of curvature) of zero. One can then apply this to a surface by doing the corrugations in two directions:
Each iteration we pitch the surface by the bend angle, reflect in two horizontal planes to produce corrugations, then rotate the surface by the yaw angle, which is 90 degrees in this case. Above we see the first five iterations.

The bend angle is variable, giving a range of dimensions from 2 up to some value less than three:
Notice that the fractal is built of multiple square spirals. Here we see bend angles π/24, π/12, π/8 and π/6. It is not known at what point the surface begins to intersect, it could be any angle > 0, but visually, there seems to be little in the way of intersection, even at π/6, which is a nice feature to have in the main.
close ups for bend angle π/12:


Apart from the bend angle, there are multiple yaw angles possible:
yaw angle = π on the left gives an extruded Koch curve (reflected at the mid point), π/2 is my chosen default surface seen above, π/3 gives hexagonal spirals, and on the right is π*2/3 which seems quite similar.
A close up of these triangular patterns:
These look slightly less regular than the square pattern, so more like crumpled up foil. If we want a randomised crumple a good choice is π/phi (the golden ratio):
While it still shows a bit of a pattern, this is because we are looking at the centre point (0,0,0), if we picked an arbitrary square section of the surface it would give a good resemblance to crumpled paper:
Therefore the family of fractal surfaces defined by the yaw angle (and bend angle) generalise the Koch curve, polygonal spiral crumples and a random crumpled surface. And the bend angle allows any level of crumpling.

To my knowledge this is the first fractal developable surface, and could make for a simple and useful archetype for rough surfaces in nature. Unlike the previous ones I have given, this surface has no hills or troughs and so is more like a tree of ridges and a tree of valleys, which is quite a good model for many landscapes, it also seems to be quite a good description for rough rocky surfaces and of course crumpled paper.

If we define the bend angle as φ and the yaw angle as θ, then generation algorithm is as follows, starting with a flat sheet spanning (-0.5,-0.5,0) to (0.5,0.5,0):
  1. yaw around origin by θ degrees
  2. φ degrees
  3. calculate scale s so concertinas are multiples (harmonics) along the same axis: 
  4. scale (dilate) around origin by s
  5. reflect around the horizontal planes (0,0,m) and (0,0,-m) to concertina the surface, where and i is the iteration count, giving larger concertinas each iteration. k controls how large the smallest concertina is.
  6. go to 1. until nothing is being reflected.
The calculation of s requires some explanation. What we want is for the concertina for a given horizontal axis to have the same length (so the same number of bends) when we return to that axis, i.e. after a turn of 180 degrees. If it has the same length then the formula for m will ensure that subsequent concertinas will be exactly double then quadruple the size etc. These are called octaves, and a consequence of using precise octaves is that the fractal folds are the same across the surface. The calculation of s is simple for integer divisions of π, but for non-integer θ the function s(θ) is not continuous, and in the general case, for irrational numbers the exponent of s is 2/π.

Note that, while various yaw angles are possible, only the default, π/2 gives a simple mesh for all bend angles.
A simple mesh means that for any given maximum concertina size, the mesh is a repeating lattice (a tiling).

This fold map shows the folds up to the first 7 iterations. We can also view how the fold map changes with bend angle:
In this case the shade goes from light to dark from bend angle π/36 to 8π/36 (almost maximum). The small corrugations seem to vary surprisingly little with bend angle but the final large corrugations seem to vary most.
Here is a render in Blender, for bend angle 15 degrees, where the centre of yaw is the bottom right corner. Which is equivalent to just looking at an offset patch of the fractal, from (0,0) to (1,1). Notice that the bottom edge is a Koch curve of bend angle 15 degrees (but with out of plane bends to the curve). The right edge is also this Koch curve. The fractal square spirals are also less obvious in this picture. 

From these renderings it is clear that self intersection does take place at least in the 15 degree case, and most likely for all bend angles > 0. In the 15 degree case the overlap only seems to account for about 0.1% of the landscape area, and increases for larger bend angles. Therefore this fractal is immersed in 3D space, but not embedded within it. 

Here's bend angle 22.5. Can you spot the self intersection?

It is a bit hard getting a good sense of depth, both depth-of-field and fog reduce the quality. Here I try adding four different coloured local light sources and I think it helps to distinguish the depth. For bend angle from 0 to 22.5 degrees:

Although the actual folding of the triangle mesh is quite complicated as the iterations build up, the actual fold curve on the piece of paper tends to a constant shape, which when not doing the largest corrugations is a repeating curve. The single repeating pattern looks like this for a 7.5 degree bend angle:
So as the iterations increase all folds look like some repetition of this on the flat paper.

The Hausdorff dimension D is a function of the bend angle φ:
So the surface has dimension 2 when the bend angle is 0, and dimension 3 when it is 45 degrees, which is equivalent to the maximum bend (dimension 2) Koch curve on each axis. Since the surface has overlap, some of the 'volume' of such a surface is due to the overlap, just like a dimension 2 Levy curve.

Since the shape derives from a flat surface it is possible to texture the surface, here is an example using the golden ratio yaw angle and a random location, to give a crumpled look:

The code for this is available on https://github.com/TGlad/CrumpleSurface, it generates a .ply file. 
The executable is here, run with -h for help, and example meshes here:
This shape is also compared with other variable dimension surfaces here, and in this article.

Finally, here's a little sequence of random meshes going from 7.5 bend angle up to 30 degrees, with some forest texture and a road:
The rainforest effect works quite well with the rougher meshes. This whole technique is potentially quite useful for computer games because it uses a square texture without any distortion. For 90 degree yaw angles at least the shape tiles, along with the texture. In addition lower and higher detail versions are inherent and share the same properties. The downside is the self intersections happen, but at least with the random location case (like above) you can try out different random seeds to minimise this.