POV-Ray macro to create eggs

In a recent video, Numberphile has interviewed Val Narushin, a researcher who has co-authored a paper about the mathematical shape of eggs. The formula he has designed has the interesting property of being able to modelise all four types of egg with only four parameters. The formula is complicate, but not difficult to implement, so I've jumped on the occasion to make a POV-Ray script based on it to generate nice looking eggs.

There was two difficulties to create that script: define how to randomly select the value of the four parameters of Narushin's formula, and define how to randomly create the texture.

The four parameters are: the length of the egg $$L$$, its maximum radius $$B$$, the deviation of the maximum radius from the center $$w$$, and the radius at a quarter length from the tip of the small end $$D$$. The length and maximum radius are easy to understand, so I've decided to use them as the input of the main macro. The two others are much less intuitive, so I decided to calculate them automatically based on the two first ones.

To control more easily the two other parameters, I'm using the ratio of the maximum radius to the length, $$r$$, and I arbitrarily impose that $$r=0.5$$ means the egg as a perfect spherical shape. The smaller $$r$$ becomes the more "triangular" the shape becomes.

The deviation of the maximum radius from the center can theoritically go from 0 to half the length, but the nearer from half the length it gets the less realistic the shape becomes. This is illustrated in figure 4 of Narushin's paper. Then I'm using $$r$$ to control it and keep the shape realistic, as follow: $$w=L/8*rand()*(1-r)$$. A perfectly spherical egg gives $$w=0$$, as expected, and less spherical eggs allow for more deviation, up to $$w=\frac{2L-B}{15L}$$.

The value of $$D$$ should always be less than those of $$B$$, and such as it produces a perfect sphere when $$r=0.5$$. It's easy to calculate that for the perfect sphere $$D=sin(cos^{-1}(0.5))B\approx 0.866025B$$. Then I choose to calculate $$D$$ as follow: $$D=B*0.866025*(a+r*(1-a))$$ where $$a=.75+.25*rand()$$.

About the texture, I've simply referred to pictures of bird eggs on the Internet and freely created one that look similar with a multiple layer approach. The first layer is the base color of the egg: one randomly chosen color biased toward light tone as bird eggs seem to be so, with subtil variations to avoid an unnatural uniformity. On top of that first layer, a random number of layers provide the stains usually seen on eggs. Each layer as a random stain color and stain size. I addition, I set the values for finish phong and normal bump randomly within ranges that render similar to the pictures I've referred to.

Finally I've split the macro into sub-macros such as it's possible to create the shape with given parameters or random parameters, with or without a random texture.

The result looks like the image below (click to enlarge):