Debian Codegraph @

avogadro/core/cube.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_CUBE_H
7 #define AVOGADRO_CORE_CUBE_H
8
9 #include "avogadrocoreexport.h"
10
11 #include "vector.h"
12
13 #include <vector>
14 #include <array>
15
16 namespace Avogadro::Core {
17
18 class Molecule;
19 class Mutex;
20
21 /**
22 * @class Cube cube.h <avogadro/core/cube.h>
23 * @brief Provide a data structure for regularly spaced 3D grids.
24 * @author Marcus D. Hanwell
25 * @author Perminder Singh
26 */
27
28 class AVOGADROCORE_EXPORT Cube
29 {
30 public:
31 Cube();
32 ~Cube();
33
34 /**
35 * \enum Type
36 * Different Cube types relating to the data
37 */
38 enum Type
39 {
40 VdW,
41 SolventAccessible,
42 SolventExcluded,
43 ESP,
44 ElectronDensity,
45 SpinDensity,
46 MO,
47 FromFile,
48 None
49 };
50
51 /**
52 * @return The minimum point in the cube.
53 */
54 Vector3 min() const { return m_min; }
55
56 /**
57 * @return The maximum point in the cube.
58 */
59 Vector3 max() const { return m_max; }
60
61 /**
62 * @return The spacing of the grid.
63 */
64 Vector3 spacing() const { return m_spacing; }
65
66 /**
67 * @return The x, y and z dimensions of the cube.
68 */
69 Vector3i dimensions() const { return m_points; }
70
71 /**
72 * Set the limits of the cube.
73 * @param min The minimum point in the cube.
74 * @param max The maximum point in the cube.
75 * @param points The number of (integer) points in the cube.
76 */
77 bool setLimits(const Vector3& min, const Vector3& max,
78 const Vector3i& points);
79
80 /**
81 * Set the limits of the cube.
82 * @param min The minimum point in the cube.
83 * @param max The maximum point in the cube.
84 * @param spacing The interval between points in the cube.
85 */
86 bool setLimits(const Vector3& min, const Vector3& max, float spacing);
87
88 /**
89 * Set the limits of the cube.
90 * @param min The minimum point in the cube.
91 * @param dim The integer dimensions of the cube in x, y and z.
92 * @param spacing The interval between points in the cube.
93 */
94 bool setLimits(const Vector3& min, const Vector3i& dim, float spacing);
95
96 /**
97 * Set the limits of the cube.
98 * @param min The minimum point in the cube.
99 * @param dim The integer dimensions of the cube in x, y and z.
100 * @param spacing The interval between points in the cube.
101 */
102 bool setLimits(const Vector3& min, const Vector3i& dim,
103 const Vector3& spacing);
104
105 /**
106 * Set the limits of the cube - copy the limits of an existing Cube.
107 * @param cube Existing Cube to copy the limits from.
108 */
109 bool setLimits(const Cube& cube);
110
111 /**
112 * Set the limits of the cube.
113 * @param mol Molecule to take limits from
114 * @param spacing The spacing of the regular grid
115 * @param padding Padding around the molecule
116 */
117 bool setLimits(const Molecule& mol, float spacing, float padding);
118
119 /**
120 * @return Pointer to the vector containing all the data in a one-dimensional
121 * array.
122 */
123 std::vector<float>* data();
124 const std::vector<float>* data() const;
125
126 /**
127 * Set the values in the cube to those passed in the vector.
128 */
129 bool setData(const std::vector<float>& values);
130
131 /**
132 * Adds the values in the cube to those passed in the vector.
133 */
134 bool addData(const std::vector<float>& values);
135
136 /**
137 * @return Const iterator to the beginning of a specific row in the cube data.
138 */
139 std::vector<float>::const_iterator getRowIter(int j, int k) const;
140
141 /**
142 * @return Index of the point closest to the position supplied.
143 * @param pos Position to get closest index for.
144 */
145 unsigned int closestIndex(const Vector3& pos) const;
146
147 /**
148 * @param pos Position to get closest index for.
149 * @return The i, j, k index closest to the position supplied.
150 */
151 Vector3i indexVector(const Vector3& pos) const;
152
153 /**
154 * @param index Index to be translated to a position.
155 * @return Position of the given index.
156 */
157 Vector3 position(unsigned int index) const;
158
159 /**
160 * This function is very quick as it just returns the value at the point.
161 * @return Cube value at the integer point i, j, k.
162 */
163 float value(int i, int j, int k) const;
164
165 /**
166 * This function is very quick as it just returns the value at the point.
167 * @return Cube value at the integer point pos.
168 */
169 float value(const Vector3i& pos) const;
170
171 /**
172 * This function uses trilinear interpolation to find the value at points
173 * between those specified in the cube.
174 * @return Cube value at the specified position.
175 * @warning This function is quite computationally expensive and should be
176 * avoided where possible.
177 */
178 float valuef(const Vector3f& pos) const;
179
180 /**
181 * This function uses trilinear interpolation to find the value at points
182 * between those specified in the cube.
183 * @return Cube value at the specified position.
184 * @warning This function is quite computationally expensive and should be
185 * avoided where possible.
186 */
187 float value(const Vector3& pos) const;
188
189 /**
190 * Sets the value at the specified point in the cube.
191 * @param i x component of the position.
192 * @param j y component of the position.
193 * @param k z component of the position.
194 * @param value Value at the specified position.
195 */
196 bool setValue(unsigned int i, unsigned int j, unsigned int k, float value);
197
198 /**
199 * Sets the value at the specified index in the cube.
200 * @param i 1-dimensional index of the point to set in the cube.
201 */
202 bool setValue(unsigned int i, float value);
203
204 /**
205 * Sets all indices in the cube to the specified value.
206 * @param value Value to fill the cube with.
207 */
208 void fill(float value);
209
210 /**
211 * Sets all indices in a Z stripe of the cube to the specified value.
212 * @param i x component of the position.
213 * @param j y component of the position.
214 * @param kfirst first z position to fill.
215 * @param klast last z position to fill.
216 * @param value Value to fill the stripe with.
217 */
218 bool fillStripe(unsigned int i, unsigned int j, unsigned int kfirst,
219 unsigned int klast, float value);
220
221 /**
222 * @return The minimum value at any point in the Cube.
223 */
224 float minValue() const { return m_minValue; }
225
226 /**
227 * @return The maximum value at any point in the Cube.
228 */
229 float maxValue() const { return m_maxValue; }
230
231 void setName(const std::string& name_) { m_name = name_; }
232 std::string name() const { return m_name; }
233
234 void setCubeType(Type type) { m_cubeType = type; }
235 Type cubeType() const { return m_cubeType; }
236
237 /**
238 * Provides locking.
239 */
240 Mutex* lock() const { return m_lock; }
241
242 /**
243 * Compute the gradient at a specific point in the cube.
244 * @param i x index
245 * @param j y index
246 * @param k z index
247 * @return Gradient vector at the specified point.
248 */
249 std::array<float, 3> computeGradient(int i, int j, int k) const;
250
251 /**
252 * Get the values of the eight corners of a cube defined by the indices
253 * (i, j, k).
254 * @param i x index
255 * @param j y index
256 * @param k z index
257 * @return Array of values at the eight corners.
258 */
259 std::array<float, 8> getValsCube(int i, int j, int k) const;
260
261 /**
262 * Get the gradients at the eight corners of the cube defined by the indices
263 * (i, j, k).
264 * @param i x index
265 * @param j y index
266 * @param k z index
267 * @return Array of gradients at the eight corners. Each gradient is a 3D
268 * vector.
269 */
270 std::array<std::array<float, 3>, 8> getGradCube(int i, int j, int k) const;
271
272 /**
273 * Get the data value at the specified indices.
274 * @param i x index
275 * @param j y index
276 * @param k z index
277 * @return Value at the specified indices.
278 */
279 float getData(int i, int j, int k) const;
280
281 int nx() const { return m_points.x(); }
282 int ny() const { return m_points.y(); }
283 int nz() const { return m_points.z(); }
284
285 inline unsigned int linearIndex(int x, int y, int z) const;
286
287 /**
288 * Retrieves the positions of the eight corners of a cube at grid indices
289 * (i, j, k).
290 *
291 * The indices (i, j, k) are converted to real-space positions (xpos, ypos,
292 * zpos), mapping grid indices to physical coordinates. The method returns a
293 * cube that spans one step in each of the x, y, and z directions, with step
294 * sizes defined by `m_spacing`.
295 *
296 * @param i X-index.
297 * @param j Y-index.
298 * @param k Z-index.
299 * @return A `std::array` of eight `(x, y, z)` coordinates representing the
300 * cube's corners.
301 */
302
303 std::array<std::array<float, 3>, 8> getPosCube(int i, int j, int k) const;
304
305 protected:
306 std::vector<float> m_data;
307 Vector3 m_min, m_max, m_spacing;
308 Vector3i m_points;
309 float m_minValue, m_maxValue;
310 std::string m_name;
311 Type m_cubeType;
312 Mutex* m_lock;
313 };
314
315 inline bool Cube::setValue(unsigned int i, float value_)
316 {
317 if (i < m_data.size()) {
318 m_data[i] = value_;
319 if (value_ > m_maxValue)
320 m_maxValue = value_;
321 if (value_ < m_minValue)
322 m_minValue = value_;
323 return true;
324 } else
325 return false;
326 }
327
328 } // namespace Avogadro::Core
329
330 #endif // AVOGADRO_CORE_CUBE_H