Bugfix in search_for_outside_edge routine.
[voro++.git] / branches / 2d / src / v_connect / cell_2d.hh
blob6a4b0fcf4fd5fe9c5bede1f53dcb245038681ac3
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 // Turn on if to be used with voro_connect
38 bool full_connect;
39 // To be used with voro_connect
40 vector<int> *vertexg;
41 // To be used with voro_connect
42 int my_id;
43 voronoicell_base_2d();
44 ~voronoicell_base_2d();
45 void init_base(double xmin,double xmax,double ymin,double ymax);
46 void draw_gnuplot(double x,double y,FILE *fp=stdout);
47 /** Outputs the edges of the Voronoi cell in gnuplot format to
48 * an output stream.
49 * \param[in] (x,y) a displacement vector to be added to the
50 * cell's position.
51 * \param[in] filename the file to write to. */
52 inline void draw_gnuplot(double x,double y,const char *filename) {
53 FILE *fp=safe_fopen(filename,"w");
54 draw_gnuplot(x,y,fp);
55 fclose(fp);
57 void draw_pov(double x,double y,FILE *fp=stdout);
58 /** Outputs the edges of the Voronoi cell in POV-Ray format to
59 * an open file stream, displacing the cell by given vector.
60 * \param[in] (x,y,z) a displacement vector to be added to the
61 * cell's position.
62 * \param[in] filename the file to write to. */
63 inline void draw_pov(double x,double y,const char *filename) {
64 FILE *fp=safe_fopen(filename,"w");
65 draw_pov(x,y,fp);
66 fclose(fp);
68 void output_custom(const char *format,int i,double x,double y,double r,FILE *fp=stdout);
69 /** Computes the Voronoi cells for all particles in the
70 * container, and for each cell, outputs a line containing
71 * custom information about the cell structure. The output
72 * format is specified using an input string with control
73 * sequences similar to the standard C printf() routine.
74 * \param[in] format the format of the output lines, using
75 * control sequences to denote the different
76 * cell statistics.
77 * \param[in] i the ID of the particle associated with this
78 * Voronoi cell.
79 * \param[in] (x,y) the position of the particle associated
80 * with this Voronoi cell.
81 * \param[in] r a radius associated with the particle.
82 * \param[in] filename the file to write to. */
83 inline void output_custom(const char *format,int i,double x,double y,double r,const char *filename) {
84 FILE *fp=safe_fopen(filename,"w");
85 output_custom(format,i,x,y,r,fp);
86 fclose(fp);
88 template<class vc_class>
89 bool nplane(vc_class &vc,double x,double y,double rs,int p_id);
90 template<class vc_class>
91 bool nplane_cut(vc_class &vc,double x,double y,double rsq,int p_id,double u,int up);
92 bool plane_intersects(double x,double y,double rs);
93 inline bool plane_intersects_guess(double x,double y,double rs) {
94 return plane_intersects(x,y,rs);
96 double max_radius_squared();
97 double perimeter();
98 double area();
99 void vertices(vector<double> &v);
100 void output_vertices(FILE *fp=stdout);
101 void vertices(double x,double y,vector<double> &v);
102 void output_vertices(double x,double y,FILE *fp=stdout);
103 void edge_lengths(vector<double> &vd);
104 void normals(vector<double> &vd);
105 void centroid(double &cx,double &cy);
106 //given 2 vectors a and b, returns an element that they have in common !=c
107 vector<int> common_gen(vector<int> &a,vector<int> &b);
108 //called from voro_connect
109 inline void full_connect_on(){
110 full_connect=true;
111 vertexg=new vector<int>[current_vertices];
112 for(int i=0;i<p;i++){
113 vertexg[i].push_back(my_id);
116 //called from voro_connect
117 inline void set_id(int id){
118 my_id=id;
120 //called from voro_connect(add vertex-generator)
121 void add_vg(vector<int> &a,int g);
122 void vg_copy(int o,int n);
124 virtual void neighbors(vector<int> &v) {v.clear();}
125 protected:
126 /** Computes the distance of a Voronoi cell vertex to a plane.
127 * \param[in] (x,y) the normal vector to the plane.
128 * \param[in] rsq the distance along this vector of the plane.
129 * \param[in] qp the index of the vertex to consider. */
130 inline double pos(double x,double y,double rsq,int qp) {
131 return x*pts[2*qp]+y*pts[2*qp+1]-rsq;
133 private:
134 template<class vc_class>
135 void add_memory_vertices(vc_class &vc);
136 void add_memory_ds(int *&stackp);
137 /** The delete stack, used to store the vertices that are
138 * deleted during the plane cutting procedure. */
139 int *ds;
140 /** A pointer to the end of the delete stack, used to detect
141 * when it is full. */
142 int *stacke;
145 class voronoicell_2d : public voronoicell_base_2d {
146 public:
147 using voronoicell_base_2d::nplane;
148 inline bool nplane(double x,double y,double rs,int p_id) {
149 return nplane(*this,x,y,rs,0);
151 inline bool nplane(double x,double y,int p_id) {
152 double rs=x*x+y*y;
153 return nplane(*this,x,y,rs,0);
155 inline bool plane(double x,double y,double rs) {
156 return nplane(*this,x,y,rs,0);
158 inline bool plane(double x,double y) {
159 double rs=x*x+y*y;
160 return nplane(*this,x,y,rs,0);
162 inline void init(double xmin,double xmax,double ymin,double ymax) {
163 init_base(xmin,xmax,ymin,ymax);
165 private:
166 inline void n_add_memory_vertices() {}
167 inline void n_copy(int a,int b) {}
168 inline void n_set(int a,int id) {}
169 friend class voronoicell_base_2d;
172 class voronoicell_neighbor_2d : public voronoicell_base_2d {
173 public:
174 using voronoicell_base_2d::nplane;
175 int *ne;
176 voronoicell_neighbor_2d() : ne(new int[init_vertices]) {}
177 ~voronoicell_neighbor_2d() {delete [] ne;}
178 inline bool nplane(double x,double y,double rs,int p_id) {
179 return nplane(*this,x,y,rs,p_id);
181 inline bool nplane(double x,double y,int p_id) {
182 double rs=x*x+y*y;
183 return nplane(*this,x,y,rs,p_id);
185 inline bool plane(double x,double y,double rs) {
186 return nplane(*this,x,y,rs,0);
188 inline bool plane(double x,double y) {
189 double rs=x*x+y*y;
190 return nplane(*this,x,y,rs,0);
192 void init(double xmin,double xmax,double ymin,double ymax);
193 virtual void neighbors(vector<int> &v);
194 private:
195 inline void n_add_memory_vertices();
196 inline void n_copy(int a,int b) {ne[a]=ne[b];}
197 inline void n_set(int a,int id) {ne[a]=id;}
198 friend class voronoicell_base_2d;
202 #endif