added sphere tutorial
[engrid.git] / src / operation.cpp
blobfa5ef187b7acf5b338b3ca91da7fd4adceb0e579
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2010 enGits GmbH +
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 "operation.h"
25 #include "guimainwindow.h"
26 #include "egvtkobject.h"
28 #include <vtkTriangleFilter.h>
29 #include <vtkInformation.h>
30 #include <vtkInformationVector.h>
31 #include <vtkObjectFactory.h>
32 #include <vtkPointData.h>
33 #include <vtkPolyData.h>
34 #include <vtkPolygon.h>
35 #include <vtkStreamingDemandDrivenPipeline.h>
36 #include <vtkCellArray.h>
37 #include <vtkCellData.h>
38 #include <vtkCellLocator.h>
39 #include <vtkFloatArray.h>
40 #include <vtkMath.h>
41 #include <vtkCharArray.h>
43 #include <QApplication>
44 #include <QTime>
46 #include "geometrytools.h"
47 using namespace GeometryTools;
49 QSet<Operation*> Operation::garbage_operations;
51 QVector<vtkIdType> m_static_DummyCells;
52 QVector<int> m_static_DummyRCells;
53 QVector<vtkIdType> m_static_DummyNodes;
54 QVector<int> m_static_DummyRNodes;
55 QVector<QVector<int> > m_static_DummyN2N;
56 QVector<QVector<int> > m_static_DummyN2C;
57 QVector<QVector<int> > m_static_DummyC2C;
59 void Operation::collectGarbage()
61 QSet<Operation*> delete_operations;
63 foreach (Operation *op, garbage_operations)
65 if (!op->getThread().isRunning()) {
66 delete_operations.insert(op);
67 cout << "deleting Operation " << op << endl;
68 delete op;
72 foreach (Operation *op, delete_operations)
74 garbage_operations.remove(op);
78 Operation::Operation()
80 m_Grid = NULL;
81 lock_gui = false;
82 m_quicksave = false;
83 m_resetoperationcounter = false;
84 err = NULL;
85 autoset = true;
86 m_TypeName = "undefined";
89 Operation::~Operation()
91 if (err) {
92 err->display();
93 delete err;
97 void Operation::del()
99 garbage_operations.insert(this);
102 void OperationThread::run()
104 try {
105 GuiMainWindow::lock();
106 GuiMainWindow::pointer()->setBusy();
107 op->operate();
108 cout << "secs. for " << qPrintable(op->getTypeName()) << ": " << op->elapsedTime() << endl;
109 } catch (Error err) {
110 op->err = new Error();
111 *(op->err) = err;
113 GuiMainWindow::unlock();
114 GuiMainWindow::pointer()->setIdle();
117 void Operation::setTypeName(QString name)
119 int i = 0;
120 while ((i < name.size()) && (name[i].isDigit())) {
121 ++i;
123 m_TypeName = name.right(name.size() - i);
126 void Operation::operator()()
128 setStartTime();
129 if (lock_gui) {
130 if (GuiMainWindow::tryLock()) {
131 checkGrid();
132 thread.setOperation(this);
133 GuiMainWindow::unlock();
134 thread.start(QThread::LowPriority);
135 } else {
136 QMessageBox::warning(NULL, "not permitted", "Operation is not permitted while background process is running!");
138 } else {
139 checkGrid();
140 const bool gui_thread = QThread::currentThread() == QCoreApplication::instance()->thread();
141 if (gui_thread) {
142 try {
143 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
144 operate();
145 QApplication::restoreOverrideCursor();
146 //cout << "secs. for " << qPrintable(getTypeName()) << ": " << elapsedTime() << endl;
147 } catch (Error err) {
148 err.display();
150 } else {
151 operate();
153 if(m_resetoperationcounter) GuiMainWindow::pointer()->resetOperationCounter();
154 if(m_quicksave) GuiMainWindow::pointer()->quickSave();
158 void Operation::setAllCells()
160 QVector<vtkIdType> all_cells;
161 getAllCells(all_cells, m_Grid);
162 setCells(all_cells);
165 void Operation::setAllVolumeCells()
167 QVector<vtkIdType> cells;
168 getAllVolumeCells(cells, m_Grid);
169 setCells(cells);
172 void Operation::setAllSurfaceCells()
174 QVector<vtkIdType> cells;
175 getAllSurfaceCells(cells, m_Grid);
176 setCells(cells);
179 void Operation::setVolume(QString volume_name)
181 m_Part.setGrid(m_Grid);
182 m_Part.setVolume(volume_name);
185 void Operation::setMeshPartition(const MeshPartition &part)
187 m_Part.setGrid(part.getGrid());
188 m_Part.setCells(part.getCells());
191 void Operation::checkGrid()
193 if (m_Grid == NULL) {
194 m_Grid = GuiMainWindow::pointer()->getGrid();
196 l2g_t cells = getPartCells();
197 if ((cells.size() == 0) && autoset) {
198 setAllCells();
202 void Operation::updateActors()
204 mainWindow()->updateActors();
207 GuiMainWindow* Operation::mainWindow()
209 return GuiMainWindow::pointer();
212 void Operation::populateBoundaryCodes(QListWidget *lw)
214 QSet<int> bcs;
215 mainWindow()->getAllBoundaryCodes(bcs);
216 foreach(int bc, bcs) {
217 QListWidgetItem *lwi = new QListWidgetItem(lw);
218 lwi->setCheckState(Qt::Unchecked);
219 QString text = "";
220 QTextStream ts(&text);
221 ts << bc << " : " << GuiMainWindow::pointer()->getBC(bc).getName();
222 lwi->setText(text);
223 lwi->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
227 void Operation::populateVolumes(QListWidget *lw)
229 QList<VolumeDefinition> vols = mainWindow()->getAllVols();
230 foreach (VolumeDefinition V, vols) {
231 QListWidgetItem *lwi = new QListWidgetItem(lw);
232 lwi->setText(V.getName());
236 void Operation::eliminateDuplicateCells(bool surf_only)
238 QVector<vtkIdType> cells;
239 if (surf_only) {
240 getAllSurfaceCells(cells, m_Grid);
241 } else {
242 getAllCells(cells, m_Grid);
244 QVector<QVector<vtkIdType> > cell_nodes(cells.size());
246 for (int i_cells = 0; i_cells < cells.size(); ++i_cells) {
247 vtkIdType id_cell = cells[i_cells];
248 if (!surf_only || isSurface(id_cell, m_Grid)) {
249 vtkIdType N_pts, *pts;
250 m_Grid->GetCellPoints(id_cell, N_pts, pts);
251 QVector<vtkIdType> nodes(N_pts);
252 for (int i = 0; i < N_pts; ++i) {
253 nodes[i] = pts[i];
255 qSort(nodes);
256 cell_nodes[i_cells] = nodes;
259 QList<vtkIdType> new_cells;
260 for (int i_cells = 0; i_cells < cells.size(); ++i_cells) {
261 vtkIdType id_cell1 = cells[i_cells];
262 bool duplicate_cell = false;
263 if (!surf_only || isSurface(id_cell1, m_Grid)) {
264 for (int j_cells = 0; j_cells < cells.size(); ++j_cells) {
265 vtkIdType id_cell2 = cells[j_cells];
266 if (i_cells != j_cells) {
267 if (!surf_only || isSurface(id_cell2, m_Grid)) {
268 if (cell_nodes[i_cells] == cell_nodes[j_cells]) {
269 duplicate_cell = true;
270 break;
276 if (!duplicate_cell) {
277 new_cells.append(id_cell1);
280 if (surf_only) {
281 QVector<vtkIdType> vol_cells;
282 getAllVolumeCells(vol_cells, m_Grid);
283 foreach(vtkIdType id_cell, vol_cells) {
284 new_cells.append(id_cell);
287 EG_VTKSP(vtkUnstructuredGrid, new_grid);
288 makeCopy(m_Grid, new_grid, new_cells);
289 makeCopy(new_grid, m_Grid);