1 // Single Voronoi cell example code
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : July 1st 2008
9 const double pi
=3.1415926535897932384626433832795;
11 // This constant sets the tolerance in the bisection search algorithm
12 const double tolwidth
=1e-7;
14 // This constant determines the density of points to test
15 const double theta_step
=pi
/200;
18 double x
,y
,z
,r
,rmin
,rmax
;
19 double theta
,phi
,phi_step
;
22 os
.open("cell_cut_region.gnu",ofstream::out
|ofstream::trunc
);
24 // Initialize the Voronoi cell to be an octahedron and make a single
25 // plane cut to add some variation
27 v
.plane(0.4,0.3,1,0.1);
29 // Output the cell in gnuplot format
30 v
.draw_gnuplot("cell.gnu",0,0,0);
32 // Now test over direction vectors from the center of the sphere. For
33 // each vector, carry out a search to find the maximum distance along
34 // that vector that a plane will intersect with cell, and save it to
36 for(theta
=theta_step
*0.5;theta
<pi
;theta
+=theta_step
) {
37 phi_step
=2*pi
/(int(2*pi
*sin(theta
)/theta_step
)+1);
38 for(phi
=phi_step
*0.5;phi
<2*pi
;phi
+=phi_step
) {
40 // Calculate a direction to look along
41 x
=sin(theta
)*cos(phi
);
42 y
=sin(theta
)*sin(phi
);
45 // Now carry out a bisection search. Here, we initialize
46 // a minimum and a maximum guess for the distance
47 // along this vector. Keep multiplying rmax by two until
48 // the plane no longer makes a cut.
50 while (v
.plane_intersects(x
,y
,z
,rmax
)) rmax
*=2;
52 // We now know that the distance is somewhere between
53 // rmin and rmax. Test the point halfway in-between
54 // these two. If it intersects, then move rmin to this
55 // point; otherwise, move rmax there. At each stage the
56 // bounding interval is divided by two. Exit when the
57 // width of the interval is smaller than the tolerance.
58 while (rmax
-rmin
>tolwidth
) {
60 if (v
.plane_intersects(x
,y
,z
,r
)) rmin
=r
;
64 // Output this point to file
67 os
<< x
<< " " << y
<< " " << z
<< endl
;