DrNUM template and exort mechanism appears to work
[engrid-github.git] / src / libengrid / pointfinder.cpp
blobfb388130190a64d42fec5595324423a1449580ab
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "pointfinder.h"
23 PointFinder::PointFinder()
25 m_MinSize = 1.0;
26 m_MaxPoints = 100;
29 void PointFinder::setGrid(vtkUnstructuredGrid *grid)
31 QVector<vec3_t> points(grid->GetNumberOfPoints());
32 for (vtkIdType id_node = 0; id_node < grid->GetNumberOfPoints(); ++id_node) {
33 grid->GetPoint(id_node, points[id_node].data());
35 setPoints(points);
38 int PointFinder::refine()
40 int N = m_Octree.getNumCells();
41 m_Octree.setSmoothTransitionOff();
42 int N_new = 0;
43 m_Octree.resetRefineMarks();
44 for (int cell = 0; cell < m_Octree.getNumCells(); ++cell) {
45 double dx = m_Octree.getDx(cell);
46 if (!m_Octree.hasChildren(cell) && m_Buckets[cell].size() > m_MaxPoints && dx > 2*m_MinSize) {
47 m_Octree.markToRefine(cell);
48 ++N_new;
51 int N_mrk = 0;
52 for (int cell = 0; cell < m_Octree.getNumCells(); ++cell) {
53 if (m_Octree.markedForRefine(cell)) {
54 ++N_mrk;
57 N_new *= 8;
58 m_Octree.refineAll();
59 if (m_Octree.getNumCells() != N + N_new) {
60 EG_BUG;
62 m_Buckets.insert(N, N_new, QList<int>());
63 for (int cell = N; cell < m_Octree.getNumCells(); ++cell) {
64 m_Timer << " " << m_Octree.getNumCells() << " octree cells" << Timer::endl;
65 if (m_Octree.getNumCells() != N + N_new) {
66 EG_BUG;
68 int parent = m_Octree.getParent(cell);
69 if (parent < 0) {
70 EG_BUG;
72 foreach (int i_points, m_Buckets[parent]) {
73 vec3_t xcell = m_Octree.getCellCentre(cell);
74 vec3_t x = m_Points[i_points];
75 bool append = true;
77 double Dx = m_Octree.getDx(cell);
78 double Dy = m_Octree.getDy(cell);
79 double Dz = m_Octree.getDz(cell);
80 if (x[0] < xcell[0] - 1.5*Dx || x[0] > xcell[0] + 1.5*Dx) append = false;
81 else if (x[1] < xcell[1] - 1.5*Dy || x[1] > xcell[1] + 1.5*Dy) append = false;
82 else if (x[2] < xcell[2] - 1.5*Dz || x[2] > xcell[2] + 1.5*Dz) append = false;
84 if (append) {
85 m_Buckets[cell].append(i_points);
89 return N_new;
92 void PointFinder::getClosePoints(vec3_t x, QVector<int> &points, double dist)
94 if (m_Octree.isInsideBounds(x)) {
95 int cell = m_Octree.findCell(x);
96 if (cell < 0) {
97 EG_BUG;
99 if (m_Octree.hasChildren(cell)) {
100 EG_BUG;
102 while ((m_Buckets[cell].size() == 0 || dist > m_Octree.getDx(cell)) && m_Octree.getParent(cell) >= 0) {
103 cell = m_Octree.getParent(cell);
105 points.resize(m_Buckets[cell].size());
106 qCopy(m_Buckets[cell].begin(), m_Buckets[cell].end(), points.begin());
107 } else {
108 points.resize(m_Points.size());
109 for (int i = 0; i < points.size(); ++i) {
110 points[i] = i;
115 void PointFinder::writeOctreeMesh(QString file_name)
117 EG_VTKSP(vtkUnstructuredGrid, otg);
118 m_Octree.toVtkGridHangingNodes(otg);
119 writeGrid(otg, file_name);