TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
geometry2.c
Go to the documentation of this file.
1 
2 #include "turtle.h"
3 #include "rw_maps.h"
4 #include "geometry.h"
5 #include "geometry_utilities.h"
6 #include "geometry_attribute.h"
7 #include "geometry_freememory.h"
8 #include "geometry_io.h"
9 #include "g_raster2plvector.h"
10 #include "sorting.h"
11 #include "linear_span.h"
12 #include "bigcells2.h"
13 #include "geometry2.h"
14 
24  POINT *p_new;
25 
26  p_new=new_point(point->index,point->x,point->y,point->z);
27 
28  return p_new;
29 }
30 
41  LINE *l_new;
42 // POINT *p_start,*p_end;
43 
44 
45  l_new=new_line_from_points(line->index,line->begin,line->end);
46 // free_line(l_new);
47 // free_line(line);
48 // printf("freed lines");
49 // stop_execution();
50 
51  return l_new;
52 }
53 
54 
64  LINEVECTOR *lines_new;
65  long j;
66 
67  lines_new=new_linevector(lines->nh);
68 
69  for (j=lines->nl;j<=lines->nh;j++){
70  lines_new->element[j]=new_line_from_line(lines->element[j]);
71 
72  }
73 
74  return lines_new;
75 
76 }
77 
85  POLYGON *pol_new;
86  char *function_name="new_polygon_from_polygon";
87  long j;
88 
89  pol_new=(POLYGON *)malloc(sizeof(POLYGON));
90  if (!pol_new) printf("Error in %s , polygon was not allocated \n",function_name);
91 
92  pol_new->index=polygon->index;
93  pol_new->area2D=polygon->area2D;
94 
95  pol_new->centroid=new_point_from_point(polygon->centroid);
96  //linee
97 
98  pol_new->edge_indices=new_longvector(polygon->edge_indices->nh);
99  for (j=pol_new->edge_indices->nl;j<=pol_new->edge_indices->nh;j++) {
100  pol_new->edge_indices->element[j]=polygon->edge_indices->element[j];
101  }
102 
103  return pol_new;
104 
105 }
106 
117  POLYGONVECTOR *polygons_new;
118  long j;
119 
120  polygons_new=new_polygonvector(polygons->nh);
121 
122  for (j=polygons->nl;j<=polygons->nh;j++) {
123  polygons_new->element[j]=new_polygon_from_polygon(polygons->element[j]);
124  }
125 
126  return polygons_new;
127 
128 }
129 
131  /*
132  *
133  *
134  * \param (polygon_connection_attributes *) - polygon_connection_attributes
135  *
136  *\author Emanuele Cordano
137  *\date April 2010
138 
139  */
141  char *function_name="new_connection_from_connection";
142  long j;
143 
145  if (!pc_new) printf("Error in %s : polygon_connection_attributes was not allocated \n",function_name);
146 
149 
150  for(j=pc_new->connections->nl;j<=pc_new->connections->nh;j++) {
151  pc_new->connections->element[j]=pc->connections->element[j];
152  }
153 
154  for(j=pc_new->d_connections->nl;j<=pc_new->d_connections->nh;j++) {
155  pc_new->d_connections->element[j]=pc->d_connections->element[j];
156  }
157 
158 
159  return pc_new;
160 }
161 
162 
173  long j;
175 
176  pca_new=new_connection_attributes(pca->nh);
177 
178  for(j=pca_new->nl;j<=pca_new->nh;j++) {
179  pca_new->element[j]=new_connection_from_connection(pca->element[j]);
180  }
181 
182  return pca_new;
183 
184 }
185 
186 
195  GRID *grid_new;
196  char *function_name="new_grid_from_grid";
197 
198  grid_new=(GRID *)malloc(sizeof(GRID));
199  if (!grid_new) printf("Error in %s, grid was not allocated",function_name);
200 
201  grid_new->lines=new_linevector_from_linevector(grid->lines);
202 
204 // free_polygonvector(grid_new->polygons);
205 // free_polygonvector(grid->polygons);
206 // printf("ba!!");
207 // stop_execution();
208 
210 // free_polygon_connection_attribute_array(grid->links);
211 // free_polygon_connection_attribute_array(grid_new->links);
212 // printf("ba2!!");
213 // stop_execution();
214  grid_new->boundary_indicator=grid->boundary_indicator;
215 
219 // free(grid_new->file_resume_lines);
220 // free(grid->file_resume_lines);
221 // printf("ba24!!");
222 // stop_execution();
223 
224 
225  return grid_new;
226 
227 }
228 
229 
240  LONGBIN *lb;
241  LONGVECTOR *index;
242  long j,r,c,cnt;
243  long initialization_value=-99;
244  char *function_name="new_longbin_from_doublematrix_array";
245 
246  index=new_longvector(lmv->nh);
247 
248  for(j=lmv->nl;j<=lmv->nh;j++) {
249  cnt=0;
250  for(r=lmv->element[j]->nrl;r<=lmv->element[j]->nrh;r++) {
251  for(c=lmv->element[j]->ncl;c<=lmv->element[j]->nch;c++) {
252  if ((lmv->element[j]->element[r][c]>0) && (lmv->element[j]->element[r][c]==lmv->element[j]->element[r][c])) cnt++;
253  }
254  }
255  index->element[j]=cnt;
256  }
257 
258  lb=new_longbin(index);
259  for (j=lb->index->nl;j<=lb->index->nh;j++) {
260  for (r=NL;r<=index->element[j];r++) {
261  lb->element[j][r]=initialization_value;
262  }
263  }
264 
265  for (j=lb->index->nl;j<=lb->index->nh;j++) {
266  cnt=0;
267  for(r=lmv->element[j]->nrl;r<=lmv->element[j]->nrh;r++) {
268  for(c=lmv->element[j]->ncl;c<=lmv->element[j]->nch;c++) {
269  if ((lmv->element[j]->element[r][c]>0) && (lmv->element[j]->element[r][c]==lmv->element[j]->element[r][c])) {
270  cnt++;
271  // andar avanti !!!
272  if (cnt<=lb->index->element[j]) {
273  // printf("Note n %s:filling %ld %ld on %ld res=%ld \n",function_name,j,cnt,lb->index->element[j],lb->element[j][cnt]);
274  lb->element[j][cnt]=lmv->element[j]->element[r][c];
275  // printf("Note n %s:filling %ld %ld on %ld res=%ld \n",function_name,j,cnt,lb->index->element[j],lb->element[j][cnt]);
276  } else {
277  printf("Error in %s : %ld at j=%ld exceeds number of elements %ld \n",function_name,cnt,j,lb->index->element[j]);
278  }
279  }
280  }
281  }
282 
283  }
284  /* verify of the longbin */
285  for (j=lb->index->nl;j<=lb->index->nh;j++) {
286  for (r=NL;r<=index->element[j];r++) {
287  if (lb->element[j][r]==initialization_value) printf("Error in %s: lb[%ld][%ld] was not set correctly \n",function_name,r,j);
288  }
289  }
290  free_longvector(index);
291 
292  return lb;
293 
294 }
295 
305  //LONGVECTOR *index;
306  LONGBIN *lb_new;
307  long r,j;
308 
309  lb_new=new_longbin(lb->index);
310 
311  for(r=lb_new->index->nl;r<=lb_new->index->nh;r++) {
312  for (j=NL;j<=lb_new->index->element[r];j++) {
313  lb_new->element[r][j]=lb->element[r][j];
314  }
315  }
316 
317  return lb_new;
318 }
319 
320 
331  char *function_name="new_double_grid_from_doublesquare_grid";
332 
333  dgrid=(DOUBLE_GRID *)malloc(sizeof(DOUBLE_GRID));
334  if (!dgrid) printf("Error in %s, double_grid was not allocated \n",function_name);
335 
336  dgrid->coarse=new_grid_from_grid(dsq->big->grid);
337  dgrid->fine=new_grid_from_grid(dsq->fine->grid);
338 
339 
342 
343  dgrid->novalue=dsq->fine->novalue;
344 
345  return dgrid;
346 
347 }
348 
349 
360  free_grid(dgrid->coarse);
361  free_grid(dgrid->fine);
364 
365  free(dgrid);
366 }
367 
368 
378  LONGVECTOR *index;
379  LONGBIN *lb_new;
380  long r,j,cnt=0;
381  long novalue=-99;
382  index=new_longvector(lb->index->nh);
383  char *function_name="new_longbin_from_longbin_cleaning_novalues";
384 
385 
386  for(r=lb->index->nl;r<=lb->index->nh;r++) {
387  cnt=lb->index->element[r];
388  index->element[r]=cnt;
389  for (j=NL;j<=lb->index->element[r];j++) {
390  // if (lb->index->element[])
391  // if (lb->index->element[])
392  if (lb->element[r][j]<0) cnt--;
393  }
394  if (cnt<NL) cnt=NL;
395  index->element[r]=cnt;
396 
397 // printf("%ld,%ld \d",lb->index->element[r],index->element[r]);
398 // stop_execution();
399 
400  }
401 
402  lb_new=new_longbin(index);
403 
404  for(r=lb_new->index->nl;r<=lb_new->index->nh;r++) {
405  for (j=NL;j<=lb_new->index->element[r];j++) {
406  lb_new->element[r][j]=novalue;
407  }
408  }
409 
410  for(r=lb_new->index->nl;r<=lb_new->index->nh;r++) {
411  lb_new->element[r][NL]=lb->element[r][NL];
412 
413  if ((lb_new->index->element[r]>NL)) {
414 
415  cnt=NL-1;
416  for (j=NL;j<=lb->index->element[r];j++) {
417  if (lb->element[r][j]>0) {
418  cnt++;
419  if (cnt>lb_new->index->element[r]) {
420  printf("Error in %s counter %ld exceeds bin size %ld at row %ld (%ld) function returns NULL\n",function_name,cnt,lb_new->index->element[r],lb->index->element[r],r);
421  return NULL;
422  }
423  lb_new->element[r][cnt]=lb->element[r][j];
424 
425  }
426 
427  }
428  }
429  }
430 
431  for(r=lb_new->index->nl;r<=lb_new->index->nh;r++) {
432  for (j=NL;j<=lb_new->index->element[r];j++) {
433 // printf("val=%ld val=%ld r=%ld j=%ld \n",lb_new->element[r][j],lb->element[r][j],r,j);
434 // stop_execution();
435  if ((lb_new->element[r][j]<=0) && (lb_new->index->element[r]!=NL)) {
436  printf("Error in %s, negative (null) value at r=%ld j=%ld of (%ld %ld) the created longbin function returns NULL\n",function_name,r,j,lb_new->element[r][j],lb->element[r][j]);
437  stop_execution();
438  // return NULL;
439  }
440 
441  }
442  }
443 
444  free_longvector(index);
445 
446  return lb_new;
447 }
448 
449 
450 
451 long bubble_sort_eleveation(long *cell_index, long nh, DOUBLEVECTOR *elevation) {
466  char *function_name="bubble_sort_eleveation";
467 
468  long i,mk,lk;
469 
470  int swapped=1;
471 
472  long n=nh;
473 
474  for (i=0;i<nh-1;i++) { if ((cell_index[i]<elevation->nl) || (cell_index[i]>elevation->nh)) {
475  printf("Error in %s , cell_index %ld exceeds elevation %ld %ld function return -1 \n",function_name,cell_index[i],elevation->nl,elevation->nh);
476  // stop_execution();
477  return -1;
478  }
479  }
480 
481 
482 // for (i=la->nl;i<=la->nh;i++) {
483 // la->element[i]=i;
484 // }
485 
486 
487  do {
488  swapped=0;
489  n=n-1;
490 
491  for(i=0;i<=n-1;i++){
492 
493 // if (elevation->element[cell_index[i]]>cell_index[i+1]){
494  if (elevation->element[cell_index[i]]<elevation->element[cell_index[i+1]]){
496  mk=cell_index[i];
497  cell_index[i]=cell_index[i+1];
498  cell_index[i+1]=mk;
500  // lk=la->element[i];
501  // la->element[i]=la->element[i+1];
502  // la->element[i+1]=lk;
503  swapped=1;
504  }
505  }
506 // printf ("n=%ld nh=%ld swapped=%d \n",n,nh,swapped);
507 
508  if (n<1 && swapped==1) {
509  printf("Error in %s ; cells were not correctly sorted!! \n",function_name);
510  // print_longvector_elements(v,print);
511  swapped=1;
512  }
513  } while (swapped==1);
514 // stop_execution();
515  return n;
516 
517 }
518 
519 
528  char *function_name="bubble_sort_eleveation_in_longbin";
529  long s;
530  long r;
531 
532  for (r=lb->index->nl;r<=lb->index->nh;r++) {
533  if (lb->index->element[r]>NL) s=bubble_sort_eleveation(&(lb->element[r][NL]),lb->index->element[r],elevation);
534  }
535 
536  return 0;
537 
538 }