forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkCMLMoleculeReader.cxx
334 lines (279 loc) · 7.75 KB
/
vtkCMLMoleculeReader.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCMLMoleculeReader.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 "vtkCMLMoleculeReader.h"
#include "vtkDataObject.h"
#include "vtkExecutive.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMolecule.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPeriodicTable.h"
#include "vtkXMLParser.h"
#include <string>
#include <vector>
// Subclass of vtkXMLParser -- definitions at end of file
class vtkCMLParser : public vtkXMLParser
{
public:
vtkTypeMacro(vtkCMLParser, vtkXMLParser);
static vtkCMLParser * New();
vtkSetObjectMacro(Target, vtkMolecule);
vtkGetObjectMacro(Target, vtkMolecule);
protected:
vtkCMLParser();
~vtkCMLParser() override;
void StartElement(const char *name, const char **attr) override;
void EndElement(const char *name) override;
std::vector<std::string> AtomNames;
vtkMolecule *Target;
void NewMolecule(const char **attr);
void NewAtom(const char **attr);
void NewBond(const char **attr);
vtkNew<vtkPeriodicTable> pTab;
private:
vtkCMLParser(const vtkCMLParser&) = delete;
void operator=(const vtkCMLParser&) = delete;
};
vtkStandardNewMacro(vtkCMLMoleculeReader);
//----------------------------------------------------------------------------
vtkCMLMoleculeReader::vtkCMLMoleculeReader()
: FileName(nullptr)
{
this->SetNumberOfInputPorts(0);
}
//----------------------------------------------------------------------------
vtkCMLMoleculeReader::~vtkCMLMoleculeReader()
{
this->SetFileName(nullptr);
}
//----------------------------------------------------------------------------
vtkMolecule *vtkCMLMoleculeReader::GetOutput()
{
return vtkMolecule::SafeDownCast(this->GetOutputDataObject(0));;
}
//----------------------------------------------------------------------------
void vtkCMLMoleculeReader::SetOutput(vtkMolecule *output)
{
this->GetExecutive()->SetOutputData(0, output);
}
int vtkCMLMoleculeReader::RequestData(
vtkInformation *,
vtkInformationVector **,
vtkInformationVector *outputVector)
{
vtkMolecule *output = vtkMolecule::SafeDownCast
(vtkDataObject::GetData(outputVector));
if (!output)
{
vtkErrorMacro(<<"vtkCMLMoleculeReader does not have a vtkMolecule "
"as output.");
return 1;
}
vtkCMLParser *parser = vtkCMLParser::New();
parser->SetDebug(this->GetDebug());
parser->SetFileName(this->FileName);
parser->SetTarget(output);
if (!parser->Parse())
{
vtkWarningMacro(<<"Cannot parse file " << this->FileName << " as CML.");
parser->Delete();
return 1;
}
parser->Delete();
return 1;
}
int vtkCMLMoleculeReader::FillOutputPortInformation(int, vtkInformation *info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMolecule");
return 1;
}
void vtkCMLMoleculeReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//
// vtkCMLParser Methods
//
vtkStandardNewMacro(vtkCMLParser);
vtkCMLParser::vtkCMLParser()
: vtkXMLParser(),
Target(nullptr)
{
}
vtkCMLParser::~vtkCMLParser()
{
this->SetTarget(nullptr);
}
void vtkCMLParser::StartElement(const char *name, const char **attr)
{
if (strcmp(name, "atom") == 0)
{
this->NewAtom(attr);
}
else if (strcmp(name, "bond") == 0)
{
this->NewBond(attr);
}
else if (strcmp(name, "molecule") == 0)
{
this->NewMolecule(attr);
}
else if (this->GetDebug())
{
std::string desc;
desc += "Unhandled CML Element. Name: ";
desc += name;
desc += "\n\tAttributes:";
int attrIndex = 0;
while (const char * cur = attr[attrIndex])
{
if (attrIndex > 0)
{
desc.push_back(' ');
}
desc += cur;
++attrIndex;
}
vtkDebugMacro(<<desc);
}
}
void vtkCMLParser::EndElement(const char *)
{
}
void vtkCMLParser::NewMolecule(const char **)
{
this->Target->Initialize();
}
void vtkCMLParser::NewAtom(const char **attr)
{
vtkAtom atom = this->Target->AppendAtom();
int attrInd = 0;
unsigned short atomicNum = 0;
float pos[3];
const char * id = nullptr;
while (const char * cur = attr[attrInd])
{
// Get atomic number
if (strcmp(cur, "elementType") == 0)
{
const char *symbol = attr[++attrInd];
atomicNum = pTab->GetAtomicNumber(symbol);
}
// Get position
else if (strcmp(cur, "x3") == 0)
pos[0] = atof(attr[++attrInd]);
else if (strcmp(cur, "y3") == 0)
pos[1] = atof(attr[++attrInd]);
else if (strcmp(cur, "z3") == 0)
pos[2] = atof(attr[++attrInd]);
// string id / names
else if (strcmp(cur, "id") == 0)
id = attr[++attrInd];
else
{
vtkDebugMacro(<< "Unhandled atom attribute: " << cur);
}
++attrInd;
}
atom.SetAtomicNumber(atomicNum);
atom.SetPosition(pos);
// Store name for lookups
size_t atomId = static_cast<size_t>(atom.GetId());
if (atomId >= this->AtomNames.size())
{
this->AtomNames.resize(atomId + 1);
}
this->AtomNames[atomId] = std::string(id);
vtkDebugMacro(<< "Added atom #" << atomId << " ('" << id << "') ");
}
void vtkCMLParser::NewBond(const char **attr)
{
int attrInd = 0;
vtkIdType atomId1 = -1;
vtkIdType atomId2 = -1;
unsigned short order = 0;
while (const char * cur = attr[attrInd])
{
// Get names of bonded atoms
if (strcmp(cur, "atomRefs2") == 0)
{
char atomRefs[128];
strncpy(atomRefs, attr[++attrInd], 128);
// Parse out atom names:
const char *nameChar = strtok(atomRefs, " ");
while (nameChar != nullptr)
{
vtkIdType currentAtomId;
bool found = false;
for (currentAtomId = 0;
currentAtomId < static_cast<vtkIdType>(this->AtomNames.size());
++currentAtomId)
{
if (this->AtomNames[currentAtomId].compare(nameChar) == 0)
{
found = true;
break;
}
}
if (!found)
{
// Create list of known atom names:
std::string allAtomNames;
for (size_t i = 0; i < this->AtomNames.size(); ++i)
{
allAtomNames += this->AtomNames[i];
allAtomNames.push_back(' ');
}
vtkWarningMacro(<< "NewBond(): unknown atom name '"
<< nameChar << "'. Known atoms:\n"
<< allAtomNames.c_str());
nameChar = strtok(nullptr, " ");
continue;
}
else if (atomId1 == -1)
{
atomId1 = currentAtomId;
}
else if (atomId2 == -1)
{
atomId2 = currentAtomId;
}
else
{
vtkWarningMacro(<< "NewBond(): atomRef2 string has >2 atom names: "
<< atomRefs);
}
nameChar = strtok(nullptr, " ");
}
}
// Get bond order
else if (strcmp(cur, "order") == 0)
{
order = static_cast<unsigned short>(atoi(attr[++attrInd]));
}
else
{
vtkDebugMacro(<< "Unhandled bond attribute: " << cur);
}
++attrInd;
}
if (atomId1 < 0 || atomId2 < 0)
{
vtkWarningMacro(<< "NewBond(): Invalid atom ids: " << atomId1
<< " " << atomId2);
return;
}
vtkDebugMacro(<< "Adding bond between atomids " << atomId1 << " "
<< atomId2);
this->Target->AppendBond(atomId1, atomId2, order);
}