TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
geometry_io.c
Go to the documentation of this file.
1 
24 #include "turtle.h"
25 #include "geometry.h"
27 #include "geometry_utilities.h"
28 #include "geometry_io.h"
29 #include "geometry_freememory.h"
30 
31 
32 #define DELIMITERS ";"
33 #define MAX_POINTS 20
34 #define NULL_VALUE -99
35 #define NO_ELEVATION 0.0
36 
37 #define NO_COLS_LINEVECTOR_DOUBLEMATRIX 8
38 
39 LINE *get_line(DOUBLEVECTOR *vertex_x_coord, DOUBLEVECTOR *vertex_y_coord, long line_index, char *number_strings, short print){
51  POINT *P1, *P2;
52  //long index1,index2,
53  long j;
54  LONGVECTOR *lvertices;
55  LINE *li;
56 
57  lvertices=read_longarray_from_string(number_strings,DELIMITERS,MAX_POINTS,0);
58 
59  if (lvertices->nh<2) printf("Error:: line %s (%ld) cannot be created, there no vertices!!\n",number_strings,line_index);
60  for (j=lvertices->nl;j<=2;j++){
61  if ((lvertices->element[j]>vertex_x_coord->nh) || (lvertices->element[j]>vertex_y_coord->nh)) {
62  printf ("Error:: line %s (%ld) cannot be created, point %ld has no coordinates or no atributes!!\n",number_strings,line_index,j);
63  }
64  }
65 
66  j=1;
67  P1=new_point(lvertices->element[j],vertex_x_coord->element[lvertices->element[j]],vertex_y_coord->element[lvertices->element[j]],NO_ELEVATION);
68 
69  j=2;
70  P2=new_point(lvertices->element[j],vertex_x_coord->element[lvertices->element[j]],vertex_y_coord->element[lvertices->element[j]],NO_ELEVATION);
71 
72  if (print==1) printf ("The two vertices %ld (x=%lf,y=%lf) and %ld (x=%lf,y=%lf) of line %s (%ld) were succesfully created!!\n",P1->index,P1->x,P1->y,P2->index,P2->x,P2->y,number_strings,line_index);
73 
74  li=new_line_from_points(line_index, P1,P2);
75 
76 
77  free_point(P1);
78  free_point(P2);
79  free_longvector(lvertices);
80 
81 
82  return li;
83 
84 
85 
86 }
87 
88 
89 
90 LINEVECTOR *get_linevector(DOUBLEVECTOR *vertex_x_coord, DOUBLEVECTOR *vertex_y_coord,STRINGBIN *line_strings , short print)
91 {
105  LINEVECTOR *lv;
106  long l;
107 
108 
109  //if (attribute_line_array->nh!=line_strings->index->nh) printf("Error: the line string array and attribute array has different length!!!\n");
110 
111  lv=new_linevector(line_strings->index->nh);
112 
113  for(l=lv->nl;l<=lv->nh;l++){
114  lv->element[l]=get_line(vertex_x_coord,vertex_y_coord,l,line_strings->element[l]+1,print);
115 
116  }
117 
118 
119 
120 
121  return lv;
122 
123 }
124 
135  LINEVECTOR *lve;
136 
137  long j;
138 // print_longvector_elements(nlines,1);
139 
140 
141  lve=new_linevector(nlines->nh);
142 
143  for (j=lve->nl;j<=lve->nh;j++){
144  lve->element[j]=new_line_from_points(nlines->element[j],lines->element[nlines->element[j]]->begin,lines->element[nlines->element[j]]->end);
145 
146  }
147 
148 
149  return lve;
150 
151 }
152 
153 POLYGON *get_polygon(long index,double x, double y, double z, char *edge_index_string,LINEVECTOR *all_lines, short print) {
172  LINEVECTOR *edges;
173  POLYGON *PO;
174  LONGVECTOR *ledges;
175  POINT *centroid;
176 
177  centroid=new_point(index,x,y,z);
178  ledges=read_longarray_from_string(edge_index_string,DELIMITERS,MAX_POINTS,0);
179  edges=extract_linvector_from_linevector(ledges,all_lines);
180  PO=new_polygon_from_a_linevector(edges,centroid);
181 
182  free_point(centroid);
183  free_longvector(ledges);
184  free_linevector(edges);
185 
186  return PO;
187 
188 
189 }
190 
191 POLYGONVECTOR *get_polygonvector(DOUBLEVECTOR *centroid_x_coord, DOUBLEVECTOR *centroid_y_coord,STRINGBIN *polygon_strings ,LINEVECTOR *all_lines,short print){
205 POLYGONVECTOR *pv;
206 long l;
207 
208 if ((centroid_x_coord->nh!=centroid_y_coord->nh) && (polygon_strings->index->nh!=centroid_y_coord->nh) && (polygon_strings->index->nh!=centroid_x_coord->nh)) printf ("Error:: Polygons stringarray (STRINGBIN *) and centroid coordinete vectors have a different number of elements!!\n");
209 pv=new_polygonvector(polygon_strings->index->nh);
210 
211 for(l=pv->nl;l<=pv->nh;l++){
212  pv->element[l]=get_polygon(l,centroid_x_coord->element[l],centroid_y_coord->element[l],NO_ELEVATION,polygon_strings->element[l]+1,all_lines,print);
213 }
214 
215 return pv;
216 
217 }
218 
219 int write_linevector(char *filename, LINEVECTOR *lines){
233  FILE *fd;
234  long i;
235  double xm,ym;
236 
237  fd=fopen(filename,"w");
238  fprintf(fd,"index{1}\n");
239  fprintf(fd,"/*!* FILE CONTAINIG NECESSARY INFORMATION FOR LINES \n");
240  fprintf(fd,"x ");
241  fprintf(fd,"y ");
242 
243  fprintf(fd,"line_index ");
244  fprintf(fd,"lenght2d ");
245  fprintf(fd,"x_P1 ");
246  fprintf(fd,"y_P1 ");
247  fprintf(fd,"x_P2 ");
248  fprintf(fd,"y_P2 \n ");
249  fprintf(fd,"*/ \n");
250  fprintf(fd,"1: double matrix lines information {%ld,%d}",lines->nh,NO_COLS_LINEVECTOR_DOUBLEMATRIX);
251  for (i=lines->nl;i<=lines->nh;i++){
252  // printf("entrato!!! %ld\n",i);
253  fprintf(fd,"\n");
254  xm=(lines->element[i]->begin->x+lines->element[i]->end->x)/2.0;
255  ym=(lines->element[i]->begin->y+lines->element[i]->end->y)/2.0;
256  fprintf(fd,"%lf ",xm);
257  fprintf(fd,"%lf ",ym);
258  fprintf(fd,"%ld ",lines->element[i]->index);
259  fprintf(fd,"%lf ",lines->element[i]->length2d);
260  fprintf(fd,"%lf ",lines->element[i]->begin->x);
261  fprintf(fd,"%lf ",lines->element[i]->begin->y);
262  fprintf(fd,"%lf ",lines->element[i]->end->x);
263  fprintf(fd,"%lf ",lines->element[i]->end->y);
264  }
265 
266 
267  fclose(fd);
268  return 0;
269 }
270 
271 int write_polygonvector(char *filename, POLYGONVECTOR *polygons) {
286  FILE *fd;
287  long i,nh,nl,l,lval;
288  nl=NL;
289  nh=NL;
290  for (i=polygons->nl;i<=polygons->nh;i++){
291  if (nl>polygons->element[i]->edge_indices->nl) nl=polygons->element[i]->edge_indices->nl;
292  if (nh<polygons->element[i]->edge_indices->nh) nh=polygons->element[i]->edge_indices->nh;
293  }
294 
295  fd=fopen(filename,"w");
297  fprintf(fd,"index{%ld}\n",polygons->nh);
298  fprintf(fd,"/*!* FILE CONTAINIG NECESSARY INFORMATION FOR POLOYGONS \n");
299  fprintf(fd," each polygon is expressed as a double array coteining the following information \n");
300  fprintf(fd,"x ");
301  fprintf(fd,"y ");
302 
303  fprintf(fd,"polygon_index ");
304  fprintf(fd,"area2d ");
305  for (l=nl;l<=nh;l++) {
306  fprintf(fd,"L%ld ",l);
307  }
308  fprintf(fd," */ \n");
309 
311  for (i=polygons->nl;i<=polygons->nh;i++){
312  fprintf(fd,"\n/*!* %ld block x ",i);
313  fprintf(fd,"y ");
314 
315  fprintf(fd,"polygon_index ");
316  fprintf(fd,"area2d ");
317  for (l=nl;l<=nh;l++) {
318  fprintf(fd,"L%ld ",l);
319  }
320  fprintf(fd," */ \n");
321  fprintf(fd,"%ld: double array {",i);
322  fprintf(fd,"%lf, ",polygons->element[i]->centroid->x);
323  fprintf(fd,"%lf, ",polygons->element[i]->centroid->y);
324  fprintf(fd,"%ld, ",polygons->element[i]->index);
325  fprintf(fd,"%lf ",polygons->element[i]->area2D);
326  for (l=nl;l<=nh;l++){
327  lval=NULL_VALUE;
328  if ((l<=polygons->element[i]->edge_indices->nh) && (l>=polygons->element[i]->edge_indices->nl)) lval=polygons->element[i]->edge_indices->element[l];
329  fprintf(fd,",%ld ",lval);
330  }
331  fprintf(fd,"}");
332  }
333 
334 
335  fprintf(fd,"\n");
336  fclose(fd);
337  return 0;
338 
339 }
340 
341 int fprint_linevector(char *filename, LINEVECTOR *lines){
355  FILE *fd;
356  long i;
357  double xm,ym;
358 
359  fd=fopen(filename,"w");
360  fprintf(fd,"x ");
361  fprintf(fd,"y ");
362 
363  fprintf(fd,"line_index ");
364  fprintf(fd,"lenght2d ");
365  fprintf(fd,"x_P1 ");
366  fprintf(fd,"y_P1 ");
367  fprintf(fd,"x_P2 ");
368  fprintf(fd,"y_P2 ");
369 
370 
371  for (i=lines->nl;i<=lines->nh;i++){
372 // printf("entrato!!! %ld\n",i);
373  fprintf(fd,"\n");
374  xm=(lines->element[i]->begin->x+lines->element[i]->end->x)/2.0;
375  ym=(lines->element[i]->begin->y+lines->element[i]->end->y)/2.0;
376  fprintf(fd,"%lf ",xm);
377  fprintf(fd,"%lf ",ym);
378  fprintf(fd,"%ld ",lines->element[i]->index);
379  fprintf(fd,"%lf ",lines->element[i]->length2d);
380  fprintf(fd,"%lf ",lines->element[i]->begin->x);
381  fprintf(fd,"%lf ",lines->element[i]->begin->y);
382  fprintf(fd,"%lf ",lines->element[i]->end->x);
383  fprintf(fd,"%lf ",lines->element[i]->end->y);
384  }
385 
386 
387  fclose(fd);
388  return 0;
389 
390 }
391 
392 int fprint_polygonvector(char *filename, POLYGONVECTOR *polygons) {
405  FILE *fd;
406  long i,nh,nl,l,lval;
407  nl=NL;
408  nh=NL;
409  for (i=polygons->nl;i<=polygons->nh;i++){
410  if (nl>polygons->element[i]->edge_indices->nl) nl=polygons->element[i]->edge_indices->nl;
411  if (nh<polygons->element[i]->edge_indices->nh) nh=polygons->element[i]->edge_indices->nh;
412  }
413 
414  fd=fopen(filename,"w");
418  fprintf(fd,"x ");
419  fprintf(fd,"y ");
420 
421  fprintf(fd,"polygon_index ");
422  fprintf(fd,"area2d ");
423  for (l=nl;l<=nh;l++) {
424  fprintf(fd,"L%ld ",l);
425  }
427  for (i=polygons->nl;i<=polygons->nh;i++){
428  fprintf(fd,"\n");
429  fprintf(fd,"%lf ",polygons->element[i]->centroid->x);
430  fprintf(fd,"%lf ",polygons->element[i]->centroid->y);
431  fprintf(fd,"%ld ",polygons->element[i]->index);
432  fprintf(fd,"%lf ",polygons->element[i]->area2D);
433  for (l=nl;l<=nh;l++){
434  lval=NULL_VALUE;
435  if ((l<=polygons->element[i]->edge_indices->nh) && (l>=polygons->element[i]->edge_indices->nl)) lval=polygons->element[i]->edge_indices->element[l];
436  fprintf(fd,"%ld ",lval);
437  }
438 
439  }
440 
441 
442  fprintf(fd,"\n");
443  fclose(fd);
444  return 0;
445 
446 }