1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : NETGENPlugin_NETGEN_2D_ONLY.cxx
21 // Author : Edward AGAPOV (OCC)
24 #include "NETGENPlugin_NETGEN_2D_ONLY.hxx"
26 #include "NETGENPlugin_Mesher.hxx"
27 #include "NETGENPlugin_Hypothesis_2D.hxx"
29 #include <SMDS_MeshElement.hxx>
30 #include <SMDS_MeshNode.hxx>
31 #include <SMESHDS_Mesh.hxx>
32 #include <SMESH_Comment.hxx>
33 #include <SMESH_Gen.hxx>
34 #include <SMESH_Mesh.hxx>
35 #include <SMESH_MesherHelper.hxx>
36 #include <SMESH_subMesh.hxx>
37 #include <StdMeshers_FaceSide.hxx>
38 #include <StdMeshers_LengthFromEdges.hxx>
39 #include <StdMeshers_MaxElementArea.hxx>
40 #include <StdMeshers_QuadranglePreference.hxx>
41 #include <StdMeshers_ViscousLayers2D.hxx>
43 #include <Precision.hxx>
44 #include <Standard_ErrorHandler.hxx>
45 #include <Standard_Failure.hxx>
47 #include <utilities.h>
62 #include <occgeom.hpp>
63 #include <meshing.hpp>
64 //#include <meshtype.hpp>
67 extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, MeshingParameters&, int, int);
69 extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, int, int, char*);
71 extern MeshingParameters mparam;
75 using namespace netgen;
76 using namespace nglib;
78 //#define DUMP_SEGMENTS
80 //=============================================================================
84 //=============================================================================
86 NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY(int hypId, int studyId,
88 : SMESH_2D_Algo(hypId, studyId, gen)
90 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY");
91 _name = "NETGEN_2D_ONLY";
93 _shapeType = (1 << TopAbs_FACE);// 1 bit /shape type
95 _compatibleHypothesis.push_back("MaxElementArea");
96 _compatibleHypothesis.push_back("LengthFromEdges");
97 _compatibleHypothesis.push_back("QuadranglePreference");
98 _compatibleHypothesis.push_back("NETGEN_Parameters_2D");
99 _compatibleHypothesis.push_back("ViscousLayers2D");
101 _hypMaxElementArea = 0;
102 _hypLengthFromEdges = 0;
103 _hypQuadranglePreference = 0;
107 //=============================================================================
111 //=============================================================================
113 NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY()
115 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY");
118 //=============================================================================
122 //=============================================================================
124 bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh& aMesh,
125 const TopoDS_Shape& aShape,
126 Hypothesis_Status& aStatus)
128 _hypMaxElementArea = 0;
129 _hypLengthFromEdges = 0;
130 _hypQuadranglePreference = 0;
132 const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
136 aStatus = HYP_OK; //SMESH_Hypothesis::HYP_MISSING;
137 return true; // (PAL13464) can work with no hypothesis, LengthFromEdges is default one
140 aStatus = HYP_MISSING;
142 list<const SMESHDS_Hypothesis*>::const_iterator ith;
143 for (ith = hyps.begin(); ith != hyps.end(); ++ith )
145 const SMESHDS_Hypothesis* hyp = (*ith);
147 string hypName = hyp->GetName();
149 if ( hypName == "MaxElementArea")
150 _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea*> (hyp);
151 else if ( hypName == "LengthFromEdges" )
152 _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges*> (hyp);
153 else if ( hypName == "QuadranglePreference" )
154 _hypQuadranglePreference = static_cast<const StdMeshers_QuadranglePreference*>(hyp);
155 else if ( hypName == "NETGEN_Parameters_2D" )
156 _hypParameters = static_cast<const NETGENPlugin_Hypothesis_2D*>(hyp);
157 else if ( hypName == StdMeshers_ViscousLayers2D::GetHypType() )
160 aStatus = HYP_INCOMPATIBLE;
165 int nbHyps = bool(_hypMaxElementArea) + bool(_hypLengthFromEdges) + bool(_hypParameters );
167 aStatus = HYP_CONCURENT;
171 return ( aStatus == HYP_OK );
174 //=============================================================================
176 *Here we are going to use the NETGEN mesher
178 //=============================================================================
180 bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh& aMesh,
181 const TopoDS_Shape& aShape)
183 #ifdef WITH_SMESH_CANCEL_COMPUTE
184 netgen::multithread.terminate = 0;
186 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::Compute()");
188 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
189 int faceID = meshDS->ShapeToIndex( aShape );
191 SMESH_MesherHelper helper(aMesh);
192 _quadraticMesh = helper.IsQuadraticSubMesh(aShape);
193 helper.SetElementsOnShape( true );
194 const bool ignoreMediumNodes = _quadraticMesh;
196 // build viscous layers if required
197 const TopoDS_Face F = TopoDS::Face( aShape.Oriented( TopAbs_FORWARD ));
198 SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( aMesh, F );
202 // ------------------------
203 // get all edges of a face
204 // ------------------------
207 StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem, proxyMesh );
208 if ( problem && !problem->IsOK() )
209 return error( problem );
210 int nbWires = wires.size();
212 return error( "Problem in StdMeshers_FaceSide::GetFaceWires()");
213 if ( wires[0]->NbSegments() < 3 ) // ex: a circle with 2 segments
214 return error(COMPERR_BAD_INPUT_MESH,
215 SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
217 // --------------------
218 // compute edge length
219 // --------------------
221 NETGENPlugin_Mesher aMesher( &aMesh, aShape, /*isVolume=*/false);
222 netgen::OCCGeometry occgeo;
223 aMesher.PrepareOCCgeometry( occgeo, F, aMesh );
224 occgeo.fmap.Clear(); // face can be reversed, which is wrong in this case (issue 19978)
225 occgeo.fmap.Add( F );
227 if ( _hypParameters )
229 aMesher.SetParameters(_hypParameters);
233 double edgeLength = 0;
234 if (_hypLengthFromEdges || (!_hypLengthFromEdges && !_hypMaxElementArea))
237 for ( int iW = 0; iW < nbWires; ++iW )
239 edgeLength += wires[ iW ]->Length();
240 nbSegments += wires[ iW ]->NbSegments();
243 edgeLength /= nbSegments;
245 if ( _hypMaxElementArea )
247 double maxArea = _hypMaxElementArea->GetMaxArea();
248 edgeLength = sqrt(2. * maxArea/sqrt(3.0));
250 if ( edgeLength < DBL_MIN )
251 edgeLength = occgeo.GetBoundingBox().Diam();
253 netgen::mparam.maxh = edgeLength;
254 netgen::mparam.minh = aMesher.GetDefaultMinSize( aShape, netgen::mparam.maxh );
255 netgen::mparam.quad = _hypQuadranglePreference ? 1 : 0;
256 netgen::mparam.grading = 0.7; // very coarse mesh by default
258 occgeo.face_maxh = netgen::mparam.maxh;
260 // -------------------------
261 // Make input netgen mesh
262 // -------------------------
264 NETGENPlugin_NetgenLibWrapper ngLib;
265 netgen::Mesh * ngMesh = (netgen::Mesh*) ngLib._ngMesh;
267 Box<3> bb = occgeo.GetBoundingBox();
268 bb.Increase (bb.Diam()/10);
269 ngMesh->SetLocalH (bb.PMin(), bb.PMax(), netgen::mparam.grading);
270 ngMesh->SetGlobalH (netgen::mparam.maxh);
272 vector< const SMDS_MeshNode* > nodeVec;
273 problem = aMesher.AddSegmentsToMesh( *ngMesh, occgeo, wires, helper, nodeVec );
274 if ( problem && !problem->IsOK() )
275 return error( problem );
277 // -------------------------
278 // Generate surface mesh
279 // -------------------------
284 int startWith = MESHCONST_MESHSURFACE;
285 int endWith = MESHCONST_OPTSURFACE;
289 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
293 err = netgen::OCCGenerateMesh(occgeo, ngMesh, netgen::mparam, startWith, endWith);
295 err = netgen::OCCGenerateMesh(occgeo, ngMesh, startWith, endWith, optstr);
297 #ifdef WITH_SMESH_CANCEL_COMPUTE
298 if(netgen::multithread.terminate)
302 error(SMESH_Comment("Error in netgen::OCCGenerateMesh() at ") << netgen::multithread.task);
304 catch (Standard_Failure& ex)
306 SMESH_Comment str("Exception in netgen::OCCGenerateMesh()");
307 str << " at " << netgen::multithread.task
308 << ": " << ex.DynamicType()->Name();
309 if ( ex.GetMessageString() && strlen( ex.GetMessageString() ))
310 str << ": " << ex.GetMessageString();
314 SMESH_Comment str("Exception in netgen::OCCGenerateMesh()");
315 str << " at " << netgen::multithread.task;
319 // ----------------------------------------------------
320 // Fill the SMESHDS with the generated nodes and faces
321 // ----------------------------------------------------
323 int nbNodes = ngMesh->GetNP();
324 int nbFaces = ngMesh->GetNSE();
326 int nbInputNodes = nodeVec.size()-1;
327 nodeVec.resize( nbNodes+1, 0 );
330 for ( int ngID = nbInputNodes + 1; ngID <= nbNodes; ++ngID )
332 const MeshPoint& ngPoint = ngMesh->Point( ngID );
333 SMDS_MeshNode * node = meshDS->AddNode(ngPoint(0), ngPoint(1), ngPoint(2));
334 nodeVec[ ngID ] = node;
338 bool reverse = ( aShape.Orientation() == TopAbs_REVERSED );
340 for ( i = 1; i <= nbFaces ; ++i )
342 const Element2d& elem = ngMesh->SurfaceElement(i);
343 vector<const SMDS_MeshNode*> nodes( elem.GetNP() );
344 for (j=1; j <= elem.GetNP(); ++j)
346 int pind = elem.PNum(j);
349 const SMDS_MeshNode* node = nodeVec[ pind ];
351 nodes[ nodes.size()-j ] = node;
354 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
356 const PointGeomInfo& pgi = elem.GeomInfoPi(j);
357 meshDS->SetNodeOnFace((SMDS_MeshNode*)node, faceID, pgi.u, pgi.v);
360 if ( j > elem.GetNP() )
362 SMDS_MeshFace* face = 0;
363 if ( elem.GetType() == TRIG )
364 face = helper.AddFace(nodes[0],nodes[1],nodes[2]);
366 face = helper.AddFace(nodes[0],nodes[1],nodes[2],nodes[3]);
373 #ifdef WITH_SMESH_CANCEL_COMPUTE
374 void NETGENPlugin_NETGEN_2D_ONLY::CancelCompute()
376 SMESH_Algo::CancelCompute();
377 netgen::multithread.terminate = 1;
381 //=============================================================================
385 //=============================================================================
387 bool NETGENPlugin_NETGEN_2D_ONLY::Evaluate(SMESH_Mesh& aMesh,
388 const TopoDS_Shape& aShape,
389 MapShapeNbElems& aResMap)
391 TopoDS_Face F = TopoDS::Face(aShape);
395 // collect info from edges
396 int nb0d = 0, nb1d = 0;
397 bool IsQuadratic = false;
399 double fullLen = 0.0;
400 TopTools_MapOfShape tmpMap;
401 for (TopExp_Explorer exp(F, TopAbs_EDGE); exp.More(); exp.Next()) {
402 TopoDS_Edge E = TopoDS::Edge(exp.Current());
403 if( tmpMap.Contains(E) )
406 SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
407 MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
408 if( anIt==aResMap.end() ) {
409 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
410 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
411 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
414 std::vector<int> aVec = (*anIt).second;
415 nb0d += aVec[SMDSEntity_Node];
416 nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
417 double aLen = SMESH_Algo::EdgeLength(E);
420 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
426 // compute edge length
428 if (_hypLengthFromEdges || !_hypLengthFromEdges && !_hypMaxElementArea) {
430 ELen = fullLen / nb1d;
432 if ( _hypMaxElementArea ) {
433 double maxArea = _hypMaxElementArea->GetMaxArea();
434 ELen = sqrt(2. * maxArea/sqrt(3.0));
437 BRepGProp::SurfaceProperties(F,G);
438 double anArea = G.Mass();
440 const int hugeNb = numeric_limits<int>::max()/10;
441 if ( anArea / hugeNb > ELen*ELen )
443 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
444 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
445 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated.\nToo small element length",this));
448 int nbFaces = (int) ( anArea / ( ELen*ELen*sqrt(3.) / 4 ) );
449 int nbNodes = (int) ( ( nbFaces*3 - (nb1d-1)*2 ) / 6 + 1 );
450 std::vector<int> aVec(SMDSEntity_Last);
451 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
453 aVec[SMDSEntity_Node] = nbNodes;
454 aVec[SMDSEntity_Quad_Triangle] = nbFaces;
457 aVec[SMDSEntity_Node] = nbNodes;
458 aVec[SMDSEntity_Triangle] = nbFaces;
460 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
461 aResMap.insert(std::make_pair(sm,aVec));