Rendering swept fractals

No Comments »

At the start of the 1980s, the late mathematician Benoît Mandelbrot made a discovery that was to capture the public imagination in a way that nobody – including himself – could have predicted. Encoded within a simple mathematical construct he found an infinity of detail: rich, enormously varied, and possessing previously unseen properties of fractal self-similarity. The computer visualisation of this construct – known as the Mandelbrot set (Fig 1, below) – sparked international interest and made fractals into some of the most iconic images of the era. At the time, Mandelbrot’s work served as sort of poster child, not only for the rapidly emerging field of chaos theory, but also in computer-aided mathematics, engineering and computer graphics. Its beauty and intrinsic complexity made it the subject of popular debate, grounded in maths but also extending into more foreign territories such as modern art. Today, Mandelbrot’s work remains enormously popular, the dramatic rise in consumer computing power having lead to fractal renderers being implemented across a plethora of systems and platforms. Their qualities are especially favoured by students of computer graphics, many of whom consider synthesizing an image of the Mandelbrot set almost as a rite of passage.

 

Fig 1: A zoomed-in portion of the Mandelbrot set.

Practically speaking, a basic fractal renderer is almost absurdly trivial to implement, relying as it does on a two-line, iterated function. The premise is simple. The x and y axes on a computer screen represent the real and imaginary axes on the complex plane. For each pixel, a complex iterator, z, is initialized to its screen coordinate: c = x + iy where z(0) = c. The system is then cycled repeatedly so that z(n+1) = z^2(n) + c. At each iteration the complex magnitude of is computed. If this value remains stable and does not diverge, its starting point at c is a member of the set (see Fig 2 below). For the remaining points – those that fly off into infinity – we can visualise how long each one takes to ‘escape’ by using a colour/intensity encoding. Since points further from the boundary of the set tend to escape faster than those close to its perimeter, the set proper appears to radiate an arua when rendered using a continuous escape time algorithm (see Fig 1 above). This encoding also gives rise to the characteristic forks and spirals so often seen in images of fractals.

Fig 2: A binary map of points that are elements (black) or non-elements (white) of the Mandelbrot set

A close cousin of the Mandelbrot set, the Julia set, is based on the same mechanics. In its case, however, z(n+1) = z^2(n)  + q, where q is a complex parameter and z(0) = c. This reliance on both q and c as variables means the Julia set is parametrically 4-dimensional. Thus, displaying it on a screen requires taking a 2-D slice for some fixed value of  p.

All of this raises an interesting question: as a 4-dimensional object, it is not possible for us to “see” the compete set on a 2-D plane except as slices in c. However, is there some way we can sample intervals in all dimensions simultaneously, thereby communicating more information about the nature of the fractal in a single image?

We start by considering each q as a plane in some pixel (x, y). A pixel, as a scalar tuple, could therefore be used to encode the integral over some subset of points in q. Choosing a good subset is critical since we’re looking to extrapolate an image which considers both intervals in both domains c and q, and yet also yields an image that is visually pleasing. We can experiment by sampling randomly over q for each pixel but quickly find that the result resembles an “out-of-focus” version of the original fractal. This analogy to a camera lens is a good one because it describes a similar phenomenon; we are essentially defocusing the fractal in the q domain. Better, then, might be integrating along a locus of points that satisfies some given criteria. We experiment by making our interval in p conform to |p| = 1. The results are below:

 

Fig 3: a) Focussed and b) Swept Mandelbrot sets.

On the left is the ‘focussed’ fractal, its interval in q restricted to a single point on the origin. On the right is the integrated version where samples in q are a distance of 1 from the origin. Although the ghost of the original Mandelbrot set is visible, other details have appeared due to the sweep formed through the domain of q. In particular, asymmetric contours that do not follow the shape of the set are clearly visible. So it seems we can produce a more interesting image by sweeping and integrating. Furthermore, the choice of locus shape and position strongly affects the pattern and formation of the swept image. Unfortunately, the example above has a distinctly muddy feel to it with “interesting” details appearing clouded and indistinct. Can we do better?

Instead of taking the mean value of the colour encoding over the sweep locus, we can instead calculate the resultant intensity based upon the second central moment (the variance) of the integral function. This means that if a pixel has a non-zero mean but a zero variance, the output will be black. This serves to attenuate the regions of the fractal that do not change and also to enhance regions where the locus passes close to the set boundary. This is particularly beneficial since the Julia set is locally connected (hypothetically, at least) and allows for impressive plasma-like arcs of light. We can improve our integrator still further by choosing the sample colour based upon the its position on the locus rather than based on its escape time value.

 

Fig 4: More swept Mandelbrot sets, rendered using a second moment sample scheme.

The image above was rendered using a spiral locus (inset) and a spectral gradient map to determine the colour. By temporally varying the phase of the spiral, we can transform the still image above into an animation:

Although similar in form to the popular class of fractals based on iterated function systems (flames), swept fractals do not offer the same versatility. This is due to the fairly limited choice of underlying recursive formulae which defines the extent of the set. However, it is still possible to get different images by using variations on the Mandelbrot and Julia sets. Amongst these is the Manowar set (below) which is simpler and results in the shape of the locus itself becoming clearly visible in the rendered image:

Fig 5: A swept fractal sampled from the Manowar set.

Messing around with fractals can become an enormous time sink, not least because you never really get the same image twice. The interested reader is directed to articles on the Buddhabrot fractal, the 3D Mandelbub and Quaternion fractals, and the multitude of fractal zoomers available to download online. Other interesting resources include fractional brownian motion for texture generation and the use of fractals in  image compression (PDF link).

Enjoy!

Capitulum sampling

No Comments »

A recent entry on the (sadly now defunct) computer graphics message board OMPF.org directed my attention to what, in my opinion, qualifies as one of the nature’s most beautiful mathematical structures. I’m referring to the sunflower capitulum, an organic arrangement that ensures an optimal packing of seeds in a circular bed based on growth over time from a single point (a process known as phyllotaxis).


The capitulum of a sunflower. Image courtesy of Unitopia (Commons licenced).

The capitulum is distinctive because its growth program is based on the golden angle, a constant which has deep roots in both mathematics and the arts. New seeds are arranged about the central axis, each at a distance proportional to the square root of its rank and at deflection of ~137.5 degrees from each predecessor. As the pattern builds, the irrational nature of the seed angle prevents periodicities in the arrangement and eliminates gaps. What’s immediately eye-catching about the capitulum are the concentric spirals that radiate from its centre. Interestingly, these curves are not “by design” insofar as they are not an intended consequence of the growth program, but are highlighted by our brain’s natural predisposition to spot patterns.

A strategy for generating points that mimic capitula distributions was introduced by Helmut Vogel [1] who proposed that a spiral based on particle energies in a cyclotron could be used to seed points from the centre outward. His paper also established a mathematically rigorous framework for the existence of the spirals, predicting where each one begins and ends as a function of the Fibonacci sequence. I won’t go into Vogel’s work in detail here except to say that it’s a interesting read and neatly proves the case for the emergence of the golden ratio in the natural world.

The synthetic capitulum and its cyclotron growth spiral.

So how does this have a bearing on rendering? In the blog post linked from the OMPF thread, Mukund Sivaraman suggested that the sunflower capitulum would make an good sampling pattern for a thin-lens camera model. Sampling, or the selection of points over the domain of an integral, is a topic of key importance in computer rendering since the selection of an appropriate algorithm is key to optimal image quality. While the capitulum would certainly do a good job of sampling a thin lens, my thoughts were drawn more to its potential in hemispherical sampling. The rationale behind this is based on the relative difficulty in placing points over a unit hemisphere, and requires a quick delve into the background math to explain.

Complete sphere’s are known as locally-Euclidean 2-manifolds. This means that an object placed on the surface (like a person on the surface of a planet) has two degrees of freedom in which to move (or walk). On top of this, spheres exist in 3-dimensional Euclidean space so every 2-manifold coordinate has a 3D spatial counterpart. So far, so good.

In optics, hemispheres represent a integral domain over which light arrives at a given point on a surface. To work out how much light is reflected towards an observer, this integral must be solved numerically with the minimum of error. Consequently, it is advantageous to be able to place samples evenly over the entirety of the hemisphere. This gives us a problem of how to evenly sample a surface which is locally 2-dimensional but which appears rounded in 3-dimensional space.

We need an analogy. In the same way that a globe of the earth cannot be flattened out into a wall map without some degree of warping, so a 2D sheet cannot be wrapped around a hemisphere without encountering the same problem. Think of it as trying to wrap a football with a single sheet of plastic food wrap covered in small dots. No matter how you fold it, you’ll end up either with bits that overlap or regions that are stretched. This in turn disturbs the original spacing of the dots. In the end, you must accept that the sheet will get warped and carefully consider the initial positions of the dots be so that after you’ve wrapped the ball, they’ll appear to be evenly distributed.

See the problem?

Stratified sampling Jittered sampling

Practically speaking, one of most basic schemes of sample placement is a simple stratification using polar coordinates as shown above. Although the samples are evenly-spaced in the polar coordinate system, the hemispherical “wrapping” function results in severe distortion around the axial origin. Jittering (shaking) the points removes this uniformity at the expense of introducing entropy. Visually, this makes the render more noisy (grainy).

Other, more sophisticated schemes perform better. HEALPix [2] is a recent development with produces a hierarchical, low-distortion distribution using a series of tiles which serve as the strata. I personally love this algorithm, particularly when combined with the polyomino sampling scheme of Ostromoukhov et al [3]. Unfortunately, methods based on tiling also tend to be comparatively complex, requiring separate parameter schemes for each tile.

Sticking to strictly 2D parameterisations, we encounter a further problem: most schemes are unable to generate arbitrary numbers of sample points while still ensuring a consistently even stratification. The polar scheme above, for example, can only yield sample sets whose size is divisible by 4. In the case of HEALPix, hierarchical subdivision means set sizes must follow the sequence 12, 48, 192, etc.

So what does all this have to do with the capitulum? Well first, being based on an optimally-packed, radial growth program in polar coordinates, it can be made to cover a hemisphere with no warping, requiring only slight modification to the original cyclotron spiral. Secondly, since points are seeded on the spiral’s curve, it represents a 1D parameterisation which places no restrictions on the size of its sample set.

Capitulum sampling

Sounds promising, but the question is: how does this translate to the final render? The table below shows a zoomed-in portion of a Cornell box rendered using random, jittered stratified and jittered capitulum schemes. 50 samples were taken per pixel. It should be noted that a certain amount of jittering (entropy) is necessary with any sample scheme in order to prevent aliasing. The underlying quality of the hemisphere mapping dictates how little jittering it’s possible to get away with. Click each image to see the full version.

 Random sampling
(worst case)
Stratified/quasi-
random sampling
Capitulum sampling Reference

Clearly, capitulum sampling performs at least as well and in many places better than jittered stratified or quasi-random sampling (not shown, but virtually identical). Compared to purely random sampling, it performs better still.

In summary, the capitulum sampling scheme presents a simple, fast and elegant way of equidistributing samples over the unit hemisphere. While using this approach is advantageous in numerous situations, it’s not a magic bullet. First and foremost, capitulum sampling is neither an unbiased nor a progressive sampling method. This is bad news for unbiased MCMC renderers like Maxwell or Fry Render. Secondly, it’s 1D parameterisation makes it useless for algorithms such as radiance caching which require a 2-manifold space over which to compute gradients.

Despite these drawbacks, it’s still pleasing to see how a pattern perfected by the natural evolution of plant growth has a positive impact when applied to a problem as artificial as global illumination.

References:
[1] A Better Way to Construct the Sunflower Head. Vogal, H. 1978.  http://www.sciencedirect.com/science/article/pii/0025556479900804
[2] HEALPix: A Framework for High-Resolution Discretization and Fast Analysis of Data Distributed on the Sphere. Górski, K.M. et al. 2005. http://iopscience.iop.org/0004-637X/622/2/759/
[3] Sampling with Polyminoes. Ostromoukhov et al. 2007. http://www.iro.umontreal.ca/~ostrom/SamplingWithPolyominoes/

Experiments with physically-based metals

No Comments »

During a discussion with a friend over a render stress test [1] (pictured below) I was asked whether I had used a complex index of refraction (IOR) to model the appearance of the metallic objects. I was intrigued, partly because I had in fact not considered it, but also because, until that point, I hadn’t contemplated how an attribute used to define reflection and transmission in refractive media could also be directly applied to metals. This gap in my knowledge was born out of a simple – though misguided – leap of logic: metals do not transmit light in the same way as dielectrics, and therefore should not have meaningful IORs. Clearly, however, this was not true and so warranted some investigation. 

 

 

In this case, the relevant formulae are the Fresnel equations, an elegant derivative of Maxwell’s work on electrodynamics. They are commonly implemented into ray tracers as a means to determine what proportion of a beam of incident light is reflected and what is refracted as it passes across the interface between two different materials. The equation,


yields the reflectance coefficient, rho R, given the IOR of both materials (etas 1 and 2) and the angles of incidence and transmission (thetas I and T). With a bit of substitution, theta T can be eliminated leaving a compact representation of rho R.

For dielectrics (air, water and so on), eta is a real number. This means that it can be manipulated using elementary arithmetic and directly indicates how much of the incident light is reflected from a surface. Furthermore, as any A-level physics student can tell you, tables of dielectric IORs are widely and conveniently available. Metals, however, exhibit different reflective properties due to the effects of absorption on the incident light waves. This means that for metallic substances, the IOR must have both a real and imaginary component [2]. Solving the Fresnel equations for complex numbers requires a couple of awkward operations, namely finding the square root of a complex number. Eventually, however, a real result can be arrived at by computing the modulus of the complex value of rho R.

The upshot of all this is that, given sufficiently complete data about the IOR of a metal, the Fresnel equations can be used to comprehensively describe its reflectivity under all illumination conditions. Before this can be incorporated into a renderer, however, we must first know about how the index of refraction changes with the wavelength of light. Gold, for example, absorbs blue wavelengths and so appears yellow. Copper more readily absorbs blue and green and so appears pink.

With dielectrics the situation is simplified thanks to convenient formulae that dictate how refractive index changes with wavelength. For metals, however, there are is no such model and we must instead rely on data captured from real-world samples. Measuring the complex refractive index of a metal requires a piece of equipment known as an ellipsometer. This tool can precisely record how much light is reflected, not only according to incident angle, but also by spectral frequency. Here, the FreeSnell project provides us with an invaluable asset in the form of tables of wavelength-dependent, complex IOR for an array of different metals. These tables are often referred to as NK data due to the real/imaginary pairs of numbers represented as N + iK.


• The visible spectrum with wavelengths in nanometers [3]

For renderers that operate in a trichromatic rather than a spectral colour space, these data require a bit of conversion before they can be practically used. My goal was to extrapolate an RGB specular absorption coefficient for a given angle of incidence (AoI), theta I. This meant integrating the Fresnel reflectivity of the surface over all wavelengths for a single AoI. Using the standard CIE 1931 spectral conversion table (derived from the visible spectrum pictured above) meant there were a couple of hundred wavelength intervals to integrate over. Hence, the operation of calculating a single RGB absorption value was far too computationally intensive to be done per-pixel at render time. I therefore resorted to a precomputed look-up table with a few thousand entries.

An hour or so of coding and a quick session with GnuPlot later, and I arrived at the following graph for the Gold NK dataset.


Here, each curve indicates the reflectivity coefficient for red, green and blue light over varying angles of incidence. The first thing that struck me about this graph is how the curves all tend to one as theta tends to Pi / 2 (grazing angle with the surface). Practically, this means that gold should appear white if viewed from a shallow enough angle. Plugging the data into my ray tracer and I was able to generate realistic metallic surfaces that could be swapped by simple altering the NK input file.


The renders above show gold, platinum and copper NK materials respectively. A bump map was also applied to give a scratched effect.

Implementing physically-based metals was an interesting experiment, although the overall effect is quite subtle. Since all metal reflectivity tends to 1 at viewing angles approaching 90 degrees to the normal, the effects demonstrated above could easily be faked using an iridescent shader controlled using Schlick’s approximation of the Fresnel function. Since many commercial renderers now include NK metal support as a standard shading option, it would be interesting to see whether this extra attention to detail is really worth the complex model that underpins it.

References:
[1] Rings and Coins render challenge.
[2] EE4344 Optical System Design Labs: http://www-ee.uta.edu/online/stelmakh/ee4344/Labs/First%205%20Labs/FresnelEquationsLab2.pdf
[3] Wikipedia.

 

Moving to WordPress…

No Comments »

Rather than trying to maintain a static landing page for my site, I’ve decided to switch to a more flexible WordPress blog for posting updates and articles of interest. Check back soon for updates!