forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkXYZMolReader.cxx
345 lines (310 loc) · 7.75 KB
/
vtkXYZMolReader.cxx
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*=========================================================================
Program: Visualization Toolkit
Module: vtkXYZMolReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkXYZMolReader.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkIdTypeArray.h"
#include "vtkPolyData.h"
#include <sys/stat.h>
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkXYZMolReader);
//----------------------------------------------------------------------------
vtkXYZMolReader::vtkXYZMolReader()
{
this->TimeStep = 0;
this->MaxTimeStep = 0;
}
//----------------------------------------------------------------------------
vtkXYZMolReader::~vtkXYZMolReader()
{
}
//----------------------------------------------------------------------------
char* vtkXYZMolReader::GetNextLine(FILE* fp, char* line, int maxlen)
{
int cc;
int len;
int comment;
char* ptr;
do
{
comment = 0;
if ( !fgets(line, maxlen, fp) )
{
//cout << "Problem when reading. EOF?" << endl;
return 0;
}
len = static_cast<int>(strlen(line));
for ( cc = 0; cc < len; cc ++ )
{
int ch = line[cc];
if ( ch == '#' )
{
comment = 1;
break;
}
else if ( ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r' )
{
break;
}
}
if ( cc == len )
{
comment = 1;
}
}
while ( comment );
//cout << "Have line that is not a comment: [" << line << "]" << endl;
len = static_cast<int>(strlen(line));
int ft = 0;
ptr = line;
for ( cc = 0; cc < len; cc ++ )
{
int ch = line[cc];
if ( !ft && ( ch == ' ' || ch == '\t' ) )
{
ptr++;
}
else if ( ch == '#' || ch == '\n' || ch == '\r' )
{
line[cc] = 0;
break;
}
else
{
ft = 1;
}
}
if ( strlen(ptr) == 0 )
{
return 0;
}
return ptr;
}
//----------------------------------------------------------------------------
int vtkXYZMolReader::GetLine1(const char* line, int *cnt)
{
char dummy[1024] = "";
if ( !line || sscanf(line, "%d%s", cnt, dummy) < 1)
{
return 0;
}
int cc;
for ( cc = 0; cc < static_cast<int>(strlen(dummy)); ++cc )
{
if ( dummy[cc] != ' ' && dummy[cc] != '\t' && dummy[cc] != '\n' &&
dummy[cc] != '\r' )
{
return 0;
}
}
return 1;
}
//----------------------------------------------------------------------------
int vtkXYZMolReader::GetLine2(const char* line, char *name)
{
char dummy[1024] = "";
if ( !line || sscanf(line, "%s%s", name, dummy) < 1)
{
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
int vtkXYZMolReader::GetAtom(const char* line, char* atom, float *x)
{
//cout << "Lookinf for atom: " << line << endl;
char dummy[1024] = "";
if ( !line || sscanf(line, "%s %f %f %f%s", atom, x, x+1, x+2, dummy) < 4)
{
return 0;
}
int cc;
for ( cc = 0; cc < static_cast<int>(strlen(dummy)); ++cc )
{
if ( dummy[cc] != ' ' && dummy[cc] != '\t' && dummy[cc] != '\n' &&
dummy[cc] != '\r' )
{
return 0;
}
}
return 1;
}
//----------------------------------------------------------------------------
void vtkXYZMolReader::InsertAtom(const char* atom, float *pos)
{
this->Points->InsertNextPoint(pos);
this->AtomType->InsertNextValue(this->MakeAtomType(atom));
}
//----------------------------------------------------------------------------
int vtkXYZMolReader::CanReadFile(const char* name)
{
if ( !name )
{
return 0;
}
// First make sure the file exists. This prevents an empty file
// from being created on older compilers.
struct stat fs;
if(stat(name, &fs) != 0)
{
return 0;
}
FILE* fp = fopen(name, "r");
if ( !fp )
{
return 0;
}
int valid = 0;
const int maxlen = 1024;
char buffer[maxlen];
char comment[maxlen];
char atom[maxlen];
char* lptr;
int num = 0;
float pos[3];
lptr = this->GetNextLine(fp, buffer, maxlen);
if ( this->GetLine1(lptr, &num) )
{
// Have header
lptr = this->GetNextLine(fp, buffer, maxlen);
if ( this->GetLine2(lptr, comment) )
{
lptr = this->GetNextLine(fp, buffer, maxlen);
if ( this->GetAtom(lptr, atom, pos) )
{
valid = 3;
}
}
else if ( this->GetAtom(lptr, atom, pos) )
{
valid = 3;
}
}
else
{
// No header
lptr = this->GetNextLine(fp, buffer, maxlen);
if ( this->GetAtom(lptr, atom, pos) )
{
valid = 3;
}
}
fclose(fp);
return valid;
}
//----------------------------------------------------------------------------
void vtkXYZMolReader::ReadSpecificMolecule(FILE *fp)
{
const int maxlen = 1024;
char buffer[maxlen];
char comment[maxlen];
char* lptr;
int have_header = 0;
int num = 0;
int cnt = 0;
int ccnt = 0;
int rcnt = 0;
int timestep = 1;
int selectstep = this->TimeStep;
float pos[3];
char atom[maxlen];
this->AtomType->Allocate(1024);
this->Points->Allocate(1024);
while ( (lptr = this->GetNextLine(fp, buffer, maxlen)) )
{
if ( ( cnt == 0 || ccnt == num ) && this->GetLine1(lptr, &num) )
{
have_header = 1;
vtkDebugMacro("Have header. Number of atoms is: " << num);
ccnt = 0;
if ( cnt > 0 )
{
timestep ++;
}
}
else if ( have_header )
{
if ( this->GetAtom(lptr, atom, pos) )
{
//cout << "Found atom: " << atom << endl;
if ( ccnt >= num )
{
vtkErrorMacro("Expecting " << num << " atoms, found: " << ccnt);
return;
}
else
{
if ( selectstep == timestep -1 )
{
// Got atom with full signature
//cout << "Insert atom: " << atom << endl;
this->InsertAtom(atom, pos);
rcnt ++;
}
ccnt ++;
}
}
else if ( ccnt == 0 && this->GetLine2(lptr, comment) )
{
vtkDebugMacro("Have comment");
}
else
{
vtkErrorMacro("Expecting atom, got: " << lptr);
return;
}
}
else
{
if ( this->GetAtom(lptr, atom, pos) )
{
// Got atom with simple signature
this->InsertAtom(atom, pos);
rcnt ++;
}
else
{
vtkErrorMacro("Expecting atom, got: " << lptr);
return;
}
}
++ cnt;
}
// Just some more checking and cleanups
if ( num == 0 )
{
num = rcnt;
}
this->AtomType->Squeeze();
this->Points->Squeeze();
if ( selectstep >= timestep )
{
this->NumberOfAtoms = 0;
vtkErrorMacro("Only have " << timestep << " time step(s)");
return;
}
vtkDebugMacro("Number of atoms: " << num << " (" << rcnt << ")");
if ( num != rcnt )
{
this->NumberOfAtoms = 0;
vtkErrorMacro("Expecting " << num << " atoms, got " << rcnt);
return;
}
this->SetMaxTimeStep(timestep);
this->NumberOfAtoms = num;
}
//----------------------------------------------------------------------------
void vtkXYZMolReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "TimeStep: " << this->TimeStep << endl;
os << indent << "MaxTimeStep: " << this->MaxTimeStep;
}