Minkowski test code.
[voro++.git] / trunk / examples / no_release / tri_mesh.cc
blob5f83359cf82f626a6965af239bb6b670450b7f3d
1 // Voronoi calculation example code
2 //
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : August 30th 2011
7 #include "voro++.hh"
8 using namespace voro;
10 #include <vector>
11 using namespace std;
13 // Set up constants for the container geometry
14 const double x_min=-1,x_max=1;
15 const double y_min=-1,y_max=1;
16 const double z_min=-1,z_max=1;
18 // Set up the number of blocks that the container is divided into
19 const int n_x=6,n_y=6,n_z=6;
21 // Set the number of particles that are going to be randomly introduced
22 const int particles=1;
24 // This function returns a random double between 0 and 1
25 double rnd() {return double(rand())/RAND_MAX;}
27 int main() {
28 int i,j,id,nv;
29 double x,y,z;
31 // Create a container with the geometry given above, and make it
32 // non-periodic in each of the three coordinates. Allocate space for
33 // eight particles within each computational block
34 container con(x_min,x_max,y_min,y_max,z_min,z_max,n_x,n_y,n_z,
35 false,false,false,8);
37 // Randomly add particles into the container
38 for(i=0;i<particles;i++) {
39 x=x_min+rnd()*(x_max-x_min);
40 y=y_min+rnd()*(y_max-y_min);
41 z=z_min+rnd()*(z_max-z_min);
42 con.put(i,x,y,z);
45 // Sum up the volumes, and check that this matches the container volume
46 c_loop_all cl(con);
47 vector<int> f_vert;
48 vector<double> v;
49 voronoicell c;
50 if(cl.start()) do if(con.compute_cell(c,cl)) {
51 cl.pos(x,y,z);id=cl.pid();
52 printf("Particle %d:\n",id);
54 // Gather information about the computed Voronoi cell
55 c.face_vertices(f_vert);
56 c.vertices(x,y,z,v);
58 // Print vertex positions
59 for(i=0;i<v.size();i+=3) printf("Vertex %d : (%g,%g,%g)\n",i/3,v[i],v[i+1],v[i+2]);
60 puts("");
62 // Loop over all faces of the Voronoi cell
63 j=0;
64 while(j<f_vert.size()) {
66 // Number of vertices in this face
67 nv=f_vert[j];
69 // Print triangles
70 for(i=2;i<nv;i++)
71 printf("Triangle : (%d,%d,%d)\n",f_vert[j+1],f_vert[j+i],f_vert[j+i+1]);
73 // Move j to point at the next face
74 j+=nv+1;
76 puts("");
77 } while (cl.inc());
79 // Output the particle positions in gnuplot format
80 con.draw_particles("random_points_p.gnu");
82 // Output the Voronoi cells in gnuplot format
83 con.draw_cells_gnuplot("random_points_v.gnu");