1 // Copyright (C) 2007-2016 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
22 // File : NETGENPlugin_Remesher_2D.cxx
23 // Created : Thu Sep 21 16:48:46 2017
24 // Author : Edward AGAPOV (eap)
27 #include "NETGENPlugin_Remesher_2D.hxx"
29 #include "NETGENPlugin_Mesher.hxx"
30 #include "NETGENPlugin_Hypothesis_2D.hxx"
32 #include <SMDS_SetIterator.hxx>
33 #include <SMESHDS_Mesh.hxx>
34 #include <SMESH_ControlsDef.hxx>
35 #include <SMESH_Gen.hxx>
36 #include <SMESH_MeshAlgos.hxx>
37 #include <SMESH_MesherHelper.hxx>
38 #include <SMESH_subMesh.hxx>
40 #include <Bnd_B3d.hxx>
41 #include <Precision.hxx>
43 #include <occgeom.hpp>
44 #include <meshing.hpp>
45 #include <stlgeom.hpp>
46 //#include <stltool.hxx>
48 #include <boost/container/flat_set.hpp>
50 using namespace nglib;
54 // #if defined(NETGEN_V5) && defined(WIN32)
57 // extern STLParameters stlparam;
62 //=============================================================================
64 * \brief Fill holes in the mesh, since netgen can remesh only a closed shell mesh.
65 * At destruction, remove triangles filling the holes
70 HoleFiller( SMESH_Mesh& meshDS );
72 void AddHoleBorders( Ng_STL_Geometry * ngStlGeo );
73 void KeepHole() { myHole.clear(); }
76 SMESHDS_Mesh* myMeshDS;
77 std::vector< std::vector< gp_XYZ > > myHole; // initial border nodes
78 std::vector< gp_XYZ > myInHolePos; // position inside each hole
81 //================================================================================
83 * \brief Fill holes in the mesh
85 //================================================================================
87 HoleFiller::HoleFiller( SMESH_Mesh& theMesh ):
88 myMeshDS( theMesh.GetMeshDS() )
90 SMESH_MeshEditor editor( &theMesh );
93 // const double tol = Max( 0.1 * netgen::mparam.minh, Precision::Confusion() );
94 // TIDSortedNodeSet allNodes;
95 // SMESH_MeshEditor::TListOfListOfNodes equalNodes;
96 // editor.FindCoincidentNodes( allNodes, tol, equalNodes, true );
97 // editor.MergeNodes( equalNodes, /*noHoles=*/false );
101 SMESH_MeshAlgos::TFreeBorderVec holes;
102 bool isManifold = true, isGoodOri = true;
103 SMESH_MeshAlgos::FindFreeBorders( *myMeshDS, holes, /*closedOnly=*/true,
104 &isManifold, &isGoodOri );
108 // set bad faces into a compute error
109 SMESH_ComputeErrorPtr error =
110 SMESH_ComputeError::New( COMPERR_BAD_INPUT_MESH,
111 "Non-manifold mesh. Only manifold mesh can be re-meshed");
112 SMESH::Controls::MultiConnection2D fun;
113 fun.SetMesh( myMeshDS );
114 SMDS_ElemIteratorPtr fIt = myMeshDS->elementsIterator( SMDSAbs_Face );
115 while ( fIt->more() )
117 const SMDS_MeshElement* f = fIt->next();
118 if ( fun.GetValue( f->GetID() ) > 2 )
119 error->myBadElements.push_back( f );
121 theMesh.GetSubMesh( theMesh.GetShapeToMesh() )->GetComputeError() = error;
123 throw SALOME_Exception("Non-manifold mesh. Only manifold mesh can be re-meshed");
127 myHole.resize( holes.size() );
128 myInHolePos.resize( holes.size() );
129 std::vector<const SMDS_MeshElement*> newFaces;
130 for ( size_t i = 0; i < holes.size(); ++i )
133 SMESH_MeshAlgos::FillHole( holes[i], *myMeshDS, newFaces );
135 // keep data to be able to remove hole filling faces after remeshing
136 if ( !newFaces.empty() )
138 myHole[i].resize( holes[i].size() );
139 for ( size_t iP = 0; iP < holes[i].size(); ++iP )
140 myHole[i][iP] = SMESH_NodeXYZ( holes[i][iP] );
142 myInHolePos[i] = ( SMESH_NodeXYZ( newFaces[0]->GetNode(0)) +
143 SMESH_NodeXYZ( newFaces[0]->GetNode(1)) +
144 SMESH_NodeXYZ( newFaces[0]->GetNode(2)) ) / 3.;
145 // unmark to be able to remove them if meshing is canceled
146 for ( size_t iF = 0; iF < newFaces.size(); ++iF )
147 newFaces[iF]->setIsMarked( false );
153 SMDS_ElemIteratorPtr fIt = myMeshDS->elementsIterator( SMDSAbs_Face );
154 while ( fIt->more() )
156 const SMDS_MeshElement* f = fIt->next();
158 if ( SMESH_MeshAlgos::FaceNormal( f, normal ))
160 TIDSortedElemSet allFaces;
161 editor.Reorient2D( allFaces, normal, f );
167 //================================================================================
169 * \brief Add hole borders to be kept in a new mesh
171 //================================================================================
173 void HoleFiller::AddHoleBorders( Ng_STL_Geometry * ngStlGeo )
175 for ( size_t i = 0; i < myHole.size(); ++i )
176 for ( size_t iP = 1; iP < myHole[i].size(); ++iP )
178 Ng_STL_AddEdge( ngStlGeo,
179 myHole[i][iP-1].ChangeData(),
180 myHole[i][iP-0].ChangeData() );
183 //================================================================================
185 * \brief Remove triangles filling the holes
187 //================================================================================
189 HoleFiller::~HoleFiller()
191 if ( myMeshDS->NbNodes() < 3 )
194 bool hasOrphanNodes = true;
196 const double tol = Max( 1e-3 * netgen::mparam.minh, Precision::Confusion() );
198 for ( size_t i = 0; i < myHole.size(); ++i )
200 std::vector< gp_XYZ >& borderPnt = myHole[i];
201 const gp_XYZ& inHolePos = myInHolePos[i];
202 if ( borderPnt.empty() ) continue;
203 borderPnt.pop_back(); // first point repeated at end
205 // mark all nodes located on the hole border
207 // new nodeSearcher for each hole, otherwise it contains removed nodes for i > 0
208 SMESHUtils::Deleter< SMESH_NodeSearcher > nodeSearcher;
209 if ( hasOrphanNodes )
211 std::vector< const SMDS_MeshNode* > sharedNodes;
212 sharedNodes.reserve( myMeshDS->NbNodes() );
213 SMDS_NodeIteratorPtr nIt = myMeshDS->nodesIterator();
214 while ( nIt->more() )
216 const SMDS_MeshNode* n = nIt->next();
217 if ( n->NbInverseElements() )
218 sharedNodes.push_back( n );
220 hasOrphanNodes = ((int) sharedNodes.size() < myMeshDS->NbNodes() );
221 SMDS_ElemIteratorPtr elemIt( new SMDS_NodeVectorElemIterator( sharedNodes.begin(),
222 sharedNodes.end() ));
223 nodeSearcher._obj = SMESH_MeshAlgos::GetNodeSearcher( elemIt );
227 nodeSearcher._obj = SMESH_MeshAlgos::GetNodeSearcher( *myMeshDS );
230 std::vector< const SMDS_MeshElement* > edgesToRemove;
231 edgesToRemove.reserve( borderPnt.size() );
233 // look for a border point coincident with a node
235 SMESH_NodeXYZ bordNode1;
236 for ( ; iP < borderPnt.size(); ++iP )
238 bordNode1 = nodeSearcher->FindClosestTo( borderPnt[iP] );
239 if (( bordNode1 - borderPnt[iP] ).SquareModulus() < tol * tol )
243 bordNode1._node->setIsMarked( true );
245 // find the rest nodes located on the hole border
246 boost::container::flat_set< const SMDS_MeshNode* > checkedNodes;
247 gp_XYZ p1 = bordNode1;
248 for ( size_t j = 0; j < borderPnt.size()+1; ++j, iP = ( iP+1 ) % borderPnt.size() )
250 // among nodes surrounding bordNode1 find one most close to vec12
251 gp_XYZ vec12 = borderPnt[iP] - p1;
252 bool pntReached = false; // last found node is at iP
253 while ( !pntReached )
255 const SMDS_MeshNode* bordNode = bordNode1._node;
256 SMDS_ElemIteratorPtr fIt = bordNode->GetInverseElementIterator( SMDSAbs_Face );
257 double minArea = 1e100;
258 checkedNodes.clear();
259 checkedNodes.insert( bordNode );
260 while ( fIt->more() )
262 const SMDS_MeshElement* f = fIt->next();
263 for ( int iN = 0, nbN = f->NbNodes(); iN < nbN; ++iN )
265 const SMDS_MeshNode* n = f->GetNode( iN );
266 if ( !checkedNodes.insert( n ).second )
268 SMESH_NodeXYZ pn = n;
269 gp_XYZ vecPN = pn - bordNode1;
270 if ( vecPN * vec12 <= 0 )
272 gp_XYZ vec1N = pn - p1;
273 double a = vec12.CrossSquareMagnitude( vec1N );
280 if ( minArea < std::numeric_limits<double>::min() )
283 if ( bordNode == bordNode1._node )
284 return; // bug in the loop above
286 SMESH_NodeXYZ bordNode2 = bordNode;
287 gp_XYZ vec1N = bordNode2 - p1;
288 double u = ( vec12 * vec1N ) / vec12.SquareModulus(); // param [0,1] of bordNode on vec12
291 bordNode->setIsMarked( true );
292 //cout << bordNode->GetID() << " ";
294 if ( const SMDS_MeshElement* edge = myMeshDS->FindEdge( bordNode1._node, bordNode ))
295 edgesToRemove.push_back( edge );
297 edgesToRemove.push_back( myMeshDS->AddEdge( bordNode1._node, bordNode ));
298 edgesToRemove.back()->setIsMarked( true );
300 if ( minArea > std::numeric_limits<double>::min() &&
301 minArea / vec12.SquareModulus() > tol * tol )
303 // node is far from the border, move it
304 gp_XYZ p = p1 + u * vec12;
305 myMeshDS->MoveNode( bordNode, p.X(), p.Y(), p.Z() );
307 bordNode1 = bordNode2;
309 //else -- there must be another border point between bordNode1 and bordNode
310 pntReached = ( u > 1 - tol );
315 //cout << endl << endl;
317 // remove all faces starting from inHolePos
319 // get a starting face
320 std::vector< const SMDS_MeshNode* > nodesToRemove;
321 std::vector< const SMDS_MeshElement* > facesToRemove;
322 const SMDS_MeshNode* inHoleNode = nodeSearcher->FindClosestTo( inHolePos );
323 if ( inHoleNode && ! inHoleNode->isMarked() )
325 SMDS_ElemIteratorPtr fIt = inHoleNode->GetInverseElementIterator( SMDSAbs_Face );
326 while ( fIt->more() )
327 facesToRemove.push_back( fIt->next() );
331 SMESHUtils::Deleter< SMESH_ElementSearcher > faceSearcher
332 ( SMESH_MeshAlgos::GetElementSearcher( *myMeshDS ));
333 if ( const SMDS_MeshElement* f = faceSearcher->FindClosestTo( inHolePos, SMDSAbs_Face ))
334 facesToRemove.push_back( f );
338 for ( size_t iF = 0; iF < facesToRemove.size(); ++iF )
339 facesToRemove[iF]->setIsMarked( true );
341 // remove faces and nodes
342 TIDSortedElemSet elemSet, avoidSet;
343 const SMDS_MeshElement* e;
344 while ( !facesToRemove.empty() )
346 const SMDS_MeshElement* inHoleFace = facesToRemove.back();
347 facesToRemove.pop_back();
349 // add adjacent faces into facesToRemove
350 for ( int iN = 0, nbN = inHoleFace->NbNodes(); iN < nbN; ++iN )
352 const SMDS_MeshNode* n1 = inHoleFace->GetNode( iN );
353 if ( !n1->isMarked() )
355 SMDS_ElemIteratorPtr eIt = n1->GetInverseElementIterator();
356 while ( eIt->more() )
359 if ( e->GetType() == SMDSAbs_Face )
361 if ( !e->isMarked() )
362 facesToRemove.push_back( e );
363 e->setIsMarked( true );
365 else if ( e->GetType() == SMDSAbs_Edge )
367 myMeshDS->RemoveFreeElement( e, 0, /*fromGroups=*/false );
370 if ( n1->NbInverseElements() == 1 )
371 nodesToRemove.push_back( n1 );
375 const SMDS_MeshNode* n2 = inHoleFace->GetNodeWrap( iN+1 );
376 if (( n2->isMarked() ) &&
377 ( !(e = myMeshDS->FindEdge( n1, n2 )) || !e->isMarked() )) // n1-n2 not hole border
379 if ( e ) // remove edge
380 myMeshDS->RemoveFreeElement( e, 0, /*fromGroups=*/false );
382 avoidSet.insert( inHoleFace );
383 if (( e = SMESH_MeshAlgos::FindFaceInSet( n1, n2, elemSet, avoidSet )))
385 if ( !e->isMarked() )
386 facesToRemove.push_back( e );
387 e->setIsMarked( true );
392 myMeshDS->RemoveFreeElement( inHoleFace, 0, /*fromGroups=*/false );
394 for ( size_t iN = 0; iN < nodesToRemove.size(); ++iN )
395 myMeshDS->RemoveFreeNode( nodesToRemove[iN], 0, /*fromGroups=*/false );
396 nodesToRemove.clear();
399 // remove edges from the hole border
400 // for ( size_t iE = 0; iE < edgesToRemove.size(); ++iE )
401 // myMeshDS->RemoveFreeElement( edgesToRemove[iE], 0, /*fromGroups=*/false );
410 //=============================================================================
414 //=============================================================================
416 NETGENPlugin_Remesher_2D::NETGENPlugin_Remesher_2D(int hypId, int studyId, SMESH_Gen* gen)
417 : SMESH_2D_Algo(hypId, studyId, gen)
419 _name = "NETGEN_Remesher_2D";
420 _shapeType = (1 << TopAbs_FACE); // 1 bit /shape type
421 _compatibleHypothesis.push_back("NETGEN_RemesherParameters_2D");
422 _requireShape = false;
427 //=============================================================================
429 * Check assigned hypotheses
431 //=============================================================================
433 bool NETGENPlugin_Remesher_2D::CheckHypothesis (SMESH_Mesh& theMesh,
434 const TopoDS_Shape& theShape,
435 Hypothesis_Status& theStatus)
439 // can work with no hypothesis
440 theStatus = SMESH_Hypothesis::HYP_OK;
442 const list<const SMESHDS_Hypothesis*>& hyps =
443 GetUsedHypothesis( theMesh, theShape, /*skipAux=*/true );
445 switch ( hyps.size() ) {
449 _hypothesis = hyps.front();
452 theStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
455 return theStatus == SMESH_Hypothesis::HYP_OK;
458 //=============================================================================
460 * Compute mesh on an input mesh
462 //=============================================================================
464 bool NETGENPlugin_Remesher_2D::Compute(SMESH_Mesh& theMesh,
465 SMESH_MesherHelper* theHelper)
467 if ( theMesh.NbFaces() == 0 )
468 return !error( COMPERR_WARNING, "No faces in input mesh");
470 NETGENPlugin_Mesher mesher( &theMesh, theMesh.GetShapeToMesh(), /*isVol=*/false);
471 NETGENPlugin_NetgenLibWrapper ngLib;
472 netgen::Mesh * ngMesh = (netgen::Mesh*) ngLib._ngMesh;
473 Ng_STL_Geometry * ngStlGeo = Ng_STL_NewGeometry();
474 netgen::STLTopology* stlTopo = (netgen::STLTopology*) ngStlGeo;
475 netgen::multithread.terminate = 0;
477 const NETGENPlugin_RemesherHypothesis_2D* hyp =
478 dynamic_cast<const NETGENPlugin_RemesherHypothesis_2D*>( _hypothesis );
479 mesher.SetParameters( hyp );// for holeFiller
481 SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
482 HoleFiller holeFiller( theMesh );
483 //theHelper->SetIsQuadratic( theMesh.NbFaces( ORDER_QUADRATIC ));
485 // fill ngStlGeo with triangles
486 SMDS_ElemIteratorPtr fIt = meshDS->elementsIterator( SMDSAbs_Face );
487 while ( fIt->more() )
489 const SMDS_MeshElement* f = fIt->next();
490 SMESH_NodeXYZ n1 = f->GetNode( 0 );
491 SMESH_NodeXYZ n2 = f->GetNode( 1 );
492 SMESH_NodeXYZ n3 = f->GetNode( 2 );
493 Ng_STL_AddTriangle( ngStlGeo,
497 if ( f->NbNodes() > 3 )
499 n2.Set( f->GetNode( 3 ));
500 Ng_STL_AddTriangle( ngStlGeo,
507 holeFiller.AddHoleBorders( ngStlGeo );
510 Ng_Result ng_res = Ng_STL_InitSTLGeometry( ngStlGeo );
511 if ( ng_res != NG_OK )
514 holeFiller.KeepHole();
516 std::string txt = "Error Initialising the STL Geometry";
517 if ( !stlTopo->GetStatusText().empty() )
518 txt += ". " + stlTopo->GetStatusText();
519 return error( COMPERR_BAD_INPUT_MESH, txt );
522 Ng_Meshing_Parameters ngParams;
523 ng_res = Ng_STL_MakeEdges( ngStlGeo, ngLib._ngMesh, &ngParams );
524 if ( ng_res != NG_OK )
525 return error( "Error in Edge Meshing" );
530 ngParams.maxh = hyp->GetMaxSize();
531 ngParams.minh = hyp->GetMinSize();
532 ngParams.meshsize_filename = (char*) hyp->GetMeshSizeFile().c_str();
533 ngParams.quad_dominated = hyp->GetQuadAllowed();
534 netgen::stlparam.yangle = hyp->GetRidgeAngle();
535 mesher.SetParameters( hyp );
539 double diagSize = Dist( stlTopo->GetBoundingBox().PMin(), stlTopo->GetBoundingBox().PMax());
540 netgen::mparam.maxh = diagSize / GetGen()->GetBoundaryBoxSegmentation();
541 netgen::mparam.minh = netgen::mparam.maxh;
544 double h = netgen::mparam.maxh;
545 ngMesh->SetGlobalH( h );
546 ngMesh->SetMinimalH( netgen::mparam.minh );
547 ngMesh->SetLocalH( stlTopo->GetBoundingBox().PMin() - netgen::Vec3d(h, h, h),
548 stlTopo->GetBoundingBox().PMax() + netgen::Vec3d(h, h, h),
549 netgen::mparam.grading );
550 ngMesh->LoadLocalMeshSize( ngParams.meshsize_filename );
552 netgen::OCCGeometry occgeo;
553 mesher.SetLocalSize( occgeo, *ngMesh );
558 ng_res = Ng_STL_GenerateSurfaceMesh( ngStlGeo, ngLib._ngMesh, &ngParams );
560 catch (netgen::NgException & ex)
562 if ( netgen::multithread.terminate )
565 if ( ng_res != NG_OK )
566 return error( "Error in Surface Meshing" );
568 int nbN = ngMesh->GetNP();
569 int nbE = ngMesh->GetNSeg();
570 int nbF = ngMesh->GetNSE();
572 return error( "Error in Surface Meshing" );
574 // remove existing mesh
575 SMDS_ElemIteratorPtr eIt = meshDS->elementsIterator();
576 while ( eIt->more() )
577 meshDS->RemoveFreeElement( eIt->next(), /*sm=*/0 );
578 SMDS_NodeIteratorPtr nIt = meshDS->nodesIterator();
579 while ( nIt->more() )
580 meshDS->RemoveFreeNode( nIt->next(), /*sm=*/0 );
585 std::vector< const SMDS_MeshNode* > newNodes( nbN+1 );
586 for ( int i = 1; i <= nbN; ++i )
588 const netgen::MeshPoint& p = ngMesh->Point(i);
589 newNodes[i] = meshDS->AddNode( p(0),p(1),p(2) );
593 std::vector<const SMDS_MeshNode*> nodes(4);
594 for ( int i = 1; i <= nbE; ++i )
596 const netgen::Segment& seg = ngMesh->LineSegment(i);
598 for ( int j = 0; j < 2; ++j )
600 size_t pind = seg.pnums[j];
601 if ( pind > 0 && pind < newNodes.size() )
602 nodes.push_back( newNodes[ pind ]);
606 if ( nodes.size() == 2 && !meshDS->FindEdge( nodes[0], nodes[1] ))
607 meshDS->AddEdge( nodes[0], nodes[1] );
611 for ( int i = 1; i <= nbF; ++i )
613 const netgen::Element2d& elem = ngMesh->SurfaceElement(i);
615 for ( int j = 1; j <= elem.GetNP(); ++j )
617 size_t pind = elem.PNum(j);
618 if ( pind > 0 && pind < newNodes.size() )
619 nodes.push_back( newNodes[ pind ]);
623 switch( nodes.size() )
625 case 3: meshDS->AddFace( nodes[0], nodes[1], nodes[2] ); break;
626 case 4: meshDS->AddFace( nodes[0], nodes[1], nodes[2], nodes[3] ); break;
630 // as we don't assign the new triangles to a shape (the pseudo-shape),
631 // to avoid their removal at hypothesis modification,
632 // we mark the shape as always computed to avoid the error messages
633 // that no elements assigned to the shape
634 theMesh.GetSubMesh( theHelper->GetSubShape() )->SetIsAlwaysComputed( true );
639 //=============================================================================
641 * Do not compute mesh on geometry
643 //=============================================================================
645 bool NETGENPlugin_Remesher_2D::Compute(SMESH_Mesh& theMesh,
646 const TopoDS_Shape& theShape)
651 //=============================================================================
653 * Terminate Compute()
655 //=============================================================================
657 void NETGENPlugin_Remesher_2D::CancelCompute()
659 SMESH_Algo::CancelCompute();
660 netgen::multithread.terminate = 1;
663 //================================================================================
665 * \brief Return progress of Compute() [0.,1]
667 //================================================================================
669 double NETGENPlugin_Remesher_2D::GetProgress() const
671 return netgen::multithread.percent / 100.;
674 //=============================================================================
678 //=============================================================================
680 bool NETGENPlugin_Remesher_2D::Evaluate(SMESH_Mesh& aMesh,
681 const TopoDS_Shape& aShape,
682 MapShapeNbElems& aResMap)