Trisurf Monte Carlo simulator
Samo Penic
2014-06-25 49dbdd4940aa78021f4ba516f4edb632058262bf
commit | author | age
0af0cf 1 #include<general.h>
SP 2 #include<cross-section.h>
3 #include<coord.h>
a61c00 4 #include<cairo/cairo.h>
49dbdd 5 #include<stdint.h>
0af0cf 6 /** @brief Calculates cross-section of vesicle with plane.
SP 7  *
8  * Function returns points of cross-section of vesicle with plane. Plane is described with equation $ax+by+cz+d=0$. Algorithm extracts coordinates of each vertex of a vesicle and then:
9  *
10  * if a distance of point to plane (given by equation $D=\frac{ax_0+by_0+cz_0+d}{\sqrt{a^2+b^2+c^2}}$, where $x_0$, $y_0$ and $z_0$ are coordinates of a given vertex) is less than maximal allowed distance between vertices {\tt sqrt(vesicle->dmax)} than vertex is a candidate for crossection calculation.
11  *
12  */
d27c07 13 ts_coord_list *get_crossection_with_plane(ts_vesicle *vesicle,ts_double a,ts_double b,ts_double c, ts_double d){
0af0cf 14
SP 15
ad4e70 16     ts_uint i, j,k,l;
0af0cf 17     ts_double pp,Dsq; // distance from the plane squared
ad4e70 18     ts_double ppn1; // distance from the plane squared of a neighbor
0af0cf 19     ts_vertex *vtx;
a61c00 20     ts_uint ntria=0; // number triangles
SP 21     ts_triangle *tria[2]; // list of triangles
0af0cf 22     ts_coord_list *pts=init_coord_list();
d27c07 23     for(i=0;i<vesicle->vlist->n;i++){
0af0cf 24         vtx=vesicle->vlist->vtx[i];
SP 25
26         pp=vtx->x*a+vtx->y*b+vtx->z*c+d;
27         Dsq=pp*pp/(a*a+b*b+c*c);
28         if(Dsq<vesicle->dmax){
29             for(j=0;j<vtx->neigh_no;j++){
a61c00 30                 ppn1=vtx->neigh[j]->x*a+vtx->neigh[j]->y*b+vtx->neigh[j]->z*c+d;
7d62fc 31                 if(pp*ppn1<=0.0){ //the combination of vertices are good candidates for a crossection
a61c00 32                     //find triangle that belongs to the two vertices
SP 33                     ntria=0;
34                     for(k=0;k<vtx->tristar_no;k++){
ad4e70 35                         if(vtx->tristar[k]->vertex[0]==vtx && ( vtx->tristar[k]->vertex[1]==vtx->neigh[j] || vtx->tristar[k]->vertex[2]==vtx->neigh[j]) ){ 
a61c00 36                             //triangle found.
SP 37                             tria[ntria]=vtx->tristar[k];
38                             ntria++;
39                         }
40                     }
7d62fc 41                     if(ntria==0) continue; // no need to continue
a61c00 42                     //find the two intersections (in general) to form a intersection line
ad4e70 43                     for(l=0;l<ntria;l++){
SP 44                     //we add intersection line between two points for each of the triangles found above.
45                         add_crosssection_point(pts,a,b,c,d,tria[l]->vertex[0], tria[l]->vertex[1]);
46                         add_crosssection_point(pts,a,b,c,d,tria[l]->vertex[0], tria[l]->vertex[2]);
47                         add_crosssection_point(pts,a,b,c,d,tria[l]->vertex[1], tria[l]->vertex[2]);
a61c00 48
SP 49                     }
0af0cf 50                 }
SP 51             }
52         }
53     }
54     return pts;
55 }
56
57
ad4e70 58
SP 59 ts_bool add_crosssection_point(ts_coord_list *pts, ts_double a, ts_double b, ts_double c, ts_double d, ts_vertex *vtx1, ts_vertex *vtx2){
60     ts_double pp=vtx1->x*a+vtx1->y*b+vtx1->z*c+d;
61     ts_double pp2=vtx2->x*a+vtx1->y*b+vtx2->z*c+d;
7d62fc 62     if(pp*pp2<=0.0){
ad4e70 63     ts_double u=pp/(a*(vtx1->x-vtx2->x)+b*(vtx1->y-vtx2->y)+c*(vtx1->z-vtx2->z));
SP 64     add_coord(pts, vtx1->x+u*(vtx2->x - vtx1->x),
65         vtx1->y+u*(vtx2->y - vtx1->y),        
66         vtx1->z+u*(vtx2->z - vtx1->z),
67         TS_COORD_CARTESIAN);
68     
69     return TS_SUCCESS;
70     } else {
71     return TS_FAIL;
72     }
73 }
74
a61c00 75 /** Saves calculated crossection as a png image */
SP 76 ts_bool crossection_to_png(ts_coord_list *pts, char *filename){
77
78     cairo_surface_t *surface;
79     cairo_t *cr;
80     ts_uint i;
49dbdd 81     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 768, 576);
a61c00 82     cr = cairo_create (surface);
49dbdd 83     cairo_rectangle(cr, 0.0, 0.0, 768,576);
SP 84     cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
a61c00 85     cairo_fill(cr);
49dbdd 86     cairo_set_line_width (cr, 2.0/8.0);
324ad7 87     cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
SP 88     cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND); 
49dbdd 89     cairo_translate(cr, 380,250);
SP 90     cairo_scale (cr, 8.0, 8.0);
91     cairo_set_source_rgb (cr, 0.3, 0.3, 0.3);
a61c00 92
SP 93     for(i=0;i<pts->n;i+=2){
94         cairo_move_to(cr, pts->coord[i]->e1, pts->coord[i]->e2);
95         cairo_line_to(cr, pts->coord[i+1]->e1, pts->coord[i+1]->e2);
96     }
97     cairo_stroke(cr);
98     cairo_surface_write_to_png (surface,filename);
49dbdd 99     append_to_isak_program_raw(surface,"experimental.img");
a61c00 100     cairo_surface_finish (surface);    
SP 101     return TS_SUCCESS;
102 }
ad4e70 103
SP 104
105 ts_bool save_crossection_snapshot(ts_coord_list *pts, ts_uint timestepno){
106     char filename[255];
107         sprintf(filename,"timestep_%.6u.png",timestepno);
108     crossection_to_png(pts,filename);
109     return TS_SUCCESS;
110 }
49dbdd 111
SP 112 ts_bool append_to_isak_program_raw(cairo_surface_t *surface, char *filename){
113     
114     unsigned char *d=cairo_image_surface_get_data(surface);
115     int w=cairo_image_surface_get_width(surface);
116     int h=cairo_image_surface_get_height(surface);
117     int s=cairo_image_surface_get_stride(surface);
118     int i,j;
119     FILE *fd=fopen(filename, "a+");
120
121     uint32_t *pixelptr; 
122     uint32_t my_pixel;
123     uint8_t pixel_channel;
124 //       red = (pixel[8] >> 16) & 0xFF;
125
126
127
128        fwrite (&w,sizeof(uint32_t),1,fd);
129        fwrite (&h,sizeof(uint32_t),1,fd);
130        fwrite (&w,sizeof(uint32_t),1,fd);//time!
131     for(i=0;i<h;i++){
132         for(j=0;j<w;j++){
133             pixelptr=(uint32_t *)(d + i * s);
134             my_pixel = pixelptr[j];
135             pixel_channel=(my_pixel>>16)&0xFF;
136             fwrite(&pixel_channel,sizeof(uint8_t), 1,fd);
137         }
138     }
139     fclose(fd);
140     return TS_SUCCESS;
141 }
142
143 /*ts_bool init_isak_program_raw_output_file(ts_uint w, ts_uint h, char *filename){
144
145
146 }*/