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>
66 extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, int, int, char*);
67 extern MeshingParameters mparam;
71 using namespace netgen;
72 using namespace nglib;
74 //#define DUMP_SEGMENTS
76 //=============================================================================
80 //=============================================================================
82 NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY(int hypId, int studyId,
84 : SMESH_2D_Algo(hypId, studyId, gen)
86 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY");
87 _name = "NETGEN_2D_ONLY";
89 _shapeType = (1 << TopAbs_FACE);// 1 bit /shape type
91 _compatibleHypothesis.push_back("MaxElementArea");
92 _compatibleHypothesis.push_back("LengthFromEdges");
93 _compatibleHypothesis.push_back("QuadranglePreference");
94 _compatibleHypothesis.push_back("NETGEN_Parameters_2D");
95 _compatibleHypothesis.push_back("ViscousLayers2D");
97 _hypMaxElementArea = 0;
98 _hypLengthFromEdges = 0;
99 _hypQuadranglePreference = 0;
103 //=============================================================================
107 //=============================================================================
109 NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY()
111 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY");
114 //=============================================================================
118 //=============================================================================
120 bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh& aMesh,
121 const TopoDS_Shape& aShape,
122 Hypothesis_Status& aStatus)
124 _hypMaxElementArea = 0;
125 _hypLengthFromEdges = 0;
126 _hypQuadranglePreference = 0;
128 const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
132 aStatus = HYP_OK; //SMESH_Hypothesis::HYP_MISSING;
133 return true; // (PAL13464) can work with no hypothesis, LengthFromEdges is default one
136 aStatus = HYP_MISSING;
138 list<const SMESHDS_Hypothesis*>::const_iterator ith;
139 for (ith = hyps.begin(); ith != hyps.end(); ++ith )
141 const SMESHDS_Hypothesis* hyp = (*ith);
143 string hypName = hyp->GetName();
145 if ( hypName == "MaxElementArea")
146 _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea*> (hyp);
147 else if ( hypName == "LengthFromEdges" )
148 _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges*> (hyp);
149 else if ( hypName == "QuadranglePreference" )
150 _hypQuadranglePreference = static_cast<const StdMeshers_QuadranglePreference*>(hyp);
151 else if ( hypName == "NETGEN_Parameters_2D" )
152 _hypParameters = static_cast<const NETGENPlugin_Hypothesis_2D*>(hyp);
153 else if ( hypName == StdMeshers_ViscousLayers2D::GetHypType() )
156 aStatus = HYP_INCOMPATIBLE;
161 int nbHyps = bool(_hypMaxElementArea) + bool(_hypLengthFromEdges) + bool(_hypParameters );
163 aStatus = HYP_CONCURENT;
167 return ( aStatus == HYP_OK );
170 //=============================================================================
172 *Here we are going to use the NETGEN mesher
174 //=============================================================================
176 bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh& aMesh,
177 const TopoDS_Shape& aShape)
179 #ifdef WITH_SMESH_CANCEL_COMPUTE
180 netgen::multithread.terminate = 0;
182 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::Compute()");
184 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
185 int faceID = meshDS->ShapeToIndex( aShape );
187 SMESH_MesherHelper helper(aMesh);
188 _quadraticMesh = helper.IsQuadraticSubMesh(aShape);
189 helper.SetElementsOnShape( true );
190 const bool ignoreMediumNodes = _quadraticMesh;
192 // build viscous layers if required
193 const TopoDS_Face F = TopoDS::Face( aShape.Oriented( TopAbs_FORWARD ));
194 SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( aMesh, F );
198 // ------------------------
199 // get all edges of a face
200 // ------------------------
203 StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem, proxyMesh );
204 if ( problem && !problem->IsOK() )
205 return error( problem );
206 int nbWires = wires.size();
208 return error( "Problem in StdMeshers_FaceSide::GetFaceWires()");
209 if ( wires[0]->NbSegments() < 3 ) // ex: a circle with 2 segments
210 return error(COMPERR_BAD_INPUT_MESH,
211 SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
213 // --------------------
214 // compute edge length
215 // --------------------
217 NETGENPlugin_Mesher aMesher( &aMesh, aShape, /*isVolume=*/false);
218 netgen::OCCGeometry occgeo;
219 aMesher.PrepareOCCgeometry( occgeo, F, aMesh );
220 occgeo.fmap.Clear(); // face can be reversed, which is wrong in this case (issue 19978)
221 occgeo.fmap.Add( F );
223 if ( _hypParameters )
225 aMesher.SetParameters(_hypParameters);
229 double edgeLength = 0;
230 if (_hypLengthFromEdges || (!_hypLengthFromEdges && !_hypMaxElementArea))
233 for ( int iW = 0; iW < nbWires; ++iW )
235 edgeLength += wires[ iW ]->Length();
236 nbSegments += wires[ iW ]->NbSegments();
239 edgeLength /= nbSegments;
241 if ( _hypMaxElementArea )
243 double maxArea = _hypMaxElementArea->GetMaxArea();
244 edgeLength = sqrt(2. * maxArea/sqrt(3.0));
246 if ( edgeLength < DBL_MIN )
247 edgeLength = occgeo.GetBoundingBox().Diam();
249 netgen::mparam.maxh = edgeLength;
250 netgen::mparam.minh = aMesher.GetDefaultMinSize( aShape, netgen::mparam.maxh );
251 netgen::mparam.quad = _hypQuadranglePreference ? 1 : 0;
252 netgen::mparam.grading = 0.7; // very coarse mesh by default
255 occgeo.face_maxh = netgen::mparam.maxh;
258 // -------------------------
259 // Make input netgen mesh
260 // -------------------------
262 NETGENPlugin_NetgenLibWrapper ngLib;
263 netgen::Mesh * ngMesh = (netgen::Mesh*) ngLib._ngMesh;
265 Box<3> bb = occgeo.GetBoundingBox();
266 bb.Increase (bb.Diam()/10);
267 ngMesh->SetLocalH (bb.PMin(), bb.PMax(), netgen::mparam.grading);
268 ngMesh->SetGlobalH (netgen::mparam.maxh);
270 vector< const SMDS_MeshNode* > nodeVec;
271 problem = aMesher.AddSegmentsToMesh( *ngMesh, occgeo, wires, helper, nodeVec );
272 if ( problem && !problem->IsOK() )
273 return error( problem );
275 // -------------------------
276 // Generate surface mesh
277 // -------------------------
280 int startWith = MESHCONST_MESHSURFACE;
281 int endWith = MESHCONST_OPTSURFACE;
285 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
288 err = netgen::OCCGenerateMesh(occgeo, ngMesh, startWith, endWith, optstr);
289 #ifdef WITH_SMESH_CANCEL_COMPUTE
290 if(netgen::multithread.terminate)
294 error(SMESH_Comment("Error in netgen::OCCGenerateMesh() at ") << netgen::multithread.task);
296 catch (Standard_Failure& ex)
298 SMESH_Comment str("Exception in netgen::OCCGenerateMesh()");
299 str << " at " << netgen::multithread.task
300 << ": " << ex.DynamicType()->Name();
301 if ( ex.GetMessageString() && strlen( ex.GetMessageString() ))
302 str << ": " << ex.GetMessageString();
306 SMESH_Comment str("Exception in netgen::OCCGenerateMesh()");
307 str << " at " << netgen::multithread.task;
311 // ----------------------------------------------------
312 // Fill the SMESHDS with the generated nodes and faces
313 // ----------------------------------------------------
315 int nbNodes = ngMesh->GetNP();
316 int nbFaces = ngMesh->GetNSE();
318 int nbInputNodes = nodeVec.size()-1;
319 nodeVec.resize( nbNodes+1, 0 );
322 for ( int ngID = nbInputNodes + 1; ngID <= nbNodes; ++ngID )
324 const MeshPoint& ngPoint = ngMesh->Point( ngID );
326 SMDS_MeshNode * node = meshDS->AddNode(ngPoint(0), ngPoint(1), ngPoint(2));
328 SMDS_MeshNode * node = meshDS->AddNode(ngPoint.X(), ngPoint.Y(), ngPoint.Z());
330 nodeVec[ ngID ] = node;
334 bool reverse = ( aShape.Orientation() == TopAbs_REVERSED );
336 for ( i = 1; i <= nbFaces ; ++i )
338 const Element2d& elem = ngMesh->SurfaceElement(i);
339 vector<const SMDS_MeshNode*> nodes( elem.GetNP() );
340 for (j=1; j <= elem.GetNP(); ++j)
342 int pind = elem.PNum(j);
345 const SMDS_MeshNode* node = nodeVec[ pind ];
347 nodes[ nodes.size()-j ] = node;
350 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
352 const PointGeomInfo& pgi = elem.GeomInfoPi(j);
353 meshDS->SetNodeOnFace((SMDS_MeshNode*)node, faceID, pgi.u, pgi.v);
356 if ( j > elem.GetNP() )
358 SMDS_MeshFace* face = 0;
359 if ( elem.GetType() == TRIG )
360 face = helper.AddFace(nodes[0],nodes[1],nodes[2]);
362 face = helper.AddFace(nodes[0],nodes[1],nodes[2],nodes[3]);
369 #ifdef WITH_SMESH_CANCEL_COMPUTE
370 void NETGENPlugin_NETGEN_2D_ONLY::CancelCompute()
372 SMESH_Algo::CancelCompute();
373 netgen::multithread.terminate = 1;
377 //=============================================================================
381 //=============================================================================
383 bool NETGENPlugin_NETGEN_2D_ONLY::Evaluate(SMESH_Mesh& aMesh,
384 const TopoDS_Shape& aShape,
385 MapShapeNbElems& aResMap)
387 TopoDS_Face F = TopoDS::Face(aShape);
391 // collect info from edges
392 int nb0d = 0, nb1d = 0;
393 bool IsQuadratic = false;
395 double fullLen = 0.0;
396 TopTools_MapOfShape tmpMap;
397 for (TopExp_Explorer exp(F, TopAbs_EDGE); exp.More(); exp.Next()) {
398 TopoDS_Edge E = TopoDS::Edge(exp.Current());
399 if( tmpMap.Contains(E) )
402 SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
403 MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
404 if( anIt==aResMap.end() ) {
405 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
406 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
407 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
410 std::vector<int> aVec = (*anIt).second;
411 nb0d += aVec[SMDSEntity_Node];
412 nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
413 double aLen = SMESH_Algo::EdgeLength(E);
416 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
422 // compute edge length
424 if (_hypLengthFromEdges || !_hypLengthFromEdges && !_hypMaxElementArea) {
426 ELen = fullLen / nb1d;
428 if ( _hypMaxElementArea ) {
429 double maxArea = _hypMaxElementArea->GetMaxArea();
430 ELen = sqrt(2. * maxArea/sqrt(3.0));
433 BRepGProp::SurfaceProperties(F,G);
434 double anArea = G.Mass();
436 const int hugeNb = numeric_limits<int>::max()/10;
437 if ( anArea / hugeNb > ELen*ELen )
439 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
440 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
441 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated.\nToo small element length",this));
444 int nbFaces = (int) ( anArea / ( ELen*ELen*sqrt(3.) / 4 ) );
445 int nbNodes = (int) ( ( nbFaces*3 - (nb1d-1)*2 ) / 6 + 1 );
446 std::vector<int> aVec(SMDSEntity_Last);
447 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
449 aVec[SMDSEntity_Node] = nbNodes;
450 aVec[SMDSEntity_Quad_Triangle] = nbFaces;
453 aVec[SMDSEntity_Node] = nbNodes;
454 aVec[SMDSEntity_Triangle] = nbFaces;
456 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
457 aResMap.insert(std::make_pair(sm,aVec));