1 | /*!\file: DomainOutlineRead.cpp
|
---|
2 | * \brief DomainOutlineRead.c: read the vertex coordinates defined in a domain
|
---|
3 | * outline from Argus (.exp file). The first contour in the file is for
|
---|
4 | * the outside domain outline. The following contours represent holes in
|
---|
5 | * the domain.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <stdio.h>
|
---|
9 | #include "../Alloc/alloc.h"
|
---|
10 | #include "../../include/include.h"
|
---|
11 | #include "../../objects/objects.h"
|
---|
12 | #include "../Exceptions/exceptions.h"
|
---|
13 | #include "../../Container/DataSet.h"
|
---|
14 |
|
---|
15 | int DomainOutlineRead(int* pnprof,int** pprofnvertices,double*** ppprofx,double*** ppprofy,bool** pclosed,char* domainname,bool whole=true){
|
---|
16 |
|
---|
17 |
|
---|
18 | /*indexing: */
|
---|
19 | int i,counter;
|
---|
20 |
|
---|
21 | /*I/O: */
|
---|
22 | FILE* fid=NULL;
|
---|
23 | char chardummy[256];
|
---|
24 | double ddummy;
|
---|
25 |
|
---|
26 | /*output: */
|
---|
27 | int nprof; //number of profiles in the domainname file
|
---|
28 | int* profnvertices=NULL; //array holding the number of vertices for the nprof profiles
|
---|
29 | double** pprofx=NULL; //array of profiles x coordinates
|
---|
30 | double** pprofy=NULL; //array of profiles y coordinates
|
---|
31 | bool* closed=NULL; //array holding closed flags for the nprof profiles
|
---|
32 |
|
---|
33 | /*For each profile: */
|
---|
34 | int n;
|
---|
35 | double* x=NULL;
|
---|
36 | double* y=NULL;
|
---|
37 | bool cl;
|
---|
38 |
|
---|
39 | /*open domain outline file for reading: */
|
---|
40 | if ((fid=fopen(domainname,"r"))==NULL){
|
---|
41 | _error_("%s%s","could not find domain file ",domainname);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /*Do a first pass through the domainname file, to figure out how many profiles
|
---|
45 | *we need to read: */
|
---|
46 | nprof=1;
|
---|
47 | for(;;){
|
---|
48 | fscanf(fid,"%256s %256s\n",chardummy,chardummy);
|
---|
49 | fscanf(fid,"%256s %256s\n",chardummy,chardummy);
|
---|
50 | fscanf(fid,"%256s %256s %256s %256s\n",chardummy,chardummy,chardummy,chardummy);
|
---|
51 | fscanf(fid,"%20u %256s\n",&n,chardummy);
|
---|
52 | fscanf(fid,"%256s %256s %256s %256s %256s\n",chardummy,chardummy,chardummy,chardummy,chardummy);
|
---|
53 | for (i=0;i<n;i++){
|
---|
54 | fscanf(fid,"%20lf %20lf\n",&ddummy,&ddummy);
|
---|
55 | }
|
---|
56 | /*Ok, we have faked one profile reading, check whether we are at the end of the file, otherwise, keep fake reading next profile:*/
|
---|
57 | if (feof(fid)){
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | nprof++;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /*Allocate and initialize all the profiles: */
|
---|
64 | profnvertices=(int*)xmalloc(nprof*sizeof(int));
|
---|
65 | pprofx=(double**)xmalloc(nprof*sizeof(double*));
|
---|
66 | pprofy=(double**)xmalloc(nprof*sizeof(double*));
|
---|
67 | for (i=0;i<nprof;i++){
|
---|
68 | pprofx[i]=NULL;
|
---|
69 | pprofy[i]=NULL;
|
---|
70 | }
|
---|
71 | closed=(bool*)xmalloc(nprof*sizeof(bool));
|
---|
72 |
|
---|
73 | /*Reaset file pointer to beginning of file: */
|
---|
74 | fseek(fid,0,SEEK_SET);
|
---|
75 |
|
---|
76 | /*Start reading profiles: */
|
---|
77 | for(counter=0;counter<nprof;counter++){
|
---|
78 |
|
---|
79 | /*Skip header: */
|
---|
80 | fscanf(fid,"%256s %256s\n",chardummy,chardummy);
|
---|
81 | fscanf(fid,"%256s %256s\n",chardummy,chardummy);
|
---|
82 | fscanf(fid,"%256s %256s %256s %256s\n",chardummy,chardummy,chardummy,chardummy);
|
---|
83 |
|
---|
84 | /*Get number of profile vertices: */
|
---|
85 | fscanf(fid,"%20u %256s\n",&n,chardummy);
|
---|
86 |
|
---|
87 | /*Skip next line: */
|
---|
88 | fscanf(fid,"%256s %256s %256s %256s %256s\n",chardummy,chardummy,chardummy,chardummy,chardummy);
|
---|
89 |
|
---|
90 | /*Allocate vertices: */
|
---|
91 | printf("number of n: %i\n",n);
|
---|
92 | x=(double*)xmalloc(n*sizeof(double));
|
---|
93 | y=(double*)xmalloc(n*sizeof(double));
|
---|
94 |
|
---|
95 |
|
---|
96 | /*Read vertices: */
|
---|
97 | for (i=0;i<n;i++){
|
---|
98 | fscanf(fid,"%20lf %20lf\n",&x[i],&y[i]);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*Now check that we are dealing with open contours: */
|
---|
102 | cl=false;
|
---|
103 | if((x[0]==x[n-1]) && (y[0]==y[n-1])){
|
---|
104 | cl=true;
|
---|
105 | if (!whole) {
|
---|
106 | n=n-1;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*Assign pointers: */
|
---|
111 | profnvertices[counter]=n;
|
---|
112 | pprofx[counter]=x;
|
---|
113 | pprofy[counter]=y;
|
---|
114 | closed[counter]=cl;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*close domain outline file: */
|
---|
118 | fclose(fid);
|
---|
119 |
|
---|
120 | /*Assign output pointers: */
|
---|
121 | *pnprof=nprof;
|
---|
122 | *pprofnvertices=profnvertices;
|
---|
123 | *ppprofx=pprofx;
|
---|
124 | *ppprofy=pprofy;
|
---|
125 | if(pclosed)*pclosed=closed;
|
---|
126 | else xfree((void**)&closed);
|
---|
127 | }
|
---|
128 |
|
---|
129 | DataSet* DomainOutlineRead(char* domainname,bool whole=true){
|
---|
130 |
|
---|
131 | /*indexing: */
|
---|
132 | int i;
|
---|
133 |
|
---|
134 | /*intermediary: */
|
---|
135 | int nprof;
|
---|
136 | int* profnvertices=NULL;
|
---|
137 | double** pprofx=NULL;
|
---|
138 | double** pprofy=NULL;
|
---|
139 |
|
---|
140 | Contour* contour=NULL;
|
---|
141 |
|
---|
142 | /*output: */
|
---|
143 | DataSet* domain=NULL;
|
---|
144 |
|
---|
145 | /*get domain outline from intermediary function:*/
|
---|
146 | DomainOutlineRead(&nprof,&profnvertices,&pprofx, &pprofy, NULL,domainname,whole);
|
---|
147 |
|
---|
148 | double* a=pprofx[0]; for(i=0;i<4;i++)printf("%g\n",a[i]);
|
---|
149 | a=pprofy[0]; for(i=0;i<4;i++)printf("%g\n",a[i]);
|
---|
150 | printf("%i %i\n",profnvertices[0],nprof);
|
---|
151 |
|
---|
152 | /*now create dataset of contours: */
|
---|
153 | domain=new DataSet(0);
|
---|
154 | printf("ok1\n");
|
---|
155 |
|
---|
156 | for(i=0;i<nprof;i++){
|
---|
157 | domain->AddObject(new Contour(i,profnvertices[i],pprofx[i],pprofy[i],1));
|
---|
158 | }
|
---|
159 | printf("ok2\n");
|
---|
160 |
|
---|
161 | return domain;
|
---|
162 | }
|
---|