Bugfix in search_for_outside_edge routine.
[voro++.git] / branches / exact / examples / interface / odd_even.cc
blobbe08b16260fbb37f9aaec70929f3636ef9e51824
1 // Odd/even face coloring 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 // This function returns a random floating point number between 0 and 1
11 double rnd() {return double(rand())/RAND_MAX;}
13 int main() {
14 unsigned int i;
15 double x,y,z,rsq,r;
16 voronoicell v;
18 // Initialize the Voronoi cell to be a cube of side length 2, centered
19 // on the origin
20 v.init(-1,1,-1,1,-1,1);
22 // Cut the cell by 250 random planes which are all a distance 1 away
23 // from the origin, to make an approximation to a sphere
24 for(i=0;i<250;i++) {
25 x=2*rnd()-1;
26 y=2*rnd()-1;
27 z=2*rnd()-1;
28 rsq=x*x+y*y+z*z;
29 if(rsq>0.01&&rsq<1) {
30 r=1/sqrt(rsq);x*=r;y*=r;z*=r;
31 v.plane(x,y,z,1);
35 // Calculate the orders of the faces and the normal vectors
36 vector<int> f_vert;
37 vector<double> nor;
38 v.face_orders(f_vert);
39 v.normals(nor);
41 // Output POV-Ray planes with textures based on whether a face is
42 // composed of an odd or even number of edges
43 const char* parity[2]={"even","odd"};
44 FILE *fp=safe_fopen("odd_even_pl.pov","w");
45 for(i=0;i<f_vert.size();i++)
46 fprintf(fp,"plane{<%g,%g,%g>,0.5 texture{t_%s}}\n"
47 ,nor[3*i],nor[3*i+1],nor[3*i+2]
48 ,parity[f_vert[i]&1]);
49 fclose(fp);
51 // Save the Voronoi cell as a spheres and cylinders
52 v.draw_pov(0,0,0,"odd_even_v.pov");