1 // File: SMESH_MesherHelper.cxx
2 // Created: 15.02.06 15:22:41
4 // Copyright: Open CASCADE 2006
7 #include "SMESH_MesherHelper.hxx"
9 #include "SMDS_FacePosition.hxx"
10 #include "SMDS_EdgePosition.hxx"
11 #include "SMESH_MeshEditor.hxx"
13 #include <BRepAdaptor_Surface.hxx>
14 #include <BRepTools.hxx>
15 #include <BRep_Tool.hxx>
16 #include <Geom2d_Curve.hxx>
17 #include <Geom_Curve.hxx>
18 #include <Geom_Surface.hxx>
19 #include <TopExp_Explorer.hxx>
20 #include <TopTools_MapOfShape.hxx>
21 #include <gp_Pnt2d.hxx>
22 #include <ShapeAnalysis.hxx>
24 //=======================================================================
25 //function : CheckShape
27 //=======================================================================
29 bool SMESH_MesherHelper::IsQuadraticSubMesh(const TopoDS_Shape& aSh)
31 SMESHDS_Mesh* meshDS = GetMesh()->GetMeshDS();
32 myShapeID = meshDS->ShapeToIndex(aSh);
33 // we can create quadratic elements only if all elements
34 // created on subshapes of given shape are quadratic
35 // also we have to fill myNLinkNodeMap
36 myCreateQuadratic = true;
37 mySeamShapeIds.clear();
38 TopAbs_ShapeEnum subType( aSh.ShapeType()==TopAbs_FACE ? TopAbs_EDGE : TopAbs_FACE );
39 SMDSAbs_ElementType elemType( subType==TopAbs_FACE ? SMDSAbs_Face : SMDSAbs_Edge );
41 TopExp_Explorer exp( aSh, subType );
42 for (; exp.More() && myCreateQuadratic; exp.Next()) {
43 if ( SMESHDS_SubMesh * subMesh = meshDS->MeshElements( exp.Current() )) {
44 if ( SMDS_ElemIteratorPtr it = subMesh->GetElements() ) {
46 const SMDS_MeshElement* e = it->next();
47 if ( e->GetType() != elemType || !e->IsQuadratic() ) {
48 myCreateQuadratic = false;
53 switch ( e->NbNodes() ) {
55 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(2)); break;
57 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(3));
58 AddNLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(4));
59 AddNLinkNode(e->GetNode(2),e->GetNode(0),e->GetNode(5)); break;
61 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(4));
62 AddNLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(5));
63 AddNLinkNode(e->GetNode(2),e->GetNode(3),e->GetNode(6));
64 AddNLinkNode(e->GetNode(3),e->GetNode(0),e->GetNode(7));
67 myCreateQuadratic = false;
76 if(!myCreateQuadratic) {
77 myNLinkNodeMap.clear();
82 return myCreateQuadratic;
85 //================================================================================
87 * \brief Set geomerty to make elements on
88 * \param aSh - geomertic shape
90 //================================================================================
92 void SMESH_MesherHelper::SetSubShape(const TopoDS_Shape& aSh)
96 // treatment of periodic faces
97 if ( aSh.ShapeType() == TopAbs_FACE )
99 const TopoDS_Face& face = TopoDS::Face( aSh );
100 BRepAdaptor_Surface surface( face );
101 if ( surface.IsUPeriodic() || surface.IsVPeriodic() )
103 SMESHDS_Mesh* meshDS = GetMesh()->GetMeshDS();
104 // look for a seam edge
105 for ( TopExp_Explorer exp( face, TopAbs_EDGE ); exp.More(); exp.Next()) {
106 const TopoDS_Edge& edge = TopoDS::Edge( exp.Current() );
107 if ( BRep_Tool::IsClosed( edge, face )) {
108 // initialize myPar1, myPar2 and myParIndex
109 if ( mySeamShapeIds.empty() ) {
111 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
112 if ( Abs( uv1.Coord(1) - uv2.Coord(1) ) < Abs( uv1.Coord(2) - uv2.Coord(2) ))
114 myParIndex = 1; // U periodic
115 myPar1 = surface.FirstUParameter();
116 myPar2 = surface.LastUParameter();
119 myParIndex = 2; // V periodic
120 myPar1 = surface.FirstVParameter();
121 myPar2 = surface.LastVParameter();
124 // store shapes indices
125 mySeamShapeIds.insert( meshDS->ShapeToIndex( exp.Current() ));
126 for ( TopExp_Explorer v( exp.Current(), TopAbs_VERTEX ); v.More(); v.Next() )
127 mySeamShapeIds.insert( meshDS->ShapeToIndex( v.Current() ));
135 //=======================================================================
136 //function : IsMedium
138 //=======================================================================
140 bool SMESH_MesherHelper::IsMedium(const SMDS_MeshNode* node,
141 const SMDSAbs_ElementType typeToCheck)
143 return SMESH_MeshEditor::IsMedium( node, typeToCheck );
146 //=======================================================================
147 //function : AddNLinkNode
149 //=======================================================================
151 * Auxilary function for filling myNLinkNodeMap
153 void SMESH_MesherHelper::AddNLinkNode(const SMDS_MeshNode* n1,
154 const SMDS_MeshNode* n2,
155 const SMDS_MeshNode* n12)
157 NLink link( n1, n2 );
158 if ( n1 > n2 ) link = NLink( n2, n1 );
159 // add new record to map
160 myNLinkNodeMap.insert( make_pair(link,n12));
163 //=======================================================================
165 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
166 * \param uv1 - UV on the seam
167 * \param uv2 - UV within a face
168 * \retval gp_Pnt2d - selected UV
170 //=======================================================================
172 gp_Pnt2d SMESH_MesherHelper::GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const
174 double p1 = uv1.Coord( myParIndex );
175 double p2 = uv2.Coord( myParIndex );
176 double p3 = ( Abs( p1 - myPar1 ) < Abs( p1 - myPar2 )) ? myPar2 : myPar1;
177 if ( Abs( p2 - p1 ) > Abs( p2 - p3 ))
179 gp_Pnt2d result = uv1;
180 result.SetCoord( myParIndex, p1 );
184 //=======================================================================
186 * \brief Return node UV on face
187 * \param F - the face
188 * \param n - the node
189 * \param n2 - a medium node will be placed between n and n2
190 * \retval gp_XY - resulting UV
192 * Auxilary function called form GetMediumNode()
194 //=======================================================================
196 gp_XY SMESH_MesherHelper::GetNodeUV(const TopoDS_Face& F,
197 const SMDS_MeshNode* n,
198 const SMDS_MeshNode* n2)
201 const SMDS_PositionPtr Pos = n->GetPosition();
202 if(Pos->GetTypeOfPosition()==SMDS_TOP_FACE) {
203 // node has position on face
204 const SMDS_FacePosition* fpos =
205 static_cast<const SMDS_FacePosition*>(n->GetPosition().get());
206 uv = gp_Pnt2d(fpos->GetUParameter(),fpos->GetVParameter());
208 else if(Pos->GetTypeOfPosition()==SMDS_TOP_EDGE) {
209 // node has position on edge => it is needed to find
210 // corresponding edge from face, get pcurve for this
211 // edge and recieve value from this pcurve
212 const SMDS_EdgePosition* epos =
213 static_cast<const SMDS_EdgePosition*>(n->GetPosition().get());
214 SMESHDS_Mesh* meshDS = GetMesh()->GetMeshDS();
215 int edgeID = Pos->GetShapeId();
216 TopoDS_Edge E = TopoDS::Edge(meshDS->IndexToShape(edgeID));
219 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
220 uv = C2d->Value( epos->GetUParameter() );
221 // for a node on a seam edge select one of UVs on 2 pcurves
222 if ( n2 && mySeamShapeIds.find( edgeID ) != mySeamShapeIds.end() )
223 uv = GetUVOnSeam( uv, GetNodeUV( F, n2, 0 ));
225 else if(Pos->GetTypeOfPosition()==SMDS_TOP_VERTEX) {
226 SMESHDS_Mesh * meshDS = GetMesh()->GetMeshDS();
227 int vertexID = n->GetPosition()->GetShapeId();
228 const TopoDS_Vertex& V = TopoDS::Vertex(meshDS->IndexToShape(vertexID));
229 uv = BRep_Tool::Parameters( V, F );
230 if ( n2 && mySeamShapeIds.find( vertexID ) != mySeamShapeIds.end() )
231 uv = GetUVOnSeam( uv, GetNodeUV( F, n2, 0 ));
236 //=======================================================================
238 * \brief Return node U on edge
239 * \param E - the Edge
240 * \param n - the node
241 * \retval double - resulting U
243 * Auxilary function called form GetMediumNode()
245 //=======================================================================
247 double SMESH_MesherHelper::GetNodeU(const TopoDS_Edge& E,
248 const SMDS_MeshNode* n)
251 const SMDS_PositionPtr Pos = n->GetPosition();
252 if(Pos->GetTypeOfPosition()==SMDS_TOP_EDGE) {
253 const SMDS_EdgePosition* epos =
254 static_cast<const SMDS_EdgePosition*>(n->GetPosition().get());
255 param = epos->GetUParameter();
257 else if(Pos->GetTypeOfPosition()==SMDS_TOP_VERTEX) {
258 SMESHDS_Mesh * meshDS = GetMesh()->GetMeshDS();
259 int vertexID = n->GetPosition()->GetShapeId();
260 const TopoDS_Vertex& V = TopoDS::Vertex(meshDS->IndexToShape(vertexID));
261 param = BRep_Tool::Parameter( V, E );
266 //=======================================================================
267 //function : GetMediumNode
269 //=======================================================================
271 * Special function for search or creation medium node
273 const SMDS_MeshNode* SMESH_MesherHelper::GetMediumNode(const SMDS_MeshNode* n1,
274 const SMDS_MeshNode* n2,
277 TopAbs_ShapeEnum shapeType = myShape.IsNull() ? TopAbs_SHAPE : myShape.ShapeType();
279 NLink link(( n1 < n2 ? n1 : n2 ), ( n1 < n2 ? n2 : n1 ));
280 ItNLinkNode itLN = myNLinkNodeMap.find( link );
281 if ( itLN != myNLinkNodeMap.end() ) {
282 return (*itLN).second;
285 // create medium node
287 SMESHDS_Mesh* meshDS = GetMesh()->GetMeshDS();
288 int faceID = -1, edgeID = -1;
289 const SMDS_PositionPtr Pos1 = n1->GetPosition();
290 const SMDS_PositionPtr Pos2 = n2->GetPosition();
292 if( myShape.IsNull() )
294 if( Pos1->GetTypeOfPosition()==SMDS_TOP_FACE ) {
295 faceID = Pos1->GetShapeId();
297 else if( Pos2->GetTypeOfPosition()==SMDS_TOP_FACE ) {
298 faceID = Pos2->GetShapeId();
301 if( Pos1->GetTypeOfPosition()==SMDS_TOP_EDGE ) {
302 edgeID = Pos1->GetShapeId();
304 if( Pos2->GetTypeOfPosition()==SMDS_TOP_EDGE ) {
305 edgeID = Pos2->GetShapeId();
310 // we try to create medium node using UV parameters of
311 // nodes, else - medium between corresponding 3d points
312 if(faceID>-1 || shapeType == TopAbs_FACE) {
313 // obtaining a face and 2d points for nodes
315 if( myShape.IsNull() )
316 F = TopoDS::Face(meshDS->IndexToShape(faceID));
318 F = TopoDS::Face(myShape);
320 gp_XY p1 = GetNodeUV(F,n1,n2);
321 gp_XY p2 = GetNodeUV(F,n2,n1);
323 //checking if surface is periodic
324 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
325 Standard_Real UF,UL,VF,VL;
326 S->Bounds(UF,UL,VF,VL);
329 Standard_Boolean isUPeriodic = S->IsUPeriodic();
331 Standard_Real UPeriod = S->UPeriod();
332 Standard_Real p2x = p2.X()+ShapeAnalysis::AdjustByPeriod(p2.X(),p1.X(),UPeriod);
333 Standard_Real pmid = (p1.X()+p2x)/2.;
334 u = pmid+ShapeAnalysis::AdjustToPeriod(pmid,UF,UL);
337 u= (p1.X()+p2.X())/2.;
339 Standard_Boolean isVPeriodic = S->IsVPeriodic();
341 Standard_Real VPeriod = S->VPeriod();
342 Standard_Real p2y = p2.Y()+ShapeAnalysis::AdjustByPeriod(p2.Y(),p1.Y(),VPeriod);
343 Standard_Real pmid = (p1.Y()+p2y)/2.;
344 v = pmid+ShapeAnalysis::AdjustToPeriod(pmid,VF,VL);
347 v = (p1.Y()+p2.Y())/2.;
349 gp_Pnt P = S->Value(u, v);
350 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
351 meshDS->SetNodeOnFace(n12, faceID, u, v);
352 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
355 if (edgeID>-1 || shapeType == TopAbs_EDGE) {
358 if( myShape.IsNull() )
359 E = TopoDS::Edge(meshDS->IndexToShape(edgeID));
361 E = TopoDS::Edge(myShape);
363 double p1 = GetNodeU(E,n1);
364 double p2 = GetNodeU(E,n2);
367 Handle(Geom_Curve) C = BRep_Tool::Curve(E, f, l);
370 Standard_Boolean isPeriodic = C->IsPeriodic();
373 Standard_Real Period = C->Period();
374 Standard_Real p = p2+ShapeAnalysis::AdjustByPeriod(p2,p1,Period);
375 Standard_Real pmid = (p1+p)/2.;
376 u = pmid+ShapeAnalysis::AdjustToPeriod(pmid,C->FirstParameter(),C->LastParameter());
381 gp_Pnt P = C->Value( u );
382 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
383 meshDS->SetNodeOnEdge(n12, edgeID, u);
384 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
390 double x = ( n1->X() + n2->X() )/2.;
391 double y = ( n1->Y() + n2->Y() )/2.;
392 double z = ( n1->Z() + n2->Z() )/2.;
393 n12 = meshDS->AddNode(x,y,z);
395 meshDS->SetNodeOnEdge(n12, edgeID);
397 meshDS->SetNodeOnFace(n12, faceID);
399 meshDS->SetNodeInVolume(n12, myShapeID);
400 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
405 //=======================================================================
406 //function : AddQuadraticEdge
408 //=======================================================================
410 * Special function for creation quadratic edge
412 SMDS_QuadraticEdge* SMESH_MesherHelper::AddQuadraticEdge(const SMDS_MeshNode* n1,
413 const SMDS_MeshNode* n2,
417 SMESHDS_Mesh * meshDS = GetMesh()->GetMeshDS();
419 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
421 myCreateQuadratic = true;
424 return (SMDS_QuadraticEdge*)(meshDS->AddEdgeWithID(n1, n2, n12, id));
426 return (SMDS_QuadraticEdge*)(meshDS->AddEdge(n1, n2, n12));
429 //=======================================================================
432 //=======================================================================
434 * Special function for creation quadratic triangle
436 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
437 const SMDS_MeshNode* n2,
438 const SMDS_MeshNode* n3,
442 SMESHDS_Mesh * meshDS = GetMesh()->GetMeshDS();
443 if(!myCreateQuadratic) {
445 return meshDS->AddFaceWithID(n1, n2, n3, id);
447 return meshDS->AddFace(n1, n2, n3);
450 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
451 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
452 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
455 return meshDS->AddFaceWithID(n1, n2, n3, n12, n23, n31, id);
457 return meshDS->AddFace(n1, n2, n3, n12, n23, n31);
461 //=======================================================================
464 //=======================================================================
466 * Special function for creation quadratic quadrangle
468 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
469 const SMDS_MeshNode* n2,
470 const SMDS_MeshNode* n3,
471 const SMDS_MeshNode* n4,
475 SMESHDS_Mesh * meshDS = GetMesh()->GetMeshDS();
476 if(!myCreateQuadratic) {
478 return meshDS->AddFaceWithID(n1, n2, n3, n4, id);
480 return meshDS->AddFace(n1, n2, n3, n4);
483 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
484 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
485 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
486 const SMDS_MeshNode* n41 = GetMediumNode(n4,n1,force3d);
489 return meshDS->AddFaceWithID(n1, n2, n3, n4, n12, n23, n34, n41, id);
491 return meshDS->AddFace(n1, n2, n3, n4, n12, n23, n34, n41);
495 //=======================================================================
496 //function : AddVolume
498 //=======================================================================
500 * Special function for creation quadratic volume
502 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
503 const SMDS_MeshNode* n2,
504 const SMDS_MeshNode* n3,
505 const SMDS_MeshNode* n4,
506 const SMDS_MeshNode* n5,
507 const SMDS_MeshNode* n6,
511 SMESHDS_Mesh * meshDS = GetMesh()->GetMeshDS();
512 if(!myCreateQuadratic) {
514 return meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, id);
516 return meshDS->AddVolume(n1, n2, n3, n4, n5, n6);
519 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
520 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
521 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
523 const SMDS_MeshNode* n45 = GetMediumNode(n4,n5,force3d);
524 const SMDS_MeshNode* n56 = GetMediumNode(n5,n6,force3d);
525 const SMDS_MeshNode* n64 = GetMediumNode(n6,n4,force3d);
527 const SMDS_MeshNode* n14 = GetMediumNode(n1,n4,force3d);
528 const SMDS_MeshNode* n25 = GetMediumNode(n2,n5,force3d);
529 const SMDS_MeshNode* n36 = GetMediumNode(n3,n6,force3d);
532 return meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6,
533 n12, n23, n31, n45, n56, n64, n14, n25, n36, id);
535 return meshDS->AddVolume(n1, n2, n3, n4, n5, n6,
536 n12, n23, n31, n45, n56, n64, n14, n25, n36);
540 //=======================================================================
541 //function : AddVolume
543 //=======================================================================
545 * Special function for creation quadratic volume
547 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
548 const SMDS_MeshNode* n2,
549 const SMDS_MeshNode* n3,
550 const SMDS_MeshNode* n4,
554 SMESHDS_Mesh * meshDS = GetMesh()->GetMeshDS();
555 if(!myCreateQuadratic) {
557 return meshDS->AddVolumeWithID(n1, n2, n3, n4, id);
559 return meshDS->AddVolume(n1, n2, n3, n4);
562 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
563 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
564 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
566 const SMDS_MeshNode* n14 = GetMediumNode(n1,n4,force3d);
567 const SMDS_MeshNode* n24 = GetMediumNode(n2,n4,force3d);
568 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
571 return meshDS->AddVolumeWithID(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34, id);
573 return meshDS->AddVolume(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34);
577 //=======================================================================
578 //function : AddVolume
580 //=======================================================================
582 * Special function for creation quadratic volume
584 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
585 const SMDS_MeshNode* n2,
586 const SMDS_MeshNode* n3,
587 const SMDS_MeshNode* n4,
588 const SMDS_MeshNode* n5,
589 const SMDS_MeshNode* n6,
590 const SMDS_MeshNode* n7,
591 const SMDS_MeshNode* n8,
595 SMESHDS_Mesh * meshDS = GetMesh()->GetMeshDS();
596 if(!myCreateQuadratic) {
598 return meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8, id);
600 return meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8);
603 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
604 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
605 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
606 const SMDS_MeshNode* n41 = GetMediumNode(n4,n1,force3d);
608 const SMDS_MeshNode* n56 = GetMediumNode(n5,n6,force3d);
609 const SMDS_MeshNode* n67 = GetMediumNode(n6,n7,force3d);
610 const SMDS_MeshNode* n78 = GetMediumNode(n7,n8,force3d);
611 const SMDS_MeshNode* n85 = GetMediumNode(n8,n5,force3d);
613 const SMDS_MeshNode* n15 = GetMediumNode(n1,n5,force3d);
614 const SMDS_MeshNode* n26 = GetMediumNode(n2,n6,force3d);
615 const SMDS_MeshNode* n37 = GetMediumNode(n3,n7,force3d);
616 const SMDS_MeshNode* n48 = GetMediumNode(n4,n8,force3d);
619 return meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8,
620 n12, n23, n34, n41, n56, n67,
621 n78, n85, n15, n26, n37, n48, id);
623 return meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8,
624 n12, n23, n34, n41, n56, n67,
625 n78, n85, n15, n26, n37, n48);