commit | author | age
|
d7639a
|
1 |
#include<stdlib.h> |
SP |
2 |
#include<math.h> |
|
3 |
#include<string.h> |
|
4 |
#include "general.h" |
|
5 |
#include "vertex.h" |
a10dd5
|
6 |
#include "bond.h" |
d7639a
|
7 |
#include<stdio.h> |
SP |
8 |
|
73f967
|
9 |
ts_vertex_list *init_vertex_list(ts_uint N){ |
d7639a
|
10 |
ts_int i; |
a10dd5
|
11 |
ts_vertex *tlist; |
SP |
12 |
ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list)); |
73f967
|
13 |
|
d7639a
|
14 |
if(N==0){ |
SP |
15 |
err("Initialized vertex list with zero elements. Pointer set to NULL"); |
73f967
|
16 |
vlist->n=0; |
SP |
17 |
vlist->vtx=NULL; |
|
18 |
return vlist; |
d7639a
|
19 |
} |
73f967
|
20 |
|
a10dd5
|
21 |
vlist->vtx=(ts_vertex **)malloc(N*sizeof(ts_vertex *)); |
SP |
22 |
tlist=(ts_vertex *)malloc(N*sizeof(ts_vertex)); |
|
23 |
if(vlist->vtx==NULL || tlist==NULL) |
73f967
|
24 |
fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100); |
a10dd5
|
25 |
for(i=0;i<N;i++) { |
SP |
26 |
vlist->vtx[i]=&tlist[i]; |
|
27 |
vlist->vtx[i]->data=init_vertex_data(); |
|
28 |
vlist->vtx[i]->idx=i; |
|
29 |
} |
73f967
|
30 |
vlist->n=N; |
d7639a
|
31 |
return vlist; |
SP |
32 |
} |
|
33 |
|
737714
|
34 |
ts_vertex_data *init_vertex_data(){ |
SP |
35 |
ts_vertex_data *data; |
a10dd5
|
36 |
data=(ts_vertex_data *)calloc(1,sizeof(ts_vertex_data)); |
737714
|
37 |
if(data==NULL) |
d7639a
|
38 |
fatal("Fatal error reserving memory space for ts_vertex! Memory full?", 100); |
737714
|
39 |
return data; |
d7639a
|
40 |
} |
SP |
41 |
|
|
42 |
|
737714
|
43 |
ts_bool vtx_add_neighbour(ts_vertex *vtx, ts_vertex *nvtx){ |
d7639a
|
44 |
ts_uint i; |
SP |
45 |
/* no neighbour can be null! */ |
|
46 |
if(vtx==NULL || nvtx==NULL) return TS_FAIL; |
|
47 |
|
|
48 |
/*if it is already a neighbour don't add it to the list */ |
737714
|
49 |
for(i=0; i<vtx->data->neigh_no;i++){ |
SP |
50 |
if(vtx->data->neigh[i]==nvtx) return TS_FAIL; |
d7639a
|
51 |
} |
a10dd5
|
52 |
ts_uint nn=++vtx->data->neigh_no; |
737714
|
53 |
vtx->data->neigh=(ts_vertex **)realloc(vtx->data->neigh, nn*sizeof(ts_vertex *)); |
a10dd5
|
54 |
vtx->data->neigh[nn-1]=nvtx; |
d7639a
|
55 |
|
SP |
56 |
/* pa se sosedu dodamo vertex */ |
|
57 |
/*if it is already a neighbour don't add it to the list */ |
737714
|
58 |
for(i=0; i<nvtx->data->neigh_no;i++){ |
SP |
59 |
if(nvtx->data->neigh[i]==vtx) return TS_FAIL; |
d7639a
|
60 |
} |
a10dd5
|
61 |
nn=++nvtx->data->neigh_no; |
737714
|
62 |
nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh, nn*sizeof(ts_vertex *)); |
a10dd5
|
63 |
nvtx->data->neigh[nn-1]=vtx; |
d7639a
|
64 |
|
SP |
65 |
|
|
66 |
return TS_SUCCESS; |
|
67 |
} |
a10dd5
|
68 |
|
9802f1
|
69 |
/* TODO: optimize this. test this. */ |
SP |
70 |
ts_bool vtx_remove_neighbour(ts_vertex *vtx, ts_vertex *nvtx){ |
|
71 |
/* find a neighbour */ |
|
72 |
/* remove it from the list while shifting remaining neighbours up */ |
|
73 |
ts_uint i,j=0; |
|
74 |
for(i=0;i<vtx->data->neigh_no;i++){ |
|
75 |
if(vtx->data->neigh[i]!=nvtx){ |
|
76 |
vtx->data->neigh[j]=vtx->data->neigh[i]; |
|
77 |
j++; |
|
78 |
} |
|
79 |
} |
|
80 |
/* resize memory. potentionally time consuming */ |
|
81 |
vtx->data->neigh_no--; |
|
82 |
vtx->data->neigh=(ts_vertex **)realloc(vtx->data->neigh,vtx->data->neigh_no*sizeof(ts_vertex *)); |
|
83 |
if(vtx->data->neigh == NULL && vtx->data->neigh_no!=0) |
|
84 |
fatal("Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100); |
|
85 |
|
|
86 |
/* repeat for the neighbour */ |
|
87 |
/* find a neighbour */ |
|
88 |
/* remove it from the list while shifting remaining neighbours up */ |
|
89 |
for(i=0;i<nvtx->data->neigh_no;i++){ |
|
90 |
if(nvtx->data->neigh[i]!=vtx){ |
|
91 |
nvtx->data->neigh[j]=nvtx->data->neigh[i]; |
|
92 |
j++; |
|
93 |
} |
|
94 |
} |
|
95 |
/* resize memory. potentionally time consuming. */ |
|
96 |
nvtx->data->neigh_no--; |
|
97 |
nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh,nvtx->data->neigh_no*sizeof(ts_vertex *)); |
|
98 |
if(nvtx->data->neigh == NULL && nvtx->data->neigh_no!=0) |
|
99 |
fatal("Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100); |
|
100 |
|
|
101 |
return TS_SUCCESS; |
|
102 |
} |
7958e9
|
103 |
|
9802f1
|
104 |
|
SP |
105 |
|
a10dd5
|
106 |
ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2){ |
SP |
107 |
ts_bond *bond; |
|
108 |
bond=bond_add(blist,vtx1,vtx2); |
|
109 |
if(bond==NULL) return TS_FAIL; |
|
110 |
vtx1->data->bond_no++; |
|
111 |
vtx2->data->bond_no++; |
|
112 |
|
|
113 |
vtx1->data->bond=(ts_bond **)realloc(vtx1->data->bond, vtx1->data->bond_no*sizeof(ts_bond *)); |
|
114 |
vtx2->data->bond=(ts_bond **)realloc(vtx2->data->bond, vtx2->data->bond_no*sizeof(ts_bond *)); |
|
115 |
vtx1->data->bond[vtx1->data->bond_no-1]=bond; |
|
116 |
vtx2->data->bond[vtx2->data->bond_no-1]=bond; |
|
117 |
return TS_SUCCESS; |
|
118 |
} |
|
119 |
|
|
120 |
ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){ |
|
121 |
ts_bool retval; |
|
122 |
retval=vtx_add_neighbour(vtx1,vtx2); |
|
123 |
if(retval==TS_SUCCESS) |
|
124 |
retval=vtx_add_bond(blist,vtx1,vtx2); |
|
125 |
return retval; |
|
126 |
} |
|
127 |
|
9802f1
|
128 |
/*TODO: write and optimize this urgently before use! */ |
SP |
129 |
ts_bool vtx_remove_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex |
|
130 |
*vtx2){ |
|
131 |
// ts_bool retval; |
|
132 |
/* remove the bond */ |
|
133 |
//retval=vtx_remove_bond(blist,vtx1,vtx2); |
|
134 |
/* remove the vertices */ |
|
135 |
return TS_SUCCESS; |
|
136 |
} |
a10dd5
|
137 |
|
d7639a
|
138 |
|
SP |
139 |
|
737714
|
140 |
ts_bool vtx_data_free(ts_vertex_data *data){ |
SP |
141 |
if(data->neigh!=NULL) free(data->neigh); |
|
142 |
if(data->tristar!=NULL) free(data->tristar); |
|
143 |
if(data->bond!=NULL) free(data->bond); |
|
144 |
if(data->cell!=NULL) free(data->cell); |
|
145 |
free(data); |
|
146 |
return TS_SUCCESS; |
|
147 |
} |
|
148 |
|
a10dd5
|
149 |
/*not usable. can be deleted */ |
737714
|
150 |
ts_bool vtx_free(ts_vertex *vtx){ |
SP |
151 |
vtx_data_free(vtx->data); |
|
152 |
free(vtx); |
d7639a
|
153 |
return TS_SUCCESS; |
SP |
154 |
} |
|
155 |
|
73f967
|
156 |
ts_bool vtx_list_free(ts_vertex_list *vlist){ |
SP |
157 |
int i; |
|
158 |
for(i=0;i<vlist->n;i++){ |
a10dd5
|
159 |
vtx_data_free(vlist->vtx[i]->data); |
73f967
|
160 |
} |
a10dd5
|
161 |
free(*(vlist->vtx)); |
737714
|
162 |
free(vlist->vtx); |
73f967
|
163 |
free(vlist); |
SP |
164 |
return TS_SUCCESS; |
|
165 |
} |
d7639a
|
166 |
|
bb77ca
|
167 |
inline ts_double vtx_distance_sq(ts_vertex *vtx1, ts_vertex *vtx2){ |
d7639a
|
168 |
ts_double dist; |
bb77ca
|
169 |
ts_vertex_data *vd1=vtx1->data, *vd2=vtx2->data; |
d7639a
|
170 |
#ifdef TS_DOUBLE_DOUBLE |
bb77ca
|
171 |
dist=pow(vd1->x-vd2->x,2) + pow(vd1->y-vd2->y,2) + pow(vd1->z-vd2->z,2); |
d7639a
|
172 |
#endif |
SP |
173 |
#ifdef TS_DOUBLE_LONGDOUBLE |
bb77ca
|
174 |
dist=powl(vd1->x-vd2->x,2) + powl(vd1->y-vd2->y,2) + powl(vd1->z-vd2->z,2); |
d7639a
|
175 |
#endif |
SP |
176 |
#ifdef TS_DOUBLE_FLOAT |
bb77ca
|
177 |
dist=powf(vd1->x-vd2->x,2) + powf(vd1->y-vd2->y,2) + powf(vd1->z-vd2->z,2); |
d7639a
|
178 |
#endif |
SP |
179 |
return(dist); |
|
180 |
} |
bb77ca
|
181 |
|
7958e9
|
182 |
|
SP |
183 |
|
|
184 |
ts_bool vtx_set_global_values(ts_vesicle *vesicle){ |
|
185 |
ts_double xk=vesicle->bending_rigidity; |
|
186 |
ts_uint i; |
|
187 |
for(i=0;i<vesicle->vlist->n;i++){ |
|
188 |
vesicle->vlist->vtx[i]->data->xk=xk; |
|
189 |
} |
|
190 |
return TS_SUCCESS; |
|
191 |
} |
|
192 |
|
|
193 |
inline ts_double vtx_direct(ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){ |
|
194 |
ts_double dX2=vtx2->data->x-vtx1->data->x; |
|
195 |
ts_double dY2=vtx2->data->y-vtx1->data->y; |
|
196 |
ts_double dZ2=vtx2->data->z-vtx1->data->z; |
|
197 |
ts_double dX3=vtx3->data->x-vtx1->data->x; |
|
198 |
ts_double dY3=vtx3->data->y-vtx1->data->y; |
|
199 |
ts_double dZ3=vtx3->data->z-vtx1->data->z; |
|
200 |
ts_double direct=vtx1->data->x*(dY2*dZ3 -dZ2*dY3)+ |
|
201 |
vtx1->data->y*(dZ2*dX3-dX2*dZ3)+ |
|
202 |
vtx1->data->z*(dX2*dY3-dY2*dX3); |
|
203 |
return(direct); |
|
204 |
} |
|
205 |
|
|
206 |
|
|
207 |
inline ts_bool vertex_add_tristar(ts_vertex *vtx, ts_triangle *tristarmem){ |
|
208 |
vtx->data->tristar_no++; |
|
209 |
vtx->data->tristar=(ts_triangle **)realloc(vtx->data->tristar,vtx->data->tristar_no*sizeof(ts_triangle *)); |
|
210 |
if(vtx->data->tristar==NULL){ |
|
211 |
fatal("Reallocation of memory while adding tristar failed.",3); |
|
212 |
} |
|
213 |
vtx->data->tristar[vtx->data->tristar_no-1]=tristarmem; |
|
214 |
return TS_SUCCESS; |
|
215 |
} |
|
216 |
|