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 "StdMeshers_FaceSide.hxx"
37 #include "StdMeshers_MaxElementArea.hxx"
38 #include "StdMeshers_LengthFromEdges.hxx"
39 #include "StdMeshers_QuadranglePreference.hxx"
41 #include <Precision.hxx>
42 #include <Standard_ErrorHandler.hxx>
43 #include <Standard_Failure.hxx>
45 #include "utilities.h"
60 #include <occgeom.hpp>
61 #include <meshing.hpp>
62 //#include <meshtype.hpp>
64 extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, int, int, char*);
65 extern MeshingParameters mparam;
69 using namespace netgen;
70 using namespace nglib;
72 //#define DUMP_SEGMENTS
74 //=============================================================================
78 //=============================================================================
80 NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY(int hypId, int studyId,
82 : SMESH_2D_Algo(hypId, studyId, gen)
84 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY");
85 _name = "NETGEN_2D_ONLY";
87 _shapeType = (1 << TopAbs_FACE);// 1 bit /shape type
89 _compatibleHypothesis.push_back("MaxElementArea");
90 _compatibleHypothesis.push_back("LengthFromEdges");
91 _compatibleHypothesis.push_back("QuadranglePreference");
92 _compatibleHypothesis.push_back("NETGEN_Parameters_2D");
94 _hypMaxElementArea = 0;
95 _hypLengthFromEdges = 0;
96 _hypQuadranglePreference = 0;
100 //=============================================================================
104 //=============================================================================
106 NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY()
108 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY");
111 //=============================================================================
115 //=============================================================================
117 bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh& aMesh,
118 const TopoDS_Shape& aShape,
119 Hypothesis_Status& aStatus)
121 _hypMaxElementArea = 0;
122 _hypLengthFromEdges = 0;
123 _hypQuadranglePreference = 0;
125 const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
129 aStatus = HYP_OK; //SMESH_Hypothesis::HYP_MISSING;
130 return true; // (PAL13464) can work with no hypothesis, LengthFromEdges is default one
133 aStatus = HYP_MISSING;
135 list<const SMESHDS_Hypothesis*>::const_iterator ith;
136 for (ith = hyps.begin(); ith != hyps.end(); ++ith )
138 const SMESHDS_Hypothesis* hyp = (*ith);
140 string hypName = hyp->GetName();
142 if ( hypName == "MaxElementArea")
143 _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea*> (hyp);
144 else if ( hypName == "LengthFromEdges" )
145 _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges*> (hyp);
146 else if ( hypName == "QuadranglePreference" )
147 _hypQuadranglePreference = static_cast<const StdMeshers_QuadranglePreference*>(hyp);
148 else if ( hypName == "NETGEN_Parameters_2D" )
149 _hypParameters = static_cast<const NETGENPlugin_Hypothesis_2D*>(hyp);
151 aStatus = HYP_INCOMPATIBLE;
156 int nbHyps = bool(_hypMaxElementArea) + bool(_hypLengthFromEdges) + bool(_hypParameters );
158 aStatus = HYP_CONCURENT;
159 else if ( nbHyps == 1)
162 return ( aStatus == HYP_OK );
165 //================================================================================
167 * \brief Fill netgen mesh with segments
168 * \retval SMESH_ComputeErrorPtr - error description
170 //================================================================================
172 static TError addSegmentsToMesh(netgen::Mesh& ngMesh,
174 const TSideVector& wires,
175 SMESH_MesherHelper& helper,
176 vector< const SMDS_MeshNode* > & nodeVec)
178 // ----------------------------
179 // Check wires and count nodes
180 // ----------------------------
182 double totalLength = 0;
183 for ( int iW = 0; iW < wires.size(); ++iW )
185 StdMeshers_FaceSidePtr wire = wires[ iW ];
186 if ( wire->MissVertexNode() )
188 // Commented for issue 0020960. It worked for the case, let's wait for case where it doesn't.
189 // It seems that there is no reason for this limitation
191 // (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH, "Missing nodes on vertices"));
192 if (getenv("USER") && string("eap")==getenv("USER"))
193 cout << "Warning: NETGENPlugin_NETGEN_2D_ONLY : try to work with missing nodes on vertices"<<endl;
195 const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
196 if ( uvPtVec.size() != wire->NbPoints() )
198 (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,
199 SMESH_Comment("Unexpected nb of points on wire ") << iW
200 << ": " << uvPtVec.size()<<" != "<<wire->NbPoints()));
201 nbNodes += wire->NbPoints();
202 totalLength += wire->Length();
204 nodeVec.reserve( nbNodes );
210 // netgen::Box<3> bb = geom.GetBoundingBox();
211 // bb.Increase (bb.Diam()/10);
212 // ngMesh.SetLocalH (bb.PMin(), bb.PMax(), 0.5); // set grading
214 // map for nodes on vertices since they can be shared between wires
215 // ( issue 0020676, face_int_box.brep)
216 map<const SMDS_MeshNode*, int > node2ngID;
218 const int faceID = 1, solidID = 0;
219 if ( ngMesh.GetNFD() < 1 )
220 ngMesh.AddFaceDescriptor (FaceDescriptor(faceID, solidID, solidID, 0));
222 for ( int iW = 0; iW < wires.size(); ++iW )
224 StdMeshers_FaceSidePtr wire = wires[ iW ];
225 const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
226 const int nbSegments = wire->NbPoints() - 1;
228 // compute length of every segment
229 vector<double> segLen( nbSegments );
230 for ( int i = 0; i < nbSegments; ++i )
231 segLen[i] = SMESH_TNodeXYZ( uvPtVec[ i ].node ).Distance( uvPtVec[ i+1 ].node );
233 int edgeID = 1, posID = -2;
234 bool isInternalWire = false;
235 for ( int i = 0; i < nbSegments; ++i ) // loop on segments
237 // Add the first point of a segment
238 const SMDS_MeshNode * n = uvPtVec[ i ].node;
239 const int posShapeID = n->getshapeId();
240 bool onVertex = ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX );
242 // skip nodes on degenerated edges
243 if ( helper.IsDegenShape( posShapeID ) &&
244 helper.IsDegenShape( uvPtVec[ i+1 ].node->getshapeId() ))
247 int ngID1 = ngMesh.GetNP() + 1, ngID2 = ngID1+1;
249 ngID1 = node2ngID.insert( make_pair( n, ngID1 )).first->second;
250 if ( ngID1 > ngMesh.GetNP() )
252 MeshPoint mp( Point<3> (n->X(), n->Y(), n->Z()) );
253 ngMesh.AddPoint ( mp, 1, EDGEPOINT );
254 nodeVec.push_back( n );
258 ngID2 = ngMesh.GetNP() + 1;
259 if ( i > 0 ) // prev segment belongs to same wire
261 Segment& prevSeg = ngMesh.LineSegment( ngMesh.GetNSeg() );
269 seg[0] = ngID1; // ng node id
270 seg[1] = ngID2; // ng node id
272 seg.edgenr = ngMesh.GetNSeg() + 1;// segment id
273 seg.si = faceID; // = geom.fmap.FindIndex (face);
275 for ( int iEnd = 0; iEnd < 2; ++iEnd)
277 const UVPtStruct& pnt = uvPtVec[ i + iEnd ];
279 seg.epgeominfo[ iEnd ].dist = pnt.param; // param on curve
280 seg.epgeominfo[ iEnd ].u = pnt.u;
281 seg.epgeominfo[ iEnd ].v = pnt.v;
283 // find out edge id and node parameter on edge
284 onVertex = ( pnt.node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX );
285 if ( onVertex || posShapeID != posID )
288 double normParam = pnt.normParam;
290 normParam = 0.5 * ( uvPtVec[ i ].normParam + uvPtVec[ i+1 ].normParam );
291 const TopoDS_Edge& edge = wire->Edge( wire->EdgeIndex( normParam ));
292 edgeID = geom.emap.FindIndex( edge );
294 isInternalWire = ( edge.Orientation() == TopAbs_INTERNAL );
295 // if ( onVertex ) // param on curve is different on each of two edges
296 // seg.epgeominfo[ iEnd ].dist = helper.GetNodeU( edge, pnt.node );
298 seg.epgeominfo[ iEnd ].edgenr = edgeID; // = geom.emap.FindIndex(edge);
301 ngMesh.AddSegment (seg);
303 // restrict size of elements near the segment
304 SMESH_TNodeXYZ np1( n ), np2( uvPtVec[ i+1 ].node );
305 // get an average size of adjacent segments to avoid sharp change of
306 // element size (regression on issue 0020452, note 0010898)
307 int iPrev = SMESH_MesherHelper::WrapIndex( i-1, nbSegments );
308 int iNext = SMESH_MesherHelper::WrapIndex( i+1, nbSegments );
309 double avgH = ( segLen[ iPrev ] + segLen[ i ] + segLen[ iNext ]) / 3;
311 NETGENPlugin_Mesher::RestrictLocalSize( ngMesh, 0.5*(np1+np2), avgH );
314 cout << "Segment: " << seg.edgenr << endl
315 << "\tp1: " << seg[0] << endl
316 << "\tp2: " << seg[1] << endl
317 << "\tp0 param: " << seg.epgeominfo[ 0 ].dist << endl
318 << "\tp0 uv: " << seg.epgeominfo[ 0 ].u <<", "<< seg.epgeominfo[ 0 ].v << endl
319 << "\tp0 edge: " << seg.epgeominfo[ 0 ].edgenr << endl
320 << "\tp1 param: " << seg.epgeominfo[ 1 ].dist << endl
321 << "\tp1 uv: " << seg.epgeominfo[ 1 ].u <<", "<< seg.epgeominfo[ 1 ].v << endl
322 << "\tp1 edge: " << seg.epgeominfo[ 1 ].edgenr << endl;
324 if ( isInternalWire )
326 swap (seg[0], seg[1]);
327 swap( seg.epgeominfo[0], seg.epgeominfo[1] );
328 seg.edgenr = ngMesh.GetNSeg() + 1; // segment id
329 ngMesh.AddSegment (seg);
331 cout << "Segment: " << seg.edgenr << endl << "\tis REVRESE of the previous one" << endl;
334 } // loop on segments on a wire
336 // close chain of segments
337 if ( nbSegments > 0 )
339 Segment& lastSeg = ngMesh.LineSegment( ngMesh.GetNSeg() - int( isInternalWire));
340 const SMDS_MeshNode * lastNode = uvPtVec.back().node;
341 lastSeg[1] = node2ngID.insert( make_pair( lastNode, lastSeg[1] )).first->second;
342 if ( lastSeg[1] > ngMesh.GetNP() )
344 MeshPoint mp( Point<3> (lastNode->X(), lastNode->Y(), lastNode->Z()) );
345 ngMesh.AddPoint ( mp, 1, EDGEPOINT );
346 nodeVec.push_back( lastNode );
348 if ( isInternalWire )
350 Segment& realLastSeg = ngMesh.LineSegment( ngMesh.GetNSeg() );
351 realLastSeg[0] = lastSeg[1];
355 } // loop on wires of a face
357 // add a segment instead of internal vertex
358 NETGENPlugin_Internals intShapes( *helper.GetMesh(), helper.GetSubShape(), /*is3D=*/false );
359 NETGENPlugin_Mesher::addIntVerticesInFaces( geom, ngMesh, nodeVec, intShapes );
361 ngMesh.CalcSurfacesOfNode();
366 //=============================================================================
368 *Here we are going to use the NETGEN mesher
370 //=============================================================================
372 bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh& aMesh,
373 const TopoDS_Shape& aShape)
375 #ifdef WITH_SMESH_CANCEL_COMPUTE
376 netgen::multithread.terminate = 0;
378 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::Compute()");
380 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
381 int faceID = meshDS->ShapeToIndex( aShape );
383 SMESH_MesherHelper helper(aMesh);
384 _quadraticMesh = helper.IsQuadraticSubMesh(aShape);
385 helper.SetElementsOnShape( true );
386 const bool ignoreMediumNodes = _quadraticMesh;
388 // ------------------------
389 // get all edges of a face
390 // ------------------------
391 const TopoDS_Face F = TopoDS::Face( aShape.Oriented( TopAbs_FORWARD ));
393 TSideVector wires = StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem );
394 if ( problem && !problem->IsOK() )
395 return error( problem );
396 int nbWires = wires.size();
398 return error( "Problem in StdMeshers_FaceSide::GetFaceWires()");
399 if ( wires[0]->NbSegments() < 3 ) // ex: a circle with 2 segments
400 return error(COMPERR_BAD_INPUT_MESH,
401 SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
403 // --------------------
404 // compute edge length
405 // --------------------
407 NETGENPlugin_Mesher aMesher( &aMesh, aShape, /*isVolume=*/false);
408 netgen::OCCGeometry occgeo;
409 aMesher.PrepareOCCgeometry( occgeo, F, aMesh );
410 occgeo.fmap.Clear(); // face can be reversed, which is wrong in this case (issue 19978)
411 occgeo.fmap.Add( F );
413 if ( _hypParameters )
415 aMesher.SetParameters(_hypParameters);
419 double edgeLength = 0;
420 if (_hypLengthFromEdges || (!_hypLengthFromEdges && !_hypMaxElementArea))
423 for ( int iW = 0; iW < nbWires; ++iW )
425 edgeLength += wires[ iW ]->Length();
426 nbSegments += wires[ iW ]->NbSegments();
429 edgeLength /= nbSegments;
431 if ( _hypMaxElementArea )
433 double maxArea = _hypMaxElementArea->GetMaxArea();
434 edgeLength = sqrt(2. * maxArea/sqrt(3.0));
436 if ( edgeLength < DBL_MIN )
437 edgeLength = occgeo.GetBoundingBox().Diam();
439 netgen::mparam.maxh = edgeLength;
440 netgen::mparam.minh = aMesher.GetDefaultMinSize( aShape, netgen::mparam.maxh );
441 netgen::mparam.quad = _hypQuadranglePreference ? 1 : 0;
442 netgen::mparam.grading = 0.7; // very coarse mesh by default
445 occgeo.face_maxh = netgen::mparam.maxh;
448 // -------------------------
449 // Make input netgen mesh
450 // -------------------------
452 NETGENPlugin_NetgenLibWrapper ngLib;
453 netgen::Mesh * ngMesh = (netgen::Mesh*) ngLib._ngMesh;
455 Box<3> bb = occgeo.GetBoundingBox();
456 bb.Increase (bb.Diam()/10);
457 ngMesh->SetLocalH (bb.PMin(), bb.PMax(), netgen::mparam.grading);
458 ngMesh->SetGlobalH (netgen::mparam.maxh);
460 vector< const SMDS_MeshNode* > nodeVec;
461 problem = addSegmentsToMesh( *ngMesh, occgeo, wires, helper, nodeVec );
462 if ( problem && !problem->IsOK() )
463 return error( problem );
465 // -------------------------
466 // Generate surface mesh
467 // -------------------------
470 int startWith = MESHCONST_MESHSURFACE;
471 int endWith = MESHCONST_OPTSURFACE;
475 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
478 err = netgen::OCCGenerateMesh(occgeo, ngMesh, startWith, endWith, optstr);
479 #ifdef WITH_SMESH_CANCEL_COMPUTE
480 if(netgen::multithread.terminate)
484 error(SMESH_Comment("Error in netgen::OCCGenerateMesh() at ") << netgen::multithread.task);
486 catch (Standard_Failure& ex)
488 SMESH_Comment str("Exception in netgen::OCCGenerateMesh()");
489 str << " at " << netgen::multithread.task
490 << ": " << ex.DynamicType()->Name();
491 if ( ex.GetMessageString() && strlen( ex.GetMessageString() ))
492 str << ": " << ex.GetMessageString();
496 SMESH_Comment str("Exception in netgen::OCCGenerateMesh()");
497 str << " at " << netgen::multithread.task;
501 // ----------------------------------------------------
502 // Fill the SMESHDS with the generated nodes and faces
503 // ----------------------------------------------------
505 int nbNodes = ngMesh->GetNP();
506 int nbFaces = ngMesh->GetNSE();
508 int nbInputNodes = nodeVec.size();
509 nodeVec.resize( nbNodes, 0 );
512 for ( int i = nbInputNodes + 1; i <= nbNodes; ++i )
514 const MeshPoint& ngPoint = ngMesh->Point(i);
516 SMDS_MeshNode * node = meshDS->AddNode(ngPoint(0), ngPoint(1), ngPoint(2));
518 SMDS_MeshNode * node = meshDS->AddNode(ngPoint.X(), ngPoint.Y(), ngPoint.Z());
520 nodeVec[ i-1 ] = node;
524 bool reverse = ( aShape.Orientation() == TopAbs_REVERSED );
526 for ( i = 1; i <= nbFaces ; ++i )
528 const Element2d& elem = ngMesh->SurfaceElement(i);
529 vector<const SMDS_MeshNode*> nodes( elem.GetNP() );
530 for (j=1; j <= elem.GetNP(); ++j)
532 int pind = elem.PNum(j);
535 const SMDS_MeshNode* node = nodeVec.at(pind-1);
537 nodes[ nodes.size()-j ] = node;
540 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
542 const PointGeomInfo& pgi = elem.GeomInfoPi(j);
543 meshDS->SetNodeOnFace((SMDS_MeshNode*)node, faceID, pgi.u, pgi.v);
546 if ( j > elem.GetNP() )
548 SMDS_MeshFace* face = 0;
549 if ( elem.GetType() == TRIG )
550 face = helper.AddFace(nodes[0],nodes[1],nodes[2]);
552 face = helper.AddFace(nodes[0],nodes[1],nodes[2],nodes[3]);
559 #ifdef WITH_SMESH_CANCEL_COMPUTE
560 void NETGENPlugin_NETGEN_2D_ONLY::CancelCompute()
562 SMESH_Algo::CancelCompute();
563 netgen::multithread.terminate = 1;
567 //=============================================================================
571 //=============================================================================
573 bool NETGENPlugin_NETGEN_2D_ONLY::Evaluate(SMESH_Mesh& aMesh,
574 const TopoDS_Shape& aShape,
575 MapShapeNbElems& aResMap)
577 TopoDS_Face F = TopoDS::Face(aShape);
581 // collect info from edges
582 int nb0d = 0, nb1d = 0;
583 bool IsQuadratic = false;
585 double fullLen = 0.0;
586 TopTools_MapOfShape tmpMap;
587 for (TopExp_Explorer exp(F, TopAbs_EDGE); exp.More(); exp.Next()) {
588 TopoDS_Edge E = TopoDS::Edge(exp.Current());
589 if( tmpMap.Contains(E) )
592 SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
593 MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
594 if( anIt==aResMap.end() ) {
595 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
596 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
597 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
600 std::vector<int> aVec = (*anIt).second;
601 nb0d += aVec[SMDSEntity_Node];
602 nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
603 double aLen = SMESH_Algo::EdgeLength(E);
606 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
612 // compute edge length
614 if (_hypLengthFromEdges || !_hypLengthFromEdges && !_hypMaxElementArea) {
616 ELen = fullLen / nb1d;
618 if ( _hypMaxElementArea ) {
619 double maxArea = _hypMaxElementArea->GetMaxArea();
620 ELen = sqrt(2. * maxArea/sqrt(3.0));
623 BRepGProp::SurfaceProperties(F,G);
624 double anArea = G.Mass();
626 const int hugeNb = numeric_limits<int>::max()/10;
627 if ( anArea / hugeNb > ELen*ELen )
629 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
630 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
631 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated.\nToo small element length",this));
634 int nbFaces = (int) ( anArea / ( ELen*ELen*sqrt(3.) / 4 ) );
635 int nbNodes = (int) ( ( nbFaces*3 - (nb1d-1)*2 ) / 6 + 1 );
636 std::vector<int> aVec(SMDSEntity_Last);
637 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
639 aVec[SMDSEntity_Node] = nbNodes;
640 aVec[SMDSEntity_Quad_Triangle] = nbFaces;
643 aVec[SMDSEntity_Node] = nbNodes;
644 aVec[SMDSEntity_Triangle] = nbFaces;
646 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
647 aResMap.insert(std::make_pair(sm,aVec));