de-activated insert-points for now
[engrid.git] / src / surfacemesher.cpp
blob33e3bd8f087bb12c0f280297f0cc26a71fc3d7ca
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "surfacemesher.h"
25 #include "insertpoints.h"
26 #include "removepoints.h"
27 #include "updatedesiredmeshdensity.h"
29 #include <vtkSmoothPolyDataFilter.h>
31 SurfaceMesher::SurfaceMesher() : SurfaceOperation()
33 EG_TYPENAME;
34 getSet("surface meshing", "maximal number of iterations", 20, m_NumMaxIter);
35 getSet("surface meshing", "number of smoothing steps", 1, m_NumSmoothSteps);
38 void SurfaceMesher::computeMeshDensity()
40 ///@@@ TODO: Optimize by using only one loop through nodes!
41 UpdateDesiredMeshDensity update_desired_mesh_density;
42 update_desired_mesh_density.setGrid(grid);
43 update_desired_mesh_density.setVertexMeshDensityVector(VMDvector);
44 update_desired_mesh_density();
47 void SurfaceMesher::updateNodeInfo(bool update_type)
49 setAllCells();
50 l2g_t nodes = getPartNodes();
51 foreach(vtkIdType node, nodes) {
52 if(update_type) {
53 EG_VTKDCN(vtkCharArray, node_type, grid, "node_type");//node type
54 node_type->SetValue(node, getNodeType(node));
56 EG_VTKDCN(vtkDoubleArray, node_meshdensity_current, grid, "node_meshdensity_current");//what we have
57 node_meshdensity_current->SetValue(node, CurrentMeshDensity(node));
59 EG_VTKDCN(vtkIntArray, node_specified_density, grid, "node_specified_density");//density index from table
60 VertexMeshDensity nodeVMD = getVMD(node);
61 int idx=VMDvector.indexOf(nodeVMD);
62 node_specified_density->SetValue(node, idx);
64 EG_VTKDCN(vtkDoubleArray, node_meshdensity_desired, grid, "node_meshdensity_desired");//what we want
65 if(idx!=-1) { //specified
66 //node_meshdensity_desired->SetValue(node, VMDvector[idx].density);
67 } else { //unspecified
68 //double D=DesiredMeshDensity(node);
69 //node_meshdensity_desired->SetValue(node, D);
74 void SurfaceMesher::swap()
76 SwapTriangles swap;
77 swap.setGrid(grid);
78 swap.setRespectBC(true);
79 swap.setFeatureSwap(true);
80 QSet<int> rest_bcs;
81 GuiMainWindow::pointer()->getAllBoundaryCodes(rest_bcs);
82 rest_bcs -= m_BCs;
83 swap.setBoundaryCodes(rest_bcs);
84 swap();
87 void SurfaceMesher::smooth(int N_iter)
89 LaplaceSmoother lap;
90 lap.setGrid(grid);
91 QVector<vtkIdType> cls;
92 getSurfaceCells(m_BCs, cls, grid);
93 lap.setCells(cls);
94 lap.setNumberOfIterations(N_iter);
95 lap();
98 int SurfaceMesher::insertNodes()
100 InsertPoints insert_points;
101 insert_points.setGrid(grid);
102 insert_points.setBCS(m_BCs);
103 insert_points.set_insert_FP(false);
104 insert_points.set_insert_EP(true);
105 insert_points();
106 return insert_points.getNumInserted();
109 int SurfaceMesher::deleteNodes()
111 RemovePoints remove_points;
112 remove_points.setGrid(grid);
113 remove_points.setBCS(m_BCs);
114 remove_points.set_remove_FP(true);
115 remove_points.set_remove_EP(true);
116 remove_points();
117 return remove_points.getNumRemoved();
120 void SurfaceMesher::operate()
122 EG_VTKDCN(vtkDoubleArray, md, grid, "node_meshdensity_desired");
123 for (vtkIdType id_node = 0; id_node < grid->GetNumberOfPoints(); ++id_node) {
124 md->SetValue(id_node, 1e-6);
126 updateNodeInfo(true);
127 int num_inserted = 0;
128 int num_deleted = 0;
129 int iter = 0;
130 bool done = false;
131 while (!done) {
132 computeMeshDensity();
133 num_inserted = insertNodes();
134 swap();
135 num_deleted = 0;
136 int N = 0;
137 int count = 0;
140 do {
141 N = deleteNodes();
142 num_deleted += N;
143 ++count;
144 } while ((N > 0) && (count < 20));
147 for (int i = 0; i < 5; ++i) {
148 swap();
149 smooth(m_NumSmoothSteps);
151 ++iter;
152 done = (iter >= m_NumMaxIter) || (num_inserted - num_deleted < grid->GetNumberOfPoints()/100);
153 cout << "surface mesher iteration " << iter << ":" << endl;
154 cout << " inserted nodes : " << num_inserted << endl;
155 cout << " deleted nodes : " << num_deleted << endl;
156 cout << " total nodes : " << grid->GetNumberOfPoints() << endl;
157 cout << " total cells : " << grid->GetNumberOfCells() << endl;
159 createIndices(grid);
160 updateNodeInfo(true);
161 computeMeshDensity();