Bugfix in search_for_outside_edge routine.
[voro++.git] / trunk / src / cell.cc
blob484cbdd53454c976275ff8224b4855ac9abef241
1 // Voro++, a 3D cell-based Voronoi library
2 //
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : August 30th 2011
7 /** \file cell.cc
8 * \brief Function implementations for the voronoicell and related classes. */
10 #include <cmath>
11 #include <cstring>
13 #include "config.hh"
14 #include "common.hh"
15 #include "cell.hh"
17 namespace voro {
19 /** Constructs a Voronoi cell and sets up the initial memory. */
20 voronoicell_base::voronoicell_base(double max_len_sq) :
21 current_vertices(init_vertices), current_vertex_order(init_vertex_order),
22 current_delete_size(init_delete_size), current_delete2_size(init_delete2_size),
23 current_xsearch_size(init_xsearch_size),
24 ed(new int*[current_vertices]), nu(new int[current_vertices]),
25 mask(new unsigned int[current_vertices]),
26 pts(new double[current_vertices<<2]), tol(tolerance*max_len_sq),
27 tol_cu(tol*sqrt(tol)), big_tol(big_tolerance_fac*tol), mem(new int[current_vertex_order]),
28 mec(new int[current_vertex_order]),
29 mep(new int*[current_vertex_order]), ds(new int[current_delete_size]),
30 stacke(ds+current_delete_size), ds2(new int[current_delete2_size]),
31 stacke2(ds2+current_delete2_size), xse(new int[current_xsearch_size]),
32 stacke3(xse+current_xsearch_size), maskc(0) {
33 int i;
34 for(i=0;i<current_vertices;i++) mask[i]=0;
35 for(i=0;i<3;i++) {
36 mem[i]=init_n_vertices;mec[i]=0;
37 mep[i]=new int[init_n_vertices*((i<<1)+1)];
39 mem[3]=init_3_vertices;mec[3]=0;
40 mep[3]=new int[init_3_vertices*7];
41 for(i=4;i<current_vertex_order;i++) {
42 mem[i]=init_n_vertices;mec[i]=0;
43 mep[i]=new int[init_n_vertices*((i<<1)+1)];
47 /** The voronoicell destructor deallocates all the dynamic memory. */
48 voronoicell_base::~voronoicell_base() {
49 for(int i=current_vertex_order-1;i>=0;i--) if(mem[i]>0) delete [] mep[i];
50 delete [] xse;
51 delete [] ds2;delete [] ds;
52 delete [] mep;delete [] mec;
53 delete [] mem;delete [] pts;
54 delete [] mask;
55 delete [] nu;delete [] ed;
58 /** Ensures that enough memory is allocated prior to carrying out a copy.
59 * \param[in] vc a reference to the specialized version of the calling class.
60 * \param[in] vb a pointered to the class to be copied. */
61 template<class vc_class>
62 void voronoicell_base::check_memory_for_copy(vc_class &vc,voronoicell_base* vb) {
63 while(current_vertex_order<vb->current_vertex_order) add_memory_vorder(vc);
64 for(int i=0;i<current_vertex_order;i++) while(mem[i]<vb->mec[i]) add_memory(vc,i);
65 while(current_vertices<vb->p) add_memory_vertices(vc);
68 /** Copies the vertex and edge information from another class. The routine
69 * assumes that enough memory is available for the copy.
70 * \param[in] vb a pointer to the class to copy. */
71 void voronoicell_base::copy(voronoicell_base* vb) {
72 int i,j;
73 p=vb->p;up=0;
74 for(i=0;i<current_vertex_order;i++) {
75 mec[i]=vb->mec[i];
76 for(j=0;j<mec[i]*(2*i+1);j++) mep[i][j]=vb->mep[i][j];
77 for(j=0;j<mec[i]*(2*i+1);j+=2*i+1) ed[mep[i][j+2*i]]=mep[i]+j;
79 for(i=0;i<p;i++) nu[i]=vb->nu[i];
80 for(i=0;i<(p<<2);i++) pts[i]=vb->pts[i];
83 /** Copies the information from another voronoicell class into this
84 * class, extending memory allocation if necessary.
85 * \param[in] c the class to copy. */
86 void voronoicell_neighbor::operator=(voronoicell &c) {
87 voronoicell_base *vb=((voronoicell_base*) &c);
88 check_memory_for_copy(*this,vb);copy(vb);
89 int i,j;
90 for(i=0;i<c.current_vertex_order;i++) {
91 for(j=0;j<c.mec[i]*i;j++) mne[i][j]=0;
92 for(j=0;j<c.mec[i];j++) ne[c.mep[i][(2*i+1)*j+2*i]]=mne[i]+(j*i);
96 /** Copies the information from another voronoicell_neighbor class into this
97 * class, extending memory allocation if necessary.
98 * \param[in] c the class to copy. */
99 void voronoicell_neighbor::operator=(voronoicell_neighbor &c) {
100 voronoicell_base *vb=((voronoicell_base*) &c);
101 check_memory_for_copy(*this,vb);copy(vb);
102 int i,j;
103 for(i=0;i<c.current_vertex_order;i++) {
104 for(j=0;j<c.mec[i]*i;j++) mne[i][j]=c.mne[i][j];
105 for(j=0;j<c.mec[i];j++) ne[c.mep[i][(2*i+1)*j+2*i]]=mne[i]+(j*i);
109 /** Translates the vertices of the Voronoi cell by a given vector.
110 * \param[in] (x,y,z) the coordinates of the vector. */
111 void voronoicell_base::translate(double x,double y,double z) {
112 x*=2;y*=2;z*=2;
113 double *ptsp=pts;
114 while(ptsp<pts+(p<<2)) {
115 *(ptsp++)+=x;*(ptsp++)+=y;*ptsp+=z;ptsp+=2;
119 /** Increases the memory storage for a particular vertex order, by increasing
120 * the size of the of the corresponding mep array. If the arrays already exist,
121 * their size is doubled; if they don't exist, then new ones of size
122 * init_n_vertices are allocated. The routine also ensures that the pointers in
123 * the ed array are updated, by making use of the back pointers. For the cases
124 * where the back pointer has been temporarily overwritten in the marginal
125 * vertex code, the auxiliary delete stack is scanned to find out how to update
126 * the ed value. If the template has been instantiated with the neighbor
127 * tracking turned on, then the routine also reallocates the corresponding mne
128 * array.
129 * \param[in] i the order of the vertex memory to be increased. */
130 template<class vc_class>
131 void voronoicell_base::add_memory(vc_class &vc,int i) {
132 int s=(i<<1)+1;
133 if(mem[i]==0) {
134 vc.n_allocate(i,init_n_vertices);
135 mep[i]=new int[init_n_vertices*s];
136 mem[i]=init_n_vertices;
137 #if VOROPP_VERBOSE >=2
138 fprintf(stderr,"Order %d vertex memory created\n",i);
139 #endif
140 } else {
141 int j=0,k,*l;
142 mem[i]<<=1;
143 if(mem[i]>max_n_vertices) voro_fatal_error("Point memory allocation exceeded absolute maximum",VOROPP_MEMORY_ERROR);
144 #if VOROPP_VERBOSE >=2
145 fprintf(stderr,"Order %d vertex memory scaled up to %d\n",i,mem[i]);
146 #endif
147 l=new int[s*mem[i]];
148 int m=0;
149 vc.n_allocate_aux1(i);
150 while(j<s*mec[i]) {
151 k=mep[i][j+(i<<1)];
152 if(k>=0) {
153 ed[k]=l+j;
154 vc.n_set_to_aux1_offset(k,m);
155 } else {
156 int *dsp;
157 for(dsp=ds2;dsp<stackp2;dsp++) {
158 if(ed[*dsp]==mep[i]+j) {
159 ed[*dsp]=l+j;
160 vc.n_set_to_aux1_offset(*dsp,m);
161 break;
164 if(dsp==stackp2) {
165 for(dsp=xse;dsp<stackp3;dsp++) {
166 if(ed[*dsp]==mep[i]+j) {
167 ed[*dsp]=l+j;
168 vc.n_set_to_aux1_offset(*dsp,m);
169 break;
172 if(dsp==stackp3) voro_fatal_error("Couldn't relocate dangling pointer",VOROPP_INTERNAL_ERROR);
174 #if VOROPP_VERBOSE >=3
175 fputs("Relocated dangling pointer",stderr);
176 #endif
178 for(k=0;k<s;k++,j++) l[j]=mep[i][j];
179 for(k=0;k<i;k++,m++) vc.n_copy_to_aux1(i,m);
181 delete [] mep[i];
182 mep[i]=l;
183 vc.n_switch_to_aux1(i);
187 /** Doubles the maximum number of vertices allowed, by reallocating the ed, nu,
188 * and pts arrays. If the allocation exceeds the absolute maximum set in
189 * max_vertices, then the routine exits with a fatal error. If the template has
190 * been instantiated with the neighbor tracking turned on, then the routine
191 * also reallocates the ne array. */
192 template<class vc_class>
193 void voronoicell_base::add_memory_vertices(vc_class &vc) {
194 int i=(current_vertices<<1),j,**pp,*pnu;
195 unsigned int* pmask;
196 if(i>max_vertices) voro_fatal_error("Vertex memory allocation exceeded absolute maximum",VOROPP_MEMORY_ERROR);
197 #if VOROPP_VERBOSE >=2
198 fprintf(stderr,"Vertex memory scaled up to %d\n",i);
199 #endif
200 double *ppts;
201 pp=new int*[i];
202 for(j=0;j<current_vertices;j++) pp[j]=ed[j];
203 delete [] ed;ed=pp;
204 vc.n_add_memory_vertices(i);
205 pnu=new int[i];
206 for(j=0;j<current_vertices;j++) pnu[j]=nu[j];
207 delete [] nu;nu=pnu;
208 pmask=new unsigned int[i];
209 for(j=0;j<current_vertices;j++) pmask[j]=mask[j];
210 while(j<i) pmask[j++]=0;
211 delete [] mask;mask=pmask;
212 ppts=new double[i<<2];
213 for(j=0;j<(current_vertices<<2);j++) ppts[j]=pts[j];
214 delete [] pts;pts=ppts;
215 current_vertices=i;
218 /** Doubles the maximum allowed vertex order, by reallocating mem, mep, and mec
219 * arrays. If the allocation exceeds the absolute maximum set in
220 * max_vertex_order, then the routine causes a fatal error. If the template has
221 * been instantiated with the neighbor tracking turned on, then the routine
222 * also reallocates the mne array. */
223 template<class vc_class>
224 void voronoicell_base::add_memory_vorder(vc_class &vc) {
225 int i=(current_vertex_order<<1),j,*p1,**p2;
226 if(i>max_vertex_order) voro_fatal_error("Vertex order memory allocation exceeded absolute maximum",VOROPP_MEMORY_ERROR);
227 #if VOROPP_VERBOSE >=2
228 fprintf(stderr,"Vertex order memory scaled up to %d\n",i);
229 #endif
230 p1=new int[i];
231 for(j=0;j<current_vertex_order;j++) p1[j]=mem[j];while(j<i) p1[j++]=0;
232 delete [] mem;mem=p1;
233 p2=new int*[i];
234 for(j=0;j<current_vertex_order;j++) p2[j]=mep[j];
235 delete [] mep;mep=p2;
236 p1=new int[i];
237 for(j=0;j<current_vertex_order;j++) p1[j]=mec[j];while(j<i) p1[j++]=0;
238 delete [] mec;mec=p1;
239 vc.n_add_memory_vorder(i);
240 current_vertex_order=i;
243 /** Doubles the size allocation of the main delete stack. If the allocation
244 * exceeds the absolute maximum set in max_delete_size, then routine causes a
245 * fatal error. */
246 void voronoicell_base::add_memory_ds() {
247 current_delete_size<<=1;
248 if(current_delete_size>max_delete_size) voro_fatal_error("Delete stack 1 memory allocation exceeded absolute maximum",VOROPP_MEMORY_ERROR);
249 #if VOROPP_VERBOSE >=2
250 fprintf(stderr,"Delete stack 1 memory scaled up to %d\n",current_delete_size);
251 #endif
252 int *dsn=new int[current_delete_size],*dsnp=dsn,*dsp=ds;
253 while(dsp<stackp) *(dsnp++)=*(dsp++);
254 delete [] ds;ds=dsn;stackp=dsnp;
255 stacke=ds+current_delete_size;
258 /** Doubles the size allocation of the auxiliary delete stack. If the
259 * allocation exceeds the absolute maximum set in max_delete2_size, then the
260 * routine causes a fatal error. */
261 void voronoicell_base::add_memory_ds2() {
262 current_delete2_size<<=1;
263 if(current_delete2_size>max_delete2_size) voro_fatal_error("Delete stack 2 memory allocation exceeded absolute maximum",VOROPP_MEMORY_ERROR);
264 #if VOROPP_VERBOSE >=2
265 fprintf(stderr,"Delete stack 2 memory scaled up to %d\n",current_delete2_size);
266 #endif
267 int *dsn=new int[current_delete2_size],*dsnp=dsn,*dsp=ds2;
268 while(dsp<stackp2) *(dsnp++)=*(dsp++);
269 delete [] ds2;ds2=dsn;stackp2=dsnp;
270 stacke2=ds2+current_delete2_size;
273 /** Doubles the size allocation of the auxiliary delete stack. If the
274 * allocation exceeds the absolute maximum set in max_delete2_size, then the
275 * routine causes a fatal error. */
276 void voronoicell_base::add_memory_xse() {
277 current_xsearch_size<<=1;
278 if(current_xsearch_size>max_xsearch_size) voro_fatal_error("Extra search stack memory allocation exceeded absolute maximum",VOROPP_MEMORY_ERROR);
279 #if VOROPP_VERBOSE >=2
280 fprintf(stderr,"Extra search stack memory scaled up to %d\n",current_xsearch_size);
281 #endif
282 int *dsn=new int[current_xsearch_size],*dsnp=dsn,*dsp=xse;
283 while(dsp<stackp3) *(dsnp++)=*(dsp++);
284 delete [] xse;xse=dsn;stackp3=dsnp;
285 stacke3=xse+current_xsearch_size;
288 /** Initializes a Voronoi cell as a rectangular box with the given dimensions.
289 * \param[in] (xmin,xmax) the minimum and maximum x coordinates.
290 * \param[in] (ymin,ymax) the minimum and maximum y coordinates.
291 * \param[in] (zmin,zmax) the minimum and maximum z coordinates. */
292 void voronoicell_base::init_base(double xmin,double xmax,double ymin,double ymax,double zmin,double zmax) {
293 for(int i=0;i<current_vertex_order;i++) mec[i]=0;up=0;
294 mec[3]=p=8;xmin*=2;xmax*=2;ymin*=2;ymax*=2;zmin*=2;zmax*=2;
295 *pts=xmin;pts[1]=ymin;pts[2]=zmin;
296 pts[4]=xmax;pts[5]=ymin;pts[6]=zmin;
297 pts[8]=xmin;pts[9]=ymax;pts[10]=zmin;
298 pts[12]=xmax;pts[13]=ymax;pts[14]=zmin;
299 pts[16]=xmin;pts[17]=ymin;pts[18]=zmax;
300 pts[20]=xmax;pts[21]=ymin;pts[22]=zmax;
301 pts[24]=xmin;pts[25]=ymax;pts[26]=zmax;
302 pts[28]=xmax;pts[29]=ymax;pts[30]=zmax;
303 int *q=mep[3];
304 *q=1;q[1]=4;q[2]=2;q[3]=2;q[4]=1;q[5]=0;q[6]=0;
305 q[7]=3;q[8]=5;q[9]=0;q[10]=2;q[11]=1;q[12]=0;q[13]=1;
306 q[14]=0;q[15]=6;q[16]=3;q[17]=2;q[18]=1;q[19]=0;q[20]=2;
307 q[21]=2;q[22]=7;q[23]=1;q[24]=2;q[25]=1;q[26]=0;q[27]=3;
308 q[28]=6;q[29]=0;q[30]=5;q[31]=2;q[32]=1;q[33]=0;q[34]=4;
309 q[35]=4;q[36]=1;q[37]=7;q[38]=2;q[39]=1;q[40]=0;q[41]=5;
310 q[42]=7;q[43]=2;q[44]=4;q[45]=2;q[46]=1;q[47]=0;q[48]=6;
311 q[49]=5;q[50]=3;q[51]=6;q[52]=2;q[53]=1;q[54]=0;q[55]=7;
312 *ed=q;ed[1]=q+7;ed[2]=q+14;ed[3]=q+21;
313 ed[4]=q+28;ed[5]=q+35;ed[6]=q+42;ed[7]=q+49;
314 *nu=nu[1]=nu[2]=nu[3]=nu[4]=nu[5]=nu[6]=nu[7]=3;
317 /** Initializes an L-shaped Voronoi cell of a fixed size for testing the
318 * convexity robustness. */
319 void voronoicell::init_l_shape() {
320 for(int i=0;i<current_vertex_order;i++) mec[i]=0;up=0;
321 mec[3]=p=12;
322 const double j=0;
323 *pts=-2;pts[1]=-2;pts[2]=-2;
324 pts[4]=2;pts[5]=-2;pts[6]=-2;
325 pts[8]=-2;pts[9]=0;pts[10]=-2;
326 pts[12]=-j;pts[13]=j;pts[14]=-2;
327 pts[16]=0;pts[17]=2;pts[18]=-2;
328 pts[20]=2;pts[21]=2;pts[22]=-2;
329 pts[24]=-2;pts[25]=-2;pts[26]=2;
330 pts[28]=2;pts[29]=-2;pts[30]=2;
331 pts[32]=-2;pts[33]=0;pts[34]=2;
332 pts[36]=-j;pts[37]=j;pts[38]=2;
333 pts[40]=0;pts[41]=2;pts[42]=2;
334 pts[44]=2;pts[45]=2;pts[46]=2;
335 int *q=mep[3];
336 *q=1;q[1]=6;q[2]=2;q[6]=0;
337 q[7]=5;q[8]=7;q[9]=0;q[13]=1;
338 q[14]=0;q[15]=8;q[16]=3;q[20]=2;
339 q[21]=2;q[22]=9;q[23]=4;q[27]=3;
340 q[28]=3;q[29]=10;q[30]=5;q[34]=4;
341 q[35]=4;q[36]=11;q[37]=1;q[41]=5;
342 q[42]=8;q[43]=0;q[44]=7;q[48]=6;
343 q[49]=6;q[50]=1;q[51]=11;q[55]=7;
344 q[56]=9;q[57]=2;q[58]=6;q[62]=8;
345 q[63]=10;q[64]=3;q[65]=8;q[69]=9;
346 q[70]=11;q[71]=4;q[72]=9;q[76]=10;
347 q[77]=7;q[78]=5;q[79]=10;q[83]=11;
348 *ed=q;ed[1]=q+7;ed[2]=q+14;ed[3]=q+21;ed[4]=q+28;ed[5]=q+35;
349 ed[6]=q+42;ed[7]=q+49;ed[8]=q+56;ed[9]=q+63;ed[10]=q+70;ed[11]=q+77;
350 for(int i=0;i<12;i++) nu[i]=3;
351 construct_relations();
354 /** Initializes a Voronoi cell as a regular octahedron.
355 * \param[in] l The distance from the octahedron center to a vertex. Six
356 * vertices are initialized at (-l,0,0), (l,0,0), (0,-l,0),
357 * (0,l,0), (0,0,-l), and (0,0,l). */
358 void voronoicell_base::init_octahedron_base(double l) {
359 for(int i=0;i<current_vertex_order;i++) mec[i]=0;up=0;
360 mec[4]=p=6;l*=2;
361 *pts=-l;pts[1]=0;pts[2]=0;
362 pts[4]=l;pts[5]=0;pts[6]=0;
363 pts[8]=0;pts[9]=-l;pts[10]=0;
364 pts[12]=0;pts[13]=l;pts[14]=0;
365 pts[16]=0;pts[17]=0;pts[18]=-l;
366 pts[20]=0;pts[21]=0;pts[22]=l;
367 int *q=mep[4];
368 *q=2;q[1]=5;q[2]=3;q[3]=4;q[4]=0;q[5]=0;q[6]=0;q[7]=0;q[8]=0;
369 q[9]=2;q[10]=4;q[11]=3;q[12]=5;q[13]=2;q[14]=2;q[15]=2;q[16]=2;q[17]=1;
370 q[18]=0;q[19]=4;q[20]=1;q[21]=5;q[22]=0;q[23]=3;q[24]=0;q[25]=1;q[26]=2;
371 q[27]=0;q[28]=5;q[29]=1;q[30]=4;q[31]=2;q[32]=3;q[33]=2;q[34]=1;q[35]=3;
372 q[36]=0;q[37]=3;q[38]=1;q[39]=2;q[40]=3;q[41]=3;q[42]=1;q[43]=1;q[44]=4;
373 q[45]=0;q[46]=2;q[47]=1;q[48]=3;q[49]=1;q[50]=3;q[51]=3;q[52]=1;q[53]=5;
374 *ed=q;ed[1]=q+9;ed[2]=q+18;ed[3]=q+27;ed[4]=q+36;ed[5]=q+45;
375 *nu=nu[1]=nu[2]=nu[3]=nu[4]=nu[5]=4;
378 /** Initializes a Voronoi cell as a tetrahedron. It assumes that the normal to
379 * the face for the first three vertices points inside.
380 * \param (x0,y0,z0) a position vector for the first vertex.
381 * \param (x1,y1,z1) a position vector for the second vertex.
382 * \param (x2,y2,z2) a position vector for the third vertex.
383 * \param (x3,y3,z3) a position vector for the fourth vertex. */
384 void voronoicell_base::init_tetrahedron_base(double x0,double y0,double z0,double x1,double y1,double z1,double x2,double y2,double z2,double x3,double y3,double z3) {
385 for(int i=0;i<current_vertex_order;i++) mec[i]=0;up=0;
386 mec[3]=p=4;
387 *pts=x0*2;pts[1]=y0*2;pts[2]=z0*2;
388 pts[4]=x1*2;pts[5]=y1*2;pts[6]=z1*2;
389 pts[8]=x2*2;pts[9]=y2*2;pts[10]=z2*2;
390 pts[12]=x3*2;pts[13]=y3*2;pts[14]=z3*2;
391 int *q=mep[3];
392 *q=1;q[1]=3;q[2]=2;q[3]=0;q[4]=0;q[5]=0;q[6]=0;
393 q[7]=0;q[8]=2;q[9]=3;q[10]=0;q[11]=2;q[12]=1;q[13]=1;
394 q[14]=0;q[15]=3;q[16]=1;q[17]=2;q[18]=2;q[19]=1;q[20]=2;
395 q[21]=0;q[22]=1;q[23]=2;q[24]=1;q[25]=2;q[26]=1;q[27]=3;
396 *ed=q;ed[1]=q+7;ed[2]=q+14;ed[3]=q+21;
397 *nu=nu[1]=nu[2]=nu[3]=3;
400 /** Checks that the relational table of the Voronoi cell is accurate, and
401 * prints out any errors. This algorithm is O(p), so running it every time the
402 * plane routine is called will result in a significant slowdown. */
403 void voronoicell_base::check_relations() {
404 int i,j;
405 for(i=0;i<p;i++) for(j=0;j<nu[i];j++) if(ed[ed[i][j]][ed[i][nu[i]+j]]!=i)
406 printf("Relational error at point %d, edge %d.\n",i,j);
409 /** This routine checks for any two vertices that are connected by more than
410 * one edge. The plane algorithm is designed so that this should not happen, so
411 * any occurrences are most likely errors. Note that the routine is O(p), so
412 * running it every time the plane routine is called will result in a
413 * significant slowdown. */
414 void voronoicell_base::check_duplicates() {
415 int i,j,k;
416 for(i=0;i<p;i++) for(j=1;j<nu[i];j++) for(k=0;k<j;k++) if(ed[i][j]==ed[i][k])
417 printf("Duplicate edges: (%d,%d) and (%d,%d) [%d]\n",i,j,i,k,ed[i][j]);
420 /** Constructs the relational table if the edges have been specified. */
421 void voronoicell_base::construct_relations() {
422 int i,j,k,l;
423 for(i=0;i<p;i++) for(j=0;j<nu[i];j++) {
424 k=ed[i][j];
425 l=0;
426 while(ed[k][l]!=i) {
427 l++;
428 if(l==nu[k]) voro_fatal_error("Relation table construction failed",VOROPP_INTERNAL_ERROR);
430 ed[i][nu[i]+j]=l;
434 /** Starting from a point within the current cutting plane, this routine attempts
435 * to find an edge to a point outside the cutting plane. This prevents the plane
436 * routine from .
437 * \param[in,out] up */
438 inline bool voronoicell_base::search_for_outside_edge(int &up) {
439 int i,lp,lw,*j=stackp2,sc2=stackp2-ds2;
440 double l;
441 *(stackp2++)=up;
442 while(j<stackp2) {
443 up=*(j++);
444 for(i=0;i<nu[up];i++) {
445 lp=ed[up][i];
446 lw=m_test(lp,l);
447 if(lw==0) {
448 stackp2=ds2+sc2;
449 return true;
451 else if(lw==1) add_to_stack(sc2,lp);
454 stackp2=ds2+sc2;
455 return false;
458 /** Adds a point to the auxiliary delete stack if it is not already there.
459 * \param[in] vc a reference to the specialized version of the calling class.
460 * \param[in] lp the index of the point to add.
461 * \param[in,out] stackp2 a pointer to the end of the stack entries. */
462 inline void voronoicell_base::add_to_stack(int sc2,int lp) {
463 for(int *k=ds2+sc2;k<stackp2;k++) if(*k==lp) return;
464 if(stackp2==stacke2) add_memory_ds2();
465 *(stackp2++)=lp;
468 /** Assuming that the point up is outside the cutting plane, this routine
469 * searches upwards along edges trying to find an edge that intersects the
470 * cutting plane.
471 * \param[in] rsq the distance along this vector of the plane.
472 * \param[in,out] u the dot product of point up with the normal.
473 * \return True if the cutting plane was reached, false otherwise. */
474 inline bool voronoicell_base::search_upward(unsigned int &uw,int &lp,int &ls,int &us,double &l,double &u) {
475 int vs;
476 lp=up;l=u;
478 // The test point is outside of the cutting space
479 for(ls=0;ls<nu[lp];ls++) {
480 up=ed[lp][ls];
481 uw=m_test(up,u);
482 if(u>l) break;
484 if(ls==nu[lp]) if(definite_max(lp,ls,l,u,uw)) {
485 up=lp;
486 return false;
489 while(uw==0) {
490 //if(++count>=p) failsafe_find(lp,ls,us,l,u);
492 // Test all the neighbors of the current point
493 // and find the one which is closest to the
494 // plane
495 vs=ed[lp][nu[lp]+ls];lp=up;l=u;
496 for(ls=0;ls<nu[lp];ls++) {
497 if(ls==vs) continue;
498 up=ed[lp][ls];
499 uw=m_test(up,u);
500 if(u>l) break;
502 if(ls==nu[lp]&&definite_max(lp,ls,l,u,uw)) {
503 up=lp;
504 return false;
507 us=ed[lp][nu[lp]+ls];
508 return true;
511 /** Checks whether a particular point lp is a definite maximum, searching
512 * through any possible minor non-convexities, for a better maximum.
513 * \param[in] (x,y,z) the normal vector to the plane. */
514 bool voronoicell_base::definite_max(int &lp,int &ls,double &l,double &u,unsigned int &uw) {
515 int tp=lp,ts,qp=0;
516 unsigned int qw;
517 double q;
519 // Check to see whether point up is a well-defined maximum. Otherwise
520 // any neighboring vertices of up that are marginal need to be
521 // followed, to see if they lead to a better maximum.
522 for(ts=0;ts<nu[tp];ts++) {
523 qp=ed[tp][ts];
524 m_test(qp,q);
525 if(q>l-big_tol) break;
527 if(ts==nu[tp]) return true;
529 // The point tp is marginal, so it will be necessary to do the
530 // flood-fill search. Mark the point tp and the point qp, and search
531 // any remaining neighbors of the point tp.
532 int *stackp=ds+1;
533 flip(lp);
534 flip(qp);
535 *ds=qp;
536 ts++;
537 while(ts<nu[tp]) {
538 qp=ed[tp][ts];
539 m_test(qp,q);
540 if(q>l-big_tol) {
541 if(stackp==stacke) add_memory_ds();
542 *(stackp++)=up;
543 flip(up);
545 ts++;
548 // Consider additional marginal points, starting with the original
549 // point qp
550 int *spp=ds;
551 while(spp<stackp) {
552 tp=*(spp++);
553 for(ts=0;ts<nu[tp];ts++) {
554 qp=ed[tp][ts];
556 // Skip the point if it's already marked
557 if(ed[qp][nu[qp]<<1]<0) continue;
558 qw=m_test(qp,q);
560 // This point is a better maximum. Reset markers and
561 // return true.
562 if(q>l) {
563 flip(lp);
564 lp=tp;
565 ls=ts;
566 m_test(lp,l);
567 up=qp;
568 uw=qw;
569 u=q;
570 while(stackp>ds) flip(*(--stackp));
571 return false;
574 // The point is marginal and therefore must also be
575 // considered
576 if(q>l-big_tol) {
577 if(stackp==stacke) {
578 int nn=stackp-spp;
579 add_memory_ds();
580 spp=stackp-nn;
582 *(stackp++)=qp;
583 flip(qp);
588 // Reset markers and return false
589 flip(lp);
590 while(stackp>ds) flip(*(--stackp));
591 return true;
594 inline bool voronoicell_base::search_downward(unsigned int &lw,int &lp,int &ls,int &us,double &l,double &u) {
595 int vs;
597 // The test point is outside of the cutting space
598 for(us=0;us<nu[up];us++) {
599 lp=ed[up][us];
600 lw=m_test(lp,l);
601 if(u>l) break;
603 if(us==nu[up]) if(definite_min(lp,us,l,u,lw)) return false;
605 while(lw==2) {
606 //if(++count>=p) failsafe_find(lp,ls,us,l,u);
608 // Test all the neighbors of the current point
609 // and find the one which is closest to the
610 // plane
611 vs=ed[up][nu[up]+us];up=lp;u=l;
612 for(us=0;us<nu[up];us++) {
613 if(us==vs) continue;
614 lp=ed[up][us];
615 lw=m_test(lp,l);
616 if(u>l) break;
618 if(us==nu[up]&&definite_min(lp,us,l,u,lw)) return false;
620 ls=ed[up][nu[up]+us];
621 return true;
624 bool voronoicell_base::definite_min(int &lp,int &us,double &l,double &u,unsigned int &lw) {
625 int tp=up,ts,qp=0;
626 unsigned int qw;
627 double q;
629 // Check to see whether point up is a well-defined maximum. Otherwise
630 // any neighboring vertices of up that are marginal need to be
631 // followed, to see if they lead to a better maximum.
632 for(ts=0;ts<nu[tp];ts++) {
633 qp=ed[tp][ts];
634 m_test(qp,q);
635 if(q<u+big_tol) break;
637 if(ts==nu[tp]) return true;
639 // The point tp is marginal, so it will be necessary to do the
640 // flood-fill search. Mark the point tp and the point qp, and search
641 // any remaining neighbors of the point tp.
642 int *stackp=ds+1;
643 flip(up);
644 flip(qp);
645 *ds=qp;
646 ts++;
647 while(ts<nu[tp]) {
648 qp=ed[tp][ts];
649 m_test(qp,q);
650 if(q<u+big_tol) {
651 if(stackp==stacke) add_memory_ds();
652 *(stackp++)=lp;
653 flip(lp);
655 ts++;
658 // Consider additional marginal points, starting with the original
659 // point qp
660 int *spp=ds;
661 while(spp<stackp) {
662 tp=*(spp++);
663 for(ts=0;ts<nu[tp];ts++) {
664 qp=ed[tp][ts];
666 // Skip the point if it's already marked
667 if(ed[qp][nu[qp]<<1]<0) continue;
668 qw=m_test(qp,q);
670 // This point is a better minimum. Reset markers and
671 // return true.
672 if(q<u) {
673 flip(up);
674 up=tp;
675 us=ts;
676 m_test(up,u);
677 lp=qp;
678 lw=qw;
679 l=q;
680 while(stackp>ds) flip(*(--stackp));
681 return false;
684 // The point is marginal and therefore must also be
685 // considered
686 if(q<u+big_tol) {
687 if(stackp==stacke) {
688 int nn=stackp-spp;
689 add_memory_ds();
690 spp=stackp-nn;
692 *(stackp++)=qp;
693 flip(qp);
698 // Reset markers and return false
699 flip(up);
700 while(stackp>ds) flip(*(--stackp));
701 return true;
704 /** Cuts the Voronoi cell by a particle whose center is at a separation of
705 * (x,y,z) from the cell center. The value of rsq should be initially set to
706 * \f$x^2+y^2+z^2\f$.
707 * \param[in] vc a reference to the specialized version of the calling class.
708 * \param[in] (x,y,z) the normal vector to the plane.
709 * \param[in] rsq the distance along this vector of the plane.
710 * \param[in] p_id the plane ID (for neighbor tracking only).
711 * \return False if the plane cut deleted the cell entirely, true otherwise. */
712 template<class vc_class>
713 bool voronoicell_base::nplane(vc_class &vc,double x,double y,double z,double rsq,int p_id) {
714 int i,j,lp=up,cp,qp,*dsp;
715 int us=0,ls=0;
716 unsigned int uw,lw;
717 int *edp,*edd;stackp=ds;
718 double u,l=0;up=0;
720 // Initialize the safe testing routine
721 px=x;py=y;pz=z;prsq=rsq;
722 maskc+=4;
723 if(maskc<4) reset_mask();
725 uw=m_test(up,u);
726 if(uw==2) {
727 if(!search_downward(lw,lp,ls,us,l,u)) return false;
728 if(lw==1) {up=lp;lp=-1;}
729 } else if(uw==0) {
730 if(!search_upward(uw,lp,ls,us,l,u)) return true;
731 if(uw==1) lp=-1;
732 } else {
733 lp=-1;
736 // Set stack pointers
737 stackp=ds;stackp2=ds2;stackp3=xse;
739 // Store initial number of vertices
740 int op=p;
742 if(create_facet(vc,lp,ls,l,us,u,p_id)) return false;
743 int k=0;int xtra=0;
744 while(xse+k<stackp3) {
745 lp=xse[k++];
746 uw=m_test(lp,l);
747 for(ls=0;ls<nu[lp];ls++) {
748 up=ed[lp][ls];
750 // Skip if this is a new vertex
751 uw=m_test(up,u);
752 if(up>=op) continue;
754 if(uw==0) {
755 if(u>-big_tol&&ed[up][nu[up]<<1]!=-1) {
756 ed[up][nu[up]<<1]=-1;
757 if(stackp3==stacke3) add_memory_xse();
758 *(stackp3++)=up;
760 } else if(uw==1) {
762 // This is a possible facet starting
763 // from a vertex on the cutting plane
764 if(create_facet(vc,-1,0,0,0,u,p_id)) return false;
765 } else {
767 // This is a new facet
768 us=ed[lp][nu[lp]+ls];
769 m_test(lp,l);
770 if(create_facet(vc,lp,ls,l,us,u,p_id)) return false;
773 xtra++;
776 // Reset back pointers on extra search stack
777 for(dsp=xse;dsp<stackp3;dsp++) {
778 j=*dsp;
779 ed[j][nu[j]<<1]=j;
782 // Delete points: first, remove any duplicates
783 dsp=ds;
784 while(dsp<stackp) {
785 j=*dsp;
786 if(ed[j][nu[j]]!=-1) {
787 ed[j][nu[j]]=-1;
788 dsp++;
789 } else *dsp=*(--stackp);
792 // Add the points in the auxiliary delete stack,
793 // and reset their back pointers
794 for(dsp=ds2;dsp<stackp2;dsp++) {
795 j=*dsp;
796 ed[j][nu[j]<<1]=j;
797 if(ed[j][nu[j]]!=-1) {
798 ed[j][nu[j]]=-1;
799 if(stackp==stacke) add_memory_ds();
800 *(stackp++)=j;
804 // Scan connections and add in extras
805 for(dsp=ds;dsp<stackp;dsp++) {
806 cp=*dsp;
807 for(edp=ed[cp];edp<ed[cp]+nu[cp];edp++) {
808 qp=*edp;
809 if(qp!=-1&&ed[qp][nu[qp]]!=-1) {
810 if(stackp==stacke) {
811 int dis=stackp-dsp;
812 add_memory_ds();
813 dsp=ds+dis;
815 *(stackp++)=qp;
816 ed[qp][nu[qp]]=-1;
820 up=0;
822 // Delete them from the array structure
823 while(stackp>ds) {
824 --p;
825 while(ed[p][nu[p]]==-1) {
826 j=nu[p];
827 edp=ed[p];edd=(mep[j]+((j<<1)+1)*--mec[j]);
828 while(edp<ed[p]+(j<<1)+1) *(edp++)=*(edd++);
829 vc.n_set_aux2_copy(p,j);
830 vc.n_copy_pointer(ed[p][(j<<1)],p);
831 ed[ed[p][(j<<1)]]=ed[p];
832 --p;
834 up=*(--stackp);
835 if(up<p) {
837 // Vertex management
838 pts[(up<<2)]=pts[(p<<2)];
839 pts[(up<<2)+1]=pts[(p<<2)+1];
840 pts[(up<<2)+2]=pts[(p<<2)+2];
842 // Memory management
843 j=nu[up];
844 edp=ed[up];edd=(mep[j]+((j<<1)+1)*--mec[j]);
845 while(edp<ed[up]+(j<<1)+1) *(edp++)=*(edd++);
846 vc.n_set_aux2_copy(up,j);
847 vc.n_copy_pointer(ed[up][j<<1],up);
848 vc.n_copy_pointer(up,p);
849 ed[ed[up][j<<1]]=ed[up];
851 // Edge management
852 ed[up]=ed[p];
853 nu[up]=nu[p];
854 for(i=0;i<nu[up];i++) ed[ed[up][i]][ed[up][nu[up]+i]]=up;
855 ed[up][nu[up]<<1]=up;
856 } else up=p++;
859 // Check for any vertices of zero order
860 if(*mec>0) voro_fatal_error("Zero order vertex formed",VOROPP_INTERNAL_ERROR);
862 // Collapse any order 2 vertices and exit
863 return collapse_order2(vc);
866 /** Creates a new facet.
867 * \return True if cell deleted, false otherwise. */
868 template<class vc_class>
869 bool voronoicell_base::create_facet(vc_class &vc,int lp,int ls,double l,int us,double u,int p_id) {
870 int i,j,k,qp,qs,iqs,cp,cs,rp,*edp,*edd;
871 unsigned int lw,qw;
872 bool new_double_edge=false,double_edge=false;
873 double q,r;
875 // We're about to add the first point of the new facet. In either
876 // routine, we have to add a point, so first check there's space for
877 // it.
878 if(p==current_vertices) add_memory_vertices(vc);
880 if(lp==-1) {
882 // We want to be strict about reaching the conclusion that the
883 // cell is entirely within the cutting plane. It's not enough
884 // to find a vertex that has edges which are all inside or on
885 // the plane. If the vertex has neighbors that are also on the
886 // plane, we should check those too.
887 if(!search_for_outside_edge(up)) return true;
889 // The search algorithm found a point which is on the cutting
890 // plane. We leave that point in place, and create a new one at
891 // the same location.
892 pts[(p<<2)]=pts[(up<<2)];
893 pts[(p<<2)+1]=pts[(up<<2)+1];
894 pts[(p<<2)+2]=pts[(up<<2)+2];
896 // Search for a collection of edges of the test vertex which
897 // are outside of the cutting space. Begin by testing the
898 // zeroth edge.
899 i=0;
900 lp=*ed[up];
901 lw=m_testx(lp,l);
902 if(lw!=0) {
904 // The first edge is either inside the cutting space,
905 // or lies within the cutting plane. Test the edges
906 // sequentially until we find one that is outside.
907 unsigned int rw=lw;
908 do {
909 i++;
911 // If we reached the last edge with no luck
912 // then all of the vertices are inside
913 // or on the plane, so the cell is completely
914 // deleted
915 if(i==nu[up]) return true;
916 lp=ed[up][i];
917 lw=m_testx(lp,l);
918 } while (lw!=0);
919 j=i+1;
921 // We found an edge outside the cutting space. Keep
922 // moving through these edges until we find one that's
923 // inside or on the plane.
924 while(j<nu[up]) {
925 lp=ed[up][j];
926 lw=m_testx(lp,l);
927 if(lw!=0) break;
928 j++;
931 // Compute the number of edges for the new vertex. In
932 // general it will be the number of outside edges
933 // found, plus two. But we need to recognize the
934 // special case when all but one edge is outside, and
935 // the remaining one is on the plane. For that case we
936 // have to reduce the edge count by one to prevent
937 // doubling up.
938 if(j==nu[up]&&i==1&&rw==1) {
939 nu[p]=nu[up];
940 double_edge=true;
941 } else nu[p]=j-i+2;
942 k=1;
944 // Add memory for the new vertex if needed, and
945 // initialize
946 while (nu[p]>=current_vertex_order) add_memory_vorder(vc);
947 if(mec[nu[p]]==mem[nu[p]]) add_memory(vc,nu[p]);
948 vc.n_set_pointer(p,nu[p]);
949 ed[p]=mep[nu[p]]+((nu[p]<<1)+1)*mec[nu[p]]++;
950 ed[p][nu[p]<<1]=p;
952 // Copy the edges of the original vertex into the new
953 // one. Delete the edges of the original vertex, and
954 // update the relational table.
955 us=cycle_down(i,up);
956 while(i<j) {
957 qp=ed[up][i];
958 qs=ed[up][nu[up]+i];
959 vc.n_copy(p,k,up,i);
960 ed[p][k]=qp;
961 ed[p][nu[p]+k]=qs;
962 ed[qp][qs]=p;
963 ed[qp][nu[qp]+qs]=k;
964 ed[up][i]=-1;
965 i++;k++;
967 qs=i==nu[up]?0:i;
968 } else {
970 // In this case, the zeroth edge is outside the cutting
971 // plane. Begin by searching backwards from the last
972 // edge until we find an edge which isn't outside.
973 i=nu[up]-1;
974 lp=ed[up][i];
975 lw=m_testx(lp,l);
976 while(lw==0) {
977 i--;
979 // If i reaches zero, then we have a point in
980 // the plane all of whose edges are outside
981 // the cutting space, so we just exit
982 if(i==0) return false;
983 lp=ed[up][i];
984 lw=m_testx(lp,l);
987 // Now search forwards from zero
988 j=1;
989 qp=ed[up][j];
990 qw=m_testx(qp,q);
991 while(qw==0) {
992 j++;
993 qp=ed[up][j];
994 qw=m_testx(qp,l);
997 // Compute the number of edges for the new vertex. In
998 // general it will be the number of outside edges
999 // found, plus two. But we need to recognize the
1000 // special case when all but one edge is outside, and
1001 // the remaining one is on the plane. For that case we
1002 // have to reduce the edge count by one to prevent
1003 // doubling up.
1004 if(i==j&&qw==1) {
1005 double_edge=true;
1006 nu[p]=nu[up];
1007 } else {
1008 nu[p]=nu[up]-i+j+1;
1011 // Add memory to store the vertex if it doesn't exist
1012 // already
1013 k=1;
1014 while(nu[p]>=current_vertex_order) add_memory_vorder(vc);
1015 if(mec[nu[p]]==mem[nu[p]]) add_memory(vc,nu[p]);
1017 // Copy the edges of the original vertex into the new
1018 // one. Delete the edges of the original vertex, and
1019 // update the relational table.
1020 vc.n_set_pointer(p,nu[p]);
1021 ed[p]=mep[nu[p]]+((nu[p]<<1)+1)*mec[nu[p]]++;
1022 ed[p][nu[p]<<1]=p;
1023 us=i++;
1024 while(i<nu[up]) {
1025 qp=ed[up][i];
1026 qs=ed[up][nu[up]+i];
1027 vc.n_copy(p,k,up,i);
1028 ed[p][k]=qp;
1029 ed[p][nu[p]+k]=qs;
1030 ed[qp][qs]=p;
1031 ed[qp][nu[qp]+qs]=k;
1032 ed[up][i]=-1;
1033 i++;k++;
1035 i=0;
1036 while(i<j) {
1037 qp=ed[up][i];
1038 qs=ed[up][nu[up]+i];
1039 vc.n_copy(p,k,up,i);
1040 ed[p][k]=qp;
1041 ed[p][nu[p]+k]=qs;
1042 ed[qp][qs]=p;
1043 ed[qp][nu[qp]+qs]=k;
1044 ed[up][i]=-1;
1045 i++;k++;
1047 qs=j;
1049 if(!double_edge) {
1050 vc.n_copy(p,k,up,qs);
1051 vc.n_set(p,0,p_id);
1052 } else vc.n_copy(p,0,up,qs);
1054 // Add this point to the auxiliary delete stack
1055 if(stackp2==stacke2) add_memory_ds2();
1056 *(stackp2++)=up;
1058 // Look at the edges on either side of the group that was
1059 // detected. We're going to commence facet computation by
1060 // moving along one of them. We are going to end up coming back
1061 // along the other one.
1062 cs=k;
1063 qp=up;q=u;
1064 i=ed[up][us];
1065 us=ed[up][nu[up]+us];
1066 up=i;
1067 ed[qp][nu[qp]<<1]=-p;
1069 } else {
1071 // The search algorithm found an intersected edge between the
1072 // points lp and up. Create a new vertex between them which
1073 // lies on the cutting plane. Since u and l differ by at least
1074 // the tolerance, this division should never screw up.
1075 if(stackp==stacke) add_memory_ds();
1076 *(stackp++)=up;
1077 r=u/(u-l);l=1-r;
1078 pts[p<<2]=pts[lp<<2]*r+pts[up<<2]*l;
1079 pts[(p<<2)+1]=pts[(lp<<2)+1]*r+pts[(up<<2)+1]*l;
1080 pts[(p<<2)+2]=pts[(lp<<2)+2]*r+pts[(up<<2)+2]*l;
1082 // This point will always have three edges. Connect one of them
1083 // to lp.
1084 nu[p]=3;
1085 if(mec[3]==mem[3]) add_memory(vc,3);
1086 vc.n_set_pointer(p,3);
1087 vc.n_set(p,0,p_id);
1088 vc.n_copy(p,1,up,us);
1089 vc.n_copy(p,2,lp,ls);
1090 ed[p]=mep[3]+7*mec[3]++;
1091 ed[p][6]=p;
1092 ed[up][us]=-1;
1093 ed[lp][ls]=p;
1094 ed[lp][nu[lp]+ls]=1;
1095 ed[p][1]=lp;
1096 ed[p][nu[p]+1]=ls;
1097 cs=2;
1099 // Set the direction to move in
1100 qs=cycle_up(us,up);
1101 qp=up;q=u;
1104 // When the code reaches here, we have initialized the first point, and
1105 // we have a direction for moving it to construct the rest of the facet
1106 cp=p;rp=p;p++;
1107 while(qp!=up||qs!=us) {
1109 // We're currently tracing round an intersected facet. Keep
1110 // moving around it until we find a point or edge which
1111 // intersects the plane.
1112 lp=ed[qp][qs];
1113 lw=m_testx(lp,l);
1115 if(lw==2) {
1117 // The point is still in the cutting space. Just add it
1118 // to the delete stack and keep moving.
1119 qs=cycle_up(ed[qp][nu[qp]+qs],lp);
1120 qp=lp;
1121 q=l;
1122 if(stackp==stacke) add_memory_ds();
1123 *(stackp++)=qp;
1125 } else if(lw==0) {
1127 // The point is outside of the cutting space, so we've
1128 // found an intersected edge. Introduce a regular point
1129 // at the point of intersection. Connect it to the
1130 // point we just tested. Also connect it to the previous
1131 // new point in the facet we're constructing.
1132 if(p==current_vertices) add_memory_vertices(vc);
1133 r=q/(q-l);l=1-r;
1134 pts[p<<2]=pts[lp<<2]*r+pts[qp<<2]*l;
1135 pts[(p<<2)+1]=pts[(lp<<2)+1]*r+pts[(qp<<2)+1]*l;
1136 pts[(p<<2)+2]=pts[(lp<<2)+2]*r+pts[(qp<<2)+2]*l;
1137 nu[p]=3;
1138 if(mec[3]==mem[3]) add_memory(vc,3);
1139 ls=ed[qp][qs+nu[qp]];
1140 vc.n_set_pointer(p,3);
1141 vc.n_set(p,0,p_id);
1142 vc.n_copy(p,1,qp,qs);
1143 vc.n_copy(p,2,lp,ls);
1144 ed[p]=mep[3]+7*mec[3]++;
1145 *ed[p]=cp;
1146 ed[p][1]=lp;
1147 ed[p][3]=cs;
1148 ed[p][4]=ls;
1149 ed[p][6]=p;
1150 ed[lp][ls]=p;
1151 ed[lp][nu[lp]+ls]=1;
1152 ed[cp][cs]=p;
1153 ed[cp][nu[cp]+cs]=0;
1154 ed[qp][qs]=-1;
1155 qs=cycle_up(qs,qp);
1156 cp=p++;
1157 cs=2;
1158 } else {
1160 // We've found a point which is on the cutting plane.
1161 // We're going to introduce a new point right here, but
1162 // first we need to figure out the number of edges it
1163 // has.
1164 if(p==current_vertices) add_memory_vertices(vc);
1166 // If the previous vertex detected a double edge, our
1167 // new vertex will have one less edge.
1168 k=double_edge?0:1;
1169 qs=ed[qp][nu[qp]+qs];
1170 qp=lp;
1171 iqs=qs;
1173 // Start testing the edges of the current point until
1174 // we find one which isn't outside the cutting space
1175 do {
1176 k++;
1177 qs=cycle_up(qs,qp);
1178 lp=ed[qp][qs];
1179 lw=m_testx(lp,l);
1180 } while (lw==0);
1182 // Now we need to find out whether this marginal vertex
1183 // we are on has been visited before, because if that's
1184 // the case, we need to add vertices to the existing
1185 // new vertex, rather than creating a fresh one. We also
1186 // need to figure out whether we're in a case where we
1187 // might be creating a duplicate edge.
1188 j=-ed[qp][nu[qp]<<1];
1189 if(qp==up&&qs==us) {
1191 // If we're heading into the final part of the
1192 // new facet, then we never worry about the
1193 // duplicate edge calculation.
1194 new_double_edge=false;
1195 if(j>0) k+=nu[j];
1196 } else {
1197 if(j>0) {
1199 // This vertex was visited before, so
1200 // count those vertices to the ones we
1201 // already have.
1202 k+=nu[j];
1204 // The only time when we might make a
1205 // duplicate edge is if the point we're
1206 // going to move to next is also a
1207 // marginal point, so test for that
1208 // first.
1209 if(lw==1) {
1211 // Now see whether this marginal point
1212 // has been visited before.
1213 i=-ed[lp][nu[lp]<<1];
1214 if(i>0) {
1216 // Now see if the last edge of that other
1217 // marginal point actually ends up here.
1218 if(ed[i][nu[i]-1]==j) {
1219 new_double_edge=true;
1220 k-=1;
1221 } else new_double_edge=false;
1222 } else {
1224 // That marginal point hasn't been visited
1225 // before, so we probably don't have to worry
1226 // about duplicate edges, except in the
1227 // case when that's the way into the end
1228 // of the facet, because that way always creates
1229 // an edge.
1230 if(j==rp&&lp==up&&ed[qp][nu[qp]+qs]==us) {
1231 new_double_edge=true;
1232 k-=1;
1233 } else new_double_edge=false;
1235 } else new_double_edge=false;
1236 } else {
1238 // The vertex hasn't been visited
1239 // before, but let's see if it's
1240 // marginal
1241 if(lw==1) {
1243 // If it is, we need to check
1244 // for the case that it's a
1245 // small branch, and that we're
1246 // heading right back to where
1247 // we came from
1248 i=-ed[lp][nu[lp]<<1];
1249 if(i==cp) {
1250 new_double_edge=true;
1251 k-=1;
1252 } else new_double_edge=false;
1253 } else new_double_edge=false;
1257 // k now holds the number of edges of the new vertex
1258 // we are forming. Add memory for it if it doesn't exist
1259 // already.
1260 while(k>=current_vertex_order) add_memory_vorder(vc);
1261 if(mec[k]==mem[k]) add_memory(vc,k);
1263 // Now create a new vertex with order k, or augment
1264 // the existing one
1265 if(j>0) {
1267 // If we're augmenting a vertex but we don't
1268 // actually need any more edges, just skip this
1269 // routine to avoid memory confusion
1270 if(nu[j]!=k) {
1272 // Allocate memory and copy the edges
1273 // of the previous instance into it
1274 vc.n_set_aux1(k);
1275 edp=mep[k]+((k<<1)+1)*mec[k]++;
1276 i=0;
1277 while(i<nu[j]) {
1278 vc.n_copy_aux1(j,i);
1279 edp[i]=ed[j][i];
1280 edp[k+i]=ed[j][nu[j]+i];
1281 i++;
1283 edp[k<<1]=j;
1285 // Remove the previous instance with
1286 // fewer vertices from the memory
1287 // structure
1288 edd=mep[nu[j]]+((nu[j]<<1)+1)*--mec[nu[j]];
1289 if(edd!=ed[j]) {
1290 for(int lll=0;lll<=(nu[j]<<1);lll++) ed[j][lll]=edd[lll];
1291 vc.n_set_aux2_copy(j,nu[j]);
1292 vc.n_copy_pointer(edd[nu[j]<<1],j);
1293 ed[edd[nu[j]<<1]]=ed[j];
1295 vc.n_set_to_aux1(j);
1296 ed[j]=edp;
1297 } else i=nu[j];
1298 } else {
1300 // Allocate a new vertex of order k
1301 vc.n_set_pointer(p,k);
1302 ed[p]=mep[k]+((k<<1)+1)*mec[k]++;
1303 ed[p][k<<1]=p;
1304 if(stackp2==stacke2) add_memory_ds2();
1305 *(stackp2++)=qp;
1306 pts[p<<2]=pts[qp<<2];
1307 pts[(p<<2)+1]=pts[(qp<<2)+1];
1308 pts[(p<<2)+2]=pts[(qp<<2)+2];
1309 ed[qp][nu[qp]<<1]=-p;
1310 j=p++;
1311 i=0;
1313 nu[j]=k;
1315 // Unless the previous case was a double edge, connect
1316 // the first available edge of the new vertex to the
1317 // last one in the facet
1318 if(!double_edge) {
1319 ed[j][i]=cp;
1320 ed[j][nu[j]+i]=cs;
1321 vc.n_set(j,i,p_id);
1322 ed[cp][cs]=j;
1323 ed[cp][nu[cp]+cs]=i;
1324 i++;
1327 // Copy in the edges of the underlying vertex,
1328 // and do one less if this was a double edge
1329 qs=iqs;
1330 while(i<(new_double_edge?k:k-1)) {
1331 qs=cycle_up(qs,qp);
1332 lp=ed[qp][qs];ls=ed[qp][nu[qp]+qs];
1333 vc.n_copy(j,i,qp,qs);
1334 ed[j][i]=lp;
1335 ed[j][nu[j]+i]=ls;
1336 ed[lp][ls]=j;
1337 ed[lp][nu[lp]+ls]=i;
1338 ed[qp][qs]=-1;
1339 i++;
1341 qs=cycle_up(qs,qp);
1342 cs=i;
1343 cp=j;
1344 vc.n_copy(j,new_double_edge?0:cs,qp,qs);
1346 // Update the double_edge flag, to pass it
1347 // to the next instance of this routine
1348 double_edge=new_double_edge;
1352 // Connect the final created vertex to the initial one
1353 ed[cp][cs]=rp;
1354 *ed[rp]=cp;
1355 ed[cp][nu[cp]+cs]=0;
1356 ed[rp][nu[rp]]=cs;
1357 return false;
1360 /** During the creation of a new facet in the plane routine, it is possible
1361 * that some order two vertices may arise. This routine removes them.
1362 * Suppose an order two vertex joins c and d. If there's a edge between
1363 * c and d already, then the order two vertex is just removed; otherwise,
1364 * the order two vertex is removed and c and d are joined together directly.
1365 * It is possible this process will create order two or order one vertices,
1366 * and the routine is continually run until all of them are removed.
1367 * \return False if the vertex removal was unsuccessful, indicative of the cell
1368 * reducing to zero volume and disappearing; true if the vertex removal
1369 * was successful. */
1370 template<class vc_class>
1371 inline bool voronoicell_base::collapse_order2(vc_class &vc) {
1372 if(!collapse_order1(vc)) return false;
1373 int a,b,i,j,k,l;
1374 while(mec[2]>0) {
1376 // Pick a order 2 vertex and read in its edges
1377 i=--mec[2];
1378 j=mep[2][5*i];k=mep[2][5*i+1];
1379 if(j==k) {
1380 #if VOROPP_VERBOSE >=1
1381 fputs("Order two vertex joins itself",stderr);
1382 #endif
1383 return false;
1386 // Scan the edges of j to see if joins k
1387 for(l=0;l<nu[j];l++) {
1388 if(ed[j][l]==k) break;
1391 // If j doesn't already join k, join them together.
1392 // Otherwise delete the connection to the current
1393 // vertex from j and k.
1394 a=mep[2][5*i+2];b=mep[2][5*i+3];i=mep[2][5*i+4];
1395 if(l==nu[j]) {
1396 ed[j][a]=k;
1397 ed[k][b]=j;
1398 ed[j][nu[j]+a]=b;
1399 ed[k][nu[k]+b]=a;
1400 } else {
1401 if(!delete_connection(vc,j,a,false)) return false;
1402 if(!delete_connection(vc,k,b,true)) return false;
1405 // Compact the memory
1406 --p;
1407 if(up==i) up=0;
1408 if(p!=i) {
1409 if(up==p) up=i;
1410 pts[i<<2]=pts[p<<2];
1411 pts[(i<<2)+1]=pts[(p<<2)+1];
1412 pts[(i<<2)+2]=pts[(p<<2)+2];
1413 for(k=0;k<nu[p];k++) ed[ed[p][k]][ed[p][nu[p]+k]]=i;
1414 vc.n_copy_pointer(i,p);
1415 ed[i]=ed[p];
1416 nu[i]=nu[p];
1417 ed[i][nu[i]<<1]=i;
1420 // Collapse any order 1 vertices if they were created
1421 if(!collapse_order1(vc)) return false;
1423 return true;
1426 /** Order one vertices can potentially be created during the order two collapse
1427 * routine. This routine keeps removing them until there are none left.
1428 * \return False if the vertex removal was unsuccessful, indicative of the cell
1429 * having zero volume and disappearing; true if the vertex removal was
1430 * successful. */
1431 template<class vc_class>
1432 bool voronoicell_base::collapse_order1(vc_class &vc) {
1433 int i,j,k;
1434 while(mec[1]>0) {
1435 up=0;
1436 #if VOROPP_VERBOSE >=1
1437 fputs("Order one collapse\n",stderr);
1438 #endif
1439 i=--mec[1];
1440 j=mep[1][3*i];k=mep[1][3*i+1];
1441 i=mep[1][3*i+2];
1442 if(!delete_connection(vc,j,k,false)) return false;
1443 --p;
1444 if(up==i) up=0;
1445 if(p!=i) {
1446 if(up==p) up=i;
1447 pts[i<<2]=pts[p<<2];
1448 pts[(i<<2)+1]=pts[(p<<2)+1];
1449 pts[(i<<2)+2]=pts[(p<<2)+2];
1450 for(k=0;k<nu[p];k++) ed[ed[p][k]][ed[p][nu[p]+k]]=i;
1451 vc.n_copy_pointer(i,p);
1452 ed[i]=ed[p];
1453 nu[i]=nu[p];
1454 ed[i][nu[i]<<1]=i;
1457 return true;
1460 /** This routine deletes the kth edge of vertex j and reorganizes the memory.
1461 * If the neighbor computation is enabled, we also have to supply an handedness
1462 * flag to decide whether to preserve the plane on the left or right of the
1463 * connection.
1464 * \return False if a zero order vertex was formed, indicative of the cell
1465 * disappearing; true if the vertex removal was successful. */
1466 template<class vc_class>
1467 bool voronoicell_base::delete_connection(vc_class &vc,int j,int k,bool hand) {
1468 int q=hand?k:cycle_up(k,j);
1469 int i=nu[j]-1,l,*edp,*edd,m;
1470 #if VOROPP_VERBOSE >=1
1471 if(i<1) {
1472 fputs("Zero order vertex formed\n",stderr);
1473 return false;
1475 #endif
1476 if(mec[i]==mem[i]) add_memory(vc,i);
1477 vc.n_set_aux1(i);
1478 for(l=0;l<q;l++) vc.n_copy_aux1(j,l);
1479 while(l<i) {
1480 vc.n_copy_aux1_shift(j,l);
1481 l++;
1483 edp=mep[i]+((i<<1)+1)*mec[i]++;
1484 edp[i<<1]=j;
1485 for(l=0;l<k;l++) {
1486 edp[l]=ed[j][l];
1487 edp[l+i]=ed[j][l+nu[j]];
1489 while(l<i) {
1490 m=ed[j][l+1];
1491 edp[l]=m;
1492 k=ed[j][l+nu[j]+1];
1493 edp[l+i]=k;
1494 ed[m][nu[m]+k]--;
1495 l++;
1498 edd=mep[nu[j]]+((nu[j]<<1)+1)*--mec[nu[j]];
1499 for(l=0;l<=(nu[j]<<1);l++) ed[j][l]=edd[l];
1500 vc.n_set_aux2_copy(j,nu[j]);
1501 vc.n_copy_pointer(edd[nu[j]<<1],j);
1502 vc.n_set_to_aux1(j);
1503 ed[edd[nu[j]<<1]]=ed[j];
1504 ed[j]=edp;
1505 nu[j]=i;
1506 return true;
1509 /** This routine is a fall-back, in case floating point errors caused the usual
1510 * search routine to fail. In the fall-back routine, we just test every edge to
1511 * find one straddling the plane. */
1512 bool voronoicell_base::failsafe_find(int &lp,int &ls,int &us,double &l,double &u) {
1513 fputs("Bailed out of convex calculation (not supported yet)\n",stderr);
1514 exit(1);
1515 /* qw=1;lw=0;
1516 for(qp=0;qp<p;qp++) {
1517 qw=m_test(qp,q);
1518 if(qw==1) {
1520 // The point is inside the cutting space. Now
1521 // see if we can find a neighbor which isn't.
1522 for(us=0;us<nu[qp];us++) {
1523 lp=ed[qp][us];
1524 if(lp<qp) {
1525 lw=m_test(lp,l);
1526 if(lw!=1) break;
1529 if(us<nu[qp]) {
1530 up=qp;
1531 if(lw==0) {
1532 complicated_setup=true;
1533 } else {
1534 complicated_setup=false;
1535 u=q;
1536 ls=ed[up][nu[up]+us];
1538 break;
1540 } else if(qw==-1) {
1542 // The point is outside the cutting space. See
1543 // if we can find a neighbor which isn't.
1544 for(ls=0;ls<nu[qp];ls++) {
1545 up=ed[qp][ls];
1546 if(up<qp) {
1547 uw=m_test(up,u);
1548 if(uw!=-1) break;
1551 if(ls<nu[qp]) {
1552 if(uw==0) {
1553 up=qp;
1554 complicated_setup=true;
1555 } else {
1556 complicated_setup=false;
1557 lp=qp;l=q;
1558 us=ed[lp][nu[lp]+ls];
1560 break;
1562 } else {
1564 // The point is in the plane, so we just
1565 // proceed with the complicated setup routine
1566 up=qp;
1567 complicated_setup=true;
1568 break;
1571 if(qp==p) return qw==-1?true:false;*/
1574 /** Calculates the volume of the Voronoi cell, by decomposing the cell into
1575 * tetrahedra extending outward from the zeroth vertex, whose volumes are
1576 * evaluated using a scalar triple product.
1577 * \return A floating point number holding the calculated volume. */
1578 double voronoicell_base::volume() {
1579 const double fe=1/48.0;
1580 double vol=0;
1581 int i,j,k,l,m,n;
1582 double ux,uy,uz,vx,vy,vz,wx,wy,wz;
1583 for(i=1;i<p;i++) {
1584 ux=*pts-pts[i<<2];
1585 uy=pts[1]-pts[(i<<2)+1];
1586 uz=pts[2]-pts[(i<<2)+2];
1587 for(j=0;j<nu[i];j++) {
1588 k=ed[i][j];
1589 if(k>=0) {
1590 ed[i][j]=-1-k;
1591 l=cycle_up(ed[i][nu[i]+j],k);
1592 vx=pts[k<<2]-*pts;
1593 vy=pts[(k<<2)+1]-pts[1];
1594 vz=pts[(k<<2)+2]-pts[2];
1595 m=ed[k][l];ed[k][l]=-1-m;
1596 while(m!=i) {
1597 n=cycle_up(ed[k][nu[k]+l],m);
1598 wx=pts[(m<<2)]-*pts;
1599 wy=pts[(m<<2)+1]-pts[1];
1600 wz=pts[(m<<2)+2]-pts[2];
1601 vol+=ux*vy*wz+uy*vz*wx+uz*vx*wy-uz*vy*wx-uy*vx*wz-ux*vz*wy;
1602 k=m;l=n;vx=wx;vy=wy;vz=wz;
1603 m=ed[k][l];ed[k][l]=-1-m;
1608 reset_edges();
1609 return vol*fe;
1612 /** Calculates the contributions to the Minkowski functionals for this Voronoi cell.
1613 * \param[in] r the radius to consider.
1614 * \param[out] ar the area functional.
1615 * \param[out] vo the volume functional. */
1616 void voronoicell_base::minkowski(double r,double &ar,double &vo) {
1617 int i,j,k,l,m,n;
1618 ar=vo=0;r*=2;
1619 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
1620 k=ed[i][j];
1621 if(k>=0) {
1622 ed[i][j]=-1-k;
1623 l=cycle_up(ed[i][nu[i]+j],k);
1624 m=ed[k][l];ed[k][l]=-1-m;
1625 while(m!=i) {
1626 n=cycle_up(ed[k][nu[k]+l],m);
1627 minkowski_contrib(i,k,m,r,ar,vo);
1628 k=m;l=n;
1629 m=ed[k][l];ed[k][l]=-1-m;
1633 vo*=0.125;
1634 ar*=0.25;
1635 reset_edges();
1638 inline void voronoicell_base::minkowski_contrib(int i,int k,int m,double r,double &ar,double &vo) {
1639 double ix=pts[4*i],iy=pts[4*i+1],iz=pts[4*i+2],
1640 kx=pts[4*k],ky=pts[4*k+1],kz=pts[4*k+2],
1641 mx=pts[4*m],my=pts[4*m+1],mz=pts[4*m+2],
1642 ux=kx-ix,uy=ky-iy,uz=kz-iz,vx=mx-kx,vy=my-ky,vz=mz-kz,
1643 e1x=uz*vy-uy*vz,e1y=ux*vz-uz*vx,e1z=uy*vx-ux*vy,e2x,e2y,e2z,
1644 wmag=e1x*e1x+e1y*e1y+e1z*e1z;
1645 if(wmag<tol*tol) return;
1646 wmag=1/sqrt(wmag);
1647 e1x*=wmag;e1y*=wmag;e1z*=wmag;
1649 // Compute second orthonormal vector
1650 if(fabs(e1x)>0.5) {
1651 e2x=-e1y;e2y=e1x;e2z=0;
1652 } else if(fabs(e1y)>0.5) {
1653 e2x=0;e2y=-e1z;e2z=e1y;
1654 } else {
1655 e2x=e1z;e2y=0;e2z=-e1x;
1657 wmag=1/sqrt(e2x*e2x+e2y*e2y+e2z*e2z);
1658 e2x*=wmag;e2y*=wmag;e2z*=wmag;
1660 // Compute third orthonormal vector
1661 double e3x=e1z*e2y-e1y*e2z,
1662 e3y=e1x*e2z-e1z*e2x,
1663 e3z=e1y*e2x-e1x*e2y,
1664 x0=e1x*ix+e1y*iy+e1z*iz;
1665 if(x0<tol) return;
1667 double ir=e2x*ix+e2y*iy+e2z*iz,is=e3x*ix+e3y*iy+e3z*iz,
1668 kr=e2x*kx+e2y*ky+e2z*kz,ks=e3x*kx+e3y*ky+e3z*kz,
1669 mr=e2x*mx+e2y*my+e2z*mz,ms=e3x*mx+e3y*my+e3z*mz;
1671 minkowski_edge(x0,ir,is,kr,ks,r,ar,vo);
1672 minkowski_edge(x0,kr,ks,mr,ms,r,ar,vo);
1673 minkowski_edge(x0,mr,ms,ir,is,r,ar,vo);
1676 void voronoicell_base::minkowski_edge(double x0,double r1,double s1,double r2,double s2,double r,double &ar,double &vo) {
1677 double r12=r2-r1,s12=s2-s1,l12=r12*r12+s12*s12;
1678 if(l12<tol*tol) return;
1679 l12=1/sqrt(l12);r12*=l12;s12*=l12;
1680 double y0=s12*r1-r12*s1;
1681 if(fabs(y0)<tol) return;
1682 minkowski_formula(x0,y0,-r12*r1-s12*s1,r,ar,vo);
1683 minkowski_formula(x0,y0,r12*r2+s12*s2,r,ar,vo);
1686 void voronoicell_base::minkowski_formula(double x0,double y0,double z0,double r,double &ar,double &vo) {
1687 const double pi=3.1415926535897932384626433832795;
1688 if(fabs(z0)<tol) return;
1689 double si;
1690 if(z0<0) {z0=-z0;si=-1;} else si=1;
1691 if(y0<0) {y0=-y0;si=-si;}
1692 double xs=x0*x0,ys=y0*y0,zs=z0*z0,res=xs+ys,rvs=res+zs,theta=atan(z0/y0),rs=r*r,rc=rs*r,temp,voc,arc;
1693 if(r<x0) {
1694 temp=2*theta-0.5*pi-asin((zs*xs-ys*rvs)/(res*(ys+zs)));
1695 voc=rc/6.*temp;
1696 arc=rs*0.5*temp;
1697 } else if(rs<res*1.0000000001) {
1698 temp=0.5*pi+asin((zs*xs-ys*rvs)/(res*(ys+zs)));
1699 voc=theta*0.5*(rs*x0-xs*x0/3.)-rc/6.*temp;
1700 arc=theta*x0*r-rs*0.5*temp;
1701 } else if(rs<rvs) {
1702 temp=theta-pi*0.5+asin(y0/sqrt(rs-xs));
1703 double temp2=(rs*x0-xs*x0/3.),
1704 x2s=rs*xs/res,y2s=rs*ys/res,
1705 temp3=asin((x2s-y2s-xs)/(rs-xs)),
1706 temp4=asin((zs*xs-ys*rvs)/(res*(ys+zs))),
1707 temp5=sqrt(rs-res);
1708 voc=0.5*temp*temp2+x0*y0/6.*temp5+r*rs/6*(temp3-temp4);
1709 arc=x0*r*temp-0.5*temp2*y0*r/((rs-xs)*temp5)+x0*y0/6.*r/temp5+rs*0.5*temp3+rs*rs/3.*2*xs*ys/(res*(rs-xs)*sqrt((rs-xs)*(rs-xs)-(x2s-y2s-xs)*(x2s-y2s-xs)))-rs*0.5*temp4;
1710 } else {
1711 voc=x0*y0*z0/6.;
1712 arc=0;
1714 vo+=voc*si;
1715 ar+=arc*si;
1718 /** Calculates the areas of each face of the Voronoi cell and prints the
1719 * results to an output stream.
1720 * \param[out] v the vector to store the results in. */
1721 void voronoicell_base::face_areas(std::vector<double> &v) {
1722 double area;
1723 v.clear();
1724 int i,j,k,l,m,n;
1725 double ux,uy,uz,vx,vy,vz,wx,wy,wz;
1726 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
1727 k=ed[i][j];
1728 if(k>=0) {
1729 area=0;
1730 ed[i][j]=-1-k;
1731 l=cycle_up(ed[i][nu[i]+j],k);
1732 m=ed[k][l];ed[k][l]=-1-m;
1733 while(m!=i) {
1734 n=cycle_up(ed[k][nu[k]+l],m);
1735 ux=pts[4*k]-pts[4*i];
1736 uy=pts[4*k+1]-pts[4*i+1];
1737 uz=pts[4*k+2]-pts[4*i+2];
1738 vx=pts[4*m]-pts[4*i];
1739 vy=pts[4*m+1]-pts[4*i+1];
1740 vz=pts[4*m+2]-pts[4*i+2];
1741 wx=uy*vz-uz*vy;
1742 wy=uz*vx-ux*vz;
1743 wz=ux*vy-uy*vx;
1744 area+=sqrt(wx*wx+wy*wy+wz*wz);
1745 k=m;l=n;
1746 m=ed[k][l];ed[k][l]=-1-m;
1748 v.push_back(0.125*area);
1751 reset_edges();
1754 /** Calculates the total surface area of the Voronoi cell.
1755 * \return The computed area. */
1756 double voronoicell_base::surface_area() {
1757 double area=0;
1758 int i,j,k,l,m,n;
1759 double ux,uy,uz,vx,vy,vz,wx,wy,wz;
1760 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
1761 k=ed[i][j];
1762 if(k>=0) {
1763 ed[i][j]=-1-k;
1764 l=cycle_up(ed[i][nu[i]+j],k);
1765 m=ed[k][l];ed[k][l]=-1-m;
1766 while(m!=i) {
1767 n=cycle_up(ed[k][nu[k]+l],m);
1768 ux=pts[4*k]-pts[4*i];
1769 uy=pts[4*k+1]-pts[4*i+1];
1770 uz=pts[4*k+2]-pts[4*i+2];
1771 vx=pts[4*m]-pts[4*i];
1772 vy=pts[4*m+1]-pts[4*i+1];
1773 vz=pts[4*m+2]-pts[4*i+2];
1774 wx=uy*vz-uz*vy;
1775 wy=uz*vx-ux*vz;
1776 wz=ux*vy-uy*vx;
1777 area+=sqrt(wx*wx+wy*wy+wz*wz);
1778 k=m;l=n;
1779 m=ed[k][l];ed[k][l]=-1-m;
1783 reset_edges();
1784 return 0.125*area;
1787 /** Calculates the centroid of the Voronoi cell, by decomposing the cell into
1788 * tetrahedra extending outward from the zeroth vertex.
1789 * \param[out] (cx,cy,cz) references to floating point numbers in which to
1790 * pass back the centroid vector. */
1791 void voronoicell_base::centroid(double &cx,double &cy,double &cz) {
1792 double tvol,vol=0;cx=cy=cz=0;
1793 int i,j,k,l,m,n;
1794 double ux,uy,uz,vx,vy,vz,wx,wy,wz;
1795 for(i=1;i<p;i++) {
1796 ux=*pts-pts[4*i];
1797 uy=pts[1]-pts[4*i+1];
1798 uz=pts[2]-pts[4*i+2];
1799 for(j=0;j<nu[i];j++) {
1800 k=ed[i][j];
1801 if(k>=0) {
1802 ed[i][j]=-1-k;
1803 l=cycle_up(ed[i][nu[i]+j],k);
1804 vx=pts[4*k]-*pts;
1805 vy=pts[4*k+1]-pts[1];
1806 vz=pts[4*k+2]-pts[2];
1807 m=ed[k][l];ed[k][l]=-1-m;
1808 while(m!=i) {
1809 n=cycle_up(ed[k][nu[k]+l],m);
1810 wx=pts[4*m]-*pts;
1811 wy=pts[4*m+1]-pts[1];
1812 wz=pts[4*m+2]-pts[2];
1813 tvol=ux*vy*wz+uy*vz*wx+uz*vx*wy-uz*vy*wx-uy*vx*wz-ux*vz*wy;
1814 vol+=tvol;
1815 cx+=(wx+vx-ux)*tvol;
1816 cy+=(wy+vy-uy)*tvol;
1817 cz+=(wz+vz-uz)*tvol;
1818 k=m;l=n;vx=wx;vy=wy;vz=wz;
1819 m=ed[k][l];ed[k][l]=-1-m;
1824 reset_edges();
1825 if(vol>tol_cu) {
1826 vol=0.125/vol;
1827 cx=cx*vol+0.5*(*pts);
1828 cy=cy*vol+0.5*pts[1];
1829 cz=cz*vol+0.5*pts[2];
1830 } else cx=cy=cz=0;
1833 /** Computes the maximum radius squared of a vertex from the center of the
1834 * cell. It can be used to determine when enough particles have been testing an
1835 * all planes that could cut the cell have been considered.
1836 * \return The maximum radius squared of a vertex.*/
1837 double voronoicell_base::max_radius_squared() {
1838 double r,s,*ptsp=pts+4,*ptse=pts+(p<<2);
1839 r=*pts*(*pts)+pts[1]*pts[1]+pts[2]*pts[2];
1840 while(ptsp<ptse) {
1841 s=*ptsp*(*ptsp);ptsp++;
1842 s+=*ptsp*(*ptsp);ptsp++;
1843 s+=*ptsp*(*ptsp);ptsp+=2;
1844 if(s>r) r=s;
1846 return r;
1849 /** Calculates the total edge distance of the Voronoi cell.
1850 * \return A floating point number holding the calculated distance. */
1851 double voronoicell_base::total_edge_distance() {
1852 int i,j,k;
1853 double dis=0,dx,dy,dz;
1854 for(i=0;i<p-1;i++) for(j=0;j<nu[i];j++) {
1855 k=ed[i][j];
1856 if(k>i) {
1857 dx=pts[k<<2]-pts[i<<2];
1858 dy=pts[(k<<2)+1]-pts[(i<<2)+1];
1859 dz=pts[(k<<2)+2]-pts[(i<<2)+2];
1860 dis+=sqrt(dx*dx+dy*dy+dz*dz);
1863 return 0.5*dis;
1866 /** Outputs the edges of the Voronoi cell in POV-Ray format to an open file
1867 * stream, displacing the cell by given vector.
1868 * \param[in] (x,y,z) a displacement vector to be added to the cell's position.
1869 * \param[in] fp a file handle to write to. */
1870 void voronoicell_base::draw_pov(double x,double y,double z,FILE* fp) {
1871 int i,j,k;double *ptsp=pts,*pt2;
1872 char posbuf1[128],posbuf2[128];
1873 for(i=0;i<p;i++,ptsp+=4) {
1874 sprintf(posbuf1,"%g,%g,%g",x+*ptsp*0.5,y+ptsp[1]*0.5,z+ptsp[2]*0.5);
1875 fprintf(fp,"sphere{<%s>,r}\n",posbuf1);
1876 for(j=0;j<nu[i];j++) {
1877 k=ed[i][j];
1878 if(k<i) {
1879 pt2=pts+(k<<2);
1880 sprintf(posbuf2,"%g,%g,%g",x+*pt2*0.5,y+0.5*pt2[1],z+0.5*pt2[2]);
1881 if(strcmp(posbuf1,posbuf2)!=0) fprintf(fp,"cylinder{<%s>,<%s>,r}\n",posbuf1,posbuf2);
1887 /** Outputs the edges of the Voronoi cell in gnuplot format to an output stream.
1888 * \param[in] (x,y,z) a displacement vector to be added to the cell's position.
1889 * \param[in] fp a file handle to write to. */
1890 void voronoicell_base::draw_gnuplot(double x,double y,double z,FILE *fp) {
1891 int i,j,k,l,m;
1892 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
1893 k=ed[i][j];
1894 if(k>=0) {
1895 fprintf(fp,"%g %g %g\n",x+0.5*pts[i<<2],y+0.5*pts[(i<<2)+1],z+0.5*pts[(i<<2)+2]);
1896 l=i;m=j;
1897 do {
1898 ed[k][ed[l][nu[l]+m]]=-1-l;
1899 ed[l][m]=-1-k;
1900 l=k;
1901 fprintf(fp,"%g %g %g\n",x+0.5*pts[k<<2],y+0.5*pts[(k<<2)+1],z+0.5*pts[(k<<2)+2]);
1902 } while (search_edge(l,m,k));
1903 fputs("\n\n",fp);
1906 reset_edges();
1909 inline bool voronoicell_base::search_edge(int l,int &m,int &k) {
1910 for(m=0;m<nu[l];m++) {
1911 k=ed[l][m];
1912 if(k>=0) return true;
1914 return false;
1917 /** Outputs the Voronoi cell in the POV mesh2 format, described in section
1918 * 1.3.2.2 of the POV-Ray documentation. The mesh2 output consists of a list of
1919 * vertex vectors, followed by a list of triangular faces. The routine also
1920 * makes use of the optional inside_vector specification, which makes the mesh
1921 * object solid, so that the POV-Ray Constructive Solid Geometry (CSG) can be
1922 * applied.
1923 * \param[in] (x,y,z) a displacement vector to be added to the cell's position.
1924 * \param[in] fp a file handle to write to. */
1925 void voronoicell_base::draw_pov_mesh(double x,double y,double z,FILE *fp) {
1926 int i,j,k,l,m,n;
1927 double *ptsp=pts;
1928 fprintf(fp,"mesh2 {\nvertex_vectors {\n%d\n",p);
1929 for(i=0;i<p;i++,ptsp+=4) fprintf(fp,",<%g,%g,%g>\n",x+*ptsp*0.5,y+ptsp[1]*0.5,z+ptsp[2]*0.5);
1930 fprintf(fp,"}\nface_indices {\n%d\n",(p-2)<<1);
1931 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
1932 k=ed[i][j];
1933 if(k>=0) {
1934 ed[i][j]=-1-k;
1935 l=cycle_up(ed[i][nu[i]+j],k);
1936 m=ed[k][l];ed[k][l]=-1-m;
1937 while(m!=i) {
1938 n=cycle_up(ed[k][nu[k]+l],m);
1939 fprintf(fp,",<%d,%d,%d>\n",i,k,m);
1940 k=m;l=n;
1941 m=ed[k][l];ed[k][l]=-1-m;
1945 fputs("}\ninside_vector <0,0,1>\n}\n",fp);
1946 reset_edges();
1949 /** Several routines in the class that gather cell-based statistics internally
1950 * track their progress by flipping edges to negative so that they know what
1951 * parts of the cell have already been tested. This function resets them back
1952 * to positive. When it is called, it assumes that every edge in the routine
1953 * should have already been flipped to negative, and it bails out with an
1954 * internal error if it encounters a positive edge. */
1955 inline void voronoicell_base::reset_edges() {
1956 int i,j;
1957 for(i=0;i<p;i++) for(j=0;j<nu[i];j++) {
1958 if(ed[i][j]>=0) voro_fatal_error("Edge reset routine found a previously untested edge",VOROPP_INTERNAL_ERROR);
1959 ed[i][j]=-1-ed[i][j];
1963 /** Checks to see if a given vertex is inside, outside or within the test
1964 * plane. If the point is far away from the test plane, the routine immediately
1965 * returns whether it is inside or outside. If the routine is close the the
1966 * plane and within the specified tolerance, then the special check_marginal()
1967 * routine is called.
1968 * \param[in] n the vertex to test.
1969 * \param[out] ans the result of the scalar product used in evaluating the
1970 * location of the point.
1971 * \return -1 if the point is inside the plane, 1 if the point is outside the
1972 * plane, or 0 if the point is within the plane. */
1973 inline unsigned int voronoicell_base::m_test(int n,double &ans) {
1974 if(mask[n]>=maskc) {
1975 ans=pts[4*n+3];
1976 return mask[n]&3;
1977 } else return m_calc(n,ans);
1980 unsigned int voronoicell_base::m_calc(int n,double &ans) {
1981 double *pp=pts+4*n;
1982 ans=*(pp++)*px;
1983 ans+=*(pp++)*py;
1984 ans+=*(pp++)*pz-prsq;
1985 *pp=ans;
1986 unsigned int maskr=ans<-tol?0:(ans>tol?2:1);
1987 mask[n]=maskc|maskr;
1988 return maskr;
1991 /** Checks to see if a given vertex is inside, outside or within the test
1992 * plane. If the point is far away from the test plane, the routine immediately
1993 * returns whether it is inside or outside. If the routine is close the the
1994 * plane and within the specified tolerance, then the special check_marginal()
1995 * routine is called.
1996 * \param[in] n the vertex to test.
1997 * \param[out] ans the result of the scalar product used in evaluating the
1998 * location of the point.
1999 * \return -1 if the point is inside the plane, 1 if the point is outside the
2000 * plane, or 0 if the point is within the plane. */
2001 inline unsigned int voronoicell_base::m_testx(int n,double &ans) {
2002 unsigned int maskr;
2003 if(mask[n]>=maskc) {
2004 ans=pts[4*n+3];
2005 maskr=mask[n]&3;
2006 } else maskr=m_calc(n,ans);
2007 if(maskr==0&&ans>-big_tol&&ed[n][nu[n]<<1]!=-1) {
2008 ed[n][nu[n]<<1]=-1;
2009 if(stackp3==stacke3) add_memory_xse();
2010 *(stackp3++)=n;
2012 return maskr;
2015 /** This routine calculates the unit normal vectors for every face.
2016 * \param[out] v the vector to store the results in. */
2017 void voronoicell_base::normals(std::vector<double> &v) {
2018 int i,j,k;
2019 v.clear();
2020 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
2021 k=ed[i][j];
2022 if(k>=0) normals_search(v,i,j,k);
2024 reset_edges();
2027 /** This inline routine is called by normals(). It attempts to construct a
2028 * single normal vector that is associated with a particular face. It first
2029 * traces around the face, trying to find two vectors along the face edges
2030 * whose vector product is above the numerical tolerance. It then constructs
2031 * the normal vector using this product. If the face is too small, and none of
2032 * the vector products are large enough, the routine may return (0,0,0) as the
2033 * normal vector.
2034 * \param[in] v the vector to store the results in.
2035 * \param[in] i the initial vertex of the face to test.
2036 * \param[in] j the index of an edge of the vertex.
2037 * \param[in] k the neighboring vertex of i, set to ed[i][j]. */
2038 inline void voronoicell_base::normals_search(std::vector<double> &v,int i,int j,int k) {
2039 ed[i][j]=-1-k;
2040 int l=cycle_up(ed[i][nu[i]+j],k),m;
2041 double ux,uy,uz,vx,vy,vz,wx,wy,wz,wmag;
2042 do {
2043 m=ed[k][l];ed[k][l]=-1-m;
2044 ux=pts[4*m]-pts[4*k];
2045 uy=pts[4*m+1]-pts[4*k+1];
2046 uz=pts[4*m+2]-pts[4*k+2];
2048 // Test to see if the length of this edge is above the tolerance
2049 if(ux*ux+uy*uy+uz*uz>tol) {
2050 while(m!=i) {
2051 l=cycle_up(ed[k][nu[k]+l],m);
2052 k=m;m=ed[k][l];ed[k][l]=-1-m;
2053 vx=pts[4*m]-pts[4*k];
2054 vy=pts[4*m+1]-pts[4*k+1];
2055 vz=pts[4*m+2]-pts[4*k+2];
2057 // Construct the vector product of this edge with
2058 // the previous one
2059 wx=uz*vy-uy*vz;
2060 wy=ux*vz-uz*vx;
2061 wz=uy*vx-ux*vy;
2062 wmag=wx*wx+wy*wy+wz*wz;
2064 // Test to see if this vector product of the
2065 // two edges is above the tolerance
2066 if(wmag>tol) {
2068 // Construct the normal vector and print it
2069 wmag=1/sqrt(wmag);
2070 v.push_back(wx*wmag);
2071 v.push_back(wy*wmag);
2072 v.push_back(wz*wmag);
2074 // Mark all of the remaining edges of this
2075 // face and exit
2076 while(m!=i) {
2077 l=cycle_up(ed[k][nu[k]+l],m);
2078 k=m;m=ed[k][l];ed[k][l]=-1-m;
2080 return;
2083 v.push_back(0);
2084 v.push_back(0);
2085 v.push_back(0);
2086 return;
2088 l=cycle_up(ed[k][nu[k]+l],m);
2089 k=m;
2090 } while (k!=i);
2091 v.push_back(0);
2092 v.push_back(0);
2093 v.push_back(0);
2096 /** Returns the number of faces of a computed Voronoi cell.
2097 * \return The number of faces. */
2098 int voronoicell_base::number_of_faces() {
2099 int i,j,k,l,m,s=0;
2100 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
2101 k=ed[i][j];
2102 if(k>=0) {
2103 s++;
2104 ed[i][j]=-1-k;
2105 l=cycle_up(ed[i][nu[i]+j],k);
2106 do {
2107 m=ed[k][l];
2108 ed[k][l]=-1-m;
2109 l=cycle_up(ed[k][nu[k]+l],m);
2110 k=m;
2111 } while (k!=i);
2115 reset_edges();
2116 return s;
2119 /** Returns a vector of the vertex orders.
2120 * \param[out] v the vector to store the results in. */
2121 void voronoicell_base::vertex_orders(std::vector<int> &v) {
2122 v.resize(p);
2123 for(int i=0;i<p;i++) v[i]=nu[i];
2126 /** Outputs the vertex orders.
2127 * \param[out] fp the file handle to write to. */
2128 void voronoicell_base::output_vertex_orders(FILE *fp) {
2129 if(p>0) {
2130 fprintf(fp,"%d",*nu);
2131 for(int *nup=nu+1;nup<nu+p;nup++) fprintf(fp," %d",*nup);
2135 /** Returns a vector of the vertex vectors using the local coordinate system.
2136 * \param[out] v the vector to store the results in. */
2137 void voronoicell_base::vertices(std::vector<double> &v) {
2138 v.resize(p<<2);
2139 double *ptsp=pts;
2140 for(int i=0;i<3*p;i+=3) {
2141 v[i]=*(ptsp++)*0.5;
2142 v[i+1]=*(ptsp++)*0.5;
2143 v[i+2]=*ptsp*0.5;ptsp+=2;
2147 /** Outputs the vertex vectors using the local coordinate system.
2148 * \param[out] fp the file handle to write to. */
2149 void voronoicell_base::output_vertices(FILE *fp) {
2150 if(p>0) {
2151 fprintf(fp,"(%g,%g,%g)",*pts*0.5,pts[1]*0.5,pts[2]*0.5);
2152 for(double *ptsp=pts+4;ptsp<pts+(p<<2);ptsp+=4) fprintf(fp," (%g,%g,%g)",*ptsp*0.5,ptsp[1]*0.5,ptsp[2]*0.5);
2156 /** Returns a vector of the vertex vectors in the global coordinate system.
2157 * \param[out] v the vector to store the results in.
2158 * \param[in] (x,y,z) the position vector of the particle in the global
2159 * coordinate system. */
2160 void voronoicell_base::vertices(double x,double y,double z,std::vector<double> &v) {
2161 v.resize(3*p);
2162 double *ptsp=pts;
2163 for(int i=0;i<3*p;i+=3) {
2164 v[i]=x+*(ptsp++)*0.5;
2165 v[i+1]=y+*(ptsp++)*0.5;
2166 v[i+2]=z+*ptsp*0.5;ptsp+=2;
2170 /** Outputs the vertex vectors using the global coordinate system.
2171 * \param[out] fp the file handle to write to.
2172 * \param[in] (x,y,z) the position vector of the particle in the global
2173 * coordinate system. */
2174 void voronoicell_base::output_vertices(double x,double y,double z,FILE *fp) {
2175 if(p>0) {
2176 fprintf(fp,"(%g,%g,%g)",x+*pts*0.5,y+pts[1]*0.5,z+pts[2]*0.5);
2177 for(double *ptsp=pts+4;ptsp<pts+(p<<2);ptsp+=4) fprintf(fp," (%g,%g,%g)",x+*ptsp*0.5,y+ptsp[1]*0.5,z+ptsp[2]*0.5);
2181 /** This routine returns the perimeters of each face.
2182 * \param[out] v the vector to store the results in. */
2183 void voronoicell_base::face_perimeters(std::vector<double> &v) {
2184 v.clear();
2185 int i,j,k,l,m;
2186 double dx,dy,dz,perim;
2187 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
2188 k=ed[i][j];
2189 if(k>=0) {
2190 dx=pts[k<<2]-pts[i<<2];
2191 dy=pts[(k<<2)+1]-pts[(i<<2)+1];
2192 dz=pts[(k<<2)+2]-pts[(i<<2)+2];
2193 perim=sqrt(dx*dx+dy*dy+dz*dz);
2194 ed[i][j]=-1-k;
2195 l=cycle_up(ed[i][nu[i]+j],k);
2196 do {
2197 m=ed[k][l];
2198 dx=pts[m<<2]-pts[k<<2];
2199 dy=pts[(m<<2)+1]-pts[(k<<2)+1];
2200 dz=pts[(m<<2)+2]-pts[(k<<2)+2];
2201 perim+=sqrt(dx*dx+dy*dy+dz*dz);
2202 ed[k][l]=-1-m;
2203 l=cycle_up(ed[k][nu[k]+l],m);
2204 k=m;
2205 } while (k!=i);
2206 v.push_back(0.5*perim);
2209 reset_edges();
2212 /** For each face, this routine outputs a bracketed sequence of numbers
2213 * containing a list of all the vertices that make up that face.
2214 * \param[out] v the vector to store the results in. */
2215 void voronoicell_base::face_vertices(std::vector<int> &v) {
2216 int i,j,k,l,m,vp(0),vn;
2217 v.clear();
2218 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
2219 k=ed[i][j];
2220 if(k>=0) {
2221 v.push_back(0);
2222 v.push_back(i);
2223 ed[i][j]=-1-k;
2224 l=cycle_up(ed[i][nu[i]+j],k);
2225 do {
2226 v.push_back(k);
2227 m=ed[k][l];
2228 ed[k][l]=-1-m;
2229 l=cycle_up(ed[k][nu[k]+l],m);
2230 k=m;
2231 } while (k!=i);
2232 vn=v.size();
2233 v[vp]=vn-vp-1;
2234 vp=vn;
2237 reset_edges();
2240 /** Outputs a list of the number of edges in each face.
2241 * \param[out] v the vector to store the results in. */
2242 void voronoicell_base::face_orders(std::vector<int> &v) {
2243 int i,j,k,l,m,q;
2244 v.clear();
2245 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
2246 k=ed[i][j];
2247 if(k>=0) {
2248 q=1;
2249 ed[i][j]=-1-k;
2250 l=cycle_up(ed[i][nu[i]+j],k);
2251 do {
2252 q++;
2253 m=ed[k][l];
2254 ed[k][l]=-1-m;
2255 l=cycle_up(ed[k][nu[k]+l],m);
2256 k=m;
2257 } while (k!=i);
2258 v.push_back(q);;
2261 reset_edges();
2264 /** Computes the number of edges that each face has and outputs a frequency
2265 * table of the results.
2266 * \param[out] v the vector to store the results in. */
2267 void voronoicell_base::face_freq_table(std::vector<int> &v) {
2268 int i,j,k,l,m,q;
2269 v.clear();
2270 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
2271 k=ed[i][j];
2272 if(k>=0) {
2273 q=1;
2274 ed[i][j]=-1-k;
2275 l=cycle_up(ed[i][nu[i]+j],k);
2276 do {
2277 q++;
2278 m=ed[k][l];
2279 ed[k][l]=-1-m;
2280 l=cycle_up(ed[k][nu[k]+l],m);
2281 k=m;
2282 } while (k!=i);
2283 if((unsigned int) q>=v.size()) v.resize(q+1,0);
2284 v[q]++;
2287 reset_edges();
2290 /** This routine tests to see whether the cell intersects a plane by starting
2291 * from the guess point up. If up intersects, then it immediately returns true.
2292 * Otherwise, it calls the plane_intersects_track() routine.
2293 * \param[in] (x,y,z) the normal vector to the plane.
2294 * \param[in] rsq the distance along this vector of the plane.
2295 * \return False if the plane does not intersect the plane, true if it does. */
2296 bool voronoicell_base::plane_intersects(double x,double y,double z,double rsq) {
2297 double g=x*pts[up<<2]+y*pts[(up<<2)+1]+z*pts[(up<<2)+2];
2298 if(g<rsq) return plane_intersects_track(x,y,z,rsq,g);
2299 return true;
2302 /** This routine tests to see if a cell intersects a plane. It first tests a
2303 * random sample of approximately sqrt(p)/4 points. If any of those are
2304 * intersect, then it immediately returns true. Otherwise, it takes the closest
2305 * point and passes that to plane_intersect_track() routine.
2306 * \param[in] (x,y,z) the normal vector to the plane.
2307 * \param[in] rsq the distance along this vector of the plane.
2308 * \return False if the plane does not intersect the plane, true if it does. */
2309 bool voronoicell_base::plane_intersects_guess(double x,double y,double z,double rsq) {
2310 up=0;
2311 double g=x*pts[up<<2]+y*pts[(up<<2)+1]+z*pts[(up<<2)+2];
2312 if(g<rsq) {
2313 int ca=1,cc=p>>3,mp=1;
2314 double m;
2315 while(ca<cc) {
2316 m=x*pts[4*mp]+y*pts[4*mp+1]+z*pts[4*mp+2];
2317 if(m>g) {
2318 if(m>rsq) return true;
2319 g=m;up=mp;
2321 ca+=mp++;
2323 return plane_intersects_track(x,y,z,rsq,g);
2325 return true;
2328 /* This routine tests to see if a cell intersects a plane, by tracing over the
2329 * cell from vertex to vertex, starting at up. It is meant to be called either
2330 * by plane_intersects() or plane_intersects_track(), when those routines
2331 * cannot immediately resolve the case.
2332 * \param[in] (x,y,z) the normal vector to the plane.
2333 * \param[in] rsq the distance along this vector of the plane.
2334 * \param[in] g the distance of up from the plane.
2335 * \return False if the plane does not intersect the plane, true if it does. */
2336 inline bool voronoicell_base::plane_intersects_track(double x,double y,double z,double rsq,double g) {
2338 for(int tp=0;tp<p;tp++) if(x*pts[tp<<2]+y*pts[(tp<<2)+1]+z*pts[(tp<<2)+2]>rsq) return true;
2339 return false;
2341 int ls,us,lp;
2342 double l,u;
2343 unsigned int uw;
2345 // Initialize the safe testing routine
2346 px=x;py=y;pz=z;prsq=rsq;
2347 maskc+=4;
2348 if(maskc<4) reset_mask();
2350 return search_upward(uw,lp,ls,us,l,u);
2353 int count=0,ls,us,tp;
2354 double t;
2355 // The test point is outside of the cutting space
2356 for(us=0;us<nu[up];us++) {
2357 tp=ed[up][us];
2358 t=x*pts[tp<<2]+y*pts[(tp<<2)+1]+z*pts[(tp<<2)+2];
2359 if(t>g) {
2360 ls=ed[up][nu[up]+us];
2361 up=tp;
2362 while (t<rsq) {
2363 if(++count>=p) {
2364 #if VOROPP_VERBOSE >=1
2365 fputs("Bailed out of convex calculation",stderr);
2366 #endif
2367 for(tp=0;tp<p;tp++) if(x*pts[tp<<2]+y*pts[(tp<<2)+1]+z*pts[(tp<<2)+2]>rsq) return true;
2368 return false;
2371 // Test all the neighbors of the current point
2372 // and find the one which is closest to the
2373 // plane
2374 for(us=0;us<ls;us++) {
2375 tp=ed[up][us];double *pp=pts+(tp<<2);
2376 g=x*(*pp)+y*pp[1]+z*pp[2];
2377 if(g>t) break;
2379 if(us==ls) {
2380 us++;
2381 while(us<nu[up]) {
2382 tp=ed[up][us];double *pp=pts+(tp<<2);
2383 g=x*(*pp)+y*pp[1]+z*pp[2];
2384 if(g>t) break;
2385 us++;
2387 if(us==nu[up]) return false;
2389 ls=ed[up][nu[up]+us];up=tp;t=g;
2391 return true;
2394 return false;*/
2397 /** Counts the number of edges of the Voronoi cell.
2398 * \return the number of edges. */
2399 int voronoicell_base::number_of_edges() {
2400 int edges=0,*nup=nu;
2401 while(nup<nu+p) edges+=*(nup++);
2402 return edges>>1;
2405 /** Outputs a custom string of information about the Voronoi cell. The string
2406 * of information follows a similar style as the C printf command, and detailed
2407 * information about its format is available at
2408 * http://math.lbl.gov/voro++/doc/custom.html.
2409 * \param[in] format the custom string to print.
2410 * \param[in] i the ID of the particle associated with this Voronoi cell.
2411 * \param[in] (x,y,z) the position of the particle associated with this Voronoi
2412 * cell.
2413 * \param[in] r a radius associated with the particle.
2414 * \param[in] fp the file handle to write to. */
2415 void voronoicell_base::output_custom(const char *format,int i,double x,double y,double z,double r,FILE *fp) {
2416 char *fmp=(const_cast<char*>(format));
2417 std::vector<int> vi;
2418 std::vector<double> vd;
2419 while(*fmp!=0) {
2420 if(*fmp=='%') {
2421 fmp++;
2422 switch(*fmp) {
2424 // Particle-related output
2425 case 'i': fprintf(fp,"%d",i);break;
2426 case 'x': fprintf(fp,"%g",x);break;
2427 case 'y': fprintf(fp,"%g",y);break;
2428 case 'z': fprintf(fp,"%g",z);break;
2429 case 'q': fprintf(fp,"%g %g %g",x,y,z);break;
2430 case 'r': fprintf(fp,"%g",r);break;
2432 // Vertex-related output
2433 case 'w': fprintf(fp,"%d",p);break;
2434 case 'p': output_vertices(fp);break;
2435 case 'P': output_vertices(x,y,z,fp);break;
2436 case 'o': output_vertex_orders(fp);break;
2437 case 'm': fprintf(fp,"%g",0.25*max_radius_squared());break;
2439 // Edge-related output
2440 case 'g': fprintf(fp,"%d",number_of_edges());break;
2441 case 'E': fprintf(fp,"%g",total_edge_distance());break;
2442 case 'e': face_perimeters(vd);voro_print_vector(vd,fp);break;
2444 // Face-related output
2445 case 's': fprintf(fp,"%d",number_of_faces());break;
2446 case 'F': fprintf(fp,"%g",surface_area());break;
2447 case 'A': {
2448 face_freq_table(vi);
2449 voro_print_vector(vi,fp);
2450 } break;
2451 case 'a': face_orders(vi);voro_print_vector(vi,fp);break;
2452 case 'f': face_areas(vd);voro_print_vector(vd,fp);break;
2453 case 't': {
2454 face_vertices(vi);
2455 voro_print_face_vertices(vi,fp);
2456 } break;
2457 case 'l': normals(vd);
2458 voro_print_positions(vd,fp);
2459 break;
2460 case 'n': neighbors(vi);
2461 voro_print_vector(vi,fp);
2462 break;
2464 // Volume-related output
2465 case 'v': fprintf(fp,"%g",volume());break;
2466 case 'c': {
2467 double cx,cy,cz;
2468 centroid(cx,cy,cz);
2469 fprintf(fp,"%g %g %g",cx,cy,cz);
2470 } break;
2471 case 'C': {
2472 double cx,cy,cz;
2473 centroid(cx,cy,cz);
2474 fprintf(fp,"%g %g %g",x+cx,y+cy,z+cz);
2475 } break;
2477 // End-of-string reached
2478 case 0: fmp--;break;
2480 // The percent sign is not part of a
2481 // control sequence
2482 default: putc('%',fp);putc(*fmp,fp);
2484 } else putc(*fmp,fp);
2485 fmp++;
2487 fputs("\n",fp);
2490 /** This initializes the class to be a rectangular box. It calls the base class
2491 * initialization routine to set up the edge and vertex information, and then
2492 * sets up the neighbor information, with initial faces being assigned ID
2493 * numbers from -1 to -6.
2494 * \param[in] (xmin,xmax) the minimum and maximum x coordinates.
2495 * \param[in] (ymin,ymax) the minimum and maximum y coordinates.
2496 * \param[in] (zmin,zmax) the minimum and maximum z coordinates. */
2497 void voronoicell_neighbor::init(double xmin,double xmax,double ymin,double ymax,double zmin,double zmax) {
2498 init_base(xmin,xmax,ymin,ymax,zmin,zmax);
2499 int *q=mne[3];
2500 *q=-5;q[1]=-3;q[2]=-1;
2501 q[3]=-5;q[4]=-2;q[5]=-3;
2502 q[6]=-5;q[7]=-1;q[8]=-4;
2503 q[9]=-5;q[10]=-4;q[11]=-2;
2504 q[12]=-6;q[13]=-1;q[14]=-3;
2505 q[15]=-6;q[16]=-3;q[17]=-2;
2506 q[18]=-6;q[19]=-4;q[20]=-1;
2507 q[21]=-6;q[22]=-2;q[23]=-4;
2508 *ne=q;ne[1]=q+3;ne[2]=q+6;ne[3]=q+9;
2509 ne[4]=q+12;ne[5]=q+15;ne[6]=q+18;ne[7]=q+21;
2512 /** This initializes the class to be an octahedron. It calls the base class
2513 * initialization routine to set up the edge and vertex information, and then
2514 * sets up the neighbor information, with the initial faces being assigned ID
2515 * numbers from -1 to -8.
2516 * \param[in] l The distance from the octahedron center to a vertex. Six
2517 * vertices are initialized at (-l,0,0), (l,0,0), (0,-l,0),
2518 * (0,l,0), (0,0,-l), and (0,0,l). */
2519 void voronoicell_neighbor::init_octahedron(double l) {
2520 init_octahedron_base(l);
2521 int *q=mne[4];
2522 *q=-5;q[1]=-6;q[2]=-7;q[3]=-8;
2523 q[4]=-1;q[5]=-2;q[6]=-3;q[7]=-4;
2524 q[8]=-6;q[9]=-5;q[10]=-2;q[11]=-1;
2525 q[12]=-8;q[13]=-7;q[14]=-4;q[15]=-3;
2526 q[16]=-5;q[17]=-8;q[18]=-3;q[19]=-2;
2527 q[20]=-7;q[21]=-6;q[22]=-1;q[23]=-4;
2528 *ne=q;ne[1]=q+4;ne[2]=q+8;ne[3]=q+12;ne[4]=q+16;ne[5]=q+20;
2531 /** This initializes the class to be a tetrahedron. It calls the base class
2532 * initialization routine to set up the edge and vertex information, and then
2533 * sets up the neighbor information, with the initial faces being assigned ID
2534 * numbers from -1 to -4.
2535 * \param (x0,y0,z0) a position vector for the first vertex.
2536 * \param (x1,y1,z1) a position vector for the second vertex.
2537 * \param (x2,y2,z2) a position vector for the third vertex.
2538 * \param (x3,y3,z3) a position vector for the fourth vertex. */
2539 void voronoicell_neighbor::init_tetrahedron(double x0,double y0,double z0,double x1,double y1,double z1,double x2,double y2,double z2,double x3,double y3,double z3) {
2540 init_tetrahedron_base(x0,y0,z0,x1,y1,z1,x2,y2,z2,x3,y3,z3);
2541 int *q=mne[3];
2542 *q=-4;q[1]=-3;q[2]=-2;
2543 q[3]=-3;q[4]=-4;q[5]=-1;
2544 q[6]=-4;q[7]=-2;q[8]=-1;
2545 q[9]=-2;q[10]=-3;q[11]=-1;
2546 *ne=q;ne[1]=q+3;ne[2]=q+6;ne[3]=q+9;
2549 /** This routine checks to make sure the neighbor information of each face is
2550 * consistent. */
2551 void voronoicell_neighbor::check_facets() {
2552 int i,j,k,l,m,q;
2553 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
2554 k=ed[i][j];
2555 if(k>=0) {
2556 ed[i][j]=-1-k;
2557 q=ne[i][j];
2558 l=cycle_up(ed[i][nu[i]+j],k);
2559 do {
2560 m=ed[k][l];
2561 ed[k][l]=-1-m;
2562 if(ne[k][l]!=q) fprintf(stderr,"Facet error at (%d,%d)=%d, started from (%d,%d)=%d\n",k,l,ne[k][l],i,j,q);
2563 l=cycle_up(ed[k][nu[k]+l],m);
2564 k=m;
2565 } while (k!=i);
2568 reset_edges();
2571 /** The class constructor allocates memory for storing neighbor information. */
2572 void voronoicell_neighbor::memory_setup() {
2573 int i;
2574 mne=new int*[current_vertex_order];
2575 ne=new int*[current_vertices];
2576 for(i=0;i<3;i++) mne[i]=new int[init_n_vertices*i];
2577 mne[3]=new int[init_3_vertices*3];
2578 for(i=4;i<current_vertex_order;i++) mne[i]=new int[init_n_vertices*i];
2581 /** The class destructor frees the dynamically allocated memory for storing
2582 * neighbor information. */
2583 voronoicell_neighbor::~voronoicell_neighbor() {
2584 for(int i=current_vertex_order-1;i>=0;i--) if(mem[i]>0) delete [] mne[i];
2585 delete [] mne;
2586 delete [] ne;
2589 /** Computes a vector list of neighbors. */
2590 void voronoicell_neighbor::neighbors(std::vector<int> &v) {
2591 v.clear();
2592 int i,j,k,l,m;
2593 for(i=1;i<p;i++) for(j=0;j<nu[i];j++) {
2594 k=ed[i][j];
2595 if(k>=0) {
2596 v.push_back(ne[i][j]);
2597 ed[i][j]=-1-k;
2598 l=cycle_up(ed[i][nu[i]+j],k);
2599 do {
2600 m=ed[k][l];
2601 ed[k][l]=-1-m;
2602 l=cycle_up(ed[k][nu[k]+l],m);
2603 k=m;
2604 } while (k!=i);
2607 reset_edges();
2610 /** Prints the vertices, their edges, the relation table, and also notifies if
2611 * any memory errors are visible. */
2612 void voronoicell_base::print_edges() {
2613 int j;
2614 double *ptsp=pts;
2615 for(int i=0;i<p;i++,ptsp+=4) {
2616 printf("%d %d ",i,nu[i]);
2617 for(j=0;j<nu[i];j++) printf(" %d",ed[i][j]);
2618 printf(" ");
2619 while(j<(nu[i]<<1)) printf(" %d",ed[i][j]);
2620 printf(" %d",ed[i][j]);
2621 print_edges_neighbors(i);
2622 printf(" %g %g %g %p",*ptsp,ptsp[1],ptsp[2],(void*) ed[i]);
2623 if(ed[i]>=mep[nu[i]]+mec[nu[i]]*((nu[i]<<1)+1)) puts(" Memory error");
2624 else puts("");
2628 /** This prints out the neighbor information for vertex i. */
2629 void voronoicell_neighbor::print_edges_neighbors(int i) {
2630 if(nu[i]>0) {
2631 int j=0;
2632 printf(" (");
2633 while(j<nu[i]-1) printf("%d,",ne[i][j++]);
2634 printf("%d)",ne[i][j]);
2635 } else printf(" ()");
2638 // Explicit instantiation
2639 template bool voronoicell_base::nplane(voronoicell&,double,double,double,double,int);
2640 template bool voronoicell_base::nplane(voronoicell_neighbor&,double,double,double,double,int);
2641 template void voronoicell_base::check_memory_for_copy(voronoicell&,voronoicell_base*);
2642 template void voronoicell_base::check_memory_for_copy(voronoicell_neighbor&,voronoicell_base*);