merged in changes from surface_smoothing branch + made changes to engrid.pro for...
[engrid.git] / fixstl.cpp
blob511b4eb81ff9baee6a4ad12edd5e34ce97462b29
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 "fixstl.h"
24 #include "deletetetras.h"
25 #include "uniquevector.h"
27 #include <vtkDelaunay3D.h>
28 #include <vtkMergePoints.h>
30 FixSTL::FixSTL()
34 void FixSTL::createTriangles(const QList<QVector<vtkIdType> > &triangles, vtkUnstructuredGrid *tetra_grid)
36 QVector<vtkIdType> cells, nodes;
37 QVector<int> _nodes;
38 getAllCells(cells, tetra_grid);
39 getNodesFromCells(cells, nodes, tetra_grid);
40 createNodeMapping(nodes, _nodes, tetra_grid);
41 QVector<bool> active(nodes.size(),false);
42 foreach (QVector<vtkIdType> T, triangles) {
43 active[_nodes[T[0]]] = true;
44 active[_nodes[T[1]]] = true;
45 active[_nodes[T[2]]] = true;
47 int N_nodes = 0;
48 QVector<vtkIdType> old2new(nodes.size());
49 for (int i_nodes = 0; i_nodes < nodes.size(); ++i_nodes) {
50 if (active[i_nodes]) {
51 old2new[i_nodes] = N_nodes;
52 ++N_nodes;
55 allocateGrid(grid, triangles.size(), N_nodes);
56 cout << triangles.size() << ',' << N_nodes << endl;
57 vtkIdType id_node = 0;
58 for (int i_nodes = 0; i_nodes < nodes.size(); ++i_nodes) {
59 if (active[i_nodes]) {
60 vec3_t x;
61 tetra_grid->GetPoint(nodes[i_nodes],x.data());
62 grid->GetPoints()->SetPoint(id_node,x.data());
63 copyNodeData(tetra_grid, nodes[i_nodes], grid, id_node);
64 ++id_node;
67 EG_VTKDCC(vtkIntArray, bc, grid, "cell_code");
68 foreach (QVector<vtkIdType> T, triangles) {
69 vtkIdType pts[3];
70 for (int i_T = 0; i_T < 3; ++i_T) {
71 pts[i_T] = old2new[T[i_T]];
73 vtkIdType id_cell = grid->InsertNextCell(VTK_TRIANGLE,3,pts);
74 bc->SetValue(id_cell,1);
78 void FixSTL::operate()
80 cout << "checking and repairing surface triangulation" << endl;
81 EG_VTKSP(vtkUnstructuredGrid, pts_grid);
82 QVector<vtkIdType> faces, nodes;
83 getAllSurfaceCells(faces, grid);
84 getNodesFromCells(faces, nodes, grid);
85 allocateGrid(pts_grid, 0, nodes.size());
86 foreach (vtkIdType id_node, nodes) {
87 vec3_t x;
88 grid->GetPoint(id_node,x.data());
89 pts_grid->GetPoints()->SetPoint(id_node,x.data());
90 copyNodeData(grid, id_node, pts_grid, id_node);
92 EG_VTKSP(vtkDelaunay3D, delaunay);
93 delaunay->SetInput(pts_grid);
94 delaunay->Update();
95 EG_VTKSP(vtkUnstructuredGrid, tetra_grid);
96 makeCopy(delaunay->GetOutput(), tetra_grid);
97 QVector<vtkIdType> tetras;
98 getAllCellsOfType(VTK_TETRA, tetras, tetra_grid);
99 QVector<QVector<int> > t2t;
100 QList<QVector<vtkIdType> > triangles;
101 QVector<vtkIdType> T(3);
102 createCellToCell(tetras, t2t, tetra_grid);
103 EG_VTKSP(vtkMergePoints,loc);
104 loc->SetDataSet(pts_grid);
105 loc->BuildLocator();
106 for (int i_tetras = 0; i_tetras < tetras.size(); ++i_tetras) {
107 vtkIdType id_tetra = tetras[i_tetras];
108 vtkIdType *pts, N_pts;
109 tetra_grid->GetCellPoints(id_tetra, N_pts, pts);
110 // face 0
111 if (t2t[i_tetras][0] < i_tetras) {
112 T[0] = pts[2]; T[1] = pts[1]; T[2] = pts[0];
113 triangles.append(T);
115 // face 1
116 if (t2t[i_tetras][1] < i_tetras) {
117 T[0] = pts[1]; T[1] = pts[3]; T[2] = pts[0];
118 triangles.append(T);
120 // face 2
121 if (t2t[i_tetras][2] < i_tetras) {
122 T[0] = pts[3]; T[1] = pts[2]; T[2] = pts[0];
123 triangles.append(T);
125 // face 3
126 if (t2t[i_tetras][3] < i_tetras) {
127 T[0] = pts[2]; T[1] = pts[3]; T[2] = pts[1];
128 triangles.append(T);
131 createTriangles(triangles, tetra_grid);