TestMolecule.cxx 3.56 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
/*=========================================================================

  Program:   Visualization Toolkit

  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 "vtkMolecule.h"
#include "vtkNew.h"
#include "vtkVector.h"
#include "vtkVectorOperators.h"

#define TEST(cond)                              \
  if (!(cond)) return false

// Example code from the molecule documentation. If this breaks,
// update the docs in vtkMolecule.h
bool MoleculeExampleCode1()
{
27
  vtkNew<vtkMolecule> mol;
28
29
30
  vtkAtom h1 = mol->AppendAtom(1, 0.0, 0.0, -0.5);
  vtkAtom h2 = mol->AppendAtom(1, 0.0, 0.0,  0.5);
  vtkBond b  = mol->AppendBond(h1, h2, 1);
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
  int errors(0);

  if (fabs(b.GetLength() - 1.0) > 1e-8)
    {
    cout << "Error bond length incorrect. Expected 1.0, but got "
         << b.GetLength() << endl;
    ++errors;
    }

  if (!h1.GetPosition().Compare(vtkVector3f(0.0, 0.0, -0.5), 1e-8))
    {
    cout << "Error atom position incorrect. Expected 0.0, 0.0, -0.5 but got "
         << h1.GetPosition() << endl;
    ++errors;
    }

  if (!h2.GetPosition().Compare(vtkVector3f(0.0, 0.0, 0.5), 1e-8))
    {
    cout << "Error atom position incorrect. Expected 0.0, 0.0, 0.5 but got "
         << h2.GetPosition() << endl;
    ++errors;
    }

  if (!h1.GetAtomicNumber() == 1)
    {
    cout << "Error atomic number incorrect. Expected 1 but got "
         << h1.GetAtomicNumber() << endl;
    ++errors;
    }

  if (!h2.GetAtomicNumber() == 1)
    {
    cout << "Error atomic number incorrect. Expected 1 but got "
         << h2.GetAtomicNumber() << endl;
    ++errors;
    }

  return errors == 0;
69
70
71
72
73
74
}

// Example code from the molecule documentation. If this breaks,
// update the docs in vtkMolecule.h
bool MoleculeExampleCode2()
{
75
  vtkNew<vtkMolecule> mol;
76

77
  vtkAtom h1 = mol->AppendAtom();
78
79
80
  h1.SetAtomicNumber(1);
  h1.SetPosition(0.0, 0.0, -0.5);

81
  vtkAtom h2 = mol->AppendAtom();
82
  h2.SetAtomicNumber(1);
83
84
  vtkVector3f displacement(0.0, 0.0, 1.0);
  h2.SetPosition(h1.GetPosition() + displacement);
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
  vtkBond b = mol->AppendBond(h1, h2, 1);

  int errors(0);

  if (fabs(b.GetLength() - 1.0) > 1e-8)
    {
    cout << "Error bond length incorrect. Expected 1.0, but got "
         << b.GetLength() << endl;
    ++errors;
    }

  if (!h1.GetPosition().Compare(vtkVector3f(0.0, 0.0, -0.5), 1e-8))
    {
    cout << "Error atom position incorrect. Expected 0.0, 0.0, -0.5 but got "
         << h1.GetPosition() << endl;
    ++errors;
    }

  if (!h2.GetPosition().Compare(vtkVector3f(0.0, 0.0, 0.5), 1e-8))
    {
    cout << "Error atom position incorrect. Expected 0.0, 0.0, 0.5 but got "
         << h2.GetPosition() << endl;
    ++errors;
    }

  if (!h1.GetAtomicNumber() == 1)
    {
    cout << "Error atomic number incorrect. Expected 1 but got "
         << h1.GetAtomicNumber() << endl;
    ++errors;
    }

  if (!h2.GetAtomicNumber() == 1)
    {
    cout << "Error atomic number incorrect. Expected 1 but got "
         << h2.GetAtomicNumber() << endl;
    ++errors;
    }

  return errors == 0;
126
127
}

128
int TestMolecule(int, char * [])
129
130
{
  // Check that the example code given in the molecule docs compiles:
131
132
  bool test1 = MoleculeExampleCode1();
  bool test2 = MoleculeExampleCode2();
133

134
  return (test1 && test2) ? 0 : 1;
135
}