1 // Copyright (C) 2007-2022 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, or (at your option) any later version.
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 : StdMeshers_QuadFromMedialAxis_1D2D.cxx
23 // Created : Wed Jun 3 17:33:45 2015
24 // Author : Edward AGAPOV (eap)
26 #include "StdMeshers_QuadFromMedialAxis_1D2D.hxx"
28 #include "SMESHDS_Mesh.hxx"
29 #include "SMESH_Block.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_MAT2d.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_MeshEditor.hxx"
34 #include "SMESH_MesherHelper.hxx"
35 #include "SMESH_ProxyMesh.hxx"
36 #include "SMESH_subMesh.hxx"
37 #include "SMESH_subMeshEventListener.hxx"
38 #include "StdMeshers_FaceSide.hxx"
39 #include "StdMeshers_LayerDistribution.hxx"
40 #include "StdMeshers_NumberOfLayers.hxx"
41 #include "StdMeshers_Regular_1D.hxx"
42 #include "StdMeshers_ViscousLayers2D.hxx"
44 #include <BRepAdaptor_Curve.hxx>
45 #include <BRepBuilderAPI_MakeEdge.hxx>
46 #include <BRepTools.hxx>
47 #include <BRep_Tool.hxx>
48 #include <GeomAPI_Interpolate.hxx>
49 #include <Geom_Surface.hxx>
50 #include <Precision.hxx>
51 #include <TColgp_HArray1OfPnt.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopLoc_Location.hxx>
55 #include <TopTools_MapOfShape.hxx>
57 #include <TopoDS_Edge.hxx>
58 #include <TopoDS_Face.hxx>
59 #include <TopoDS_Vertex.hxx>
67 //================================================================================
71 class StdMeshers_QuadFromMedialAxis_1D2D::Algo1D : public StdMeshers_Regular_1D
74 Algo1D(SMESH_Gen* gen):
75 StdMeshers_Regular_1D( gen->GetANewId(), gen )
78 void SetSegmentLength( double len )
80 SMESH_Algo::_usedHypList.clear();
81 _value[ BEG_LENGTH_IND ] = len;
82 _value[ PRECISION_IND ] = 1e-7;
83 _hypType = LOCAL_LENGTH;
85 void SetRadialDistribution( const SMESHDS_Hypothesis* hyp )
87 SMESH_Algo::_usedHypList.clear();
91 if ( const StdMeshers_NumberOfLayers* nl =
92 dynamic_cast< const StdMeshers_NumberOfLayers* >( hyp ))
94 _ivalue[ NB_SEGMENTS_IND ] = nl->GetNumberOfLayers();
95 _ivalue[ DISTR_TYPE_IND ] = 0;
96 _hypType = NB_SEGMENTS;
98 if ( const StdMeshers_LayerDistribution* ld =
99 dynamic_cast< const StdMeshers_LayerDistribution* >( hyp ))
101 if ( SMESH_Hypothesis* h = ld->GetLayerDistribution() )
103 SMESH_Algo::_usedHypList.clear();
104 SMESH_Algo::_usedHypList.push_back( h );
108 void ComputeDistribution(SMESH_MesherHelper& theHelper,
109 const gp_Pnt& thePnt1,
110 const gp_Pnt& thePnt2,
111 list< double >& theParams)
113 SMESH_Mesh& mesh = *theHelper.GetMesh();
114 TopoDS_Edge edge = BRepBuilderAPI_MakeEdge( thePnt1, thePnt2 );
116 SMESH_Hypothesis::Hypothesis_Status aStatus;
117 CheckHypothesis( mesh, edge, aStatus );
120 BRepAdaptor_Curve C3D(edge);
121 double f = C3D.FirstParameter(), l = C3D.LastParameter(), len = thePnt1.Distance( thePnt2 );
122 if ( !StdMeshers_Regular_1D::computeInternalParameters( mesh, C3D, len, f, l, theParams, false))
124 for ( size_t i = 1; i < 15; ++i )
125 theParams.push_back( i/15. ); // ????
129 for (list<double>::iterator itU = theParams.begin(); itU != theParams.end(); ++itU )
133 virtual const list <const SMESHDS_Hypothesis *> &
134 GetUsedHypothesis(SMESH_Mesh &, const TopoDS_Shape &, const bool)
136 return SMESH_Algo::_usedHypList;
138 virtual bool CheckHypothesis(SMESH_Mesh& aMesh,
139 const TopoDS_Shape& aShape,
140 SMESH_Hypothesis::Hypothesis_Status& aStatus)
142 if ( !SMESH_Algo::_usedHypList.empty() )
143 return StdMeshers_Regular_1D::CheckHypothesis( aMesh, aShape, aStatus );
148 //================================================================================
150 * \brief Constructor sets algo features
152 //================================================================================
154 StdMeshers_QuadFromMedialAxis_1D2D::StdMeshers_QuadFromMedialAxis_1D2D(int hypId,
156 : StdMeshers_Quadrangle_2D(hypId, gen),
159 _name = "QuadFromMedialAxis_1D2D";
160 _shapeType = (1 << TopAbs_FACE);
161 _onlyUnaryInput = true; // FACE by FACE so far
162 _requireDiscreteBoundary = false; // make 1D by myself
163 _supportSubmeshes = true; // make 1D by myself
164 _neededLowerHyps[ 1 ] = true; // suppress warning on hiding a global 1D algo
165 _neededLowerHyps[ 2 ] = true; // suppress warning on hiding a global 2D algo
166 _compatibleHypothesis.clear();
167 _compatibleHypothesis.push_back("ViscousLayers2D");
168 _compatibleHypothesis.push_back("LayerDistribution2D");
169 _compatibleHypothesis.push_back("NumberOfLayers2D");
172 //================================================================================
176 //================================================================================
178 StdMeshers_QuadFromMedialAxis_1D2D::~StdMeshers_QuadFromMedialAxis_1D2D()
184 //================================================================================
186 * \brief Check if needed hypotheses are present
188 //================================================================================
190 bool StdMeshers_QuadFromMedialAxis_1D2D::CheckHypothesis(SMESH_Mesh& aMesh,
191 const TopoDS_Shape& aShape,
192 Hypothesis_Status& aStatus)
196 // get one main optional hypothesis
197 const list <const SMESHDS_Hypothesis * >& hyps = GetUsedHypothesis(aMesh, aShape);
198 _hyp2D = hyps.empty() ? 0 : hyps.front();
200 return true; // does not require hypothesis
205 typedef map< const SMDS_MeshNode*, list< const SMDS_MeshNode* > > TMergeMap;
207 //================================================================================
209 * \brief Sinuous face
213 FaceQuadStruct::Ptr _quad;
214 vector< TopoDS_Edge > _edges;
215 vector< TopoDS_Edge > _sinuSide[2], _shortSide[2];
216 vector< TopoDS_Edge > _sinuEdges;
217 vector< Handle(Geom_Curve) > _sinuCurves;
219 list< int > _nbEdgesInWire;
220 TMergeMap _nodesToMerge;
222 SinuousFace( const TopoDS_Face& f ): _quad( new FaceQuadStruct )
224 list< TopoDS_Edge > edges;
225 _nbWires = SMESH_Block::GetOrderedEdges (f, edges, _nbEdgesInWire);
226 _edges.assign( edges.begin(), edges.end() );
228 _quad->side.resize( 4 );
231 const TopoDS_Face& Face() const { return _quad->face; }
232 bool IsRing() const { return _shortSide[0].empty() && !_sinuSide[0].empty(); }
235 //================================================================================
237 * \brief Temporary mesh
239 struct TmpMesh : public SMESH_Mesh
243 _meshDS = new SMESHDS_Mesh(/*id=*/0, /*isEmbeddedMode=*/true);
247 //================================================================================
249 * \brief Event listener which removes mesh from EDGEs when 2D hyps change
251 struct EdgeCleaner : public SMESH_subMeshEventListener
255 SMESH_subMeshEventListener( /*isDeletable=*/true,
256 "StdMeshers_QuadFromMedialAxis_1D2D::EdgeCleaner")
260 virtual void ProcessEvent(const int event,
262 SMESH_subMesh* faceSubMesh,
263 SMESH_subMeshEventListenerData* /*data*/,
264 const SMESH_Hypothesis* /*hyp*/)
266 if ( eventType == SMESH_subMesh::ALGO_EVENT )
268 _prevAlgoEvent = event;
271 // SMESH_subMesh::COMPUTE_EVENT
272 if ( _prevAlgoEvent == SMESH_subMesh::REMOVE_HYP ||
273 _prevAlgoEvent == SMESH_subMesh::REMOVE_ALGO ||
274 _prevAlgoEvent == SMESH_subMesh::MODIF_HYP )
276 SMESH_subMeshIteratorPtr smIt = faceSubMesh->getDependsOnIterator(/*includeSelf=*/false);
277 while ( smIt->more() )
278 smIt->next()->ComputeStateEngine( SMESH_subMesh::CLEAN );
284 //================================================================================
286 * \brief Return a member of a std::pair
288 //================================================================================
290 template< typename T >
291 T& get( std::pair< T, T >& thePair, bool is2nd )
293 return is2nd ? thePair.second : thePair.first;
296 //================================================================================
298 * \brief Select two EDGEs from a map, either mapped to least values or to max values
300 //================================================================================
302 // template< class TVal2EdgesMap >
303 // void getTwo( bool least,
304 // TVal2EdgesMap& map,
305 // vector<TopoDS_Edge>& twoEdges,
306 // vector<TopoDS_Edge>& otherEdges)
309 // otherEdges.clear();
312 // TVal2EdgesMap::iterator i = map.begin();
313 // twoEdges.push_back( i->second );
314 // twoEdges.push_back( ++i->second );
315 // for ( ; i != map.end(); ++i )
316 // otherEdges.push_back( i->second );
320 // TVal2EdgesMap::reverse_iterator i = map.rbegin();
321 // twoEdges.push_back( i->second );
322 // twoEdges.push_back( ++i->second );
323 // for ( ; i != map.rend(); ++i )
324 // otherEdges.push_back( i->second );
327 // if ( TopExp::CommonVertex( twoEdges[0], twoEdges[1], v ))
329 // twoEdges.clear(); // two EDGEs must not be connected
330 // otherEdges.clear();
334 //================================================================================
336 * \brief Finds out a minimal segment length given EDGEs will be divided into.
337 * This length is further used to discretize the Medial Axis
339 //================================================================================
341 double getMinSegLen(SMESH_MesherHelper& theHelper,
342 const vector<TopoDS_Edge>& theEdges)
345 SMESH_Mesh* mesh = theHelper.GetMesh();
347 vector< SMESH_Algo* > algos( theEdges.size() );
348 for ( size_t i = 0; i < theEdges.size(); ++i )
350 SMESH_subMesh* sm = mesh->GetSubMesh( theEdges[i] );
351 algos[i] = sm->GetAlgo();
354 int nbSegDflt = mesh->GetGen() ? mesh->GetGen()->GetDefaultNbSegments() : 15;
355 double minSegLen = Precision::Infinite();
357 for ( size_t i = 0; i < theEdges.size(); ++i )
359 SMESH_subMesh* sm = mesh->GetSubMesh( theEdges[i] );
360 if ( SMESH_Algo::IsStraight( theEdges[i], /*degenResult=*/true ))
363 size_t iOpp = ( theEdges.size() == 4 ? (i+2)%4 : i );
364 SMESH_Algo* algo = sm->GetAlgo();
365 if ( !algo ) algo = algos[ iOpp ];
367 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_MISSING;
370 if ( !algo->CheckHypothesis( *mesh, theEdges[i], status ))
371 algo->CheckHypothesis( *mesh, theEdges[iOpp], status );
374 if ( status != SMESH_Hypothesis::HYP_OK )
376 minSegLen = Min( minSegLen, SMESH_Algo::EdgeLength( theEdges[i] ) / nbSegDflt );
381 tmpMesh.ShapeToMesh( TopoDS_Shape());
382 tmpMesh.ShapeToMesh( theEdges[i] );
384 if ( !mesh->GetGen() ) continue; // tmp mesh
385 mesh->GetGen()->Compute( tmpMesh, theEdges[i], SMESH_Gen::SHAPE_ONLY_UPWARD ); // make nodes on VERTEXes
386 if ( !algo->Compute( tmpMesh, theEdges[i] ))
392 SMDS_EdgeIteratorPtr segIt = tmpMesh.GetMeshDS()->edgesIterator();
393 while ( segIt->more() )
395 const SMDS_MeshElement* seg = segIt->next();
396 double len = SMESH_TNodeXYZ( seg->GetNode(0) ).Distance( seg->GetNode(1) );
397 minSegLen = Min( minSegLen, len );
401 if ( Precision::IsInfinite( minSegLen ))
402 minSegLen = mesh->GetShapeDiagonalSize() / nbSegDflt;
407 //================================================================================
409 * \brief Returns EDGEs located between two VERTEXes at which given MA branches end
410 * \param [in] br1 - one MA branch
411 * \param [in] br2 - one more MA branch
412 * \param [in] allEdges - all EDGEs of a FACE
413 * \param [out] shortEdges - the found EDGEs
414 * \return bool - is OK or not
416 //================================================================================
418 bool getConnectedEdges( const SMESH_MAT2d::Branch* br1,
419 const SMESH_MAT2d::Branch* br2,
420 const vector<TopoDS_Edge>& allEdges,
421 vector<TopoDS_Edge>& shortEdges)
423 vector< size_t > edgeIDs[4];
424 br1->getGeomEdges( edgeIDs[0], edgeIDs[1] );
425 br2->getGeomEdges( edgeIDs[2], edgeIDs[3] );
427 // EDGEs returned by a Branch form a connected chain with a VERTEX where
428 // the Branch ends at the chain middle. One of end EDGEs of the chain is common
429 // with either end EDGE of the chain of the other Branch, or the chains are connected
430 // at a common VERTEX;
432 // Get indices of end EDGEs of the branches
433 bool vAtStart1 = ( br1->getEnd(0)->_type == SMESH_MAT2d::BE_ON_VERTEX );
434 bool vAtStart2 = ( br2->getEnd(0)->_type == SMESH_MAT2d::BE_ON_VERTEX );
436 vAtStart1 ? edgeIDs[0].back() : edgeIDs[0][0],
437 vAtStart1 ? edgeIDs[1].back() : edgeIDs[1][0],
438 vAtStart2 ? edgeIDs[2].back() : edgeIDs[2][0],
439 vAtStart2 ? edgeIDs[3].back() : edgeIDs[3][0]
442 set< size_t > connectedIDs;
443 TopoDS_Vertex vCommon;
444 // look for the same EDGEs
445 for ( int i = 0; i < 2; ++i )
446 for ( int j = 2; j < 4; ++j )
447 if ( iEnd[i] == iEnd[j] )
449 connectedIDs.insert( edgeIDs[i].begin(), edgeIDs[i].end() );
450 connectedIDs.insert( edgeIDs[j].begin(), edgeIDs[j].end() );
453 if ( connectedIDs.empty() )
454 // look for connected EDGEs
455 for ( int i = 0; i < 2; ++i )
456 for ( int j = 2; j < 4; ++j )
457 if ( TopExp::CommonVertex( allEdges[ iEnd[i]], allEdges[ iEnd[j]], vCommon ))
459 connectedIDs.insert( edgeIDs[i].begin(), edgeIDs[i].end() );
460 connectedIDs.insert( edgeIDs[j].begin(), edgeIDs[j].end() );
463 if ( connectedIDs.empty() || // nothing
464 allEdges.size() - connectedIDs.size() < 2 ) // too many
467 // set shortEdges in the order as in allEdges
468 if ( connectedIDs.count( 0 ) &&
469 connectedIDs.count( allEdges.size()-1 ))
471 size_t iE = allEdges.size()-1;
472 while ( connectedIDs.count( iE-1 ))
474 for ( size_t i = 0; i < connectedIDs.size(); ++i )
476 shortEdges.push_back( allEdges[ iE ]);
477 iE = ( iE + 1 ) % allEdges.size();
482 set< size_t >::iterator i = connectedIDs.begin();
483 for ( ; i != connectedIDs.end(); ++i )
484 shortEdges.push_back( allEdges[ *i ]);
489 //================================================================================
491 * \brief Find EDGEs to discretize using projection from MA
492 * \param [in,out] theSinuFace - the FACE to be meshed
493 * \return bool - OK or not
495 * It separates all EDGEs into four sides of a quadrangle connected in the order:
496 * theSinuEdges[0], theShortEdges[0], theSinuEdges[1], theShortEdges[1]
498 //================================================================================
500 bool getSinuousEdges( SMESH_MesherHelper& theHelper,
501 SinuousFace& theSinuFace)
503 vector<TopoDS_Edge> * theSinuEdges = & theSinuFace._sinuSide [0];
504 vector<TopoDS_Edge> * theShortEdges = & theSinuFace._shortSide[0];
505 theSinuEdges[0].clear();
506 theSinuEdges[1].clear();
507 theShortEdges[0].clear();
508 theShortEdges[1].clear();
510 vector<TopoDS_Edge> & allEdges = theSinuFace._edges;
511 const size_t nbEdges = allEdges.size();
512 if ( nbEdges < 4 && theSinuFace._nbWires == 1 )
515 if ( theSinuFace._nbWires == 2 ) // ring
517 size_t nbOutEdges = theSinuFace._nbEdgesInWire.front();
518 theSinuEdges[0].assign ( allEdges.begin(), allEdges.begin() + nbOutEdges );
519 theSinuEdges[1].assign ( allEdges.begin() + nbOutEdges, allEdges.end() );
520 theSinuFace._sinuEdges = allEdges;
523 if ( theSinuFace._nbWires > 2 )
526 // create MedialAxis to find short edges by analyzing MA branches
527 double minSegLen = getMinSegLen( theHelper, allEdges );
528 SMESH_MAT2d::MedialAxis ma( theSinuFace.Face(), allEdges, minSegLen * 3 );
530 // in an initial request case, theFace represents a part of a river with almost parallel banks
531 // so there should be two branch points
532 using SMESH_MAT2d::BranchEnd;
533 using SMESH_MAT2d::Branch;
534 const vector< const BranchEnd* >& braPoints = ma.getBranchPoints();
535 if ( braPoints.size() < 2 )
537 TopTools_MapOfShape shortMap;
538 size_t nbBranchPoints = 0;
539 for ( size_t i = 0; i < braPoints.size(); ++i )
541 vector< const Branch* > vertBranches; // branches with an end on VERTEX
542 for ( size_t ib = 0; ib < braPoints[i]->_branches.size(); ++ib )
544 const Branch* branch = braPoints[i]->_branches[ ib ];
545 if ( branch->hasEndOfType( SMESH_MAT2d::BE_ON_VERTEX ))
546 vertBranches.push_back( branch );
548 if ( vertBranches.size() != 2 || braPoints[i]->_branches.size() != 3)
551 // get common EDGEs of two branches
552 if ( !getConnectedEdges( vertBranches[0], vertBranches[1],
553 allEdges, theShortEdges[ nbBranchPoints > 0 ] ))
556 for ( size_t iS = 0; iS < theShortEdges[ nbBranchPoints > 0 ].size(); ++iS )
557 shortMap.Add( theShortEdges[ nbBranchPoints > 0 ][ iS ]);
562 if ( nbBranchPoints != 2 )
565 // add to theSinuEdges all edges that are not theShortEdges
566 vector< vector<TopoDS_Edge> > sinuEdges(1);
567 TopoDS_Vertex vCommon;
568 for ( size_t i = 0; i < allEdges.size(); ++i )
570 if ( !shortMap.Contains( allEdges[i] ))
572 if ( !sinuEdges.back().empty() )
573 if ( !TopExp::CommonVertex( sinuEdges.back().back(), allEdges[ i ], vCommon ))
574 sinuEdges.resize( sinuEdges.size() + 1 );
576 sinuEdges.back().push_back( allEdges[i] );
579 if ( sinuEdges.size() == 3 )
581 if ( !TopExp::CommonVertex( sinuEdges.back().back(), sinuEdges[0][0], vCommon ))
583 vector<TopoDS_Edge>& last = sinuEdges.back();
584 last.insert( last.end(), sinuEdges[0].begin(), sinuEdges[0].end() );
585 sinuEdges[0].swap( last );
586 sinuEdges.resize( 2 );
588 if ( sinuEdges.size() != 2 )
591 theSinuEdges[0].swap( sinuEdges[0] );
592 theSinuEdges[1].swap( sinuEdges[1] );
594 if ( !TopExp::CommonVertex( theSinuEdges[0].back(), theShortEdges[0][0], vCommon ) ||
595 !vCommon.IsSame( theHelper.IthVertex( 1, theSinuEdges[0].back() )))
596 theShortEdges[0].swap( theShortEdges[1] );
598 theSinuFace._sinuEdges = theSinuEdges[0];
599 theSinuFace._sinuEdges.insert( theSinuFace._sinuEdges.end(),
600 theSinuEdges[1].begin(), theSinuEdges[1].end() );
602 return ( theShortEdges[0].size() > 0 && theShortEdges[1].size() > 0 &&
603 theSinuEdges [0].size() > 0 && theSinuEdges [1].size() > 0 );
605 // the sinuous EDGEs can be composite and C0 continuous,
606 // therefore we use a complex criterion to find TWO short non-sinuous EDGEs
607 // and the rest EDGEs will be treated as sinuous.
608 // A short edge should have the following features:
611 // c) with convex corners at ends
612 // d) far from the other short EDGE
614 // vector< double > isStraightEdge( nbEdges, 0 ); // criterion value
616 // // a0) evaluate continuity
617 // const double contiWgt = 0.5; // weight of continuity in the criterion
618 // multimap< int, TopoDS_Edge > continuity;
619 // for ( size_t i = 0; i < nbEdges; ++I )
621 // BRepAdaptor_Curve curve( allEdges[i] );
622 // GeomAbs_Shape C = GeomAbs_CN;
624 // C = curve.Continuity(); // C0, G1, C1, G2, C2, C3, CN
625 // catch ( Standard_Failure ) {}
626 // continuity.insert( make_pair( C, allEdges[i] ));
627 // isStraight[i] += double( C ) / double( CN ) * contiWgt;
630 // // try to choose by continuity
631 // int mostStraight = (int) continuity.rbegin()->first;
632 // int lessStraight = (int) continuity.begin()->first;
633 // if ( mostStraight != lessStraight )
635 // int nbStraight = continuity.count( mostStraight );
636 // if ( nbStraight == 2 )
638 // getTwo( /*least=*/false, continuity, theShortEdges, theSinuEdges );
640 // else if ( nbStraight == 3 && nbEdges == 4 )
642 // theSinuEdges.push_back( continuity.begin()->second );
643 // vector<TopoDS_Edge>::iterator it =
644 // std::find( allEdges.begin(), allEdges.end(), theSinuEdges[0] );
645 // int i = std::distance( allEdges.begin(), it );
646 // theSinuEdges .push_back( allEdges[( i+2 )%4 ]);
647 // theShortEdges.push_back( allEdges[( i+1 )%4 ]);
648 // theShortEdges.push_back( allEdges[( i+3 )%4 ]);
650 // if ( theShortEdges.size() == 2 )
654 // // a) curvature; evaluate aspect ratio
656 // const double curvWgt = 0.5;
657 // for ( size_t i = 0; i < nbEdges; ++I )
659 // BRepAdaptor_Curve curve( allEdges[i] );
660 // double curvature = 1;
661 // if ( !curve.IsClosed() )
663 // const double f = curve.FirstParameter(), l = curve.LastParameter();
664 // gp_Pnt pf = curve.Value( f ), pl = curve.Value( l );
665 // gp_Lin line( pf, pl.XYZ() - pf.XYZ() );
666 // double distMax = 0;
667 // for ( double u = f; u < l; u += (l-f)/30. )
668 // distMax = Max( distMax, line.SquareDistance( curve.Value( u )));
669 // curvature = Sqrt( distMax ) / ( pf.Distance( pl ));
671 // isStraight[i] += curvWgt / ( curvature + 1e-20 );
676 // const double lenWgt = 0.5;
677 // for ( size_t i = 0; i < nbEdges; ++I )
679 // double length = SMESH_Algo::Length( allEdges[i] );
681 // isStraight[i] += lenWgt / length;
684 // // c) with convex corners at ends
686 // const double cornerWgt = 0.25;
687 // for ( size_t i = 0; i < nbEdges; ++I )
689 // double convex = 0;
690 // int iPrev = SMESH_MesherHelper::WrapIndex( int(i)-1, nbEdges );
691 // int iNext = SMESH_MesherHelper::WrapIndex( int(i)+1, nbEdges );
692 // TopoDS_Vertex v = helper.IthVertex( 0, allEdges[i] );
693 // double angle = SMESH_MesherHelper::GetAngle( allEdges[iPrev], allEdges[i], theFace, v );
694 // if ( angle < M_PI ) // [-PI; PI]
695 // convex += ( angle + M_PI ) / M_PI / M_PI;
696 // v = helper.IthVertex( 1, allEdges[i] );
697 // angle = SMESH_MesherHelper::GetAngle( allEdges[iNext], allEdges[i], theFace, v );
698 // if ( angle < M_PI ) // [-PI; PI]
699 // convex += ( angle + M_PI ) / M_PI / M_PI;
700 // isStraight[i] += cornerWgt * convex;
705 //================================================================================
707 * \brief Creates an EDGE from a sole branch of MA
709 //================================================================================
711 TopoDS_Edge makeEdgeFromMA( SMESH_MesherHelper& theHelper,
712 const SMESH_MAT2d::MedialAxis& theMA,
713 const double theMinSegLen)
715 if ( theMA.nbBranches() != 1 )
716 return TopoDS_Edge();
719 theMA.getPoints( theMA.getBranch(0), uv );
721 return TopoDS_Edge();
723 TopoDS_Face face = TopoDS::Face( theHelper.GetSubShape() );
724 Handle(Geom_Surface) surface = BRep_Tool::Surface( face );
726 vector< gp_Pnt > pnt;
727 pnt.reserve( uv.size() * 2 );
728 pnt.push_back( surface->Value( uv[0].X(), uv[0].Y() ));
729 for ( size_t i = 1; i < uv.size(); ++i )
731 gp_Pnt p = surface->Value( uv[i].X(), uv[i].Y() );
732 int nbDiv = int( p.Distance( pnt.back() ) / theMinSegLen );
733 for ( int iD = 1; iD < nbDiv; ++iD )
735 double R = iD / double( nbDiv );
736 gp_XY uvR = uv[i-1] * (1 - R) + uv[i] * R;
737 pnt.push_back( surface->Value( uvR.X(), uvR.Y() ));
742 // cout << "from salome.geom import geomBuilder" << endl;
743 // cout << "geompy = geomBuilder.New()" << endl;
744 Handle(TColgp_HArray1OfPnt) points = new TColgp_HArray1OfPnt(1, pnt.size());
745 for ( size_t i = 0; i < pnt.size(); ++i )
748 points->SetValue( i+1, p );
749 // cout << "geompy.MakeVertex( "<< p.X()<<", " << p.Y()<<", " << p.Z()
750 // <<" theName = 'p_" << i << "')" << endl;
753 GeomAPI_Interpolate interpol( points, /*isClosed=*/false, gp::Resolution());
755 if ( !interpol.IsDone())
756 return TopoDS_Edge();
758 TopoDS_Edge branchEdge = BRepBuilderAPI_MakeEdge(interpol.Curve());
762 //================================================================================
764 * \brief Returns a type of shape, to which a hypothesis used to mesh a given edge is assigned
766 //================================================================================
768 TopAbs_ShapeEnum getHypShape( SMESH_Mesh* mesh, const TopoDS_Shape& edge )
770 TopAbs_ShapeEnum shapeType = TopAbs_SHAPE;
772 SMESH_subMesh* sm = mesh->GetSubMesh( edge );
773 SMESH_Algo* algo = sm->GetAlgo();
774 if ( !algo ) return shapeType;
776 const list <const SMESHDS_Hypothesis *> & hyps =
777 algo->GetUsedHypothesis( *mesh, edge, /*ignoreAuxiliary=*/true );
778 if ( hyps.empty() ) return shapeType;
780 TopoDS_Shape shapeOfHyp =
781 SMESH_MesherHelper::GetShapeOfHypothesis( hyps.front(), edge, mesh);
783 return SMESH_MesherHelper::GetGroupType( shapeOfHyp, /*woCompound=*/true);
786 //================================================================================
788 * \brief Discretize a sole branch of MA an returns parameters of divisions on MA
790 //================================================================================
792 bool divideMA( SMESH_MesherHelper& theHelper,
793 const SMESH_MAT2d::MedialAxis& theMA,
794 const SinuousFace& theSinuFace,
795 SMESH_Algo* the1dAlgo,
796 const double theMinSegLen,
797 vector<double>& theMAParams )
799 // Check if all EDGEs of one size are meshed, then MA discretization is not needed
800 SMESH_Mesh* mesh = theHelper.GetMesh();
801 size_t nbComputedEdges[2] = { 0, 0 };
802 for ( size_t iS = 0; iS < 2; ++iS )
803 for ( size_t i = 0; i < theSinuFace._sinuSide[iS].size(); ++i )
805 const TopoDS_Edge& sinuEdge = theSinuFace._sinuSide[iS][i];
806 SMESH_subMesh* sm = mesh->GetSubMesh( sinuEdge );
807 bool isComputed = ( !sm->IsEmpty() );
810 TopAbs_ShapeEnum shape = getHypShape( mesh, sinuEdge );
811 if ( shape == TopAbs_SHAPE || shape <= TopAbs_FACE )
813 // EDGE computed using global hypothesis -> clear it
814 bool hasComputedFace = false;
815 PShapeIteratorPtr faceIt = theHelper.GetAncestors( sinuEdge, *mesh, TopAbs_FACE );
816 while ( const TopoDS_Shape* face = faceIt->next() )
817 if (( !face->IsSame( theSinuFace.Face() )) &&
818 ( hasComputedFace = !mesh->GetSubMesh( *face )->IsEmpty() ))
820 if ( !hasComputedFace )
822 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
827 nbComputedEdges[ iS ] += isComputed;
829 if ( nbComputedEdges[0] == theSinuFace._sinuSide[0].size() ||
830 nbComputedEdges[1] == theSinuFace._sinuSide[1].size() )
831 return true; // discretization is not needed
834 TopoDS_Edge branchEdge = makeEdgeFromMA( theHelper, theMA, theMinSegLen );
835 if ( branchEdge.IsNull() )
838 // const char* file = "/misc/dn25/salome/eap/salome/misc/tmp/MAedge.brep";
839 // BRepTools::Write( branchEdge, file);
840 // cout << "Write " << file << endl;
843 // Find 1D algo to mesh branchEdge
845 // look for a most local 1D hyp assigned to the FACE
846 int mostSimpleShape = -1, maxShape = TopAbs_EDGE;
848 for ( size_t i = 0; i < theSinuFace._sinuEdges.size(); ++i )
850 TopAbs_ShapeEnum shapeType = getHypShape( mesh, theSinuFace._sinuEdges[i] );
851 if ( mostSimpleShape < shapeType && shapeType < maxShape )
853 edge = theSinuFace._sinuEdges[i];
854 mostSimpleShape = shapeType;
858 SMESH_Algo* algo = the1dAlgo;
859 if ( mostSimpleShape > -1 )
861 algo = mesh->GetSubMesh( edge )->GetAlgo();
862 SMESH_Hypothesis::Hypothesis_Status status;
863 if ( !algo->CheckHypothesis( *mesh, edge, status ))
868 tmpMesh.ShapeToMesh( branchEdge );
870 mesh->GetGen()->Compute( tmpMesh, branchEdge, SMESH_Gen::SHAPE_ONLY_UPWARD ); // make nodes on VERTEXes
871 if ( !algo->Compute( tmpMesh, branchEdge ))
877 return SMESH_Algo::GetNodeParamOnEdge( tmpMesh.GetMeshDS(), branchEdge, theMAParams );
880 //================================================================================
882 * \brief Select division parameters on MA and make them coincide at ends with
883 * projections of VERTEXes to MA for a given pair of opposite EDGEs
884 * \param [in] theEdgePairInd - index of the EDGE pair
885 * \param [in] theDivPoints - the BranchPoint's dividing MA into parts each
886 * corresponding to a unique pair of opposite EDGEs
887 * \param [in] theMAParams - the MA division parameters
888 * \param [out] theSelectedMAParams - the selected MA parameters
889 * \return bool - is OK
891 //================================================================================
893 bool getParamsForEdgePair( const size_t theEdgePairInd,
894 const vector< SMESH_MAT2d::BranchPoint >& theDivPoints,
895 const vector<double>& theMAParams,
896 vector<double>& theSelectedMAParams)
898 if ( theDivPoints.empty() )
900 theSelectedMAParams = theMAParams;
903 if ( theEdgePairInd > theDivPoints.size() || theMAParams.empty() )
906 // find a range of params to copy
910 if ( theEdgePairInd > 0 )
912 const SMESH_MAT2d::BranchPoint& bp = theDivPoints[ theEdgePairInd-1 ];
913 bp._branch->getParameter( bp, par1 );
914 while ( theMAParams[ iPar1 ] < par1 ) ++iPar1;
915 if ( par1 - theMAParams[ iPar1-1 ] < theMAParams[ iPar1 ] - par1 )
920 size_t iPar2 = theMAParams.size() - 1;
921 if ( theEdgePairInd < theDivPoints.size() )
923 const SMESH_MAT2d::BranchPoint& bp = theDivPoints[ theEdgePairInd ];
924 bp._branch->getParameter( bp, par2 );
926 while ( theMAParams[ iPar2 ] < par2 ) ++iPar2;
927 if ( par2 - theMAParams[ iPar2-1 ] < theMAParams[ iPar2 ] - par2 )
931 theSelectedMAParams.assign( theMAParams.begin() + iPar1,
932 theMAParams.begin() + iPar2 + 1 );
934 // adjust theSelectedMAParams to fit between par1 and par2
936 double d = par1 - theSelectedMAParams[0];
937 double f = ( par2 - par1 ) / ( theSelectedMAParams.back() - theSelectedMAParams[0] );
939 for ( size_t i = 0; i < theSelectedMAParams.size(); ++i )
941 theSelectedMAParams[i] += d;
942 theSelectedMAParams[i] = par1 + ( theSelectedMAParams[i] - par1 ) * f;
948 //--------------------------------------------------------------------------------
949 // node or node parameter on EDGE
952 const SMDS_MeshNode* _node;
954 size_t _edgeInd; // index in theSinuEdges vector
956 NodePoint(): _node(0), _u(0), _edgeInd(-1) {}
957 NodePoint(const SMDS_MeshNode* n, double u, size_t iEdge ): _node(n), _u(u), _edgeInd(iEdge) {}
958 NodePoint(double u, size_t iEdge) : _node(0), _u(u), _edgeInd(iEdge) {}
959 NodePoint(const SMESH_MAT2d::BoundaryPoint& p) : _node(0), _u(p._param), _edgeInd(p._edgeIndex) {}
960 gp_Pnt Point(const vector< Handle(Geom_Curve) >& curves) const
962 return _node ? SMESH_TNodeXYZ(_node) : curves[ _edgeInd ]->Value( _u );
965 typedef multimap< double, pair< NodePoint, NodePoint > > TMAPar2NPoints;
967 //================================================================================
969 * \brief Finds a VERTEX corresponding to a point on EDGE, which is also filled
970 * with a node on the VERTEX, present or created
971 * \param [in,out] theNodePnt - the node position on the EDGE
972 * \param [in] theSinuEdges - the sinuous EDGEs
973 * \param [in] theMeshDS - the mesh
974 * \return bool - true if the \a theBndPnt is on VERTEX
976 //================================================================================
978 bool findVertexAndNode( NodePoint& theNodePnt,
979 const vector<TopoDS_Edge>& theSinuEdges,
980 SMESHDS_Mesh* theMeshDS = 0,
981 size_t theEdgeIndPrev = 0,
982 size_t theEdgeIndNext = 0)
984 if ( theNodePnt._edgeInd >= theSinuEdges.size() )
988 BRep_Tool::Range( theSinuEdges[ theNodePnt._edgeInd ], f,l );
989 const double tol = 1e-3 * ( l - f );
992 if ( Abs( f - theNodePnt._u ) < tol )
993 V = SMESH_MesherHelper::IthVertex( 0, theSinuEdges[ theNodePnt._edgeInd ], /*CumOri=*/false);
994 else if ( Abs( l - theNodePnt._u ) < tol )
995 V = SMESH_MesherHelper::IthVertex( 1, theSinuEdges[ theNodePnt._edgeInd ], /*CumOri=*/false);
996 else if ( theEdgeIndPrev != theEdgeIndNext )
997 TopExp::CommonVertex( theSinuEdges[theEdgeIndPrev], theSinuEdges[theEdgeIndNext], V );
999 if ( !V.IsNull() && theMeshDS )
1001 theNodePnt._node = SMESH_Algo::VertexNode( V, theMeshDS );
1002 if ( !theNodePnt._node )
1004 gp_Pnt p = BRep_Tool::Pnt( V );
1005 theNodePnt._node = theMeshDS->AddNode( p.X(), p.Y(), p.Z() );
1006 theMeshDS->SetNodeOnVertex( theNodePnt._node, V );
1012 //================================================================================
1014 * \brief Add to the map of NodePoint's those on VERTEXes
1015 * \param [in,out] theHelper - the helper
1016 * \param [in] theMA - Medial Axis
1017 * \param [in] theMinSegLen - minimal segment length
1018 * \param [in] theDivPoints - projections of VERTEXes to MA
1019 * \param [in] theSinuEdges - the sinuous EDGEs
1020 * \param [in] theSideEdgeIDs - indices of sinuous EDGEs per side
1021 * \param [in] theIsEdgeComputed - is sinuous EDGE is meshed
1022 * \param [in,out] thePointsOnE - the map to fill
1023 * \param [out] theNodes2Merge - the map of nodes to merge
1025 //================================================================================
1027 bool projectVertices( SMESH_MesherHelper& theHelper,
1028 const SMESH_MAT2d::MedialAxis& theMA,
1029 vector< SMESH_MAT2d::BranchPoint >& theDivPoints,
1030 const vector< std::size_t > & theEdgeIDs1,
1031 const vector< std::size_t > & theEdgeIDs2,
1032 const vector< bool >& theIsEdgeComputed,
1033 TMAPar2NPoints & thePointsOnE,
1034 SinuousFace& theSinuFace)
1036 if ( theDivPoints.empty() )
1039 SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
1040 const vector< TopoDS_Edge >& theSinuEdges = theSinuFace._sinuEdges;
1041 const vector< Handle(Geom_Curve) >& theCurves = theSinuFace._sinuCurves;
1044 SMESH_MAT2d::BoundaryPoint bp[2]; // 2 sinuous sides
1045 const SMESH_MAT2d::Branch& branch = *theMA.getBranch(0);
1047 // add to thePointsOnE NodePoint's of ends of theSinuEdges
1048 if ( !branch.getBoundaryPoints( 0., bp[0], bp[1] ) ||
1049 !theMA.getBoundary().moveToClosestEdgeEnd( bp[0] )) return false;
1050 if ( !theSinuFace.IsRing() &&
1051 !theMA.getBoundary().moveToClosestEdgeEnd( bp[1] )) return false;
1052 NodePoint np0( bp[0] ), np1( bp[1] );
1053 findVertexAndNode( np0, theSinuEdges, meshDS );
1054 findVertexAndNode( np1, theSinuEdges, meshDS );
1055 thePointsOnE.insert( make_pair( -0.1, make_pair( np0, np1 )));
1057 if ( !theSinuFace.IsRing() )
1059 if ( !branch.getBoundaryPoints( 1., bp[0], bp[1] ) ||
1060 !theMA.getBoundary().moveToClosestEdgeEnd( bp[0] ) ||
1061 !theMA.getBoundary().moveToClosestEdgeEnd( bp[1] )) return false;
1062 NodePoint np0( bp[0] ), np1( bp[1] );
1063 findVertexAndNode( np0, theSinuEdges, meshDS );
1064 findVertexAndNode( np1, theSinuEdges, meshDS );
1065 thePointsOnE.insert( make_pair( 1.1, make_pair( np0, np1)));
1069 // project a VERTEX of outer sinuous side corresponding to branch(0.)
1070 // which is not included into theDivPoints
1071 if ( ! ( theDivPoints[0]._iEdge == 0 &&
1072 theDivPoints[0]._edgeParam == 0. )) // recursive call
1074 SMESH_MAT2d::BranchPoint brp( &branch, 0, 0. );
1075 vector< SMESH_MAT2d::BranchPoint > divPoint( 1, brp );
1076 vector< std::size_t > edgeIDs1(2), edgeIDs2(2);
1077 edgeIDs1[0] = theEdgeIDs1.back();
1078 edgeIDs1[1] = theEdgeIDs1[0];
1079 edgeIDs2[0] = theEdgeIDs2.back();
1080 edgeIDs2[1] = theEdgeIDs2[0];
1081 projectVertices( theHelper, theMA, divPoint, edgeIDs1, edgeIDs2,
1082 theIsEdgeComputed, thePointsOnE, theSinuFace );
1086 // project theDivPoints and keep projections to merge
1088 TMAPar2NPoints::iterator u2NP;
1089 vector< TMAPar2NPoints::iterator > projToMerge;
1090 for ( size_t i = 0; i < theDivPoints.size(); ++i )
1092 if ( !branch.getParameter( theDivPoints[i], uMA ))
1094 if ( !branch.getBoundaryPoints( theDivPoints[i], bp[0], bp[1] ))
1101 bool isVertex[2] = {
1102 findVertexAndNode( np[0], theSinuEdges, meshDS, theEdgeIDs1[i], theEdgeIDs1[i+1] ),
1103 findVertexAndNode( np[1], theSinuEdges, meshDS, theEdgeIDs2[i], theEdgeIDs2[i+1] )
1105 const size_t iVert = isVertex[0] ? 0 : 1; // side with a VERTEX
1106 const size_t iNode = 1 - iVert; // opposite (meshed?) side
1108 if ( isVertex[0] != isVertex[1] ) // try to find an opposite VERTEX
1110 theMA.getBoundary().moveToClosestEdgeEnd( bp[iNode] ); // EDGE -> VERTEX
1111 SMESH_MAT2d::BranchPoint brp;
1112 theMA.getBoundary().getBranchPoint( bp[iNode], brp ); // WIRE -> MA
1113 SMESH_MAT2d::BoundaryPoint bp2[2];
1114 branch.getBoundaryPoints( brp, bp2[0], bp2[1] ); // MA -> WIRE
1115 NodePoint np2[2] = { NodePoint( bp2[0]), NodePoint( bp2[1]) };
1116 findVertexAndNode( np2[0], theSinuEdges, meshDS );
1117 findVertexAndNode( np2[1], theSinuEdges, meshDS );
1118 if ( np2[ iVert ]._node == np[ iVert ]._node &&
1121 np[ iNode ] = np2[ iNode ];
1122 isVertex[ iNode ] = true;
1126 u2NP = thePointsOnE.insert( make_pair( uMA, make_pair( np[0], np[1])));
1128 if ( !isVertex[0] && !isVertex[1] ) return false; // error
1129 if ( isVertex[0] && isVertex[1] )
1132 // bool isOppComputed = theIsEdgeComputed[ np[ iNode ]._edgeInd ];
1133 // if ( isOppComputed )
1134 projToMerge.push_back( u2NP );
1137 // merge projections
1139 for ( size_t i = 0; i < projToMerge.size(); ++i )
1141 u2NP = projToMerge[i];
1142 const size_t iVert = get( u2NP->second, 0 )._node ? 0 : 1; // side with a VERTEX
1143 const size_t iNode = 1 - iVert; // opposite (meshed?) side
1145 // a VERTEX is projected on a meshed EDGE; there are two options:
1146 // 1) a projected point is joined with a closet node if a strip between this and neighbor
1147 // projection is WIDE enough; joining is done by creating a node coincident with the
1148 // existing node which will be merged together after all;
1149 // 2) a neighbor projection is merged with this one if it is TOO CLOSE; a node of deleted
1150 // projection is set to the BoundaryPoint of this projection
1152 // evaluate distance to neighbor projections
1153 const double rShort = 0.33;
1154 bool isShortPrev[2], isShortNext[2], isPrevCloser[2];
1155 TMAPar2NPoints::iterator u2NPPrev = u2NP, u2NPNext = u2NP;
1156 --u2NPPrev; ++u2NPNext;
1157 if ( u2NPNext == thePointsOnE.end() )
1158 u2NPNext = thePointsOnE.begin(); // hope theSinuFace.IsRing()
1159 for ( int iS = 0; iS < 2; ++iS ) // side with Vertex and side with Nodes
1161 NodePoint np = get( u2NP->second, iS );
1162 NodePoint npPrev = get( u2NPPrev->second, iS );
1163 NodePoint npNext = get( u2NPNext->second, iS );
1164 gp_Pnt p = np .Point( theCurves );
1165 gp_Pnt pPrev = npPrev.Point( theCurves );
1166 gp_Pnt pNext = npNext.Point( theCurves );
1167 double distPrev = p.Distance( pPrev );
1168 double distNext = p.Distance( pNext );
1169 double r = distPrev / ( distPrev + distNext );
1170 isShortPrev [iS] = ( r < rShort );
1171 isShortNext [iS] = (( 1 - r ) < rShort );
1172 isPrevCloser[iS] = (( r < 0.5 ) && ( theSinuFace.IsRing() || u2NPPrev->first > 0 ));
1175 TMAPar2NPoints::iterator u2NPClose;
1177 if (( isShortPrev[0] && isShortPrev[1] ) || // option 2) -> remove a too close projection
1178 ( isShortNext[0] && isShortNext[1] ))
1180 u2NPClose = isPrevCloser[0] ? u2NPPrev : u2NPNext;
1181 NodePoint& npProj = get( u2NP->second, iNode ); // NP of VERTEX projection
1182 NodePoint& npVert = get( u2NP->second, iVert ); // NP of VERTEX
1183 NodePoint npCloseN = get( u2NPClose->second, iNode ); // NP close to npProj
1184 NodePoint npCloseV = get( u2NPClose->second, iVert ); // NP close to npVert
1185 if ( !npCloseV._node || npCloseV._node == npVert._node )
1188 if ( i+1 < projToMerge.size() && u2NPClose == projToMerge[ i+1 ])
1190 thePointsOnE.erase( u2NPClose );
1195 // can't remove the neighbor projection as it is also from VERTEX -> option 1)
1198 // else: option 1) - wide enough -> "duplicate" existing node
1200 u2NPClose = isPrevCloser[ iNode ] ? u2NPPrev : u2NPNext;
1201 NodePoint& npProj = get( u2NP->second, iNode ); // NP of VERTEX projection
1202 NodePoint& npCloseN = get( u2NPClose->second, iNode ); // NP close to npProj
1205 //npProj._edgeInd = npCloseN._edgeInd;
1206 // npProj._u = npCloseN._u + 1e-3 * Abs( get( u2NPPrev->second, iNode )._u -
1207 // get( u2NPNext->second, iNode )._u );
1208 // gp_Pnt p = npProj.Point( theCurves );
1209 // npProj._node = meshDS->AddNode( p.X(), p.Y(), p.Z() );
1210 // meshDS->SetNodeOnEdge( npProj._node, theSinuEdges[ npProj._edgeInd ], npProj._u );
1212 //theNodes2Merge[ npCloseN._node ].push_back( npProj._node );
1216 // remove auxiliary NodePoint's of ends of theSinuEdges
1217 for ( u2NP = thePointsOnE.begin(); u2NP->first < 0; )
1218 thePointsOnE.erase( u2NP++ );
1219 thePointsOnE.erase( 1.1 );
1224 double getUOnEdgeByPoint( const size_t iEdge,
1225 const NodePoint* point,
1226 SinuousFace& sinuFace )
1228 if ( point->_edgeInd == iEdge )
1231 TopoDS_Vertex V0 = TopExp::FirstVertex( sinuFace._sinuEdges[ iEdge ]);
1232 TopoDS_Vertex V1 = TopExp::LastVertex ( sinuFace._sinuEdges[ iEdge ]);
1233 gp_Pnt p0 = BRep_Tool::Pnt( V0 );
1234 gp_Pnt p1 = BRep_Tool::Pnt( V1 );
1235 gp_Pnt p = point->Point( sinuFace._sinuCurves );
1238 BRep_Tool::Range( sinuFace._sinuEdges[ iEdge ], f,l );
1239 return p.SquareDistance( p0 ) < p.SquareDistance( p1 ) ? f : l;
1242 //================================================================================
1244 * \brief Move coincident nodes to make node params on EDGE unique
1245 * \param [in] theHelper - the helper
1246 * \param [in] thePointsOnE - nodes on two opposite river sides
1247 * \param [in] theSinuFace - the sinuous FACE
1248 * \param [out] theNodes2Merge - the map of nodes to merge
1250 //================================================================================
1252 void separateNodes( SMESH_MesherHelper& theHelper,
1253 const SMESH_MAT2d::MedialAxis& /*theMA*/,
1254 TMAPar2NPoints & thePointsOnE,
1255 SinuousFace& theSinuFace,
1256 const vector< bool >& theIsComputedEdge)
1258 if ( thePointsOnE.size() < 2 )
1261 SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
1262 const vector<TopoDS_Edge>& theSinuEdges = theSinuFace._sinuEdges;
1263 const vector< Handle(Geom_Curve) >& curves = theSinuFace._sinuCurves;
1265 //SMESH_MAT2d::BoundaryPoint bp[2];
1266 //const SMESH_MAT2d::Branch& branch = *theMA.getBranch(0);
1268 typedef TMAPar2NPoints::iterator TIterator;
1270 for ( int iSide = 0; iSide < 2; ++iSide ) // loop on two sinuous sides
1272 // get a tolerance to compare points
1273 double tol = Precision::Confusion();
1274 for ( size_t i = 0; i < theSinuFace._sinuSide[ iSide ].size(); ++i )
1275 tol = Max( tol , BRep_Tool::Tolerance( theSinuFace._sinuSide[ iSide ][ i ]));
1277 // find coincident points
1278 TIterator u2NP = thePointsOnE.begin();
1279 vector< TIterator > sameU2NP( 1, u2NP++ );
1280 while ( u2NP != thePointsOnE.end() )
1282 for ( ; u2NP != thePointsOnE.end(); ++u2NP )
1284 NodePoint& np1 = get( sameU2NP.back()->second, iSide );
1285 NodePoint& np2 = get( u2NP ->second, iSide );
1287 if (( !np1._node || !np2._node ) &&
1288 ( np1.Point( curves ).SquareDistance( np2.Point( curves )) < tol*tol ))
1290 sameU2NP.push_back( u2NP );
1292 else if ( sameU2NP.size() == 1 )
1294 sameU2NP[ 0 ] = u2NP;
1302 if ( sameU2NP.size() > 1 )
1304 // find an existing node on VERTEX among sameU2NP and get underlying EDGEs
1305 const SMDS_MeshNode* existingNode = 0;
1306 set< size_t > edgeInds;
1308 for ( size_t i = 0; i < sameU2NP.size(); ++i )
1310 np = &get( sameU2NP[i]->second, iSide );
1312 if ( !existingNode || np->_node->GetPosition()->GetDim() == 0 )
1313 existingNode = np->_node;
1314 edgeInds.insert( np->_edgeInd );
1316 list< const SMDS_MeshNode* >& mergeNodes = theSinuFace._nodesToMerge[ existingNode ];
1318 TIterator u2NPprev = sameU2NP.front();
1319 TIterator u2NPnext = sameU2NP.back() ;
1320 if ( u2NPprev->first < 0. ) ++u2NPprev;
1321 if ( u2NPnext->first > 1. ) --u2NPnext;
1323 set< size_t >::iterator edgeID = edgeInds.begin();
1324 for ( ; edgeID != edgeInds.end(); ++edgeID )
1326 // get U range on iEdge within which the equal points will be distributed
1328 np = &get( u2NPprev->second, iSide );
1329 u0 = getUOnEdgeByPoint( *edgeID, np, theSinuFace );
1331 np = &get( u2NPnext->second, iSide );
1332 u1 = getUOnEdgeByPoint( *edgeID, np, theSinuFace );
1336 if ( u2NPprev != thePointsOnE.begin() ) --u2NPprev;
1337 if ( u2NPnext != --thePointsOnE.end() ) ++u2NPnext;
1338 np = &get( u2NPprev->second, iSide );
1339 u0 = getUOnEdgeByPoint( *edgeID, np, theSinuFace );
1340 np = &get( u2NPnext->second, iSide );
1341 u1 = getUOnEdgeByPoint( *edgeID, np, theSinuFace );
1344 // distribute points and create nodes
1345 double du = ( u1 - u0 ) / ( sameU2NP.size() + 1 /*!existingNode*/ );
1347 for ( size_t i = 0; i < sameU2NP.size(); ++i )
1349 np = &get( sameU2NP[i]->second, iSide );
1350 if ( !np->_node && *edgeID == np->_edgeInd )
1354 gp_Pnt p = np->Point( curves );
1355 np->_node = meshDS->AddNode( p.X(), p.Y(), p.Z() );
1356 meshDS->SetNodeOnEdge( np->_node, theSinuEdges[ *edgeID ], np->_u );
1358 if ( theIsComputedEdge[ *edgeID ])
1359 mergeNodes.push_back( np->_node );
1364 sameU2NP.resize( 1 );
1365 u2NP = ++sameU2NP.back();
1366 sameU2NP[ 0 ] = u2NP;
1368 } // if ( sameU2NP.size() > 1 )
1369 } // while ( u2NP != thePointsOnE.end() )
1370 } // for ( int iSide = 0; iSide < 2; ++iSide )
1373 } // separateNodes()
1376 //================================================================================
1378 * \brief Find association of nodes existing on the sinuous sides of a ring
1380 * TMAPar2NPoints filled here is used in setQuadSides() only if theSinuFace.IsRing()
1381 * to find most distant nodes of the inner and the outer wires
1383 //================================================================================
1385 void assocNodes( SMESH_MesherHelper& theHelper,
1386 SinuousFace& theSinuFace,
1387 const SMESH_MAT2d::MedialAxis& theMA,
1388 TMAPar2NPoints & thePointsOnE )
1390 SMESH_Mesh* mesh = theHelper.GetMesh();
1391 SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
1393 list< TopoDS_Edge > ee1( theSinuFace._sinuSide [0].begin(), theSinuFace._sinuSide [0].end() );
1394 list< TopoDS_Edge > ee2( theSinuFace._sinuSide [1].begin(), theSinuFace._sinuSide [1].end() );
1395 StdMeshers_FaceSide sideOut( theSinuFace.Face(), ee1, mesh, true, true, &theHelper );
1396 StdMeshers_FaceSide sideIn ( theSinuFace.Face(), ee2, mesh, true, true, &theHelper );
1397 const UVPtStructVec& uvsOut = sideOut.GetUVPtStruct();
1398 const UVPtStructVec& uvsIn = sideIn.GetUVPtStruct();
1399 // if ( uvs1.size() != uvs2.size() )
1402 const SMESH_MAT2d::Branch& branch = *theMA.getBranch(0);
1403 SMESH_MAT2d::BoundaryPoint bp[2];
1404 SMESH_MAT2d::BranchPoint brp;
1406 map< double, const SMDS_MeshNode* > nodeParams; // params of existing nodes
1407 map< double, const SMDS_MeshNode* >::iterator u2n;
1409 // find a node of sideOut most distant from sideIn
1411 vector< BRepAdaptor_Curve > curvesIn( theSinuFace._sinuSide[1].size() );
1412 for ( size_t iE = 0; iE < theSinuFace._sinuSide[1].size(); ++iE )
1413 curvesIn[ iE ].Initialize( theSinuFace._sinuSide[1][iE] );
1416 SMESH_MAT2d::BoundaryPoint bpIn; // closest IN point
1417 const SMDS_MeshNode* nOut = 0;
1418 const size_t nbEOut = theSinuFace._sinuSide[0].size();
1419 for ( size_t iE = 0; iE < nbEOut; ++iE )
1421 const TopoDS_Edge& E = theSinuFace._sinuSide[0][iE];
1423 if ( !SMESH_Algo::GetSortedNodesOnEdge( meshDS, E, /*skipMedium=*/true, nodeParams ))
1425 for ( u2n = nodeParams.begin(); u2n != nodeParams.end(); ++u2n )
1427 // point on EDGE (u2n) --> MA point (brp)
1428 if ( !theMA.getBoundary().getBranchPoint( iE, u2n->first, brp ) ||
1429 !branch.getBoundaryPoints( brp, bp[0], bp[1] ))
1431 gp_Pnt pOut = SMESH_TNodeXYZ( u2n->second );
1432 gp_Pnt pIn = curvesIn[ bp[1]._edgeIndex - nbEOut ].Value( bp[1]._param );
1433 double dist = pOut.SquareDistance( pIn );
1434 if ( dist > maxDist )
1442 const SMDS_MeshNode* nIn = 0;
1443 if ( !SMESH_Algo::GetSortedNodesOnEdge( meshDS,
1444 theSinuFace._sinuEdges[ bpIn._edgeIndex ],
1445 /*skipMedium=*/true,
1448 u2n = nodeParams.lower_bound( bpIn._param );
1449 if ( u2n == nodeParams.end() )
1450 nIn = nodeParams.rbegin()->second;
1454 // find position of distant nodes in uvsOut and uvsIn
1455 size_t iDistOut, iDistIn;
1456 for ( iDistOut = 0; iDistOut < uvsOut.size(); ++iDistOut )
1458 if ( uvsOut[iDistOut].node == nOut )
1461 for ( iDistIn = 0; iDistIn < uvsIn.size(); ++iDistIn )
1463 if ( uvsIn[iDistIn].node == nIn )
1466 if ( iDistOut == uvsOut.size() || iDistIn == uvsIn.size() )
1469 // store opposite nodes in thePointsOnE (param and EDGE have no sense)
1470 pair< NodePoint, NodePoint > oppNodes( NodePoint( nOut, 0, 0 ), NodePoint( nIn, 0, 0));
1471 thePointsOnE.insert( make_pair( uvsOut[ iDistOut ].normParam, oppNodes ));
1472 int iOut = iDistOut, iIn = iDistIn;
1473 int i, nbNodes = std::min( uvsOut.size(), uvsIn.size() );
1474 if ( nbNodes > 5 ) nbNodes = 5;
1475 for ( i = 0, ++iOut, --iIn; i < nbNodes; ++iOut, --iIn, ++i )
1477 iOut = theHelper.WrapIndex( iOut, uvsOut.size() );
1478 iIn = theHelper.WrapIndex( iIn, uvsIn.size() );
1479 oppNodes.first._node = uvsOut[ iOut ].node;
1480 oppNodes.second._node = uvsIn[ iIn ].node;
1481 thePointsOnE.insert( make_pair( uvsOut[ iOut ].normParam, oppNodes ));
1487 //================================================================================
1489 * \brief Setup sides of SinuousFace::_quad
1490 * \param [in] theHelper - helper
1491 * \param [in] thePointsOnEdges - NodePoint's on sinuous sides
1492 * \param [in,out] theSinuFace - the FACE
1493 * \param [in] the1dAlgo - algorithm to use for radial discretization of a ring FACE
1494 * \return bool - is OK
1496 //================================================================================
1498 bool setQuadSides(SMESH_MesherHelper& theHelper,
1499 const TMAPar2NPoints& thePointsOnEdges,
1500 SinuousFace& theFace,
1501 SMESH_Algo* the1dAlgo)
1503 SMESH_Mesh* mesh = theHelper.GetMesh();
1504 const TopoDS_Face& face = theFace._quad->face;
1505 SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( *mesh, face );
1509 list< TopoDS_Edge > side[4];
1510 side[0].insert( side[0].end(), theFace._shortSide[0].begin(), theFace._shortSide[0].end() );
1511 side[1].insert( side[1].end(), theFace._sinuSide [1].begin(), theFace._sinuSide [1].end() );
1512 side[2].insert( side[2].end(), theFace._shortSide[1].begin(), theFace._shortSide[1].end() );
1513 side[3].insert( side[3].end(), theFace._sinuSide [0].begin(), theFace._sinuSide [0].end() );
1515 for ( int i = 0; i < 4; ++i )
1517 theFace._quad->side[i] = StdMeshers_FaceSide::New( face, side[i], mesh, i < QUAD_TOP_SIDE,
1518 /*skipMediumNodes=*/true,
1519 &theHelper, proxyMesh );
1522 if ( theFace.IsRing() )
1524 // --------------------------------------
1525 // Discretize a ring in radial direction
1526 // --------------------------------------
1528 if ( thePointsOnEdges.size() < 4 )
1531 int nbOut = theFace._quad->side[ 1 ].GetUVPtStruct().size();
1532 int nbIn = theFace._quad->side[ 3 ].GetUVPtStruct().size();
1533 if ( nbOut == 0 || nbIn == 0 )
1536 // find most distant opposite nodes
1537 double maxDist = 0, dist;
1538 TMAPar2NPoints::const_iterator u2NPdist, u2NP = thePointsOnEdges.begin();
1539 for ( ; u2NP != thePointsOnEdges.end(); ++u2NP )
1541 SMESH_TNodeXYZ xyz( u2NP->second.first._node ); // node out
1542 dist = xyz.SquareDistance( u2NP->second.second._node );// node in
1543 if ( dist > maxDist )
1549 // compute distribution of radial nodes
1550 list< double > params; // normalized params
1551 static_cast< StdMeshers_QuadFromMedialAxis_1D2D::Algo1D* >
1552 ( the1dAlgo )->ComputeDistribution( theHelper,
1553 SMESH_TNodeXYZ( u2NPdist->second.first._node ),
1554 SMESH_TNodeXYZ( u2NPdist->second.second._node ),
1557 // add a radial quad side
1559 theHelper.SetElementsOnShape( true );
1560 u2NP = thePointsOnEdges.begin();
1561 const SMDS_MeshNode* nOut = u2NP->second.first._node;
1562 const SMDS_MeshNode* nIn = u2NP->second.second._node;
1563 nOut = proxyMesh->GetProxyNode( nOut );
1564 nIn = proxyMesh->GetProxyNode( nIn );
1565 gp_XY uvOut = theHelper.GetNodeUV( face, nOut );
1566 gp_XY uvIn = theHelper.GetNodeUV( face, nIn );
1567 Handle(Geom_Surface) surface = BRep_Tool::Surface( face );
1568 UVPtStructVec uvsNew; UVPtStruct uvPt;
1572 uvsNew.push_back( uvPt );
1573 for (list<double>::iterator itU = params.begin(); itU != params.end(); ++itU )
1575 gp_XY uv = ( 1 - *itU ) * uvOut + *itU * uvIn; // applied in direction Out -> In
1576 gp_Pnt p = surface->Value( uv.X(), uv.Y() );
1577 uvPt.node = theHelper.AddNode( p.X(), p.Y(), p.Z(), /*id=*/0, uv.X(), uv.Y() );
1580 uvsNew.push_back( uvPt );
1585 uvsNew.push_back( uvPt );
1587 theFace._quad->side[ 0 ] = StdMeshers_FaceSide::New( uvsNew );
1588 theFace._quad->side[ 2 ] = theFace._quad->side[ 0 ];
1589 if ( nbIn != nbOut )
1590 theFace._quad->side[ 2 ] = StdMeshers_FaceSide::New( uvsNew );
1592 // assure that the outer sinuous side starts at nOut
1594 const UVPtStructVec& uvsOut = theFace._quad->side[ 3 ].GetUVPtStruct(); // _sinuSide[0]
1595 size_t i; // find UVPtStruct holding nOut
1596 for ( i = 0; i < uvsOut.size(); ++i )
1597 if ( nOut == uvsOut[i].node )
1599 if ( i == uvsOut.size() )
1602 if ( i != 0 && i != uvsOut.size()-1 )
1604 // create a new OUT quad side
1606 uvsNew.reserve( uvsOut.size() );
1607 uvsNew.insert( uvsNew.end(), uvsOut.begin() + i, uvsOut.end() );
1608 uvsNew.insert( uvsNew.end(), uvsOut.begin() + 1, uvsOut.begin() + i + 1);
1609 theFace._quad->side[ 3 ] = StdMeshers_FaceSide::New( uvsNew );
1613 // rotate the IN side if opposite nodes of IN and OUT sides don't match
1615 const SMDS_MeshNode * nIn0 = theFace._quad->side[ 1 ].First().node;
1618 nIn = proxyMesh->GetProxyNode( nIn );
1619 const UVPtStructVec& uvsIn = theFace._quad->side[ 1 ].GetUVPtStruct(); // _sinuSide[1]
1620 size_t i; // find UVPtStruct holding nIn
1621 for ( i = 0; i < uvsIn.size(); ++i )
1622 if ( nIn == uvsIn[i].node )
1624 if ( i == uvsIn.size() )
1627 // create a new IN quad side
1629 uvsNew.reserve( uvsIn.size() );
1630 uvsNew.insert( uvsNew.end(), uvsIn.begin() + i, uvsIn.end() );
1631 uvsNew.insert( uvsNew.end(), uvsIn.begin() + 1, uvsIn.begin() + i + 1);
1632 theFace._quad->side[ 1 ] = StdMeshers_FaceSide::New( uvsNew );
1635 if ( theFace._quad->side[ 1 ].GetUVPtStruct().empty() ||
1636 theFace._quad->side[ 3 ].GetUVPtStruct().empty() )
1639 } // if ( theFace.IsRing() )
1645 //================================================================================
1647 * \brief Divide the sinuous EDGEs by projecting the division point of Medial
1649 * \param [in] theHelper - the helper
1650 * \param [in] theMinSegLen - minimal segment length
1651 * \param [in] theMA - the Medial Axis
1652 * \param [in] theMAParams - parameters of division points of \a theMA
1653 * \param [in] theSinuEdges - the EDGEs to make nodes on
1654 * \param [in] theSinuSide0Size - the number of EDGEs in the 1st sinuous side
1655 * \param [in] the1dAlgo - algorithm to use for radial discretization of a ring FACE
1656 * \return bool - is OK or not
1658 //================================================================================
1660 bool computeSinuEdges( SMESH_MesherHelper& theHelper,
1661 double /*theMinSegLen*/,
1662 SMESH_MAT2d::MedialAxis& theMA,
1663 vector<double>& theMAParams,
1664 SinuousFace& theSinuFace,
1665 SMESH_Algo* the1dAlgo)
1667 if ( theMA.nbBranches() != 1 )
1670 // normalize theMAParams
1671 for ( size_t i = 0; i < theMAParams.size(); ++i )
1672 theMAParams[i] /= theMAParams.back();
1675 SMESH_Mesh* mesh = theHelper.GetMesh();
1676 SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
1679 // get data of sinuous EDGEs and remove unnecessary nodes
1680 const vector< TopoDS_Edge >& theSinuEdges = theSinuFace._sinuEdges;
1681 vector< Handle(Geom_Curve) >& curves = theSinuFace._sinuCurves;
1682 vector< int > edgeIDs ( theSinuEdges.size() ); // IDs in the main shape
1683 vector< bool > isComputed( theSinuEdges.size() );
1684 curves.resize( theSinuEdges.size(), 0 );
1685 bool allComputed = true;
1686 for ( size_t i = 0; i < theSinuEdges.size(); ++i )
1688 curves[i] = BRep_Tool::Curve( theSinuEdges[i], f,l );
1691 SMESH_subMesh* sm = mesh->GetSubMesh( theSinuEdges[i] );
1692 edgeIDs [i] = sm->GetId();
1693 isComputed[i] = ( !sm->IsEmpty() );
1694 if ( !isComputed[i] )
1695 allComputed = false;
1698 const SMESH_MAT2d::Branch& branch = *theMA.getBranch(0);
1699 SMESH_MAT2d::BoundaryPoint bp[2];
1701 TMAPar2NPoints pointsOnE;
1702 // check that computed EDGEs are opposite and equally meshed
1705 // int nbNodes[2] = { 0, 0 };
1706 // for ( int iSide = 0; iSide < 2; ++iSide ) // loop on two sinuous sides
1707 // nbNodes[ iSide ] += meshDS->MeshElements( theSinuFace._sinuSide[ iSide ])->NbNodes() - 1;
1709 // if ( nbNodes[0] != nbNodes[1] )
1712 if ( theSinuFace.IsRing() )
1713 assocNodes( theHelper, theSinuFace, theMA, pointsOnE );
1717 vector< std::size_t > edgeIDs1, edgeIDs2; // indices in theSinuEdges
1718 vector< SMESH_MAT2d::BranchPoint > divPoints;
1719 branch.getOppositeGeomEdges( edgeIDs1, edgeIDs2, divPoints );
1721 for ( size_t i = 0; i < edgeIDs1.size(); ++i )
1722 if ( isComputed[ edgeIDs1[i]] &&
1723 isComputed[ edgeIDs2[i]] )
1725 smIdType nbNodes1 = meshDS->MeshElements(edgeIDs[ edgeIDs1[i]] )->NbNodes();
1726 smIdType nbNodes2 = meshDS->MeshElements(edgeIDs[ edgeIDs2[i]] )->NbNodes();
1727 if ( nbNodes1 != nbNodes2 )
1729 if (( int(i)-1 >= 0 ) &&
1730 ( edgeIDs1[i-1] == edgeIDs1[i] ||
1731 edgeIDs2[i-1] == edgeIDs2[i] ))
1733 if (( i+1 < edgeIDs1.size() ) &&
1734 ( edgeIDs1[i+1] == edgeIDs1[i] ||
1735 edgeIDs2[i+1] == edgeIDs2[i] ))
1739 // map (param on MA) to (parameters of nodes on a pair of theSinuEdges)
1740 vector<double> maParams;
1741 set<int> projectedEdges; // treated EDGEs which 'isComputed'
1743 // compute params of nodes on EDGEs by projecting division points from MA
1745 for ( size_t iEdgePair = 0; iEdgePair < edgeIDs1.size(); ++iEdgePair )
1746 // loop on pairs of opposite EDGEs
1748 if ( projectedEdges.count( edgeIDs1[ iEdgePair ]) ||
1749 projectedEdges.count( edgeIDs2[ iEdgePair ]) )
1752 // --------------------------------------------------------------------------------
1753 if ( isComputed[ edgeIDs1[ iEdgePair ]] != // one EDGE is meshed
1754 isComputed[ edgeIDs2[ iEdgePair ]])
1756 // "projection" from one side to the other
1758 size_t iEdgeComputed = edgeIDs1[iEdgePair], iSideComputed = 0;
1759 if ( !isComputed[ iEdgeComputed ])
1760 ++iSideComputed, iEdgeComputed = edgeIDs2[iEdgePair];
1762 map< double, const SMDS_MeshNode* > nodeParams; // params of existing nodes
1763 if ( !SMESH_Algo::GetSortedNodesOnEdge( meshDS, theSinuEdges[ iEdgeComputed ], /*skipMedium=*/true, nodeParams ))
1766 projectedEdges.insert( iEdgeComputed );
1768 SMESH_MAT2d::BoundaryPoint& bndPnt = bp[ 1-iSideComputed ];
1769 SMESH_MAT2d::BranchPoint brp;
1770 NodePoint npN, npB; // NodePoint's initialized by node and BoundaryPoint
1771 NodePoint& np0 = iSideComputed ? npB : npN;
1772 NodePoint& np1 = iSideComputed ? npN : npB;
1774 double maParam1st, maParamLast, maParam;
1775 if ( !theMA.getBoundary().getBranchPoint( iEdgeComputed, nodeParams.begin()->first, brp ))
1777 branch.getParameter( brp, maParam1st );
1778 if ( !theMA.getBoundary().getBranchPoint( iEdgeComputed, nodeParams.rbegin()->first, brp ))
1780 branch.getParameter( brp, maParamLast );
1782 map< double, const SMDS_MeshNode* >::iterator u2n = nodeParams.begin(), u2nEnd = nodeParams.end();
1783 TMAPar2NPoints::iterator end = pointsOnE.end(), pos = end;
1784 TMAPar2NPoints::iterator & hint = (maParamLast > maParam1st) ? end : pos;
1785 for ( ++u2n, --u2nEnd; u2n != u2nEnd; ++u2n )
1787 // point on EDGE (u2n) --> MA point (brp)
1788 if ( !theMA.getBoundary().getBranchPoint( iEdgeComputed, u2n->first, brp ))
1790 // MA point --> points on 2 EDGEs (bp)
1791 if ( !branch.getBoundaryPoints( brp, bp[0], bp[1] ) ||
1792 !branch.getParameter( brp, maParam ))
1795 npN = NodePoint( u2n->second, u2n->first, iEdgeComputed );
1796 npB = NodePoint( bndPnt );
1797 pos = pointsOnE.insert( hint, make_pair( maParam, make_pair( np0, np1 )));
1800 // --------------------------------------------------------------------------------
1801 else if ( !isComputed[ edgeIDs1[ iEdgePair ]] && // none of EDGEs is meshed
1802 !isComputed[ edgeIDs2[ iEdgePair ]])
1804 // "projection" from MA
1806 if ( !getParamsForEdgePair( iEdgePair, divPoints, theMAParams, maParams ))
1809 for ( size_t i = 1; i < maParams.size()-1; ++i )
1811 if ( !branch.getBoundaryPoints( maParams[i], bp[0], bp[1] ))
1814 pointsOnE.insert( pointsOnE.end(), make_pair( maParams[i], make_pair( NodePoint(bp[0]),
1815 NodePoint(bp[1]))));
1818 // --------------------------------------------------------------------------------
1819 else if ( isComputed[ edgeIDs1[ iEdgePair ]] && // equally meshed EDGES
1820 isComputed[ edgeIDs2[ iEdgePair ]])
1822 // add existing nodes
1824 size_t iE0 = edgeIDs1[ iEdgePair ];
1825 size_t iE1 = edgeIDs2[ iEdgePair ];
1826 map< double, const SMDS_MeshNode* > nodeParams[2]; // params of existing nodes
1827 if ( !SMESH_Algo::GetSortedNodesOnEdge( meshDS, theSinuEdges[ iE0 ],
1828 /*skipMedium=*/false, nodeParams[0] ) ||
1829 !SMESH_Algo::GetSortedNodesOnEdge( meshDS, theSinuEdges[ iE1 ],
1830 /*skipMedium=*/false, nodeParams[1] ) ||
1831 nodeParams[0].size() != nodeParams[1].size() )
1834 if ( nodeParams[0].size() <= 2 )
1835 continue; // nodes on VERTEXes only
1837 bool reverse = ( theSinuEdges[0].Orientation() == theSinuEdges[1].Orientation() );
1839 SMESH_MAT2d::BranchPoint brp;
1840 std::pair< NodePoint, NodePoint > npPair;
1842 map< double, const SMDS_MeshNode* >::iterator
1843 u2n0F = ++nodeParams[0].begin(),
1844 u2n1F = ++nodeParams[1].begin();
1845 map< double, const SMDS_MeshNode* >::reverse_iterator
1846 u2n1R = ++nodeParams[1].rbegin();
1847 for ( ; u2n0F != nodeParams[0].end(); ++u2n0F )
1849 if ( !theMA.getBoundary().getBranchPoint( iE0, u2n0F->first, brp ) ||
1850 !branch.getParameter( brp, maParam ))
1853 npPair.first = NodePoint( u2n0F->second, u2n0F->first, iE0 );
1856 npPair.second = NodePoint( u2n1R->second, u2n1R->first, iE1 );
1861 npPair.second = NodePoint( u2n1F->second, u2n1F->first, iE1 );
1864 pointsOnE.insert( make_pair( maParam, npPair ));
1867 } // loop on pairs of opposite EDGEs
1869 if ( !projectVertices( theHelper, theMA, divPoints, edgeIDs1, edgeIDs2,
1870 isComputed, pointsOnE, theSinuFace ))
1873 separateNodes( theHelper, theMA, pointsOnE, theSinuFace, isComputed );
1876 TMAPar2NPoints::iterator u2np = pointsOnE.begin();
1877 for ( ; u2np != pointsOnE.end(); ++u2np )
1879 NodePoint* np[2] = { & u2np->second.first, & u2np->second.second };
1880 for ( int iSide = 0; iSide < 2; ++iSide )
1882 if ( np[ iSide ]->_node ) continue;
1883 size_t iEdge = np[ iSide ]->_edgeInd;
1884 double u = np[ iSide ]->_u;
1885 gp_Pnt p = curves[ iEdge ]->Value( u );
1886 np[ iSide ]->_node = meshDS->AddNode( p.X(), p.Y(), p.Z() );
1887 meshDS->SetNodeOnEdge( np[ iSide ]->_node, edgeIDs[ iEdge ], u );
1891 // create mesh segments on EDGEs
1892 theHelper.SetElementsOnShape( false );
1893 TopoDS_Face face = TopoDS::Face( theHelper.GetSubShape() );
1894 for ( size_t i = 0; i < theSinuEdges.size(); ++i )
1896 SMESH_subMesh* sm = mesh->GetSubMesh( theSinuEdges[i] );
1897 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->NbElements() > 0 )
1900 StdMeshers_FaceSide side( face, theSinuEdges[i], mesh,
1901 /*isFwd=*/true, /*skipMediumNodes=*/true, &theHelper );
1902 vector<const SMDS_MeshNode*> nodes = side.GetOrderedNodes();
1903 for ( size_t in = 1; in < nodes.size(); ++in )
1905 const SMDS_MeshElement* seg = theHelper.AddEdge( nodes[in-1], nodes[in], 0, false );
1906 meshDS->SetMeshElementOnShape( seg, edgeIDs[ i ] );
1910 // update sub-meshes on VERTEXes
1911 for ( size_t i = 0; i < theSinuEdges.size(); ++i )
1913 mesh->GetSubMesh( theHelper.IthVertex( 0, theSinuEdges[i] ))
1914 ->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1915 mesh->GetSubMesh( theHelper.IthVertex( 1, theSinuEdges[i] ))
1916 ->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1920 // Setup sides of a quadrangle
1921 if ( !setQuadSides( theHelper, pointsOnE, theSinuFace, the1dAlgo ))
1927 //================================================================================
1929 * \brief Mesh short EDGEs
1931 //================================================================================
1933 bool computeShortEdges( SMESH_MesherHelper& theHelper,
1934 const vector<TopoDS_Edge>& theShortEdges,
1935 SMESH_Algo* the1dAlgo,
1936 const bool theHasRadialHyp,
1937 const bool /*theIs2nd*/)
1939 SMESH_Hypothesis::Hypothesis_Status aStatus;
1940 for ( size_t i = 0; i < theShortEdges.size(); ++i )
1942 if ( !theHasRadialHyp )
1944 theHelper.GetGen()->Compute( *theHelper.GetMesh(), theShortEdges[i],
1945 SMESH_Gen::SHAPE_ONLY_UPWARD );
1947 SMESH_subMesh* sm = theHelper.GetMesh()->GetSubMesh(theShortEdges[i] );
1948 if ( sm->IsEmpty() )
1950 // use 2D hyp or minSegLen
1953 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/false);
1954 while ( smIt->more() )
1955 smIt->next()->ComputeStateEngine( SMESH_subMesh::COMPUTE );
1958 the1dAlgo->CheckHypothesis( *theHelper.GetMesh(), theShortEdges[i], aStatus );
1959 if ( !the1dAlgo->Compute( *theHelper.GetMesh(), theShortEdges[i] ))
1965 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1966 if ( sm->IsEmpty() )
1973 inline double area( const UVPtStruct& p1, const UVPtStruct& p2, const UVPtStruct& p3 )
1975 gp_XY v1 = p2.UV() - p1.UV();
1976 gp_XY v2 = p3.UV() - p1.UV();
1980 bool ellipticSmooth( FaceQuadStruct::Ptr quad, int nbLoops )
1983 if ( quad->uv_grid.empty() )
1986 int nbhoriz = quad->iSize;
1987 int nbvertic = quad->jSize;
1989 const double dksi = 0.5, deta = 0.5;
1990 const double dksi2 = dksi*dksi, deta2 = deta*deta;
1991 double err = 0., g11, g22, g12;
1994 FaceQuadStruct& q = *quad;
1997 //double refArea = area( q.UVPt(0,0), q.UVPt(1,0), q.UVPt(1,1) );
1999 for ( int iLoop = 0; iLoop < nbLoops; ++iLoop )
2002 for ( int i = 1; i < nbhoriz - 1; i++ )
2003 for ( int j = 1; j < nbvertic - 1; j++ )
2005 g11 = ( (q.U(i,j+1) - q.U(i,j-1))*(q.U(i,j+1) - q.U(i,j-1))/dksi2 +
2006 (q.V(i,j+1) - q.V(i,j-1))*(q.V(i,j+1) - q.V(i,j-1))/deta2 )/4;
2008 g22 = ( (q.U(i+1,j) - q.U(i-1,j))*(q.U(i+1,j) - q.U(i-1,j))/dksi2 +
2009 (q.V(i+1,j) - q.V(i-1,j))*(q.V(i+1,j) - q.V(i-1,j))/deta2 )/4;
2011 g12 = ( (q.U(i+1,j) - q.U(i-1,j))*(q.U(i,j+1) - q.U(i,j-1))/dksi2 +
2012 (q.V(i+1,j) - q.V(i-1,j))*(q.V(i,j+1) - q.V(i,j-1))/deta2 )/(4*dksi*deta);
2014 pNew.u = dksi2/(2*(g11+g22)) * (g11*(q.U(i+1,j) + q.U(i-1,j))/dksi2 +
2015 g22*(q.U(i,j+1) + q.U(i,j-1))/dksi2
2016 - 0.5*g12*q.U(i+1,j+1) + 0.5*g12*q.U(i-1,j+1) +
2017 - 0.5*g12*q.U(i-1,j-1) + 0.5*g12*q.U(i+1,j-1));
2019 pNew.v = deta2/(2*(g11+g22)) * (g11*(q.V(i+1,j) + q.V(i-1,j))/deta2 +
2020 g22*(q.V(i,j+1) + q.V(i,j-1))/deta2
2021 - 0.5*g12*q.V(i+1,j+1) + 0.5*g12*q.V(i-1,j+1) +
2022 - 0.5*g12*q.V(i-1,j-1) + 0.5*g12*q.V(i+1,j-1));
2024 // if (( refArea * area( q.UVPt(i-1,j-1), q.UVPt(i,j-1), pNew ) > 0 ) &&
2025 // ( refArea * area( q.UVPt(i+1,j-1), q.UVPt(i+1,j), pNew ) > 0 ) &&
2026 // ( refArea * area( q.UVPt(i+1,j+1), q.UVPt(i,j+1), pNew ) > 0 ) &&
2027 // ( refArea * area( q.UVPt(i-1,j), q.UVPt(i-1,j-1), pNew ) > 0 ))
2029 err += sqrt(( q.U(i,j) - pNew.u ) * ( q.U(i,j) - pNew.u ) +
2030 ( q.V(i,j) - pNew.v ) * ( q.V(i,j) - pNew.v ));
2034 // else if ( ++nbErr < 10 )
2036 // cout << i << ", " << j << endl;
2038 // << "[ " << q.U(i-1,j-1) << ", " <<q.U(i,j-1) << ", " << q.U(i+1,j-1) << " ],"
2039 // << "[ " << q.U(i-1,j-0) << ", " <<q.U(i,j-0) << ", " << q.U(i+1,j-0) << " ],"
2040 // << "[ " << q.U(i-1,j+1) << ", " <<q.U(i,j+1) << ", " << q.U(i+1,j+1) << " ]]" << endl;
2042 // << "[ " << q.V(i-1,j-1) << ", " <<q.V(i,j-1) << ", " << q.V(i+1,j-1) << " ],"
2043 // << "[ " << q.V(i-1,j-0) << ", " <<q.V(i,j-0) << ", " << q.V(i+1,j-0) << " ],"
2044 // << "[ " << q.V(i-1,j+1) << ", " <<q.V(i,j+1) << ", " << q.V(i+1,j+1) << " ]]" << endl<<endl;
2048 if ( err / ( nbhoriz - 2 ) / ( nbvertic - 2 ) < 1e-6 )
2051 //cout << " ERR " << err / ( nbhoriz - 2 ) / ( nbvertic - 2 ) << endl;
2056 //================================================================================
2058 * \brief Remove temporary node
2060 //================================================================================
2062 void mergeNodes( SMESH_MesherHelper& theHelper,
2063 SinuousFace& theSinuFace )
2065 SMESH_MeshEditor editor( theHelper.GetMesh() );
2066 SMESH_MeshEditor::TListOfListOfNodes nodesGroups;
2068 TMergeMap::iterator n2nn = theSinuFace._nodesToMerge.begin();
2069 for ( ; n2nn != theSinuFace._nodesToMerge.end(); ++n2nn )
2071 if ( !n2nn->first ) continue;
2072 nodesGroups.push_back( list< const SMDS_MeshNode* >() );
2073 list< const SMDS_MeshNode* > & group = nodesGroups.back();
2075 group.push_back( n2nn->first );
2076 group.splice( group.end(), n2nn->second );
2078 editor.MergeNodes( nodesGroups );
2083 //================================================================================
2085 * \brief Sets event listener which removes mesh from EDGEs when 2D hyps change
2087 //================================================================================
2089 void StdMeshers_QuadFromMedialAxis_1D2D::SetEventListener(SMESH_subMesh* faceSubMesh)
2091 faceSubMesh->SetEventListener( new EdgeCleaner, 0, faceSubMesh );
2094 //================================================================================
2096 * \brief Create quadrangle elements
2097 * \param [in] theHelper - the helper
2098 * \param [in] theFace - the face to mesh
2099 * \param [in] theSinuEdges - the sinuous EDGEs
2100 * \param [in] theShortEdges - the short EDGEs
2101 * \return bool - is OK or not
2103 //================================================================================
2105 bool StdMeshers_QuadFromMedialAxis_1D2D::computeQuads( SMESH_MesherHelper& theHelper,
2106 FaceQuadStruct::Ptr theQuad)
2108 StdMeshers_Quadrangle_2D::myHelper = &theHelper;
2109 StdMeshers_Quadrangle_2D::myNeedSmooth = false;
2110 StdMeshers_Quadrangle_2D::myCheckOri = false;
2111 StdMeshers_Quadrangle_2D::myQuadList.clear();
2113 int nbNodesShort0 = theQuad->side[0].NbPoints();
2114 int nbNodesShort1 = theQuad->side[2].NbPoints();
2115 int nbNodesSinu0 = theQuad->side[1].NbPoints();
2116 int nbNodesSinu1 = theQuad->side[3].NbPoints();
2118 // compute UV of internal points
2119 myQuadList.push_back( theQuad );
2120 // if ( !StdMeshers_Quadrangle_2D::setNormalizedGrid( theQuad ))
2123 // elliptic smooth of internal points to get boundary cell normal to the boundary
2124 bool isRing = theQuad->side[0].grid->Edge(0).IsNull();
2126 if ( !StdMeshers_Quadrangle_2D::setNormalizedGrid( theQuad ))
2128 ellipticSmooth( theQuad, 1 );
2130 // create quadrangles
2132 theHelper.SetElementsOnShape( true );
2133 if ( nbNodesShort0 == nbNodesShort1 && nbNodesSinu0 == nbNodesSinu1 )
2134 ok = StdMeshers_Quadrangle_2D::computeQuadDominant( *theHelper.GetMesh(),
2135 theQuad->face, theQuad );
2137 ok = StdMeshers_Quadrangle_2D::computeTriangles( *theHelper.GetMesh(),
2138 theQuad->face, theQuad );
2140 StdMeshers_Quadrangle_2D::myHelper = 0;
2145 //================================================================================
2147 * \brief Generate quadrangle mesh
2149 //================================================================================
2151 bool StdMeshers_QuadFromMedialAxis_1D2D::Compute(SMESH_Mesh& theMesh,
2152 const TopoDS_Shape& theShape)
2154 SMESH_MesherHelper helper( theMesh );
2155 helper.SetSubShape( theShape );
2157 TopoDS_Face F = TopoDS::Face( theShape );
2158 if ( F.Orientation() >= TopAbs_INTERNAL ) F.Orientation( TopAbs_FORWARD );
2160 SinuousFace sinuFace( F );
2164 if ( getSinuousEdges( helper, sinuFace ))
2168 double minSegLen = getMinSegLen( helper, sinuFace._sinuEdges );
2169 SMESH_MAT2d::MedialAxis ma( F, sinuFace._sinuEdges, minSegLen, /*ignoreCorners=*/true );
2172 _regular1D = new Algo1D( _gen );
2173 _regular1D->SetSegmentLength( minSegLen );
2175 vector<double> maParams;
2176 if ( ! divideMA( helper, ma, sinuFace, _regular1D, minSegLen, maParams ))
2177 return error(COMPERR_BAD_SHAPE);
2181 _regular1D->SetRadialDistribution( _hyp2D );
2183 if ( !computeShortEdges( helper, sinuFace._shortSide[0], _regular1D, _hyp2D, 0 ) ||
2184 !computeShortEdges( helper, sinuFace._shortSide[1], _regular1D, _hyp2D, 1 ))
2185 return error("Failed to mesh short edges");
2189 if ( !computeSinuEdges( helper, minSegLen, ma, maParams, sinuFace, _regular1D ))
2190 return error("Failed to mesh sinuous edges");
2194 bool ok = computeQuads( helper, sinuFace._quad );
2197 mergeNodes( helper, sinuFace );
2204 return error(COMPERR_BAD_SHAPE, "Not implemented so far");
2207 //================================================================================
2209 * \brief Predict nb of elements
2211 //================================================================================
2213 bool StdMeshers_QuadFromMedialAxis_1D2D::Evaluate(SMESH_Mesh & theMesh,
2214 const TopoDS_Shape & theShape,
2215 MapShapeNbElems& theResMap)
2217 return StdMeshers_Quadrangle_2D::Evaluate(theMesh,theShape,theResMap);
2220 //================================================================================
2222 * \brief Return true if the algorithm can mesh this shape
2223 * \param [in] aShape - shape to check
2224 * \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
2225 * else, returns OK if at least one shape is OK
2227 //================================================================================
2229 bool StdMeshers_QuadFromMedialAxis_1D2D::IsApplicable( const TopoDS_Shape & aShape,
2233 SMESH_MesherHelper helper( tmpMesh );
2235 int nbFoundFaces = 0;
2236 for (TopExp_Explorer exp( aShape, TopAbs_FACE ); exp.More(); exp.Next(), ++nbFoundFaces )
2238 const TopoDS_Face& face = TopoDS::Face( exp.Current() );
2239 SinuousFace sinuFace( face );
2240 bool isApplicable = getSinuousEdges( helper, sinuFace );
2242 if ( toCheckAll && !isApplicable ) return false;
2243 if ( !toCheckAll && isApplicable ) return true;
2245 return ( toCheckAll && nbFoundFaces != 0 );