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 |
|
|
7 |
ts_bool init_triangle_list(ts_triangle_list *tlist){ |
|
8 |
tlist->n = 0; |
|
9 |
tlist->triangle=NULL; |
|
10 |
return TS_SUCCESS; |
|
11 |
} |
|
12 |
|
|
13 |
ts_bool clear_triangle_values(ts_triangle *triang,ts_uint idx){ |
|
14 |
triang->idx=idx; |
|
15 |
triang->neigh_no=0; |
|
16 |
triang->vertex[0]=NULL; |
|
17 |
triang->vertex[1]=NULL; |
|
18 |
triang->vertex[2]=NULL; |
|
19 |
triang->neigh=NULL; |
|
20 |
triang->xnorm=0; |
|
21 |
triang->ynorm=0; |
|
22 |
triang->znorm=0; |
|
23 |
return TS_SUCCESS; |
|
24 |
} |
|
25 |
|
|
26 |
ts_bool triangle_add(ts_triangle_list *tlist, ts_vertex *vtx1, ts_vertex *vtx2, ts_vertex *vtx3){ |
|
27 |
tlist->n++; |
|
28 |
tlist->triangle=realloc(tlist->triangle,tlist->n*sizeof(ts_triangle)); |
|
29 |
if(tlist->triangle==NULL) fatal("Cannot reallocate memory for additional ts_triangle.",5); |
|
30 |
clear_triangle_values(&tlist->triangle[tlist->n-1],tlist->n-1); |
|
31 |
//NOW insert vertices! |
|
32 |
tlist->triangle[tlist->n - 1].idx=tlist->n; |
|
33 |
tlist->triangle[tlist->n - 1].vertex[0]=vtx1; |
|
34 |
tlist->triangle[tlist->n - 1].vertex[1]=vtx2; |
|
35 |
tlist->triangle[tlist->n - 1].vertex[2]=vtx3; |
|
36 |
return TS_SUCCESS; |
|
37 |
} |
|
38 |
|
|
39 |
|
|
40 |
ts_bool triangle_add_neighbour(ts_triangle *tria, ts_triangle *ntria){ |
|
41 |
// int i; |
|
42 |
//fprintf(stderr,"Sosedi so:\n"); |
|
43 |
// for(i=0;i<tria->neigh_no;i++) |
|
44 |
// fprintf(stderr,"\t %d\n",tria->neigh[i]); |
|
45 |
|
|
46 |
|
|
47 |
tria->neigh_no++; |
|
48 |
//We need to reallocate space! The pointer *neight must be zero if not |
|
49 |
//having neighbours yet (if neigh_no was 0 at thime of calling |
|
50 |
tria->neigh=realloc(tria->neigh,tria->neigh_no*sizeof(ts_triangle *)); |
|
51 |
if(tria->neigh == NULL){ |
|
52 |
fatal("Reallocation of memory failed during insertion of triangle neighbour in triangle_add_neighbour",3); |
|
53 |
} |
|
54 |
tria->neigh[tria->neigh_no-1]=ntria; |
|
55 |
// fprintf(stderr,"dodajamo soseda %d!\n",ntria); |
|
56 |
|
|
57 |
//fprintf(stderr,"Sosedi so:\n"); |
|
58 |
// for(i=0;i<tria->neigh_no;i++) |
|
59 |
// fprintf(stderr,"\t %d\n",tria->neigh[i]); |
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
return TS_SUCCESS; |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
ts_bool triangle_remove_neighbour(ts_triangle *tria, ts_triangle *ntria){ |
|
68 |
ts_uint i,j=0; |
|
69 |
/* fprintf(stderr,"Sosedi so:\n"); |
|
70 |
for(i=0;i<tria->neigh_no;i++) |
|
71 |
fprintf(stderr,"%d, ",tria->neigh[i]); |
|
72 |
fprintf(stderr,"\n"); |
|
73 |
*/ |
|
74 |
|
|
75 |
for(i=0;i<tria->neigh_no;i++){ |
|
76 |
if(tria->neigh[i]!=ntria){ |
|
77 |
tria->neigh[j]=tria->neigh[i]; |
|
78 |
j++; |
|
79 |
|
|
80 |
} |
|
81 |
// else { |
|
82 |
// fprintf(stderr,"was here once\n"); |
|
83 |
|
|
84 |
// } |
|
85 |
} |
|
86 |
|
|
87 |
// fprintf(stderr,"Sosedi so:\n"); |
|
88 |
// for(i=0;i<tria->neigh_no;i++) |
|
89 |
// fprintf(stderr,"\t %d\n",tria->neigh[i]); |
|
90 |
|
|
91 |
// fprintf(stderr,"iscemo soseda %d!\n",ntria); |
|
92 |
if(j==i) fatal("In triangle_remove_neighbour: Specified neighbour does not exist for given triangle",3); |
|
93 |
// fprintf(stderr,"old nuber of neigh=%i\n",tria->neigh_no); |
|
94 |
tria->neigh_no--; |
|
95 |
// fprintf(stderr,"new nuber of neigh=%i\n",tria->neigh_no); |
|
96 |
tria->neigh=realloc(tria->neigh,tria->neigh_no*sizeof(ts_triangle *)); |
|
97 |
if(tria->neigh == NULL){ |
|
98 |
fatal("Reallocation of memory failed during insertion of vertex neighbour in triangle_remove_neighbour",3); |
|
99 |
} |
|
100 |
return TS_SUCCESS; |
|
101 |
} |
|
102 |
|
|
103 |
ts_bool triangle_normal_vector(ts_triangle *tria){ |
|
104 |
ts_double x21,x31,y21,y31,z21,z31,xden; |
|
105 |
x21=tria->vertex[1]->x - tria->vertex[0]->x; |
|
106 |
x31=tria->vertex[2]->x - tria->vertex[0]->x; |
|
107 |
y21=tria->vertex[1]->y - tria->vertex[0]->y; |
|
108 |
y31=tria->vertex[2]->y - tria->vertex[0]->y; |
|
109 |
z21=tria->vertex[1]->z - tria->vertex[0]->z; |
|
110 |
z31=tria->vertex[2]->z - tria->vertex[0]->z; |
|
111 |
|
|
112 |
tria->xnorm=y21*z31 - z21*y31; |
|
113 |
tria->ynorm=z21*x31 - x21*z31; |
|
114 |
tria->znorm=x21*y31 - y21*x31; |
|
115 |
xden=tria->xnorm*tria->xnorm + tria->ynorm*tria->ynorm + tria->znorm*tria->znorm; |
|
116 |
#ifdef TS_DOUBLE_DOUBLE |
|
117 |
xden=sqrt(xden); |
|
118 |
#endif |
|
119 |
#ifdef TS_DOUBLE_FLOAT |
|
120 |
xden=sqrtf(xden); |
|
121 |
#endif |
|
122 |
#ifdef TS_DOUBLE_LONGDOUBLE |
|
123 |
xden=sqrtl(xden); |
|
124 |
#endif |
|
125 |
tria->xnorm=tria->xnorm/xden; |
|
126 |
tria->ynorm=tria->ynorm/xden; |
|
127 |
tria->znorm=tria->znorm/xden; |
|
128 |
return TS_SUCCESS; |
|
129 |
} |
|
130 |
|
|
131 |
|
|
132 |
ts_bool triangle_free(ts_triangle *triang){ |
|
133 |
free(triang->neigh); |
|
134 |
return TS_SUCCESS; |
|
135 |
} |
|
136 |
|
|
137 |
ts_bool triangle_list_free(ts_triangle_list *tlist){ |
|
138 |
int i; |
|
139 |
for(i=0;i<tlist->n;i++){ |
|
140 |
triangle_free(&tlist->triangle[i]); |
|
141 |
} |
|
142 |
free(tlist->triangle); |
|
143 |
return TS_SUCCESS; |
|
144 |
} |
|
145 |
|