2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2013 enGits GmbH +
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. +
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. +
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/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26 #include <QMessageBox>
30 #include <vtkSmartPointer.h>
31 #include <vtkLongArray.h>
32 #include <vtkLongLongArray.h>
33 #include <vtkIntArray.h>
34 #include <vtkDoubleArray.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...
45 typedef vtkLongLongArray vtkLongArray_t
;
49 #if ( __WORDSIZE == 64 )
50 typedef vtkLongArray vtkLongArray_t
;
52 typedef vtkLongLongArray vtkLongArray_t
;
57 #define EG_ERR_RETURN(TXT) \
60 line.setNum(__LINE__); \
61 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
64 err.setType(Error::ExitOperation); \
68 #define EG_ERR_DISPLAY(TXT) \
71 line.setNum(__LINE__); \
72 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
75 err.setType(Error::ExitOperation); \
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"; \
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"; \
98 err.setType(Error::ExitOperation); \
104 #define EG_ERR_STOP(TXT) \
107 line.setNum(__LINE__); \
108 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
111 err.setType(Error::ExitProgram); \
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) \
127 if (GRID->GetCellData()->GetScalars(NAME)) { \
128 VAR = dynamic_cast<TYPE*>(GRID->GetCellData()->GetScalars(NAME)); \
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"; \
152 EG_ERR_RETURN(msg); \
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) \
169 if (GRID->GetPointData()->GetScalars(NAME)) { \
170 VAR = dynamic_cast<TYPE*>(GRID->GetPointData()->GetScalars(NAME)); \
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"; \
194 EG_ERR_RETURN(msg); \
197 QString msg = QString("array '") + NAME + "' does not exist."; \
198 EG_ERR_RETURN(msg); \
201 #define EG_STDINTERSLOT(OPER) \
205 } catch (Error err) { \
211 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(false); \
215 #define EG_STDSLOT(OPER) \
219 } catch (Error err) { \
223 oper->setLockGui(); \
229 #define EG_STDREADERSLOT(OPER) \
230 OPER *oper = new OPER(); \
233 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(true); \
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
; }
272 inline T
sign1(T t
) {
273 if (t
>= 0) return 1;
277 inline int factorial_rec(int num
)
281 return factorial_rec(num
-1)*num
; // recursive call
284 inline int factorial_it(int num
)
287 for (int i
=1; i
<=num
; ++i
)
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;
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
); }
319 SortedPair
<T
>::SortedPair(T v1
, T v2
)
331 SortedPair
<T
>::SortedPair()
338 bool SortedPair
<T
>::operator<(const SortedPair
<T
>& P
) const
342 } else if (v1
== P
.v1
) {
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
;