Added basic compilation instructions to README.md
[engrid-github.git] / src / libengrid / engrid.h
blobe1235f1cecf51f83c5534a1fc92b34e783c0426b
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 <vtkSmartPointer.h>
38 #include <vtkLongArray.h>
39 #include <vtkLongLongArray.h>
40 #include <vtkIntArray.h>
41 #include <vtkDoubleArray.h>
43 #include "error.h"
44 #include "math/mathvector.h"
45 #include "math/smallsquarematrix.h"
46 #include "geometrytools.h"
47 #include "containertricks.h"
49 // #include "engrid_version.h" //Moved this to the only place where it was being used for now...
51 #ifdef WIN32
52 typedef vtkLongLongArray vtkLongArray_t;
53 #else
55 #include <limits.h>
56 #if ( __WORDSIZE == 64 )
57 typedef vtkLongArray vtkLongArray_t;
58 #else
59 typedef vtkLongLongArray vtkLongArray_t;
60 #endif
62 #endif
64 #define EG_ERR_RETURN(TXT) \
65 { \
66 QString line; \
67 line.setNum(__LINE__); \
68 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
69 Error err; \
70 err.setText(txt); \
71 err.setType(Error::ExitOperation); \
72 QApplication::restoreOverrideCursor(); \
73 throw err; \
76 #define EG_ERR_DISPLAY(TXT) \
77 { \
78 QString line; \
79 line.setNum(__LINE__); \
80 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
81 Error err; \
82 err.setText(txt); \
83 err.setType(Error::ExitOperation); \
84 err.display(); \
87 #ifdef QT_DEBUG
88 #define EG_BUG \
89 { \
90 QString line; \
91 line.setNum(__LINE__); \
92 QString txt = "This seems to be a bug in enGrid"; \
93 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
94 qWarning()<<txt; \
95 abort(); \
97 #else
98 #define EG_BUG \
99 { \
100 QString line; \
101 line.setNum(__LINE__); \
102 QString txt = "This seems to be a bug in enGrid"; \
103 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
104 Error err; \
105 err.setText(txt); \
106 err.setType(Error::ExitOperation); \
107 throw err; \
109 #endif
112 #define EG_ERR_STOP(TXT) \
114 QString line; \
115 line.setNum(__LINE__); \
116 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
117 Error err; \
118 err.setText(txt); \
119 err.setType(Error::ExitProgram); \
120 throw err; \
123 #define EG_VTKSP(TYPE,VAR) vtkSmartPointer<TYPE> VAR = vtkSmartPointer<TYPE>::New();
126 * Get an automatically casted pointer to a cell data array of a grid
127 * (vtkUnstructuredGrid).
128 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
129 * @param VAR the name for the C++ pointer variable
130 * @param GRID a pointer to the vtkUnstructuredGrid
131 * @param NAME the sybolic name of the data array; for example "cell_index"
133 #define EG_VTKDCC(TYPE,VAR,GRID,NAME) \
134 TYPE *VAR = NULL; \
135 if (GRID->GetCellData()->GetScalars(NAME)) { \
136 VAR = dynamic_cast<TYPE*>(GRID->GetCellData()->GetScalars(NAME)); \
137 if (!VAR) { \
138 QString msg = "type mismatch ("; \
139 int t = GRID->GetCellData()->GetScalars(NAME)->GetDataType(); \
140 if (t == VTK_VOID) msg += "VTK_VOID"; \
141 if (t == VTK_BIT) msg += "VTK_BIT"; \
142 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
143 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
144 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
145 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
146 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
147 if (t == VTK_INT) msg += "VTK_INT"; \
148 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
149 if (t == VTK_LONG) msg += "VTK_LONG"; \
150 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
151 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
152 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
153 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
154 if (t == VTK_STRING) msg += "VTK_STRING"; \
155 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
156 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
157 if (t == VTK___INT64) msg += "VTK___INT64"; \
158 if (t == VTK_UNSIGNED___INT64) msg += "VTK_UNSIGNED___INT64"; \
159 msg += ")"; \
160 EG_ERR_RETURN(msg); \
161 }; \
162 } else { \
163 QString msg = QString("array '") + NAME + "' does not exist."; \
164 EG_ERR_RETURN(msg); \
168 * Get an automatically casted pointer to a node data array of a grid
169 * (vtkUnstructuredGrid).
170 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
171 * @param VAR the name for the C++ pointer variable
172 * @param GRID a pointer to the vtkUnstructuredGrid
173 * @param NAME the sybolic name of the data array; for example "node_index"
175 #define EG_VTKDCN(TYPE,VAR,GRID,NAME) \
176 TYPE *VAR = NULL; \
177 if (GRID->GetPointData()->GetScalars(NAME)) { \
178 VAR = dynamic_cast<TYPE*>(GRID->GetPointData()->GetScalars(NAME)); \
179 if (!VAR) { \
180 QString msg = "type mismatch ("; \
181 int t = GRID->GetPointData()->GetScalars(NAME)->GetDataType(); \
182 if (t == VTK_VOID) msg += "VTK_VOID"; \
183 if (t == VTK_BIT) msg += "VTK_BIT"; \
184 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
185 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
186 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
187 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
188 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
189 if (t == VTK_INT) msg += "VTK_INT"; \
190 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
191 if (t == VTK_LONG) msg += "VTK_LONG"; \
192 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
193 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
194 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
195 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
196 if (t == VTK_STRING) msg += "VTK_STRING"; \
197 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
198 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
199 if (t == VTK___INT64) msg += "VTK___INT64"; \
200 if (t == VTK_UNSIGNED___INT64) msg += "VTK_UNSIGNED___INT64"; \
201 msg += ")"; \
202 EG_ERR_RETURN(msg); \
203 }; \
204 } else { \
205 QString msg = QString("array '") + NAME + "' does not exist."; \
206 EG_ERR_RETURN(msg); \
209 #define EG_STDINTERSLOT(OPER) \
210 OPER *oper = NULL; \
211 try { \
212 oper = new OPER(); \
213 } catch (Error err) { \
214 err.display(); \
216 if (oper) { \
217 (*oper)(); \
218 oper->del(); \
219 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(false); \
220 updateActors(); \
223 #define EG_STDSLOT(OPER) \
224 OPER *oper = NULL; \
225 try { \
226 oper = new OPER(); \
227 } catch (Error err) { \
228 err.display(); \
230 if (oper) { \
231 oper->setLockGui(); \
232 (*oper)(); \
233 oper->del(); \
234 updateActors(); \
237 #define EG_STDREADERSLOT(OPER) \
238 OPER *oper = new OPER(); \
239 (*oper)(); \
240 oper->del(); \
241 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(true); \
242 updateActors(); \
243 updateStatusBar(); \
244 zoomAll();
246 #define EG_STDCONNECT(OPER) \
247 connect(ui.action ## OPER, SIGNAL(triggered()), this, SLOT(call ## OPER ()));
249 #define DUMMY_VAR(X) X = X
252 * Perform a loop over all cells of a grid.
253 * @param ID_CELL the cell index iteration variable
254 * @param GRID a pointer to the vtkUnstructuredGrid
256 #define EG_FORALL_CELLS(ID_CELL, GRID) for (vtkIdType ID_CELL = 0; ID_CELL < GRID->GetNumberOfCells(); ++ID_CELL)
259 * Perform a loop over all nodes of a grid.
260 * @param ID_NODE the node index iteration variable
261 * @param GRID a pointer to the vtkUnstructuredGrid
263 #define EG_FORALL_NODES(ID_NODE, GRID) for (vtkIdType ID_NODE = 0; ID_NODE < GRID->GetNumberOfPoints(); ++ID_NODE)
266 * Get the type and the nodes of a cell.
267 * The following variables are created:
268 * - num_pts (number of points)
269 * - pts (array with the points)
270 * - type_cell (VTK type of the cell)
271 * @param ID_CELL the cell index
272 * @param GRID a pointer to the vtkUnstructuredGrid
274 #define EG_GET_CELL(ID_CELL, GRID) \
275 vtkIdType num_pts, *pts; \
276 vtkIdType type_cell = GRID->GetCellType(ID_CELL); \
277 GRID->GetCellPoints(ID_CELL, num_pts, pts);
279 #define EG_LARGE_REAL 1e99
281 #define EG_STOPDATE(STOP_DATE) \
282 QDate stop_date = QDate::fromString(STOP_DATE, "yyyy-MM-dd"); \
283 if (stop_date <= QDate::currentDate()) { \
284 EG_ERR_RETURN(QString("This feature is not supported after ") + STOP_DATE); \
287 inline double sqr(double x) { return x*x; }
289 template <class T>
290 inline T sign1(T t) {
291 if (t >= 0) return 1;
292 return -1;
295 inline int factorial_rec(int num)
297 if (num<=1)
298 return 1;
299 return factorial_rec(num-1)*num; // recursive call
302 inline int factorial_it(int num)
304 int result=1;
305 for (int i=1; i<=num; ++i)
306 result=result*i;
307 return result;
310 inline int N_Combinations(int N,int k)
312 return(factorial_rec(N)/(factorial_rec(N-k)*factorial_rec(k)));
315 inline int N_Permutations(int N,int k)
317 return(factorial_rec(N)/(factorial_rec(N-k)));
320 #define LINE_STR "=================" << endl;
322 #define USE(X) X=X
324 template <class T>
325 struct SortedPair
327 T v1, v2;
328 SortedPair();
329 SortedPair(T v1, T v2);
330 bool operator==(const SortedPair<T>& P) const{ return (v1 == P.v1) && (v2 == P.v2); }
331 bool operator<(const SortedPair<T>& P) const;
332 bool operator>(const SortedPair<T>& P) const { return !operator<(P); }
336 template <class T>
337 SortedPair<T>::SortedPair(T v1, T v2)
339 if (v1 > v2) {
340 this->v1 = v2;
341 this->v2 = v1;
342 } else {
343 this->v1 = v1;
344 this->v2 = v2;
348 template <class T>
349 SortedPair<T>::SortedPair()
351 v1 = 0;
352 v2 = 0;
355 template <class T>
356 bool SortedPair<T>::operator<(const SortedPair<T>& P) const
358 if (v1 < P.v1) {
359 return true;
360 } else if (v1 == P.v1) {
361 return v2 < P.v2;
363 return false;
366 template <class T>
367 inline uint qHash(const SortedPair<T> &P)
369 uint h1 = qHash(P.v1);
370 uint h2 = qHash(P.v2);
371 return ((h1 << 16) | (h1 >> 16)) ^ h2;
375 #endif