1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
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. +
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. +
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/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 //#ifdef VTK_VERSION_LESS_THAN_6_1
25 //#define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
27 //#include <vtkAutoInit.h>
28 //VTK_MODULE_INIT(vtkRenderingOpenGL);
31 #include <QMessageBox>
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>
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...
54 // typedef vtkLongLongArray vtkLongArray_t;
57 // #include <limits.h>
58 // #if ( __WORDSIZE == 64 )
59 // typedef vtkLongArray vtkLongArray_t;
61 // typedef vtkLongLongArray vtkLongArray_t;
66 typedef vtkLongLongArray vtkLongArray_t
;
67 typedef vtkSignedCharArray vtkCharArray_t
;
69 #define EG_ERR_RETURN(TXT) \
72 line.setNum(__LINE__); \
73 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
76 err.setType(Error::ExitOperation); \
77 QApplication::restoreOverrideCursor(); \
81 #define EG_ERR_DISPLAY(TXT) \
84 line.setNum(__LINE__); \
85 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
88 err.setType(Error::ExitOperation); \
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"; \
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"; \
111 err.setType(Error::ExitOperation); \
117 #define EG_ERR_STOP(TXT) \
120 line.setNum(__LINE__); \
121 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
124 err.setType(Error::ExitProgram); \
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) \
140 if (GRID->GetCellData()->GetScalars(NAME)) { \
141 VAR = dynamic_cast<TYPE*>(GRID->GetCellData()->GetScalars(NAME)); \
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"; \
163 EG_ERR_RETURN(msg); \
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) \
180 if (GRID->GetPointData()->GetScalars(NAME)) { \
181 VAR = dynamic_cast<TYPE*>(GRID->GetPointData()->GetScalars(NAME)); \
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"; \
203 EG_ERR_RETURN(msg); \
206 QString msg = QString("array '") + NAME + "' does not exist."; \
207 EG_ERR_RETURN(msg); \
210 #define EG_STDINTERSLOT(OPER) \
214 } catch (Error err) { \
220 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(false); \
224 #define EG_STDSLOT(OPER) \
228 } catch (Error err) { \
232 oper->setLockGui(); \
238 #define EG_STDREADERSLOT(OPER) \
239 OPER *oper = new OPER(); \
242 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(true); \
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; }
297 inline T
sign1(T t
) {
298 if (t
>= 0) return 1;
302 inline int factorial_rec(int num
)
306 return factorial_rec(num
-1)*num
; // recursive call
309 inline int factorial_it(int num
)
312 for (int i
=1; i
<=num
; ++i
)
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;
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
); }
344 SortedPair
<T
>::SortedPair(T v1
, T v2
)
356 SortedPair
<T
>::SortedPair()
363 bool SortedPair
<T
>::operator<(const SortedPair
<T
>& P
) const
367 } else if (v1
== P
.v1
) {
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
;
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
]);