local changes to project file
[engrid.git] / src / stlreader.cpp
blobf410283d2a6f42c9d1d4825f6debd38f5172bc76
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 "stlreader.h"
24 #include "correctsurfaceorientation.h"
26 #include "vtkEgPolyDataToUnstructuredGridFilter.h"
27 #include <vtkSTLReader.h>
28 #include <vtkCleanPolyData.h>
30 #include <QFileInfo>
31 #include "guimainwindow.h"
33 StlReader::StlReader()
35 setFormat("STL files(*.stl *.STL)");
38 void StlReader::operate()
40 QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
41 readInputFileName(file_info.completeBaseName() + ".stl");
42 if (isValid()) {
43 vtkSTLReader *stl = vtkSTLReader::New();
44 stl->MergingOn();
45 stl->SetFileName(getCFileName());
46 stl->Update();
47 EG_VTKSP(vtkPolyData, poly);
48 poly->DeepCopy(stl->GetOutput());
49 poly->BuildCells();
50 double L = 1e99;
51 for (vtkIdType cellId = 0; cellId < poly->GetNumberOfCells(); ++cellId) {
52 vtkIdType *pts, Npts;
53 poly->GetCellPoints(cellId, Npts, pts);
54 for (int i = 0; i < Npts; ++i) {
55 vec3_t x1, x2;
56 poly->GetPoints()->GetPoint(pts[i], x1.data());
57 if (i == Npts - 1) {
58 poly->GetPoints()->GetPoint(pts[0], x2.data());
59 } else {
60 poly->GetPoints()->GetPoint(pts[i+1], x2.data());
62 L = min(L, (x1-x2).abs());
65 EG_VTKSP(vtkCleanPolyData, poly_clean);
66 poly_clean->ToleranceIsAbsoluteOn();
67 poly_clean->SetAbsoluteTolerance(0.5*L);
68 poly_clean->SetInput(poly);
69 EG_VTKSP(vtkEgPolyDataToUnstructuredGridFilter, poly2ugrid);
70 poly2ugrid->SetInput(poly_clean->GetOutput());
71 poly2ugrid->Update();
73 allocateGrid(grid, poly2ugrid->GetOutput()->GetNumberOfCells(), poly2ugrid->GetOutput()->GetNumberOfPoints());
74 for (vtkIdType id_node = 0; id_node < poly2ugrid->GetOutput()->GetNumberOfPoints(); ++id_node) {
75 vec3_t x;
76 poly2ugrid->GetOutput()->GetPoints()->GetPoint(id_node, x.data());
77 grid->GetPoints()->SetPoint(id_node, x.data());
79 for (vtkIdType id_cell = 0; id_cell < poly2ugrid->GetOutput()->GetNumberOfCells(); ++id_cell) {
80 vtkIdType N_pts, *pts;
81 vtkIdType type_cell = poly2ugrid->GetOutput()->GetCellType(id_cell);
82 poly2ugrid->GetOutput()->GetCellPoints(id_cell, N_pts, pts);
83 grid->InsertNextCell(type_cell, N_pts, pts);
86 EG_VTKDCC(vtkIntArray, bc, grid, "cell_code");
87 EG_VTKDCC(vtkIntArray, orgdir, grid, "cell_orgdir");
88 EG_VTKDCC(vtkIntArray, voldir, grid, "cell_voldir");
89 EG_VTKDCC(vtkIntArray, curdir, grid, "cell_curdir");
90 for (vtkIdType id_cell = 0; id_cell < grid->GetNumberOfCells(); ++id_cell) {
91 bc->SetValue(id_cell, 999);
92 orgdir->SetValue(id_cell, 0);
93 voldir->SetValue(id_cell, 0);
94 curdir->SetValue(id_cell, 0);
96 CorrectSurfaceOrientation corr_surf;
97 corr_surf.setGrid(grid);
98 corr_surf();