tetrahedron.cc – Using the plane wall object to create a tetrahedron

Voronoi tessellation in a tetrahedron

This example code demonstrates the use of the wall_plane class to carry out a Voronoi cell computation in a tetrahedron. On lines 37 to 40 four planar walls are created and added to the container. From lines 44 to 51, the code adds a number of points to the container. To test whether the points are within the tetrahedron, the code makes use of the point_inside() routine, which returns true or false depending on whether the point is within the container boundaries and within the walls.

The code computes the Voronoi cells in Gnuplot and POV-Ray formats, and the POV-Ray output is shown above, using the scene header file “tetrahedron.pov”. Unlike the cylinder and frustum examples, the Voronoi computation for this case is exact, and there are no errors associated with approximating curved surfaces with planes.

Code listing

 1: // Tetrahedron example 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: // Set up constants for the container geometry
11: const double x_min=-2,x_max=2;
12: const double y_min=-2,y_max=2;
13: const double z_min=-2,z_max=2;
14: 
15: // Set up the number of blocks that the container is divided
16: // into
17: const int n_x=7,n_y=7,n_z=7;
18: 
19: // Set the number of particles that are going to be randomly
20: // introduced
21: const int particles=64;
22: 
23: // This function returns a random double between 0 and 1
24: double rnd() {return double(rand())/RAND_MAX;}
25: 
26: int main() {
27:         int i=0;
28:         double x,y,z;
29: 
30:         // Create a container with the geometry given above, and make it
31:         // non-periodic in each of the three coordinates. Allocate space for 8
32:         // particles within each computational block.
33:         container con(x_min,x_max,y_min,y_max,z_min,z_max,n_x,n_y,n_z,
34:                         false,false,false,8);
35: 
36:         // Add four plane walls to the container to make a tetrahedron
37:         wall_plane p1(1,1,1,1);con.add_wall(p1);
38:         wall_plane p2(-1,-1,1,1);con.add_wall(p2);
39:         wall_plane p3(1,-1,-1,1);con.add_wall(p3);
40:         wall_plane p4(-1,1,-1,1);con.add_wall(p4);
41: 
42:         // Randomly insert particles into the container, checking that they lie
43:         // inside the tetrahedron
44:         while(i<particles) {
45:                 x=x_min+rnd()*(x_max-x_min);
46:                 y=y_min+rnd()*(y_max-y_min);
47:                 z=z_min+rnd()*(z_max-z_min);
48:                 if (con.point_inside(x,y,z)) {
49:                         con.put(i,x,y,z);i++;
50:                 }
51:         }
52: 
53:         // Output the particle positions and the Voronoi cells in Gnuplot and
54:         // POV-Ray formats
55:         con.draw_particles("tetrahedron_p.gnu");
56:         con.draw_cells_gnuplot("tetrahedron_v.gnu");
57:         con.draw_particles_pov("tetrahedron_p.pov");
58:         con.draw_cells_pov("tetrahedron_v.pov");
59: }