limited volume meshing to boundary layer only
[engrid-github.git] / src / libengrid / fixstl.cpp
blob3ccade98bc9481645a964560793044ae71506676
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 "fixstl.h"
22 #include "deletetetras.h"
23 #include "engrid.h"
24 #include "uniquevector.h"
26 #include <vtkDelaunay3D.h>
27 #include <vtkMergePoints.h>
29 FixSTL::FixSTL()
33 void FixSTL::createTriangles(const QList<QVector<vtkIdType> > &triangles, vtkUnstructuredGrid *tetra_grid)
35 QVector<vtkIdType> cells, nodes;
36 QVector<int> _nodes;
37 getAllCells(cells, tetra_grid);
38 getNodesFromCells(cells, nodes, tetra_grid);
39 createNodeMapping(nodes, _nodes, tetra_grid);
40 QVector<bool> active(nodes.size(),false);
41 foreach (QVector<vtkIdType> T, triangles) {
42 active[_nodes[T[0]]] = true;
43 active[_nodes[T[1]]] = true;
44 active[_nodes[T[2]]] = true;
46 int N_nodes = 0;
47 QVector<vtkIdType> old2new(nodes.size());
48 for (int i_nodes = 0; i_nodes < nodes.size(); ++i_nodes) {
49 if (active[i_nodes]) {
50 old2new[i_nodes] = N_nodes;
51 ++N_nodes;
54 allocateGrid(m_Grid, triangles.size(), N_nodes);
55 cout << triangles.size() << ',' << N_nodes << endl;
56 vtkIdType id_node = 0;
57 for (int i_nodes = 0; i_nodes < nodes.size(); ++i_nodes) {
58 if (active[i_nodes]) {
59 vec3_t x;
60 tetra_grid->GetPoint(nodes[i_nodes],x.data());
61 m_Grid->GetPoints()->SetPoint(id_node,x.data());
62 copyNodeData(tetra_grid, nodes[i_nodes], m_Grid, id_node);
63 ++id_node;
66 EG_VTKDCC(vtkIntArray, bc, m_Grid, "cell_code");
67 foreach (QVector<vtkIdType> T, triangles) {
68 vtkIdType pts[3];
69 for (int i_T = 0; i_T < 3; ++i_T) {
70 pts[i_T] = old2new[T[i_T]];
72 vtkIdType id_cell = m_Grid->InsertNextCell(VTK_TRIANGLE,3,pts);
73 bc->SetValue(id_cell,1);
77 void FixSTL::operate()
79 cout << "checking and repairing surface triangulation" << endl;
80 EG_VTKSP(vtkUnstructuredGrid, pts_grid);
81 QVector<vtkIdType> faces, nodes;
82 getAllSurfaceCells(faces, m_Grid);
83 getNodesFromCells(faces, nodes, m_Grid);
84 allocateGrid(pts_grid, 0, nodes.size());
85 foreach (vtkIdType id_node, nodes) {
86 vec3_t x;
87 m_Grid->GetPoint(id_node,x.data());
88 pts_grid->GetPoints()->SetPoint(id_node,x.data());
89 copyNodeData(m_Grid, id_node, pts_grid, id_node);
91 EG_VTKSP(vtkDelaunay3D, delaunay);
92 delaunay->SetInputData(pts_grid);
93 delaunay->Update();
94 EG_VTKSP(vtkUnstructuredGrid, tetra_grid);
95 makeCopy(delaunay->GetOutput(), tetra_grid);
96 QVector<vtkIdType> tetras;
97 getAllCellsOfType(VTK_TETRA, tetras, tetra_grid);
98 QVector<QVector<int> > t2t;
99 QList<QVector<vtkIdType> > triangles;
100 QVector<vtkIdType> T(3);
101 createCellToCell(tetras, t2t, tetra_grid);
102 EG_VTKSP(vtkMergePoints,loc);
103 loc->SetDataSet(pts_grid);
104 loc->BuildLocator();
105 for (int i_tetras = 0; i_tetras < tetras.size(); ++i_tetras) {
106 vtkIdType id_tetra = tetras[i_tetras];
107 EG_GET_CELL(id_tetra, m_Grid);
108 // face 0
109 if (t2t[i_tetras][0] < i_tetras) {
110 T[0] = pts[2]; T[1] = pts[1]; T[2] = pts[0];
111 triangles.append(T);
113 // face 1
114 if (t2t[i_tetras][1] < i_tetras) {
115 T[0] = pts[1]; T[1] = pts[3]; T[2] = pts[0];
116 triangles.append(T);
118 // face 2
119 if (t2t[i_tetras][2] < i_tetras) {
120 T[0] = pts[3]; T[1] = pts[2]; T[2] = pts[0];
121 triangles.append(T);
123 // face 3
124 if (t2t[i_tetras][3] < i_tetras) {
125 T[0] = pts[2]; T[1] = pts[3]; T[2] = pts[1];
126 triangles.append(T);
129 createTriangles(triangles, tetra_grid);