1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
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. +
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. +
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/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 #include "deletetetras.h"
24 #include "uniquevector.h"
26 #include <vtkDelaunay3D.h>
27 #include <vtkMergePoints.h>
33 void FixSTL::createTriangles(const QList
<QVector
<vtkIdType
> > &triangles
, vtkUnstructuredGrid
*tetra_grid
)
35 QVector
<vtkIdType
> cells
, 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;
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
;
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
]) {
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
);
66 EG_VTKDCC(vtkIntArray
, bc
, m_Grid
, "cell_code");
67 foreach (QVector
<vtkIdType
> T
, triangles
) {
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
) {
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
);
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
);
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
);
109 if (t2t
[i_tetras
][0] < i_tetras
) {
110 T
[0] = pts
[2]; T
[1] = pts
[1]; T
[2] = pts
[0];
114 if (t2t
[i_tetras
][1] < i_tetras
) {
115 T
[0] = pts
[1]; T
[1] = pts
[3]; T
[2] = pts
[0];
119 if (t2t
[i_tetras
][2] < i_tetras
) {
120 T
[0] = pts
[3]; T
[1] = pts
[2]; T
[2] = pts
[0];
124 if (t2t
[i_tetras
][3] < i_tetras
) {
125 T
[0] = pts
[2]; T
[1] = pts
[3]; T
[2] = pts
[1];
129 createTriangles(triangles
, tetra_grid
);