superellipsoid.cc – Construction of a superellipsoid

A superellipsoid

This code is a variation on the single Voronoi cell example. It applies plane cuts in a similar manner, but weights them in order to create a superellipsoid of the form x4+y4+z4=r4.

The code applies 5000 random plane cuts, and outputs the results in POV-Ray and Gnuplot formats. The POV-Ray header file “superellipsoid.pov” can be used to generate the image above.

Code listing

 1: // Superellipsoid example code
 2: //
 3: // Author   : Chris H. Rycroft (LBL / UC Berkeley)
 4: // Email    : [email protected]
 5: // Date     : August 30th 2011
 6: 
 7: #include "voro++.hh"
 8: using namespace voro;
 9: 
10: // This function returns a random floating point number between 0 and 1
11: double rnd() {return double(rand())/RAND_MAX;}
12: 
13: int main() {
14:         double x,y,z,rsq,r;
15:         voronoicell v;
16: 
17:         // Initialize the Voronoi cell to be a cube of side length 2, centered
18:         // on the origin
19:         v.init(-1,1,-1,1,-1,1);
20: 
21:         // Cut the cell by 5000 random planes that are scaled to create a
22:         // superellipsoid
23:         for(int i=0;i<5000;i++) {
24:                 x=2*rnd()-1;
25:                 y=2*rnd()-1;
26:                 z=2*rnd()-1;
27:                 rsq=x*x*x*x+y*y*y*y+z*z*z*z;
28:                 if(rsq>0.01&&rsq<1) {
29:                         r=1/sqrt(sqrt(rsq));
30:                         x*=r;y*=r;z*=r;
31:                         v.plane(x*x*x,y*y*y,z*z*z,x*x*x*x+y*y*y*y+z*z*z*z);
32:                 }
33:         }
34: 
35:         // Output the Voronoi cell to a file, in the gnuplot format
36:         v.draw_gnuplot(0,0,0,"superellipsoid.gnu");
37: 
38:         // Output the Voronoi cell to a file in POV-Ray formats
39:         v.draw_pov(0,0,0,"superellipsoid_v.pov");
40:         v.draw_pov_mesh(0,0,0,"superellipsoid_m.pov");
41: }