compiles on openSUSE 15.4 part 2
[engrid-github.git] / src / libengrid / engrid.h
blob966d07ec3f076839b71c8be2316efbdac988eaea
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>
44 #include "error.h"
45 #include "math/mathvector.h"
46 #include "math/smallsquarematrix.h"
47 #include "geometrytools.h"
48 #include "containertricks.h"
50 // #include "engrid_version.h" //Moved this to the only place where it was being used for now...
52 #ifdef WIN32
53 typedef vtkLongLongArray vtkLongArray_t;
54 #else
56 #include <limits.h>
57 #if ( __WORDSIZE == 64 )
58 typedef vtkLongArray vtkLongArray_t;
59 #else
60 typedef vtkLongLongArray vtkLongArray_t;
61 #endif
63 #endif
65 #define EG_ERR_RETURN(TXT) \
66 { \
67 QString line; \
68 line.setNum(__LINE__); \
69 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
70 Error err; \
71 err.setText(txt); \
72 err.setType(Error::ExitOperation); \
73 QApplication::restoreOverrideCursor(); \
74 throw err; \
77 #define EG_ERR_DISPLAY(TXT) \
78 { \
79 QString line; \
80 line.setNum(__LINE__); \
81 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
82 Error err; \
83 err.setText(txt); \
84 err.setType(Error::ExitOperation); \
85 err.display(); \
88 #ifdef QT_DEBUG
89 #define EG_BUG \
90 { \
91 QString line; \
92 line.setNum(__LINE__); \
93 QString txt = "This seems to be a bug in enGrid"; \
94 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
95 qWarning()<<txt; \
96 abort(); \
98 #else
99 #define EG_BUG \
101 QString line; \
102 line.setNum(__LINE__); \
103 QString txt = "This seems to be a bug in enGrid"; \
104 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
105 Error err; \
106 err.setText(txt); \
107 err.setType(Error::ExitOperation); \
108 throw err; \
110 #endif
113 #define EG_ERR_STOP(TXT) \
115 QString line; \
116 line.setNum(__LINE__); \
117 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
118 Error err; \
119 err.setText(txt); \
120 err.setType(Error::ExitProgram); \
121 throw err; \
124 #define EG_VTKSP(TYPE,VAR) vtkSmartPointer<TYPE> VAR = vtkSmartPointer<TYPE>::New();
127 * Get an automatically casted pointer to a cell data array of a grid
128 * (vtkUnstructuredGrid).
129 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
130 * @param VAR the name for the C++ pointer variable
131 * @param GRID a pointer to the vtkUnstructuredGrid
132 * @param NAME the sybolic name of the data array; for example "cell_index"
134 #define EG_VTKDCC(TYPE,VAR,GRID,NAME) \
135 TYPE *VAR = NULL; \
136 if (GRID->GetCellData()->GetScalars(NAME)) { \
137 VAR = dynamic_cast<TYPE*>(GRID->GetCellData()->GetScalars(NAME)); \
138 if (!VAR) { \
139 QString msg = "type mismatch ("; \
140 int t = GRID->GetCellData()->GetScalars(NAME)->GetDataType(); \
141 if (t == VTK_VOID) msg += "VTK_VOID"; \
142 if (t == VTK_BIT) msg += "VTK_BIT"; \
143 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
144 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
145 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
146 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
147 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
148 if (t == VTK_INT) msg += "VTK_INT"; \
149 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
150 if (t == VTK_LONG) msg += "VTK_LONG"; \
151 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
152 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
153 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
154 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
155 if (t == VTK_STRING) msg += "VTK_STRING"; \
156 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
157 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
158 msg += ")"; \
159 EG_ERR_RETURN(msg); \
160 }; \
161 } else { \
162 QString msg = QString("array '") + NAME + "' does not exist."; \
163 EG_ERR_RETURN(msg); \
167 * Get an automatically casted pointer to a node data array of a grid
168 * (vtkUnstructuredGrid).
169 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
170 * @param VAR the name for the C++ pointer variable
171 * @param GRID a pointer to the vtkUnstructuredGrid
172 * @param NAME the sybolic name of the data array; for example "node_index"
174 #define EG_VTKDCN(TYPE,VAR,GRID,NAME) \
175 TYPE *VAR = NULL; \
176 if (GRID->GetPointData()->GetScalars(NAME)) { \
177 VAR = dynamic_cast<TYPE*>(GRID->GetPointData()->GetScalars(NAME)); \
178 if (!VAR) { \
179 QString msg = "type mismatch ("; \
180 int t = GRID->GetPointData()->GetScalars(NAME)->GetDataType(); \
181 if (t == VTK_VOID) msg += "VTK_VOID"; \
182 if (t == VTK_BIT) msg += "VTK_BIT"; \
183 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
184 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
185 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
186 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
187 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
188 if (t == VTK_INT) msg += "VTK_INT"; \
189 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
190 if (t == VTK_LONG) msg += "VTK_LONG"; \
191 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
192 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
193 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
194 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
195 if (t == VTK_STRING) msg += "VTK_STRING"; \
196 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
197 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
198 msg += ")"; \
199 EG_ERR_RETURN(msg); \
200 }; \
201 } else { \
202 QString msg = QString("array '") + NAME + "' does not exist."; \
203 EG_ERR_RETURN(msg); \
206 #define EG_STDINTERSLOT(OPER) \
207 OPER *oper = NULL; \
208 try { \
209 oper = new OPER(); \
210 } catch (Error err) { \
211 err.display(); \
213 if (oper) { \
214 (*oper)(); \
215 oper->del(); \
216 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(false); \
217 updateActors(); \
220 #define EG_STDSLOT(OPER) \
221 OPER *oper = NULL; \
222 try { \
223 oper = new OPER(); \
224 } catch (Error err) { \
225 err.display(); \
227 if (oper) { \
228 oper->setLockGui(); \
229 (*oper)(); \
230 oper->del(); \
231 updateActors(); \
234 #define EG_STDREADERSLOT(OPER) \
235 OPER *oper = new OPER(); \
236 (*oper)(); \
237 oper->del(); \
238 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(true); \
239 updateActors(); \
240 updateStatusBar(); \
241 zoomAll();
243 #define EG_STDCONNECT(OPER) \
244 connect(ui.action ## OPER, SIGNAL(triggered()), this, SLOT(call ## OPER ()));
246 #define DUMMY_VAR(X) X = X
249 * Perform a loop over all cells of a grid.
250 * @param ID_CELL the cell index iteration variable
251 * @param GRID a pointer to the vtkUnstructuredGrid
253 #define EG_FORALL_CELLS(ID_CELL, GRID) for (vtkIdType ID_CELL = 0; ID_CELL < GRID->GetNumberOfCells(); ++ID_CELL)
256 * Perform a loop over all nodes of a grid.
257 * @param ID_NODE the node index iteration variable
258 * @param GRID a pointer to the vtkUnstructuredGrid
260 #define EG_FORALL_NODES(ID_NODE, GRID) for (vtkIdType ID_NODE = 0; ID_NODE < GRID->GetNumberOfPoints(); ++ID_NODE)
263 * Get the type and the nodes of a cell.
264 * The following variables are created:
265 * - num_pts (number of points)
266 * - pts (array with the points)
267 * - type_cell (VTK type of the cell)
268 * @param ID_CELL the cell index
269 * @param GRID a pointer to the vtkUnstructuredGrid
271 #define EG_GET_CELL(ID_CELL, GRID) \
272 vtkSmartPointer<vtkIdList> ptIds = vtkSmartPointer<vtkIdList>::New(); \
273 vtkIdType type_cell = GRID->GetCellType(ID_CELL); \
274 GRID->GetCellPoints(ID_CELL, ptIds); \
275 vtkIdType num_pts = ptIds->GetNumberOfIds(); \
276 std::vector<vtkIdType> pts(num_pts); \
277 for (int i_pts = 0; i_pts < num_pts; ++i_pts) { \
278 pts[i_pts] = ptIds->GetId(i_pts); \
282 #define EG_LARGE_REAL 1e99
284 #define EG_STOPDATE(STOP_DATE) \
285 QDate stop_date = QDate::fromString(STOP_DATE, "yyyy-MM-dd"); \
286 if (stop_date <= QDate::currentDate()) { \
287 EG_ERR_RETURN(QString("This feature is not supported after ") + STOP_DATE); \
290 inline double sqr(double x) { return x*x; }
292 template <class T>
293 inline T sign1(T t) {
294 if (t >= 0) return 1;
295 return -1;
298 inline int factorial_rec(int num)
300 if (num<=1)
301 return 1;
302 return factorial_rec(num-1)*num; // recursive call
305 inline int factorial_it(int num)
307 int result=1;
308 for (int i=1; i<=num; ++i)
309 result=result*i;
310 return result;
313 inline int N_Combinations(int N,int k)
315 return(factorial_rec(N)/(factorial_rec(N-k)*factorial_rec(k)));
318 inline int N_Permutations(int N,int k)
320 return(factorial_rec(N)/(factorial_rec(N-k)));
323 #define LINE_STR "=================" << endl;
325 #define USE(X) X=X
327 template <class T>
328 struct SortedPair
330 T v1, v2;
331 SortedPair();
332 SortedPair(T v1, T v2);
333 bool operator==(const SortedPair<T>& P) const{ return (v1 == P.v1) && (v2 == P.v2); }
334 bool operator<(const SortedPair<T>& P) const;
335 bool operator>(const SortedPair<T>& P) const { return !operator<(P); }
339 template <class T>
340 SortedPair<T>::SortedPair(T v1, T v2)
342 if (v1 > v2) {
343 this->v1 = v2;
344 this->v2 = v1;
345 } else {
346 this->v1 = v1;
347 this->v2 = v2;
351 template <class T>
352 SortedPair<T>::SortedPair()
354 v1 = 0;
355 v2 = 0;
358 template <class T>
359 bool SortedPair<T>::operator<(const SortedPair<T>& P) const
361 if (v1 < P.v1) {
362 return true;
363 } else if (v1 == P.v1) {
364 return v2 < P.v2;
366 return false;
369 template <class T>
370 inline uint qHash(const SortedPair<T> &P)
372 uint h1 = qHash(P.v1);
373 uint h2 = qHash(P.v2);
374 return ((h1 << 16) | (h1 >> 16)) ^ h2;
377 template<class C>
378 inline vtkSmartPointer<vtkIdList> idListFromVector(const C& c)
380 vtkSmartPointer<vtkIdList> id_list = vtkSmartPointer<vtkIdList>::New();
381 id_list->SetNumberOfIds(c.size());
382 for (int i = 0; i < c.size(); ++i) {
383 id_list->SetId(i, c[i]);
385 return id_list;
389 #endif