TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
bigcells2.c
Go to the documentation of this file.
1 /* BGEOMETRY BUILDS THE MESH FROM A RASTER FOR THE BOUSSINESQ MODEL
2 BGEOMETRY Version 0.9375 KMackenzie
3 
4 file bigcells.h
5 
6 Copyright, 2009 Emanuele Cordano and Riccardo Rigon
7 
8 This file is part of BGEOMETRY.
9  BGEOMETRY is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  BGEOMETRY is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "turtle.h"
23 #include "rw_maps.h"
24 #include "geometry.h"
25 #include "geometry_utilities.h"
26 #include "geometry_attribute.h"
27 #include "geometry_freememory.h"
28 #include "geometry_io.h"
29 #include "g_raster2plvector.h"
30 #include "sorting.h"
31 #include "linear_span.h"
32 #include "bigcells2.h"
33 #define BOUNDARY -10
34 #define EWRES 1
35 #define NSRES 2
36 #define X_BOTTOMLEFTCORNER 4
37 #define Y_BOTTOMLEFTCORNER 3
38 #define XY_ORIGIN 0.0
39 #define A_FLAG 2
40 #define INTEGER_NULL -99
41 #define INIT_VALUE2 -990
42 #define MAX_COL 100
43 #define REFERENCE_INDEX 1
44 SQUARE_GRID *get_square_grid(DOUBLEMATRIX *DTM, T_INIT *UV,char *file_resume_lines, char *file_resume_polygons, char *file_resume_connections,long (*index_pixel_from_a_bin)(long r, long c,LONGVECTOR *s_index), int (*check_novalues)(double x, DOUBLEVECTOR *V),long displacement,short print) {
64  SQUARE_GRID *sq;
65  long count,r,c,vl;
66 
67  sq=(SQUARE_GRID *)malloc(sizeof(SQUARE_GRID));
68  if (!sq) t_error("Square Grid sq was not allocated");
69  sq->indices_pixel=m_indices_from_mask(DTM,0,0,INTEGER_NULL,0,index_pixel_from_a_bin,UV->V,(*check_novalues));
70  sq->indices_vertex=m_indices_from_mask(DTM,-1,-1,INTEGER_NULL,0,(*index_pixel_from_a_bin),UV->V,(*check_novalues));
71  sq->indices_horizontal_lines=m_indices_from_mask(DTM,-1,0,INTEGER_NULL,0,(*index_pixel_from_a_bin),UV->V,(*check_novalues));
72  count=0;
73  for(r=sq->indices_horizontal_lines->nrl;r<=sq->indices_horizontal_lines->nrh;r++){
74  for (c=sq->indices_horizontal_lines->ncl;c<=sq->indices_horizontal_lines->nch;c++){
75  if (sq->indices_horizontal_lines->element[r][c]!=INTEGER_NULL) count++;
76  }
77  }
78  sq->nhorizontal_lines=count;
80  sq->indices_vertical_lines=m_indices_from_mask(DTM,0,-1,INTEGER_NULL,count,(*index_pixel_from_a_bin),UV->V,(*check_novalues));
81 
82  sq->grid=(GRID *)malloc(sizeof(GRID));
83  if (!sq->grid) t_error("Grid of a Square Grid was not allocated");
84 
85 
86 
89  if (print==1) printf("number of lines: %ld \n",sq->grid->lines->nh);
91  if (print==1) printf("number of polygons: %ld \n",sq->grid->polygons->nh);
92  // displacement=sq->indices_vertex->nch*2
94 
98  sq->grid->file_resume_lines=copy_string(file_resume_lines);
99  sq->grid->file_resume_polygons=copy_string(file_resume_polygons);
100  sq->grid->file_resume_connections=copy_string(file_resume_connections);
101 // write_grid(sq->grid);
102 
103  return sq;
104 
105 }
106 
107 void write_grid(GRID *grid){
113  int l,s,r;
114 
115  //if (strcmp(filenames->element[O_INFO_LINES]+1,MISSING_FILE))
117  //if (strcmp(filenames->element[O_INFO_POLYGONS]+1,MISSING_FILE))
119  //if (strcmp(filenames->element[O_INFO_CONNECTIONS]+1,MISSING_FILE))
121 
122  printf("write_grid function: write_linevector %s (exit statues %d)\n",grid->file_resume_lines,l);
123  printf("write_grid function: write_polygonvector %s (exit statues %d)\n",grid->file_resume_polygons,s);
124  printf("write_grid function: write_polygonconnectionattributearray %s (exit statues %d)\n",grid->file_resume_connections,r);
125  //r=fprint_polygonconnectionattributearray(grid->file_resume_connections,grid->links);
126 
127 }
128 
141  RASTER_MAP *raster;
142 
143  raster=(RASTER_MAP *)malloc(sizeof(RASTER_MAP));
144  if (!raster) t_error("Raster map was not allocated");
145 
146 
147  raster->nl=NL;
148  raster->nh=nh;
151  raster->UV=(T_INIT *)malloc(sizeof(T_INIT));
152  if (!raster->UV) t_error("Raster map UV was not allocated");
153 
154  raster->layer=(DOUBLEMATRIX **)malloc((size_t)((nh-NL+1+NR_END)*sizeof(DOUBLEMATRIX *)));
155  if (!raster->layer) t_error("Raster layer was not allocated");
156 
157  return raster;
158 
159 }
160 
161 int add_map_to_raster(long index,char *filename, RASTER_MAP *raster, short a,short print) {
178 DOUBLEMATRIX *M;
179 if(print==1) printf("\n raster [%ld] reference_map %ld: \n",index,raster->reference_index_map);
180 
181 if (raster->reference_index_map==INTEGER_NULL) {
182  M=new_doublematrix(2,2);
183  raster->layer[index]=read_map(0,filename,M,raster->UV);
185  raster->reference_index_map=index;
186  if (print==1) printf ("Add_map_to_raster map: map %s was successfully read (index %ld of %ld) (reference index: %ld) (map %ld,%ld)\n",filename,index,raster->nh,raster->reference_index_map,raster->layer[index]->nrh,raster->layer[index]->nch);
187  return 0;
188 } else if (index==raster->reference_index_map) {
189  printf ("Error in add_map_to_raster map index of %s corresponds to the reference map index %ld \n",filename,index);
190 } else {
191  raster->layer[index]=read_map(2,filename,raster->layer[raster->reference_index_map],raster->UV);
192  if (print==1) printf ("Add_map_to_raster map: map %s was successfully read (index %ld of %ld) (reference index: %ld) \n",filename,index,raster->nh,raster->reference_index_map);
193  return 0;
194 }
195 printf ("Error in add_map_to_raster map: map (%s) was not read (index %ld) \n",filename,index);
196 
197 return -1;
198 
199 }
200 
213 
214  vector=(LONGMATRIX_VECTOR *)malloc(sizeof(LONGMATRIX_VECTOR));
215  if (!vector) t_error("Longmatrix_vector map was not allocated");
216 
217 
218  vector->nl=NL;
219  vector->nh=nh;
220 
221  vector->element=(LONGMATRIX **)malloc((size_t)((nh-NL+1+NR_END)*sizeof(LONGMATRIX *)));
222  if (!vector->element) t_error("Longmatrix vector was not allocated");
223  printf("new_longmatrix_vector\n\n ");
224  return vector;
225 
226 
227 }
228 
229 LONGMATRIX_VECTOR *get_fine_indices(LONGMATRIX *fine,LONGMATRIX *coarse,long i_firstcell,long i_lastcell,long novalue,short print) {
246  LONGMATRIX_VECTOR *lco;
247  long l,r,c,rs,cs,rp,cp;
248  long nrsh,ncsh,enrsh,encsh;
249  long i_novalue=novalue-5;
250  long ncells=i_lastcell-i_firstcell+1;
251  lco=new_longmatrix_vector(ncells);
252 
253  nrsh=fine->nrh/coarse->nrh;
254  ncsh=fine->nch/coarse->nch;
255  enrsh=fine->nrh%coarse->nrh;
256  encsh=fine->nch%coarse->nch;
257 
258  if (enrsh!=0) t_error("Error in get_fine_indices: number of rows (coarse and grid) are not multiples");
259  if (encsh!=0) t_error("Error in get_fine_indices: number of rows (coarse and grid) are not multiples");
260 
261 // if (print==1) printf ("get_fine_indices: generation of a LONGMATRIX_VECTOR (ncells=%ld(%ld,%ld)) (small square dimension %ld %ld) (coarse gird %ld %ld)(fine grid %ld %ld)\n",lco->nh,i_firstcell,i_lastcell,nrsh,ncsh,coarse->nrh,coarse->nch,fine->nrh,fine->nch);
262  for(l=lco->nl;l<=lco->nh;l++) {
263  //free(lco->element[l]);// added by Emanuele Cordano on 2 September 2009
264  lco->element[l]=new_longmatrix(nrsh,ncsh);
265  for (r=lco->element[l]->nrl;r<=lco->element[l]->nrh;r++){
266  for (c=lco->element[l]->ncl;c<=lco->element[l]->nch;c++){
267  lco->element[l]->element[r][c]=i_novalue;
268  }
269  }
270  // if (print==1) printf("get_fine_indices: Element %ld of %ld was initialized \n",l,lco->nh);
271  }
272 
273  for(r=fine->nrl;r<=fine->nrh;r++){
274  for(c=fine->ncl;c<=fine->nch;c++){
275  rs=(r-1)/nrsh+1;
276  cs=(c-1)/ncsh+1;
277  rp=(r-1)%nrsh+1;
278  cp=(c-1)%ncsh+1;
279  // if (print==1) printf("get_fine_indices: r=%ld of %ld c=%ld of %ld rs=%ld cs=%ld rp=%ld cp=%ld element=%ld novalue=%ld \n",r,fine->nrh,c,fine->nch,rs,cs,rp,cp,coarse->element[rs][cs],novalue);
280  l=coarse->element[rs][cs];
281  if (l!=novalue) l=l-i_firstcell+1;
282  if ((l!=novalue) && ((l<=lco->nh) && (l>=lco->nl))) lco->element[l]->element[rp][cp]=fine->element[r][c];
283  if ((l!=novalue) && ((l>lco->nh) || (l<lco->nl))) printf("Error in get_fine_indices: incorrect value %ld of %ld for coearse->element[%ld][%ld] r=%ld c=%ld \n",l,lco->nh,rs,cs,rp,cp);
284 
285 
286  }
287  }
288 
289  for(l=lco->nl;l<=lco->nh;l++) {
290  for (r=lco->element[l]->nrl;r<=lco->element[l]->nrh;r++){
291  for (c=lco->element[l]->ncl;c<=lco->element[l]->nch;c++){
292  if (lco->element[l]->element[r][c]==i_novalue) {
293  printf("Error in get_fine_elements element %ld (row=%ld col=%ld) was not assigned (number of elements: %ld)",l,r,c,lco->nh);
294  return lco;
295  }
296  }
297  }
298  }
299 
300  if (print==1) printf("Function get_fine_elements was successfully assigned (number of elements: %ld)",lco->nh);
301 
302  return lco;
303 
304 }
305 
306 LONGBIN *get_fine_line_indices (LONGMATRIX *h_fine,LONGMATRIX *h_coarse,LONGMATRIX *v_fine,LONGMATRIX *v_coarse,long n_horizontal,long n_lines, long novalue,short print){
315 LONGMATRIX_VECTOR *horizontal, *vertical;
316 
317 //long n_lines;
318 LONGVECTOR *index;
319 LONGBIN *lb;
320 
321 long i,l;
323 if (print==1) printf("get_fine_line_indices n_horizonal=%ld \n",n_horizontal);
324 horizontal=get_fine_indices(h_fine,h_coarse,1,n_horizontal,novalue,print);
325 //n_horizontal=horizontal->nh;
326 if (print==1) printf("get_fine_line_indices (vertical) n_horizonal=%ld \n",n_horizontal);
327 vertical=get_fine_indices(v_fine,v_coarse,n_horizontal+1,n_lines,novalue,print);
328 //n_lines=vertical->nh+n_horizontal;
329 
330 index=new_longvector(n_lines);
331 for (l=index->nl;l<=n_horizontal;l++){
332  index->element[l]=horizontal->element[l]->nch;
333 }
334 for (l=n_horizontal+1;l<=index->nh;l++){
335  index->element[l]=vertical->element[l-n_horizontal]->nrh;
336 }
337 
338 lb=new_longbin(index);
339 
340 for(l=lb->index->nl;l<=n_horizontal;l++){
341  for (i=1;i<=lb->index->element[l];i++){
342  lb->element[l][i]=horizontal->element[l]->element[horizontal->element[l]->nrl][i];
343  if (print==1) printf("get_fine_line_indices: horizontal line element [%ld][%ld] = %ld was assigned!\n",l,i,lb->element[l][i]);
344  }
345 
346 }
347 
348 for(l=n_horizontal+1;l<=lb->index->nh;l++) {
349  for (i=1;i<=lb->index->element[l];i++){
350  lb->element[l][i]=vertical->element[l-n_horizontal]->element[i][vertical->element[l-n_horizontal]->ncl];
351  if (print==1) printf("get_fine_line_indices: vertical line element [%ld][%ld] = %ld was assigned!\n",l,i,lb->element[l][i]);
352  }
353 }
354 
355 
356 
357 free_longvector(index);
358 
359 free_longmatrix_vector(vertical);
360 free_longmatrix_vector(horizontal);
361 
362 if (print==1) printf("Function get_fine_elements was successfully assigned (number of elements: %ld)",lb->index->nh);
363 
364 return lb;
365 
366 
367 
368 
369 }
370 
371 
372 DOUBLERASTER_MAP *get_doubleraster_map(long n_coarsemaps, long n_finemaps,char *coarse_mapname, char *fine_mapname,short print){
389  //char *aa;
390 
391  draster=(DOUBLERASTER_MAP *)malloc(sizeof(DOUBLERASTER_MAP));
392  if (!draster) t_error("DOUBLERASTER_MAP was not allocated");
393 
394  draster->coarse=new_raster_map(n_coarsemaps);
395  draster->fine=new_raster_map(n_finemaps);
396 
397 
398  add_map_to_raster(draster->coarse->nl,coarse_mapname,draster->coarse,0,print);
399  add_map_to_raster(draster->fine->nl,fine_mapname,draster->fine,0,print);
400 
401 // if (print==1) printf("\n DOUBLERASTER_MAP was successfully read %ld \n",strlen(coarse_mapname)+1);
402 
403 // strcpy(aa,coarse_mapname);
404 // draster->mapname_coarse=(char *)malloc((strlen(coarse_mapname)+1)*sizeof(char));
405 // memcpy(draster->mapname_coarse,coarse_mapname,strlen(coarse_mapname)+1);
406 // strcpy(draster->mapname_coarse,coarse_mapname);
407 // draster->mapname_fine=(char *)malloc((strlen(fine_mapname)+1)*sizeof(char));
408 // strcpy(draster->mapname_fine,fine_mapname);
409 
410  draster->mapname_coarse=copy_string(coarse_mapname);
411  draster->mapname_fine=copy_string(fine_mapname);
412 
413  if (print==1) printf("\n Doubleraster_map %s (coarse_map) and %s (fine_map) was successfully read \n",draster->mapname_coarse,draster->mapname_fine); // ,coarse_mapname,fine_mapname);
414 // if (print==1) printf("\n DOUBLERASTER_MAP was successfully read: %s \n",aa);
415 
416  return draster;
417 
418 }
419 
420 
421 DOUBLESQUARE_GRID *get_doublesquare_grid(DOUBLERASTER_MAP *draster, char *resume_filenames,long (*index_pixel_from_a_bin_coarse)(long r, long c,LONGVECTOR *s_index),long (*index_pixel_from_a_bin_fine)(long r, long c,LONGVECTOR *s_index),long d_coarse,long d_fine,short print){
438  if (print==1) printf("Function get_doublesquare_grid file_resume: %s \n",resume_filenames);
439  char *file_resume_lines_coarse=join_strings(resume_filenames,"__coarse_lines.txt");
440  char *file_resume_polygons_coarse=join_strings(resume_filenames,"__coarse_polygons.txt");
441  char *file_resume_connections_coarse=join_strings(resume_filenames,"__coarse_connections.txt");
442 
443  char *file_resume_lines_fine=join_strings(resume_filenames,"__fine_lines.txt");
444  char *file_resume_polygons_fine=join_strings(resume_filenames,"__fine_polygons.txt");
445  char *file_resume_connections_fine=join_strings(resume_filenames,"__fine_connections.txt");
446  char *file_resume_c_polygon=join_strings(resume_filenames,"_c_polygon.txt");
447  char *file_resume_c_line=join_strings(resume_filenames,"_c_line.txt");
448 
449  dsq=(DOUBLESQUARE_GRID *)malloc(sizeof(DOUBLESQUARE_GRID));
450  if (!dsq) t_error("DOUBLESQUARE_GRID was not allocated");
451 
452 
453  dsq->big=get_square_grid(draster->coarse->layer[draster->coarse->reference_index_map],draster->coarse->UV,file_resume_lines_coarse,file_resume_polygons_coarse,file_resume_connections_coarse,(*index_pixel_from_a_bin_coarse),draster->coarse->check_novalues,d_coarse,print);
454  dsq->fine=get_square_grid(draster->fine->layer[draster->fine->reference_index_map],draster->fine->UV,file_resume_lines_fine,file_resume_polygons_fine,file_resume_connections_fine,(*index_pixel_from_a_bin_fine),draster->fine->check_novalues,d_fine,print);
455 
458 
459 // strcpy(dsq->file_resume_c_polygon,file_resume_c_polygon);
460 // strcpy(dsq->file_resume_c_line,file_resume_c_line);
461 
462  dsq->file_resume_c_line=copy_string(file_resume_c_line);
463  dsq->file_resume_c_polygon=copy_string(file_resume_c_polygon);
464 
465  if (print==1) printf("Function get_doublesquare_grid was executed successfully! \n");
466 
467  free(file_resume_lines_coarse);
468  free(file_resume_polygons_coarse);
469  free(file_resume_connections_coarse);
470  free(file_resume_lines_fine);
471  free(file_resume_polygons_fine);
472  free(file_resume_connections_fine);
473  free(file_resume_c_polygon);
474  free(file_resume_c_line);
475 
476  return dsq;
477 }
478 
479 
482 int write_longmatrix_vector(LONGMATRIX_VECTOR *lmv,char *filename) {
492  FILE *f;
493  long i;
494 
495  f=t_fopen(filename,"w");
496  fprintf(f,"index{%ld} \n\n",lmv->nh);
497  fprintf(f," /*!* MATRICES OF ''SMALL'' cells */ \n ");
498  for (i=lmv->nl;i<=lmv->nh;i++) {
499  fprintf(f,"\n");
500  fprintf(f,"%ld: long matrix small_cell {%ld,%ld} \n",i,lmv->element[i]->nrh,lmv->element[i]->nch);
502 
503  }
504 
505 
506  t_fclose(f);
507  return 0;
508 
509 }
510 
511 int write_small_lines_indices(LONGBIN *small_lines,char *filename) {
522  FILE *f;
523  long j,c;
524 
525  f=t_fopen(filename,"w");
526  fprintf(f,"index{%ld}\n",small_lines->index->nh);
527  fprintf(f,"/*!* FILE CONTAINIG NECESSARY INFORMATION FOR LINE CONTENTS \n");
528  fprintf(f," each (small line) array for (big) line is expressed as a double array */ \n");
529  for(j=small_lines->index->nl;j<=small_lines->index->nh;j++) {
530  fprintf(f,"%ld:long array big line %ld {",j,j);
531  for(c=NL;c<small_lines->index->element[j];c++){
532  fprintf(f,"%ld,",small_lines->element[j][c]);
533  }
534  fprintf(f,"%ld}\n",small_lines->element[j][small_lines->index->element[j]]);
535  }
536 
537 
538 // write_longbin_elements(f,small_lines,MAX_COL);
539  t_fclose(f);
540 
541  return 0;
542 
543 }
544 
551  write_grid(dsq->big->grid);
552  write_grid(dsq->fine->grid);
555  return 0;
556 }
557 
567  long i;
568 
569  if (!lmv) printf("Error in free_longmatrix_vector: longmatrix_vector was not allocated! /n");
570  if (!lmv->element) printf("Error in free_longmatrix_vector: longmatrix_vector elements were not allocated! /n");
571  for (i=lmv->nl;i<=lmv->nh;i++){
572 
573  if (!lmv->element[i]) printf("Error in free_longmatrix_vector: longmatrix at %ld (of %ld) was not allocated! /n",i,lmv->nh);
574  free_longmatrix(lmv->element[i]);
575  }
576 
577  //free(lmv->element[0]);
578  free(lmv->element);
579  free(lmv);
580 
581 
582 }
583 
584 void free_grid(GRID *grid) {
590  //da fare...
591  if (!grid) printf("Error in free_grid: grid was never allocated! \n");
592  if (!grid->links) printf("Error in free_grid: links (connection) was never allocated! \n");
594  if (!grid->polygons) printf("Error in free_grid: polygons was never allocated! \n");
596  if (!grid->lines) printf("Error in free_grid: lines was never allocated! \n");
597  free_linevector(grid->lines);
598 
599  free(grid->file_resume_connections);
600  free(grid->file_resume_lines);
601  free(grid->file_resume_polygons);
602  free(grid);
603 
604 }
605 
612  if (!sq->grid) printf("Error in free_square_grid: a grid was never allocated!\n");
613  free_grid(sq->grid);
614 
615  if (!sq->indices_horizontal_lines->element) printf("Error in free_square_grid: indices_horizontal_lines was never allocated \n");
617  if (!sq->indices_vertical_lines->element) printf("Error in free_square_grid: indices_vertical_lines was never allocated \n");
619  if (!sq->indices_vertex->element) printf("Error in free_square_grid: indices_vertex was never allocated \n");
621  if (!sq->indices_pixel->element) printf("Error in free_square_grid: indices_pixel was never allocated \n");
623 
624 
625  free(sq);//added on 2 September 2009
626 
627 }
628 
636  long i;
637  free_doublevector(raster->UV->U);
638  free_doublevector(raster->UV->V);
639  free(raster->UV);
640  for(i=raster->nl;i<=raster->nh;i++){
641  if (!(!raster->layer[i]->element)) free_doublematrix(raster->layer[i]);
642  }
643 
644  free(raster->layer);
645  free(raster);
646 
647 
648 
649 }
650 
659  free_raster_map(draster->coarse);
660  free_raster_map(draster->fine);
661  free(draster->mapname_coarse);
662  free(draster->mapname_fine);
663  free(draster);
664 }
665 
673  if (!dsq) printf("Error in free_doublesquare_grid: doublesquare_grid was never allocated! /n");
674  if (!dsq->big) printf("Error in free_doublesquare_grid: coarse (big) grid was never allocated! /n");
675  free_square_grid(dsq->big);
676  if (!dsq->fine) printf("Error in free_doublesquare_grid: fine grid was never allocated! /n");
677  free_square_grid(dsq->fine);
678  if (!dsq->small_content_line) printf("Error in free_doublesquare_grid: small_content_line was never allocated! /n");
680  if (!dsq->small_content_polygon) printf("Error in free_doublesquare_grid: small_content_polygon was never allocated! /n");
682 
683  free(dsq->file_resume_c_line);
684  free(dsq->file_resume_c_polygon);
685 
686  free(dsq);
687 
688 }
689 
692 char *strcpy2(char *dest, const char *src)
693 {
694  const char *p;
695  char *q;
696 
697  for(p = src, q = dest; *p != '\0'; p++, q++)
698  *q = *p;
699 
700  *q = '\0';
701 
702  return dest;
703 }
704 
705 char *copy_string(char *origin){
712 char *dest;
713 
714 //dest=origin;
715 dest=(char *)malloc((size_t)(strlen(origin)+4)*sizeof(char));
716 strcpy(dest,origin);
717 return dest;
718 }
719