1 // Copyright (C) 2007-2008 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.
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_NETGEN_2D_ONLY.cxx
23 // Author : Edward AGAPOV (OCC)
26 #include "NETGENPlugin_NETGEN_2D_ONLY.hxx"
28 #include "NETGENPlugin_Mesher.hxx"
30 #include "SMDS_MeshElement.hxx"
31 #include "SMDS_MeshNode.hxx"
32 #include "SMESHDS_Mesh.hxx"
33 #include "SMESH_Comment.hxx"
34 #include "SMESH_Gen.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "StdMeshers_FaceSide.hxx"
38 #include "StdMeshers_MaxElementArea.hxx"
39 #include "StdMeshers_LengthFromEdges.hxx"
40 #include "StdMeshers_QuadranglePreference.hxx"
42 #include <Standard_ErrorHandler.hxx>
43 #include <Standard_Failure.hxx>
45 #include "utilities.h"
57 #include <occgeom.hpp>
58 #include <meshing.hpp>
59 //#include <meshtype.hpp>
61 extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, int, int, char*);
62 /*extern*/ MeshingParameters mparam;
66 using namespace netgen;
67 using namespace nglib;
69 //=============================================================================
73 //=============================================================================
75 NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY(int hypId, int studyId,
77 : SMESH_2D_Algo(hypId, studyId, gen)
79 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY");
80 _name = "NETGEN_2D_ONLY";
82 _shapeType = (1 << TopAbs_FACE);// 1 bit /shape type
84 _compatibleHypothesis.push_back("MaxElementArea");
85 _compatibleHypothesis.push_back("LengthFromEdges");
86 _compatibleHypothesis.push_back("QuadranglePreference");
88 _hypMaxElementArea = 0;
89 _hypLengthFromEdges = 0;
90 _hypQuadranglePreference = 0;
93 //=============================================================================
97 //=============================================================================
99 NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY()
101 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY");
104 //=============================================================================
108 //=============================================================================
110 bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh& aMesh,
111 const TopoDS_Shape& aShape,
112 Hypothesis_Status& aStatus)
114 _hypMaxElementArea = 0;
115 _hypLengthFromEdges = 0;
116 _hypQuadranglePreference = 0;
118 const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
122 aStatus = HYP_OK; //SMESH_Hypothesis::HYP_MISSING;
123 return true; // (PAL13464) can work with no hypothesis, LengthFromEdges is default one
126 aStatus = HYP_MISSING;
128 list<const SMESHDS_Hypothesis*>::const_iterator ith;
129 for (ith = hyps.begin(); ith != hyps.end(); ++ith )
131 const SMESHDS_Hypothesis* hyp = (*ith);
133 string hypName = hyp->GetName();
135 if ( hypName == "MaxElementArea")
136 _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea*> (hyp);
137 else if ( hypName == "LengthFromEdges" )
138 _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges*> (hyp);
139 else if ( hypName == "QuadranglePreference" )
140 _hypQuadranglePreference = static_cast<const StdMeshers_QuadranglePreference*>(hyp);
142 aStatus = HYP_INCOMPATIBLE;
147 if ( _hypMaxElementArea && _hypLengthFromEdges ) {
148 aStatus = HYP_CONCURENT;
152 if ( _hypMaxElementArea || _hypLengthFromEdges )
155 return aStatus == HYP_OK;
158 //================================================================================
160 * \brief Fill netgen mesh with segments
161 * \retval SMESH_ComputeErrorPtr - error description
163 //================================================================================
165 static TError AddSegmentsToMesh(netgen::Mesh& ngMesh,
167 const TSideVector& wires,
168 SMESH_MesherHelper& helper,
169 vector< const SMDS_MeshNode* > & nodeVec)
171 // ----------------------------
172 // Check wires and count nodes
173 // ----------------------------
175 for ( int iW = 0; iW < wires.size(); ++iW )
177 StdMeshers_FaceSidePtr wire = wires[ iW ];
178 if ( wire->MissVertexNode() )
180 (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH, "Missing nodes on vertices"));
182 const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
183 if ( uvPtVec.size() != wire->NbPoints() )
185 (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,
186 SMESH_Comment("Unexpected nb of points on wire ") << iW
187 << ": " << uvPtVec.size()<<" != "<<wire->NbPoints()));
188 nbNodes += wire->NbSegments();
190 nodeVec.reserve( nbNodes );
196 // netgen::Box<3> bb = geom.GetBoundingBox();
197 // bb.Increase (bb.Diam()/10);
198 // ngMesh.SetLocalH (bb.PMin(), bb.PMax(), 0.5); // set grading
200 const int faceID = 1, solidID = 0;
201 ngMesh.AddFaceDescriptor (FaceDescriptor(faceID, solidID, solidID, 0));
203 for ( int iW = 0; iW < wires.size(); ++iW )
205 StdMeshers_FaceSidePtr wire = wires[ iW ];
206 const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
208 int firstPointID = ngMesh.GetNP() + 1;
209 int edgeID = 1, posID = -2;
210 for ( int i = 0; i < wire->NbSegments(); ++i ) // loop on segments
212 // Add the first point of a segment
213 const SMDS_MeshNode * n = uvPtVec[ i ].node;
214 const int posShapeID = n->GetPosition()->GetShapeId();
216 // skip nodes on degenerated edges
217 if ( helper.IsDegenShape( posShapeID ) &&
218 helper.IsDegenShape( uvPtVec[ i+1 ].node->GetPosition()->GetShapeId() ))
221 nodeVec.push_back( n );
223 MeshPoint mp( Point<3> (n->X(), n->Y(), n->Z()) );
224 ngMesh.AddPoint ( mp, 1, EDGEPOINT );
229 seg.p1 = ngMesh.GetNP(); // ng node id
230 seg.p2 = seg.p1 + 1; // ng node id
231 seg.edgenr = ngMesh.GetNSeg() + 1;// segment id
232 seg.si = faceID; // = geom.fmap.FindIndex (face);
234 for ( int iEnd = 0; iEnd < 2; ++iEnd)
236 const UVPtStruct& pnt = uvPtVec[ i + iEnd ];
238 seg.epgeominfo[ iEnd ].dist = pnt.param; // param on curve
239 seg.epgeominfo[ iEnd ].u = pnt.u;
240 seg.epgeominfo[ iEnd ].v = pnt.v;
242 // find out edge id and node parameter on edge
243 bool onVertex = ( pnt.node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX );
244 if ( onVertex || posShapeID != posID )
247 double normParam = pnt.normParam;
249 normParam = 0.5 * ( uvPtVec[ i ].normParam + uvPtVec[ i+1 ].normParam );
250 const TopoDS_Edge& edge = wire->Edge( wire->EdgeIndex( normParam ));
251 edgeID = geom.emap.FindIndex( edge );
253 if ( onVertex ) // param on curve is different on each of two edges
254 seg.epgeominfo[ iEnd ].dist = helper.GetNodeU( edge, pnt.node );
256 seg.epgeominfo[ iEnd ].edgenr = edgeID; // = geom.emap.FindIndex(edge);
259 ngMesh.AddSegment (seg);
261 // cout << "Segment: " << seg.edgenr << endl
262 // << "\tp1: " << seg.p1 << endl
263 // << "\tp2: " << seg.p2 << endl
264 // << "\tp0 param: " << seg.epgeominfo[ 0 ].dist << endl
265 // << "\tp0 uv: " << seg.epgeominfo[ 0 ].u <<", "<< seg.epgeominfo[ 0 ].v << endl
266 // << "\tp0 edge: " << seg.epgeominfo[ 0 ].edgenr << endl
267 // << "\tp1 param: " << seg.epgeominfo[ 1 ].dist << endl
268 // << "\tp1 uv: " << seg.epgeominfo[ 1 ].u <<", "<< seg.epgeominfo[ 1 ].v << endl
269 // << "\tp1 edge: " << seg.epgeominfo[ 1 ].edgenr << endl;
271 Segment& seg = ngMesh.LineSegment( ngMesh.GetNSeg() );
272 seg.p2 = firstPointID;
275 ngMesh.CalcSurfacesOfNode();
280 //=============================================================================
282 *Here we are going to use the NETGEN mesher
284 //=============================================================================
286 bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh& aMesh,
287 const TopoDS_Shape& aShape)
289 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::Compute()");
291 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
292 int faceID = meshDS->ShapeToIndex( aShape );
294 SMESH_MesherHelper helper(aMesh);
295 _quadraticMesh = helper.IsQuadraticSubMesh(aShape);
296 helper.SetElementsOnShape( true );
297 const bool ignoreMediumNodes = _quadraticMesh;
299 // ------------------------
300 // get all edges of a face
301 // ------------------------
302 const TopoDS_Face F = TopoDS::Face( aShape.Oriented( TopAbs_FORWARD ));
304 TSideVector wires = StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem );
305 if ( problem && !problem->IsOK() )
306 return error( problem );
307 int nbWires = wires.size();
309 return error( "Problem in StdMeshers_FaceSide::GetFaceWires()");
310 if ( wires[0]->NbSegments() < 3 ) // ex: a circle with 2 segments
311 return error(COMPERR_BAD_INPUT_MESH,
312 SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
314 // -------------------------
315 // Make input netgen mesh
316 // -------------------------
319 netgen::Mesh * ngMesh = new netgen::Mesh ();
321 netgen::OCCGeometry occgeo;
322 NETGENPlugin_Mesher::PrepareOCCgeometry( occgeo, F, aMesh );
323 occgeo.fmap.Clear(); // face can be reversed, which is wrong in this case (issue 19978)
324 occgeo.fmap.Add( F );
326 vector< const SMDS_MeshNode* > nodeVec;
327 problem = AddSegmentsToMesh( *ngMesh, occgeo, wires, helper, nodeVec );
328 if ( problem && !problem->IsOK() ) {
329 delete ngMesh; Ng_Exit();
330 return error( problem );
333 // --------------------
334 // compute edge length
335 // --------------------
337 double edgeLength = 0;
338 if (_hypLengthFromEdges || !_hypLengthFromEdges && !_hypMaxElementArea)
341 for ( int iW = 0; iW < nbWires; ++iW )
343 edgeLength += wires[ iW ]->Length();
344 nbSegments += wires[ iW ]->NbSegments();
347 edgeLength /= nbSegments;
349 if ( _hypMaxElementArea )
351 double maxArea = _hypMaxElementArea->GetMaxArea();
352 edgeLength = sqrt(2. * maxArea/sqrt(3.0));
354 if ( edgeLength < DBL_MIN )
355 edgeLength = occgeo.GetBoundingBox().Diam();
357 //cout << " edgeLength = " << edgeLength << endl;
359 netgen::mparam.maxh = edgeLength;
360 netgen::mparam.quad = _hypQuadranglePreference ? 1 : 0;
361 //ngMesh->SetGlobalH ( edgeLength );
363 // -------------------------
364 // Generate surface mesh
365 // -------------------------
368 int startWith = MESHCONST_MESHSURFACE;
369 int endWith = MESHCONST_OPTSURFACE;
373 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
376 err = netgen::OCCGenerateMesh(occgeo, ngMesh, startWith, endWith, optstr);
378 catch (Standard_Failure& ex) {
379 string comment = ex.DynamicType()->Name();
380 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
382 comment += ex.GetMessageString();
384 error(COMPERR_OCC_EXCEPTION, comment);
386 catch (NgException exc) {
387 error( SMESH_Comment("NgException: ") << exc.What() );
390 error(COMPERR_EXCEPTION,"Exception in netgen::OCCGenerateMesh()");
393 // ----------------------------------------------------
394 // Fill the SMESHDS with the generated nodes and faces
395 // ----------------------------------------------------
397 int nbNodes = ngMesh->GetNP();
398 int nbFaces = ngMesh->GetNSE();
400 int nbInputNodes = nodeVec.size();
401 nodeVec.resize( nbNodes, 0 );
404 for ( int i = nbInputNodes + 1; i <= nbNodes; ++i )
406 const MeshPoint& ngPoint = ngMesh->Point(i);
407 SMDS_MeshNode * node = meshDS->AddNode(ngPoint.X(), ngPoint.Y(), ngPoint.Z());
408 nodeVec[ i-1 ] = node;
412 bool reverse = ( aShape.Orientation() == TopAbs_REVERSED );
413 for ( int i = 1; i <= nbFaces ; ++i )
415 const Element2d& elem = ngMesh->SurfaceElement(i);
416 vector<const SMDS_MeshNode*> nodes( elem.GetNP() );
417 for (int j=1; j <= elem.GetNP(); ++j)
419 int pind = elem.PNum(j);
420 const SMDS_MeshNode* node = nodeVec.at(pind-1);
422 nodes[ nodes.size()-j ] = node;
425 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
427 const PointGeomInfo& pgi = elem.GeomInfoPi(j);
428 meshDS->SetNodeOnFace((SMDS_MeshNode*)node, faceID, pgi.u, pgi.v);
431 SMDS_MeshFace* face = 0;
432 if ( elem.GetType() == TRIG )
433 face = helper.AddFace(nodes[0],nodes[1],nodes[2]);
435 face = helper.AddFace(nodes[0],nodes[1],nodes[2],nodes[3]);
438 Ng_DeleteMesh((nglib::Ng_Mesh*)ngMesh);
441 NETGENPlugin_Mesher::RemoveTmpFiles();