vtkBlueObeliskDataParser.cxx 17.1 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkBlueObeliskDataParser.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 "vtkBlueObeliskDataParser.h"

#include "vtkAbstractArray.h"
#include "vtkBlueObeliskData.h"
#include "vtkFloatArray.h"
#include "vtkObjectFactory.h"
#include "vtkStdString.h"
#include "vtkStringArray.h"
#include "vtkUnsignedShortArray.h"

// Defines VTK_BODR_DATA_PATH
#include "vtkChemistryConfigure.h"

#include <locale>
30
#include <sstream>
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
#include <sys/types.h>
#include <sys/stat.h>

#ifdef MSC_VER
#define stat _stat
#endif

//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkBlueObeliskDataParser);

//----------------------------------------------------------------------------
vtkBlueObeliskDataParser::vtkBlueObeliskDataParser()
  : vtkXMLParser(),
    Target(NULL),
    IsProcessingAtom(false),
    IsProcessingValue(false),
    CurrentValueType(None),
    CurrentSymbol(new vtkStdString),
    CurrentName(new vtkStdString),
    CurrentPeriodicTableBlock(new vtkStdString),
    CurrentElectronicConfiguration(new vtkStdString),
    CurrentFamily(new vtkStdString)
{
}

//----------------------------------------------------------------------------
vtkBlueObeliskDataParser::~vtkBlueObeliskDataParser()
{
  this->SetTarget(NULL);
  delete CurrentSymbol;
  delete CurrentName;
  delete CurrentPeriodicTableBlock;
  delete CurrentElectronicConfiguration;
  delete CurrentFamily;
}

//----------------------------------------------------------------------------
void vtkBlueObeliskDataParser::SetTarget(vtkBlueObeliskData *bodr)
{
  vtkSetObjectBodyMacro(Target, vtkBlueObeliskData, bodr);
}

//----------------------------------------------------------------------------
int vtkBlueObeliskDataParser::Parse()
{
  if (!this->Target)
    {
    vtkWarningMacro(<<"No target set. Aborting.");
    return 0;
    }

  // Setup BlueObeliskData arrays
  this->Target->Reset();
  this->Target->Allocate(119); // 118 elements + dummy (0)

  int ret = this->Superclass::Parse();

  this->Target->Squeeze();

  // Set number of elements to the length of the symbol array minus
  // one (index 0 is a dummy atom type)
  this->Target->NumberOfElements =
    this->Target->Symbols->GetNumberOfTuples() - 1;

  return ret;
}

//----------------------------------------------------------------------------
int vtkBlueObeliskDataParser::Parse(const char *)
{
  return this->Parse();
}

//----------------------------------------------------------------------------
int vtkBlueObeliskDataParser::Parse(const char *,
                                    unsigned int)
{
  return this->Parse();
}

//----------------------------------------------------------------------------
void vtkBlueObeliskDataParser::StartElement(const char *name,
                                            const char **attr)
{
  if (this->GetDebug())
    {
    std::string desc;
    desc += "Encountered BODR 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);
    }

  if (strcmp(name, "atom") == 0)
    {
    this->NewAtomStarted(attr);
    }
  else if (strcmp(name, "scalar") == 0 ||
           strcmp(name, "label") == 0 ||
           strcmp(name, "array") == 0)
    {
    this->NewValueStarted(attr);
    }
  else if (this->GetDebug())
    {
    vtkDebugMacro(<<"Unhandled BODR element: " << name);
    }

  return;
}

//----------------------------------------------------------------------------
void vtkBlueObeliskDataParser::EndElement(const char *name)
{
  if (strcmp(name, "atom") == 0)
    {
    this->NewAtomFinished();
    }
  else if (strcmp(name, "scalar") == 0 ||
           strcmp(name, "label") == 0 ||
           strcmp(name, "array") == 0)
    {
    this->NewValueFinished();
    }
}

//----------------------------------------------------------------------------
168
void vtkBlueObeliskDataParser::NewAtomStarted(const char **)
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
{
  this->CurrentAtomicNumber = -1;
  this->CurrentSymbol->clear();
  this->CurrentName->clear();
  this->CurrentPeriodicTableBlock->clear();
  this->CurrentElectronicConfiguration->clear();
  this->CurrentFamily->clear();
  this->CurrentMass = VTK_FLOAT_MAX;
  this->CurrentExactMass =VTK_FLOAT_MAX;
  this->CurrentIonizationEnergy = VTK_FLOAT_MAX;
  this->CurrentElectronAffinity = VTK_FLOAT_MAX;
  this->CurrentPaulingElectronegativity = VTK_FLOAT_MAX;
  this->CurrentCovalentRadius = VTK_FLOAT_MAX;
  this->CurrentVDWRadius = VTK_FLOAT_MAX;
  this->CurrentDefaultColor[0] = 0.0;
  this->CurrentDefaultColor[1] = 0.0;
  this->CurrentDefaultColor[2] = 0.0;
  this->CurrentBoilingPoint = VTK_FLOAT_MAX;
  this->CurrentMeltingPoint = VTK_FLOAT_MAX;
  this->CurrentPeriod = VTK_UNSIGNED_SHORT_MAX;
  this->CurrentGroup = VTK_UNSIGNED_SHORT_MAX;

  this->CurrentValueType = None;

  this->IsProcessingAtom = true;
}

//----------------------------------------------------------------------------
void vtkBlueObeliskDataParser::NewAtomFinished()
{
  if (this->CurrentAtomicNumber < 0)
    {
    vtkWarningMacro(<<"Skipping invalid atom...");
    this->IsProcessingAtom = false;
    return;
    }

  vtkDebugMacro(<<"Adding info for atomic number: " <<
                this->CurrentAtomicNumber);

  vtkIdType index = static_cast<vtkIdType>(this->CurrentAtomicNumber);

  this->ResizeAndSetValue(this->CurrentSymbol,
                          this->Target->Symbols.GetPointer(),
                          index);
  // this->ToLower will modify the input string, so this must follow
  // this->Symbol
  this->ResizeAndSetValue(this->ToLower(this->CurrentSymbol),
                          this->Target->LowerSymbols.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentName,
                          this->Target->Names.GetPointer(),
                          index);
  // this->ToLower will modify the input string, so this must follow
  // this->Name
  this->ResizeAndSetValue(this->ToLower(this->CurrentName),
                          this->Target->LowerNames.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentPeriodicTableBlock,
                          this->Target->PeriodicTableBlocks.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentElectronicConfiguration,
                          this->Target->ElectronicConfigurations.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentFamily,
                          this->Target->Families.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentMass,
                          this->Target->Masses.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentExactMass,
                          this->Target->ExactMasses.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentIonizationEnergy,
                          this->Target->IonizationEnergies.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentElectronAffinity,
                          this->Target->ElectronAffinities.GetPointer(),
                          index);
  this->ResizeAndSetValue
    (this->CurrentPaulingElectronegativity,
     this->Target->PaulingElectronegativities.GetPointer(), index);
  this->ResizeAndSetValue(this->CurrentCovalentRadius,
                          this->Target->CovalentRadii.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentVDWRadius,
                          this->Target->VDWRadii.GetPointer(),
                          index);
  // Tuple handled differently
  this->ResizeArrayIfNeeded(this->Target->DefaultColors.GetPointer(),
                            index);
David C. Lonie's avatar
David C. Lonie committed
260
  this->Target->DefaultColors->SetTypedTuple(index,
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
                                             this->CurrentDefaultColor);
  this->ResizeAndSetValue(this->CurrentBoilingPoint,
                          this->Target->BoilingPoints.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentMeltingPoint,
                          this->Target->MeltingPoints.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentPeriod,
                          this->Target->Periods.GetPointer(),
                          index);
  this->ResizeAndSetValue(this->CurrentGroup,
                          this->Target->Groups.GetPointer(),
                          index);
  this->IsProcessingAtom = false;
}

//----------------------------------------------------------------------------
void vtkBlueObeliskDataParser::NewValueStarted(const char **attr)
{
  this->IsProcessingValue = true;
  unsigned int attrInd = 0;

  while (const char * cur = attr[attrInd])
    {
    if (strcmp(cur, "value") == 0)
      {
      this->SetCurrentValue(attr[++attrInd]);
      }
    else if (strcmp(cur, "bo:atomicNumber") == 0)
      {
      this->CurrentValueType = AtomicNumber;
      }
    else if (strcmp(cur, "bo:symbol") == 0)
      {
      this->CurrentValueType = Symbol;
      }
    else if (strcmp(cur, "bo:name") == 0)
      {
      this->CurrentValueType = Name;
      }
    else if (strcmp(cur, "bo:periodTableBlock") == 0)
      {
      this->CurrentValueType = PeriodicTableBlock;
      }
    else if (strcmp(cur, "bo:electronicConfiguration") == 0)
      {
      this->CurrentValueType = ElectronicConfiguration;
      }
    else if (strcmp(cur, "bo:family") == 0)
      {
      this->CurrentValueType = Family;
      }
    else if (strcmp(cur, "bo:mass") == 0)
      {
      this->CurrentValueType = Mass;
      }
    else if (strcmp(cur, "bo:exactMass") == 0)
      {
      this->CurrentValueType = ExactMass;
      }
    else if (strcmp(cur, "bo:ionization") == 0)
      {
      this->CurrentValueType = IonizationEnergy;
      }
    else if (strcmp(cur, "bo:electronAffinity") == 0)
      {
      this->CurrentValueType = ElectronAffinity;
      }
    else if (strcmp(cur, "bo:electronegativityPauling") == 0)
      {
      this->CurrentValueType = PaulingElectronegativity;
      }
    else if (strcmp(cur, "bo:radiusCovalent") == 0)
      {
      this->CurrentValueType = CovalentRadius;
      }
    else if (strcmp(cur, "bo:radiusVDW") == 0)
      {
      this->CurrentValueType = VDWRadius;
      }
    else if (strcmp(cur, "bo:elementColor") == 0)
      {
      this->CurrentValueType = DefaultColor;
      }
    else if (strcmp(cur, "bo:boilingpoint") == 0)
      {
      this->CurrentValueType = BoilingPoint;
      }
    else if (strcmp(cur, "bo:meltingpoint") == 0)
      {
      this->CurrentValueType = MeltingPoint;
      }
    else if (strcmp(cur, "bo:period") == 0)
      {
      this->CurrentValueType = Period;
      }
    else if (strcmp(cur, "bo:group") == 0)
      {
      this->CurrentValueType = Group;
      }
    ++attrInd;
    }
}

//----------------------------------------------------------------------------
void vtkBlueObeliskDataParser::NewValueFinished()
{
368
  this->CurrentValueType = None;
369
  this->IsProcessingValue = false;
370
  this->CharacterDataValueBuffer.clear();
371
372
373
374
375
376
377
378
379
380
381
382
383
}

//----------------------------------------------------------------------------
void vtkBlueObeliskDataParser::CharacterDataHandler(const char *data,
                                                    int length)
{
  if (this->IsProcessingAtom && this->IsProcessingValue)
    {
    this->SetCurrentValue(data, length);
    }
}

//----------------------------------------------------------------------------
384
void vtkBlueObeliskDataParser::SetCurrentValue(const char *data, int length)
385
{
386
  this->CharacterDataValueBuffer += std::string(data, data+length);
387

388
  this->SetCurrentValue(this->CharacterDataValueBuffer.c_str());
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
}

//----------------------------------------------------------------------------
void vtkBlueObeliskDataParser::SetCurrentValue(const char *data)
{
  vtkDebugMacro(<<"Parsing string '" << data << "' for datatype "
                << this->CurrentValueType << ".");
  switch (this->CurrentValueType)
    {
    case AtomicNumber:
      this->CurrentAtomicNumber = this->parseInt(data);
      return;
    case Symbol:
      this->CurrentSymbol->assign(data);
      return;
    case Name:
      this->CurrentName->assign(data);
      return;
    case PeriodicTableBlock:
      this->CurrentPeriodicTableBlock->assign(data);
      return;
    case ElectronicConfiguration:
      this->CurrentElectronicConfiguration->assign(data);
      return;
    case Family:
      this->CurrentFamily->assign(data);
      return;
    case Mass:
      this->CurrentMass = this->parseFloat(data);
      return;
    case ExactMass:
      this->CurrentExactMass = this->parseFloat(data);
      return;
    case IonizationEnergy:
      this->CurrentIonizationEnergy = this->parseFloat(data);
      return;
    case ElectronAffinity:
      this->CurrentElectronAffinity = this->parseFloat(data);
      return;
    case PaulingElectronegativity:
      this->CurrentPaulingElectronegativity = this->parseFloat(data);
      return;
    case CovalentRadius:
      this->CurrentCovalentRadius = this->parseFloat(data);
      return;
    case VDWRadius:
      this->CurrentVDWRadius = this->parseFloat(data);
      return;
    case DefaultColor:
      this->parseFloat3(data, this->CurrentDefaultColor);
      return;
    case BoilingPoint:
      this->CurrentBoilingPoint = this->parseFloat(data);
      return;
    case MeltingPoint:
      this->CurrentMeltingPoint = this->parseFloat(data);
      return;
    case Period:
      this->CurrentPeriod = this->parseUnsignedShort(data);
      return;
    case Group:
      this->CurrentGroup = this->parseUnsignedShort(data);
      return;
    case None:
    default:
      vtkDebugMacro(<<"Called with no CurrentValueType. data: "<<data);
    }
  return;
}

//----------------------------------------------------------------------------
460
461
void  vtkBlueObeliskDataParser::ResizeArrayIfNeeded(vtkAbstractArray *arr,
                                                    vtkIdType ind)
462
463
464
465
466
467
468
469
{
  if (ind >= arr->GetNumberOfTuples())
    {
    arr->SetNumberOfTuples(ind + 1);
    }
}

//----------------------------------------------------------------------------
470
471
472
void vtkBlueObeliskDataParser::ResizeAndSetValue(vtkStdString *val,
                                                 vtkStringArray *arr,
                                                 vtkIdType ind)
473
474
475
476
477
478
{
  vtkBlueObeliskDataParser::ResizeArrayIfNeeded(arr, ind);
  arr->SetValue(ind, val->c_str());
}

//----------------------------------------------------------------------------
479
480
481
void vtkBlueObeliskDataParser::ResizeAndSetValue(float val,
                                                 vtkFloatArray *arr,
                                                 vtkIdType ind)
482
483
484
485
486
487
{
  vtkBlueObeliskDataParser::ResizeArrayIfNeeded(arr, ind);
  arr->SetValue(ind, val);
}

//----------------------------------------------------------------------------
488
489
490
void vtkBlueObeliskDataParser::ResizeAndSetValue(unsigned short val,
                                                 vtkUnsignedShortArray *arr,
                                                 vtkIdType ind)
491
492
493
494
495
496
497
498
499
500
501
502
503
504
{
  vtkBlueObeliskDataParser::ResizeArrayIfNeeded(arr, ind);
  arr->SetValue(ind, val);
}

//----------------------------------------------------------------------------
inline int vtkBlueObeliskDataParser::parseInt(const char *d)
{
  return atoi(d);
}

//----------------------------------------------------------------------------
inline float vtkBlueObeliskDataParser::parseFloat(const char *d)
{
505
506
507
508
509
510
511
512
513
514
  float value;
  std::stringstream stream(d);
  stream >> value;

  if(stream.fail())
    {
    return 0.f;
    }

  return value;
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
}

//----------------------------------------------------------------------------
inline void vtkBlueObeliskDataParser::parseFloat3(const char *str,
                                                  float arr[3])
{
  unsigned short ind = 0;
  // Make copy of d for strtok.:
  char *strcopy = new char[strlen(str) + 1];
  strcpy(strcopy, str);

  char *curTok = strtok(strcopy, " ");

  while (curTok != NULL)
    {
    if (ind == 3)
      break;

    arr[ind++] = static_cast<float>(atof(curTok));
    curTok = strtok(NULL, " ");
    }

  if (ind != 3)
    {
    arr[0] = arr[1] = arr[2] == VTK_FLOAT_MAX;
    }

  delete [] strcopy;
}

//----------------------------------------------------------------------------
inline unsigned short
vtkBlueObeliskDataParser::parseUnsignedShort(const char *d)
{
  return static_cast<unsigned short>(atoi(d));
}

//----------------------------------------------------------------------------
inline vtkStdString * vtkBlueObeliskDataParser::ToLower(vtkStdString *str)
{
  for (vtkStdString::iterator it = str->begin(), it_end = str->end();
         it != it_end; ++it)
    {
    *it = static_cast<char>(tolower(*it));
    }
  return str;
}