TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
geometry_attribute.c
Go to the documentation of this file.
1 
2 /* BGEOMETRY BUILDS THE MESH FROM A RASTER FOR THE BOUSSINESQ MODEL
3 KeyPalette Version 0.9375 KMackenzie
4 
5 file geometry_attribute.c
6 
7 Copyright, 2009 Emanuele Cordano and Riccardo Rigon
8 
9 This file is part of BGEOMETRY.
10  BGEOMETRY is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  BGEOMETRY is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #include "turtle.h"
25 
26 #include "geometry.h"
27 #include "t_datamanipulation.h"
28 #include "geometry_utilities.h"
29 
30 
31 //#include "geometry_io.h"
32 #include "geometry_attribute.h"
33 
34 #define NO_INTERSECTION -99
35 #define NULL_VALUE -99
36 
37 polygon_connection_attributes *get_connection(POLYGON *polygon,POLYGONVECTOR *polygons, long boundary, long displacement ,short print){
54  long l,s,l_po1,l_po2,icnt;
55  double dist;
56  long A_min,A_max;
58  icnt=1;
59 
61  if (!pca) printf("Error: polygon_connection_attributes was not allocated at %ld polygon",polygon->index);
62  pca->connections=new_longvector(polygon->edge_indices->nh);
64  initialize_longvector(pca->connections,boundary);
66  A_min=fmax(polygon->index-displacement,polygons->nl);
67  A_max=fmin(polygon->index+displacement,polygons->nh);
68 
69  for (l=A_min;l<=A_max;l++){
70  if (l!=polygon->index) {
71  dist=1.0;
72  l_po1=0;
73  l_po2=0;
74 
75  s=shared_edges(polygon,polygons->element[l],NO_INTERSECTION,&l_po1,&l_po2,&dist);
76 
77  if (s!=NO_INTERSECTION ){
78  if (l_po1>polygon->edge_indices->nh) printf ("Error: Line %ld : (%ld for polygon %ld) Not coherent data!!! \n ",s,l_po1,polygon->index);
79  pca->connections->element[l_po1]=polygons->element[l]->index;
80  pca->d_connections->element[l_po1]=dist;
81 // printf(" pca conn. %ld %lf",pca->connections->element[l_po1],pca->d_connections->element[l_po1]);
82  }
83  }
84 
85 
86  }
87 
88 
89  return pca;
90 
91 }
92 
93 
94 
95 
96 
97 
106 
108  if (!apv) t_error("polygon_connection_attribute_array was not allocated");
109 
110  apv->isdynamic=isDynamic;
111  apv->nl=NL;
112  apv->nh=nh;
113 
114 
115  apv->element=(polygon_connection_attributes **)malloc((size_t)((nh-NL+1+NR_END)*sizeof(polygon_connection_attributes *)));
116  if (!apv) t_error("polygon_connection_attribute_array element struct was not allocated");
117 
118  return apv;
119 
120 }
121 
122 polygon_connection_attribute_array *get_connection_array(POLYGONVECTOR *polygons, long boundary, long displacement, short print){
123 
140  long l;
141 
142  pca=new_connection_attributes(polygons->nh);
143  printf ("pca Allocated!\n");
144 
145  for(l=pca->nl;l<=pca->nh;l++){
146  pca->element[l]=get_connection(polygons->element[l],polygons,boundary,displacement,print);
147  if (print==1) printf ("Polygons %ld (%ld connections) of %ld (displacement=%ld)!! \n",l,pca->element[l]->connections->nh,pca->nh,displacement);
148  }
149 
150  if (print==1) printf (" Finished polygon_connection_array struct filling!! \n ");
151 
152 
153 
154  return pca;
155 }
156 
157 
172  FILE *fd;
173  long i,nh,nl,l,link_val;
174  double d_link_val;
175  nl=NL;
176  nh=NL;
177  for (i=pca->nl;i<=pca->nh;i++){
178  if (nl>pca->element[i]->connections->nl) nl=pca->element[i]->connections->nl;
179  if (nh<pca->element[i]->connections->nh) nh=pca->element[i]->connections->nh;
180  }
181 
182  fd=fopen(filename,"w");
184  fprintf(fd,"index{%ld}\n",pca->nh);
185  fprintf(fd,"/*!* FILE CONTAINIG NECESSARY INFORMATION FOR POLOYGON CONNECTION \n");
186  fprintf(fd," each polygon connection array is expressed as a double array containing the following information \n");
187  fprintf(fd,"polygon_index ");
188 
189  for (l=nl;l<=nh;l++) {
190  fprintf(fd,"P%ld ",l);
191  fprintf(fd,"d_P%ld ",l);
192  }
193  fprintf(fd," */ \n ");
194 
196  for (i=pca->nl;i<=pca->nh;i++){
197  fprintf(fd,"\n");
198  fprintf(fd,"%ld: double array connections %ld {",i,i);
199  fprintf(fd,"%ld ",i);
200 
201  for (l=nl;l<=nh;l++){
202  link_val=NULL_VALUE;
203  d_link_val=NULL_VALUE;
204  if ((l<=pca->element[i]->connections->nh) && (l>=pca->element[i]->connections->nl)) link_val=pca->element[i]->connections->element[l];
205  if ((l<=pca->element[i]->d_connections->nh) && (l>=pca->element[i]->d_connections->nl)) d_link_val=pca->element[i]->d_connections->element[l];
206  fprintf(fd,",%ld, ",link_val);
207  fprintf(fd,"%lf ",d_link_val);
208  }
209  fprintf(fd,"}");
210  }
211 
212 
213  fprintf(fd,"\n");
214  fclose(fd);
215  return 0;
216 
217 }
218 
219 
233  FILE *fd;
234  long i,nh,nl,l,link_val;
235  double d_link_val;
236  nl=NL;
237  nh=NL;
238  for (i=pca->nl;i<=pca->nh;i++){
239  if (nl>pca->element[i]->connections->nl) nl=pca->element[i]->connections->nl;
240  if (nh<pca->element[i]->connections->nh) nh=pca->element[i]->connections->nh;
241  }
242 
243  fd=fopen(filename,"w");
247  fprintf(fd,"polygon_index ");
248 
249  for (l=nl;l<=nh;l++) {
250  fprintf(fd,"P%ld ",l);
251  fprintf(fd,"d_P%ld ",l);
252  }
254  for (i=pca->nl;i<=pca->nh;i++){
255  fprintf(fd,"\n");
256 
257  fprintf(fd,"%ld ",i);
258 
259  for (l=nl;l<=nh;l++){
260  link_val=NULL_VALUE;
261  d_link_val=NULL_VALUE;
262  if ((l<=pca->element[i]->connections->nh) && (l>=pca->element[i]->connections->nl)) link_val=pca->element[i]->connections->element[l];
263  if ((l<=pca->element[i]->d_connections->nh) && (l>=pca->element[i]->d_connections->nl)) d_link_val=pca->element[i]->d_connections->element[l];
264  fprintf(fd,"%ld ",link_val);
265  fprintf(fd,"%lf ",d_link_val);
266  }
267 
268  }
269 
270 
271  fprintf(fd,"\n");
272  fclose(fd);
273  return 0;
274 
275 }
276 
277 
288  long i,j,kl,c;
289  int s=1,sk;
291  for (i=pca->nl;i<=pca->nh;i++) {
292  for (j=pca->element[i]->connections->nl;j<=pca->element[i]->connections->nh;j++) {
293  if (pca->element[i]->connections->element[j]!=boundary){
294 
295  kl=pca->element[i]->connections->element[j];
296  sk=0;
297  for (c=pca->element[kl]->connections->nl;c<=pca->element[kl]->connections->nh;c++) {
298  if (pca->element[kl]->connections->element[c]==i) sk=1;
299  }
300  if (sk==0) {
301  s=0;
302  printf("Error in pca: struct is not symmetric in the calls %ld , %ld /n",i,kl);
303  }
304  }
305  }
306  }
307 
308  return s;
309 
310 }
311 
312 
313 
314 
315 polygon_connection_attributes *get_connection_squares(POLYGON *polygon,POLYGONVECTOR *polygons, long boundary,long novalue, long r ,long c,LONGMATRIX *mask,short print){
336  long l,s,l_po1,l_po2,icnt;
337  double dist;
338  long r_min,r_max,c_min,c_max;
339  long rs,cs;
340 
341  icnt=1;
342 
344  if (!pca) printf("Error: polygon_connection_attributes was not allocated at %ld polygon",polygon->index);
345  pca->connections=new_longvector(polygon->edge_indices->nh);
347  initialize_longvector(pca->connections,boundary);
349  r_min=fmax(r-1,mask->nrl);
350  r_max=fmin(r+1,mask->nrh);
351  c_min=fmax(c-1,mask->ncl);
352  c_max=fmin(c+1,mask->nch);
353 
354  for (rs=r_min;rs<=r_max;rs++) {
355  for (cs=c_min;cs<=c_max;cs++) {
356  l=mask->element[rs][cs];
357  if ((l!=polygon->index) && (l!=novalue)) {
358  dist=1.0;
359  l_po1=0;
360  l_po2=0;
361 
362  s=shared_edges(polygon,polygons->element[l],NO_INTERSECTION,&l_po1,&l_po2,&dist);
363 
364  if (s!=NO_INTERSECTION ){
365  if (l_po1>polygon->edge_indices->nh) printf ("Error: Line %ld : (%ld for polygon %ld) Not coherent data!!! \n ",s,l_po1,polygon->index);
366  pca->connections->element[l_po1]=polygons->element[l]->index;
367  pca->d_connections->element[l_po1]=dist;
368  // printf(" pca conn. %ld %lf",pca->connections->element[l_po1],pca->d_connections->element[l_po1]);
369  }
370  }
371 
372  }
373  }
374 
375 
376  return pca;
377 
378 }
379 
380 
381 
382 
383 polygon_connection_attribute_array *get_connection_array_squares(POLYGONVECTOR *polygons, long boundary, long novalue, LONGMATRIX *pixels, short print){
384 
402  long l,r,c;
403 
404  pca=new_connection_attributes(polygons->nh);
405  printf ("pca Allocated!\n");
406 
407  for (r=pixels->nrl;r<=pixels->nrh;r++) {
408  for (c=pixels->ncl;c<=pixels->nch;c++){
409  l=pixels->element[r][c];
410  if (l!=novalue) {
411  pca->element[l]=get_connection_squares(polygons->element[l],polygons,boundary,novalue,r,c,pixels,print);
412  if (print==1) printf ("Polygons %ld (%ld connections) of %ld !! \n",l,pca->element[l]->connections->nh,pca->nh);
413  }
414  }
415  }
416  for(l=pca->nl;l<=pca->nh;l++){
417  if (pca->element[l]==NULL) printf("Error: Polygon connections %ld of %ld were not allocated!! \n",l,pca->nh);
418 
419  }
420 
421  if (print==1) printf (" Finished polygon_connection_array struct filling!! \n ");
422 
423 
424 
425  return pca;
426 }