commit | author | age
|
d7639a
|
1 |
#include<stdlib.h> |
SP |
2 |
#include "general.h" |
a10dd5
|
3 |
#include "vertex.h" |
d7639a
|
4 |
|
a10dd5
|
5 |
ts_bond_list *init_bond_list(){ |
SP |
6 |
ts_bond_list *blist=(ts_bond_list *)malloc(sizeof(ts_bond_list)); |
d7639a
|
7 |
blist->n=0; |
SP |
8 |
blist->bond=NULL; |
a10dd5
|
9 |
return blist; |
d7639a
|
10 |
} |
SP |
11 |
|
a10dd5
|
12 |
ts_bond *bond_add(ts_bond_list *blist, ts_vertex *vtx1, ts_vertex *vtx2){ |
b01cc1
|
13 |
ts_uint i; |
a10dd5
|
14 |
/* no vertices must be null! */ |
SP |
15 |
if(vtx1==NULL || vtx2==NULL) return NULL; |
b01cc1
|
16 |
/* Verify if the bond already exists... Don't do multiple bonds */ |
SP |
17 |
for(i=0;i<blist->n;i++){ |
e016c4
|
18 |
if((blist->bond[i]->vtx1==vtx1 || blist->bond[i]->vtx1==vtx2) |
SP |
19 |
&&(blist->bond[i]->vtx2==vtx1 || blist->bond[i]->vtx2==vtx2)) |
b01cc1
|
20 |
return blist->bond[i]; |
SP |
21 |
} |
d7639a
|
22 |
blist->n++; |
a10dd5
|
23 |
blist->bond=(ts_bond **)realloc(blist->bond,blist->n*sizeof(ts_bond *)); |
SP |
24 |
if(blist->bond==NULL) fatal("Cannot reallocate memory for additional **ts_bond.",100); |
|
25 |
blist->bond[blist->n-1]=(ts_bond *)malloc(sizeof(ts_bond)); |
|
26 |
if(blist->bond[blist->n-1]==NULL) fatal("Cannot allocate memory for additional *ts_bond.",100); |
|
27 |
|
|
28 |
//NOW insert vertices into data! |
e016c4
|
29 |
blist->bond[blist->n - 1]->vtx1=vtx1; |
SP |
30 |
blist->bond[blist->n - 1]->vtx2=vtx2; |
a63f17
|
31 |
blist->bond[blist->n - 1]->tainted=0; |
a10dd5
|
32 |
//Should we calculate bond length NOW? |
d335d9
|
33 |
|
SP |
34 |
|
|
35 |
|
a10dd5
|
36 |
|
SP |
37 |
return blist->bond[blist->n-1]; |
d7639a
|
38 |
} |
SP |
39 |
|
d335d9
|
40 |
|
SP |
41 |
/* Two triangles are assigned to the bond. Bond must already be initialized. */ |
|
42 |
ts_bool bond_assign_triangles(ts_bond *bond){ |
|
43 |
ts_vertex *vtx1=bond->vtx1, *vtx2=bond->vtx2; |
|
44 |
ts_uint i,j,k; |
d33abe
|
45 |
ts_uint i1=0,i2=0,i3=0,im=0; |
SP |
46 |
ts_triangle *ttria; |
d335d9
|
47 |
for(i=0,k=0;i<vtx1->tristar_no;i++){ |
SP |
48 |
for(j=0;j<3;j++){ |
|
49 |
if(vtx1->tristar[i]->vertex[j]==vtx2){ |
|
50 |
//triangle found; |
|
51 |
bond->tria[k]=vtx1->tristar[i]; |
|
52 |
k++; |
|
53 |
} |
|
54 |
} |
|
55 |
} |
|
56 |
if(k<2) return TS_FAIL; |
d33abe
|
57 |
|
SP |
58 |
//We will sort the triangles so, they are oriented as km and kp for bondflip |
|
59 |
for(i=0;i<3;i++){ |
|
60 |
if(bond->tria[0]->vertex[i]==vtx1){ |
|
61 |
i1=i; |
|
62 |
} |
|
63 |
else if(bond->tria[0]->vertex[i]==vtx2){ |
|
64 |
i2=i; |
|
65 |
} |
|
66 |
else i3=i; |
|
67 |
} |
|
68 |
|
|
69 |
if(i1>i2 || (i1==0 && i2==2)){ |
|
70 |
//pozitivna orientacija! |
|
71 |
ttria=bond->tria[0]; |
|
72 |
bond->tria[0]=bond->tria[1]; |
|
73 |
bond->tria[1]=ttria; |
|
74 |
bond->adjvtx[1]=bond->tria[0]->vertex[i3]; |
|
75 |
im=0; |
|
76 |
} |
|
77 |
else{ |
|
78 |
bond->adjvtx[0]=bond->tria[0]->vertex[i3]; |
|
79 |
im=1; |
|
80 |
} |
|
81 |
|
|
82 |
for(i=0;i<3;i++){ |
|
83 |
if(bond->tria[1]->vertex[i]==vtx1){ |
|
84 |
i1=i; |
|
85 |
} |
|
86 |
else if(bond->tria[1]->vertex[i]==vtx2){ |
|
87 |
i2=i; |
|
88 |
} |
|
89 |
else i3=i; |
|
90 |
} |
|
91 |
bond->adjvtx[im]=bond->tria[1]->vertex[i3]; |
|
92 |
|
|
93 |
|
|
94 |
return TS_SUCCESS; |
d335d9
|
95 |
} |
SP |
96 |
|
d7639a
|
97 |
ts_bool bond_list_free(ts_bond_list *blist){ |
a10dd5
|
98 |
ts_uint i; |
SP |
99 |
for(i=0;i<blist->n;i++){ |
|
100 |
free(blist->bond[i]); |
|
101 |
} |
d7639a
|
102 |
free(blist->bond); |
a10dd5
|
103 |
free(blist); |
d7639a
|
104 |
return TS_SUCCESS; |
SP |
105 |
} |