1 // Voro++, a cell-based Voronoi library
3 // Authors : Chris H. Rycroft (LBL / UC Berkeley)
4 // Cody Robert Dance (UC Berkeley)
5 // Email : chr@alum.mit.edu
6 // Date : August 30th 2011
8 #include "voro++_2d.hh"
15 const double pad
=0.01;
17 int main(int argc
,char **argv
) {
20 fprintf(stderr
,"Syntax: voro++_bdry <input_file>\n");
24 // Read file to determine the size of the system and the number
28 double x
,y
,minx
=large_number
,maxx
=-minx
,miny
=minx
,maxy
=maxx
;
33 bool neg_label
=false,boundary_track
=false,start
=false;
34 char *buf(new char[512]);
36 FILE *fp
=safe_fopen(argv
[1],"r");
38 while(fgets(buf
,512,fp
)!=NULL
) {
40 if(strcmp(buf
,"#Start\n")==0||strcmp(buf
,"# Start\n")==0) {
42 // Check that two consecutive start tokens haven't been
44 if(boundary_track
) voro_fatal_error("File import error - two consecutive start tokens found",VOROPP_FILE_ERROR
);
45 start
=true;boundary_track
=true;
47 } else if(strcmp(buf
,"#End\n")==0||strcmp(buf
,"# End\n")==0||
48 strcmp(buf
,"#End")==0||strcmp(buf
,"# End")==0) {
50 // Check that two consecutive end tokens haven't been
52 if(start
) voro_fatal_error("File import error - end token immediately after start token",VOROPP_FILE_ERROR
);
53 if(!boundary_track
) voro_fatal_error("File import error - found end token without start token",VOROPP_FILE_ERROR
);
54 vbd
[i
-1]|=2;boundary_track
=false;
57 // Try and read three entries from the line
58 if(sscanf(buf
,"%d %lg %lg",&id
,&x
,&y
)!=3) voro_fatal_error("File import error #1",VOROPP_FILE_ERROR
);
62 vbd
.push_back(start
?1:0);
66 if(id
<0) neg_label
=true;
77 if(boundary_track
) voro_fatal_error("File import error - boundary not finished",VOROPP_FILE_ERROR
);
78 if(!feof(fp
)) voro_fatal_error("File import error #2",VOROPP_FILE_ERROR
);
81 // Add small amount of padding to container bounds
82 double dx
=maxx
-minx
,dy
=maxy
-miny
;
83 minx
-=pad
*dx
;maxx
+=pad
*dx
;dx
+=2*pad
*dx
;
84 miny
-=pad
*dy
;maxy
+=pad
*dy
;dy
+=2*pad
*dy
;
86 // Guess the optimal computationl grid, aiming at eight particles per
88 double lscale
=sqrt(8.0*dx
*dy
/i
);
89 int nx
=int(dx
/lscale
)+1,ny
=int(dy
/lscale
)+1;
91 // Print diagnostic information
92 printf("Container bounds : [%g:%g] [%g:%g]\n"
93 "Total particles : %d\n"
94 "Compute grid : %d by %d\n",minx
,maxx
,miny
,maxy
,i
,nx
,ny
);
97 container_boundary_2d
con(minx
,maxx
,miny
,maxy
,nx
,ny
,false,false,16);
100 for(j
=0;j
<vid
.size();j
++) {
101 if(vbd
[j
]&1) con
.start_boundary();
102 con
.put(vid
[j
],vpos
[2*j
],vpos
[2*j
+1]);
103 if(vbd
[j
]&2) con
.end_boundary();
106 // Carry out all of the setup prior to computing any Voronoi cells
109 // Save the boundary in a format that can be read by Gnuplot
110 char *outfn(new char[strlen(argv
[1])+5]);
111 sprintf(outfn
,"%s.bd",argv
[1]);
113 con
.draw_boundary_gnuplot(outfn
);
115 //Test Global Connectivity Info
116 // cout << "beginning" << endl;
117 // char *connectfn(new char[strlen(argv[1])+5]);
118 // sprintf(connectfn,"%s.connect",argv[1]);
119 // con.print_custom(argv[2],connectfn );
120 // cout << "finished" << endl;
122 // Compute the Voronoi cells and save them to file
123 sprintf(outfn
,"%s.gnu",argv
[1]);
124 con
.draw_cells_gnuplot(outfn
);
126 // Output the neighbor mesh in gnuplot format
127 if(mid
>16777216) puts("Network output disabled due to too high Ids");
128 else if(neg_label
) puts("Network output disabled due to negative IDs");
131 int *mp
=new int[mid
+1];
132 for(j
=0;j
<i
;j
++) mp
[vid
[j
]]=j
;
134 sprintf(outfn
,"%s.net",argv
[1]);
135 FILE *ff
=safe_fopen(outfn
,"w");
138 voronoicell_nonconvex_neighbor_2d c
;
139 c_loop_all_2d
cl(con
);
140 if(cl
.start()) do if(con
.compute_cell(c
,cl
)) {
141 id
=cl
.pid();l1
=2*mp
[id
];
143 for(j
=0;j
<vi
.size();j
++) if(vi
[j
]>id
) {
145 fprintf(ff
,"%g %g\n%g %g\n\n\n",
146 vpos
[l1
],vpos
[l1
+1],vpos
[l2
],vpos
[l2
+1]);
153 // delete [] connectfn;