feature magic defaults to 1 now
[engrid.git] / src / libengrid / engrid.h
blob8df1e2b2f2a53256df568c349e00f58f5d3af206
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2013 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 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
23 #ifndef engrid_H
24 #define engrid_H
26 #include <QMessageBox>
27 #include <QtDebug>
28 #include <QString>
30 #include <vtkSmartPointer.h>
31 #include <vtkLongArray.h>
32 #include <vtkLongLongArray.h>
33 #include <vtkIntArray.h>
34 #include <vtkDoubleArray.h>
36 #include "error.h"
37 #include "math/mathvector.h"
38 #include "math/smallsquarematrix.h"
39 #include "geometrytools.h"
40 #include "containertricks.h"
42 // #include "engrid_version.h" //Moved this to the only place where it was being used for now...
44 #ifdef WIN32
45 typedef vtkLongLongArray vtkLongArray_t;
46 #else
48 #include <limits.h>
49 #if ( __WORDSIZE == 64 )
50 typedef vtkLongArray vtkLongArray_t;
51 #else
52 typedef vtkLongLongArray vtkLongArray_t;
53 #endif
55 #endif
57 #define EG_ERR_RETURN(TXT) \
58 { \
59 QString line; \
60 line.setNum(__LINE__); \
61 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
62 Error err; \
63 err.setText(txt); \
64 err.setType(Error::ExitOperation); \
65 throw err; \
68 #define EG_ERR_DISPLAY(TXT) \
69 { \
70 QString line; \
71 line.setNum(__LINE__); \
72 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
73 Error err; \
74 err.setText(txt); \
75 err.setType(Error::ExitOperation); \
76 err.display(); \
79 #ifdef QT_DEBUG
80 #define EG_BUG \
81 { \
82 QString line; \
83 line.setNum(__LINE__); \
84 QString txt = "This seems to be a bug in enGrid"; \
85 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
86 qWarning()<<txt; \
87 abort(); \
89 #else
90 #define EG_BUG \
91 { \
92 QString line; \
93 line.setNum(__LINE__); \
94 QString txt = "This seems to be a bug in enGrid"; \
95 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
96 Error err; \
97 err.setText(txt); \
98 err.setType(Error::ExitOperation); \
99 throw err; \
101 #endif
104 #define EG_ERR_STOP(TXT) \
106 QString line; \
107 line.setNum(__LINE__); \
108 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
109 Error err; \
110 err.setText(txt); \
111 err.setType(Error::ExitProgram); \
112 throw err; \
115 #define EG_VTKSP(TYPE,VAR) vtkSmartPointer<TYPE> VAR = vtkSmartPointer<TYPE>::New();
118 * Get an automatically casted pointer to a cell data array of a grid
119 * (vtkUnstructuredGrid).
120 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
121 * @param VAR the name for the C++ pointer variable
122 * @param GRID a pointer to the vtkUnstructuredGrid
123 * @param NAME the sybolic name of the data array; for example "cell_index"
125 #define EG_VTKDCC(TYPE,VAR,GRID,NAME) \
126 TYPE *VAR = NULL; \
127 if (GRID->GetCellData()->GetScalars(NAME)) { \
128 VAR = dynamic_cast<TYPE*>(GRID->GetCellData()->GetScalars(NAME)); \
129 if (!VAR) { \
130 QString msg = "type mismatch ("; \
131 int t = GRID->GetCellData()->GetScalars(NAME)->GetDataType(); \
132 if (t == VTK_VOID) msg += "VTK_VOID"; \
133 if (t == VTK_BIT) msg += "VTK_BIT"; \
134 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
135 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
136 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
137 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
138 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
139 if (t == VTK_INT) msg += "VTK_INT"; \
140 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
141 if (t == VTK_LONG) msg += "VTK_LONG"; \
142 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
143 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
144 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
145 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
146 if (t == VTK_STRING) msg += "VTK_STRING"; \
147 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
148 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
149 if (t == VTK___INT64) msg += "VTK___INT64"; \
150 if (t == VTK_UNSIGNED___INT64) msg += "VTK_UNSIGNED___INT64"; \
151 msg += ")"; \
152 EG_ERR_RETURN(msg); \
153 }; \
154 } else { \
155 QString msg = QString("array '") + NAME + "' does not exist."; \
156 EG_ERR_RETURN(msg); \
160 * Get an automatically casted pointer to a node data array of a grid
161 * (vtkUnstructuredGrid).
162 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
163 * @param VAR the name for the C++ pointer variable
164 * @param GRID a pointer to the vtkUnstructuredGrid
165 * @param NAME the sybolic name of the data array; for example "node_index"
167 #define EG_VTKDCN(TYPE,VAR,GRID,NAME) \
168 TYPE *VAR = NULL; \
169 if (GRID->GetPointData()->GetScalars(NAME)) { \
170 VAR = dynamic_cast<TYPE*>(GRID->GetPointData()->GetScalars(NAME)); \
171 if (!VAR) { \
172 QString msg = "type mismatch ("; \
173 int t = GRID->GetPointData()->GetScalars(NAME)->GetDataType(); \
174 if (t == VTK_VOID) msg += "VTK_VOID"; \
175 if (t == VTK_BIT) msg += "VTK_BIT"; \
176 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
177 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
178 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
179 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
180 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
181 if (t == VTK_INT) msg += "VTK_INT"; \
182 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
183 if (t == VTK_LONG) msg += "VTK_LONG"; \
184 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
185 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
186 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
187 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
188 if (t == VTK_STRING) msg += "VTK_STRING"; \
189 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
190 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
191 if (t == VTK___INT64) msg += "VTK___INT64"; \
192 if (t == VTK_UNSIGNED___INT64) msg += "VTK_UNSIGNED___INT64"; \
193 msg += ")"; \
194 EG_ERR_RETURN(msg); \
195 }; \
196 } else { \
197 QString msg = QString("array '") + NAME + "' does not exist."; \
198 EG_ERR_RETURN(msg); \
201 #define EG_STDINTERSLOT(OPER) \
202 OPER *oper = NULL; \
203 try { \
204 oper = new OPER(); \
205 } catch (Error err) { \
206 err.display(); \
208 if (oper) { \
209 (*oper)(); \
210 oper->del(); \
211 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(false); \
212 updateActors(); \
215 #define EG_STDSLOT(OPER) \
216 OPER *oper = NULL; \
217 try { \
218 oper = new OPER(); \
219 } catch (Error err) { \
220 err.display(); \
222 if (oper) { \
223 oper->setLockGui(); \
224 (*oper)(); \
225 oper->del(); \
226 updateActors(); \
229 #define EG_STDREADERSLOT(OPER) \
230 OPER *oper = new OPER(); \
231 (*oper)(); \
232 oper->del(); \
233 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(true); \
234 updateActors(); \
235 updateStatusBar(); \
236 zoomAll();
238 #define EG_STDCONNECT(OPER) \
239 connect(ui.action ## OPER, SIGNAL(triggered()), this, SLOT(call ## OPER ()));
242 * Perform a loop over all cells of a grid.
243 * @param ID_CELL the cell index iteration variable
244 * @param GRID a pointer to the vtkUnstructuredGrid
246 #define EG_FORALL_CELLS(ID_CELL, GRID) for (vtkIdType ID_CELL = 0; ID_CELL < GRID->GetNumberOfCells(); ++ID_CELL)
249 * Perform a loop over all nodes of a grid.
250 * @param ID_NODE the node index iteration variable
251 * @param GRID a pointer to the vtkUnstructuredGrid
253 #define EG_FORALL_NODES(ID_NODE, GRID) for (vtkIdType ID_NODE = 0; ID_NODE < GRID->GetNumberOfPoints(); ++ID_NODE)
256 * Get the type and the nodes of a cell.
257 * The following variables are created:
258 * - num_pts (number of points)
259 * - pts (array with the points)
260 * - type_cell (VTK type of the cell)
261 * @param ID_CELL the cell index
262 * @param GRID a pointer to the vtkUnstructuredGrid
264 #define EG_GET_CELL(ID_CELL, GRID) \
265 vtkIdType num_pts, *pts; \
266 vtkIdType type_cell = GRID->GetCellType(ID_CELL); \
267 GRID->GetCellPoints(ID_CELL, num_pts, pts);
269 inline double sqr(double x) { return x*x; }
271 template <class T>
272 inline T sign1(T t) {
273 if (t >= 0) return 1;
274 return -1;
277 inline int factorial_rec(int num)
279 if (num<=1)
280 return 1;
281 return factorial_rec(num-1)*num; // recursive call
284 inline int factorial_it(int num)
286 int result=1;
287 for (int i=1; i<=num; ++i)
288 result=result*i;
289 return result;
292 inline int N_Combinations(int N,int k)
294 return(factorial_rec(N)/(factorial_rec(N-k)*factorial_rec(k)));
297 inline int N_Permutations(int N,int k)
299 return(factorial_rec(N)/(factorial_rec(N-k)));
302 #define LINE "========================================================================" << endl;
304 #define USE(X) X=X
306 template <class T>
307 struct SortedPair
309 T v1, v2;
310 SortedPair();
311 SortedPair(T v1, T v2);
312 bool operator==(const SortedPair<T>& P) const{ return (v1 == P.v1) && (v2 == P.v2); }
313 bool operator<(const SortedPair<T>& P) const;
314 bool operator>(const SortedPair<T>& P) const { return !operator<(P); }
318 template <class T>
319 SortedPair<T>::SortedPair(T v1, T v2)
321 if (v1 > v2) {
322 this->v1 = v2;
323 this->v2 = v1;
324 } else {
325 this->v1 = v1;
326 this->v2 = v2;
330 template <class T>
331 SortedPair<T>::SortedPair()
333 v1 = 0;
334 v2 = 0;
337 template <class T>
338 bool SortedPair<T>::operator<(const SortedPair<T>& P) const
340 if (v1 < P.v1) {
341 return true;
342 } else if (v1 == P.v1) {
343 return v2 < P.v2;
345 return false;
348 template <class T>
349 inline uint qHash(const SortedPair<T> &P)
351 uint h1 = qHash(P.v1);
352 uint h2 = qHash(P.v2);
353 return ((h1 << 16) | (h1 >> 16)) ^ h2;
357 #endif