Trisurf Monte Carlo simulator
Samo Penic
2010-11-28 a2a67644115039486dc07aa4d9d636267f99b22a
commit | author | age
d7639a 1 #include<stdlib.h>
SP 2 #include<stdio.h>
3 #include "general.h"
4 #include "triangle.h"
5 #include<math.h>
6
a2a676 7 ts_triangle_list *init_triangle_list(){
SP 8     ts_triangle_list *tlist=(ts_triangle_list *)malloc(sizeof(ts_triangle_list));
d7639a 9     tlist->n = 0;
a2a676 10     tlist->tria=NULL;
SP 11     return tlist;
d7639a 12 }
SP 13
14
a2a676 15 ts_triangle *triangle_add(ts_triangle_list *tlist, ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){
SP 16         if(vtx1==NULL || vtx2==NULL || vtx3==NULL){
17             return NULL;
18         }
d7639a 19         tlist->n++;
a2a676 20         tlist->tria=(ts_triangle **)realloc(tlist->tria,tlist->n*sizeof(ts_triangle *));
SP 21         if(tlist->tria==NULL) fatal("Cannot reallocate memory for additional ts_triangle.",5);
22
23         tlist->tria[tlist->n-1]=(ts_triangle *)calloc(1,sizeof(ts_triangle));
24         if(tlist->tria[tlist->n-1]==NULL) fatal("Cannot reallocate memory for additional ts_triangle.",5);
25         tlist->tria[tlist->n-1]->data=(ts_triangle_data *)calloc(1,sizeof(ts_triangle_data));
26
d7639a 27         //NOW insert vertices!
a2a676 28         tlist->tria[tlist->n - 1]->idx=tlist->n-1;
SP 29         tlist->tria[tlist->n - 1]->data->vertex[0]=vtx1;
30         tlist->tria[tlist->n - 1]->data->vertex[1]=vtx2;
31         tlist->tria[tlist->n - 1]->data->vertex[2]=vtx3;
32         return tlist->tria[tlist->n-1];
d7639a 33 }
SP 34
35
36 ts_bool triangle_add_neighbour(ts_triangle *tria, ts_triangle *ntria){
a2a676 37     if(tria==NULL || ntria==NULL) return TS_FAIL;
SP 38 /*TODO: check if the neighbour already exists! Now there is no such check
39  * because of the performance issue. */
40     tria->data->neigh_no++;
41     tria->data->neigh=realloc(tria->data->neigh,tria->data->neigh_no*sizeof(ts_triangle *));
42     if(tria->data->neigh == NULL)
d7639a 43             fatal("Reallocation of memory failed during insertion of triangle neighbour in triangle_add_neighbour",3);
a2a676 44     tria->data->neigh[tria->data->neigh_no-1]=ntria;
SP 45    
46 /* we repeat the procedure for the neighbour */  
47     ntria->data->neigh_no++;
48     ntria->data->neigh=realloc(ntria->data->neigh,ntria->data->neigh_no*sizeof(ts_triangle *));
49     if(ntria->data->neigh == NULL)
50             fatal("Reallocation of memory failed during insertion of triangle neighbour in triangle_add_neighbour",3);
51     ntria->data->neigh[ntria->data->neigh_no-1]=tria;
d7639a 52     return TS_SUCCESS;
SP 53 }
54
55
56 ts_bool triangle_remove_neighbour(ts_triangle *tria, ts_triangle *ntria){
a2a676 57     ts_uint i,j=0; 
SP 58     if(tria==NULL || ntria==NULL) return TS_FAIL;
59
60     for(i=0;i<tria->data->neigh_no;i++){
61         if(tria->data->neigh[i]!=ntria){
62             tria->data->neigh[j]=tria->data->neigh[i];
d7639a 63             j++;
SP 64         } 
65     }
a2a676 66     if(j==i) {
SP 67         return TS_FAIL; 
68         //fatal("In triangle_remove_neighbour: Specified neighbour does not exist for given triangle",3);
69     }
70     tria->data->neigh_no--;
71     tria->data->neigh=(ts_triangle **)realloc(tria->data->neigh,tria->data->neigh_no*sizeof(ts_triangle *));
72     if(tria->data->neigh == NULL){
73         fatal("Reallocation of memory failed during removal of vertex neighbour in triangle_remove_neighbour",100);
74     }
75 /* we repeat the procedure for neighbour */
76     for(i=0;i<ntria->data->neigh_no;i++){
77         if(ntria->data->neigh[i]!=tria){
78             ntria->data->neigh[j]=ntria->data->neigh[i];
79             j++;
80         } 
81     }
82     if(j==i) {
83         return TS_FAIL; 
84         //fatal("In triangle_remove_neighbour: Specified neighbour does not exist for given triangle",3);
85     }
86     ntria->data->neigh_no--;
87     ntria->data->neigh=(ts_triangle **)realloc(ntria->data->neigh,ntria->data->neigh_no*sizeof(ts_triangle *));
88     if(ntria->data->neigh == NULL){
89         fatal("Reallocation of memory failed during removal of vertex neighbour in triangle_remove_neighbour",100);
90     }
d7639a 91     return TS_SUCCESS;
SP 92 }
93
94 ts_bool triangle_normal_vector(ts_triangle *tria){
95     ts_double x21,x31,y21,y31,z21,z31,xden;
a2a676 96     x21=tria->data->vertex[1]->data->x - tria->data->vertex[0]->data->x;
SP 97     x31=tria->data->vertex[2]->data->x - tria->data->vertex[0]->data->x;
98     y21=tria->data->vertex[1]->data->y - tria->data->vertex[0]->data->y;
99     y31=tria->data->vertex[2]->data->y - tria->data->vertex[0]->data->y;
100     z21=tria->data->vertex[1]->data->z - tria->data->vertex[0]->data->z;
101     z31=tria->data->vertex[2]->data->z - tria->data->vertex[0]->data->z;
d7639a 102
a2a676 103     tria->data->xnorm=y21*z31 - z21*y31;
SP 104     tria->data->ynorm=z21*x31 - x21*z31;
105     tria->data->znorm=x21*y31 - y21*x31;
106     xden=tria->data->xnorm*tria->data->xnorm +
107          tria->data->ynorm*tria->data->ynorm + 
108          tria->data->znorm*tria->data->znorm;
d7639a 109 #ifdef TS_DOUBLE_DOUBLE
SP 110     xden=sqrt(xden);
111 #endif
112 #ifdef TS_DOUBLE_FLOAT
113     xden=sqrtf(xden);
114 #endif
115 #ifdef TS_DOUBLE_LONGDOUBLE
116     xden=sqrtl(xden);
117 #endif
a2a676 118     tria->data->xnorm=tria->data->xnorm/xden;
SP 119     tria->data->ynorm=tria->data->ynorm/xden;
120     tria->data->znorm=tria->data->znorm/xden;    
d7639a 121     return TS_SUCCESS;
SP 122 }
123
124
a2a676 125 ts_bool triangle_data_free(ts_triangle_data *data){
SP 126     if(data->neigh!=NULL) free(data->neigh);
127     free(data);
d7639a 128     return TS_SUCCESS;
SP 129 }
130
131 ts_bool triangle_list_free(ts_triangle_list *tlist){
a2a676 132     ts_uint i;
d7639a 133     for(i=0;i<tlist->n;i++){
a2a676 134         triangle_data_free(tlist->tria[i]->data);
SP 135         free(tlist->tria[i]);
d7639a 136     }
a2a676 137     free(tlist->tria);
SP 138     free(tlist);  
d7639a 139     return TS_SUCCESS;
SP 140 }
141