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; |
bba37c
|
11 |
ts_vertex_list *vlist=(ts_vertex_list *)calloc(1,sizeof(ts_vertex_list)); |
SP |
12 |
|
d7639a
|
13 |
if(N==0){ |
bba37c
|
14 |
vlist->n=0; |
SP |
15 |
vlist->vtx=(ts_vertex **)calloc(1,sizeof(ts_vertex *)); /*Allocate one memory space for vlist anyway */ |
73f967
|
16 |
return vlist; |
d7639a
|
17 |
} |
73f967
|
18 |
|
8f6a69
|
19 |
vlist->vtx=(ts_vertex **)calloc(N,sizeof(ts_vertex *)); |
SP |
20 |
if(vlist->vtx==NULL) |
73f967
|
21 |
fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100); |
a10dd5
|
22 |
for(i=0;i<N;i++) { |
8f6a69
|
23 |
vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex)); |
a10dd5
|
24 |
vlist->vtx[i]->idx=i; |
bba37c
|
25 |
/* initialize Ylm for spherical hamonics DONE in sh.c */ |
SP |
26 |
} |
73f967
|
27 |
vlist->n=N; |
d7639a
|
28 |
return vlist; |
SP |
29 |
} |
|
30 |
|
64c113
|
31 |
|
SP |
32 |
ts_bool vertex_list_add_vtx(ts_vertex_list *vlist, ts_vertex *vtx){ |
|
33 |
|
|
34 |
#ifdef DEBUG |
|
35 |
if(vtx==NULL || vlist==NULL) return TS_FAIL; |
|
36 |
#endif |
|
37 |
if(vlist->list_size < vlist->n+1){ |
|
38 |
vlist->vtx=(ts_vertex **)realloc(vlist->vtx, (vlist->list_size+TS_VLIST_CHUNK)*sizeof(ts_vertex*)); |
|
39 |
if(vlist->vtx==NULL){ |
|
40 |
fatal("Error in vertex_list_add. Could not reallocate memory space.",9999); |
|
41 |
} |
|
42 |
vlist->list_size+=TS_VLIST_CHUNK; |
|
43 |
} |
bba37c
|
44 |
vlist->vtx[vlist->n]=vtx; |
64c113
|
45 |
vlist->n++; |
SP |
46 |
return TS_SUCCESS; |
|
47 |
} |
|
48 |
|
|
49 |
|
|
50 |
/* Idea is to delete vertex by removing it from list. The emply place is then filled in by the last |
|
51 |
vertex in the list. List can then be resized by one -- unless resize is required when max list size - |
|
52 |
real list size > predefined value */ |
|
53 |
ts_bool vertex_list_remove_vtx(ts_vertex_list *vlist, ts_vertex *vtx){ |
|
54 |
#ifdef DEBUG |
|
55 |
if(vtx==NULL || vlist==NULL) return TS_FAIL; |
|
56 |
#endif |
bba37c
|
57 |
ts_uint i; |
64c113
|
58 |
for(i=0; i<vlist->n;i++){ |
SP |
59 |
if(vlist->vtx[i]==vtx){ |
bba37c
|
60 |
vlist->vtx[i]=vlist->vtx[vlist->n-1]; |
64c113
|
61 |
vlist->n--; |
SP |
62 |
if(vlist->list_size-vlist->n > TS_VLIST_CHUNK){ |
|
63 |
vlist->vtx=(ts_vertex **)realloc(vlist->vtx, (vlist->list_size-TS_VLIST_CHUNK)*sizeof(ts_vertex*)); |
|
64 |
if(vlist->vtx==NULL){ |
|
65 |
fatal("Error in vertex_list_add. Could not reallocate memory space.",9999); |
|
66 |
} |
|
67 |
vlist->list_size-=TS_VLIST_CHUNK; |
|
68 |
} |
|
69 |
return TS_SUCCESS; |
|
70 |
} |
|
71 |
} |
|
72 |
return TS_FAIL; |
9802f1
|
73 |
} |
7958e9
|
74 |
|
9802f1
|
75 |
|
SP |
76 |
|
a10dd5
|
77 |
ts_bool vtx_add_bond(ts_bond_list *blist,ts_vertex *vtx1,ts_vertex *vtx2){ |
SP |
78 |
ts_bond *bond; |
|
79 |
bond=bond_add(blist,vtx1,vtx2); |
|
80 |
if(bond==NULL) return TS_FAIL; |
8f6a69
|
81 |
vtx1->bond_no++; |
30ee9c
|
82 |
vtx2->bond_no++; |
3131dc
|
83 |
// vtx2->data->bond_no++; |
a10dd5
|
84 |
|
8f6a69
|
85 |
vtx1->bond=(ts_bond **)realloc(vtx1->bond, vtx1->bond_no*sizeof(ts_bond *)); |
30ee9c
|
86 |
vtx2->bond=(ts_bond **)realloc(vtx2->bond, vtx2->bond_no*sizeof(ts_bond *)); |
3131dc
|
87 |
// vtx2->data->bond=(ts_bond **)realloc(vtx2->data->bond, vtx2->data->bond_no*sizeof(ts_bond *)); |
8f6a69
|
88 |
vtx1->bond[vtx1->bond_no-1]=bond; |
30ee9c
|
89 |
vtx2->bond[vtx2->bond_no-1]=bond; |
8f6a69
|
90 |
// vtx2->ata->bond[vtx2->data->bond_no-1]=bond; |
a10dd5
|
91 |
return TS_SUCCESS; |
SP |
92 |
} |
|
93 |
|
2f8ee7
|
94 |
|
SP |
95 |
ts_bool vtx_add_neighbour(ts_vertex *vtx1, ts_vertex *vtx2){ |
|
96 |
vertex_list_add_vtx(vtx1->neigh, vtx2); |
|
97 |
vertex_list_add_vtx(vtx2->neigh, vtx1); |
|
98 |
return TS_SUCCESS; |
|
99 |
} |
|
100 |
|
|
101 |
ts_bool vtx_add_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){ |
a10dd5
|
102 |
ts_bool retval; |
bba37c
|
103 |
retval=vertex_list_add_vtx(vtx1->neigh, vtx2); |
SP |
104 |
retval=vertex_list_add_vtx(vtx2->neigh, vtx1); |
a10dd5
|
105 |
if(retval==TS_SUCCESS) |
bba37c
|
106 |
retval=vtx_add_bond(blist,vtx1,vtx2); |
SP |
107 |
|
a10dd5
|
108 |
return retval; |
SP |
109 |
} |
|
110 |
|
9802f1
|
111 |
/*TODO: write and optimize this urgently before use! */ |
SP |
112 |
ts_bool vtx_remove_cneighbour(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex |
|
113 |
*vtx2){ |
|
114 |
// ts_bool retval; |
|
115 |
/* remove the bond */ |
|
116 |
//retval=vtx_remove_bond(blist,vtx1,vtx2); |
|
117 |
/* remove the vertices */ |
|
118 |
return TS_SUCCESS; |
|
119 |
} |
a10dd5
|
120 |
|
d7639a
|
121 |
|
737714
|
122 |
ts_bool vtx_free(ts_vertex *vtx){ |
8f6a69
|
123 |
if(vtx->neigh!=NULL) free(vtx->neigh); |
SP |
124 |
if(vtx->tristar!=NULL) free(vtx->tristar); |
|
125 |
if(vtx->bond!=NULL) free(vtx->bond); |
737714
|
126 |
free(vtx); |
d7639a
|
127 |
return TS_SUCCESS; |
SP |
128 |
} |
|
129 |
|
73f967
|
130 |
ts_bool vtx_list_free(ts_vertex_list *vlist){ |
SP |
131 |
int i; |
|
132 |
for(i=0;i<vlist->n;i++){ |
8f6a69
|
133 |
if(vlist->vtx[i]!=NULL) vtx_free(vlist->vtx[i]); |
73f967
|
134 |
} |
8f6a69
|
135 |
//free(*(vlist->vtx)); |
737714
|
136 |
free(vlist->vtx); |
73f967
|
137 |
free(vlist); |
SP |
138 |
return TS_SUCCESS; |
|
139 |
} |
d7639a
|
140 |
|
bb77ca
|
141 |
inline ts_double vtx_distance_sq(ts_vertex *vtx1, ts_vertex *vtx2){ |
d7639a
|
142 |
ts_double dist; |
SP |
143 |
#ifdef TS_DOUBLE_DOUBLE |
8f6a69
|
144 |
dist=pow(vtx1->x-vtx2->x,2) + pow(vtx1->y-vtx2->y,2) + pow(vtx1->z-vtx2->z,2); |
d7639a
|
145 |
#endif |
SP |
146 |
#ifdef TS_DOUBLE_LONGDOUBLE |
8f6a69
|
147 |
dist=powl(vtx1->x-vtx2->x,2) + powl(vtx1->y-vtx2->y,2) + powl(vtx1->z-vtx2->z,2); |
d7639a
|
148 |
#endif |
SP |
149 |
#ifdef TS_DOUBLE_FLOAT |
8f6a69
|
150 |
dist=powf(vtx1->x-vtx2->x,2) + powf(vtx1->y-vtx2->y,2) + powf(vtx1->z-vtx2->z,2); |
d7639a
|
151 |
#endif |
SP |
152 |
return(dist); |
|
153 |
} |
bb77ca
|
154 |
|
7958e9
|
155 |
|
SP |
156 |
|
|
157 |
ts_bool vtx_set_global_values(ts_vesicle *vesicle){ |
|
158 |
ts_double xk=vesicle->bending_rigidity; |
|
159 |
ts_uint i; |
|
160 |
for(i=0;i<vesicle->vlist->n;i++){ |
8f6a69
|
161 |
vesicle->vlist->vtx[i]->xk=xk; |
7958e9
|
162 |
} |
SP |
163 |
return TS_SUCCESS; |
|
164 |
} |
|
165 |
|
|
166 |
inline ts_double vtx_direct(ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){ |
8f6a69
|
167 |
ts_double dX2=vtx2->x-vtx1->x; |
SP |
168 |
ts_double dY2=vtx2->y-vtx1->y; |
|
169 |
ts_double dZ2=vtx2->z-vtx1->z; |
|
170 |
ts_double dX3=vtx3->x-vtx1->x; |
|
171 |
ts_double dY3=vtx3->y-vtx1->y; |
|
172 |
ts_double dZ3=vtx3->z-vtx1->z; |
|
173 |
ts_double direct=vtx1->x*(dY2*dZ3 -dZ2*dY3)+ |
|
174 |
vtx1->y*(dZ2*dX3-dX2*dZ3)+ |
|
175 |
vtx1->z*(dX2*dY3-dY2*dX3); |
7958e9
|
176 |
return(direct); |
SP |
177 |
} |
|
178 |
|
|
179 |
|
|
180 |
inline ts_bool vertex_add_tristar(ts_vertex *vtx, ts_triangle *tristarmem){ |
8f6a69
|
181 |
vtx->tristar_no++; |
SP |
182 |
vtx->tristar=(ts_triangle **)realloc(vtx->tristar,vtx->tristar_no*sizeof(ts_triangle *)); |
|
183 |
if(vtx->tristar==NULL){ |
7958e9
|
184 |
fatal("Reallocation of memory while adding tristar failed.",3); |
SP |
185 |
} |
8f6a69
|
186 |
vtx->tristar[vtx->tristar_no-1]=tristarmem; |
7958e9
|
187 |
return TS_SUCCESS; |
SP |
188 |
} |
|
189 |
|
f74313
|
190 |
|
30ee9c
|
191 |
|
SP |
192 |
/* vtx remove tristar is required in bondflip. */ |
|
193 |
/* TODO: Check whether it is important to keep the numbering of tristar |
|
194 |
* elements in some order or not! */ |
|
195 |
inline ts_bool vtx_remove_tristar(ts_vertex *vtx, ts_triangle *tristar){ |
|
196 |
ts_uint i,j=0; |
|
197 |
for(i=0;i<vtx->tristar_no;i++){ |
|
198 |
if(vtx->tristar[i]!=tristar){ |
|
199 |
vtx->tristar[j]=vtx->tristar[i]; |
|
200 |
j++; |
|
201 |
} |
|
202 |
} |
|
203 |
vtx->tristar_no--; |
|
204 |
vtx->tristar=realloc(vtx->tristar,vtx->tristar_no*sizeof(ts_triangle *)); |
|
205 |
if(vtx->neigh == NULL){ |
|
206 |
fatal("Reallocation of memory failed during insertion of vertex neighbour in vertex_add_neighbour",3); |
|
207 |
} |
|
208 |
return TS_SUCCESS; |
|
209 |
} |
|
210 |
|
|
211 |
|
|
212 |
|
f74313
|
213 |
/* ****************************************************************** */ |
SP |
214 |
/* ***** New vertex copy operations. Inherently they are slow. ***** */ |
|
215 |
/* ****************************************************************** */ |
|
216 |
|
b01cc1
|
217 |
ts_bool vtx_copy(ts_vertex *cvtx, ts_vertex *ovtx){ |
f74313
|
218 |
memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex)); |
8f6a69
|
219 |
cvtx->neigh=NULL; |
SP |
220 |
cvtx->tristar_no=0; |
|
221 |
cvtx->bond_no=0; |
|
222 |
cvtx->tristar=NULL; |
|
223 |
cvtx->bond=NULL; |
|
224 |
cvtx->cell=NULL; |
b01cc1
|
225 |
return TS_SUCCESS; |
f74313
|
226 |
} |
dac2e5
|
227 |
|
SP |
228 |
ts_bool vtx_duplicate(ts_vertex *cvtx, ts_vertex *ovtx){ |
|
229 |
memcpy((void *)cvtx,(void *)ovtx,sizeof(ts_vertex)); |
|
230 |
return TS_SUCCESS; |
|
231 |
} |
|
232 |
|
f74313
|
233 |
//TODO: needs to be done |
SP |
234 |
ts_vertex **vtx_neigh_copy(ts_vertex_list *vlist,ts_vertex *ovtx){ |
|
235 |
return NULL; |
|
236 |
} |
|
237 |
|
dac2e5
|
238 |
|
SP |
239 |
|
f74313
|
240 |
ts_vertex_list *vertex_list_copy(ts_vertex_list *ovlist){ |
SP |
241 |
ts_uint i; |
|
242 |
ts_vertex_list *vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list)); |
b01cc1
|
243 |
vlist=memcpy((void *)vlist, (void *)ovlist, sizeof(ts_vertex_list)); |
f74313
|
244 |
ts_vertex **vtx=(ts_vertex **)malloc(vlist->n*sizeof(ts_vertex *)); |
b01cc1
|
245 |
vlist->vtx=vtx; |
8f6a69
|
246 |
if(vlist->vtx==NULL) |
b01cc1
|
247 |
fatal("Fatal error reserving memory space for vertex list! Could number of requsted vertices be too large?", 100); |
SP |
248 |
for(i=0;i<vlist->n;i++) { |
8f6a69
|
249 |
vlist->vtx[i]=(ts_vertex *)calloc(1,sizeof(ts_vertex)); |
b01cc1
|
250 |
vlist->vtx[i]->idx=i; |
SP |
251 |
vtx_copy(vlist->vtx[i],ovlist->vtx[i]); |
f74313
|
252 |
} |
b01cc1
|
253 |
|
f74313
|
254 |
return vlist; |
SP |
255 |
} |