TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
geometry.c
Go to the documentation of this file.
1 
24 #include "turtle.h"
25 #include "geometry.h"
26 #include "geometry_utilities.h"
27 #include "geometry_freememory.h"
28 
29 POINT *new_point(long index,double x,double y,double z){
43  POINT *P;
44 
45  P=(POINT *)malloc((sizeof(POINT)));
46  if (!P) t_error("point was not allocated");
47  P->index=index;
48  P->x=x;
49  P->y=y;
50  P->z=z;
51  //P->attributes=(attribute_point *)malloc((size_t)(sizeof(attribute_point)));
52  //if (!P) t_error("attribute_point was not allocated");
53  //P->attributes=attributes;
54 
55  return P;
56 
57 
58 }
59 
60 
61 
62 LINE *new_line_from_points(long index, POINT *P1,POINT *P2){
75  LINE *L;
76  L=(LINE *)malloc((sizeof(LINE)));
77  if (!L) t_error("line was not allocated");
78  L->index=index;
79 
80  L->begin=new_point(P1->index,P1->x,P1->y,P1->z);
81  L->end=new_point(P2->index,P2->x,P2->y,P2->z);
82 // L->begin_point_index=P1->index;
83 // L->end_point_index=P2->index;
84 // L->attributes=(attribute_line *)malloc(sizeof(attribute_line));
85 // if (!L->attributes) t_error("attribute_line was not allocated");
86 // L->attributes=attributes;
87  L->length2d=distance2D(P1,P2);
88 
89 
90  return L;
91 
92 }
93 
101  LINEVECTOR *lv;
102 
103  lv=(LINEVECTOR *)malloc(sizeof(LINEVECTOR));
104  if (!lv) t_error("Linevector struct was not allocated");
105 
106  lv->isdynamic=isDynamic;
107  lv->nl=NL;
108  lv->nh=nh;
109 
110 
111  lv->element=(LINE **)malloc((size_t)((nh-NL+1+NR_END)*sizeof(LINE *)));
112  if (!lv->element) t_error("Linevector element struct was not allocated");
113 
114  return lv;
115 
116 }
117 
118 
126  POLYGONVECTOR *pv;
127 
128  pv=(POLYGONVECTOR *)malloc(sizeof(POLYGONVECTOR));
129  if (!pv) t_error("Polygonvector struct was not allocated");
130 
131  pv->isdynamic=isDynamic;
132  pv->nl=NL;
133  pv->nh=nh;
134 
135 
136  pv->element=(POLYGON **)malloc((size_t)((nh-NL+1+NR_END)*sizeof(POLYGON *)));
137  if (!pv) t_error("Polygonvector element struct was not allocated");
138 
139  return pv;
140 
141 }
142 
143 
144 
156  POLYGON *PO;
157  LONGVECTOR *lvertices;
158  long i,j;
159  //l,s,se,a;
160  double area;
161 
162  PO=(POLYGON *)malloc(sizeof(POLYGON));
163  if (!PO) t_error("polygon was not allocated");
164 
165  PO->centroid=new_point(centroid->index,centroid->x,centroid->y,centroid->z);
166 
167  PO->index=centroid->index;
168 
169 
170 
171 
172 
173  for (i=lines->nl;i<=lines->nh;i++){
174  for (j=lines->nl;j<i;j++){
175  if (segment_intersection_occurence(lines->element[i],lines->element[j])!=0) {
176  printf("Warning:the lines %ld and %ld (polyogon-internal numeration) share an internal point, the polygon %ld cannot be created!! \n",i,j,centroid->index);
177  return PO;
178  }
179  }
180  }
181 
182  lvertices=new_longvector(lines->nh*2);
183 
184 
185  for (i=lines->nl;i<=lines->nh;i++){
186  lvertices->element[2*i-1]=lines->element[i]->begin->index;
187  lvertices->element[2*i]=lines->element[i]->end->index;
188  if (lines->element[i]->end->index==lines->element[i]->begin->index) {
189  printf("Warning:the line %ld has the same extremes in index %ld, the polygon %ld cannot be created!!",i,lines->element[i]->begin->index,centroid->index);
190  return PO;
191  }
192  }
193 
194  // print_longvector_elements(lvertices,1);
195 
196  for (i=lines->nl;i<=lines->nh;i++){
197  if(query_freq_longvector(lvertices,lines->element[i]->begin->index)!=2) {
198  printf("Warning: problem in begin vertex of %ld, (%ld points in the same vertex) the polygon %ld cannot be created!! \n",lines->element[i]->begin->index,query_freq_longvector(lvertices,lines->element[i]->begin->index),centroid->index);
199 
200  return PO;
201  }
202 
203  if(query_freq_longvector(lvertices,lines->element[i]->end->index)!=2) {
204  printf("Warning: problem in end vertex of line %ld, (%ld points in the same vertex) the polygon %ld cannot be created!! \n",lines->element[i]->end->index,query_freq_longvector(lvertices,lines->element[i]->begin->index),centroid->index);
205 
206  return PO;
207  }
208  }
209 
210 /* The polygon can be defined */
211 /* It calculates the area of a polygon */
212 /* It defines the edge of the polygon */
213  area=0.0;
214 
215  PO->edge_indices=new_longvector(lines->nh);
216  for (i=lines->nl;i<=lines->nh;i++){
217  area=area+area2d(centroid,lines->element[i]->begin,lines->element[i]->end);
218 
219  PO->edge_indices->element[i]=lines->element[i]->index;
220 
221 
222  }
223  PO->area2D=area;
224 
225 
226 
227  free_longvector(lvertices);
228 
229 
230 
231 
232  return PO;
233 
234 
235 
236 }
237 
238