Voro++
common.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 common.cc
00008  * \brief Implementations of the small helper functions. */
00009 
00010 #include "common.hh"
00011 
00012 namespace voro {
00013 
00014 /** \brief Prints a vector of integers.
00015  *
00016  * Prints a vector of integers.
00017  * \param[in] v the vector to print.
00018  * \param[in] fp the file stream to print to. */
00019 void voro_print_vector(vector<int> &v,FILE *fp) {
00020         int k=0,s=v.size();
00021         while(k+4<s) {
00022                 fprintf(fp,"%d %d %d %d ",v[k],v[k+1],v[k+2],v[k+3]);
00023                 k+=4;
00024         }
00025         if(k+3<=s) {
00026                 if(k+4==s) fprintf(fp,"%d %d %d %d",v[k],v[k+1],v[k+2],v[k+3]);
00027                 else fprintf(fp,"%d %d %d",v[k],v[k+1],v[k+2]);
00028         } else {
00029                 if(k+2==s) fprintf(fp,"%d %d",v[k],v[k+1]);
00030                 else fprintf(fp,"%d",v[k]);
00031         }
00032 }
00033 
00034 /** \brief Prints a vector of doubles.
00035  *
00036  * Prints a vector of doubles.
00037  * \param[in] v the vector to print.
00038  * \param[in] fp the file stream to print to. */
00039 void voro_print_vector(vector<double> &v,FILE *fp) {
00040         int k=0,s=v.size();
00041         while(k+4<s) {
00042                 fprintf(fp,"%g %g %g %g ",v[k],v[k+1],v[k+2],v[k+3]);
00043                 k+=4;
00044         }
00045         if(k+3<=s) {
00046                 if(k+4==s) fprintf(fp,"%g %g %g %g",v[k],v[k+1],v[k+2],v[k+3]);
00047                 else fprintf(fp,"%g %g %g",v[k],v[k+1],v[k+2]);
00048         } else {
00049                 if(k+2==s) fprintf(fp,"%g %g",v[k],v[k+1]);
00050                 else fprintf(fp,"%g",v[k]);
00051         }
00052 }
00053 
00054 /** \brief Prints a vector a face vertex information.
00055  *
00056  * Prints a vector of face vertex information. A value is read, which
00057  * corresponds to the number of vertices in the next face. The routine reads
00058  * this number of values and prints them as a bracked list. This is repeated
00059  * until the end of the vector is reached.
00060  * \param[in] v the vector to interpret and print.
00061  * \param[in] fp the file stream to print to. */
00062 void voro_print_face_vertices(vector<int> &v,FILE *fp) {
00063         int j,k=0,l;
00064         if(v.size()>0) {
00065                 l=v[k++];
00066                 if(l<=1) {
00067                         if(l==1) fprintf(fp,"(%d)",v[k++]);
00068                         else fputs("()",fp);
00069                 } else {
00070                         j=k+l;
00071                         fprintf(fp,"(%d",v[k++]);
00072                         while(k<j) fprintf(fp,",%d",v[k++]);
00073                         fputs(")",fp);
00074                 }
00075                 while((unsigned int) k<v.size()) {
00076                         l=v[k++];
00077                         if(l<=1) {
00078                                 if(l==1) fprintf(fp," (%d)",v[k++]);
00079                                 else fputs(" ()",fp);
00080                         } else {
00081                                 j=k+l;
00082                                 fprintf(fp," (%d",v[k++]);
00083                                 while(k<j) fprintf(fp,",%d",v[k++]);
00084                                 fputs(")",fp);
00085                         }
00086                 }
00087         }
00088 }
00089 
00090 }