limited volume meshing to boundary layer only
[engrid-github.git] / src / libengrid / engrid.h
blob2ccd97c6e1525cbac5964a34822786e9a7f45022
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
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. +
11 // + +
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. +
16 // + +
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/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #ifndef engrid_H
22 #define engrid_H
24 //#ifdef VTK_VERSION_LESS_THAN_6_1
25 //#define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
26 //#else
27 //#include <vtkAutoInit.h>
28 //VTK_MODULE_INIT(vtkRenderingOpenGL);
29 //#endif
31 #include <QMessageBox>
32 #include <QtDebug>
33 #include <QString>
34 #include <QDate>
35 #include <QApplication>
37 #include <vtkIdList.h>
38 #include <vtkSmartPointer.h>
39 #include <vtkLongArray.h>
40 #include <vtkLongLongArray.h>
41 #include <vtkIntArray.h>
42 #include <vtkDoubleArray.h>
43 #include <vtkSignedCharArray.h>
45 #include "error.h"
46 #include "math/mathvector.h"
47 #include "math/smallsquarematrix.h"
48 #include "geometrytools.h"
49 #include "containertricks.h"
51 // #include "engrid_version.h" //Moved this to the only place where it was being used for now...
53 // #ifdef WIN32
54 // typedef vtkLongLongArray vtkLongArray_t;
55 // #else
57 // #include <limits.h>
58 // #if ( __WORDSIZE == 64 )
59 // typedef vtkLongArray vtkLongArray_t;
60 // #else
61 // typedef vtkLongLongArray vtkLongArray_t;
62 // #endif
64 // #endif
66 typedef vtkLongLongArray vtkLongArray_t;
67 typedef vtkSignedCharArray vtkCharArray_t;
69 #define EG_ERR_RETURN(TXT) \
70 { \
71 QString line; \
72 line.setNum(__LINE__); \
73 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
74 Error err; \
75 err.setText(txt); \
76 err.setType(Error::ExitOperation); \
77 QApplication::restoreOverrideCursor(); \
78 throw err; \
81 #define EG_ERR_DISPLAY(TXT) \
82 { \
83 QString line; \
84 line.setNum(__LINE__); \
85 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
86 Error err; \
87 err.setText(txt); \
88 err.setType(Error::ExitOperation); \
89 err.display(); \
92 #ifdef QT_DEBUG
93 #define EG_BUG \
94 { \
95 QString line; \
96 line.setNum(__LINE__); \
97 QString txt = "This seems to be a bug in enGrid"; \
98 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
99 qWarning()<<txt; \
100 abort(); \
102 #else
103 #define EG_BUG \
105 QString line; \
106 line.setNum(__LINE__); \
107 QString txt = "This seems to be a bug in enGrid"; \
108 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
109 Error err; \
110 err.setText(txt); \
111 err.setType(Error::ExitOperation); \
112 throw err; \
114 #endif
117 #define EG_ERR_STOP(TXT) \
119 QString line; \
120 line.setNum(__LINE__); \
121 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
122 Error err; \
123 err.setText(txt); \
124 err.setType(Error::ExitProgram); \
125 throw err; \
128 #define EG_VTKSP(TYPE,VAR) vtkSmartPointer<TYPE> VAR = vtkSmartPointer<TYPE>::New();
131 * Get an automatically casted pointer to a cell data array of a grid
132 * (vtkUnstructuredGrid).
133 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
134 * @param VAR the name for the C++ pointer variable
135 * @param GRID a pointer to the vtkUnstructuredGrid
136 * @param NAME the sybolic name of the data array; for example "cell_index"
138 #define EG_VTKDCC(TYPE,VAR,GRID,NAME) \
139 TYPE *VAR = NULL; \
140 if (GRID->GetCellData()->GetScalars(NAME)) { \
141 VAR = dynamic_cast<TYPE*>(GRID->GetCellData()->GetScalars(NAME)); \
142 if (!VAR) { \
143 QString msg = "type mismatch ("; \
144 int t = GRID->GetCellData()->GetScalars(NAME)->GetDataType(); \
145 if (t == VTK_VOID) msg += "VTK_VOID"; \
146 if (t == VTK_BIT) msg += "VTK_BIT"; \
147 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
148 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
149 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
150 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
151 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
152 if (t == VTK_INT) msg += "VTK_INT"; \
153 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
154 if (t == VTK_LONG) msg += "VTK_LONG"; \
155 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
156 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
157 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
158 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
159 if (t == VTK_STRING) msg += "VTK_STRING"; \
160 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
161 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
162 msg += ")"; \
163 EG_ERR_RETURN(msg); \
164 }; \
165 } else { \
166 QString msg = QString("array '") + NAME + "' does not exist."; \
167 EG_ERR_RETURN(msg); \
171 * Get an automatically casted pointer to a node data array of a grid
172 * (vtkUnstructuredGrid).
173 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
174 * @param VAR the name for the C++ pointer variable
175 * @param GRID a pointer to the vtkUnstructuredGrid
176 * @param NAME the sybolic name of the data array; for example "node_index"
178 #define EG_VTKDCN(TYPE,VAR,GRID,NAME) \
179 TYPE *VAR = NULL; \
180 if (GRID->GetPointData()->GetScalars(NAME)) { \
181 VAR = dynamic_cast<TYPE*>(GRID->GetPointData()->GetScalars(NAME)); \
182 if (!VAR) { \
183 QString msg = "type mismatch ("; \
184 int t = GRID->GetPointData()->GetScalars(NAME)->GetDataType(); \
185 if (t == VTK_VOID) msg += "VTK_VOID"; \
186 if (t == VTK_BIT) msg += "VTK_BIT"; \
187 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
188 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
189 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
190 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
191 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
192 if (t == VTK_INT) msg += "VTK_INT"; \
193 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
194 if (t == VTK_LONG) msg += "VTK_LONG"; \
195 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
196 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
197 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
198 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
199 if (t == VTK_STRING) msg += "VTK_STRING"; \
200 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
201 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
202 msg += ")"; \
203 EG_ERR_RETURN(msg); \
204 }; \
205 } else { \
206 QString msg = QString("array '") + NAME + "' does not exist."; \
207 EG_ERR_RETURN(msg); \
210 #define EG_STDINTERSLOT(OPER) \
211 OPER *oper = NULL; \
212 try { \
213 oper = new OPER(); \
214 } catch (Error err) { \
215 err.display(); \
217 if (oper) { \
218 (*oper)(); \
219 oper->del(); \
220 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(false); \
221 updateActors(); \
224 #define EG_STDSLOT(OPER) \
225 OPER *oper = NULL; \
226 try { \
227 oper = new OPER(); \
228 } catch (Error err) { \
229 err.display(); \
231 if (oper) { \
232 oper->setLockGui(); \
233 (*oper)(); \
234 oper->del(); \
235 updateActors(); \
238 #define EG_STDREADERSLOT(OPER) \
239 OPER *oper = new OPER(); \
240 (*oper)(); \
241 oper->del(); \
242 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(true); \
243 updateActors(); \
244 updateStatusBar(); \
245 zoomAll();
247 #define EG_STDCONNECT(OPER) \
248 connect(ui.action ## OPER, SIGNAL(triggered()), this, SLOT(call ## OPER ()));
250 #define DUMMY_VAR(X) X = X
253 * Perform a loop over all cells of a grid.
254 * @param ID_CELL the cell index iteration variable
255 * @param GRID a pointer to the vtkUnstructuredGrid
257 #define EG_FORALL_CELLS(ID_CELL, GRID) for (vtkIdType ID_CELL = 0; ID_CELL < GRID->GetNumberOfCells(); ++ID_CELL)
260 * Perform a loop over all nodes of a grid.
261 * @param ID_NODE the node index iteration variable
262 * @param GRID a pointer to the vtkUnstructuredGrid
264 #define EG_FORALL_NODES(ID_NODE, GRID) for (vtkIdType ID_NODE = 0; ID_NODE < GRID->GetNumberOfPoints(); ++ID_NODE)
267 * Get the type and the nodes of a cell.
268 * The following variables are created:
269 * - num_pts (number of points)
270 * - pts (array with the points)
271 * - type_cell (VTK type of the cell)
272 * @param ID_CELL the cell index
273 * @param GRID a pointer to the vtkUnstructuredGrid
275 #define EG_GET_CELL(ID_CELL, GRID) \
276 vtkSmartPointer<vtkIdList> ptIds = vtkSmartPointer<vtkIdList>::New(); \
277 vtkIdType type_cell = GRID->GetCellType(ID_CELL); \
278 GRID->GetCellPoints(ID_CELL, ptIds); \
279 vtkIdType num_pts = ptIds->GetNumberOfIds(); \
280 std::vector<vtkIdType> pts(num_pts); \
281 for (int i_pts = 0; i_pts < num_pts; ++i_pts) { \
282 pts[i_pts] = ptIds->GetId(i_pts); \
286 #define EG_LARGE_REAL 1e99
288 #define EG_STOPDATE(STOP_DATE) \
289 QDate stop_date = QDate::fromString(STOP_DATE, "yyyy-MM-dd"); \
290 if (stop_date <= QDate::currentDate()) { \
291 EG_ERR_RETURN(QString("This feature is not supported after ") + STOP_DATE); \
294 inline double sqr(double x) { return x*x; }
296 template <class T>
297 inline T sign1(T t) {
298 if (t >= 0) return 1;
299 return -1;
302 inline int factorial_rec(int num)
304 if (num<=1)
305 return 1;
306 return factorial_rec(num-1)*num; // recursive call
309 inline int factorial_it(int num)
311 int result=1;
312 for (int i=1; i<=num; ++i)
313 result=result*i;
314 return result;
317 inline int N_Combinations(int N,int k)
319 return(factorial_rec(N)/(factorial_rec(N-k)*factorial_rec(k)));
322 inline int N_Permutations(int N,int k)
324 return(factorial_rec(N)/(factorial_rec(N-k)));
327 #define LINE_STR "=================" << endl;
329 #define USE(X) X=X
331 template <class T>
332 struct SortedPair
334 T v1, v2;
335 SortedPair();
336 SortedPair(T v1, T v2);
337 bool operator==(const SortedPair<T>& P) const{ return (v1 == P.v1) && (v2 == P.v2); }
338 bool operator<(const SortedPair<T>& P) const;
339 bool operator>(const SortedPair<T>& P) const { return !operator<(P); }
343 template <class T>
344 SortedPair<T>::SortedPair(T v1, T v2)
346 if (v1 > v2) {
347 this->v1 = v2;
348 this->v2 = v1;
349 } else {
350 this->v1 = v1;
351 this->v2 = v2;
355 template <class T>
356 SortedPair<T>::SortedPair()
358 v1 = 0;
359 v2 = 0;
362 template <class T>
363 bool SortedPair<T>::operator<(const SortedPair<T>& P) const
365 if (v1 < P.v1) {
366 return true;
367 } else if (v1 == P.v1) {
368 return v2 < P.v2;
370 return false;
373 template <class T>
374 inline uint qHash(const SortedPair<T> &P)
376 uint h1 = qHash(P.v1);
377 uint h2 = qHash(P.v2);
378 return ((h1 << 16) | (h1 >> 16)) ^ h2;
381 template<class C>
382 inline vtkSmartPointer<vtkIdList> idListFromVector(const C& c)
384 vtkSmartPointer<vtkIdList> id_list = vtkSmartPointer<vtkIdList>::New();
385 id_list->SetNumberOfIds(c.size());
386 for (int i = 0; i < c.size(); ++i) {
387 id_list->SetId(i, c[i]);
389 return id_list;
393 #endif