1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 //=============================================================================
24 // File : NETGENPlugin_NETGEN_3D.cxx
25 // Moved here from SMESH_NETGEN_3D.cxx
26 // Created : lundi 27 Janvier 2003
27 // Author : Nadir BOUHAMOU (CEA)
29 //=============================================================================
31 #include "NETGENPlugin_NETGEN_3D.hxx"
33 #include "NETGENPlugin_Hypothesis.hxx"
35 #include <SMDS_MeshElement.hxx>
36 #include <SMDS_MeshNode.hxx>
37 #include <SMESHDS_Mesh.hxx>
38 #include <SMESH_Comment.hxx>
39 #include <SMESH_ControlsDef.hxx>
40 #include <SMESH_Gen.hxx>
41 #include <SMESH_Mesh.hxx>
42 #include <SMESH_MeshEditor.hxx>
43 #include <SMESH_MesherHelper.hxx>
44 #include <SMESH_subMesh.hxx>
45 #include <StdMeshers_MaxElementVolume.hxx>
46 #include <StdMeshers_QuadToTriaAdaptor.hxx>
47 #include <StdMeshers_ViscousLayers.hxx>
49 #include <BRepGProp.hxx>
50 #include <BRep_Tool.hxx>
51 #include <GProp_GProps.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #include <Standard_Failure.hxx>
58 #include <Standard_ErrorHandler.hxx>
60 #include <utilities.h>
73 #include <occgeom.hpp>
76 #include <ngexception.hpp>
79 #include <core/exception.hpp>
87 NETGENPLUGIN_DLL_HEADER
88 extern MeshingParameters mparam;
90 NETGENPLUGIN_DLL_HEADER
91 extern volatile multithreadt multithread;
93 using namespace nglib;
96 //=============================================================================
100 //=============================================================================
102 NETGENPlugin_NETGEN_3D::NETGENPlugin_NETGEN_3D(int hypId, SMESH_Gen* gen)
103 : SMESH_3D_Algo(hypId, gen)
106 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
107 _compatibleHypothesis.push_back("MaxElementVolume");
108 _compatibleHypothesis.push_back("NETGEN_Parameters");
109 _compatibleHypothesis.push_back("ViscousLayers");
111 _maxElementVolume = 0.;
113 _hypMaxElementVolume = NULL;
114 _hypParameters = NULL;
115 _viscousLayersHyp = NULL;
117 _requireShape = false; // can work without shape
120 //=============================================================================
124 //=============================================================================
126 NETGENPlugin_NETGEN_3D::~NETGENPlugin_NETGEN_3D()
130 //=============================================================================
134 //=============================================================================
136 bool NETGENPlugin_NETGEN_3D::CheckHypothesis (SMESH_Mesh& aMesh,
137 const TopoDS_Shape& aShape,
138 Hypothesis_Status& aStatus)
140 _hypMaxElementVolume = NULL;
141 _hypParameters = NULL;
142 _viscousLayersHyp = NULL;
143 _maxElementVolume = DBL_MAX;
145 // for correct work of GetProgress():
146 netgen::multithread.percent = 0.;
147 netgen::multithread.task = "Volume meshing";
148 _progressByTic = -1.;
150 list<const SMESHDS_Hypothesis*>::const_iterator itl;
151 //const SMESHDS_Hypothesis* theHyp;
153 const list<const SMESHDS_Hypothesis*>& hyps =
154 GetUsedHypothesis(aMesh, aShape, /*ignoreAuxiliary=*/false);
155 list <const SMESHDS_Hypothesis* >::const_iterator h = hyps.begin();
156 if ( h == hyps.end())
158 aStatus = SMESH_Hypothesis::HYP_OK;
159 return true; // can work with no hypothesis
163 for ( ; h != hyps.end(); ++h )
165 if ( !_hypMaxElementVolume )
166 _hypMaxElementVolume = dynamic_cast< const StdMeshers_MaxElementVolume*> ( *h );
167 if ( !_viscousLayersHyp ) // several _viscousLayersHyp's allowed
168 _viscousLayersHyp = dynamic_cast< const StdMeshers_ViscousLayers*> ( *h );
169 if ( ! _hypParameters )
170 _hypParameters = dynamic_cast< const NETGENPlugin_Hypothesis*> ( *h );
172 if ( *h != _hypMaxElementVolume &&
173 *h != _viscousLayersHyp &&
174 *h != _hypParameters &&
175 !dynamic_cast< const StdMeshers_ViscousLayers*>(*h)) // several VL hyps allowed
176 aStatus = HYP_INCOMPATIBLE;
178 if ( _hypMaxElementVolume && _hypParameters )
179 aStatus = HYP_INCOMPATIBLE;
180 else if ( aStatus == HYP_OK && _viscousLayersHyp )
181 error( _viscousLayersHyp->CheckHypothesis( aMesh, aShape, aStatus ));
183 if ( _hypMaxElementVolume )
184 _maxElementVolume = _hypMaxElementVolume->GetMaxVolume();
186 return aStatus == HYP_OK;
189 //=============================================================================
191 *Here we are going to use the NETGEN mesher
193 //=============================================================================
195 bool NETGENPlugin_NETGEN_3D::Compute(SMESH_Mesh& aMesh,
196 const TopoDS_Shape& aShape)
198 netgen::multithread.terminate = 0;
199 netgen::multithread.task = "Volume meshing";
200 _progressByTic = -1.;
202 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
204 SMESH_MesherHelper helper(aMesh);
205 _quadraticMesh = helper.IsQuadraticSubMesh(aShape);
206 helper.SetElementsOnShape( true );
208 int Netgen_NbOfNodes = 0;
209 double Netgen_point[3];
210 int Netgen_triangle[3];
212 NETGENPlugin_NetgenLibWrapper ngLib;
213 Ng_Mesh * Netgen_mesh = (Ng_Mesh*)ngLib._ngMesh;
215 // vector of nodes in which node index == netgen ID
216 vector< const SMDS_MeshNode* > nodeVec;
218 const int invalid_ID = -1;
220 SMESH::Controls::Area areaControl;
221 SMESH::Controls::TSequenceOfXYZ nodesCoords;
223 // maps nodes to ng ID
224 typedef map< const SMDS_MeshNode*, int, TIDCompare > TNodeToIDMap;
225 typedef TNodeToIDMap::value_type TN2ID;
226 TNodeToIDMap nodeToNetgenID;
228 // find internal shapes
229 NETGENPlugin_Internals internals( aMesh, aShape, /*is3D=*/true );
231 // ---------------------------------
232 // Feed the Netgen with surface mesh
233 // ---------------------------------
235 TopAbs_ShapeEnum mainType = aMesh.GetShapeToMesh().ShapeType();
236 bool checkReverse = ( mainType == TopAbs_COMPOUND || mainType == TopAbs_COMPSOLID );
238 SMESH_ProxyMesh::Ptr proxyMesh( new SMESH_ProxyMesh( aMesh ));
239 if ( _viscousLayersHyp )
241 netgen::multithread.percent = 3;
242 proxyMesh = _viscousLayersHyp->Compute( aMesh, aShape );
246 if ( aMesh.NbQuadrangles() > 0 )
248 netgen::multithread.percent = 6;
249 StdMeshers_QuadToTriaAdaptor* Adaptor = new StdMeshers_QuadToTriaAdaptor;
250 Adaptor->Compute(aMesh,aShape,proxyMesh.get());
251 proxyMesh.reset( Adaptor );
254 for ( TopExp_Explorer exFa( aShape, TopAbs_FACE ); exFa.More(); exFa.Next())
256 const TopoDS_Shape& aShapeFace = exFa.Current();
257 int faceID = meshDS->ShapeToIndex( aShapeFace );
258 bool isInternalFace = internals.isInternalShape( faceID );
260 if ( checkReverse && !isInternalFace &&
261 helper.NbAncestors(aShapeFace, aMesh, aShape.ShapeType()) > 1 )
262 // IsReversedSubMesh() can work wrong on strongly curved faces,
263 // so we use it as less as possible
264 isRev = helper.IsReversedSubMesh( TopoDS::Face( aShapeFace ));
266 const SMESHDS_SubMesh * aSubMeshDSFace = proxyMesh->GetSubMesh( aShapeFace );
267 if ( !aSubMeshDSFace ) continue;
269 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
270 if ( _quadraticMesh &&
271 dynamic_cast< const SMESH_ProxyMesh::SubMesh*>( aSubMeshDSFace ))
273 // add medium nodes of proxy triangles to helper (#16843)
274 while ( iteratorElem->more() )
275 helper.AddTLinks( static_cast< const SMDS_MeshFace* >( iteratorElem->next() ));
277 iteratorElem = aSubMeshDSFace->GetElements();
279 while ( iteratorElem->more() ) // loop on elements on a geom face
282 const SMDS_MeshElement* elem = iteratorElem->next();
284 return error( COMPERR_BAD_INPUT_MESH, "Null element encounters");
285 if ( elem->NbCornerNodes() != 3 )
286 return error( COMPERR_BAD_INPUT_MESH, "Not triangle element encounters");
288 // Add nodes of triangles and triangles them-selves to netgen mesh
290 // add three nodes of triangle
291 bool hasDegen = false;
292 for ( int iN = 0; iN < 3; ++iN )
294 const SMDS_MeshNode* node = elem->GetNode( iN );
295 const int shapeID = node->getshapeId();
296 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_EDGE &&
297 helper.IsDegenShape( shapeID ))
299 // ignore all nodes on degeneraged edge and use node on its vertex instead
300 TopoDS_Shape vertex = TopoDS_Iterator( meshDS->IndexToShape( shapeID )).Value();
301 node = SMESH_Algo::VertexNode( TopoDS::Vertex( vertex ), meshDS );
304 int& ngID = nodeToNetgenID.insert(TN2ID( node, invalid_ID )).first->second;
305 if ( ngID == invalid_ID )
307 ngID = ++Netgen_NbOfNodes;
308 Netgen_point [ 0 ] = node->X();
309 Netgen_point [ 1 ] = node->Y();
310 Netgen_point [ 2 ] = node->Z();
311 Ng_AddPoint(Netgen_mesh, Netgen_point);
313 Netgen_triangle[ isRev ? 2-iN : iN ] = ngID;
316 if ( hasDegen && (Netgen_triangle[0] == Netgen_triangle[1] ||
317 Netgen_triangle[0] == Netgen_triangle[2] ||
318 Netgen_triangle[2] == Netgen_triangle[1] ))
321 Ng_AddSurfaceElement(Netgen_mesh, NG_TRIG, Netgen_triangle);
323 if ( isInternalFace && !proxyMesh->IsTemporary( elem ))
325 swap( Netgen_triangle[1], Netgen_triangle[2] );
326 Ng_AddSurfaceElement(Netgen_mesh, NG_TRIG, Netgen_triangle);
328 } // loop on elements on a face
329 } // loop on faces of a SOLID or SHELL
331 // insert old nodes into nodeVec
332 nodeVec.resize( nodeToNetgenID.size() + 1, 0 );
333 TNodeToIDMap::iterator n_id = nodeToNetgenID.begin();
334 for ( ; n_id != nodeToNetgenID.end(); ++n_id )
335 nodeVec[ n_id->second ] = n_id->first;
336 nodeToNetgenID.clear();
338 if ( internals.hasInternalVertexInSolid() )
340 netgen::OCCGeometry occgeo;
341 NETGENPlugin_Mesher::AddIntVerticesInSolids( occgeo,
342 (netgen::Mesh&) *Netgen_mesh,
348 // -------------------------
349 // Generate the volume mesh
350 // -------------------------
352 return ( ngLib._isComputeOk = compute( aMesh, helper, nodeVec, ngLib ));
357 // void limitVolumeSize( netgen::Mesh* ngMesh,
360 // // get average h of faces
363 // for (int i = 1; i <= ngMesh->GetNSE(); i++)
365 // const netgen::Element2d& face = ngMesh->SurfaceElement(i);
366 // for (int j=1; j <= face.GetNP(); ++j)
368 // const netgen::PointIndex & i1 = face.PNumMod(j);
369 // const netgen::PointIndex & i2 = face.PNumMod(j+1);
372 // const netgen::Point3d & p1 = ngMesh->Point( i1 );
373 // const netgen::Point3d & p2 = ngMesh->Point( i2 );
374 // faceh += netgen::Dist2( p1, p2 );
379 // faceh = Sqrt( faceh / nbh );
382 // if ( faceh < 0.5 * maxh ) compareh = -1;
383 // else if ( faceh > 1.5 * maxh ) compareh = 1;
384 // else compareh = 0;
385 // // cerr << "faceh " << faceh << endl;
386 // // cerr << "init maxh " << maxh << endl;
387 // // cerr << "compareh " << compareh << endl;
389 // if ( compareh > 0 )
393 // // cerr << "maxh " << maxh << endl;
396 // netgen::Point3d pmin, pmax;
397 // ngMesh->GetBox( pmin, pmax, 0 );
398 // const double dx = pmax.X() - pmin.X();
399 // const double dy = pmax.Y() - pmin.Y();
400 // const double dz = pmax.Z() - pmin.Z();
402 // if ( ! & ngMesh->LocalHFunction() )
403 // ngMesh->SetLocalH( pmin, pmax, compareh <= 0 ? 0.1 : 0.5 );
405 // // adjusted by SALOME_TESTS/Grids/smesh/bugs_08/I8
406 // const int nbX = Max( 2, int( dx / maxh * 2 ));
407 // const int nbY = Max( 2, int( dy / maxh * 2 ));
408 // const int nbZ = Max( 2, int( dz / maxh * 2 ));
410 // netgen::Point3d p;
411 // for ( int i = 0; i <= nbX; ++i )
413 // p.X() = pmin.X() + i * dx / nbX;
414 // for ( int j = 0; j <= nbY; ++j )
416 // p.Y() = pmin.Y() + j * dy / nbY;
417 // for ( int k = 0; k <= nbZ; ++k )
419 // p.Z() = pmin.Z() + k * dz / nbZ;
420 // ngMesh->RestrictLocalH( p, maxh );
427 //================================================================================
429 * \brief set parameters and generate the volume mesh
431 //================================================================================
433 bool NETGENPlugin_NETGEN_3D::compute(SMESH_Mesh& aMesh,
434 SMESH_MesherHelper& helper,
435 vector< const SMDS_MeshNode* >& nodeVec,
436 NETGENPlugin_NetgenLibWrapper& ngLib)
438 netgen::multithread.terminate = 0;
440 netgen::Mesh* ngMesh = ngLib._ngMesh;
441 Ng_Mesh* Netgen_mesh = ngLib.ngMesh();
442 int Netgen_NbOfNodes = Ng_GetNP( Netgen_mesh );
444 int startWith = netgen::MESHCONST_MESHVOLUME;
445 int endWith = netgen::MESHCONST_OPTVOLUME;
448 NETGENPlugin_Mesher aMesher( &aMesh, helper.GetSubShape(), /*isVolume=*/true );
449 netgen::OCCGeometry occgeo;
451 if ( _hypParameters )
453 aMesher.SetParameters( _hypParameters );
455 if ( !_hypParameters->GetLocalSizesAndEntries().empty() ||
456 !_hypParameters->GetMeshSizeFile().empty() )
458 if ( ! &ngMesh->LocalHFunction() )
460 netgen::Point3d pmin, pmax;
461 ngMesh->GetBox( pmin, pmax, 0 );
462 ngMesh->SetLocalH( pmin, pmax, _hypParameters->GetGrowthRate() );
464 aMesher.SetLocalSize( occgeo, *ngMesh );
467 ngMesh->LoadLocalMeshSize( netgen::mparam.meshsizefilename );
468 } catch (netgen::NgException & ex) {
469 return error( COMPERR_BAD_PARMETERS, ex.What() );
472 if ( !_hypParameters->GetOptimize() )
473 endWith = netgen::MESHCONST_MESHVOLUME;
475 else if ( _hypMaxElementVolume )
477 netgen::mparam.maxh = pow( 72, 1/6. ) * pow( _maxElementVolume, 1/3. );
478 // limitVolumeSize( ngMesh, netgen::mparam.maxh ); // result is unpredictable
480 else if ( aMesh.HasShapeToMesh() )
482 aMesher.PrepareOCCgeometry( occgeo, helper.GetSubShape(), aMesh );
483 netgen::mparam.maxh = occgeo.GetBoundingBox().Diam()/2;
487 netgen::Point3d pmin, pmax;
488 ngMesh->GetBox (pmin, pmax);
489 netgen::mparam.maxh = Dist(pmin, pmax)/2;
492 if ( !_hypParameters && aMesh.HasShapeToMesh() )
494 netgen::mparam.minh = aMesher.GetDefaultMinSize( helper.GetSubShape(), netgen::mparam.maxh );
501 ngLib.CalcLocalH(ngMesh);
502 err = ngLib.GenerateMesh(occgeo, startWith, endWith);
504 if(netgen::multithread.terminate)
507 error(SMESH_Comment("Error in netgen::OCCGenerateMesh() at ") << netgen::multithread.task);
509 catch (Standard_Failure& ex)
511 SMESH_Comment str("Exception in netgen::OCCGenerateMesh()");
512 str << " at " << netgen::multithread.task
513 << ": " << ex.DynamicType()->Name();
514 if ( ex.GetMessageString() && strlen( ex.GetMessageString() ))
515 str << ": " << ex.GetMessageString();
518 catch (netgen::NgException& exc)
520 SMESH_Comment str("NgException");
521 if ( strlen( netgen::multithread.task ) > 0 )
522 str << " at " << netgen::multithread.task;
523 str << ": " << exc.What();
528 SMESH_Comment str("Exception in netgen::OCCGenerateMesh()");
529 if ( strlen( netgen::multithread.task ) > 0 )
530 str << " at " << netgen::multithread.task;
534 int Netgen_NbOfNodesNew = Ng_GetNP(Netgen_mesh);
535 int Netgen_NbOfTetra = Ng_GetNE(Netgen_mesh);
537 // -------------------------------------------------------------------
538 // Feed back the SMESHDS with the generated Nodes and Volume Elements
539 // -------------------------------------------------------------------
543 SMESH_ComputeErrorPtr ce = NETGENPlugin_Mesher::ReadErrors(nodeVec);
544 if ( ce && ce->HasBadElems() )
548 bool isOK = ( /*status == NG_OK &&*/ Netgen_NbOfTetra > 0 );// get whatever built
551 double Netgen_point[3];
552 int Netgen_tetrahedron[4];
554 // create and insert new nodes into nodeVec
555 nodeVec.resize( Netgen_NbOfNodesNew + 1, 0 );
556 int nodeIndex = Netgen_NbOfNodes + 1;
557 for ( ; nodeIndex <= Netgen_NbOfNodesNew; ++nodeIndex )
559 Ng_GetPoint( Netgen_mesh, nodeIndex, Netgen_point );
560 nodeVec.at(nodeIndex) = helper.AddNode(Netgen_point[0], Netgen_point[1], Netgen_point[2]);
563 // create tetrahedrons
564 for ( int elemIndex = 1; elemIndex <= Netgen_NbOfTetra; ++elemIndex )
566 Ng_GetVolumeElement(Netgen_mesh, elemIndex, Netgen_tetrahedron);
569 helper.AddVolume (nodeVec.at( Netgen_tetrahedron[0] ),
570 nodeVec.at( Netgen_tetrahedron[1] ),
571 nodeVec.at( Netgen_tetrahedron[2] ),
572 nodeVec.at( Netgen_tetrahedron[3] ));
583 //================================================================================
585 * \brief Compute tetrahedral mesh from 2D mesh without geometry
587 //================================================================================
589 bool NETGENPlugin_NETGEN_3D::Compute(SMESH_Mesh& aMesh,
590 SMESH_MesherHelper* aHelper)
592 const int invalid_ID = -1;
594 netgen::multithread.terminate = 0;
595 _progressByTic = -1.;
597 SMESH_MesherHelper::MType MeshType = aHelper->IsQuadraticMesh();
598 if ( MeshType == SMESH_MesherHelper::COMP )
599 return error( COMPERR_BAD_INPUT_MESH,
600 SMESH_Comment("Mesh with linear and quadratic elements given"));
602 aHelper->SetIsQuadratic( MeshType == SMESH_MesherHelper::QUADRATIC );
604 // ---------------------------------
605 // Feed the Netgen with surface mesh
606 // ---------------------------------
608 int Netgen_NbOfNodes = 0;
609 double Netgen_point[3];
610 int Netgen_triangle[3];
612 NETGENPlugin_NetgenLibWrapper ngLib;
613 Ng_Mesh * Netgen_mesh = ngLib.ngMesh();
615 SMESH_ProxyMesh::Ptr proxyMesh( new SMESH_ProxyMesh( aMesh ));
616 if ( aMesh.NbQuadrangles() > 0 )
618 StdMeshers_QuadToTriaAdaptor* Adaptor = new StdMeshers_QuadToTriaAdaptor;
619 Adaptor->Compute(aMesh);
620 proxyMesh.reset( Adaptor );
622 if ( aHelper->IsQuadraticMesh() )
624 SMDS_ElemIteratorPtr fIt = proxyMesh->GetFaces();
626 aHelper->AddTLinks( static_cast< const SMDS_MeshFace* >( fIt->next() ));
630 // maps nodes to ng ID
631 typedef map< const SMDS_MeshNode*, int, TIDCompare > TNodeToIDMap;
632 typedef TNodeToIDMap::value_type TN2ID;
633 TNodeToIDMap nodeToNetgenID;
635 SMDS_ElemIteratorPtr fIt = proxyMesh->GetFaces();
639 const SMDS_MeshElement* elem = fIt->next();
641 return error( COMPERR_BAD_INPUT_MESH, "Null element encounters");
642 if ( elem->NbCornerNodes() != 3 )
643 return error( COMPERR_BAD_INPUT_MESH, "Not triangle element encounters");
645 // add three nodes of triangle
646 for ( int iN = 0; iN < 3; ++iN )
648 const SMDS_MeshNode* node = elem->GetNode( iN );
649 int& ngID = nodeToNetgenID.insert(TN2ID( node, invalid_ID )).first->second;
650 if ( ngID == invalid_ID )
652 ngID = ++Netgen_NbOfNodes;
653 Netgen_point [ 0 ] = node->X();
654 Netgen_point [ 1 ] = node->Y();
655 Netgen_point [ 2 ] = node->Z();
656 Ng_AddPoint(Netgen_mesh, Netgen_point);
658 Netgen_triangle[ iN ] = ngID;
660 Ng_AddSurfaceElement(Netgen_mesh, NG_TRIG, Netgen_triangle);
662 proxyMesh.reset(); // delete tmp faces
664 // vector of nodes in which node index == netgen ID
665 vector< const SMDS_MeshNode* > nodeVec ( nodeToNetgenID.size() + 1 );
666 // insert old nodes into nodeVec
667 TNodeToIDMap::iterator n_id = nodeToNetgenID.begin();
668 for ( ; n_id != nodeToNetgenID.end(); ++n_id )
669 nodeVec.at( n_id->second ) = n_id->first;
670 nodeToNetgenID.clear();
672 // -------------------------
673 // Generate the volume mesh
674 // -------------------------
676 return ( ngLib._isComputeOk = compute( aMesh, *aHelper, nodeVec, ngLib ));
679 void NETGENPlugin_NETGEN_3D::CancelCompute()
681 SMESH_Algo::CancelCompute();
682 netgen::multithread.terminate = 1;
685 //================================================================================
687 * \brief Return Compute progress
689 //================================================================================
691 double NETGENPlugin_NETGEN_3D::GetProgress() const
694 const char* volMeshing = "Volume meshing";
695 const char* dlnMeshing = "Delaunay meshing";
696 const double meshingRatio = 0.15;
697 const_cast<NETGENPlugin_NETGEN_3D*>( this )->_progressTic++;
699 if ( _progressByTic < 0. &&
700 ( strncmp( netgen::multithread.task, dlnMeshing, 3 ) == 0 ||
701 strncmp( netgen::multithread.task, volMeshing, 3 ) == 0 ))
703 res = 0.001 + meshingRatio * netgen::multithread.percent / 100.;
704 //cout << netgen::multithread.task << " " <<_progressTic << "-" << netgen::multithread.percent << endl;
706 else // different otimizations
708 if ( _progressByTic < 0. )
709 ((NETGENPlugin_NETGEN_3D*)this)->_progressByTic = meshingRatio / _progressTic;
710 res = _progressByTic * _progressTic;
711 //cout << netgen::multithread.task << " " << _progressTic << " " << res << endl;
713 return Min ( res, 0.98 );
716 //=============================================================================
720 //=============================================================================
722 bool NETGENPlugin_NETGEN_3D::Evaluate(SMESH_Mesh& aMesh,
723 const TopoDS_Shape& aShape,
724 MapShapeNbElems& aResMap)
726 smIdType nbtri = 0, nbqua = 0;
727 double fullArea = 0.0;
728 for (TopExp_Explorer expF(aShape, TopAbs_FACE); expF.More(); expF.Next()) {
729 TopoDS_Face F = TopoDS::Face( expF.Current() );
730 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
731 MapShapeNbElemsItr anIt = aResMap.find(sm);
732 if( anIt==aResMap.end() ) {
733 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
734 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
737 std::vector<smIdType> aVec = (*anIt).second;
738 nbtri += std::max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
739 nbqua += std::max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
741 BRepGProp::SurfaceProperties(F,G);
742 double anArea = G.Mass();
746 // collect info from edges
747 smIdType nb0d_e = 0, nb1d_e = 0;
748 bool IsQuadratic = false;
750 TopTools_MapOfShape tmpMap;
751 for (TopExp_Explorer expF(aShape, TopAbs_EDGE); expF.More(); expF.Next()) {
752 TopoDS_Edge E = TopoDS::Edge(expF.Current());
753 if( tmpMap.Contains(E) )
756 SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(expF.Current());
757 MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
758 if( anIt==aResMap.end() ) {
759 SMESH_ComputeErrorPtr& smError = aSubMesh->GetComputeError();
760 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,
761 "Submesh can not be evaluated",this));
764 std::vector<smIdType> aVec = (*anIt).second;
765 nb0d_e += aVec[SMDSEntity_Node];
766 nb1d_e += std::max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
768 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
774 double ELen_face = sqrt(2.* ( fullArea/double(nbtri+nbqua*2) ) / sqrt(3.0) );
775 double ELen_vol = pow( 72, 1/6. ) * pow( _maxElementVolume, 1/3. );
776 double ELen = Min(ELen_vol,ELen_face*2);
779 BRepGProp::VolumeProperties(aShape,G);
780 double aVolume = G.Mass();
781 double tetrVol = 0.1179*ELen*ELen*ELen;
782 double CoeffQuality = 0.9;
783 smIdType nbVols = (smIdType)( aVolume/tetrVol/CoeffQuality );
784 smIdType nb1d_f = (nbtri*3 + nbqua*4 - nb1d_e) / 2;
785 smIdType nb1d_in = (nbVols*6 - nb1d_e - nb1d_f ) / 5;
786 std::vector<smIdType> aVec(SMDSEntity_Last);
787 for(smIdType i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
789 aVec[SMDSEntity_Node] = nb1d_in/6 + 1 + nb1d_in;
790 aVec[SMDSEntity_Quad_Tetra] = nbVols - nbqua*2;
791 aVec[SMDSEntity_Quad_Pyramid] = nbqua;
794 aVec[SMDSEntity_Node] = nb1d_in/6 + 1;
795 aVec[SMDSEntity_Tetra] = nbVols - nbqua*2;
796 aVec[SMDSEntity_Pyramid] = nbqua;
798 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
799 aResMap.insert(std::make_pair(sm,aVec));