1 /*****************************************************************************
2  *                                                                           *
3  *  Elmer, A Finite Element Software for Multiphysical Problems              *
4  *                                                                           *
5  *  Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland    *
6  *                                                                           *
7  *  This program is free software; you can redistribute it and/or            *
8  *  modify it under the terms of the GNU General Public License              *
9  *  as published by the Free Software Foundation; either version 2           *
10  *  of the License, or (at your option) any later version.                   *
11  *                                                                           *
12  *  This program 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 this program (in file fem/GPL-2); if not, write to the        *
19  *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,         *
20  *  Boston, MA 02110-1301, USA.                                              *
21  *                                                                           *
22  *****************************************************************************/
23 
24 /*****************************************************************************
25  *                                                                           *
26  *  ElmerGUI meshingthread                                                   *
27  *                                                                           *
28  *****************************************************************************
29  *                                                                           *
30  *  Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back                   *
31  *  Email:   Juha.Ruokolainen@csc.fi                                         *
32  *  Web:     http://www.csc.fi/elmer                                         *
33  *  Address: CSC - IT Center for Science Ltd.                                 *
34  *           Keilaranta 14                                                   *
35  *           02101 Espoo, Finland                                            *
36  *                                                                           *
37  *  Original Date: 15 Mar 2008                                               *
38  *                                                                           *
39  *****************************************************************************/
40 
41 #include <QtGui>
42 #include <iostream>
43 #include <stdio.h>
44 #include "meshingthread.h"
45 
46 using namespace std;
47 
MeshingThread(QObject * parent)48 MeshingThread::MeshingThread(QObject *parent)
49   : QThread(parent)
50 {
51   this->setTerminationEnabled(true);
52 }
53 
54 
~MeshingThread()55 MeshingThread::~MeshingThread()
56 {
57 }
58 
getNgMesh()59 nglib::Ng_Mesh* MeshingThread::getNgMesh()
60 {
61   return this->ngmesh;
62 }
63 
generate(int generatorType,QString cs,TetlibAPI * tetlibAPI,nglib::Ng_Mesh * ngmesh,nglib::Ng_STL_Geometry * nggeom,nglib::Ng_Geometry_2D * nggeom2d,int ngDim,nglib::Ng_Meshing_Parameters * mp)64 void MeshingThread::generate(int generatorType,
65 			     QString cs,
66 			     TetlibAPI *tetlibAPI,
67 			     nglib::Ng_Mesh *ngmesh,
68 			     nglib::Ng_STL_Geometry *nggeom,
69 			     nglib::Ng_Geometry_2D *nggeom2d,
70 			     int ngDim,
71 			     nglib::Ng_Meshing_Parameters *mp)
72 {
73   this->generatorType = generatorType;
74 
75   this->tetgenControlString = cs;
76   this->tetlibAPI = tetlibAPI;
77   this->in = tetlibAPI->in;
78   this->out = tetlibAPI->out;
79 
80   this->delegate_tetrahedralize = tetlibAPI->delegate_tetrahedralize;
81 
82   this->ngmesh = ngmesh;
83   this->nggeom = nggeom;
84   this->nggeom2d = nggeom2d;
85   this->mp = mp;
86   this->ngDim = ngDim;
87 
88   if (!isRunning()) {
89     cout << "Starting meshing thread with low priority" << endl;
90     cout.flush();
91     start(LowPriority);
92   } else {
93     cout << "Meshing thread is already running" << endl;
94     cout.flush();
95     return;
96   }
97 }
98 
stopMeshing()99 void MeshingThread::stopMeshing()
100 {
101   cout << "Terminating meshing thread... ";
102   cout.flush();
103 
104   terminate();
105   // wait();
106 
107   cout << "done" << endl;
108   cout.flush();
109 }
110 
run()111 void MeshingThread::run()
112 {
113   QString qs;
114   char ss[1024];
115 
116   if(generatorType == GEN_TETLIB) {
117 
118     cout << "tetlib: control string: "
119 #if WITH_QT5
120 	 << string(tetgenControlString.toLatin1()) << endl;
121 #else
122 	 << string(tetgenControlString.toAscii()) << endl;
123 #endif
124     cout << "tetlib: input points: " << in->numberofpoints << endl;
125     cout.flush();
126 
127     out->deinitialize();
128     out->initialize();
129 
130 #if WITH_QT5
131     sprintf(ss, "%s", (const char*)(tetgenControlString.toLatin1()));
132 #else
133     sprintf(ss, "%s", (const char*)(tetgenControlString.toAscii()));
134 #endif
135 
136     if(delegate_tetrahedralize)
137       delegate_tetrahedralize(1, NULL, ss, in, out, NULL, NULL);
138 
139     cout << "tetlib: nodes: " << out->numberofpoints << endl;
140     cout << "tetlib: elements: " << out->numberoftetrahedra << endl;
141     cout << "tetlib: boundary elements: " << out->numberoftrifaces << endl;
142     cout.flush();
143 
144   } else if(generatorType == GEN_NGLIB) {
145 
146     int rv = 0;
147 
148     if(ngDim == 3) {
149 
150       cout << "3D meshing..." << endl;
151 
152       rv = nglib::Ng_STL_GenerateSurfaceMesh(nggeom, ngmesh, mp);
153       cout << "Generate Surface Mesh: Ng_result=" << rv << endl;
154 
155       rv = nglib::Ng_GenerateVolumeMesh(ngmesh, mp);
156       cout << "Generate Volume Mesh: Ng_result=" << rv << endl;
157 
158       int np = nglib::Ng_GetNP(ngmesh);
159       cout << "Meshing thread: nodes: " << np << endl;
160 
161       int ne = nglib::Ng_GetNE(ngmesh);
162       cout << "Meshing thread: elements: " << ne << endl;
163 
164       int nse = nglib::Ng_GetNSE(ngmesh);
165       cout << "Meshing thread: boundary elements: " << nse << endl;
166       cout.flush();
167 
168     } else if(ngDim == 2) {
169 
170       cout << "2D meshing..." << endl;
171 
172       rv = nglib::Ng_GenerateMesh_2D(nggeom2d, &ngmesh, mp);
173       cout << "Generate 2D Mesh: Ng_result=" << rv << endl;
174 
175       int np = nglib::Ng_GetNP_2D(ngmesh);
176       cout << "Meshing thread: nodes: " << np << endl;
177 
178       int ne = nglib::Ng_GetNE_2D(ngmesh);
179       cout << "Meshing thread: elements: " << ne << endl;
180 
181       int nse = nglib::Ng_GetNSeg_2D(ngmesh);
182       cout << "Meshing thread: boundary elements: " << nse << endl;
183       cout.flush();
184 
185     } else {
186 
187       cout << "Illegal spatial dimension: " << ngDim << endl;
188 
189     }
190 
191   } else {
192 
193     cout << "Meshgen: unknown generator type\n";
194     cout.flush();
195 
196   }
197 }
198