1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS, L3S, LJLL, MENSI
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : NETGENPlugin_NETGEN_2D_ONLY.cxx
21 // Author : Edward AGAPOV (OCC)
25 #include "NETGENPlugin_NETGEN_2D_ONLY.hxx"
27 #include "NETGENPlugin_Mesher.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 <Standard_ErrorHandler.hxx>
42 #include <Standard_Failure.hxx>
44 #include "utilities.h"
56 #include <occgeom.hpp>
57 #include <meshing.hpp>
58 //#include <meshtype.hpp>
60 extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, int, int, char*);
61 /*extern*/ MeshingParameters mparam;
65 using namespace netgen;
66 using namespace nglib;
68 //=============================================================================
72 //=============================================================================
74 NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY(int hypId, int studyId,
76 : SMESH_2D_Algo(hypId, studyId, gen)
78 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::NETGENPlugin_NETGEN_2D_ONLY");
79 _name = "NETGEN_2D_ONLY";
81 _shapeType = (1 << TopAbs_FACE);// 1 bit /shape type
83 _compatibleHypothesis.push_back("MaxElementArea");
84 _compatibleHypothesis.push_back("LengthFromEdges");
85 _compatibleHypothesis.push_back("QuadranglePreference");
87 _hypMaxElementArea = 0;
88 _hypLengthFromEdges = 0;
89 _hypQuadranglePreference = 0;
92 //=============================================================================
96 //=============================================================================
98 NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY()
100 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::~NETGENPlugin_NETGEN_2D_ONLY");
103 //=============================================================================
107 //=============================================================================
109 bool NETGENPlugin_NETGEN_2D_ONLY::CheckHypothesis (SMESH_Mesh& aMesh,
110 const TopoDS_Shape& aShape,
111 Hypothesis_Status& aStatus)
113 _hypMaxElementArea = 0;
114 _hypLengthFromEdges = 0;
115 _hypQuadranglePreference = 0;
117 const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
121 aStatus = HYP_OK; //SMESH_Hypothesis::HYP_MISSING;
122 return true; // (PAL13464) can work with no hypothesis, LengthFromEdges is default one
125 aStatus = HYP_MISSING;
127 list<const SMESHDS_Hypothesis*>::const_iterator ith;
128 for (ith = hyps.begin(); ith != hyps.end(); ++ith )
130 const SMESHDS_Hypothesis* hyp = (*ith);
132 string hypName = hyp->GetName();
134 if ( hypName == "MaxElementArea")
135 _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea*> (hyp);
136 else if ( hypName == "LengthFromEdges" )
137 _hypLengthFromEdges = static_cast<const StdMeshers_LengthFromEdges*> (hyp);
138 else if ( hypName == "QuadranglePreference" )
139 _hypQuadranglePreference = static_cast<const StdMeshers_QuadranglePreference*>(hyp);
141 aStatus = HYP_INCOMPATIBLE;
146 if ( _hypMaxElementArea && _hypLengthFromEdges ) {
147 aStatus = HYP_CONCURENT;
151 if ( _hypMaxElementArea || _hypLengthFromEdges )
154 return aStatus == HYP_OK;
157 //================================================================================
159 * \brief Fill netgen mesh with segments
160 * \retval SMESH_ComputeErrorPtr - error description
162 //================================================================================
164 static TError AddSegmentsToMesh(netgen::Mesh& ngMesh,
166 const TSideVector& wires,
167 SMESH_MesherHelper& helper,
168 vector< const SMDS_MeshNode* > & nodeVec)
170 // ----------------------------
171 // Check wires and count nodes
172 // ----------------------------
174 for ( int iW = 0; iW < wires.size(); ++iW )
176 StdMeshers_FaceSidePtr wire = wires[ iW ];
177 if ( wire->MissVertexNode() )
179 (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH, "Missing nodes on vertices"));
181 const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
182 if ( uvPtVec.size() != wire->NbPoints() )
184 (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,
185 SMESH_Comment("Unexpected nb of points on wire ") << iW
186 << ": " << uvPtVec.size()<<" != "<<wire->NbPoints()));
187 nbNodes += wire->NbSegments();
189 nodeVec.reserve( nbNodes );
195 // netgen::Box<3> bb = geom.GetBoundingBox();
196 // bb.Increase (bb.Diam()/10);
197 // ngMesh.SetLocalH (bb.PMin(), bb.PMax(), 0.5); // set grading
199 const int faceID = 1, solidID = 0;
200 ngMesh.AddFaceDescriptor (FaceDescriptor(faceID, solidID, solidID, 0));
202 for ( int iW = 0; iW < wires.size(); ++iW )
204 StdMeshers_FaceSidePtr wire = wires[ iW ];
205 const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
207 int firstPointID = ngMesh.GetNP() + 1;
208 int edgeID = 1, posID = -2;
209 for ( int i = 0; i < wire->NbSegments(); ++i ) // loop on segments
211 // Add the first point of a segment
212 const SMDS_MeshNode * n = uvPtVec[ i ].node;
213 const int posShapeID = n->GetPosition()->GetShapeId();
215 // skip nodes on degenerated edges
216 if ( helper.IsDegenShape( posShapeID ) &&
217 helper.IsDegenShape( uvPtVec[ i+1 ].node->GetPosition()->GetShapeId() ))
220 nodeVec.push_back( n );
222 MeshPoint mp( Point<3> (n->X(), n->Y(), n->Z()) );
223 ngMesh.AddPoint ( mp, 1, EDGEPOINT );
228 seg.p1 = ngMesh.GetNP(); // ng node id
229 seg.p2 = seg.p1 + 1; // ng node id
230 seg.edgenr = ngMesh.GetNSeg() + 1;// segment id
231 seg.si = faceID; // = geom.fmap.FindIndex (face);
233 for ( int iEnd = 0; iEnd < 2; ++iEnd)
235 const UVPtStruct& pnt = uvPtVec[ i + iEnd ];
237 seg.epgeominfo[ iEnd ].dist = pnt.param; // param on curve
238 seg.epgeominfo[ iEnd ].u = pnt.u;
239 seg.epgeominfo[ iEnd ].v = pnt.v;
241 // find out edge id and node parameter on edge
242 bool onVertex = ( pnt.node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX );
243 if ( onVertex || posShapeID != posID )
246 double normParam = pnt.normParam;
248 normParam = 0.5 * ( uvPtVec[ i ].normParam + uvPtVec[ i+1 ].normParam );
249 const TopoDS_Edge& edge = wire->Edge( wire->EdgeIndex( normParam ));
250 edgeID = geom.emap.FindIndex( edge );
252 if ( onVertex ) // param on curve is different on each of two edges
253 seg.epgeominfo[ iEnd ].dist = helper.GetNodeU( edge, pnt.node );
255 seg.epgeominfo[ iEnd ].edgenr = edgeID; // = geom.emap.FindIndex(edge);
258 ngMesh.AddSegment (seg);
260 // cout << "Segment: " << seg.edgenr << endl
261 // << "\tp1: " << seg.p1 << endl
262 // << "\tp2: " << seg.p2 << endl
263 // << "\tp0 param: " << seg.epgeominfo[ 0 ].dist << endl
264 // << "\tp0 uv: " << seg.epgeominfo[ 0 ].u <<", "<< seg.epgeominfo[ 0 ].v << endl
265 // << "\tp0 edge: " << seg.epgeominfo[ 0 ].edgenr << endl
266 // << "\tp1 param: " << seg.epgeominfo[ 1 ].dist << endl
267 // << "\tp1 uv: " << seg.epgeominfo[ 1 ].u <<", "<< seg.epgeominfo[ 1 ].v << endl
268 // << "\tp1 edge: " << seg.epgeominfo[ 1 ].edgenr << endl;
270 Segment& seg = ngMesh.LineSegment( ngMesh.GetNSeg() );
271 seg.p2 = firstPointID;
274 ngMesh.CalcSurfacesOfNode();
279 //=============================================================================
281 *Here we are going to use the NETGEN mesher
283 //=============================================================================
285 bool NETGENPlugin_NETGEN_2D_ONLY::Compute(SMESH_Mesh& aMesh,
286 const TopoDS_Shape& aShape)
288 MESSAGE("NETGENPlugin_NETGEN_2D_ONLY::Compute()");
290 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
291 int faceID = meshDS->ShapeToIndex( aShape );
293 SMESH_MesherHelper helper(aMesh);
294 _quadraticMesh = helper.IsQuadraticSubMesh(aShape);
295 helper.SetElementsOnShape( true );
296 const bool ignoreMediumNodes = _quadraticMesh;
298 // ------------------------
299 // get all edges of a face
300 // ------------------------
301 const TopoDS_Face F = TopoDS::Face( aShape.Oriented( TopAbs_FORWARD ));
303 TSideVector wires = StdMeshers_FaceSide::GetFaceWires( F, aMesh, ignoreMediumNodes, problem );
304 if ( problem && !problem->IsOK() )
305 return error( problem );
306 int nbWires = wires.size();
308 return error( "Problem in StdMeshers_FaceSide::GetFaceWires()");
309 if ( wires[0]->NbSegments() < 3 ) // ex: a circle with 2 segments
310 return error(COMPERR_BAD_INPUT_MESH,
311 SMESH_Comment("Too few segments: ")<<wires[0]->NbSegments());
313 // -------------------------
314 // Make input netgen mesh
315 // -------------------------
318 netgen::Mesh * ngMesh = new netgen::Mesh ();
320 netgen::OCCGeometry occgeo;
321 NETGENPlugin_Mesher::PrepareOCCgeometry( occgeo, F, aMesh );
322 occgeo.fmap.Clear(); // face can be reversed, which is wrong in this case (issue 19978)
323 occgeo.fmap.Add( F );
325 vector< const SMDS_MeshNode* > nodeVec;
326 problem = AddSegmentsToMesh( *ngMesh, occgeo, wires, helper, nodeVec );
327 if ( problem && !problem->IsOK() ) {
328 delete ngMesh; Ng_Exit();
329 return error( problem );
332 // --------------------
333 // compute edge length
334 // --------------------
336 double edgeLength = 0;
337 if (_hypLengthFromEdges || !_hypLengthFromEdges && !_hypMaxElementArea)
340 for ( int iW = 0; iW < nbWires; ++iW )
342 edgeLength += wires[ iW ]->Length();
343 nbSegments += wires[ iW ]->NbSegments();
346 edgeLength /= nbSegments;
348 if ( _hypMaxElementArea )
350 double maxArea = _hypMaxElementArea->GetMaxArea();
351 edgeLength = sqrt(2. * maxArea/sqrt(3.0));
353 if ( edgeLength < DBL_MIN )
354 edgeLength = occgeo.GetBoundingBox().Diam();
356 //cout << " edgeLength = " << edgeLength << endl;
358 netgen::mparam.maxh = edgeLength;
359 netgen::mparam.quad = _hypQuadranglePreference ? 1 : 0;
360 //ngMesh->SetGlobalH ( edgeLength );
362 // -------------------------
363 // Generate surface mesh
364 // -------------------------
367 int startWith = MESHCONST_MESHSURFACE;
368 int endWith = MESHCONST_OPTSURFACE;
372 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
375 err = netgen::OCCGenerateMesh(occgeo, ngMesh, startWith, endWith, optstr);
377 catch (Standard_Failure& ex) {
378 string comment = ex.DynamicType()->Name();
379 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
381 comment += ex.GetMessageString();
383 error(COMPERR_OCC_EXCEPTION, comment);
385 catch (NgException exc) {
386 error( SMESH_Comment("NgException: ") << exc.What() );
389 error(COMPERR_EXCEPTION,"Exception in netgen::OCCGenerateMesh()");
392 // ----------------------------------------------------
393 // Fill the SMESHDS with the generated nodes and faces
394 // ----------------------------------------------------
396 int nbNodes = ngMesh->GetNP();
397 int nbFaces = ngMesh->GetNSE();
399 int nbInputNodes = nodeVec.size();
400 nodeVec.resize( nbNodes, 0 );
403 for ( int i = nbInputNodes + 1; i <= nbNodes; ++i )
405 const MeshPoint& ngPoint = ngMesh->Point(i);
406 SMDS_MeshNode * node = meshDS->AddNode(ngPoint.X(), ngPoint.Y(), ngPoint.Z());
407 nodeVec[ i-1 ] = node;
411 bool reverse = ( aShape.Orientation() == TopAbs_REVERSED );
412 for ( int i = 1; i <= nbFaces ; ++i )
414 const Element2d& elem = ngMesh->SurfaceElement(i);
415 vector<const SMDS_MeshNode*> nodes( elem.GetNP() );
416 for (int j=1; j <= elem.GetNP(); ++j)
418 int pind = elem.PNum(j);
419 const SMDS_MeshNode* node = nodeVec.at(pind-1);
421 nodes[ nodes.size()-j ] = node;
424 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
426 const PointGeomInfo& pgi = elem.GeomInfoPi(j);
427 meshDS->SetNodeOnFace((SMDS_MeshNode*)node, faceID, pgi.u, pgi.v);
430 SMDS_MeshFace* face = 0;
431 if ( elem.GetType() == TRIG )
432 face = helper.AddFace(nodes[0],nodes[1],nodes[2]);
434 face = helper.AddFace(nodes[0],nodes[1],nodes[2],nodes[3]);
437 Ng_DeleteMesh((nglib::Ng_Mesh*)ngMesh);
440 NETGENPlugin_Mesher::RemoveTmpFiles();