Voro++
v_compute.cc
Go to the documentation of this file.
00001 // Voro++, a 3D cell-based Voronoi library
00002 //
00003 // Author   : Chris H. Rycroft (LBL / UC Berkeley)
00004 // Email    : chr@alum.mit.edu
00005 // Date     : August 30th 2011
00006 
00007 /** \file v_compute.cc
00008  * \brief Function implementantions for the voro_compute template. */
00009 
00010 #include "worklist.hh"
00011 #include "v_compute.hh"
00012 #include "container.hh"
00013 #include "container_prd.hh"
00014 
00015 namespace voro {
00016 
00017 /** The class constructor initializes constants from the container class, and
00018  * sets up the mask and queue used for Voronoi computations.
00019  * \param[in] con_ a reference to the container class to use.
00020  * \param[in] (hx_,hy_,hz_) the size of the mask to use. */
00021 template<class c_class>
00022 voro_compute<c_class>::voro_compute(c_class &con_,int hx_,int hy_,int hz_) :
00023         con(con_), boxx(con_.boxx), boxy(con_.boxy), boxz(con_.boxz),
00024         xsp(con_.xsp), ysp(con_.ysp), zsp(con_.zsp),
00025         hx(hx_), hy(hy_), hz(hz_), hxy(hx_*hy_), hxyz(hxy*hz_), ps(con_.ps),
00026         id(con_.id), p(con_.p), co(con_.co), bxsq(boxx*boxx+boxy*boxy+boxz*boxz),
00027         mv(0), qu_size(3*(3+hxy+hz*(hx+hy))), wl(con_.wl), mrad(con_.mrad),
00028         mask(new unsigned int[hxyz]), qu(new int[qu_size]), qu_l(qu+qu_size) {
00029         reset_mask();
00030 }
00031 
00032 /** Scans all of the particles within a block to see if any of them have a
00033  * smaller distance to the given test vector. If one is found, the routine
00034  * updates the minimum distance and store information about this particle.
00035  * \param[in] ijk the index of the block.
00036  * \param[in] (x,y,z) the test vector to consider (which may have already had a
00037  *                    periodic displacement applied to it).
00038  * \param[in] (di,dj,dk) the coordinates of the current block, to store if the
00039  *                       particle record is updated.
00040  * \param[in,out] w a reference to a particle record in which to store
00041  *                  information about the particle whose Voronoi cell the
00042  *                  vector is within.
00043  * \param[in,out] mrs the current minimum distance, that may be updated if a
00044  *                    closer particle is found. */
00045 template<class c_class>
00046 inline void voro_compute<c_class>::scan_all(int ijk,double x,double y,double z,int di,int dj,int dk,particle_record &w,double &mrs) {
00047         double x1,y1,z1,rs;bool in_block=false;
00048         for(int l=0;l<co[ijk];l++) {
00049                 x1=p[ijk][ps*l]-x;
00050                 y1=p[ijk][ps*l+1]-y;
00051                 z1=p[ijk][ps*l+2]-z;
00052                 rs=con.r_current_sub(x1*x1+y1*y1+z1*z1,ijk,l);
00053                 if(rs<mrs) {mrs=rs;w.l=l;in_block=true;}
00054         }
00055         if(in_block) {w.ijk=ijk;w.di=di;w.dj=dj,w.dk=dk;}
00056 }
00057 
00058 /** Finds the Voronoi cell that given vector is within. For containers that are
00059  * not radially dependent, this corresponds to findig the particle that is
00060  * closest to the vector; for the radical tessellation containers, this
00061  * corresponds to a finding the minimum weighted distance.
00062  * \param[in] (x,y,z) the vector to consider.
00063  * \param[in] (ci,cj,ck) the coordinates of the block that the test particle is
00064  *                       in relative to the container data structure.
00065  * \param[in] ijk the index of the block that the test particle is in.
00066  * \param[out] w a reference to a particle record in which to store information
00067  *               about the particle whose Voronoi cell the vector is within.
00068  * \param[out] mrs the minimum computed distance. */
00069 template<class c_class>
00070 void voro_compute<c_class>::find_voronoi_cell(double x,double y,double z,int ci,int cj,int ck,int ijk,particle_record &w,double &mrs) {
00071         double qx=0,qy=0,qz=0,rs;
00072         int i,j,k,di,dj,dk,ei,ej,ek,f,g,disp;
00073         double fx,fy,fz,mxs,mys,mzs,*radp;
00074         unsigned int q,*e,*mijk;
00075 
00076         // Init setup for parameters to return
00077         w.ijk=-1;mrs=large_number;
00078 
00079         con.initialize_search(ci,cj,ck,ijk,i,j,k,disp);
00080 
00081         // Test all particles in the particle's local region first
00082         scan_all(ijk,x,y,z,0,0,0,w,mrs);
00083 
00084         // Now compute the fractional position of the particle within its
00085         // region and store it in (fx,fy,fz). We use this to compute an index
00086         // (di,dj,dk) of which subregion the particle is within.
00087         unsigned int m1,m2;
00088         con.frac_pos(x,y,z,ci,cj,ck,fx,fy,fz);
00089         di=int(fx*xsp*wl_fgrid);dj=int(fy*ysp*wl_fgrid);dk=int(fz*zsp*wl_fgrid);
00090 
00091         // The indices (di,dj,dk) tell us which worklist to use, to test the
00092         // blocks in the optimal order. But we only store worklists for the
00093         // eighth of the region where di, dj, and dk are all less than half the
00094         // full grid. The rest of the cases are handled by symmetry. In this
00095         // section, we detect for these cases, by reflecting high values of di,
00096         // dj, and dk. For these cases, a mask is constructed in m1 and m2
00097         // which is used to flip the worklist information when it is loaded.
00098         if(di>=wl_hgrid) {
00099                 mxs=boxx-fx;
00100                 m1=127+(3<<21);m2=1+(1<<21);di=wl_fgrid-1-di;if(di<0) di=0;
00101         } else {m1=m2=0;mxs=fx;}
00102         if(dj>=wl_hgrid) {
00103                 mys=boxy-fy;
00104                 m1|=(127<<7)+(3<<24);m2|=(1<<7)+(1<<24);dj=wl_fgrid-1-dj;if(dj<0) dj=0;
00105         } else mys=fy;
00106         if(dk>=wl_hgrid) {
00107                 mzs=boxz-fz;
00108                 m1|=(127<<14)+(3<<27);m2|=(1<<14)+(1<<27);dk=wl_fgrid-1-dk;if(dk<0) dk=0;
00109         } else mzs=fz;
00110 
00111         // Do a quick test to account for the case when the minimum radius is
00112         // small enought that no other blocks need to be considered
00113         rs=con.r_max_add(mrs);
00114         if(mxs*mxs>rs&&mys*mys>rs&&mzs*mzs>rs) return;
00115 
00116         // Now compute which worklist we are going to use, and set radp and e to
00117         // point at the right offsets
00118         ijk=di+wl_hgrid*(dj+wl_hgrid*dk);
00119         radp=mrad+ijk*wl_seq_length;
00120         e=(const_cast<unsigned int*> (wl))+ijk*wl_seq_length;
00121 
00122         // Read in how many items in the worklist can be tested without having to
00123         // worry about writing to the mask
00124         f=e[0];g=0;
00125         do {
00126 
00127                 // If mrs is less than the minimum distance to any untested
00128                 // block, then we are done
00129                 if(con.r_max_add(mrs)<radp[g]) return;
00130                 g++;
00131 
00132                 // Load in a block off the worklist, permute it with the
00133                 // symmetry mask, and decode its position. These are all
00134                 // integer bit operations so they should run very fast.
00135                 q=e[g];q^=m1;q+=m2;
00136                 di=q&127;di-=64;
00137                 dj=(q>>7)&127;dj-=64;
00138                 dk=(q>>14)&127;dk-=64;
00139 
00140                 // Check that the worklist position is in range
00141                 ei=di+i;if(ei<0||ei>=hx) continue;
00142                 ej=dj+j;if(ej<0||ej>=hy) continue;
00143                 ek=dk+k;if(ek<0||ek>=hz) continue;
00144 
00145                 // Call the compute_min_max_radius() function. This returns
00146                 // true if the minimum distance to the block is bigger than the
00147                 // current mrs, in which case we skip this block and move on.
00148                 // Otherwise, it computes the maximum distance to the block and
00149                 // returns it in crs.
00150                 if(compute_min_radius(di,dj,dk,fx,fy,fz,mrs)) continue;
00151 
00152                 // Now compute which region we are going to loop over, adding a
00153                 // displacement for the periodic cases
00154                 ijk=con.region_index(ci,cj,ck,ei,ej,ek,qx,qy,qz,disp);
00155 
00156                 // If mrs is bigger than the maximum distance to the block,
00157                 // then we have to test all particles in the block for
00158                 // intersections. Otherwise, we do additional checks and skip
00159                 // those particles which can't possibly intersect the block.
00160                 scan_all(ijk,x-qx,y-qy,z-qz,di,dj,dk,w,mrs);
00161         } while(g<f);
00162 
00163         // Update mask value and initialize queue
00164         mv++;
00165         if(mv==0) {reset_mask();mv=1;}
00166         int *qu_s=qu,*qu_e=qu;
00167 
00168         while(g<wl_seq_length-1) {
00169 
00170                 // If mrs is less than the minimum distance to any untested
00171                 // block, then we are done
00172                 if(con.r_max_add(mrs)<radp[g]) return;
00173                 g++;
00174 
00175                 // Load in a block off the worklist, permute it with the
00176                 // symmetry mask, and decode its position. These are all
00177                 // integer bit operations so they should run very fast.
00178                 q=e[g];q^=m1;q+=m2;
00179                 di=q&127;di-=64;
00180                 dj=(q>>7)&127;dj-=64;
00181                 dk=(q>>14)&127;dk-=64;
00182 
00183                 // Compute the position in the mask of the current block. If
00184                 // this lies outside the mask, then skip it. Otherwise, mark
00185                 // it.
00186                 ei=di+i;if(ei<0||ei>=hx) continue;
00187                 ej=dj+j;if(ej<0||ej>=hy) continue;
00188                 ek=dk+k;if(ek<0||ek>=hz) continue;
00189                 mijk=mask+ei+hx*(ej+hy*ek);
00190                 *mijk=mv;
00191 
00192                 // Skip this block if it is further away than the current
00193                 // minimum radius
00194                 if(compute_min_radius(di,dj,dk,fx,fy,fz,mrs)) continue;
00195 
00196                 // Now compute which region we are going to loop over, adding a
00197                 // displacement for the periodic cases
00198                 ijk=con.region_index(ci,cj,ck,ei,ej,ek,qx,qy,qz,disp);
00199                 scan_all(ijk,x-qx,y-qy,z-qz,di,dj,dk,w,mrs);
00200 
00201                 if(qu_e>qu_l-18) add_list_memory(qu_s,qu_e);
00202                 scan_bits_mask_add(q,mijk,ei,ej,ek,qu_e);
00203         }
00204 
00205         // Do a check to see if we've reached the radius cutoff
00206         if(con.r_max_add(mrs)<radp[g]) return;
00207 
00208         // We were unable to completely compute the cell based on the blocks in
00209         // the worklist, so now we have to go block by block, reading in items
00210         // off the list
00211         while(qu_s!=qu_e) {
00212 
00213                 // Read the next entry of the queue
00214                 if(qu_s==qu_l) qu_s=qu;
00215                 ei=*(qu_s++);ej=*(qu_s++);ek=*(qu_s++);
00216                 di=ei-i;dj=ej-j;dk=ek-k;
00217                 if(compute_min_radius(di,dj,dk,fx,fy,fz,mrs)) continue;
00218 
00219                 ijk=con.region_index(ci,cj,ck,ei,ej,ek,qx,qy,qz,disp);
00220                 scan_all(ijk,x-qx,y-qy,z-qz,di,dj,dk,w,mrs);
00221 
00222                 // Test the neighbors of the current block, and add them to the
00223                 // block list if they haven't already been tested
00224                 if((qu_s<=qu_e?(qu_l-qu_e)+(qu_s-qu):qu_s-qu_e)<18) add_list_memory(qu_s,qu_e);
00225                 add_to_mask(ei,ej,ek,qu_e);
00226         }
00227 }
00228 
00229 /** Scans the six orthogonal neighbors of a given block and adds them to the
00230  * queue if they haven't been considered already. It assumes that the queue
00231  * will definitely have enough memory to add six entries at the end.
00232  * \param[in] (ei,ej,ek) the block to consider.
00233  * \param[in,out] qu_e a pointer to the end of the queue. */
00234 template<class c_class>
00235 inline void voro_compute<c_class>::add_to_mask(int ei,int ej,int ek,int *&qu_e) {
00236         unsigned int *mijk=mask+ei+hx*(ej+hy*ek);
00237         if(ek>0) if(*(mijk-hxy)!=mv) {if(qu_e==qu_l) qu_e=qu;*(mijk-hxy)=mv;*(qu_e++)=ei;*(qu_e++)=ej;*(qu_e++)=ek-1;}
00238         if(ej>0) if(*(mijk-hx)!=mv) {if(qu_e==qu_l) qu_e=qu;*(mijk-hx)=mv;*(qu_e++)=ei;*(qu_e++)=ej-1;*(qu_e++)=ek;}
00239         if(ei>0) if(*(mijk-1)!=mv) {if(qu_e==qu_l) qu_e=qu;*(mijk-1)=mv;*(qu_e++)=ei-1;*(qu_e++)=ej;*(qu_e++)=ek;}
00240         if(ei<hx-1) if(*(mijk+1)!=mv) {if(qu_e==qu_l) qu_e=qu;*(mijk+1)=mv;*(qu_e++)=ei+1;*(qu_e++)=ej;*(qu_e++)=ek;}
00241         if(ej<hy-1) if(*(mijk+hx)!=mv) {if(qu_e==qu_l) qu_e=qu;*(mijk+hx)=mv;*(qu_e++)=ei;*(qu_e++)=ej+1;*(qu_e++)=ek;}
00242         if(ek<hz-1) if(*(mijk+hxy)!=mv) {if(qu_e==qu_l) qu_e=qu;*(mijk+hxy)=mv;*(qu_e++)=ei;*(qu_e++)=ej;*(qu_e++)=ek+1;}
00243 }
00244 
00245 /** Scans a worklist entry and adds any blocks to the queue
00246  * \param[in] (ei,ej,ek) the block to consider.
00247  * \param[in,out] qu_e a pointer to the end of the queue. */
00248 template<class c_class>
00249 inline void voro_compute<c_class>::scan_bits_mask_add(unsigned int q,unsigned int *mijk,int ei,int ej,int ek,int *&qu_e) {
00250         const unsigned int b1=1<<21,b2=1<<22,b3=1<<24,b4=1<<25,b5=1<<27,b6=1<<28;
00251         if((q&b2)==b2) {
00252                 if(ei>0) {*(mijk-1)=mv;*(qu_e++)=ei-1;*(qu_e++)=ej;*(qu_e++)=ek;}
00253                 if((q&b1)==0&&ei<hx-1) {*(mijk+1)=mv;*(qu_e++)=ei+1;*(qu_e++)=ej;*(qu_e++)=ek;}
00254         } else if((q&b1)==b1&&ei<hx-1) {*(mijk+1)=mv;*(qu_e++)=ei+1;*(qu_e++)=ej;*(qu_e++)=ek;}
00255         if((q&b4)==b4) {
00256                 if(ej>0) {*(mijk-hx)=mv;*(qu_e++)=ei;*(qu_e++)=ej-1;*(qu_e++)=ek;}
00257                 if((q&b3)==0&&ej<hy-1) {*(mijk+hx)=mv;*(qu_e++)=ei;*(qu_e++)=ej+1;*(qu_e++)=ek;}
00258         } else if((q&b3)==b3&&ej<hy-1) {*(mijk+hx)=mv;*(qu_e++)=ei;*(qu_e++)=ej+1;*(qu_e++)=ek;}
00259         if((q&b6)==b6) {
00260                 if(ek>0) {*(mijk-hxy)=mv;*(qu_e++)=ei;*(qu_e++)=ej;*(qu_e++)=ek-1;}
00261                 if((q&b5)==0&&ek<hz-1) {*(mijk+hxy)=mv;*(qu_e++)=ei;*(qu_e++)=ej;*(qu_e++)=ek+1;}
00262         } else if((q&b5)==b5&&ek<hz-1) {*(mijk+hxy)=mv;*(qu_e++)=ei;*(qu_e++)=ej;*(qu_e++)=ek+1;}
00263 }
00264 
00265 /** This routine computes a Voronoi cell for a single particle in the
00266  * container. It can be called by the user, but is also forms the core part of
00267  * several of the main functions, such as store_cell_volumes(), print_all(),
00268  * and the drawing routines. The algorithm constructs the cell by testing over
00269  * the neighbors of the particle, working outwards until it reaches those
00270  * particles which could not possibly intersect the cell. For maximum
00271  * efficiency, this algorithm is divided into three parts. In the first
00272  * section, the algorithm tests over the blocks which are in the immediate
00273  * vicinity of the particle, by making use of one of the precomputed worklists.
00274  * The code then continues to test blocks on the worklist, but also begins to
00275  * construct a list of neighboring blocks outside the worklist which may need
00276  * to be test. In the third section, the routine starts testing these
00277  * neighboring blocks, evaluating whether or not a particle in them could
00278  * possibly intersect the cell. For blocks that intersect the cell, it tests
00279  * the particles in that block, and then adds the block neighbors to the list
00280  * of potential places to consider.
00281  * \param[in,out] c a reference to a voronoicell object.
00282  * \param[in] ijk the index of the block that the test particle is in.
00283  * \param[in] s the index of the particle within the test block.
00284  * \param[in] (ci,cj,ck) the coordinates of the block that the test particle is
00285  *                       in relative to the container data structure.
00286  * \return False if the Voronoi cell was completely removed during the
00287  *         computation and has zero volume, true otherwise. */
00288 template<class c_class>
00289 template<class v_cell>
00290 bool voro_compute<c_class>::compute_cell(v_cell &c,int ijk,int s,int ci,int cj,int ck) {
00291         static const int count_list[8]={7,11,15,19,26,35,45,59},*count_e=count_list+8;
00292         double x,y,z,x1,y1,z1,qx=0,qy=0,qz=0;
00293         double xlo,ylo,zlo,xhi,yhi,zhi,x2,y2,z2,rs;
00294         int i,j,k,di,dj,dk,ei,ej,ek,f,g,l,disp;
00295         double fx,fy,fz,gxs,gys,gzs,*radp;
00296         unsigned int q,*e,*mijk;
00297 
00298         if(!con.initialize_voronoicell(c,ijk,s,ci,cj,ck,i,j,k,x,y,z,disp)) return false;
00299         con.r_init(ijk,s);
00300 
00301         // Initialize the Voronoi cell to fill the entire container
00302         double crs,mrs;
00303 
00304         int next_count=3,*count_p=(const_cast<int*> (count_list));
00305 
00306         // Test all particles in the particle's local region first
00307         for(l=0;l<s;l++) {
00308                 x1=p[ijk][ps*l]-x;
00309                 y1=p[ijk][ps*l+1]-y;
00310                 z1=p[ijk][ps*l+2]-z;
00311                 rs=con.r_scale(x1*x1+y1*y1+z1*z1,ijk,l);
00312                 if(!c.nplane(x1,y1,z1,rs,id[ijk][l])) return false;
00313         }
00314         l++;
00315         while(l<co[ijk]) {
00316                 x1=p[ijk][ps*l]-x;
00317                 y1=p[ijk][ps*l+1]-y;
00318                 z1=p[ijk][ps*l+2]-z;
00319                 rs=con.r_scale(x1*x1+y1*y1+z1*z1,ijk,l);
00320                 if(!c.nplane(x1,y1,z1,rs,id[ijk][l])) return false;
00321                 l++;
00322         }
00323 
00324         // Now compute the maximum distance squared from the cell center to a
00325         // vertex. This is used to cut off the calculation since we only need
00326         // to test out to twice this range.
00327         mrs=c.max_radius_squared();
00328 
00329         // Now compute the fractional position of the particle within its
00330         // region and store it in (fx,fy,fz). We use this to compute an index
00331         // (di,dj,dk) of which subregion the particle is within.
00332         unsigned int m1,m2;
00333         con.frac_pos(x,y,z,ci,cj,ck,fx,fy,fz);
00334         di=int(fx*xsp*wl_fgrid);dj=int(fy*ysp*wl_fgrid);dk=int(fz*zsp*wl_fgrid);
00335 
00336         // The indices (di,dj,dk) tell us which worklist to use, to test the
00337         // blocks in the optimal order. But we only store worklists for the
00338         // eighth of the region where di, dj, and dk are all less than half the
00339         // full grid. The rest of the cases are handled by symmetry. In this
00340         // section, we detect for these cases, by reflecting high values of di,
00341         // dj, and dk. For these cases, a mask is constructed in m1 and m2
00342         // which is used to flip the worklist information when it is loaded.
00343         if(di>=wl_hgrid) {
00344                 gxs=fx;
00345                 m1=127+(3<<21);m2=1+(1<<21);di=wl_fgrid-1-di;if(di<0) di=0;
00346         } else {m1=m2=0;gxs=boxx-fx;}
00347         if(dj>=wl_hgrid) {
00348                 gys=fy;
00349                 m1|=(127<<7)+(3<<24);m2|=(1<<7)+(1<<24);dj=wl_fgrid-1-dj;if(dj<0) dj=0;
00350         } else gys=boxy-fy;
00351         if(dk>=wl_hgrid) {
00352                 gzs=fz;
00353                 m1|=(127<<14)+(3<<27);m2|=(1<<14)+(1<<27);dk=wl_fgrid-1-dk;if(dk<0) dk=0;
00354         } else gzs=boxz-fz;
00355         gxs*=gxs;gys*=gys;gzs*=gzs;
00356 
00357         // Now compute which worklist we are going to use, and set radp and e to
00358         // point at the right offsets
00359         ijk=di+wl_hgrid*(dj+wl_hgrid*dk);
00360         radp=mrad+ijk*wl_seq_length;
00361         e=(const_cast<unsigned int*> (wl))+ijk*wl_seq_length;
00362 
00363         // Read in how many items in the worklist can be tested without having to
00364         // worry about writing to the mask
00365         f=e[0];g=0;
00366         do {
00367 
00368                 // At the intervals specified by count_list, we recompute the
00369                 // maximum radius squared
00370                 if(g==next_count) {
00371                         mrs=c.max_radius_squared();
00372                         if(count_p!=count_e) next_count=*(count_p++);
00373                 }
00374 
00375                 // If mrs is less than the minimum distance to any untested
00376                 // block, then we are done
00377                 if(con.r_ctest(radp[g],mrs)) return true;
00378                 g++;
00379 
00380                 // Load in a block off the worklist, permute it with the
00381                 // symmetry mask, and decode its position. These are all
00382                 // integer bit operations so they should run very fast.
00383                 q=e[g];q^=m1;q+=m2;
00384                 di=q&127;di-=64;
00385                 dj=(q>>7)&127;dj-=64;
00386                 dk=(q>>14)&127;dk-=64;
00387 
00388                 // Check that the worklist position is in range
00389                 ei=di+i;if(ei<0||ei>=hx) continue;
00390                 ej=dj+j;if(ej<0||ej>=hy) continue;
00391                 ek=dk+k;if(ek<0||ek>=hz) continue;
00392 
00393                 // Call the compute_min_max_radius() function. This returns
00394                 // true if the minimum distance to the block is bigger than the
00395                 // current mrs, in which case we skip this block and move on.
00396                 // Otherwise, it computes the maximum distance to the block and
00397                 // returns it in crs.
00398                 if(compute_min_max_radius(di,dj,dk,fx,fy,fz,gxs,gys,gzs,crs,mrs)) continue;
00399 
00400                 // Now compute which region we are going to loop over, adding a
00401                 // displacement for the periodic cases
00402                 ijk=con.region_index(ci,cj,ck,ei,ej,ek,qx,qy,qz,disp);
00403 
00404                 // If mrs is bigger than the maximum distance to the block,
00405                 // then we have to test all particles in the block for
00406                 // intersections. Otherwise, we do additional checks and skip
00407                 // those particles which can't possibly intersect the block.
00408                 if(co[ijk]>0) {
00409                         l=0;x2=x-qx;y2=y-qy;z2=z-qz;
00410                         if(!con.r_ctest(crs,mrs)) {
00411                                 do {
00412                                         x1=p[ijk][ps*l]-x2;
00413                                         y1=p[ijk][ps*l+1]-y2;
00414                                         z1=p[ijk][ps*l+2]-z2;
00415                                         rs=con.r_scale(x1*x1+y1*y1+z1*z1,ijk,l);
00416                                         if(!c.nplane(x1,y1,z1,rs,id[ijk][l])) return false;
00417                                         l++;
00418                                 } while (l<co[ijk]);
00419                         } else {
00420                                 do {
00421                                         x1=p[ijk][ps*l]-x2;
00422                                         y1=p[ijk][ps*l+1]-y2;
00423                                         z1=p[ijk][ps*l+2]-z2;
00424                                         rs=x1*x1+y1*y1+z1*z1;
00425                                         if(con.r_scale_check(rs,mrs,ijk,l)&&!c.nplane(x1,y1,z1,rs,id[ijk][l])) return false;
00426                                         l++;
00427                                 } while (l<co[ijk]);
00428                         }
00429                 }
00430         } while(g<f);
00431 
00432         // If we reach here, we were unable to compute the entire cell using
00433         // the first part of the worklist. This section of the algorithm
00434         // continues the worklist, but it now starts preparing the mask that we
00435         // need if we end up going block by block. We do the same as before,
00436         // but we put a mark down on the mask for every block that's tested.
00437         // The worklist also contains information about which neighbors of each
00438         // block are not also on the worklist, and we start storing those
00439         // points in a list in case we have to go block by block. Update the
00440         // mask counter, and if it wraps around then reset the whole mask; that
00441         // will only happen once every 2^32 tries.
00442         mv++;
00443         if(mv==0) {reset_mask();mv=1;}
00444 
00445         // Set the queue pointers
00446         int *qu_s=qu,*qu_e=qu;
00447 
00448         while(g<wl_seq_length-1) {
00449 
00450                 // At the intervals specified by count_list, we recompute the
00451                 // maximum radius squared
00452                 if(g==next_count) {
00453                         mrs=c.max_radius_squared();
00454                         if(count_p!=count_e) next_count=*(count_p++);
00455                 }
00456 
00457                 // If mrs is less than the minimum distance to any untested
00458                 // block, then we are done
00459                 if(con.r_ctest(radp[g],mrs)) return true;
00460                 g++;
00461 
00462                 // Load in a block off the worklist, permute it with the
00463                 // symmetry mask, and decode its position. These are all
00464                 // integer bit operations so they should run very fast.
00465                 q=e[g];q^=m1;q+=m2;
00466                 di=q&127;di-=64;
00467                 dj=(q>>7)&127;dj-=64;
00468                 dk=(q>>14)&127;dk-=64;
00469 
00470                 // Compute the position in the mask of the current block. If
00471                 // this lies outside the mask, then skip it. Otherwise, mark
00472                 // it.
00473                 ei=di+i;if(ei<0||ei>=hx) continue;
00474                 ej=dj+j;if(ej<0||ej>=hy) continue;
00475                 ek=dk+k;if(ek<0||ek>=hz) continue;
00476                 mijk=mask+ei+hx*(ej+hy*ek);
00477                 *mijk=mv;
00478 
00479                 // Call the compute_min_max_radius() function. This returns
00480                 // true if the minimum distance to the block is bigger than the
00481                 // current mrs, in which case we skip this block and move on.
00482                 // Otherwise, it computes the maximum distance to the block and
00483                 // returns it in crs.
00484                 if(compute_min_max_radius(di,dj,dk,fx,fy,fz,gxs,gys,gzs,crs,mrs)) continue;
00485 
00486                 // Now compute which region we are going to loop over, adding a
00487                 // displacement for the periodic cases
00488                 ijk=con.region_index(ci,cj,ck,ei,ej,ek,qx,qy,qz,disp);
00489 
00490                 // If mrs is bigger than the maximum distance to the block,
00491                 // then we have to test all particles in the block for
00492                 // intersections. Otherwise, we do additional checks and skip
00493                 // those particles which can't possibly intersect the block.
00494                 if(co[ijk]>0) {
00495                         l=0;x2=x-qx;y2=y-qy;z2=z-qz;
00496                         if(!con.r_ctest(crs,mrs)) {
00497                                 do {
00498                                         x1=p[ijk][ps*l]-x2;
00499                                         y1=p[ijk][ps*l+1]-y2;
00500                                         z1=p[ijk][ps*l+2]-z2;
00501                                         rs=con.r_scale(x1*x1+y1*y1+z1*z1,ijk,l);
00502                                         if(!c.nplane(x1,y1,z1,rs,id[ijk][l])) return false;
00503                                         l++;
00504                                 } while (l<co[ijk]);
00505                         } else {
00506                                 do {
00507                                         x1=p[ijk][ps*l]-x2;
00508                                         y1=p[ijk][ps*l+1]-y2;
00509                                         z1=p[ijk][ps*l+2]-z2;
00510                                         rs=x1*x1+y1*y1+z1*z1;
00511                                         if(con.r_scale_check(rs,mrs,ijk,l)&&!c.nplane(x1,y1,z1,rs,id[ijk][l])) return false;
00512                                         l++;
00513                                 } while (l<co[ijk]);
00514                         }
00515                 }
00516 
00517                 // If there might not be enough memory on the list for these
00518                 // additions, then add more
00519                 if(qu_e>qu_l-18) add_list_memory(qu_s,qu_e);
00520 
00521                 // Test the parts of the worklist element which tell us what
00522                 // neighbors of this block are not on the worklist. Store them
00523                 // on the block list, and mark the mask.
00524                 scan_bits_mask_add(q,mijk,ei,ej,ek,qu_e);
00525         }
00526 
00527         // Do a check to see if we've reached the radius cutoff
00528         if(con.r_ctest(radp[g],mrs)) return true;
00529 
00530         // We were unable to completely compute the cell based on the blocks in
00531         // the worklist, so now we have to go block by block, reading in items
00532         // off the list
00533         while(qu_s!=qu_e) {
00534 
00535                 // If we reached the end of the list memory loop back to the
00536                 // start
00537                 if(qu_s==qu_l) qu_s=qu;
00538 
00539                 // Read in a block off the list, and compute the upper and lower
00540                 // coordinates in each of the three dimensions
00541                 ei=*(qu_s++);ej=*(qu_s++);ek=*(qu_s++);
00542                 xlo=(ei-i)*boxx-fx;xhi=xlo+boxx;
00543                 ylo=(ej-j)*boxy-fy;yhi=ylo+boxy;
00544                 zlo=(ek-k)*boxz-fz;zhi=zlo+boxz;
00545 
00546                 // Carry out plane tests to see if any particle in this block
00547                 // could possibly intersect the cell
00548                 if(ei>i) {
00549                         if(ej>j) {
00550                                 if(ek>k) {if(corner_test(c,xlo,ylo,zlo,xhi,yhi,zhi)) continue;}
00551                                 else if(ek<k) {if(corner_test(c,xlo,ylo,zhi,xhi,yhi,zlo)) continue;}
00552                                 else {if(edge_z_test(c,xlo,ylo,zlo,xhi,yhi,zhi)) continue;}
00553                         } else if(ej<j) {
00554                                 if(ek>k) {if(corner_test(c,xlo,yhi,zlo,xhi,ylo,zhi)) continue;}
00555                                 else if(ek<k) {if(corner_test(c,xlo,yhi,zhi,xhi,ylo,zlo)) continue;}
00556                                 else {if(edge_z_test(c,xlo,yhi,zlo,xhi,ylo,zhi)) continue;}
00557                         } else {
00558                                 if(ek>k) {if(edge_y_test(c,xlo,ylo,zlo,xhi,yhi,zhi)) continue;}
00559                                 else if(ek<k) {if(edge_y_test(c,xlo,ylo,zhi,xhi,yhi,zlo)) continue;}
00560                                 else {if(face_x_test(c,xlo,ylo,zlo,yhi,zhi)) continue;}
00561                         }
00562                 } else if(ei<i) {
00563                         if(ej>j) {
00564                                 if(ek>k) {if(corner_test(c,xhi,ylo,zlo,xlo,yhi,zhi)) continue;}
00565                                 else if(ek<k) {if(corner_test(c,xhi,ylo,zhi,xlo,yhi,zlo)) continue;}
00566                                 else {if(edge_z_test(c,xhi,ylo,zlo,xlo,yhi,zhi)) continue;}
00567                         } else if(ej<j) {
00568                                 if(ek>k) {if(corner_test(c,xhi,yhi,zlo,xlo,ylo,zhi)) continue;}
00569                                 else if(ek<k) {if(corner_test(c,xhi,yhi,zhi,xlo,ylo,zlo)) continue;}
00570                                 else {if(edge_z_test(c,xhi,yhi,zlo,xlo,ylo,zhi)) continue;}
00571                         } else {
00572                                 if(ek>k) {if(edge_y_test(c,xhi,ylo,zlo,xlo,yhi,zhi)) continue;}
00573                                 else if(ek<k) {if(edge_y_test(c,xhi,ylo,zhi,xlo,yhi,zlo)) continue;}
00574                                 else {if(face_x_test(c,xhi,ylo,zlo,yhi,zhi)) continue;}
00575                         }
00576                 } else {
00577                         if(ej>j) {
00578                                 if(ek>k) {if(edge_x_test(c,xlo,ylo,zlo,xhi,yhi,zhi)) continue;}
00579                                 else if(ek<k) {if(edge_x_test(c,xlo,ylo,zhi,xhi,yhi,zlo)) continue;}
00580                                 else {if(face_y_test(c,xlo,ylo,zlo,xhi,zhi)) continue;}
00581                         } else if(ej<j) {
00582                                 if(ek>k) {if(edge_x_test(c,xlo,yhi,zlo,xhi,ylo,zhi)) continue;}
00583                                 else if(ek<k) {if(edge_x_test(c,xlo,yhi,zhi,xhi,ylo,zlo)) continue;}
00584                                 else {if(face_y_test(c,xlo,yhi,zlo,xhi,zhi)) continue;}
00585                         } else {
00586                                 if(ek>k) {if(face_z_test(c,xlo,ylo,zlo,xhi,yhi)) continue;}
00587                                 else if(ek<k) {if(face_z_test(c,xlo,ylo,zhi,xhi,yhi)) continue;}
00588                                 else voro_fatal_error("Compute cell routine revisiting central block, which should never\nhappen.",VOROPP_INTERNAL_ERROR);
00589                         }
00590                 }
00591 
00592                 // Now compute the region that we are going to test over, and
00593                 // set a displacement vector for the periodic cases
00594                 ijk=con.region_index(ci,cj,ck,ei,ej,ek,qx,qy,qz,disp);
00595 
00596                 // Loop over all the elements in the block to test for cuts. It
00597                 // would be possible to exclude some of these cases by testing
00598                 // against mrs, but this will probably not save time.
00599                 if(co[ijk]>0) {
00600                         l=0;x2=x-qx;y2=y-qy;z2=z-qz;
00601                         do {
00602                                 x1=p[ijk][ps*l]-x2;
00603                                 y1=p[ijk][ps*l+1]-y2;
00604                                 z1=p[ijk][ps*l+2]-z2;
00605                                 rs=con.r_scale(x1*x1+y1*y1+z1*z1,ijk,l);
00606                                 if(!c.nplane(x1,y1,z1,rs,id[ijk][l])) return false;
00607                                 l++;
00608                         } while (l<co[ijk]);
00609                 }
00610 
00611                 // If there's not much memory on the block list then add more
00612                 if((qu_s<=qu_e?(qu_l-qu_e)+(qu_s-qu):qu_s-qu_e)<18) add_list_memory(qu_s,qu_e);
00613 
00614                 // Test the neighbors of the current block, and add them to the
00615                 // block list if they haven't already been tested
00616                 add_to_mask(ei,ej,ek,qu_e);
00617         }
00618 
00619         return true;
00620 }
00621 
00622 /** This function checks to see whether a particular block can possibly have
00623  * any intersection with a Voronoi cell, for the case when the closest point
00624  * from the cell center to the block is at a corner.
00625  * \param[in,out] c a reference to a Voronoi cell.
00626  * \param[in] (xl,yl,zl) the relative coordinates of the corner of the block
00627  *                       closest to the cell center.
00628  * \param[in] (xh,yh,zh) the relative coordinates of the corner of the block
00629  *                       furthest away from the cell center.
00630  * \return False if the block may intersect, true if does not. */
00631 template<class c_class>
00632 template<class v_cell>
00633 bool voro_compute<c_class>::corner_test(v_cell &c,double xl,double yl,double zl,double xh,double yh,double zh) {
00634         con.r_prime(xl*xl+yl*yl+zl*zl);
00635         if(c.plane_intersects_guess(xh,yl,zl,con.r_cutoff(xl*xh+yl*yl+zl*zl))) return false;
00636         if(c.plane_intersects(xh,yh,zl,con.r_cutoff(xl*xh+yl*yh+zl*zl))) return false;
00637         if(c.plane_intersects(xl,yh,zl,con.r_cutoff(xl*xl+yl*yh+zl*zl))) return false;
00638         if(c.plane_intersects(xl,yh,zh,con.r_cutoff(xl*xl+yl*yh+zl*zh))) return false;
00639         if(c.plane_intersects(xl,yl,zh,con.r_cutoff(xl*xl+yl*yl+zl*zh))) return false;
00640         if(c.plane_intersects(xh,yl,zh,con.r_cutoff(xl*xh+yl*yl+zl*zh))) return false;
00641         return true;
00642 }
00643 
00644 /** This function checks to see whether a particular block can possibly have
00645  * any intersection with a Voronoi cell, for the case when the closest point
00646  * from the cell center to the block is on an edge which points along the x
00647  * direction.
00648  * \param[in,out] c a reference to a Voronoi cell.
00649  * \param[in] (x0,x1) the minimum and maximum relative x coordinates of the
00650  *                    block.
00651  * \param[in] (yl,zl) the relative y and z coordinates of the corner of the
00652  *                    block closest to the cell center.
00653  * \param[in] (yh,zh) the relative y and z coordinates of the corner of the
00654  *                    block furthest away from the cell center.
00655  * \return False if the block may intersect, true if does not. */
00656 template<class c_class>
00657 template<class v_cell>
00658 inline bool voro_compute<c_class>::edge_x_test(v_cell &c,double x0,double yl,double zl,double x1,double yh,double zh) {
00659         con.r_prime(yl*yl+zl*zl);
00660         if(c.plane_intersects_guess(x0,yl,zh,con.r_cutoff(yl*yl+zl*zh))) return false;
00661         if(c.plane_intersects(x1,yl,zh,con.r_cutoff(yl*yl+zl*zh))) return false;
00662         if(c.plane_intersects(x1,yl,zl,con.r_cutoff(yl*yl+zl*zl))) return false;
00663         if(c.plane_intersects(x0,yl,zl,con.r_cutoff(yl*yl+zl*zl))) return false;
00664         if(c.plane_intersects(x0,yh,zl,con.r_cutoff(yl*yh+zl*zl))) return false;
00665         if(c.plane_intersects(x1,yh,zl,con.r_cutoff(yl*yh+zl*zl))) return false;
00666         return true;
00667 }
00668 
00669 /** This function checks to see whether a particular block can possibly have
00670  * any intersection with a Voronoi cell, for the case when the closest point
00671  * from the cell center to the block is on an edge which points along the y
00672  * direction.
00673  * \param[in,out] c a reference to a Voronoi cell.
00674  * \param[in] (y0,y1) the minimum and maximum relative y coordinates of the
00675  *                    block.
00676  * \param[in] (xl,zl) the relative x and z coordinates of the corner of the
00677  *                    block closest to the cell center.
00678  * \param[in] (xh,zh) the relative x and z coordinates of the corner of the
00679  *                    block furthest away from the cell center.
00680  * \return False if the block may intersect, true if does not. */
00681 template<class c_class>
00682 template<class v_cell>
00683 inline bool voro_compute<c_class>::edge_y_test(v_cell &c,double xl,double y0,double zl,double xh,double y1,double zh) {
00684         con.r_prime(xl*xl+zl*zl);
00685         if(c.plane_intersects_guess(xl,y0,zh,con.r_cutoff(xl*xl+zl*zh))) return false;
00686         if(c.plane_intersects(xl,y1,zh,con.r_cutoff(xl*xl+zl*zh))) return false;
00687         if(c.plane_intersects(xl,y1,zl,con.r_cutoff(xl*xl+zl*zl))) return false;
00688         if(c.plane_intersects(xl,y0,zl,con.r_cutoff(xl*xl+zl*zl))) return false;
00689         if(c.plane_intersects(xh,y0,zl,con.r_cutoff(xl*xh+zl*zl))) return false;
00690         if(c.plane_intersects(xh,y1,zl,con.r_cutoff(xl*xh+zl*zl))) return false;
00691         return true;
00692 }
00693 
00694 /** This function checks to see whether a particular block can possibly have
00695  * any intersection with a Voronoi cell, for the case when the closest point
00696  * from the cell center to the block is on an edge which points along the z
00697  * direction.
00698  * \param[in,out] c a reference to a Voronoi cell.
00699  * \param[in] (z0,z1) the minimum and maximum relative z coordinates of the block.
00700  * \param[in] (xl,yl) the relative x and y coordinates of the corner of the
00701  *                    block closest to the cell center.
00702  * \param[in] (xh,yh) the relative x and y coordinates of the corner of the
00703  *                    block furthest away from the cell center.
00704  * \return False if the block may intersect, true if does not. */
00705 template<class c_class>
00706 template<class v_cell>
00707 inline bool voro_compute<c_class>::edge_z_test(v_cell &c,double xl,double yl,double z0,double xh,double yh,double z1) {
00708         con.r_prime(xl*xl+yl*yl);
00709         if(c.plane_intersects_guess(xl,yh,z0,con.r_cutoff(xl*xl+yl*yh))) return false;
00710         if(c.plane_intersects(xl,yh,z1,con.r_cutoff(xl*xl+yl*yh))) return false;
00711         if(c.plane_intersects(xl,yl,z1,con.r_cutoff(xl*xl+yl*yl))) return false;
00712         if(c.plane_intersects(xl,yl,z0,con.r_cutoff(xl*xl+yl*yl))) return false;
00713         if(c.plane_intersects(xh,yl,z0,con.r_cutoff(xl*xh+yl*yl))) return false;
00714         if(c.plane_intersects(xh,yl,z1,con.r_cutoff(xl*xh+yl*yl))) return false;
00715         return true;
00716 }
00717 
00718 /** This function checks to see whether a particular block can possibly have
00719  * any intersection with a Voronoi cell, for the case when the closest point
00720  * from the cell center to the block is on a face aligned with the x direction.
00721  * \param[in,out] c a reference to a Voronoi cell.
00722  * \param[in] xl the minimum distance from the cell center to the face.
00723  * \param[in] (y0,y1) the minimum and maximum relative y coordinates of the
00724  *                    block.
00725  * \param[in] (z0,z1) the minimum and maximum relative z coordinates of the
00726  *                    block.
00727  * \return False if the block may intersect, true if does not. */
00728 template<class c_class>
00729 template<class v_cell>
00730 inline bool voro_compute<c_class>::face_x_test(v_cell &c,double xl,double y0,double z0,double y1,double z1) {
00731         con.r_prime(xl*xl);
00732         if(c.plane_intersects_guess(xl,y0,z0,con.r_cutoff(xl*xl))) return false;
00733         if(c.plane_intersects(xl,y0,z1,con.r_cutoff(xl*xl))) return false;
00734         if(c.plane_intersects(xl,y1,z1,con.r_cutoff(xl*xl))) return false;
00735         if(c.plane_intersects(xl,y1,z0,con.r_cutoff(xl*xl))) return false;
00736         return true;
00737 }
00738 
00739 /** This function checks to see whether a particular block can possibly have
00740  * any intersection with a Voronoi cell, for the case when the closest point
00741  * from the cell center to the block is on a face aligned with the y direction.
00742  * \param[in,out] c a reference to a Voronoi cell.
00743  * \param[in] yl the minimum distance from the cell center to the face.
00744  * \param[in] (x0,x1) the minimum and maximum relative x coordinates of the
00745  *                    block.
00746  * \param[in] (z0,z1) the minimum and maximum relative z coordinates of the
00747  *                    block.
00748  * \return False if the block may intersect, true if does not. */
00749 template<class c_class>
00750 template<class v_cell>
00751 inline bool voro_compute<c_class>::face_y_test(v_cell &c,double x0,double yl,double z0,double x1,double z1) {
00752         con.r_prime(yl*yl);
00753         if(c.plane_intersects_guess(x0,yl,z0,con.r_cutoff(yl*yl))) return false;
00754         if(c.plane_intersects(x0,yl,z1,con.r_cutoff(yl*yl))) return false;
00755         if(c.plane_intersects(x1,yl,z1,con.r_cutoff(yl*yl))) return false;
00756         if(c.plane_intersects(x1,yl,z0,con.r_cutoff(yl*yl))) return false;
00757         return true;
00758 }
00759 
00760 /** This function checks to see whether a particular block can possibly have
00761  * any intersection with a Voronoi cell, for the case when the closest point
00762  * from the cell center to the block is on a face aligned with the z direction.
00763  * \param[in,out] c a reference to a Voronoi cell.
00764  * \param[in] zl the minimum distance from the cell center to the face.
00765  * \param[in] (x0,x1) the minimum and maximum relative x coordinates of the
00766  *                    block.
00767  * \param[in] (y0,y1) the minimum and maximum relative y coordinates of the
00768  *                    block.
00769  * \return False if the block may intersect, true if does not. */
00770 template<class c_class>
00771 template<class v_cell>
00772 inline bool voro_compute<c_class>::face_z_test(v_cell &c,double x0,double y0,double zl,double x1,double y1) {
00773         con.r_prime(zl*zl);
00774         if(c.plane_intersects_guess(x0,y0,zl,con.r_cutoff(zl*zl))) return false;
00775         if(c.plane_intersects(x0,y1,zl,con.r_cutoff(zl*zl))) return false;
00776         if(c.plane_intersects(x1,y1,zl,con.r_cutoff(zl*zl))) return false;
00777         if(c.plane_intersects(x1,y0,zl,con.r_cutoff(zl*zl))) return false;
00778         return true;
00779 }
00780 
00781 
00782 /** This routine checks to see whether a point is within a particular distance
00783  * of a nearby region. If the point is within the distance of the region, then
00784  * the routine returns true, and computes the maximum distance from the point
00785  * to the region. Otherwise, the routine returns false.
00786  * \param[in] (di,dj,dk) the position of the nearby region to be tested,
00787  *                       relative to the region that the point is in.
00788  * \param[in] (fx,fy,fz) the displacement of the point within its region.
00789  * \param[in] (gxs,gys,gzs) the maximum squared distances from the point to the
00790  *                          sides of its region.
00791  * \param[out] crs a reference in which to return the maximum distance to the
00792  *                 region (only computed if the routine returns false).
00793  * \param[in] mrs the distance to be tested.
00794  * \return True if the region is further away than mrs, false if the region in
00795  *         within mrs. */
00796 template<class c_class>
00797 bool voro_compute<c_class>::compute_min_max_radius(int di,int dj,int dk,double fx,double fy,double fz,double gxs,double gys,double gzs,double &crs,double mrs) {
00798         double xlo,ylo,zlo;
00799         if(di>0) {
00800                 xlo=di*boxx-fx;
00801                 crs=xlo*xlo;
00802                 if(dj>0) {
00803                         ylo=dj*boxy-fy;
00804                         crs+=ylo*ylo;
00805                         if(dk>0) {
00806                                 zlo=dk*boxz-fz;
00807                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00808                                 crs+=bxsq+2*(boxx*xlo+boxy*ylo+boxz*zlo);
00809                         } else if(dk<0) {
00810                                 zlo=(dk+1)*boxz-fz;
00811                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00812                                 crs+=bxsq+2*(boxx*xlo+boxy*ylo-boxz*zlo);
00813                         } else {
00814                                 if(con.r_ctest(crs,mrs)) return true;
00815                                 crs+=boxx*(2*xlo+boxx)+boxy*(2*ylo+boxy)+gzs;
00816                         }
00817                 } else if(dj<0) {
00818                         ylo=(dj+1)*boxy-fy;
00819                         crs+=ylo*ylo;
00820                         if(dk>0) {
00821                                 zlo=dk*boxz-fz;
00822                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00823                                 crs+=bxsq+2*(boxx*xlo-boxy*ylo+boxz*zlo);
00824                         } else if(dk<0) {
00825                                 zlo=(dk+1)*boxz-fz;
00826                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00827                                 crs+=bxsq+2*(boxx*xlo-boxy*ylo-boxz*zlo);
00828                         } else {
00829                                 if(con.r_ctest(crs,mrs)) return true;
00830                                 crs+=boxx*(2*xlo+boxx)+boxy*(-2*ylo+boxy)+gzs;
00831                         }
00832                 } else {
00833                         if(dk>0) {
00834                                 zlo=dk*boxz-fz;
00835                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00836                                 crs+=boxz*(2*zlo+boxz);
00837                         } else if(dk<0) {
00838                                 zlo=(dk+1)*boxz-fz;
00839                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00840                                 crs+=boxz*(-2*zlo+boxz);
00841                         } else {
00842                                 if(con.r_ctest(crs,mrs)) return true;
00843                                 crs+=gzs;
00844                         }
00845                         crs+=gys+boxx*(2*xlo+boxx);
00846                 }
00847         } else if(di<0) {
00848                 xlo=(di+1)*boxx-fx;
00849                 crs=xlo*xlo;
00850                 if(dj>0) {
00851                         ylo=dj*boxy-fy;
00852                         crs+=ylo*ylo;
00853                         if(dk>0) {
00854                                 zlo=dk*boxz-fz;
00855                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00856                                 crs+=bxsq+2*(-boxx*xlo+boxy*ylo+boxz*zlo);
00857                         } else if(dk<0) {
00858                                 zlo=(dk+1)*boxz-fz;
00859                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00860                                 crs+=bxsq+2*(-boxx*xlo+boxy*ylo-boxz*zlo);
00861                         } else {
00862                                 if(con.r_ctest(crs,mrs)) return true;
00863                                 crs+=boxx*(-2*xlo+boxx)+boxy*(2*ylo+boxy)+gzs;
00864                         }
00865                 } else if(dj<0) {
00866                         ylo=(dj+1)*boxy-fy;
00867                         crs+=ylo*ylo;
00868                         if(dk>0) {
00869                                 zlo=dk*boxz-fz;
00870                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00871                                 crs+=bxsq+2*(-boxx*xlo-boxy*ylo+boxz*zlo);
00872                         } else if(dk<0) {
00873                                 zlo=(dk+1)*boxz-fz;
00874                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00875                                 crs+=bxsq+2*(-boxx*xlo-boxy*ylo-boxz*zlo);
00876                         } else {
00877                                 if(con.r_ctest(crs,mrs)) return true;
00878                                 crs+=boxx*(-2*xlo+boxx)+boxy*(-2*ylo+boxy)+gzs;
00879                         }
00880                 } else {
00881                         if(dk>0) {
00882                                 zlo=dk*boxz-fz;
00883                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00884                                 crs+=boxz*(2*zlo+boxz);
00885                         } else if(dk<0) {
00886                                 zlo=(dk+1)*boxz-fz;
00887                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00888                                 crs+=boxz*(-2*zlo+boxz);
00889                         } else {
00890                                 if(con.r_ctest(crs,mrs)) return true;
00891                                 crs+=gzs;
00892                         }
00893                         crs+=gys+boxx*(-2*xlo+boxx);
00894                 }
00895         } else {
00896                 if(dj>0) {
00897                         ylo=dj*boxy-fy;
00898                         crs=ylo*ylo;
00899                         if(dk>0) {
00900                                 zlo=dk*boxz-fz;
00901                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00902                                 crs+=boxz*(2*zlo+boxz);
00903                         } else if(dk<0) {
00904                                 zlo=(dk+1)*boxz-fz;
00905                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00906                                 crs+=boxz*(-2*zlo+boxz);
00907                         } else {
00908                                 if(con.r_ctest(crs,mrs)) return true;
00909                                 crs+=gzs;
00910                         }
00911                         crs+=boxy*(2*ylo+boxy);
00912                 } else if(dj<0) {
00913                         ylo=(dj+1)*boxy-fy;
00914                         crs=ylo*ylo;
00915                         if(dk>0) {
00916                                 zlo=dk*boxz-fz;
00917                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00918                                 crs+=boxz*(2*zlo+boxz);
00919                         } else if(dk<0) {
00920                                 zlo=(dk+1)*boxz-fz;
00921                                 crs+=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00922                                 crs+=boxz*(-2*zlo+boxz);
00923                         } else {
00924                                 if(con.r_ctest(crs,mrs)) return true;
00925                                 crs+=gzs;
00926                         }
00927                         crs+=boxy*(-2*ylo+boxy);
00928                 } else {
00929                         if(dk>0) {
00930                                 zlo=dk*boxz-fz;crs=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00931                                 crs+=boxz*(2*zlo+boxz);
00932                         } else if(dk<0) {
00933                                 zlo=(dk+1)*boxz-fz;crs=zlo*zlo;if(con.r_ctest(crs,mrs)) return true;
00934                                 crs+=boxz*(-2*zlo+boxz);
00935                         } else {
00936                                 crs=0;
00937                                 voro_fatal_error("Min/max radius function called for central block, which should never\nhappen.",VOROPP_INTERNAL_ERROR);
00938                         }
00939                         crs+=gys;
00940                 }
00941                 crs+=gxs;
00942         }
00943         return false;
00944 }
00945 
00946 template<class c_class>
00947 bool voro_compute<c_class>::compute_min_radius(int di,int dj,int dk,double fx,double fy,double fz,double mrs) {
00948         double t,crs;
00949 
00950         if(di>0) {t=di*boxx-fx;crs=t*t;}
00951         else if(di<0) {t=(di+1)*boxx-fx;crs=t*t;}
00952         else crs=0;
00953 
00954         if(dj>0) {t=dj*boxy-fy;crs+=t*t;}
00955         else if(dj<0) {t=(dj+1)*boxy-fy;crs+=t*t;}
00956 
00957         if(dk>0) {t=dk*boxz-fz;crs+=t*t;}
00958         else if(dk<0) {t=(dk+1)*boxz-fz;crs+=t*t;}
00959 
00960         return crs>con.r_max_add(mrs);
00961 }
00962 
00963 /** Adds memory to the queue.
00964  * \param[in,out] qu_s a reference to the queue start pointer.
00965  * \param[in,out] qu_e a reference to the queue end pointer. */
00966 template<class c_class>
00967 inline void voro_compute<c_class>::add_list_memory(int*& qu_s,int*& qu_e) {
00968         qu_size<<=1;
00969         int *qu_n=new int[qu_size],*qu_c=qu_n;
00970 #if VOROPP_VERBOSE >=2
00971         fprintf(stderr,"List memory scaled up to %d\n",qu_size);
00972 #endif
00973         if(qu_s<=qu_e) {
00974                 while(qu_s<qu_e) *(qu_c++)=*(qu_s++);
00975         } else {
00976                 while(qu_s<qu_l) *(qu_c++)=*(qu_s++);qu_s=qu;
00977                 while(qu_s<qu_e) *(qu_c++)=*(qu_s++);
00978         }
00979         delete [] qu;
00980         qu_s=qu=qu_n;
00981         qu_l=qu+qu_size;
00982         qu_e=qu_c;
00983 }
00984 
00985 // Explicit template instantiation
00986 template voro_compute<container>::voro_compute(container&,int,int,int);
00987 template voro_compute<container_poly>::voro_compute(container_poly&,int,int,int);
00988 template bool voro_compute<container>::compute_cell(voronoicell&,int,int,int,int,int);
00989 template bool voro_compute<container>::compute_cell(voronoicell_neighbor&,int,int,int,int,int);
00990 template void voro_compute<container>::find_voronoi_cell(double,double,double,int,int,int,int,particle_record&,double&);
00991 template bool voro_compute<container_poly>::compute_cell(voronoicell&,int,int,int,int,int);
00992 template bool voro_compute<container_poly>::compute_cell(voronoicell_neighbor&,int,int,int,int,int);
00993 template void voro_compute<container_poly>::find_voronoi_cell(double,double,double,int,int,int,int,particle_record&,double&);
00994 
00995 // Explicit template instantiation
00996 template voro_compute<container_periodic>::voro_compute(container_periodic&,int,int,int);
00997 template voro_compute<container_periodic_poly>::voro_compute(container_periodic_poly&,int,int,int);
00998 template bool voro_compute<container_periodic>::compute_cell(voronoicell&,int,int,int,int,int);
00999 template bool voro_compute<container_periodic>::compute_cell(voronoicell_neighbor&,int,int,int,int,int);
01000 template void voro_compute<container_periodic>::find_voronoi_cell(double,double,double,int,int,int,int,particle_record&,double&);
01001 template bool voro_compute<container_periodic_poly>::compute_cell(voronoicell&,int,int,int,int,int);
01002 template bool voro_compute<container_periodic_poly>::compute_cell(voronoicell_neighbor&,int,int,int,int,int);
01003 template void voro_compute<container_periodic_poly>::find_voronoi_cell(double,double,double,int,int,int,int,particle_record&,double&);
01004 
01005 }