Debian Codegraph @

avogadro/core/cube.cpp

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 #include "cube.h"
7
8 #include "molecule.h"
9 #include "mutex.h"
10
11 namespace Avogadro::Core {
12
13 Cube::Cube()
14 : m_data(0), m_min(0.0, 0.0, 0.0), m_max(0.0, 0.0, 0.0),
15 m_spacing(0.0, 0.0, 0.0), m_points(0, 0, 0), m_minValue(0.0),
16 m_maxValue(0.0), m_cubeType(None), m_lock(nullptr)
17 {
18 }
19
20 Cube::~Cube()
21 {
22 // delete m_lock;
23 m_lock = nullptr;
24 }
25
26 bool Cube::setLimits(const Vector3& min_, const Vector3& max_,
27 const Vector3i& points)
28 {
29 // We can calculate all necessary properties and initialise our data
30 Vector3 delta = max_ - min_;
31 m_spacing =
32 Vector3(delta.x() / (points.x() - 1), delta.y() / (points.y() - 1),
33 delta.z() / (points.z() - 1));
34 m_min = min_;
35 m_max = max_;
36 m_points = points;
37 m_data.resize(m_points.x() * m_points.y() * m_points.z());
38 return true;
39 }
40 bool Cube::setLimits(const Vector3& min_, const Vector3& max_, float spacing_)
41 {
42 Vector3 delta = max_ - min_;
43 delta = delta / spacing_;
44 return setLimits(min_, max_, delta.cast<int>());
45 }
46
47 bool Cube::setLimits(const Vector3& min_, const Vector3i& dim, float spacing_)
48 {
49 return setLimits(min_, dim, Vector3(spacing_, spacing_, spacing_));
50 }
51
52 bool Cube::setLimits(const Vector3& min_, const Vector3i& dim,
53 const Vector3& spacing_)
54 {
55 Vector3 max_ = Vector3(min_.x() + (dim.x() - 1) * spacing_[0],
56 min_.y() + (dim.y() - 1) * spacing_[1],
57 min_.z() + (dim.z() - 1) * spacing_[2]);
58 m_min = min_;
59 m_max = max_;
60 m_points = dim;
61 m_spacing = spacing_;
62 m_data.resize(m_points.x() * m_points.y() * m_points.z());
63 return true;
64 }
65
66 bool Cube::setLimits(const Cube& cube)
67 {
68 m_min = cube.m_min;
69 m_max = cube.m_max;
70 m_points = cube.m_points;
71 m_spacing = cube.m_spacing;
72 m_data.resize(m_points.x() * m_points.y() * m_points.z());
73 return true;
74 }
75
76 bool Cube::setLimits(const Molecule& mol, float spacing_, float padding)
77 {
78 Index numAtoms = mol.atomCount();
79 Vector3 min_, max_;
80 if (numAtoms) {
81 Vector3 curPos = min_ = max_ = mol.atomPositions3d()[0];
82 for (Index i = 1; i < numAtoms; ++i) {
83 curPos = mol.atomPositions3d()[i];
84 if (curPos.x() < min_.x())
85 min_.x() = curPos.x();
86 if (curPos.x() > max_.x())
87 max_.x() = curPos.x();
88 if (curPos.y() < min_.y())
89 min_.y() = curPos.y();
90 if (curPos.y() > max_.y())
91 max_.y() = curPos.y();
92 if (curPos.z() < min_.z())
93 min_.z() = curPos.z();
94 if (curPos.z() > max_.z())
95 max_.z() = curPos.z();
96 }
97 } else {
98 min_ = max_ = Vector3::Zero();
99 }
100
101 // Now to take care of the padding term
102 min_ += Vector3(-padding, -padding, -padding);
103 max_ += Vector3(padding, padding, padding);
104
105 return setLimits(min_, max_, spacing_);
106 }
107
108 std::vector<float>* Cube::data()
109 {
110 return &m_data;
111 }
112
113 const std::vector<float>* Cube::data() const
114 {
115 return &m_data;
116 }
117
118 bool Cube::setData(const std::vector<float>& values)
119 {
120 if (!values.size())
121 return false;
122
123 if (static_cast<int>(values.size()) ==
124 m_points.x() * m_points.y() * m_points.z()) {
125 m_data = values;
126 // Now to update the minimum and maximum values
127 m_minValue = m_maxValue = m_data[0];
128 for (float value : values) {
129 if (value < m_minValue)
130 m_minValue = value;
131 else if (value > m_maxValue)
132 m_maxValue = value;
133 }
134 return true;
135 } else {
136 return false;
137 }
138 }
139
140 bool Cube::addData(const std::vector<float>& values)
141 {
142 // Initialise the cube to zero if necessary
143 if (!m_data.size())
144 m_data.resize(m_points.x() * m_points.y() * m_points.z());
145 if (values.size() != m_data.size() || !values.size())
146 return false;
147 for (unsigned int i = 0; i < m_data.size(); i++) {
148 m_data[i] += values[i];
149 if (m_data[i] < m_minValue)
150 m_minValue = m_data[i];
151 else if (m_data[i] > m_maxValue)
152 m_maxValue = m_data[i];
153 }
154 return true;
155 }
156
157 unsigned int Cube::closestIndex(const Vector3& pos) const
158 {
159 int i, j, k;
160 // Calculate how many steps each coordinate is along its axis
161 i = int((pos.x() - m_min.x()) / m_spacing.x());
162 j = int((pos.y() - m_min.y()) / m_spacing.y());
163 k = int((pos.z() - m_min.z()) / m_spacing.z());
164 return i * m_points.y() * m_points.z() + j * m_points.z() + k;
165 }
166
167 Vector3i Cube::indexVector(const Vector3& pos) const
168 {
169 // Calculate how many steps each coordinate is along its axis
170 int i, j, k;
171 i = int((pos.x() - m_min.x()) / m_spacing.x());
172 j = int((pos.y() - m_min.y()) / m_spacing.y());
173 k = int((pos.z() - m_min.z()) / m_spacing.z());
174 return Vector3i(i, j, k);
175 }
176
177 Vector3 Cube::position(unsigned int index) const
178 {
179 int x, y, z;
180 x = int(index / (m_points.y() * m_points.z()));
181 y = int((index - (x * m_points.y() * m_points.z())) / m_points.z());
182 z = index % m_points.z();
183 return Vector3(x * m_spacing.x() + m_min.x(), y * m_spacing.y() + m_min.y(),
184 z * m_spacing.z() + m_min.z());
185 }
186
187 float Cube::value(int i, int j, int k) const
188 {
189 unsigned int index = i * m_points.y() * m_points.z() + j * m_points.z() + k;
190 if (index < m_data.size())
191 return m_data[index];
192 else
193 return 0.0;
194 }
195
196 std::array<float, 3> Cube::computeGradient(int i, int j, int k) const
197 {
198 int nx = m_points.x();
199 int ny = m_points.y();
200 int nz = m_points.z();
201 int dataIdx = (i * ny * nz) + (j * nz) + k;
202
203 std::array<std::array<float, 2>, 3> x;
204 std::array<float, 3> run;
205
206 // X-direction
207 if (i == 0) {
208 x[0][0] = m_data[dataIdx + ny * nz];
209 x[0][1] = m_data[dataIdx];
210 run[0] = m_spacing.x();
211 } else if (i == (nx - 1)) {
212 x[0][0] = m_data[dataIdx];
213 x[0][1] = m_data[dataIdx - ny * nz];
214 run[0] = m_spacing.x();
215 } else {
216 x[0][0] = m_data[dataIdx + ny * nz];
217 x[0][1] = m_data[dataIdx - ny * nz];
218 run[0] = 2 * m_spacing.x();
219 }
220
221 // Y-direction
222 if (j == 0) {
223 x[1][0] = m_data[dataIdx + nz];
224 x[1][1] = m_data[dataIdx];
225 run[1] = m_spacing.y();
226 } else if (j == (ny - 1)) {
227 x[1][0] = m_data[dataIdx];
228 x[1][1] = m_data[dataIdx - nz];
229 run[1] = m_spacing.y();
230 } else {
231 x[1][0] = m_data[dataIdx + nz];
232 x[1][1] = m_data[dataIdx - nz];
233 run[1] = 2 * m_spacing.y();
234 }
235
236 // Z-direction
237 if (k == 0) {
238 x[2][0] = m_data[dataIdx + 1];
239 x[2][1] = m_data[dataIdx];
240 run[2] = m_spacing.z();
241 } else if (k == (nz - 1)) {
242 x[2][0] = m_data[dataIdx];
243 x[2][1] = m_data[dataIdx - 1];
244 run[2] = m_spacing.z();
245 } else {
246 x[2][0] = m_data[dataIdx + 1];
247 x[2][1] = m_data[dataIdx - 1];
248 run[2] = 2 * m_spacing.z();
249 }
250
251 std::array<float, 3> ret;
252
253 ret[0] = (x[0][1] - x[0][0]) / run[0];
254 ret[1] = (x[1][1] - x[1][0]) / run[1];
255 ret[2] = (x[2][1] - x[2][0]) / run[2];
256
257 return ret;
258 }
259
260 std::array<std::array<float, 3>, 8> Cube::getGradCube(int i, int j, int k) const
261 {
262 std::array<std::array<float, 3>, 8> grad;
263
264 grad[0] = computeGradient(i, j, k);
265 grad[1] = computeGradient(i + 1, j, k);
266 grad[2] = computeGradient(i + 1, j + 1, k);
267 grad[3] = computeGradient(i, j + 1, k);
268 grad[4] = computeGradient(i, j, k + 1);
269 grad[5] = computeGradient(i + 1, j, k + 1);
270 grad[6] = computeGradient(i + 1, j + 1, k + 1);
271 grad[7] = computeGradient(i, j + 1, k + 1);
272
273 return grad;
274 }
275
276 std::array<float, 8> Cube::getValsCube(int i, int j, int k) const
277 {
278 std::array<float, 8> vals;
279
280 vals[0] = getData(i, j, k);
281 vals[1] = getData(i + 1, j, k);
282 vals[2] = getData(i + 1, j + 1, k);
283 vals[3] = getData(i, j + 1, k);
284 vals[4] = getData(i, j, k + 1);
285 vals[5] = getData(i + 1, j, k + 1);
286 vals[6] = getData(i + 1, j + 1, k + 1);
287 vals[7] = getData(i, j + 1, k + 1);
288
289 return vals;
290 }
291
292 float Cube::getData(int i, int j, int k) const
293 {
294 int ny = m_points.y();
295 int nz = m_points.z();
296 return m_data[(i * ny * nz) + (j * nz) + k];
297 }
298
299 std::array<std::array<float, 3>, 8> Cube::getPosCube(int i, int j, int k) const
300 {
301
302 std::array<std::array<float, 3>, 8> pos;
303
304 float xpos = m_min.x() + (i * m_spacing.x());
305 float ypos = m_min.y() + (j * m_spacing.y());
306 float zpos = m_min.z() + (k * m_spacing.z());
307
308 pos[0][0] = xpos;
309 pos[0][1] = ypos;
310 pos[0][2] = zpos;
311
312 pos[1][0] = xpos + m_spacing.x();
313 pos[1][1] = ypos;
314 pos[1][2] = zpos;
315
316 pos[2][0] = xpos + m_spacing.x();
317 pos[2][1] = ypos + m_spacing.y();
318 pos[2][2] = zpos;
319
320 pos[3][0] = xpos;
321 pos[3][1] = ypos + m_spacing.y();
322 pos[3][2] = zpos;
323
324 pos[4][0] = xpos;
325 pos[4][1] = ypos;
326 pos[4][2] = zpos + m_spacing.z();
327
328 pos[5][0] = xpos + m_spacing.x();
329 pos[5][1] = ypos;
330 pos[5][2] = zpos + m_spacing.z();
331
332 pos[6][0] = xpos + m_spacing.x();
333 pos[6][1] = ypos + m_spacing.y();
334 pos[6][2] = zpos + m_spacing.z();
335
336 pos[7][0] = xpos;
337 pos[7][1] = ypos + m_spacing.y();
338 pos[7][2] = zpos + m_spacing.z();
339
340 return pos;
341 }
342
343 float Cube::value(const Vector3i& pos) const
344 {
345 unsigned int index =
346 pos.x() * m_points.y() * m_points.z() + pos.y() * m_points.z() + pos.z();
347 if (index < m_data.size())
348 return m_data[index];
349 else
350 return 6969.0;
351 }
352
353 float Cube::valuef(const Vector3f& pos) const
354 {
355 // This is a really expensive operation and so should be avoided
356 // Interpolate the value at the supplied vector - trilinear interpolation...
357 Vector3f delta = pos - m_min.cast<float>();
358 // Find the integer low and high corners
359 Vector3i lC(static_cast<int>(delta.x() / m_spacing.x()),
360 static_cast<int>(delta.y() / m_spacing.y()),
361 static_cast<int>(delta.z() / m_spacing.z()));
362 Vector3i hC(lC.x() + 1, lC.y() + 1, lC.z() + 1);
363 // So there are six corners in total - work out the delta of the position
364 // and the low corner
365 const Vector3f lCf(lC.cast<float>());
366 const Vector3f spacingf(m_spacing.cast<float>());
367 Vector3f P((delta.x() - lCf.x() * spacingf.x()) / spacingf.x(),
368 (delta.y() - lCf.y() * spacingf.y()) / spacingf.y(),
369 (delta.z() - lCf.z() * spacingf.z()) / spacingf.z());
370 Vector3f dP = Vector3f(1.0f, 1.0f, 1.0f) - P;
371 // Now calculate and return the interpolated value
372 return static_cast<float>(
373 value(lC.x(), lC.y(), lC.z()) * dP.x() * dP.y() * dP.z() +
374 value(hC.x(), lC.y(), lC.z()) * P.x() * dP.y() * dP.z() +
375 value(lC.x(), hC.y(), lC.z()) * dP.x() * P.y() * dP.z() +
376 value(lC.x(), lC.y(), hC.z()) * dP.x() * dP.y() * P.z() +
377 value(hC.x(), lC.y(), hC.z()) * P.x() * dP.y() * P.z() +
378 value(lC.x(), hC.y(), hC.z()) * dP.x() * P.y() * P.z() +
379 value(hC.x(), hC.y(), lC.z()) * P.x() * P.y() * dP.z() +
380 value(hC.x(), hC.y(), hC.z()) * P.x() * P.y() * P.z());
381 }
382
383 float Cube::value(const Vector3& pos) const
384 {
385 // This is a really expensive operation and so should be avoided
386 // Interpolate the value at the supplied vector - trilinear interpolation...
387 Vector3 delta = pos - m_min;
388 // Find the integer low and high corners
389 Vector3i lC(static_cast<int>(delta.x() / m_spacing.x()),
390 static_cast<int>(delta.y() / m_spacing.y()),
391 static_cast<int>(delta.z() / m_spacing.z()));
392 Vector3i hC(lC.x() + 1, lC.y() + 1, lC.z() + 1);
393 // So there are six corners in total - work out the delta of the position
394 // and the low corner
395 Vector3 P((delta.x() - lC.x() * m_spacing.x()) / m_spacing.x(),
396 (delta.y() - lC.y() * m_spacing.y()) / m_spacing.y(),
397 (delta.z() - lC.z() * m_spacing.z()) / m_spacing.z());
398 Vector3 dP = Vector3(1.0, 1.0, 1.0) - P;
399 // Now calculate and return the interpolated value
400 return value(lC.x(), lC.y(), lC.z()) * dP.x() * dP.y() * dP.z() +
401 value(hC.x(), lC.y(), lC.z()) * P.x() * dP.y() * dP.z() +
402 value(lC.x(), hC.y(), lC.z()) * dP.x() * P.y() * dP.z() +
403 value(lC.x(), lC.y(), hC.z()) * dP.x() * dP.y() * P.z() +
404 value(hC.x(), lC.y(), hC.z()) * P.x() * dP.y() * P.z() +
405 value(lC.x(), hC.y(), hC.z()) * dP.x() * P.y() * P.z() +
406 value(hC.x(), hC.y(), lC.z()) * P.x() * P.y() * dP.z() +
407 value(hC.x(), hC.y(), hC.z()) * P.x() * P.y() * P.z();
408 }
409
410 bool Cube::setValue(unsigned int i, unsigned int j, unsigned int k,
411 float value_)
412 {
413 unsigned int index = i * m_points.y() * m_points.z() + j * m_points.z() + k;
414 if (index >= m_data.size())
415 return false;
416 m_data[index] = value_;
417 if (value_ < m_minValue)
418 m_minValue = value_;
419 else if (value_ > m_maxValue)
420 m_maxValue = value_;
421 return true;
422 }
423
424 void Cube::fill(float value_)
425 {
426 std::fill(m_data.begin(), m_data.end(), value_);
427 m_minValue = m_maxValue = value_;
428 }
429
430 bool Cube::fillStripe(unsigned int i, unsigned int j, unsigned int kfirst,
431 unsigned int klast, float value_)
432 {
433 unsigned int stripeStartIndex =
434 i * m_points.y() * m_points.z() + j * m_points.z();
435 unsigned int firstIndex = stripeStartIndex + kfirst;
436 if (firstIndex >= m_data.size())
437 return false;
438 unsigned int lastIndex = stripeStartIndex + klast;
439 if (lastIndex >= m_data.size())
440 return false;
441 std::fill(&m_data[firstIndex], &m_data[lastIndex + 1], value_);
442 return true;
443 }
444
445 } // namespace Avogadro::Core