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