1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
#if defined(_MSC_VER) /* MSVC Compiler */
#pragma warning ( disable : 4786 )
#endif
#include <float.h>
#include <stdio.h>
#include <qtextstream.h>
#include "qwt3d_surfaceplot.h"
#include "qwt3d_io_reader.h"
using namespace std;
using namespace Qwt3D;
const char* NativeReader::magicstring = "jk:11051895-17021986";
namespace
{
FILE* open(QString fname)
{
FILE* file = fopen(QWT3DLOCAL8BIT(fname), "r");
if (!file)
{
fprintf(stderr, "NativeReader::read: cannot open data file \"%s\"\n", QWT3DLOCAL8BIT(fname));
}
return file;
}
int read_char (FILE * fp, bool skipcomments = true)
{
int c;
if ((c = fgetc (fp)) == EOF)
return (c);
if (skipcomments)
{
if (c == '#')
{
do
{
if ((c = fgetc (fp)) == EOF)
return (c);
}
while (c != '\n' && c != '\r');
}
}
return (c);
}
char* read_field (FILE * fp, bool skipcomments = true)
{
static char buf[71];
int c, i;
do
{
if ((c = read_char (fp,skipcomments)) == EOF)
return (NULL);
}
while (isspace (c));
for (i = 0; i < 70 && !isspace (c); ++i)
{
buf[i] = c;
if ((c = read_char (fp,skipcomments)) == EOF)
break;
}
buf[i] = '\0';
return (buf);
}
//! set to data begin
bool extract_info(FILE* fp, unsigned int& xmesh, unsigned int& ymesh, double& xmin, double& xmax, double& ymin, double& ymax)
{
char* p;
// find out the size
if ((p = read_field (fp)) == 0)
return false;
xmesh = (unsigned int)atoi(p);
if ((p = read_field (fp)) == 0)
return false;
ymesh = (unsigned int)atoi (p);
if (xmesh < 1 || ymesh < 1)
return false;
// ... and the limits
if ((p = read_field (fp)) == 0)
return false;
xmin = atof (p);
if ((p = read_field (fp)) == 0)
return false;
xmax = atof (p);
if ((p = read_field (fp)) == 0)
return false;
ymin = atof (p);
if ((p = read_field (fp)) == 0)
return false;
ymax = atof (p);
if (xmin > xmax || ymin > ymax)
return false;
return true;
}
//! find out what the magic string is and compare
bool check_magic(FILE* fp, const char* val)
{
char* p;
if ((p = read_field (fp,false)) == 0)
return false;
if (strcmp (p, val ) != 0)
return false;
return true;
}
//! find out what the type is
bool check_type(FILE* fp, const char* val)
{
char* p;
if ((p = read_field (fp)) == 0)
return false;
if (strcmp (p, val ) != 0)
return false;
return true;
}
double** allocateData(int columns, int rows)
{
double** data = new double* [columns] ;
for ( int i = 0; i < columns; ++i)
{
data[i] = new double [rows];
}
return data;
}
void deleteData(double**data, int columns)
{
for ( int i = 0; i < columns; i++)
{
delete [] data[i];
}
delete [] data;
}
}
NativeReader::NativeReader()
: minz_(-DBL_MAX), maxz_(DBL_MAX)
{
}
bool NativeReader::collectInfo(FILE*& file, QString const& fname, unsigned& xmesh, unsigned& ymesh,
double& minx, double& maxx, double& miny, double& maxy)
{
if (fname.isEmpty())
return false;
file = open(fname);
if (!file)
return false;
if (
(!check_magic(file, magicstring))
||(!check_type(file, "MESH"))
||(!extract_info(file, xmesh, ymesh, minx, maxx, miny, maxy))
)
{
fclose(file);
return false;
}
return true;
}
bool NativeReader::operator()(Plot3D* plot, QString const& fname)
{
FILE* file;
unsigned int xmesh, ymesh;
double minx, maxx, miny, maxy;
if ( !collectInfo(file, fname, xmesh, ymesh, minx, maxx, miny, maxy) )
return false;
/* allocate some space for the mesh */
double** data = allocateData(xmesh, ymesh);
for (unsigned int j = 0; j < ymesh; j++)
{
for (unsigned int i = 0; i < xmesh; i++)
{
if (fscanf(file, "%lf", &data[i][j]) != 1)
{
fprintf(stderr, "NativeReader::read: error in data file \"%s\"\n", QWT3DLOCAL8BIT(fname));
return false;
}
if (data[i][j] > maxz_)
data[i][j] = maxz_;
else if (data[i][j] < minz_)
data[i][j] = minz_;
}
}
/* close the file */
fclose(file);
((SurfacePlot*)plot)->loadFromData(data, xmesh, ymesh, minx, maxx, miny, maxy);
deleteData(data,xmesh);
return true;
}
|