investigating a little logic pb
[engrid.git] / operation.h
blob9e9248de0dd41c4c81a4ab493327599e211829b5
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 #ifndef operation_H
24 #define operation_H
26 class Operation;
27 class GuiMainWindow;
29 #include "egvtkobject.h"
31 #include <vtkUnstructuredGrid.h>
32 #include <vtkCellType.h>
33 #include <vtkSmartPointer.h>
35 #include <QThread>
36 #include <QMutex>
37 #include <QListWidget>
39 #include <typeinfo>
41 class OperationThread : public QThread
44 private:
46 Operation *op;
48 protected:
50 virtual void run();
52 public:
54 void setOperation(Operation *an_op) { op = an_op; };
58 struct stencil_t {
59 vtkIdType id_cell1;
60 vtkIdType id_cell2;
61 vtkIdType p[4];
62 bool valid;
64 ostream& operator<<(ostream &out, stencil_t S);
66 /**
67 * This is the base class for all mesh operations.
68 * Operations will typically be triggered by a Qt event; the MainWindow
69 * object will call operator() with the current grid as parameter.
71 class Operation : public EgVtkObject
74 friend class OperationThread;
75 OperationThread thread;
77 private: // static attributes
79 static QSet<Operation*> garbage_operations;
81 private: // attributes
83 QVector<vtkIdType> nodes_map;
84 QVector<vtkIdType> cells_map;
85 bool gui;
86 bool autoset;
87 Error *err;
89 private: // methods
91 void initMapping();
93 protected: // attributes
95 vtkUnstructuredGrid *grid;
96 QVector<vtkIdType> cells;
97 QVector<int> _cells;
98 QVector<vtkIdType> nodes;
99 QVector<int> _nodes;
100 QVector<QSet<int> > n2c;
101 QVector<QSet<int> > n2n;
102 QVector<QVector<int> > c2c;
103 QVector<bool> node_fixed;
104 QVector<bool> cell_fixed;
106 protected: // methods
108 void checkGrid();
109 void updateActors();
110 GuiMainWindow* mainWindow();
111 virtual void operate() = 0;
113 public: // methods
115 Operation();
116 virtual ~Operation();
117 void del();
119 void setGrid(vtkUnstructuredGrid *ug) { grid = ug; };
120 void setAllCells();
121 void setAllVolumeCells();
122 void setAllSurfaceCells();
123 vtkIdType getNewNode(vtkIdType id_old_node) { return nodes_map[_nodes[id_old_node]] ; };
124 vtkIdType getNewCell(vtkIdType id_old_cell) { return cells_map[_cells[id_old_cell]] ; };
125 void setNewNode(vtkIdType id_old_node, vtkIdType id_new_node) { nodes_map[_nodes[id_old_node]] = id_new_node; };
126 void setNewCell(vtkIdType id_old_cell, vtkIdType id_new_cell) { cells_map[_cells[id_old_cell]] = id_new_cell; };
127 void setGui() { gui = true; };
128 OperationThread& getThread() { return thread; };
129 void enableAutoSet() { autoset = true; };
130 void disableAutoSet() { autoset = false; };
133 * Fill a QListWidget with all available boundary codes from a grid.
134 * @param lw The QListWidget to fill.
135 * @param grid The grid to use.
137 void populateBoundaryCodes(QListWidget *lw);
139 virtual void operator()();
141 template <class T> void setCells(const T &cls);
142 template <class T> void setNodes(const T &nds);
144 static void collectGarbage();
145 stencil_t getStencil(vtkIdType id_cell1, int j1);
147 vtkIdType getClosestNode(vtkIdType a_id_node,vtkUnstructuredGrid* a_grid);
148 vtkIdType getFarthestNode(vtkIdType a_id_node,vtkUnstructuredGrid* a_grid);
150 bool SwapCells(vtkUnstructuredGrid* a_grid, stencil_t S);
151 void quad2triangle(vtkUnstructuredGrid* src,vtkIdType quadcell);
152 void quad2triangle(vtkUnstructuredGrid* src,vtkIdType quadcell,vtkIdType MovingPoint);
154 bool DeletePoint(vtkUnstructuredGrid *src, vtkIdType DeadNode);
155 int NumberOfCommonPoints(vtkIdType node1, vtkIdType node2, bool& IsTetra);
156 vtkIdType FindSnapPoint(vtkUnstructuredGrid *src, vtkIdType DeadNode);
157 bool EmptyVolume(vtkIdType DeadNode, vtkIdType PSP);
159 vec3_t GetCenter(vtkIdType cellId, double& R);
162 template <class T>
163 void Operation::setCells(const T &cls)
165 cells.resize(cls.size());
166 qCopy(cls.begin(), cls.end(), cells.begin());
167 getNodesFromCells(cells, nodes, grid);
168 createCellMapping(cells, _cells, grid);
169 createNodeMapping(nodes, _nodes, grid);
170 createNodeToCell(cells, nodes, _nodes, n2c, grid);
171 createNodeToNode(cells, nodes, _nodes, n2n, grid);
172 createCellToCell(cells, c2c, grid);
173 node_fixed.fill(nodes.size(), false);
174 cell_fixed.fill(cells.size(), false);
175 initMapping();
178 template <class T>
179 void Operation::setNodes(const T &nds)
181 nodes.resize(nds.size());
182 qCopy(nds.begin(), nds.end(), nodes.begin());
183 createNodeMapping(nodes, _nodes, grid);
184 QSet<vtkIdType> cls;
185 for (vtkIdType id_cell = 0; id_cell < grid->GetNumberOfCells(); ++id_cell) {
186 vtkIdType *pts, N_pts;
187 grid->GetCellPoints(id_cell, N_pts, pts);
188 for (int i_pts = 0; i_pts < N_pts; ++i_pts) {
189 if (_nodes[pts[i_pts]] >= 0) {
190 cls.insert(id_cell);
191 break;
195 cells.resize(cls.size());
196 qCopy(cls.begin(), cls.end(), cells.begin());
197 createCellMapping(cells, _cells, grid);
198 createNodeToCell(cells, nodes, _nodes, n2c, grid);
199 createNodeToNode(cells, nodes, _nodes, n2n, grid);
200 createCellToCell(cells, c2c, grid);
201 node_fixed.fill(nodes.size(), false);
202 cell_fixed.fill(cells.size(), false);
203 initMapping();
206 //////////////////////////////////////////////
207 double CurrentVertexAvgDist(vtkIdType a_vertex,QVector< QSet< int > > &n2n,vtkUnstructuredGrid *a_grid);
208 double CurrentMeshDensity(vtkIdType a_vertex,QVector< QSet< int > > &n2n,vtkUnstructuredGrid *a_grid);
209 double DesiredVertexAvgDist(vtkIdType a_vertex,QVector< QSet< int > > &n2n,vtkUnstructuredGrid *a_grid);
210 double DesiredMeshDensity(vtkIdType a_vertex,QVector< QSet< int > > &n2n,vtkUnstructuredGrid *a_grid);
212 #endif