1 // Voro++, a 3D cell-based Voronoi library
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : August 30th 2011
8 * \brief Implementations of the small helper functions. */
14 void check_duplicate(int n
,double x
,double y
,double z
,int id
,double *qp
) {
15 double dx
=*qp
-x
,dy
=qp
[1]-y
,dz
=qp
[2]-z
;
16 if(dx
*dx
+dy
*dy
+dz
*dz
<1e-10) {
17 printf("Duplicate: %d (%g,%g,%g) matches %d (%g,%g,%g)\n",n
,x
,y
,z
,id
,*qp
,qp
[1],qp
[2]);
22 /** \brief Function for printing fatal error messages and exiting.
24 * Function for printing fatal error messages and exiting.
25 * \param[in] p a pointer to the message to print.
26 * \param[in] status the status code to return with. */
27 void voro_fatal_error(const char *p
,int status
) {
28 fprintf(stderr
,"voro++: %s\n",p
);
32 /** \brief Prints a vector of positions.
34 * Prints a vector of positions as bracketed triplets.
35 * \param[in] v the vector to print.
36 * \param[in] fp the file stream to print to. */
37 void voro_print_positions(std::vector
<double> &v
,FILE *fp
) {
39 fprintf(fp
,"(%g,%g,%g)",v
[0],v
[1],v
[2]);
40 for(int k
=3;(unsigned int) k
<v
.size();k
+=3) {
41 fprintf(fp
," (%g,%g,%g)",v
[k
],v
[k
+1],v
[k
+2]);
46 /** \brief Opens a file and checks the operation was successful.
48 * Opens a file, and checks the return value to ensure that the operation
50 * \param[in] filename the file to open.
51 * \param[in] mode the cstdio fopen mode to use.
52 * \return The file handle. */
53 FILE* safe_fopen(const char *filename
,const char *mode
) {
54 FILE *fp
=fopen(filename
,mode
);
56 fprintf(stderr
,"voro++: Unable to open file '%s'\n",filename
);
57 exit(VOROPP_FILE_ERROR
);
62 /** \brief Prints a vector of integers.
64 * Prints a vector of integers.
65 * \param[in] v the vector to print.
66 * \param[in] fp the file stream to print to. */
67 void voro_print_vector(std::vector
<int> &v
,FILE *fp
) {
70 fprintf(fp
,"%d %d %d %d ",v
[k
],v
[k
+1],v
[k
+2],v
[k
+3]);
74 if(k
+4==s
) fprintf(fp
,"%d %d %d %d",v
[k
],v
[k
+1],v
[k
+2],v
[k
+3]);
75 else fprintf(fp
,"%d %d %d",v
[k
],v
[k
+1],v
[k
+2]);
77 if(k
+2==s
) fprintf(fp
,"%d %d",v
[k
],v
[k
+1]);
78 else fprintf(fp
,"%d",v
[k
]);
82 /** \brief Prints a vector of doubles.
84 * Prints a vector of doubles.
85 * \param[in] v the vector to print.
86 * \param[in] fp the file stream to print to. */
87 void voro_print_vector(std::vector
<double> &v
,FILE *fp
) {
90 fprintf(fp
,"%g %g %g %g ",v
[k
],v
[k
+1],v
[k
+2],v
[k
+3]);
94 if(k
+4==s
) fprintf(fp
,"%g %g %g %g",v
[k
],v
[k
+1],v
[k
+2],v
[k
+3]);
95 else fprintf(fp
,"%g %g %g",v
[k
],v
[k
+1],v
[k
+2]);
97 if(k
+2==s
) fprintf(fp
,"%g %g",v
[k
],v
[k
+1]);
98 else fprintf(fp
,"%g",v
[k
]);
102 /** \brief Prints a vector a face vertex information.
104 * Prints a vector of face vertex information. A value is read, which
105 * corresponds to the number of vertices in the next face. The routine reads
106 * this number of values and prints them as a bracked list. This is repeated
107 * until the end of the vector is reached.
108 * \param[in] v the vector to interpret and print.
109 * \param[in] fp the file stream to print to. */
110 void voro_print_face_vertices(std::vector
<int> &v
,FILE *fp
) {
115 if(l
==1) fprintf(fp
,"(%d)",v
[k
++]);
119 fprintf(fp
,"(%d",v
[k
++]);
120 while(k
<j
) fprintf(fp
,",%d",v
[k
++]);
123 while((unsigned int) k
<v
.size()) {
126 if(l
==1) fprintf(fp
," (%d)",v
[k
++]);
127 else fputs(" ()",fp
);
130 fprintf(fp
," (%d",v
[k
++]);
131 while(k
<j
) fprintf(fp
,",%d",v
[k
++]);