Reset platonic code.
[voro++.git] / branches / 2d / src / cell_2d.hh
blob60d578f67a9f56d58f1a0df5dca89964e54a605f
1 /** \file cell_2d.hh
2 * \brief Header file for the voronoicell_2d class. */
4 #ifndef VOROPP_CELL_2D_HH
5 #define VOROPP_CELL_2D_HH
7 #include <cstdio>
8 #include <cstdlib>
9 #include <cmath>
10 #include <vector>
11 using namespace std;
13 #include "common.hh"
14 #include "config.hh"
16 namespace voro {
18 /** \brief A class encapsulating all the routines for storing and calculating a
19 * single Voronoi cell. */
20 class voronoicell_base_2d {
21 public:
22 /** This holds the current size of the ed and pts arrays. If
23 * more vertices are created than can fit in these arrays, then
24 * they are dynamically extended using the add_memory_vertices
25 * routine. */
26 int current_vertices;
27 /** This sets the size of the current delete stack. */
28 int current_delete_size;
29 /** The total nuber of vertices in the current cell. */
30 int p;
31 /** An array with size 2*current_vertices holding information
32 * about edge connections between vertices.*/
33 int *ed;
34 /** An array with size 2*current_vertices for holding
35 * the positions of the vertices. */
36 double *pts;
37 voronoicell_base_2d();
38 ~voronoicell_base_2d();
39 void init_base(double xmin,double xmax,double ymin,double ymax);
40 void draw_gnuplot(double x,double y,FILE *fp=stdout);
41 /** Outputs the edges of the Voronoi cell in gnuplot format to
42 * an output stream.
43 * \param[in] (x,y) a displacement vector to be added to the
44 * cell's position.
45 * \param[in] filename the file to write to. */
46 inline void draw_gnuplot(double x,double y,const char *filename) {
47 FILE *fp=safe_fopen(filename,"w");
48 draw_gnuplot(x,y,fp);
49 fclose(fp);
51 void draw_pov(double x,double y,FILE *fp=stdout);
52 /** Outputs the edges of the Voronoi cell in POV-Ray format to
53 * an open file stream, displacing the cell by given vector.
54 * \param[in] (x,y,z) a displacement vector to be added to the
55 * cell's position.
56 * \param[in] filename the file to write to. */
57 inline void draw_pov(double x,double y,const char *filename) {
58 FILE *fp=safe_fopen(filename,"w");
59 draw_pov(x,y,fp);
60 fclose(fp);
62 void output_custom(const char *format,int i,double x,double y,double r,FILE *fp=stdout);
63 /** Computes the Voronoi cells for all particles in the
64 * container, and for each cell, outputs a line containing
65 * custom information about the cell structure. The output
66 * format is specified using an input string with control
67 * sequences similar to the standard C printf() routine.
68 * \param[in] format the format of the output lines, using
69 * control sequences to denote the different
70 * cell statistics.
71 * \param[in] i the ID of the particle associated with this
72 * Voronoi cell.
73 * \param[in] (x,y) the position of the particle associated
74 * with this Voronoi cell.
75 * \param[in] r a radius associated with the particle.
76 * \param[in] filename the file to write to. */
77 inline void output_custom(const char *format,int i,double x,double y,double r,const char *filename) {
78 FILE *fp=safe_fopen(filename,"w");
79 output_custom(format,i,x,y,r,fp);
80 fclose(fp);
82 template<class vc_class>
83 bool nplane(vc_class &vc,double x,double y,double rs,int p_id);
84 template<class vc_class>
85 bool nplane_cut(vc_class &vc,double x,double y,double rsq,int p_id,double u,int up);
86 bool plane_intersects(double x,double y,double rs);
87 inline bool plane_intersects_guess(double x,double y,double rs) {
88 return plane_intersects(x,y,rs);
90 double max_radius_squared();
91 double perimeter();
92 double area();
93 void vertices(vector<double> &v);
94 void output_vertices(FILE *fp=stdout);
95 void vertices(double x,double y,vector<double> &v);
96 void output_vertices(double x,double y,FILE *fp=stdout);
97 void edge_lengths(vector<double> &vd);
98 void normals(vector<double> &vd);
99 void centroid(double &cx,double &cy);
100 virtual void neighbors(vector<int> &v) {v.clear();}
101 protected:
102 /** Computes the distance of a Voronoi cell vertex to a plane.
103 * \param[in] (x,y) the normal vector to the plane.
104 * \param[in] rsq the distance along this vector of the plane.
105 * \param[in] qp the index of the vertex to consider. */
106 inline double pos(double x,double y,double rsq,int qp) {
107 return x*pts[2*qp]+y*pts[2*qp+1]-rsq;
109 private:
110 template<class vc_class>
111 void add_memory_vertices(vc_class &vc);
112 void add_memory_ds(int *&stackp);
113 /** The delete stack, used to store the vertices that are
114 * deleted during the plane cutting procedure. */
115 int *ds;
116 /** A pointer to the end of the delete stack, used to detect
117 * when it is full. */
118 int *stacke;
121 class voronoicell_2d : public voronoicell_base_2d {
122 public:
123 using voronoicell_base_2d::nplane;
124 inline bool nplane(double x,double y,double rs,int p_id) {
125 return nplane(*this,x,y,rs,0);
127 inline bool nplane(double x,double y,int p_id) {
128 double rs=x*x+y*y;
129 return nplane(*this,x,y,rs,0);
131 inline bool plane(double x,double y,double rs) {
132 return nplane(*this,x,y,rs,0);
134 inline bool plane(double x,double y) {
135 double rs=x*x+y*y;
136 return nplane(*this,x,y,rs,0);
138 inline void init(double xmin,double xmax,double ymin,double ymax) {
139 init_base(xmin,xmax,ymin,ymax);
141 private:
142 inline void n_add_memory_vertices() {}
143 inline void n_copy(int a,int b) {}
144 inline void n_set(int a,int id) {}
145 friend class voronoicell_base_2d;
148 class voronoicell_neighbor_2d : public voronoicell_base_2d {
149 public:
150 using voronoicell_base_2d::nplane;
151 int *ne;
152 voronoicell_neighbor_2d() : ne(new int[init_vertices]) {}
153 ~voronoicell_neighbor_2d() {delete [] ne;}
154 inline bool nplane(double x,double y,double rs,int p_id) {
155 return nplane(*this,x,y,rs,p_id);
157 inline bool nplane(double x,double y,int p_id) {
158 double rs=x*x+y*y;
159 return nplane(*this,x,y,rs,p_id);
161 inline bool plane(double x,double y,double rs) {
162 return nplane(*this,x,y,rs,0);
164 inline bool plane(double x,double y) {
165 double rs=x*x+y*y;
166 return nplane(*this,x,y,rs,0);
168 void init(double xmin,double xmax,double ymin,double ymax);
169 virtual void neighbors(vector<int> &v);
170 private:
171 inline void n_add_memory_vertices();
172 inline void n_copy(int a,int b) {ne[a]=ne[b];}
173 inline void n_set(int a,int id) {ne[a]=id;}
174 friend class voronoicell_base_2d;
178 #endif