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; |
3131dc
|
55 |
/* This was a bug in creating DIPYRAMID (the neighbours were not in right |
SP |
56 |
* order). |
|
57 |
*/ |
d7639a
|
58 |
/* pa se sosedu dodamo vertex */ |
SP |
59 |
/*if it is already a neighbour don't add it to the list */ |
3131dc
|
60 |
/* |
737714
|
61 |
for(i=0; i<nvtx->data->neigh_no;i++){ |
SP |
62 |
if(nvtx->data->neigh[i]==vtx) return TS_FAIL; |
d7639a
|
63 |
} |
a10dd5
|
64 |
nn=++nvtx->data->neigh_no; |
737714
|
65 |
nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh, nn*sizeof(ts_vertex *)); |
a10dd5
|
66 |
nvtx->data->neigh[nn-1]=vtx; |
3131dc
|
67 |
*/ |
d7639a
|
68 |
|
SP |
69 |
return TS_SUCCESS; |
|
70 |
} |
a10dd5
|
71 |
|
9802f1
|
72 |
/* TODO: optimize this. test this. */ |
SP |
73 |
ts_bool vtx_remove_neighbour(ts_vertex *vtx, ts_vertex *nvtx){ |
|
74 |
/* find a neighbour */ |
|
75 |
/* remove it from the list while shifting remaining neighbours up */ |
|
76 |
ts_uint i,j=0; |
|
77 |
for(i=0;i<vtx->data->neigh_no;i++){ |
|
78 |
if(vtx->data->neigh[i]!=nvtx){ |
|
79 |
vtx->data->neigh[j]=vtx->data->neigh[i]; |
|
80 |
j++; |
|
81 |
} |
|
82 |
} |
|
83 |
/* resize memory. potentionally time consuming */ |
|
84 |
vtx->data->neigh_no--; |
|
85 |
vtx->data->neigh=(ts_vertex **)realloc(vtx->data->neigh,vtx->data->neigh_no*sizeof(ts_vertex *)); |
|
86 |
if(vtx->data->neigh == NULL && vtx->data->neigh_no!=0) |
|
87 |
fatal("Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100); |
|
88 |
|
|
89 |
/* repeat for the neighbour */ |
|
90 |
/* find a neighbour */ |
|
91 |
/* remove it from the list while shifting remaining neighbours up */ |
|
92 |
for(i=0;i<nvtx->data->neigh_no;i++){ |
|
93 |
if(nvtx->data->neigh[i]!=vtx){ |
|
94 |
nvtx->data->neigh[j]=nvtx->data->neigh[i]; |
|
95 |
j++; |
|
96 |
} |
|
97 |
} |
|
98 |
/* resize memory. potentionally time consuming. */ |
|
99 |
nvtx->data->neigh_no--; |
|
100 |
nvtx->data->neigh=(ts_vertex **)realloc(nvtx->data->neigh,nvtx->data->neigh_no*sizeof(ts_vertex *)); |
|
101 |
if(nvtx->data->neigh == NULL && nvtx->data->neigh_no!=0) |
|
102 |
fatal("Reallocation of memory failed during removal of vertex neighbour in vtx_remove_neighbour",100); |
|
103 |
|
|
104 |
return TS_SUCCESS; |
|
105 |
} |
7958e9
|
106 |
|
9802f1
|
107 |
|
SP |
108 |
|
a10dd5
|
109 |
ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2){ |
SP |
110 |
ts_bond *bond; |
|
111 |
bond=bond_add(blist,vtx1,vtx2); |
|
112 |
if(bond==NULL) return TS_FAIL; |
|
113 |
vtx1->data->bond_no++; |
3131dc
|
114 |
// vtx2->data->bond_no++; |
a10dd5
|
115 |
|
SP |
116 |
vtx1->data->bond=(ts_bond **)realloc(vtx1->data->bond, vtx1->data->bond_no*sizeof(ts_bond *)); |
3131dc
|
117 |
// vtx2->data->bond=(ts_bond **)realloc(vtx2->data->bond, vtx2->data->bond_no*sizeof(ts_bond *)); |
a10dd5
|
118 |
vtx1->data->bond[vtx1->data->bond_no-1]=bond; |
3131dc
|
119 |
// vtx2->data->bond[vtx2->data->bond_no-1]=bond; |
a10dd5
|
120 |
return TS_SUCCESS; |
SP |
121 |
} |
|
122 |
|
|
123 |
ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){ |
|
124 |
ts_bool retval; |
|
125 |
retval=vtx_add_neighbour(vtx1,vtx2); |
|
126 |
if(retval==TS_SUCCESS) |
|
127 |
retval=vtx_add_bond(blist,vtx1,vtx2); |
|
128 |
return retval; |
|
129 |
} |
|
130 |
|
9802f1
|
131 |
/*TODO: write and optimize this urgently before use! */ |
SP |
132 |
ts_bool vtx_remove_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex |
|
133 |
*vtx2){ |
|
134 |
// ts_bool retval; |
|
135 |
/* remove the bond */ |
|
136 |
//retval=vtx_remove_bond(blist,vtx1,vtx2); |
|
137 |
/* remove the vertices */ |
|
138 |
return TS_SUCCESS; |
|
139 |
} |
a10dd5
|
140 |
|
d7639a
|
141 |
|
SP |
142 |
|
737714
|
143 |
ts_bool vtx_data_free(ts_vertex_data *data){ |
SP |
144 |
if(data->neigh!=NULL) free(data->neigh); |
|
145 |
if(data->tristar!=NULL) free(data->tristar); |
|
146 |
if(data->bond!=NULL) free(data->bond); |
dac2e5
|
147 |
//Cells are freed separately. |
SP |
148 |
// if(data->cell!=NULL) free(data->cell); |
737714
|
149 |
free(data); |
SP |
150 |
return TS_SUCCESS; |
|
151 |
} |
|
152 |
|
a10dd5
|
153 |
/*not usable. can be deleted */ |
737714
|
154 |
ts_bool vtx_free(ts_vertex *vtx){ |
SP |
155 |
vtx_data_free(vtx->data); |
|
156 |
free(vtx); |
d7639a
|
157 |
return TS_SUCCESS; |
SP |
158 |
} |
|
159 |
|
73f967
|
160 |
ts_bool vtx_list_free(ts_vertex_list *vlist){ |
SP |
161 |
int i; |
|
162 |
for(i=0;i<vlist->n;i++){ |
b01cc1
|
163 |
if(vlist->vtx[i]->data!=NULL) vtx_data_free(vlist->vtx[i]->data); |
73f967
|
164 |
} |
a10dd5
|
165 |
free(*(vlist->vtx)); |
737714
|
166 |
free(vlist->vtx); |
73f967
|
167 |
free(vlist); |
SP |
168 |
return TS_SUCCESS; |
|
169 |
} |
d7639a
|
170 |
|
bb77ca
|
171 |
inline ts_double vtx_distance_sq(ts_vertex *vtx1, ts_vertex *vtx2){ |
d7639a
|
172 |
ts_double dist; |
bb77ca
|
173 |
ts_vertex_data *vd1=vtx1->data, *vd2=vtx2->data; |
d7639a
|
174 |
#ifdef TS_DOUBLE_DOUBLE |
bb77ca
|
175 |
dist=pow(vd1->x-vd2->x,2) + pow(vd1->y-vd2->y,2) + pow(vd1->z-vd2->z,2); |
d7639a
|
176 |
#endif |
SP |
177 |
#ifdef TS_DOUBLE_LONGDOUBLE |
bb77ca
|
178 |
dist=powl(vd1->x-vd2->x,2) + powl(vd1->y-vd2->y,2) + powl(vd1->z-vd2->z,2); |
d7639a
|
179 |
#endif |
SP |
180 |
#ifdef TS_DOUBLE_FLOAT |
bb77ca
|
181 |
dist=powf(vd1->x-vd2->x,2) + powf(vd1->y-vd2->y,2) + powf(vd1->z-vd2->z,2); |
d7639a
|
182 |
#endif |
SP |
183 |
return(dist); |
|
184 |
} |
bb77ca
|
185 |
|
7958e9
|
186 |
|
SP |
187 |
|
|
188 |
ts_bool vtx_set_global_values(ts_vesicle *vesicle){ |
|
189 |
ts_double xk=vesicle->bending_rigidity; |
|
190 |
ts_uint i; |
|
191 |
for(i=0;i<vesicle->vlist->n;i++){ |
|
192 |
vesicle->vlist->vtx[i]->data->xk=xk; |
|
193 |
} |
|
194 |
return TS_SUCCESS; |
|
195 |
} |
|
196 |
|
|
197 |
inline ts_double vtx_direct(ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){ |
|
198 |
ts_double dX2=vtx2->data->x-vtx1->data->x; |
|
199 |
ts_double dY2=vtx2->data->y-vtx1->data->y; |
|
200 |
ts_double dZ2=vtx2->data->z-vtx1->data->z; |
|
201 |
ts_double dX3=vtx3->data->x-vtx1->data->x; |
|
202 |
ts_double dY3=vtx3->data->y-vtx1->data->y; |
|
203 |
ts_double dZ3=vtx3->data->z-vtx1->data->z; |
|
204 |
ts_double direct=vtx1->data->x*(dY2*dZ3 -dZ2*dY3)+ |
|
205 |
vtx1->data->y*(dZ2*dX3-dX2*dZ3)+ |
|
206 |
vtx1->data->z*(dX2*dY3-dY2*dX3); |
|
207 |
return(direct); |
|
208 |
} |
|
209 |
|
|
210 |
|
|
211 |
inline ts_bool vertex_add_tristar(ts_vertex *vtx, ts_triangle *tristarmem){ |
|
212 |
vtx->data->tristar_no++; |
|
213 |
vtx->data->tristar=(ts_triangle **)realloc(vtx->data->tristar,vtx->data->tristar_no*sizeof(ts_triangle *)); |
|
214 |
if(vtx->data->tristar==NULL){ |
|
215 |
fatal("Reallocation of memory while adding tristar failed.",3); |
|
216 |
} |
|
217 |
vtx->data->tristar[vtx->data->tristar_no-1]=tristarmem; |
|
218 |
return TS_SUCCESS; |
|
219 |
} |
|
220 |
|
f74313
|
221 |
|
SP |
222 |
/* ****************************************************************** */ |
|
223 |
/* ***** New vertex copy operations. Inherently they are slow. ***** */ |
|
224 |
/* ****************************************************************** */ |
|
225 |
|
b01cc1
|
226 |
ts_bool vtx_copy(ts_vertex *cvtx, ts_vertex *ovtx){ |
f74313
|
227 |
memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex)); |
SP |
228 |
cvtx->data=(ts_vertex_data *)malloc(sizeof(ts_vertex_data)); |
|
229 |
memcpy((void *)cvtx->data,(void *)ovtx->data,sizeof(ts_vertex_data)); |
|
230 |
cvtx->data->neigh=NULL; |
|
231 |
cvtx->data->neigh_no=0; |
|
232 |
cvtx->data->tristar_no=0; |
|
233 |
cvtx->data->bond_no=0; |
|
234 |
cvtx->data->tristar=NULL; |
|
235 |
cvtx->data->bond=NULL; |
b01cc1
|
236 |
cvtx->data->cell=NULL; |
SP |
237 |
return TS_SUCCESS; |
f74313
|
238 |
} |
dac2e5
|
239 |
|
SP |
240 |
ts_bool vtx_duplicate(ts_vertex *cvtx, ts_vertex *ovtx){ |
|
241 |
memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex)); |
|
242 |
cvtx->data=(ts_vertex_data *)malloc(sizeof(ts_vertex_data)); |
|
243 |
memcpy((void *)cvtx->data,(void *)ovtx->data,sizeof(ts_vertex_data)); |
|
244 |
return TS_SUCCESS; |
|
245 |
} |
|
246 |
|
f74313
|
247 |
//TODO: needs to be done |
SP |
248 |
ts_vertex **vtx_neigh_copy(ts_vertex_list *vlist,ts_vertex *ovtx){ |
|
249 |
return NULL; |
|
250 |
} |
|
251 |
|
dac2e5
|
252 |
|
SP |
253 |
|
f74313
|
254 |
ts_vertex_list *vertex_list_copy(ts_vertex_list *ovlist){ |
SP |
255 |
ts_uint i; |
|
256 |
ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list)); |
b01cc1
|
257 |
vlist=memcpy((void *)vlist, (void *)ovlist, sizeof(ts_vertex_list)); |
f74313
|
258 |
ts_vertex **vtx=(ts_vertex **)malloc(vlist->n*sizeof(ts_vertex *)); |
b01cc1
|
259 |
ts_vertex *tlist=(ts_vertex *)calloc(vlist->n,sizeof(ts_vertex)); |
SP |
260 |
vlist->vtx=vtx; |
|
261 |
if(vlist->vtx==NULL || tlist==NULL) |
|
262 |
fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100); |
|
263 |
for(i=0;i<vlist->n;i++) { |
|
264 |
vlist->vtx[i]=&tlist[i]; |
|
265 |
vlist->vtx[i]->idx=i; |
|
266 |
vtx_copy(vlist->vtx[i],ovlist->vtx[i]); |
f74313
|
267 |
} |
b01cc1
|
268 |
|
f74313
|
269 |
return vlist; |
SP |
270 |
} |