1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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: SMESH_MesherHelper.cxx
21 // Created: 15.02.06 15:22:41
22 // Author: Sergey KUUL
23 // Copyright: Open CASCADE 2006
26 #include "SMESH_MesherHelper.hxx"
28 #include "SMDS_FacePosition.hxx"
29 #include "SMDS_EdgePosition.hxx"
30 #include "SMESH_MeshEditor.hxx"
32 #include <BRepAdaptor_Surface.hxx>
33 #include <BRepTools.hxx>
34 #include <BRep_Tool.hxx>
35 #include <Geom2d_Curve.hxx>
36 #include <Geom_Curve.hxx>
37 #include <Geom_Surface.hxx>
38 #include <TopExp_Explorer.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <gp_Pnt2d.hxx>
41 #include <ShapeAnalysis.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <Standard_Failure.hxx>
45 #include <Standard_ErrorHandler.hxx>
47 #include <utilities.h>
49 #define RETURN_BAD_RESULT(msg) { MESSAGE(msg); return false; }
51 //================================================================================
55 //================================================================================
57 SMESH_MesherHelper::SMESH_MesherHelper(SMESH_Mesh& theMesh)
58 : myMesh(&theMesh), myShapeID(-1), myCreateQuadratic(false)
60 mySetElemOnShape = ( ! myMesh->HasShapeToMesh() );
63 //=======================================================================
64 //function : CheckShape
66 //=======================================================================
68 bool SMESH_MesherHelper::IsQuadraticSubMesh(const TopoDS_Shape& aSh)
70 SMESHDS_Mesh* meshDS = GetMeshDS();
71 // we can create quadratic elements only if all elements
72 // created on subshapes of given shape are quadratic
73 // also we have to fill myNLinkNodeMap
74 myCreateQuadratic = true;
75 mySeamShapeIds.clear();
76 myDegenShapeIds.clear();
77 TopAbs_ShapeEnum subType( aSh.ShapeType()==TopAbs_FACE ? TopAbs_EDGE : TopAbs_FACE );
78 SMDSAbs_ElementType elemType( subType==TopAbs_FACE ? SMDSAbs_Face : SMDSAbs_Edge );
80 TopExp_Explorer exp( aSh, subType );
81 for (; exp.More() && myCreateQuadratic; exp.Next()) {
82 if ( SMESHDS_SubMesh * subMesh = meshDS->MeshElements( exp.Current() )) {
83 if ( SMDS_ElemIteratorPtr it = subMesh->GetElements() ) {
85 const SMDS_MeshElement* e = it->next();
86 if ( e->GetType() != elemType || !e->IsQuadratic() ) {
87 myCreateQuadratic = false;
92 switch ( e->NbNodes() ) {
94 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(2)); break;
96 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(3));
97 AddNLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(4));
98 AddNLinkNode(e->GetNode(2),e->GetNode(0),e->GetNode(5)); break;
100 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(4));
101 AddNLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(5));
102 AddNLinkNode(e->GetNode(2),e->GetNode(3),e->GetNode(6));
103 AddNLinkNode(e->GetNode(3),e->GetNode(0),e->GetNode(7));
106 myCreateQuadratic = false;
115 if(!myCreateQuadratic) {
116 myNLinkNodeMap.clear();
120 return myCreateQuadratic;
123 //================================================================================
125 * \brief Set geomerty to make elements on
126 * \param aSh - geomertic shape
128 //================================================================================
130 void SMESH_MesherHelper::SetSubShape(const int aShID)
132 if ( aShID == myShapeID )
135 SetSubShape( GetMeshDS()->IndexToShape( aShID ));
137 SetSubShape( TopoDS_Shape() );
140 //================================================================================
142 * \brief Set geomerty to make elements on
143 * \param aSh - geomertic shape
145 //================================================================================
147 void SMESH_MesherHelper::SetSubShape(const TopoDS_Shape& aSh)
149 if ( myShape.IsSame( aSh ))
153 mySeamShapeIds.clear();
154 myDegenShapeIds.clear();
156 if ( myShape.IsNull() ) {
160 SMESHDS_Mesh* meshDS = GetMeshDS();
161 myShapeID = meshDS->ShapeToIndex(aSh);
163 // treatment of periodic faces
164 for ( TopExp_Explorer eF( aSh, TopAbs_FACE ); eF.More(); eF.Next() )
166 const TopoDS_Face& face = TopoDS::Face( eF.Current() );
167 BRepAdaptor_Surface surface( face );
168 if ( surface.IsUPeriodic() || surface.IsVPeriodic() )
170 for ( TopExp_Explorer exp( face, TopAbs_EDGE ); exp.More(); exp.Next())
172 // look for a seam edge
173 const TopoDS_Edge& edge = TopoDS::Edge( exp.Current() );
174 if ( BRep_Tool::IsClosed( edge, face )) {
175 // initialize myPar1, myPar2 and myParIndex
176 if ( mySeamShapeIds.empty() ) {
178 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
179 if ( Abs( uv1.Coord(1) - uv2.Coord(1) ) < Abs( uv1.Coord(2) - uv2.Coord(2) ))
181 myParIndex = 1; // U periodic
182 myPar1 = surface.FirstUParameter();
183 myPar2 = surface.LastUParameter();
186 myParIndex = 2; // V periodic
187 myPar1 = surface.FirstVParameter();
188 myPar2 = surface.LastVParameter();
191 // store shapes indices
192 mySeamShapeIds.insert( meshDS->ShapeToIndex( edge ));
193 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next() )
194 mySeamShapeIds.insert( meshDS->ShapeToIndex( v.Current() ));
197 // look for a degenerated edge
198 if ( BRep_Tool::Degenerated( edge )) {
199 myDegenShapeIds.insert( meshDS->ShapeToIndex( edge ));
200 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next() )
201 myDegenShapeIds.insert( meshDS->ShapeToIndex( v.Current() ));
208 //================================================================================
210 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
211 * \param F - the face
212 * \retval bool - return true if the face is periodic
214 //================================================================================
216 bool SMESH_MesherHelper::GetNodeUVneedInFaceNode(const TopoDS_Face& F) const
218 if ( F.IsNull() ) return !mySeamShapeIds.empty();
220 if ( !F.IsNull() && !myShape.IsNull() && myShape.IsSame( F ))
221 return !mySeamShapeIds.empty();
223 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( F );
224 if ( !aSurface.IsNull() )
225 return ( aSurface->IsUPeriodic() || aSurface->IsVPeriodic() );
230 //=======================================================================
231 //function : IsMedium
233 //=======================================================================
235 bool SMESH_MesherHelper::IsMedium(const SMDS_MeshNode* node,
236 const SMDSAbs_ElementType typeToCheck)
238 return SMESH_MeshEditor::IsMedium( node, typeToCheck );
241 //=======================================================================
242 //function : AddNLinkNode
244 //=======================================================================
246 * Auxilary function for filling myNLinkNodeMap
248 void SMESH_MesherHelper::AddNLinkNode(const SMDS_MeshNode* n1,
249 const SMDS_MeshNode* n2,
250 const SMDS_MeshNode* n12)
252 NLink link( n1, n2 );
253 if ( n1 > n2 ) link = NLink( n2, n1 );
254 // add new record to map
255 myNLinkNodeMap.insert( make_pair(link,n12));
258 //=======================================================================
260 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
261 * \param uv1 - UV on the seam
262 * \param uv2 - UV within a face
263 * \retval gp_Pnt2d - selected UV
265 //=======================================================================
267 gp_Pnt2d SMESH_MesherHelper::GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const
269 double p1 = uv1.Coord( myParIndex );
270 double p2 = uv2.Coord( myParIndex );
271 double p3 = ( Abs( p1 - myPar1 ) < Abs( p1 - myPar2 )) ? myPar2 : myPar1;
272 if ( Abs( p2 - p1 ) > Abs( p2 - p3 ))
274 gp_Pnt2d result = uv1;
275 result.SetCoord( myParIndex, p1 );
279 //=======================================================================
281 * \brief Return node UV on face
282 * \param F - the face
283 * \param n - the node
284 * \param n2 - a node of element being created located inside a face
285 * \retval gp_XY - resulting UV
287 * Auxilary function called form GetMediumNode()
289 //=======================================================================
291 gp_XY SMESH_MesherHelper::GetNodeUV(const TopoDS_Face& F,
292 const SMDS_MeshNode* n,
293 const SMDS_MeshNode* n2) const
295 gp_Pnt2d uv( 1e100, 1e100 );
296 const SMDS_PositionPtr Pos = n->GetPosition();
297 if(Pos->GetTypeOfPosition()==SMDS_TOP_FACE)
299 // node has position on face
300 const SMDS_FacePosition* fpos =
301 static_cast<const SMDS_FacePosition*>(n->GetPosition().get());
302 uv = gp_Pnt2d(fpos->GetUParameter(),fpos->GetVParameter());
304 else if(Pos->GetTypeOfPosition()==SMDS_TOP_EDGE)
306 // node has position on edge => it is needed to find
307 // corresponding edge from face, get pcurve for this
308 // edge and recieve value from this pcurve
309 const SMDS_EdgePosition* epos =
310 static_cast<const SMDS_EdgePosition*>(n->GetPosition().get());
311 SMESHDS_Mesh* meshDS = GetMeshDS();
312 int edgeID = Pos->GetShapeId();
313 TopoDS_Edge E = TopoDS::Edge(meshDS->IndexToShape(edgeID));
316 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
317 uv = C2d->Value( epos->GetUParameter() );
318 // for a node on a seam edge select one of UVs on 2 pcurves
319 if ( n2 && mySeamShapeIds.find( edgeID ) != mySeamShapeIds.end() )
320 uv = GetUVOnSeam( uv, GetNodeUV( F, n2, 0 ));
322 else if(Pos->GetTypeOfPosition()==SMDS_TOP_VERTEX)
324 if ( int vertexID = n->GetPosition()->GetShapeId() ) {
326 const TopoDS_Vertex& V = TopoDS::Vertex(GetMeshDS()->IndexToShape(vertexID));
328 uv = BRep_Tool::Parameters( V, F );
330 catch (Standard_Failure& exc) {
334 for ( TopExp_Explorer vert(F,TopAbs_VERTEX); !ok && vert.More(); vert.Next() )
335 ok = ( V == vert.Current() );
338 cout << "SMESH_MesherHelper::GetNodeUV(); Vertex " << vertexID
339 << " not in face " << GetMeshDS()->ShapeToIndex( F ) << endl;
341 // get UV of a vertex closest to the node
343 gp_Pnt pn ( n->X(),n->Y(),n->Z() );
344 for ( TopExp_Explorer vert(F,TopAbs_VERTEX); !ok && vert.More(); vert.Next() ) {
345 TopoDS_Vertex curV = TopoDS::Vertex( vert.Current() );
346 gp_Pnt p = BRep_Tool::Pnt( curV );
347 double curDist = p.SquareDistance( pn );
348 if ( curDist < dist ) {
350 uv = BRep_Tool::Parameters( curV, F );
351 if ( dist < DBL_MIN ) break;
356 TopTools_ListIteratorOfListOfShape it( myMesh->GetAncestors( V ));
357 for ( ; it.More(); it.Next() ) {
358 if ( it.Value().ShapeType() == TopAbs_EDGE ) {
359 const TopoDS_Edge & edge = TopoDS::Edge( it.Value() );
361 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(edge, F, f, l);
362 if ( !C2d.IsNull() ) {
363 double u = ( V == TopExp::FirstVertex( edge ) ) ? f : l;
364 uv = C2d->Value( u );
371 if ( n2 && mySeamShapeIds.find( vertexID ) != mySeamShapeIds.end() )
372 uv = GetUVOnSeam( uv, GetNodeUV( F, n2, 0 ));
378 //=======================================================================
380 * \brief Return node U on edge
381 * \param E - the Edge
382 * \param n - the node
383 * \retval double - resulting U
385 * Auxilary function called form GetMediumNode()
387 //=======================================================================
389 double SMESH_MesherHelper::GetNodeU(const TopoDS_Edge& E,
390 const SMDS_MeshNode* n)
393 const SMDS_PositionPtr Pos = n->GetPosition();
394 if(Pos->GetTypeOfPosition()==SMDS_TOP_EDGE) {
395 const SMDS_EdgePosition* epos =
396 static_cast<const SMDS_EdgePosition*>(n->GetPosition().get());
397 param = epos->GetUParameter();
399 else if(Pos->GetTypeOfPosition()==SMDS_TOP_VERTEX) {
400 SMESHDS_Mesh * meshDS = GetMeshDS();
401 int vertexID = n->GetPosition()->GetShapeId();
402 const TopoDS_Vertex& V = TopoDS::Vertex(meshDS->IndexToShape(vertexID));
403 param = BRep_Tool::Parameter( V, E );
408 //=======================================================================
409 //function : GetMediumNode
411 //=======================================================================
413 * Special function for search or creation medium node
415 const SMDS_MeshNode* SMESH_MesherHelper::GetMediumNode(const SMDS_MeshNode* n1,
416 const SMDS_MeshNode* n2,
419 TopAbs_ShapeEnum shapeType = myShape.IsNull() ? TopAbs_SHAPE : myShape.ShapeType();
421 NLink link(( n1 < n2 ? n1 : n2 ), ( n1 < n2 ? n2 : n1 ));
422 ItNLinkNode itLN = myNLinkNodeMap.find( link );
423 if ( itLN != myNLinkNodeMap.end() ) {
424 return (*itLN).second;
427 // create medium node
429 SMESHDS_Mesh* meshDS = GetMeshDS();
430 int faceID = -1, edgeID = -1;
431 const SMDS_PositionPtr Pos1 = n1->GetPosition();
432 const SMDS_PositionPtr Pos2 = n2->GetPosition();
434 if( myShape.IsNull() )
436 if( Pos1->GetTypeOfPosition()==SMDS_TOP_FACE ) {
437 faceID = Pos1->GetShapeId();
439 else if( Pos2->GetTypeOfPosition()==SMDS_TOP_FACE ) {
440 faceID = Pos2->GetShapeId();
443 if( Pos1->GetTypeOfPosition()==SMDS_TOP_EDGE ) {
444 edgeID = Pos1->GetShapeId();
446 if( Pos2->GetTypeOfPosition()==SMDS_TOP_EDGE ) {
447 edgeID = Pos2->GetShapeId();
452 // we try to create medium node using UV parameters of
453 // nodes, else - medium between corresponding 3d points
454 if(faceID>-1 || shapeType == TopAbs_FACE) {
455 // obtaining a face and 2d points for nodes
457 if( myShape.IsNull() )
458 F = TopoDS::Face(meshDS->IndexToShape(faceID));
460 F = TopoDS::Face(myShape);
464 gp_XY p1 = GetNodeUV(F,n1,n2);
465 gp_XY p2 = GetNodeUV(F,n2,n1);
467 //checking if surface is periodic
468 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
469 Standard_Real UF,UL,VF,VL;
470 S->Bounds(UF,UL,VF,VL);
473 Standard_Boolean isUPeriodic = S->IsUPeriodic();
475 Standard_Real UPeriod = S->UPeriod();
476 Standard_Real p2x = p2.X()+ShapeAnalysis::AdjustByPeriod(p2.X(),p1.X(),UPeriod);
477 Standard_Real pmid = (p1.X()+p2x)/2.;
478 u = pmid+ShapeAnalysis::AdjustToPeriod(pmid,UF,UL);
481 u= (p1.X()+p2.X())/2.;
483 Standard_Boolean isVPeriodic = S->IsVPeriodic();
485 Standard_Real VPeriod = S->VPeriod();
486 Standard_Real p2y = p2.Y()+ShapeAnalysis::AdjustByPeriod(p2.Y(),p1.Y(),VPeriod);
487 Standard_Real pmid = (p1.Y()+p2y)/2.;
488 v = pmid+ShapeAnalysis::AdjustToPeriod(pmid,VF,VL);
491 v = (p1.Y()+p2.Y())/2.;
493 gp_Pnt P = S->Value(u, v);
494 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
495 meshDS->SetNodeOnFace(n12, faceID, u, v);
496 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
499 if (edgeID>-1 || shapeType == TopAbs_EDGE) {
502 if( myShape.IsNull() )
503 E = TopoDS::Edge(meshDS->IndexToShape(edgeID));
505 E = TopoDS::Edge(myShape);
509 double p1 = GetNodeU(E,n1);
510 double p2 = GetNodeU(E,n2);
513 Handle(Geom_Curve) C = BRep_Tool::Curve(E, f, l);
516 Standard_Boolean isPeriodic = C->IsPeriodic();
519 Standard_Real Period = C->Period();
520 Standard_Real p = p2+ShapeAnalysis::AdjustByPeriod(p2,p1,Period);
521 Standard_Real pmid = (p1+p)/2.;
522 u = pmid+ShapeAnalysis::AdjustToPeriod(pmid,C->FirstParameter(),C->LastParameter());
527 gp_Pnt P = C->Value( u );
528 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
529 meshDS->SetNodeOnEdge(n12, edgeID, u);
530 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
536 double x = ( n1->X() + n2->X() )/2.;
537 double y = ( n1->Y() + n2->Y() )/2.;
538 double z = ( n1->Z() + n2->Z() )/2.;
539 n12 = meshDS->AddNode(x,y,z);
541 meshDS->SetNodeOnEdge(n12, edgeID);
543 meshDS->SetNodeOnFace(n12, faceID);
545 meshDS->SetNodeInVolume(n12, myShapeID);
546 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
551 //=======================================================================
555 //=======================================================================
557 SMDS_MeshNode* SMESH_MesherHelper::AddNode(double x, double y, double z, int ID)
559 SMESHDS_Mesh * meshDS = GetMeshDS();
560 SMDS_MeshNode* node = 0;
562 node = meshDS->AddNodeWithID( x, y, z, ID );
564 node = meshDS->AddNode( x, y, z );
565 if ( mySetElemOnShape && myShapeID > 0 ) {
566 switch ( myShape.ShapeType() ) {
567 case TopAbs_SOLID: meshDS->SetNodeInVolume( node, myShapeID); break;
568 case TopAbs_SHELL: meshDS->SetNodeInVolume( node, myShapeID); break;
569 case TopAbs_FACE: meshDS->SetNodeOnFace( node, myShapeID); break;
570 case TopAbs_EDGE: meshDS->SetNodeOnEdge( node, myShapeID); break;
571 case TopAbs_VERTEX: meshDS->SetNodeOnVertex( node, myShapeID); break;
578 //=======================================================================
580 * Creates quadratic or linear edge
582 //=======================================================================
584 SMDS_MeshEdge* SMESH_MesherHelper::AddEdge(const SMDS_MeshNode* n1,
585 const SMDS_MeshNode* n2,
589 SMESHDS_Mesh * meshDS = GetMeshDS();
591 SMDS_MeshEdge* edge = 0;
592 if (myCreateQuadratic) {
593 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
595 edge = meshDS->AddEdgeWithID(n1, n2, n12, id);
597 edge = meshDS->AddEdge(n1, n2, n12);
601 edge = meshDS->AddEdgeWithID(n1, n2, id);
603 edge = meshDS->AddEdge(n1, n2);
606 if ( mySetElemOnShape && myShapeID > 0 )
607 meshDS->SetMeshElementOnShape( edge, myShapeID );
612 //=======================================================================
614 * Creates quadratic or linear triangle
616 //=======================================================================
618 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
619 const SMDS_MeshNode* n2,
620 const SMDS_MeshNode* n3,
624 SMESHDS_Mesh * meshDS = GetMeshDS();
625 SMDS_MeshFace* elem = 0;
626 if(!myCreateQuadratic) {
628 elem = meshDS->AddFaceWithID(n1, n2, n3, id);
630 elem = meshDS->AddFace(n1, n2, n3);
633 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
634 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
635 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
638 elem = meshDS->AddFaceWithID(n1, n2, n3, n12, n23, n31, id);
640 elem = meshDS->AddFace(n1, n2, n3, n12, n23, n31);
642 if ( mySetElemOnShape && myShapeID > 0 )
643 meshDS->SetMeshElementOnShape( elem, myShapeID );
648 //=======================================================================
650 * Creates quadratic or linear quadrangle
652 //=======================================================================
654 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
655 const SMDS_MeshNode* n2,
656 const SMDS_MeshNode* n3,
657 const SMDS_MeshNode* n4,
661 SMESHDS_Mesh * meshDS = GetMeshDS();
662 SMDS_MeshFace* elem = 0;
663 if(!myCreateQuadratic) {
665 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, id);
667 elem = meshDS->AddFace(n1, n2, n3, n4);
670 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
671 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
672 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
673 const SMDS_MeshNode* n41 = GetMediumNode(n4,n1,force3d);
676 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, n12, n23, n34, n41, id);
678 elem = meshDS->AddFace(n1, n2, n3, n4, n12, n23, n34, n41);
680 if ( mySetElemOnShape && myShapeID > 0 )
681 meshDS->SetMeshElementOnShape( elem, myShapeID );
686 //=======================================================================
688 * Creates quadratic or linear volume
690 //=======================================================================
692 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
693 const SMDS_MeshNode* n2,
694 const SMDS_MeshNode* n3,
695 const SMDS_MeshNode* n4,
696 const SMDS_MeshNode* n5,
697 const SMDS_MeshNode* n6,
701 SMESHDS_Mesh * meshDS = GetMeshDS();
702 SMDS_MeshVolume* elem = 0;
703 if(!myCreateQuadratic) {
705 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, id);
707 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6);
710 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
711 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
712 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
714 const SMDS_MeshNode* n45 = GetMediumNode(n4,n5,force3d);
715 const SMDS_MeshNode* n56 = GetMediumNode(n5,n6,force3d);
716 const SMDS_MeshNode* n64 = GetMediumNode(n6,n4,force3d);
718 const SMDS_MeshNode* n14 = GetMediumNode(n1,n4,force3d);
719 const SMDS_MeshNode* n25 = GetMediumNode(n2,n5,force3d);
720 const SMDS_MeshNode* n36 = GetMediumNode(n3,n6,force3d);
723 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6,
724 n12, n23, n31, n45, n56, n64, n14, n25, n36, id);
726 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6,
727 n12, n23, n31, n45, n56, n64, n14, n25, n36);
729 if ( mySetElemOnShape && myShapeID > 0 )
730 meshDS->SetMeshElementOnShape( elem, myShapeID );
735 //=======================================================================
737 * Creates quadratic or linear volume
739 //=======================================================================
741 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
742 const SMDS_MeshNode* n2,
743 const SMDS_MeshNode* n3,
744 const SMDS_MeshNode* n4,
748 SMESHDS_Mesh * meshDS = GetMeshDS();
749 SMDS_MeshVolume* elem = 0;
750 if(!myCreateQuadratic) {
752 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, id);
754 elem = meshDS->AddVolume(n1, n2, n3, n4);
757 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
758 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
759 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
761 const SMDS_MeshNode* n14 = GetMediumNode(n1,n4,force3d);
762 const SMDS_MeshNode* n24 = GetMediumNode(n2,n4,force3d);
763 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
766 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34, id);
768 elem = meshDS->AddVolume(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34);
770 if ( mySetElemOnShape && myShapeID > 0 )
771 meshDS->SetMeshElementOnShape( elem, myShapeID );
776 //=======================================================================
778 * Creates quadratic or linear pyramid
780 //=======================================================================
782 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
783 const SMDS_MeshNode* n2,
784 const SMDS_MeshNode* n3,
785 const SMDS_MeshNode* n4,
786 const SMDS_MeshNode* n5,
790 SMDS_MeshVolume* elem = 0;
791 if(!myCreateQuadratic) {
793 elem = GetMeshDS()->AddVolumeWithID(n1, n2, n3, n4, n5, id);
795 elem = GetMeshDS()->AddVolume(n1, n2, n3, n4, n5);
798 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
799 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
800 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
801 const SMDS_MeshNode* n41 = GetMediumNode(n4,n1,force3d);
803 const SMDS_MeshNode* n15 = GetMediumNode(n1,n5,force3d);
804 const SMDS_MeshNode* n25 = GetMediumNode(n2,n5,force3d);
805 const SMDS_MeshNode* n35 = GetMediumNode(n3,n5,force3d);
806 const SMDS_MeshNode* n45 = GetMediumNode(n4,n5,force3d);
809 elem = GetMeshDS()->AddVolumeWithID ( n1, n2, n3, n4, n5,
814 elem = GetMeshDS()->AddVolume( n1, n2, n3, n4, n5,
818 if ( mySetElemOnShape && myShapeID > 0 )
819 GetMeshDS()->SetMeshElementOnShape( elem, myShapeID );
824 //=======================================================================
826 * Creates quadratic or linear hexahedron
828 //=======================================================================
830 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
831 const SMDS_MeshNode* n2,
832 const SMDS_MeshNode* n3,
833 const SMDS_MeshNode* n4,
834 const SMDS_MeshNode* n5,
835 const SMDS_MeshNode* n6,
836 const SMDS_MeshNode* n7,
837 const SMDS_MeshNode* n8,
841 SMESHDS_Mesh * meshDS = GetMeshDS();
842 SMDS_MeshVolume* elem = 0;
843 if(!myCreateQuadratic) {
845 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8, id);
847 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8);
850 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
851 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
852 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
853 const SMDS_MeshNode* n41 = GetMediumNode(n4,n1,force3d);
855 const SMDS_MeshNode* n56 = GetMediumNode(n5,n6,force3d);
856 const SMDS_MeshNode* n67 = GetMediumNode(n6,n7,force3d);
857 const SMDS_MeshNode* n78 = GetMediumNode(n7,n8,force3d);
858 const SMDS_MeshNode* n85 = GetMediumNode(n8,n5,force3d);
860 const SMDS_MeshNode* n15 = GetMediumNode(n1,n5,force3d);
861 const SMDS_MeshNode* n26 = GetMediumNode(n2,n6,force3d);
862 const SMDS_MeshNode* n37 = GetMediumNode(n3,n7,force3d);
863 const SMDS_MeshNode* n48 = GetMediumNode(n4,n8,force3d);
866 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8,
867 n12, n23, n34, n41, n56, n67,
868 n78, n85, n15, n26, n37, n48, id);
870 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8,
871 n12, n23, n34, n41, n56, n67,
872 n78, n85, n15, n26, n37, n48);
874 if ( mySetElemOnShape && myShapeID > 0 )
875 meshDS->SetMeshElementOnShape( elem, myShapeID );
880 //=======================================================================
882 * \brief Load nodes bound to face into a map of node columns
883 * \param theParam2ColumnMap - map of node columns to fill
884 * \param theFace - the face on which nodes are searched for
885 * \param theBaseEdge - the edge nodes of which are columns' bases
886 * \param theMesh - the mesh containing nodes
887 * \retval bool - false if something is wrong
889 * The key of the map is a normalized parameter of each
890 * base node on theBaseEdge.
891 * This method works in supposition that nodes on the face
892 * forms a rectangular grid and elements can be quardrangles or triangles
894 //=======================================================================
896 bool SMESH_MesherHelper::LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
897 const TopoDS_Face& theFace,
898 const TopoDS_Edge& theBaseEdge,
899 SMESHDS_Mesh* theMesh)
901 // get vertices of theBaseEdge
902 TopoDS_Vertex vfb, vlb, vft; // first and last, bottom and top vertices
903 TopoDS_Edge eFrw = TopoDS::Edge( theBaseEdge.Oriented( TopAbs_FORWARD ));
904 TopExp::Vertices( eFrw, vfb, vlb );
906 // find the other edges of theFace and orientation of e1
907 TopoDS_Edge e1, e2, eTop;
908 bool rev1, CumOri = false;
909 TopExp_Explorer exp( theFace, TopAbs_EDGE );
911 for ( ; exp.More(); exp.Next() ) {
912 if ( ++nbEdges > 4 ) {
913 return false; // more than 4 edges in theFace
915 TopoDS_Edge e = TopoDS::Edge( exp.Current() );
916 if ( theBaseEdge.IsSame( e ))
918 TopoDS_Vertex vCommon;
919 if ( !TopExp::CommonVertex( theBaseEdge, e, vCommon ))
921 else if ( vCommon.IsSame( vfb )) {
923 vft = TopExp::LastVertex( e1, CumOri );
924 rev1 = vfb.IsSame( vft );
926 vft = TopExp::FirstVertex( e1, CumOri );
932 return false; // less than 4 edges in theFace
934 if ( e2.IsNull() && vfb.IsSame( vlb ))
937 // submeshes corresponding to shapes
938 SMESHDS_SubMesh* smFace = theMesh->MeshElements( theFace );
939 SMESHDS_SubMesh* smb = theMesh->MeshElements( theBaseEdge );
940 SMESHDS_SubMesh* smt = theMesh->MeshElements( eTop );
941 SMESHDS_SubMesh* sm1 = theMesh->MeshElements( e1 );
942 SMESHDS_SubMesh* sm2 = theMesh->MeshElements( e2 );
943 SMESHDS_SubMesh* smVfb = theMesh->MeshElements( vfb );
944 SMESHDS_SubMesh* smVlb = theMesh->MeshElements( vlb );
945 SMESHDS_SubMesh* smVft = theMesh->MeshElements( vft );
946 if (!smFace || !smb || !smt || !sm1 || !sm2 || !smVfb || !smVlb || !smVft ) {
947 RETURN_BAD_RESULT( "NULL submesh " <<smFace<<" "<<smb<<" "<<smt<<" "<<
948 sm1<<" "<<sm2<<" "<<smVfb<<" "<<smVlb<<" "<<smVft);
950 if ( smb->NbNodes() != smt->NbNodes() || sm1->NbNodes() != sm2->NbNodes() ) {
951 RETURN_BAD_RESULT(" Diff nb of nodes on opposite edges" );
953 if (smVfb->NbNodes() != 1 || smVlb->NbNodes() != 1 || smVft->NbNodes() != 1) {
954 RETURN_BAD_RESULT("Empty submesh of vertex");
956 // define whether mesh is quadratic
957 bool isQuadraticMesh = false;
958 SMDS_ElemIteratorPtr eIt = smFace->GetElements();
959 if ( !eIt->more() ) {
960 RETURN_BAD_RESULT("No elements on the face");
962 const SMDS_MeshElement* e = eIt->next();
963 isQuadraticMesh = e->IsQuadratic();
965 if ( sm1->NbNodes() * smb->NbNodes() != smFace->NbNodes() ) {
966 // check quadratic case
967 if ( isQuadraticMesh ) {
968 // what if there are quadrangles and triangles mixed?
969 // int n1 = sm1->NbNodes()/2;
970 // int n2 = smb->NbNodes()/2;
971 // int n3 = sm1->NbNodes() - n1;
972 // int n4 = smb->NbNodes() - n2;
973 // int nf = sm1->NbNodes()*smb->NbNodes() - n3*n4;
974 // if( nf != smFace->NbNodes() ) {
975 // MESSAGE( "Wrong nb face nodes: " <<
976 // sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
981 RETURN_BAD_RESULT( "Wrong nb face nodes: " <<
982 sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
986 int vsize = sm1->NbNodes() + 2;
987 int hsize = smb->NbNodes() + 2;
988 if(isQuadraticMesh) {
989 vsize = vsize - sm1->NbNodes()/2 -1;
990 hsize = hsize - smb->NbNodes()/2 -1;
993 // load nodes from theBaseEdge
995 set<const SMDS_MeshNode*> loadedNodes;
996 const SMDS_MeshNode* nullNode = 0;
998 vector<const SMDS_MeshNode*> & nVecf = theParam2ColumnMap[ 0.];
999 nVecf.resize( vsize, nullNode );
1000 loadedNodes.insert( nVecf[ 0 ] = smVfb->GetNodes()->next() );
1002 vector<const SMDS_MeshNode*> & nVecl = theParam2ColumnMap[ 1.];
1003 nVecl.resize( vsize, nullNode );
1004 loadedNodes.insert( nVecl[ 0 ] = smVlb->GetNodes()->next() );
1007 BRep_Tool::Range( eFrw, f, l );
1008 double range = l - f;
1009 SMDS_NodeIteratorPtr nIt = smb->GetNodes();
1010 const SMDS_MeshNode* node;
1011 while ( nIt->more() ) {
1013 if(IsMedium(node, SMDSAbs_Edge))
1015 const SMDS_EdgePosition* pos =
1016 dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1020 double u = ( pos->GetUParameter() - f ) / range;
1021 vector<const SMDS_MeshNode*> & nVec = theParam2ColumnMap[ u ];
1022 nVec.resize( vsize, nullNode );
1023 loadedNodes.insert( nVec[ 0 ] = node );
1025 if ( theParam2ColumnMap.size() != hsize ) {
1026 RETURN_BAD_RESULT( "Wrong node positions on theBaseEdge" );
1029 // load nodes from e1
1031 map< double, const SMDS_MeshNode*> sortedNodes; // sort by param on edge
1032 nIt = sm1->GetNodes();
1033 while ( nIt->more() ) {
1037 const SMDS_EdgePosition* pos =
1038 dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1042 sortedNodes.insert( make_pair( pos->GetUParameter(), node ));
1044 loadedNodes.insert( nVecf[ vsize - 1 ] = smVft->GetNodes()->next() );
1045 map< double, const SMDS_MeshNode*>::iterator u_n = sortedNodes.begin();
1046 int row = rev1 ? vsize - 1 : 0;
1047 int dRow = rev1 ? -1 : +1;
1048 for ( ; u_n != sortedNodes.end(); u_n++ ) {
1050 loadedNodes.insert( nVecf[ row ] = u_n->second );
1053 // try to load the rest nodes
1055 // get all faces from theFace
1056 TIDSortedElemSet allFaces, foundFaces;
1057 eIt = smFace->GetElements();
1058 while ( eIt->more() ) {
1059 const SMDS_MeshElement* e = eIt->next();
1060 if ( e->GetType() == SMDSAbs_Face )
1061 allFaces.insert( e );
1063 // Starting from 2 neighbour nodes on theBaseEdge, look for a face
1064 // the nodes belong to, and between the nodes of the found face,
1065 // look for a not loaded node considering this node to be the next
1066 // in a column of the starting second node. Repeat, starting
1067 // from nodes next to the previous starting nodes in their columns,
1068 // and so on while a face can be found. Then go the the next pair
1069 // of nodes on theBaseEdge.
1070 TParam2ColumnMap::iterator par_nVec_1 = theParam2ColumnMap.begin();
1071 TParam2ColumnMap::iterator par_nVec_2 = par_nVec_1;
1074 for ( par_nVec_2++; par_nVec_2 != theParam2ColumnMap.end(); par_nVec_1++, par_nVec_2++ ) {
1077 const SMDS_MeshNode* n1 = par_nVec_1->second[ row ];
1078 const SMDS_MeshNode* n2 = par_nVec_2->second[ row ];
1079 const SMDS_MeshElement* face = 0;
1080 bool lastColOnClosedFace = ( nVecf[ row ] == n2 );
1082 // look for a face by 2 nodes
1083 face = SMESH_MeshEditor::FindFaceInSet( n1, n2, allFaces, foundFaces );
1085 int nbFaceNodes = face->NbNodes();
1086 if ( face->IsQuadratic() )
1088 if ( nbFaceNodes>4 ) {
1089 RETURN_BAD_RESULT(" Too many nodes in a face: " << nbFaceNodes );
1091 // look for a not loaded node of the <face>
1093 const SMDS_MeshNode* n3 = 0; // a node defferent from n1 and n2
1094 for ( int i = 0; i < nbFaceNodes && !found; ++i ) {
1095 node = face->GetNode( i );
1096 found = loadedNodes.insert( node ).second;
1097 if ( !found && node != n1 && node != n2 )
1100 if ( lastColOnClosedFace && row + 1 < vsize ) {
1101 node = nVecf[ row + 1 ];
1102 found = ( face->GetNodeIndex( node ) >= 0 );
1105 if ( ++row > vsize - 1 ) {
1106 RETURN_BAD_RESULT( "Too many nodes in column "<< col <<": "<< row+1);
1108 par_nVec_2->second[ row ] = node;
1109 foundFaces.insert( face );
1111 if ( nbFaceNodes==4 ) {
1112 n1 = par_nVec_1->second[ row ];
1115 else if ( nbFaceNodes==3 && n3 == par_nVec_1->second[ row + 1 ] ) {
1119 RETURN_BAD_RESULT( "Not quad mesh, column "<< col );
1123 while ( face && n1 && n2 );
1125 if ( row < vsize - 1 ) {
1126 MESSAGE( "Too few nodes in column "<< col <<": "<< row+1);
1127 MESSAGE( "Base node 1: "<< par_nVec_1->second[0]);
1128 MESSAGE( "Base node 2: "<< par_nVec_2->second[0]);
1129 if ( n1 ) { MESSAGE( "Current node 1: "<< n1); }
1130 else { MESSAGE( "Current node 1: NULL"); }
1131 if ( n2 ) { MESSAGE( "Current node 2: "<< n2); }
1132 else { MESSAGE( "Current node 2: NULL"); }
1133 MESSAGE( "first base node: "<< theParam2ColumnMap.begin()->second[0]);
1134 MESSAGE( "last base node: "<< theParam2ColumnMap.rbegin()->second[0]);
1137 } // loop on columns