Coverage Report

Created: 2025-12-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openbabel/src/griddata.cpp
Line
Count
Source
1
/**********************************************************************
2
griddata.cpp - Store grids of data linked to a molecule (e.g. Gaussian cube)
3
4
// Molekel - Molecular Visualization Program
5
// Copyright (C) 2006, 2007 Swiss National Supercomputing Centre (CSCS)
6
7
 Some Portions Copyright (c) 2007 by Geoffrey R. Hutchison
8
 Some Portions Copyright (C) 2008 by Marcus D. Hanwell
9
10
This file is part of the Open Babel project.
11
For more information, see <http://openbabel.org/>
12
13
This program is free software; you can redistribute it and/or modify
14
it under the terms of the GNU General Public License as published by
15
the Free Software Foundation version 2 of the License.
16
17
This program is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
GNU General Public License for more details.
21
***********************************************************************/
22
23
#include <openbabel/babelconfig.h>
24
25
#include <openbabel/griddata.h>
26
#include <openbabel/mol.h>
27
#include <openbabel/grid.h>
28
29
#include <vector>
30
#include <algorithm>
31
#include <limits>
32
33
using namespace std;
34
35
namespace OpenBabel {
36
37
  class GridDataPrivate {
38
  public:
39
0
    GridDataPrivate() {    }
40
41
    OBFloatGrid  floatGrid;
42
    OBGridData::Unit _unit;
43
44
    double           _max;
45
    double           _min;
46
47
    bool             _unrestricted;
48
    int              _symmetries;
49
  };
50
51
  /** \class OBGridData griddata.h <openbabel/griddata.h>
52
    \brief Store values for numeric grids such as orbitals or electrostatic potential
53
    \since version 2.2
54
    \sa OBFloatGrid
55
56
    OBGridData facilitates attaching grids and cubes to molecular data. A "grid" is
57
    data representing some function f(x,y,z), such as a molecule's electrostatic potential
58
    or molecular orbitals. This need not be a "cube" even though this file format from Gaussian
59
    is frequently used. Axes need not be identical, and indeed do not need to be orthogonal.
60
61
    Open Babel supports reading several types of grid file formats, including Gaussian cube,
62
    and OpenDX. The latter is notably used by the APBS program for numeric evaluation of molecular
63
    and protein electrostatic potential.
64
65
    \code
66
    OBGridData *gd = new OBGridData;
67
    gd->SetAttribute("Example Grid"); // the title of the grid -- e.g., for user display
68
    vector<int> voxels(3); // the number of voxels in each direction
69
    vector3 origin; // the beginning x, y, z coordinate of the grid
70
    vector<vector3> axes; // the xyz displacements for each of the grid axes
71
    ...
72
    gd->SetNumberOfPoints(voxels[0], voxels[1], voxels[2]);
73
    gd->SetLimits(origin, axes[0], axes[1], axes[2]);
74
    gd->SetUnit(OBGridData::ANGSTROM);
75
    gd->SetOrigin(fileformatInput); // i.e., is this data from a file or determined by Open Babel
76
77
    for (int k = 0; k < voxels[2]; ++k)
78
      for (int j = 0; j < voxels[1]; ++j)
79
        for (int i = 0; i < voxels[0]; ++i)
80
          {
81
            gd->SetValue(i, j, k,
82
                         grid[k*voxels[0]*voxels[1] + j*voxels[0] + i]);
83
          }
84
85
    mol->SetData(gd);
86
    \endcode
87
88
    \code
89
    if (mol->HasData(OBGenericDataType::GridData)) {
90
      vector<OBGenericData*> grids = mol->GetAllData(OBGenericDataType::GridData)
91
      // Output the name of the grid
92
      if (grids[0] != nullptr)
93
        cout << grids[0]->GetAttribute();
94
    }
95
    \endcode
96
97
  */
98
99
0
  OBGridData::OBGridData() : OBGenericData("GridData", OBGenericDataType::GridData),
100
0
    d(new GridDataPrivate)
101
0
  {
102
0
  }
103
104
  OBGridData::~OBGridData()
105
0
  {
106
0
    delete d;
107
0
  }
108
109
  void OBGridData::GetAxes( double x[3], double y[3], double z[3] ) const
110
0
  {
111
0
    vector3 v1, v2, v3;
112
0
    v1 = d->floatGrid.GetXAxis();
113
0
    v2 = d->floatGrid.GetYAxis();
114
0
    v3 = d->floatGrid.GetZAxis();
115
116
0
    x[0] = v1.x(); x[1] = v1.y(), x[2] = v1.z();
117
0
    y[0] = v2.x(); y[1] = v2.y(), y[2] = v2.z();
118
0
    z[0] = v3.x(); z[1] = v3.y(), z[2] = v3.z();
119
0
  }
120
121
  vector3 OBGridData::GetXAxis() const
122
0
  {
123
0
    return d->floatGrid.GetXAxis();
124
0
  }
125
126
  vector3 OBGridData::GetYAxis() const
127
0
  {
128
0
    return d->floatGrid.GetYAxis();
129
0
  }
130
131
  vector3 OBGridData::GetZAxis() const
132
0
  {
133
0
    return d->floatGrid.GetZAxis();
134
0
  }
135
136
  void OBGridData::GetAxes( vector3 &v1, vector3 &v2, vector3 &v3 ) const
137
0
  {
138
0
    v1 = d->floatGrid.GetXAxis();
139
0
    v2 = d->floatGrid.GetYAxis();
140
0
    v3 = d->floatGrid.GetZAxis();
141
0
  }
142
143
  void OBGridData::GetNumberOfPoints( int &nx, int &ny, int &nz) const
144
0
  {
145
0
    nx = d->floatGrid.GetXdim();
146
0
    ny = d->floatGrid.GetYdim();
147
0
    nz = d->floatGrid.GetZdim();
148
0
  }
149
150
  int OBGridData::GetNumberOfPoints() const
151
0
  {
152
0
    return d->floatGrid.GetXdim() * d->floatGrid.GetYdim() * d->floatGrid.GetZdim();
153
0
  }
154
155
  void OBGridData::GetNumberOfSteps( int steps[ 3 ] ) const
156
0
  {
157
0
    steps[0] = d->floatGrid.GetXdim() - 1;
158
0
    steps[1] = d->floatGrid.GetYdim() - 1;
159
0
    steps[2] = d->floatGrid.GetZdim() - 1;
160
0
  }
161
162
  std::vector< double > OBGridData::GetValues() const
163
0
  {
164
0
    return d->floatGrid.GetDataVector();
165
0
  }
166
167
  double OBGridData::GetValue( int i, int j, int k ) const
168
0
  {
169
0
    return d->floatGrid.GetValue(i, j, k);
170
0
  }
171
172
  double OBGridData::GetValue(vector3 pos) const
173
0
  {
174
0
    return d->floatGrid.Interpolate(pos.x(), pos.y(), pos.z());
175
0
  }
176
177
  OBGridData::Unit OBGridData::GetUnit() const
178
0
  {
179
0
    return d->_unit;
180
0
  }
181
182
  double OBGridData::GetMinValue() const
183
0
  {
184
0
    return d->_min;
185
0
  }
186
187
  double OBGridData::GetMaxValue() const
188
0
  {
189
0
    return d->_max;
190
0
  }
191
192
  void OBGridData::GetOriginVector( double o[ 3 ] ) const
193
0
  {
194
0
    d->floatGrid.GetMin(o);
195
0
  }
196
197
  vector3 OBGridData::GetOriginVector() const
198
0
  {
199
0
    return d->floatGrid.GetMin();
200
0
  }
201
202
  vector3 OBGridData::GetMaxVector() const
203
0
  {
204
0
    return d->floatGrid.GetMax();
205
0
  }
206
207
  bool OBGridData::GetUnrestricted() const
208
0
  {
209
0
    return d->_unrestricted;
210
0
  }
211
212
  int OBGridData::GetNumSymmetries() const
213
0
  {
214
0
    return d->_symmetries;
215
0
  }
216
217
  void OBGridData::SetUnrestricted( bool u )
218
0
  {
219
0
    d->_unrestricted = u;
220
0
  }
221
222
  void OBGridData::SetNumSymmetries( int s )
223
0
  {
224
0
    d->_symmetries = s;
225
0
  }
226
227
  void OBGridData::SetNumberOfPoints( int nx, int ny, int nz )
228
0
  {
229
0
    d->floatGrid.SetNumberOfPoints(nx, ny, nz);
230
0
  }
231
232
  void OBGridData::SetLimits(const double origin [3], const double x[3],
233
                             const double y[3], const double z[3])
234
0
  {
235
0
    d->floatGrid.SetLimits(origin, x, y, z);
236
0
  }
237
238
  void OBGridData::SetLimits(const vector3 &origin, const vector3 &x,
239
                             const vector3 &y, const vector3 &z)
240
0
  {
241
0
    d->floatGrid.SetLimits(origin, x, y, z);
242
0
  }
243
244
  bool OBGridData::SetValue(int i, int j, int k, double val)
245
0
  {
246
0
    return d->floatGrid.SetValue(i, j, k, val);
247
0
  }
248
249
  void OBGridData::SetValues( const std::vector< double >& v )
250
0
  {
251
0
    d->floatGrid.SetVals(v);
252
0
    d->_min = *std::min_element( v.begin(), v.end() );
253
0
    d->_max = *std::max_element( v.begin(), v.end() );
254
0
  }
255
256
  void OBGridData::SetUnit( OBGridData::Unit u )
257
0
  {
258
0
    d->_unit = u;
259
0
  }
260
261
} // end namespace
262
263
//! \file griddata.cpp
264
//! \brief OBGenericData class to connect numeric grids (e.g., orbitals, electrostatic potential) to molecules