## cell_statistics.cc – Statistics for a single Voronoi cell

The `voronoicell`

class of Voro++ contains a large number of
routines for computing basic statistics about a computed Voronoi cell's
structure. This example code creates a very simple Voronoi cell, and uses it to
demonstrate the routines that are available. There are many routines with names
such as `face_perimeters`

and `vertices`

that will return
information about the cell directly to the calling program, sometimes making
use of STL vectors to pass back a variable amount of data. In addition, many of
these routines have a variant that is prefixed with
“`output_`

”, which prints the results to screen or to a
file – these routines are demonstrated in this example. Here, statistics
are only computed for a single cell, but the same routines can be used to
analyze complete Voronoi packings using the `print_all_custom`

routine, which is demonstrated in the
custom_output.cc example.

A simple Voronoi cell is created in lines 19–22 by initializing a cube and then removing an edge with a single plane cut to make a pentagonal prism. This cell is saved in gnuplot format and is shown above. The code then starts to calculate statistics of the cell, and these can roughly be divided into those that are based on vertices, edges, faces, and the cell volume. The vertex-related statistics are shown first:

`Total vertices : 10`

Vertex positions : (-1,-1,-1) (1,-1,-1) (-1,1,-1) (0,1,1) (-1,-1,1)

(1,-1,1) (-1,1,1) (1,0,1) (1,0,-1) (0,1,-1)

Vertex orders : 3 3 3 3 3 3 3 3 3 3

Max rad. sq. vertex : 3

Vertex positions : (-1,-1,-1) (1,-1,-1) (-1,1,-1) (0,1,1) (-1,-1,1)

(1,-1,1) (-1,1,1) (1,0,1) (1,0,-1) (0,1,-1)

Vertex orders : 3 3 3 3 3 3 3 3 3 3

Max rad. sq. vertex : 3

For this cell, all of the vertices have the usual order of 3, and there are no higher-order cases. The vertex positions and orders are always displayed in the same order, allowing for scripting languages to match the entries in the two lists. The last entry shows the maximum distance squared of a vertex from the cell center, which in this case is 3=1²+1²+1². The edge-based statistics are as follows:

`Total edges : 15`

Total edge distance : 24.8284

Face perimeters : 7.41421 6 8 8 6 6.82843 7.41421

Total edge distance : 24.8284

Face perimeters : 7.41421 6 8 8 6 6.82843 7.41421

Since each edge is counted twice when computing the face perimeters, the sum of these adds up to twice the total edge distance. The face-related statistics are:

`Total faces : 7`

Surface area : 21.8284

Face freq. table : 0 0 0 0 5 2

Face orders : 5 4 4 4 4 4 5

Face areas : 3.5 2 4 4 2 2.82843 3.5

Face normals : (0,0,-1) (1,-0,0) (0,-1,0) (-1,-0,-0) (0,1,-0)

(0.707107,0.707107,-0) (0,0,1)

Face vertices : (1,8,9,2,0) (1,5,7,8) (1,0,4,5) (2,6,4,0)

(2,9,3,6) (3,9,8,7) (3,7,5,4,6)

Surface area : 21.8284

Face freq. table : 0 0 0 0 5 2

Face orders : 5 4 4 4 4 4 5

Face areas : 3.5 2 4 4 2 2.82843 3.5

Face normals : (0,0,-1) (1,-0,0) (0,-1,0) (-1,-0,-0) (0,1,-0)

(0.707107,0.707107,-0) (0,0,1)

Face vertices : (1,8,9,2,0) (1,5,7,8) (1,0,4,5) (2,6,4,0)

(2,9,3,6) (3,9,8,7) (3,7,5,4,6)

The face frequency table shows the total number of faces that have different numbers of edges; in this case there are two pentagonal faces and five quadrilateral faces. The following lines list a complete breakdown of information about each face. Each of these routines outputs the faces in the same order, to make it easy for scripting languages to match the entries in each list. The volume-related statistics are:

`Volume : 7`

Centroid vector : (-0.0952381,-0.0952381,0)

Centroid vector : (-0.0952381,-0.0952381,0)

For this Voronoi cell, the centroid vector can be calculated analytically to be (-2/21,-2/21,0) which matches this result.

## Code listing

1: // Simple cell statistics demonstration code 2: // 3: // Author : Chris H. Rycroft (LBL / UC Berkeley) 4: // Email : chr@alum.mit.edu 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; 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: // Remove one edge of the cell with a single plane cut 22: v.plane(1,1,0,2); 23: 24: // Output the Voronoi cell to a file in gnuplot format 25: v.draw_gnuplot(0,0,0,"simple_cell.gnu"); 26: 27: // Output vertex-based statistics 28: printf("Total vertices : %d\n",v.p); 29: printf("Vertex positions : ");v.output_vertices();puts(""); 30: printf("Vertex orders : ");v.output_vertex_orders();puts(""); 31: printf("Max rad. sq. vertex : %g\n\n",0.25*v.max_radius_squared()); 32: 33: // Output edge-based statistics 34: printf("Total edges : %d\n",v.number_of_edges()); 35: printf("Total edge distance : %g\n",v.total_edge_distance()); 36: printf("Face perimeters : ");v.output_face_perimeters();puts("\n"); 37: 38: // Output face-based statistics 39: printf("Total faces : %d\n",v.number_of_faces()); 40: printf("Surface area : %g\n",v.surface_area()); 41: printf("Face freq. table : ");v.output_face_freq_table();puts(""); 42: printf("Face orders : ");v.output_face_orders();puts(""); 43: printf("Face areas : ");v.output_face_areas();puts(""); 44: printf("Face normals : ");v.output_normals();puts(""); 45: printf("Face vertices : ");v.output_face_vertices();puts("\n"); 46: 47: // Output volume-based statistics 48: v.centroid(x,y,z); 49: printf("Volume : %g\n" 50: "Centroid vector : (%g,%g,%g)\n",v.volume(),x,y,z); 51: 52: }