fixed edge display for volume cells
[engrid-github.git] / src / libengrid / engrid.h
blobfa620025af4f3b480a6ec86cd6929635673402d4
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 #define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
26 #include <QMessageBox>
27 #include <QtDebug>
28 #include <QString>
29 #include <QDate>
31 #include <vtkSmartPointer.h>
32 #include <vtkLongArray.h>
33 #include <vtkLongLongArray.h>
34 #include <vtkIntArray.h>
35 #include <vtkDoubleArray.h>
37 #include "error.h"
38 #include "math/mathvector.h"
39 #include "math/smallsquarematrix.h"
40 #include "geometrytools.h"
41 #include "containertricks.h"
43 // #include "engrid_version.h" //Moved this to the only place where it was being used for now...
45 #ifdef WIN32
46 typedef vtkLongLongArray vtkLongArray_t;
47 #else
49 #include <limits.h>
50 #if ( __WORDSIZE == 64 )
51 typedef vtkLongArray vtkLongArray_t;
52 #else
53 typedef vtkLongLongArray vtkLongArray_t;
54 #endif
56 #endif
58 #define EG_ERR_RETURN(TXT) \
59 { \
60 QString line; \
61 line.setNum(__LINE__); \
62 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
63 Error err; \
64 err.setText(txt); \
65 err.setType(Error::ExitOperation); \
66 throw err; \
69 #define EG_ERR_DISPLAY(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 err.display(); \
80 #ifdef QT_DEBUG
81 #define EG_BUG \
82 { \
83 QString line; \
84 line.setNum(__LINE__); \
85 QString txt = "This seems to be a bug in enGrid"; \
86 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
87 qWarning()<<txt; \
88 abort(); \
90 #else
91 #define EG_BUG \
92 { \
93 QString line; \
94 line.setNum(__LINE__); \
95 QString txt = "This seems to be a bug in enGrid"; \
96 txt += QString("\n\nfile: ") + __FILE__ + "\nline:" + line + "\n\n"; \
97 Error err; \
98 err.setText(txt); \
99 err.setType(Error::ExitOperation); \
100 throw err; \
102 #endif
105 #define EG_ERR_STOP(TXT) \
107 QString line; \
108 line.setNum(__LINE__); \
109 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
110 Error err; \
111 err.setText(txt); \
112 err.setType(Error::ExitProgram); \
113 throw err; \
116 #define EG_VTKSP(TYPE,VAR) vtkSmartPointer<TYPE> VAR = vtkSmartPointer<TYPE>::New();
119 * Get an automatically casted pointer to a cell data array of a grid
120 * (vtkUnstructuredGrid).
121 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
122 * @param VAR the name for the C++ pointer variable
123 * @param GRID a pointer to the vtkUnstructuredGrid
124 * @param NAME the sybolic name of the data array; for example "cell_index"
126 #define EG_VTKDCC(TYPE,VAR,GRID,NAME) \
127 TYPE *VAR = NULL; \
128 if (GRID->GetCellData()->GetScalars(NAME)) { \
129 VAR = dynamic_cast<TYPE*>(GRID->GetCellData()->GetScalars(NAME)); \
130 if (!VAR) { \
131 QString msg = "type mismatch ("; \
132 int t = GRID->GetCellData()->GetScalars(NAME)->GetDataType(); \
133 if (t == VTK_VOID) msg += "VTK_VOID"; \
134 if (t == VTK_BIT) msg += "VTK_BIT"; \
135 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
136 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
137 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
138 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
139 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
140 if (t == VTK_INT) msg += "VTK_INT"; \
141 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
142 if (t == VTK_LONG) msg += "VTK_LONG"; \
143 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
144 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
145 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
146 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
147 if (t == VTK_STRING) msg += "VTK_STRING"; \
148 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
149 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
150 if (t == VTK___INT64) msg += "VTK___INT64"; \
151 if (t == VTK_UNSIGNED___INT64) msg += "VTK_UNSIGNED___INT64"; \
152 msg += ")"; \
153 EG_ERR_RETURN(msg); \
154 }; \
155 } else { \
156 QString msg = QString("array '") + NAME + "' does not exist."; \
157 EG_ERR_RETURN(msg); \
161 * Get an automatically casted pointer to a node data array of a grid
162 * (vtkUnstructuredGrid).
163 * @param TYPE the type to cast to; examples are: vtkLongArray_t, vtkDoubleArray, ...
164 * @param VAR the name for the C++ pointer variable
165 * @param GRID a pointer to the vtkUnstructuredGrid
166 * @param NAME the sybolic name of the data array; for example "node_index"
168 #define EG_VTKDCN(TYPE,VAR,GRID,NAME) \
169 TYPE *VAR = NULL; \
170 if (GRID->GetPointData()->GetScalars(NAME)) { \
171 VAR = dynamic_cast<TYPE*>(GRID->GetPointData()->GetScalars(NAME)); \
172 if (!VAR) { \
173 QString msg = "type mismatch ("; \
174 int t = GRID->GetPointData()->GetScalars(NAME)->GetDataType(); \
175 if (t == VTK_VOID) msg += "VTK_VOID"; \
176 if (t == VTK_BIT) msg += "VTK_BIT"; \
177 if (t == VTK_CHAR) msg += "VTK_CHAR"; \
178 if (t == VTK_SIGNED_CHAR) msg += "VTK_SIGNED_CHAR"; \
179 if (t == VTK_UNSIGNED_CHAR) msg += "VTK_UNSIGNED_CHAR"; \
180 if (t == VTK_SHORT) msg += "VTK_SHORT"; \
181 if (t == VTK_UNSIGNED_SHORT) msg += "VTK_UNSIGNED_SHORT"; \
182 if (t == VTK_INT) msg += "VTK_INT"; \
183 if (t == VTK_UNSIGNED_INT) msg += "VTK_UNSIGNED_INT"; \
184 if (t == VTK_LONG) msg += "VTK_LONG"; \
185 if (t == VTK_UNSIGNED_LONG) msg += "VTK_UNSIGNED_LONG"; \
186 if (t == VTK_FLOAT) msg += "VTK_FLOAT"; \
187 if (t == VTK_DOUBLE) msg += "VTK_DOUBLE"; \
188 if (t == VTK_ID_TYPE) msg += "VTK_ID_TYPE"; \
189 if (t == VTK_STRING) msg += "VTK_STRING"; \
190 if (t == VTK_LONG_LONG) msg += "VTK_LONG_LONG"; \
191 if (t == VTK_UNSIGNED_LONG_LONG) msg += "VTK_UNSIGNED_LONG_LONG"; \
192 if (t == VTK___INT64) msg += "VTK___INT64"; \
193 if (t == VTK_UNSIGNED___INT64) msg += "VTK_UNSIGNED___INT64"; \
194 msg += ")"; \
195 EG_ERR_RETURN(msg); \
196 }; \
197 } else { \
198 QString msg = QString("array '") + NAME + "' does not exist."; \
199 EG_ERR_RETURN(msg); \
202 #define EG_STDINTERSLOT(OPER) \
203 OPER *oper = NULL; \
204 try { \
205 oper = new OPER(); \
206 } catch (Error err) { \
207 err.display(); \
209 if (oper) { \
210 (*oper)(); \
211 oper->del(); \
212 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(false); \
213 updateActors(); \
216 #define EG_STDSLOT(OPER) \
217 OPER *oper = NULL; \
218 try { \
219 oper = new OPER(); \
220 } catch (Error err) { \
221 err.display(); \
223 if (oper) { \
224 oper->setLockGui(); \
225 (*oper)(); \
226 oper->del(); \
227 updateActors(); \
230 #define EG_STDREADERSLOT(OPER) \
231 OPER *oper = new OPER(); \
232 (*oper)(); \
233 oper->del(); \
234 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(true); \
235 updateActors(); \
236 updateStatusBar(); \
237 zoomAll();
239 #define EG_STDCONNECT(OPER) \
240 connect(ui.action ## OPER, SIGNAL(triggered()), this, SLOT(call ## OPER ()));
242 #define DUMMY_VAR(X) X = X
245 * Perform a loop over all cells of a grid.
246 * @param ID_CELL the cell index iteration variable
247 * @param GRID a pointer to the vtkUnstructuredGrid
249 #define EG_FORALL_CELLS(ID_CELL, GRID) for (vtkIdType ID_CELL = 0; ID_CELL < GRID->GetNumberOfCells(); ++ID_CELL)
252 * Perform a loop over all nodes of a grid.
253 * @param ID_NODE the node index iteration variable
254 * @param GRID a pointer to the vtkUnstructuredGrid
256 #define EG_FORALL_NODES(ID_NODE, GRID) for (vtkIdType ID_NODE = 0; ID_NODE < GRID->GetNumberOfPoints(); ++ID_NODE)
259 * Get the type and the nodes of a cell.
260 * The following variables are created:
261 * - num_pts (number of points)
262 * - pts (array with the points)
263 * - type_cell (VTK type of the cell)
264 * @param ID_CELL the cell index
265 * @param GRID a pointer to the vtkUnstructuredGrid
267 #define EG_GET_CELL(ID_CELL, GRID) \
268 vtkIdType num_pts, *pts; \
269 vtkIdType type_cell = GRID->GetCellType(ID_CELL); \
270 GRID->GetCellPoints(ID_CELL, num_pts, pts);
272 #define EG_LARGE_REAL 1e99
274 #define EG_STOPDATE(STOP_DATE) \
275 QDate stop_date = QDate::fromString(STOP_DATE, "yyyy-MM-dd"); \
276 if (stop_date <= QDate::currentDate()) { \
277 EG_ERR_RETURN(QString("This feature is not supported after ") + STOP_DATE); \
280 inline double sqr(double x) { return x*x; }
282 template <class T>
283 inline T sign1(T t) {
284 if (t >= 0) return 1;
285 return -1;
288 inline int factorial_rec(int num)
290 if (num<=1)
291 return 1;
292 return factorial_rec(num-1)*num; // recursive call
295 inline int factorial_it(int num)
297 int result=1;
298 for (int i=1; i<=num; ++i)
299 result=result*i;
300 return result;
303 inline int N_Combinations(int N,int k)
305 return(factorial_rec(N)/(factorial_rec(N-k)*factorial_rec(k)));
308 inline int N_Permutations(int N,int k)
310 return(factorial_rec(N)/(factorial_rec(N-k)));
313 #define LINE_STR "=================" << endl;
315 #define USE(X) X=X
317 template <class T>
318 struct SortedPair
320 T v1, v2;
321 SortedPair();
322 SortedPair(T v1, T v2);
323 bool operator==(const SortedPair<T>& P) const{ return (v1 == P.v1) && (v2 == P.v2); }
324 bool operator<(const SortedPair<T>& P) const;
325 bool operator>(const SortedPair<T>& P) const { return !operator<(P); }
329 template <class T>
330 SortedPair<T>::SortedPair(T v1, T v2)
332 if (v1 > v2) {
333 this->v1 = v2;
334 this->v2 = v1;
335 } else {
336 this->v1 = v1;
337 this->v2 = v2;
341 template <class T>
342 SortedPair<T>::SortedPair()
344 v1 = 0;
345 v2 = 0;
348 template <class T>
349 bool SortedPair<T>::operator<(const SortedPair<T>& P) const
351 if (v1 < P.v1) {
352 return true;
353 } else if (v1 == P.v1) {
354 return v2 < P.v2;
356 return false;
359 template <class T>
360 inline uint qHash(const SortedPair<T> &P)
362 uint h1 = qHash(P.v1);
363 uint h2 = qHash(P.v2);
364 return ((h1 << 16) | (h1 >> 16)) ^ h2;
368 #endif