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>
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...
53 typedef vtkLongLongArray vtkLongArray_t
;
57 #if ( __WORDSIZE == 64 )
58 typedef vtkLongArray vtkLongArray_t
;
60 typedef vtkLongLongArray vtkLongArray_t
;
65 #define EG_ERR_RETURN(TXT) \
68 line.setNum(__LINE__); \
69 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
72 err.setType(Error::ExitOperation); \
73 QApplication::restoreOverrideCursor(); \
77 #define EG_ERR_DISPLAY(TXT) \
80 line.setNum(__LINE__); \
81 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
84 err.setType(Error::ExitOperation); \
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"; \
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"; \
107 err.setType(Error::ExitOperation); \
113 #define EG_ERR_STOP(TXT) \
116 line.setNum(__LINE__); \
117 QString txt = QString(TXT) + "\n\nfile: " + __FILE__ + "\nline:" + line + "\n\n"; \
120 err.setType(Error::ExitProgram); \
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) \
136 if (GRID->GetCellData()->GetScalars(NAME)) { \
137 VAR = dynamic_cast<TYPE*>(GRID->GetCellData()->GetScalars(NAME)); \
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"; \
159 EG_ERR_RETURN(msg); \
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) \
176 if (GRID->GetPointData()->GetScalars(NAME)) { \
177 VAR = dynamic_cast<TYPE*>(GRID->GetPointData()->GetScalars(NAME)); \
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"; \
199 EG_ERR_RETURN(msg); \
202 QString msg = QString("array '") + NAME + "' does not exist."; \
203 EG_ERR_RETURN(msg); \
206 #define EG_STDINTERSLOT(OPER) \
210 } catch (Error err) { \
216 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(false); \
220 #define EG_STDSLOT(OPER) \
224 } catch (Error err) { \
228 oper->setLockGui(); \
234 #define EG_STDREADERSLOT(OPER) \
235 OPER *oper = new OPER(); \
238 if(m_Grid->GetNumberOfPoints()) updateBoundaryCodes(true); \
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; }
293 inline T
sign1(T t
) {
294 if (t
>= 0) return 1;
298 inline int factorial_rec(int num
)
302 return factorial_rec(num
-1)*num
; // recursive call
305 inline int factorial_it(int num
)
308 for (int i
=1; i
<=num
; ++i
)
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;
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
); }
340 SortedPair
<T
>::SortedPair(T v1
, T v2
)
352 SortedPair
<T
>::SortedPair()
359 bool SortedPair
<T
>::operator<(const SortedPair
<T
>& P
) const
363 } else if (v1
== P
.v1
) {
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
;
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
]);