1 // Copyright (C) 2007-2016 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
23 // File : StdMeshers_Prism_3D.cxx
25 // Created : Fri Oct 20 11:37:07 2006
26 // Author : Edward AGAPOV (eap)
28 #include "StdMeshers_Prism_3D.hxx"
30 #include "SMDS_EdgePosition.hxx"
31 #include "SMDS_VolumeOfNodes.hxx"
32 #include "SMDS_VolumeTool.hxx"
33 #include "SMESH_Comment.hxx"
34 #include "SMESH_Gen.hxx"
35 #include "SMESH_HypoFilter.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "StdMeshers_FaceSide.hxx"
38 #include "StdMeshers_ProjectionSource1D.hxx"
39 #include "StdMeshers_ProjectionSource2D.hxx"
40 #include "StdMeshers_ProjectionUtils.hxx"
41 #include "StdMeshers_Projection_1D.hxx"
42 #include "StdMeshers_Projection_1D2D.hxx"
43 #include "StdMeshers_Quadrangle_2D.hxx"
45 #include "utilities.h"
47 #include <BRepAdaptor_CompCurve.hxx>
48 #include <BRep_Tool.hxx>
49 #include <Bnd_B3d.hxx>
50 #include <Geom2dAdaptor_Curve.hxx>
51 #include <Geom2d_Line.hxx>
52 #include <GeomLib_IsPlanarSurface.hxx>
53 #include <Geom_Curve.hxx>
54 #include <Standard_ErrorHandler.hxx>
56 #include <TopExp_Explorer.hxx>
57 #include <TopTools_ListIteratorOfListOfShape.hxx>
58 #include <TopTools_ListOfShape.hxx>
59 #include <TopTools_MapOfShape.hxx>
60 #include <TopTools_SequenceOfShape.hxx>
70 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
71 #define gpXYZ(n) SMESH_TNodeXYZ(n)
74 #define DBGOUT(msg) //cout << msg << endl;
75 #define SHOWYXZ(msg, xyz) \
76 //{ gp_Pnt p (xyz); cout << msg << " ("<< p.X() << "; " <<p.Y() << "; " <<p.Z() << ") " <<endl; }
79 #define SHOWYXZ(msg, xyz)
82 namespace NSProjUtils = StdMeshers_ProjectionUtils;
84 typedef SMESH_Comment TCom;
86 enum { ID_BOT_FACE = SMESH_Block::ID_Fxy0,
87 ID_TOP_FACE = SMESH_Block::ID_Fxy1,
88 BOTTOM_EDGE = 0, TOP_EDGE, V0_EDGE, V1_EDGE, // edge IDs in face
89 NB_WALL_FACES = 4 }; //
93 //=======================================================================
95 * \brief Quadrangle algorithm
97 struct TQuadrangleAlgo : public StdMeshers_Quadrangle_2D
99 TQuadrangleAlgo(int studyId, SMESH_Gen* gen)
100 : StdMeshers_Quadrangle_2D( gen->GetANewId(), studyId, gen)
103 static StdMeshers_Quadrangle_2D* instance( SMESH_Algo* fatherAlgo,
104 SMESH_MesherHelper* helper=0)
106 static TQuadrangleAlgo* algo = new TQuadrangleAlgo( fatherAlgo->GetStudyId(),
107 fatherAlgo->GetGen() );
110 algo->myProxyMesh->GetMesh() != helper->GetMesh() )
111 algo->myProxyMesh.reset( new SMESH_ProxyMesh( *helper->GetMesh() ));
113 algo->myQuadList.clear();
117 algo->_quadraticMesh = helper->GetIsQuadratic();
122 //=======================================================================
124 * \brief Algorithm projecting 1D mesh
126 struct TProjction1dAlgo : public StdMeshers_Projection_1D
128 StdMeshers_ProjectionSource1D myHyp;
130 TProjction1dAlgo(int studyId, SMESH_Gen* gen)
131 : StdMeshers_Projection_1D( gen->GetANewId(), studyId, gen),
132 myHyp( gen->GetANewId(), studyId, gen)
134 StdMeshers_Projection_1D::_sourceHypo = & myHyp;
136 static TProjction1dAlgo* instance( SMESH_Algo* fatherAlgo )
138 static TProjction1dAlgo* algo = new TProjction1dAlgo( fatherAlgo->GetStudyId(),
139 fatherAlgo->GetGen() );
143 //=======================================================================
145 * \brief Algorithm projecting 2D mesh
147 struct TProjction2dAlgo : public StdMeshers_Projection_1D2D
149 StdMeshers_ProjectionSource2D myHyp;
151 TProjction2dAlgo(int studyId, SMESH_Gen* gen)
152 : StdMeshers_Projection_1D2D( gen->GetANewId(), studyId, gen),
153 myHyp( gen->GetANewId(), studyId, gen)
155 StdMeshers_Projection_2D::_sourceHypo = & myHyp;
157 static TProjction2dAlgo* instance( SMESH_Algo* fatherAlgo )
159 static TProjction2dAlgo* algo = new TProjction2dAlgo( fatherAlgo->GetStudyId(),
160 fatherAlgo->GetGen() );
163 const NSProjUtils::TNodeNodeMap& GetNodesMap()
165 return _src2tgtNodes;
167 void SetEventListener( SMESH_subMesh* tgtSubMesh )
169 NSProjUtils::SetEventListener( tgtSubMesh,
170 _sourceHypo->GetSourceFace(),
171 _sourceHypo->GetSourceMesh() );
174 //=======================================================================
176 * \brief Returns already computed EDGEs
178 void getPrecomputedEdges( SMESH_MesherHelper& theHelper,
179 const TopoDS_Shape& theShape,
180 vector< TopoDS_Edge >& theEdges)
184 SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
187 TopTools_IndexedMapOfShape edges;
188 TopExp::MapShapes( theShape, TopAbs_EDGE, edges );
189 for ( int iE = 1; iE <= edges.Extent(); ++iE )
191 const TopoDS_Shape edge = edges( iE );
192 if (( ! ( sm = meshDS->MeshElements( edge ))) ||
193 ( sm->NbElements() == 0 ))
196 // there must not be FACEs meshed with triangles and sharing a computed EDGE
197 // as the precomputed EDGEs are used for propagation other to 'vertical' EDGEs
198 bool faceFound = false;
199 PShapeIteratorPtr faceIt =
200 theHelper.GetAncestors( edge, *theHelper.GetMesh(), TopAbs_FACE );
201 while ( const TopoDS_Shape* face = faceIt->next() )
203 if (( sm = meshDS->MeshElements( *face )) &&
204 ( sm->NbElements() > 0 ) &&
205 ( !theHelper.IsSameElemGeometry( sm, SMDSGeom_QUADRANGLE ) ))
211 theEdges.push_back( TopoDS::Edge( edge ));
215 //================================================================================
217 * \brief Make \a botE be the BOTTOM_SIDE of \a quad.
218 * Return false if the BOTTOM_SIDE is composite
220 //================================================================================
222 bool setBottomEdge( const TopoDS_Edge& botE,
223 FaceQuadStruct::Ptr& quad,
224 const TopoDS_Shape& face)
226 quad->side[ QUAD_TOP_SIDE ].grid->Reverse();
227 quad->side[ QUAD_LEFT_SIDE ].grid->Reverse();
229 bool isComposite = false;
230 for ( size_t i = 0; i < quad->side.size(); ++i )
232 StdMeshers_FaceSidePtr quadSide = quad->side[i];
233 for ( int iE = 0; iE < quadSide->NbEdges(); ++iE )
234 if ( botE.IsSame( quadSide->Edge( iE )))
236 if ( quadSide->NbEdges() > 1 )
237 isComposite = true; //return false;
239 i = quad->side.size(); // to quit from the outer loop
243 if ( edgeIndex != QUAD_BOTTOM_SIDE )
244 quad->shift( quad->side.size() - edgeIndex, /*keepUnitOri=*/false );
246 quad->face = TopoDS::Face( face );
251 //================================================================================
253 * \brief Return iterator pointing to node column for the given parameter
254 * \param columnsMap - node column map
255 * \param parameter - parameter
256 * \retval TParam2ColumnMap::iterator - result
258 * it returns closest left column
260 //================================================================================
262 TParam2ColumnIt getColumn( const TParam2ColumnMap* columnsMap,
263 const double parameter )
265 TParam2ColumnIt u_col = columnsMap->upper_bound( parameter );
266 if ( u_col != columnsMap->begin() )
268 return u_col; // return left column
271 //================================================================================
273 * \brief Return nodes around given parameter and a ratio
274 * \param column - node column
275 * \param param - parameter
276 * \param node1 - lower node
277 * \param node2 - upper node
278 * \retval double - ratio
280 //================================================================================
282 double getRAndNodes( const TNodeColumn* column,
284 const SMDS_MeshNode* & node1,
285 const SMDS_MeshNode* & node2)
287 if ( param >= 1.0 || column->size() == 1) {
288 node1 = node2 = column->back();
292 int i = int( param * ( column->size() - 1 ));
293 double u0 = double( i )/ double( column->size() - 1 );
294 double r = ( param - u0 ) * ( column->size() - 1 );
296 node1 = (*column)[ i ];
297 node2 = (*column)[ i + 1];
301 //================================================================================
303 * \brief Compute boundary parameters of face parts
304 * \param nbParts - nb of parts to split columns into
305 * \param columnsMap - node columns of the face to split
306 * \param params - computed parameters
308 //================================================================================
310 void splitParams( const int nbParts,
311 const TParam2ColumnMap* columnsMap,
312 vector< double > & params)
315 params.reserve( nbParts + 1 );
316 TParam2ColumnIt last_par_col = --columnsMap->end();
317 double par = columnsMap->begin()->first; // 0.
318 double parLast = last_par_col->first;
319 params.push_back( par );
320 for ( int i = 0; i < nbParts - 1; ++ i )
322 double partSize = ( parLast - par ) / double ( nbParts - i );
323 TParam2ColumnIt par_col = getColumn( columnsMap, par + partSize );
324 if ( par_col->first == par ) {
326 if ( par_col == last_par_col ) {
327 while ( i < nbParts - 1 )
328 params.push_back( par + partSize * i++ );
332 par = par_col->first;
333 params.push_back( par );
335 params.push_back( parLast ); // 1.
338 //================================================================================
340 * \brief Return coordinate system for z-th layer of nodes
342 //================================================================================
344 gp_Ax2 getLayerCoordSys(const int z,
345 const vector< const TNodeColumn* >& columns,
348 // gravity center of a layer
351 for ( size_t i = 0; i < columns.size(); ++i )
353 O += gpXYZ( (*columns[ i ])[ z ]);
354 if ( vertexCol < 0 &&
355 columns[ i ]->front()->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
362 int iPrev = columns.size()-1;
363 for ( size_t i = 0; i < columns.size(); ++i )
365 gp_Vec v1( O, gpXYZ( (*columns[ iPrev ])[ z ]));
366 gp_Vec v2( O, gpXYZ( (*columns[ i ] )[ z ]));
371 if ( vertexCol >= 0 )
373 O = gpXYZ( (*columns[ vertexCol ])[ z ]);
375 if ( xColumn < 0 || xColumn >= (int) columns.size() )
377 // select a column for X dir
379 for ( size_t i = 0; i < columns.size(); ++i )
381 double dist = ( O - gpXYZ((*columns[ i ])[ z ])).SquareModulus();
382 if ( dist > maxDist )
391 gp_Vec X( O, gpXYZ( (*columns[ xColumn ])[ z ]));
393 return gp_Ax2( O, Z, X);
396 //================================================================================
398 * \brief Removes submeshes that are or can be meshed with regular grid from given list
399 * \retval int - nb of removed submeshes
401 //================================================================================
403 int removeQuasiQuads(list< SMESH_subMesh* >& notQuadSubMesh,
404 SMESH_MesherHelper* helper,
405 StdMeshers_Quadrangle_2D* quadAlgo)
408 //SMESHDS_Mesh* mesh = notQuadSubMesh.front()->GetFather()->GetMeshDS();
409 list< SMESH_subMesh* >::iterator smIt = notQuadSubMesh.begin();
410 while ( smIt != notQuadSubMesh.end() )
412 SMESH_subMesh* faceSm = *smIt;
413 SMESHDS_SubMesh* faceSmDS = faceSm->GetSubMeshDS();
414 int nbQuads = faceSmDS ? faceSmDS->NbElements() : 0;
417 toRemove = helper->IsStructured( faceSm );
419 toRemove = ( quadAlgo->CheckNbEdges( *helper->GetMesh(),
420 faceSm->GetSubShape() ) != NULL );
421 nbRemoved += toRemove;
423 smIt = notQuadSubMesh.erase( smIt );
431 //================================================================================
433 * \brief Return and angle between two EDGEs
434 * \return double - the angle normalized so that
441 //================================================================================
443 // double normAngle(const TopoDS_Edge & E1, const TopoDS_Edge & E2, const TopoDS_Face & F)
445 // return SMESH_MesherHelper::GetAngle( E1, E2, F ) / ( 0.5 * M_PI );
448 //================================================================================
450 * Consider continuous straight EDGES as one side - mark them to unite
452 //================================================================================
454 int countNbSides( const Prism_3D::TPrismTopo & thePrism,
455 vector<int> & nbUnitePerEdge,
456 vector< double > & edgeLength)
458 int nbEdges = thePrism.myNbEdgesInWires.front(); // nb outer edges
459 int nbSides = nbEdges;
462 list< TopoDS_Edge >::const_iterator edgeIt = thePrism.myBottomEdges.begin();
463 std::advance( edgeIt, nbEdges-1 );
464 TopoDS_Edge prevE = *edgeIt;
465 // bool isPrevStraight = SMESH_Algo::IsStraight( prevE );
466 // int iPrev = nbEdges - 1;
468 // int iUnite = -1; // the first of united EDGEs
470 // analyse angles between EDGEs
472 vector< bool > isCorner( nbEdges );
473 edgeIt = thePrism.myBottomEdges.begin();
474 for ( int iE = 0; iE < nbEdges; ++iE, ++edgeIt )
476 const TopoDS_Edge& curE = *edgeIt;
477 edgeLength[ iE ] = SMESH_Algo::EdgeLength( curE );
479 // double normAngle = normAngle( prevE, curE, thePrism.myBottom );
480 // isCorner[ iE ] = false;
481 // if ( normAngle < 2.0 )
483 // if ( normAngle < 0.001 ) // straight or obtuse angle
485 // // unite EDGEs in order not to put a corner of the unit quadrangle at this VERTEX
488 // nbUnitePerEdge[ iUnite ]++;
489 // nbUnitePerEdge[ iE ] = -1;
494 // isCorner[ iE ] = true;
504 // define which of corners to put on a side of the unit quadrangle
506 // edgeIt = thePrism.myBottomEdges.begin();
507 // for ( int iE = 0; iE < nbEdges; ++iE, ++edgeIt )
509 // const TopoDS_Edge& curE = *edgeIt;
510 // edgeLength[ iE ] = SMESH_Algo::EdgeLength( curE );
512 // const bool isCurStraight = SMESH_Algo::IsStraight( curE );
513 // if ( isPrevStraight && isCurStraight && SMESH_Algo::IsContinuous( prevE, curE ))
517 // nbUnitePerEdge[ iUnite ]++;
518 // nbUnitePerEdge[ iE ] = -1;
526 // isPrevStraight = isCurStraight;
533 //================================================================================
535 * \brief Set/get wire index to FaceQuadStruct
537 //================================================================================
539 void setWireIndex( TFaceQuadStructPtr& quad, int iWire )
543 int getWireIndex( const TFaceQuadStructPtr& quad )
548 //================================================================================
550 * \brief Print Python commands adding given points to a mesh
552 //================================================================================
554 void pointsToPython(const std::vector<gp_XYZ>& p)
557 for ( size_t i = SMESH_Block::ID_V000; i < p.size(); ++i )
559 cout << "mesh.AddNode( " << p[i].X() << ", "<< p[i].Y() << ", "<< p[i].Z() << ") # " << i <<" " ;
560 SMESH_Block::DumpShapeID( i, cout ) << endl;
566 //=======================================================================
567 //function : StdMeshers_Prism_3D
569 //=======================================================================
571 StdMeshers_Prism_3D::StdMeshers_Prism_3D(int hypId, int studyId, SMESH_Gen* gen)
572 :SMESH_3D_Algo(hypId, studyId, gen)
575 _shapeType = (1 << TopAbs_SOLID); // 1 bit per shape type
576 _onlyUnaryInput = false; // mesh all SOLIDs at once
577 _requireDiscreteBoundary = false; // mesh FACEs and EDGEs by myself
578 _supportSubmeshes = true; // "source" FACE must be meshed by other algo
579 _neededLowerHyps[ 1 ] = true; // suppress warning on hiding a global 1D algo
580 _neededLowerHyps[ 2 ] = true; // suppress warning on hiding a global 2D algo
582 //myProjectTriangles = false;
583 mySetErrorToSM = true; // to pass an error to a sub-mesh of a current solid or not
584 myPrevBottomSM = 0; // last treated bottom sub-mesh with a suitable algorithm
587 //================================================================================
591 //================================================================================
593 StdMeshers_Prism_3D::~StdMeshers_Prism_3D()
595 pointsToPython( std::vector<gp_XYZ>() ); // avoid warning: pointsToPython defined but not used
598 //=======================================================================
599 //function : CheckHypothesis
601 //=======================================================================
603 bool StdMeshers_Prism_3D::CheckHypothesis(SMESH_Mesh& aMesh,
604 const TopoDS_Shape& aShape,
605 SMESH_Hypothesis::Hypothesis_Status& aStatus)
608 aStatus = SMESH_Hypothesis::HYP_OK;
612 //=======================================================================
614 //purpose : Compute mesh on a COMPOUND of SOLIDs
615 //=======================================================================
617 bool StdMeshers_Prism_3D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
619 SMESH_MesherHelper helper( theMesh );
623 int nbSolids = helper.Count( theShape, TopAbs_SOLID, /*skipSame=*/false );
627 TopTools_IndexedDataMapOfShapeListOfShape faceToSolids;
628 TopExp::MapShapesAndAncestors( theShape, TopAbs_FACE, TopAbs_SOLID, faceToSolids );
630 // look for meshed FACEs ("source" FACEs) that must be prism bottoms
631 list< TopoDS_Face > meshedFaces, notQuadMeshedFaces, notQuadFaces;
632 const bool meshHasQuads = ( theMesh.NbQuadrangles() > 0 );
633 //StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this );
634 for ( int iF = 1; iF <= faceToSolids.Extent(); ++iF )
636 const TopoDS_Face& face = TopoDS::Face( faceToSolids.FindKey( iF ));
637 SMESH_subMesh* faceSM = theMesh.GetSubMesh( face );
638 if ( !faceSM->IsEmpty() )
640 if ( !meshHasQuads ||
641 !helper.IsSameElemGeometry( faceSM->GetSubMeshDS(), SMDSGeom_QUADRANGLE ) ||
642 !helper.IsStructured( faceSM )
644 notQuadMeshedFaces.push_front( face );
645 else if ( myHelper->Count( face, TopAbs_EDGE, /*ignoreSame=*/false ) != 4 )
646 meshedFaces.push_front( face );
648 meshedFaces.push_back( face );
650 // not add not quadrilateral FACE as we can't compute it
651 // else if ( !quadAlgo->CheckNbEdges( theMesh, face ))
652 // // not add not quadrilateral FACE as it can be a prism side
653 // // else if ( myHelper->Count( face, TopAbs_EDGE, /*ignoreSame=*/false ) != 4 )
655 // notQuadFaces.push_back( face );
658 // notQuadFaces are of medium priority, put them before ordinary meshed faces
659 meshedFaces.splice( meshedFaces.begin(), notQuadFaces );
660 // notQuadMeshedFaces are of highest priority, put them before notQuadFaces
661 meshedFaces.splice( meshedFaces.begin(), notQuadMeshedFaces );
663 Prism_3D::TPrismTopo prism;
665 bool selectBottom = meshedFaces.empty();
669 TopoDS_Shape solid = TopExp_Explorer( theShape, TopAbs_SOLID ).Current();
670 if ( !meshedFaces.empty() )
671 prism.myBottom = meshedFaces.front();
672 return ( initPrism( prism, solid, selectBottom ) &&
676 // find propagation chains from already computed EDGEs
677 vector< TopoDS_Edge > computedEdges;
678 getPrecomputedEdges( helper, theShape, computedEdges );
679 myPropagChains = new TopTools_IndexedMapOfShape[ computedEdges.size() + 1 ];
680 SMESHUtils::ArrayDeleter< TopTools_IndexedMapOfShape > pcDel( myPropagChains );
681 for ( size_t i = 0, nb = 0; i < computedEdges.size(); ++i )
683 StdMeshers_ProjectionUtils::GetPropagationEdge( &theMesh, TopoDS_Edge(),
684 computedEdges[i], myPropagChains + nb );
685 if ( myPropagChains[ nb ].Extent() < 2 ) // an empty map is a termination sign
686 myPropagChains[ nb ].Clear();
691 TopTools_MapOfShape meshedSolids;
692 list< Prism_3D::TPrismTopo > meshedPrism;
693 list< TopoDS_Face > suspectSourceFaces;
694 TopTools_ListIteratorOfListOfShape solidIt;
696 while ( meshedSolids.Extent() < nbSolids )
698 if ( _computeCanceled )
699 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
701 // compute prisms having avident computed source FACE
702 while ( !meshedFaces.empty() )
704 TopoDS_Face face = meshedFaces.front();
705 meshedFaces.pop_front();
706 TopTools_ListOfShape& solidList = faceToSolids.ChangeFromKey( face );
707 while ( !solidList.IsEmpty() )
709 TopoDS_Shape solid = solidList.First();
710 solidList.RemoveFirst();
711 if ( meshedSolids.Add( solid ))
714 prism.myBottom = face;
715 if ( !initPrism( prism, solid, selectBottom ) ||
719 SMESHDS_SubMesh* smDS = theMesh.GetMeshDS()->MeshElements( prism.myTop );
720 if ( !myHelper->IsSameElemGeometry( smDS, SMDSGeom_QUADRANGLE ))
722 meshedFaces.push_front( prism.myTop );
726 suspectSourceFaces.push_back( prism.myTop );
728 meshedPrism.push_back( prism );
732 if ( meshedSolids.Extent() == nbSolids )
735 // below in the loop we try to find source FACEs somehow
737 // project mesh from source FACEs of computed prisms to
738 // prisms sharing wall FACEs
739 list< Prism_3D::TPrismTopo >::iterator prismIt = meshedPrism.begin();
740 for ( ; prismIt != meshedPrism.end(); ++prismIt )
742 for ( size_t iW = 0; iW < prismIt->myWallQuads.size(); ++iW )
744 Prism_3D::TQuadList::iterator wQuad = prismIt->myWallQuads[iW].begin();
745 for ( ; wQuad != prismIt->myWallQuads[iW].end(); ++ wQuad )
747 const TopoDS_Face& wFace = (*wQuad)->face;
748 TopTools_ListOfShape& solidList = faceToSolids.ChangeFromKey( wFace );
749 solidIt.Initialize( solidList );
750 while ( solidIt.More() )
752 const TopoDS_Shape& solid = solidIt.Value();
753 if ( meshedSolids.Contains( solid )) {
754 solidList.Remove( solidIt );
755 continue; // already computed prism
757 if ( myHelper->IsBlock( solid )) {
759 continue; // too trivial
761 // find a source FACE of the SOLID: it's a FACE sharing a bottom EDGE with wFace
762 const TopoDS_Edge& wEdge = (*wQuad)->side[ QUAD_TOP_SIDE ].grid->Edge(0);
763 PShapeIteratorPtr faceIt = myHelper->GetAncestors( wEdge, *myHelper->GetMesh(),
765 while ( const TopoDS_Shape* f = faceIt->next() )
767 const TopoDS_Face& candidateF = TopoDS::Face( *f );
768 if ( candidateF.IsSame( wFace )) continue;
769 // select a source FACE: prismIt->myBottom or prismIt->myTop
770 TopoDS_Face sourceF = prismIt->myBottom;
771 for ( TopExp_Explorer v( prismIt->myTop, TopAbs_VERTEX ); v.More(); v.Next() )
772 if ( myHelper->IsSubShape( v.Current(), candidateF )) {
773 sourceF = prismIt->myTop;
777 prism.myBottom = candidateF;
778 mySetErrorToSM = false;
779 if ( !myHelper->IsSubShape( candidateF, prismIt->myShape3D ) &&
780 myHelper ->IsSubShape( candidateF, solid ) &&
781 !myHelper->GetMesh()->GetSubMesh( candidateF )->IsMeshComputed() &&
782 initPrism( prism, solid, /*selectBottom=*/false ) &&
783 !myHelper->GetMesh()->GetSubMesh( prism.myTop )->IsMeshComputed() &&
784 !myHelper->GetMesh()->GetSubMesh( prism.myBottom )->IsMeshComputed() &&
785 project2dMesh( sourceF, prism.myBottom ))
787 mySetErrorToSM = true;
788 if ( !compute( prism ))
790 SMESHDS_SubMesh* smDS = theMesh.GetMeshDS()->MeshElements( prism.myTop );
791 if ( !myHelper->IsSameElemGeometry( smDS, SMDSGeom_QUADRANGLE ))
793 meshedFaces.push_front( prism.myTop );
794 meshedFaces.push_front( prism.myBottom );
795 selectBottom = false;
797 meshedPrism.push_back( prism );
798 meshedSolids.Add( solid );
802 mySetErrorToSM = true;
804 if ( meshedSolids.Contains( solid ))
805 solidList.Remove( solidIt );
811 if ( !meshedFaces.empty() )
812 break; // to compute prisms with avident sources
815 if ( meshedFaces.empty() )
817 meshedFaces.splice( meshedFaces.end(), suspectSourceFaces );
821 // find FACEs with local 1D hyps, which has to be computed by now,
822 // or at least any computed FACEs
823 if ( meshedFaces.empty() )
826 for ( int iF = 1; iF <= faceToSolids.Extent(); ++iF )
828 const TopoDS_Face& face = TopoDS::Face( faceToSolids.FindKey( iF ));
829 const TopTools_ListOfShape& solidList = faceToSolids.FindFromKey( face );
830 if ( solidList.IsEmpty() ) continue;
831 SMESH_subMesh* faceSM = theMesh.GetSubMesh( face );
832 if ( !faceSM->IsEmpty() )
834 int nbFaces = faceSM->GetSubMeshDS()->NbElements();
835 if ( prevNbFaces < nbFaces )
837 if ( !meshedFaces.empty() ) meshedFaces.pop_back();
838 meshedFaces.push_back( face ); // lower priority
840 prevNbFaces = nbFaces;
845 bool allSubMeComputed = true;
846 SMESH_subMeshIteratorPtr smIt = faceSM->getDependsOnIterator(false,true);
847 while ( smIt->more() && allSubMeComputed )
848 allSubMeComputed = smIt->next()->IsMeshComputed();
849 if ( allSubMeComputed )
851 faceSM->ComputeStateEngine( SMESH_subMesh::COMPUTE );
852 if ( !faceSM->IsEmpty() ) {
853 meshedFaces.push_front( face ); // higher priority
858 faceSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
866 // TODO. there are other ways to find out the source FACE:
867 // propagation, topological similarity, etc...
869 // simply try to mesh all not meshed SOLIDs
870 if ( meshedFaces.empty() )
872 for ( TopExp_Explorer solid( theShape, TopAbs_SOLID ); solid.More(); solid.Next() )
874 mySetErrorToSM = false;
876 if ( !meshedSolids.Contains( solid.Current() ) &&
877 initPrism( prism, solid.Current() ))
879 mySetErrorToSM = true;
880 if ( !compute( prism ))
882 meshedFaces.push_front( prism.myTop );
883 meshedFaces.push_front( prism.myBottom );
884 meshedPrism.push_back( prism );
885 meshedSolids.Add( solid.Current() );
888 mySetErrorToSM = true;
892 if ( meshedFaces.empty() ) // set same error to 10 not-computed solids
894 SMESH_ComputeErrorPtr err = SMESH_ComputeError::New
895 ( COMPERR_BAD_INPUT_MESH, "No meshed source face found", this );
897 const int maxNbErrors = 10; // limit nb errors not to overload the Compute dialog
898 TopExp_Explorer solid( theShape, TopAbs_SOLID );
899 for ( int i = 0; ( i < maxNbErrors && solid.More() ); ++i, solid.Next() )
900 if ( !meshedSolids.Contains( solid.Current() ))
902 SMESH_subMesh* sm = theMesh.GetSubMesh( solid.Current() );
903 sm->GetComputeError() = err;
908 return error( COMPERR_OK );
911 //================================================================================
913 * \brief Find wall faces by bottom edges
915 //================================================================================
917 bool StdMeshers_Prism_3D::getWallFaces( Prism_3D::TPrismTopo & thePrism,
918 const int totalNbFaces)
920 thePrism.myWallQuads.clear();
922 SMESH_Mesh* mesh = myHelper->GetMesh();
924 StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this, myHelper );
926 TopTools_MapOfShape faceMap;
927 TopTools_IndexedDataMapOfShapeListOfShape edgeToFaces;
928 TopExp::MapShapesAndAncestors( thePrism.myShape3D,
929 TopAbs_EDGE, TopAbs_FACE, edgeToFaces );
931 // ------------------------------
932 // Get the 1st row of wall FACEs
933 // ------------------------------
935 list< TopoDS_Edge >::iterator edge = thePrism.myBottomEdges.begin();
936 std::list< int >::iterator nbE = thePrism.myNbEdgesInWires.begin();
937 std::list< int > nbQuadsPerWire;
938 int iE = 0, iWire = 0;
939 while ( edge != thePrism.myBottomEdges.end() )
942 if ( SMESH_Algo::isDegenerated( *edge ))
944 edge = thePrism.myBottomEdges.erase( edge );
950 bool hasWallFace = false;
951 TopTools_ListIteratorOfListOfShape faceIt( edgeToFaces.FindFromKey( *edge ));
952 for ( ; faceIt.More(); faceIt.Next() )
954 const TopoDS_Face& face = TopoDS::Face( faceIt.Value() );
955 if ( !thePrism.myBottom.IsSame( face ))
958 Prism_3D::TQuadList quadList( 1, quadAlgo->CheckNbEdges( *mesh, face ));
959 if ( !quadList.back() )
960 return toSM( error(TCom("Side face #") << shapeID( face )
961 << " not meshable with quadrangles"));
962 bool isCompositeBase = ! setBottomEdge( *edge, quadList.back(), face );
963 if ( isCompositeBase )
965 // it's OK if all EDGEs of the bottom side belongs to the bottom FACE
966 StdMeshers_FaceSidePtr botSide = quadList.back()->side[ QUAD_BOTTOM_SIDE ];
967 for ( int iE = 0; iE < botSide->NbEdges(); ++iE )
968 if ( !myHelper->IsSubShape( botSide->Edge(iE), thePrism.myBottom ))
969 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
971 if ( faceMap.Add( face ))
973 setWireIndex( quadList.back(), iWire ); // for use in makeQuadsForOutInProjection()
974 thePrism.myWallQuads.push_back( quadList );
983 else // seam edge (IPAL53561)
985 edge = thePrism.myBottomEdges.erase( edge );
995 int nbQuadPrev = std::accumulate( nbQuadsPerWire.begin(), nbQuadsPerWire.end(), 0 );
996 nbQuadsPerWire.push_back( thePrism.myWallQuads.size() - nbQuadPrev );
1000 // -------------------------
1001 // Find the rest wall FACEs
1002 // -------------------------
1004 // Compose a vector of indixes of right neighbour FACE for each wall FACE
1005 // that is not so evident in case of several WIREs in the bottom FACE
1006 thePrism.myRightQuadIndex.clear();
1007 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1009 thePrism.myRightQuadIndex.push_back( i+1 ); // OK for all but the last EDGE of a WIRE
1011 list< int >::iterator nbQinW = nbQuadsPerWire.begin();
1012 for ( int iLeft = 0; nbQinW != nbQuadsPerWire.end(); ++nbQinW )
1014 thePrism.myRightQuadIndex[ iLeft + *nbQinW - 1 ] = iLeft; // for the last EDGE of a WIRE
1018 while ( totalNbFaces - faceMap.Extent() > 2 )
1020 // find wall FACEs adjacent to each of wallQuads by the right side EDGE
1023 nbKnownFaces = faceMap.Extent();
1024 StdMeshers_FaceSidePtr rightSide, topSide; // sides of the quad
1025 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1027 rightSide = thePrism.myWallQuads[i].back()->side[ QUAD_RIGHT_SIDE ];
1028 for ( int iE = 0; iE < rightSide->NbEdges(); ++iE ) // rightSide can be composite
1030 const TopoDS_Edge & rightE = rightSide->Edge( iE );
1031 TopTools_ListIteratorOfListOfShape face( edgeToFaces.FindFromKey( rightE ));
1032 for ( ; face.More(); face.Next() )
1033 if ( faceMap.Add( face.Value() ))
1035 // a new wall FACE encountered, store it in thePrism.myWallQuads
1036 const int iRight = thePrism.myRightQuadIndex[i];
1037 topSide = thePrism.myWallQuads[ iRight ].back()->side[ QUAD_TOP_SIDE ];
1038 const TopoDS_Edge& newBotE = topSide->Edge(0);
1039 const TopoDS_Shape& newWallF = face.Value();
1040 thePrism.myWallQuads[ iRight ].push_back( quadAlgo->CheckNbEdges( *mesh, newWallF ));
1041 if ( !thePrism.myWallQuads[ iRight ].back() )
1042 return toSM( error(TCom("Side face #") << shapeID( newWallF ) <<
1043 " not meshable with quadrangles"));
1044 if ( ! setBottomEdge( newBotE, thePrism.myWallQuads[ iRight ].back(), newWallF ))
1045 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
1049 } while ( nbKnownFaces != faceMap.Extent() );
1051 // find wall FACEs adjacent to each of thePrism.myWallQuads by the top side EDGE
1052 if ( totalNbFaces - faceMap.Extent() > 2 )
1054 const int nbFoundWalls = faceMap.Extent();
1055 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1057 StdMeshers_FaceSidePtr topSide = thePrism.myWallQuads[i].back()->side[ QUAD_TOP_SIDE ];
1058 const TopoDS_Edge & topE = topSide->Edge( 0 );
1059 if ( topSide->NbEdges() > 1 )
1060 return toSM( error(COMPERR_BAD_SHAPE, TCom("Side face #") <<
1061 shapeID( thePrism.myWallQuads[i].back()->face )
1062 << " has a composite top edge"));
1063 TopTools_ListIteratorOfListOfShape faceIt( edgeToFaces.FindFromKey( topE ));
1064 for ( ; faceIt.More(); faceIt.Next() )
1065 if ( faceMap.Add( faceIt.Value() ))
1067 // a new wall FACE encountered, store it in wallQuads
1068 thePrism.myWallQuads[ i ].push_back( quadAlgo->CheckNbEdges( *mesh, faceIt.Value() ));
1069 if ( !thePrism.myWallQuads[ i ].back() )
1070 return toSM( error(TCom("Side face #") << shapeID( faceIt.Value() ) <<
1071 " not meshable with quadrangles"));
1072 if ( ! setBottomEdge( topE, thePrism.myWallQuads[ i ].back(), faceIt.Value() ))
1073 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
1074 if ( totalNbFaces - faceMap.Extent() == 2 )
1076 i = thePrism.myWallQuads.size(); // to quit from the outer loop
1081 if ( nbFoundWalls == faceMap.Extent() )
1082 return toSM( error("Failed to find wall faces"));
1085 } // while ( totalNbFaces - faceMap.Extent() > 2 )
1087 // ------------------
1088 // Find the top FACE
1089 // ------------------
1091 if ( thePrism.myTop.IsNull() )
1093 // now only top and bottom FACEs are not in the faceMap
1094 faceMap.Add( thePrism.myBottom );
1095 for ( TopExp_Explorer f( thePrism.myShape3D, TopAbs_FACE ); f.More(); f.Next() )
1096 if ( !faceMap.Contains( f.Current() )) {
1097 thePrism.myTop = TopoDS::Face( f.Current() );
1100 if ( thePrism.myTop.IsNull() )
1101 return toSM( error("Top face not found"));
1104 // Check that the top FACE shares all the top EDGEs
1105 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1107 StdMeshers_FaceSidePtr topSide = thePrism.myWallQuads[i].back()->side[ QUAD_TOP_SIDE ];
1108 const TopoDS_Edge & topE = topSide->Edge( 0 );
1109 if ( !myHelper->IsSubShape( topE, thePrism.myTop ))
1110 return toSM( error( TCom("Wrong source face: #") << shapeID( thePrism.myBottom )));
1116 //=======================================================================
1117 //function : compute
1118 //purpose : Compute mesh on a SOLID
1119 //=======================================================================
1121 bool StdMeshers_Prism_3D::compute(const Prism_3D::TPrismTopo& thePrism)
1123 myHelper->IsQuadraticSubMesh( thePrism.myShape3D );
1124 if ( _computeCanceled )
1125 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
1127 // Assure the bottom is meshed
1128 if ( !computeBase( thePrism ))
1131 // Make all side FACEs of thePrism meshed with quads
1132 if ( !computeWalls( thePrism ))
1135 // Analyse mesh and geometry to find all block sub-shapes and submeshes
1136 // (after fixing IPAL52499 myBlock is used as a holder of boundary nodes
1137 // and for 2D projection in hard cases where StdMeshers_Projection_2D fails;
1138 // location of internal nodes is usually computed by StdMeshers_Sweeper)
1139 if ( !myBlock.Init( myHelper, thePrism ))
1140 return toSM( error( myBlock.GetError()));
1142 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
1144 int volumeID = meshDS->ShapeToIndex( thePrism.myShape3D );
1146 // Try to get gp_Trsf to get all nodes from bottom ones
1147 vector<gp_Trsf> trsf;
1148 gp_Trsf bottomToTopTrsf;
1149 // if ( !myBlock.GetLayersTransformation( trsf, thePrism ))
1151 // else if ( !trsf.empty() )
1152 // bottomToTopTrsf = trsf.back();
1154 // To compute coordinates of a node inside a block using "block approach",
1155 // it is necessary to know
1156 // 1. normalized parameters of the node by which
1157 // 2. coordinates of node projections on all block sub-shapes are computed
1159 // So we fill projections on vertices at once as they are same for all nodes
1160 myShapeXYZ.resize( myBlock.NbSubShapes() );
1161 for ( int iV = SMESH_Block::ID_FirstV; iV < SMESH_Block::ID_FirstE; ++iV ) {
1162 myBlock.VertexPoint( iV, myShapeXYZ[ iV ]);
1163 SHOWYXZ("V point " <<iV << " ", myShapeXYZ[ iV ]);
1166 // Projections on the top and bottom faces are taken from nodes existing
1167 // on these faces; find correspondence between bottom and top nodes
1168 myUseBlock = false; // is set to true if projection is done using "block approach"
1169 myBotToColumnMap.clear();
1170 if ( !assocOrProjBottom2Top( bottomToTopTrsf, thePrism ) ) // it also fills myBotToColumnMap
1173 // If all "vertical" EDGEs are straight, then all nodes of an internal node column
1174 // are located on a line connecting the top node and the bottom node.
1175 bool isStrightColunm = allVerticalEdgesStraight( thePrism );
1176 if ( isStrightColunm )
1179 // Create nodes inside the block
1183 // use transformation (issue 0020680, IPAL0052499) or a "straight line" approach
1184 StdMeshers_Sweeper sweeper;
1185 sweeper.myHelper = myHelper;
1186 sweeper.myBotFace = thePrism.myBottom;
1187 sweeper.myTopFace = thePrism.myTop;
1189 // load boundary nodes into sweeper
1191 std::set< const SMDS_MeshNode* > usedEndNodes;
1192 list< TopoDS_Edge >::const_iterator edge = thePrism.myBottomEdges.begin();
1193 for ( ; edge != thePrism.myBottomEdges.end(); ++edge )
1195 int edgeID = meshDS->ShapeToIndex( *edge );
1196 TParam2ColumnMap* u2col = const_cast<TParam2ColumnMap*>
1197 ( myBlock.GetParam2ColumnMap( edgeID, dummy ));
1199 TParam2ColumnMap::iterator u2colIt = u2col->begin(), u2colEnd = u2col->end();
1200 const SMDS_MeshNode* n0 = u2colIt->second[0];
1201 const SMDS_MeshNode* n1 = u2col->rbegin()->second[0];
1202 if ( !usedEndNodes.insert ( n0 ).second ) ++u2colIt;
1203 if ( !usedEndNodes.insert ( n1 ).second ) --u2colEnd;
1205 for ( ; u2colIt != u2colEnd; ++u2colIt )
1206 sweeper.myBndColumns.push_back( & u2colIt->second );
1208 // load node columns inside the bottom FACE
1209 sweeper.myIntColumns.reserve( myBotToColumnMap.size() );
1210 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.begin();
1211 for ( ; bot_column != myBotToColumnMap.end(); ++bot_column )
1212 sweeper.myIntColumns.push_back( & bot_column->second );
1214 myHelper->SetElementsOnShape( true );
1216 if ( !isStrightColunm )
1218 double tol = getSweepTolerance( thePrism );
1219 bool allowHighBndError = !isSimpleBottom( thePrism );
1220 myUseBlock = !sweeper.ComputeNodesByTrsf( tol, allowHighBndError );
1222 else if ( sweeper.CheckSameZ() )
1224 myUseBlock = !sweeper.ComputeNodesOnStraightSameZ();
1228 myUseBlock = !sweeper.ComputeNodesOnStraight();
1230 myHelper->SetElementsOnShape( false );
1233 if ( myUseBlock ) // use block approach
1235 // loop on nodes inside the bottom face
1236 Prism_3D::TNode prevBNode;
1237 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.begin();
1238 for ( ; bot_column != myBotToColumnMap.end(); ++bot_column )
1240 const Prism_3D::TNode& tBotNode = bot_column->first; // bottom TNode
1241 if ( tBotNode.GetPositionType() != SMDS_TOP_FACE &&
1242 myBlock.HasNodeColumn( tBotNode.myNode ))
1243 continue; // node is not inside the FACE
1245 // column nodes; middle part of the column are zero pointers
1246 TNodeColumn& column = bot_column->second;
1248 // check if a column is already computed using non-block approach
1250 for ( i = 0; i < column.size(); ++i )
1253 if ( i == column.size() )
1254 continue; // all nodes created
1256 gp_XYZ botParams, topParams;
1257 if ( !tBotNode.HasParams() )
1259 // compute bottom node parameters
1260 gp_XYZ paramHint(-1,-1,-1);
1261 if ( prevBNode.IsNeighbor( tBotNode ))
1262 paramHint = prevBNode.GetParams();
1263 if ( !myBlock.ComputeParameters( tBotNode.GetCoords(), tBotNode.ChangeParams(),
1264 ID_BOT_FACE, paramHint ))
1265 return toSM( error(TCom("Can't compute normalized parameters for node ")
1266 << tBotNode.myNode->GetID() << " on the face #"
1267 << myBlock.SubMesh( ID_BOT_FACE )->GetId() ));
1268 prevBNode = tBotNode;
1270 botParams = topParams = tBotNode.GetParams();
1271 topParams.SetZ( 1 );
1273 // compute top node parameters
1274 if ( column.size() > 2 ) {
1275 gp_Pnt topCoords = gpXYZ( column.back() );
1276 if ( !myBlock.ComputeParameters( topCoords, topParams, ID_TOP_FACE, topParams ))
1277 return toSM( error(TCom("Can't compute normalized parameters ")
1278 << "for node " << column.back()->GetID()
1279 << " on the face #"<< column.back()->getshapeId() ));
1282 else // top nodes are created by projection using parameters
1284 botParams = topParams = tBotNode.GetParams();
1285 topParams.SetZ( 1 );
1288 myShapeXYZ[ ID_BOT_FACE ] = tBotNode.GetCoords();
1289 myShapeXYZ[ ID_TOP_FACE ] = gpXYZ( column.back() );
1292 TNodeColumn::iterator columnNodes = column.begin();
1293 for ( int z = 0; columnNodes != column.end(); ++columnNodes, ++z)
1295 const SMDS_MeshNode* & node = *columnNodes;
1296 if ( node ) continue; // skip bottom or top node
1298 // params of a node to create
1299 double rz = (double) z / (double) ( column.size() - 1 );
1300 gp_XYZ params = botParams * ( 1 - rz ) + topParams * rz;
1302 // set coords on all faces and nodes
1303 const int nbSideFaces = 4;
1304 int sideFaceIDs[nbSideFaces] = { SMESH_Block::ID_Fx0z,
1305 SMESH_Block::ID_Fx1z,
1306 SMESH_Block::ID_F0yz,
1307 SMESH_Block::ID_F1yz };
1308 for ( int iF = 0; iF < nbSideFaces; ++iF )
1309 if ( !setFaceAndEdgesXYZ( sideFaceIDs[ iF ], params, z ))
1312 // compute coords for a new node
1314 if ( !SMESH_Block::ShellPoint( params, myShapeXYZ, coords ))
1315 return toSM( error("Can't compute coordinates by normalized parameters"));
1317 // if ( !meshDS->MeshElements( volumeID ) ||
1318 // meshDS->MeshElements( volumeID )->NbNodes() == 0 )
1319 // pointsToPython(myShapeXYZ);
1320 SHOWYXZ("TOPFacePoint ",myShapeXYZ[ ID_TOP_FACE]);
1321 SHOWYXZ("BOT Node "<< tBotNode.myNode->GetID(),gpXYZ(tBotNode.myNode));
1322 SHOWYXZ("ShellPoint ",coords);
1325 node = meshDS->AddNode( coords.X(), coords.Y(), coords.Z() );
1326 meshDS->SetNodeInVolume( node, volumeID );
1328 if ( _computeCanceled )
1331 } // loop on bottom nodes
1336 SMESHDS_SubMesh* smDS = myBlock.SubMeshDS( ID_BOT_FACE );
1337 if ( !smDS ) return toSM( error(COMPERR_BAD_INPUT_MESH, "Null submesh"));
1339 // loop on bottom mesh faces
1340 vector< const TNodeColumn* > columns;
1341 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
1342 while ( faceIt->more() )
1344 const SMDS_MeshElement* face = faceIt->next();
1345 if ( !face || face->GetType() != SMDSAbs_Face )
1348 // find node columns for each node
1349 int nbNodes = face->NbCornerNodes();
1350 columns.resize( nbNodes );
1351 for ( int i = 0; i < nbNodes; ++i )
1353 const SMDS_MeshNode* n = face->GetNode( i );
1354 columns[ i ] = NULL;
1356 if ( n->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
1357 columns[ i ] = myBlock.GetNodeColumn( n );
1359 if ( !columns[ i ] )
1361 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.find( n );
1362 if ( bot_column == myBotToColumnMap.end() )
1363 return toSM( error(TCom("No side nodes found above node ") << n->GetID() ));
1364 columns[ i ] = & bot_column->second;
1368 if ( !AddPrisms( columns, myHelper ))
1369 return toSM( error("Different 'vertical' discretization"));
1371 } // loop on bottom mesh faces
1374 myBotToColumnMap.clear();
1377 // update state of sub-meshes (mostly in order to erase improper errors)
1378 SMESH_subMesh* sm = myHelper->GetMesh()->GetSubMesh( thePrism.myShape3D );
1379 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true);
1380 while ( smIt->more() )
1383 sm->GetComputeError().reset();
1384 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1390 //=======================================================================
1391 //function : computeBase
1392 //purpose : Compute the base face of a prism
1393 //=======================================================================
1395 bool StdMeshers_Prism_3D::computeBase(const Prism_3D::TPrismTopo& thePrism)
1397 SMESH_Mesh* mesh = myHelper->GetMesh();
1398 SMESH_subMesh* botSM = mesh->GetSubMesh( thePrism.myBottom );
1399 if (( botSM->IsEmpty() ) &&
1400 ( ! botSM->GetAlgo() ||
1401 ! _gen->Compute( *botSM->GetFather(), botSM->GetSubShape(), /*shapeOnly=*/true )))
1403 // find any applicable algorithm assigned to any FACE of the main shape
1404 std::vector< TopoDS_Shape > faces;
1405 if ( myPrevBottomSM &&
1406 myPrevBottomSM->GetAlgo()->IsApplicableToShape( thePrism.myBottom, /*all=*/false ))
1407 faces.push_back( myPrevBottomSM->GetSubShape() );
1409 TopExp_Explorer faceIt( mesh->GetShapeToMesh(), TopAbs_FACE );
1410 for ( ; faceIt.More(); faceIt.Next() )
1411 faces.push_back( faceIt.Current() );
1413 faces.push_back( TopoDS_Shape() ); // to try quadrangle algorithm
1415 SMESH_Algo* algo = 0;
1416 for ( size_t i = 0; i < faces.size() && botSM->IsEmpty(); ++i )
1418 if ( faces[i].IsNull() ) algo = TQuadrangleAlgo::instance( this, myHelper );
1419 else algo = mesh->GetSubMesh( faces[i] )->GetAlgo();
1420 if ( algo && algo->IsApplicableToShape( thePrism.myBottom, /*all=*/false ))
1422 // try to compute the bottom FACE
1423 if ( algo->NeedDiscreteBoundary() )
1425 // compute sub-shapes
1426 SMESH_subMeshIteratorPtr smIt = botSM->getDependsOnIterator(false,false);
1428 while ( smIt->more() && subOK )
1430 SMESH_subMesh* sub = smIt->next();
1431 sub->ComputeStateEngine( SMESH_subMesh::COMPUTE );
1432 subOK = sub->IsMeshComputed();
1439 algo->InitComputeError();
1440 algo->Compute( *mesh, botSM->GetSubShape() );
1448 if ( botSM->IsEmpty() )
1449 return error( COMPERR_BAD_INPUT_MESH,
1450 TCom( "No mesher defined to compute the base face #")
1451 << shapeID( thePrism.myBottom ));
1453 if ( botSM->GetAlgo() )
1454 myPrevBottomSM = botSM;
1459 //=======================================================================
1460 //function : computeWalls
1461 //purpose : Compute 2D mesh on walls FACEs of a prism
1462 //=======================================================================
1464 bool StdMeshers_Prism_3D::computeWalls(const Prism_3D::TPrismTopo& thePrism)
1466 SMESH_Mesh* mesh = myHelper->GetMesh();
1467 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
1468 DBGOUT( endl << "COMPUTE Prism " << meshDS->ShapeToIndex( thePrism.myShape3D ));
1470 TProjction1dAlgo* projector1D = TProjction1dAlgo::instance( this );
1471 StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this, myHelper );
1473 // SMESH_HypoFilter hyp1dFilter( SMESH_HypoFilter::IsAlgo(),/*not=*/true);
1474 // hyp1dFilter.And( SMESH_HypoFilter::HasDim( 1 ));
1475 // hyp1dFilter.And( SMESH_HypoFilter::IsMoreLocalThan( thePrism.myShape3D, *mesh ));
1477 // Discretize equally 'vertical' EDGEs
1478 // -----------------------------------
1479 // find source FACE sides for projection: either already computed ones or
1480 // the 'most composite' ones
1481 const size_t nbWalls = thePrism.myWallQuads.size();
1482 vector< int > wgt( nbWalls, 0 ); // "weight" of a wall
1483 for ( size_t iW = 0; iW != nbWalls; ++iW )
1485 Prism_3D::TQuadList::const_iterator quad = thePrism.myWallQuads[iW].begin();
1486 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1488 StdMeshers_FaceSidePtr lftSide = (*quad)->side[ QUAD_LEFT_SIDE ];
1489 lftSide->Reverse(); // to go up
1490 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1493 const TopoDS_Edge& E = lftSide->Edge(i);
1494 if ( mesh->GetSubMesh( E )->IsMeshComputed() )
1497 wgt[ myHelper->WrapIndex( iW+1, nbWalls)] += 10;
1498 wgt[ myHelper->WrapIndex( iW-1, nbWalls)] += 10;
1500 // else if ( mesh->GetHypothesis( E, hyp1dFilter, true )) // local hypothesis!
1504 // in quadratic mesh, pass ignoreMediumNodes to quad sides
1505 if ( myHelper->GetIsQuadratic() )
1507 quad = thePrism.myWallQuads[iW].begin();
1508 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1509 for ( int i = 0; i < NB_QUAD_SIDES; ++i )
1510 (*quad)->side[ i ].grid->SetIgnoreMediumNodes( true );
1513 multimap< int, int > wgt2quad;
1514 for ( size_t iW = 0; iW != nbWalls; ++iW )
1515 wgt2quad.insert( make_pair( wgt[ iW ], iW ));
1517 // artificial quads to do outer <-> inner wall projection
1518 std::map< int, FaceQuadStruct > iW2oiQuads;
1519 std::map< int, FaceQuadStruct >::iterator w2oiq;
1520 makeQuadsForOutInProjection( thePrism, wgt2quad, iW2oiQuads );
1522 // Project 'vertical' EDGEs, from left to right
1523 multimap< int, int >::reverse_iterator w2q = wgt2quad.rbegin();
1524 for ( ; w2q != wgt2quad.rend(); ++w2q )
1526 const int iW = w2q->second;
1527 const Prism_3D::TQuadList& quads = thePrism.myWallQuads[ iW ];
1528 Prism_3D::TQuadList::const_iterator quad = quads.begin();
1529 for ( ; quad != quads.end(); ++quad )
1531 StdMeshers_FaceSidePtr rgtSide = (*quad)->side[ QUAD_RIGHT_SIDE ]; // tgt
1532 StdMeshers_FaceSidePtr lftSide = (*quad)->side[ QUAD_LEFT_SIDE ]; // src
1533 bool swapLeftRight = ( lftSide->NbSegments( /*update=*/true ) == 0 &&
1534 rgtSide->NbSegments( /*update=*/true ) > 0 );
1535 if ( swapLeftRight )
1536 std::swap( lftSide, rgtSide );
1538 bool isArtificialQuad = (( w2oiq = iW2oiQuads.find( iW )) != iW2oiQuads.end() );
1539 if ( isArtificialQuad )
1541 // reset sides to perform the outer <-> inner projection
1542 FaceQuadStruct& oiQuad = w2oiq->second;
1543 rgtSide = oiQuad.side[ QUAD_RIGHT_SIDE ];
1544 lftSide = oiQuad.side[ QUAD_LEFT_SIDE ];
1545 iW2oiQuads.erase( w2oiq );
1548 // assure that all the source (left) EDGEs are meshed
1549 int nbSrcSegments = 0;
1550 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1552 if ( isArtificialQuad )
1554 nbSrcSegments = lftSide->NbPoints()-1;
1557 const TopoDS_Edge& srcE = lftSide->Edge(i);
1558 SMESH_subMesh* srcSM = mesh->GetSubMesh( srcE );
1559 if ( !srcSM->IsMeshComputed() ) {
1560 DBGOUT( "COMPUTE V edge " << srcSM->GetId() );
1561 TopoDS_Edge prpgSrcE = findPropagationSource( srcE );
1562 if ( !prpgSrcE.IsNull() ) {
1563 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1564 projector1D->myHyp.SetSourceEdge( prpgSrcE );
1565 projector1D->Compute( *mesh, srcE );
1566 srcSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1569 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1570 srcSM->ComputeStateEngine ( SMESH_subMesh::COMPUTE );
1572 if ( !srcSM->IsMeshComputed() )
1573 return toSM( error( "Can't compute 1D mesh" ));
1575 nbSrcSegments += srcSM->GetSubMeshDS()->NbElements();
1577 // check target EDGEs
1578 int nbTgtMeshed = 0, nbTgtSegments = 0;
1579 vector< bool > isTgtEdgeComputed( rgtSide->NbEdges() );
1580 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1582 const TopoDS_Edge& tgtE = rgtSide->Edge(i);
1583 SMESH_subMesh* tgtSM = mesh->GetSubMesh( tgtE );
1584 if ( !( isTgtEdgeComputed[ i ] = tgtSM->IsMeshComputed() )) {
1585 tgtSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1586 tgtSM->ComputeStateEngine ( SMESH_subMesh::COMPUTE );
1588 if ( tgtSM->IsMeshComputed() ) {
1590 nbTgtSegments += tgtSM->GetSubMeshDS()->NbElements();
1593 if ( rgtSide->NbEdges() == nbTgtMeshed ) // all tgt EDGEs meshed
1595 if ( nbTgtSegments != nbSrcSegments )
1597 bool badMeshRemoved = false;
1598 // remove just computed segments
1599 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1600 if ( !isTgtEdgeComputed[ i ])
1602 const TopoDS_Edge& tgtE = rgtSide->Edge(i);
1603 SMESH_subMesh* tgtSM = mesh->GetSubMesh( tgtE );
1604 tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
1605 badMeshRemoved = true;
1608 if ( !badMeshRemoved )
1610 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1611 addBadInputElements( meshDS->MeshElements( lftSide->Edge( i )));
1612 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1613 addBadInputElements( meshDS->MeshElements( rgtSide->Edge( i )));
1614 return toSM( error( TCom("Different nb of segment on logically vertical edges #")
1615 << shapeID( lftSide->Edge(0) ) << " and #"
1616 << shapeID( rgtSide->Edge(0) ) << ": "
1617 << nbSrcSegments << " != " << nbTgtSegments ));
1620 else // if ( nbTgtSegments == nbSrcSegments )
1625 // Compute 'vertical projection'
1626 if ( nbTgtMeshed == 0 )
1628 // compute nodes on target VERTEXes
1629 const UVPtStructVec& srcNodeStr = lftSide->GetUVPtStruct();
1630 if ( srcNodeStr.size() == 0 )
1631 return toSM( error( TCom("Invalid node positions on edge #") <<
1632 lftSide->EdgeID(0) ));
1633 vector< SMDS_MeshNode* > newNodes( srcNodeStr.size() );
1634 for ( int is2ndV = 0; is2ndV < 2; ++is2ndV )
1636 const TopoDS_Edge& E = rgtSide->Edge( is2ndV ? rgtSide->NbEdges()-1 : 0 );
1637 TopoDS_Vertex v = myHelper->IthVertex( is2ndV, E );
1638 mesh->GetSubMesh( v )->ComputeStateEngine( SMESH_subMesh::COMPUTE );
1639 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, meshDS );
1640 newNodes[ is2ndV ? newNodes.size()-1 : 0 ] = (SMDS_MeshNode*) n;
1643 // compute nodes on target EDGEs
1644 DBGOUT( "COMPUTE V edge (proj) " << shapeID( lftSide->Edge(0)));
1645 //rgtSide->Reverse(); // direct it same as the lftSide
1646 myHelper->SetElementsOnShape( false ); // myHelper holds the prism shape
1647 TopoDS_Edge tgtEdge;
1648 for ( size_t iN = 1; iN < srcNodeStr.size()-1; ++iN ) // add nodes
1650 gp_Pnt p = rgtSide->Value3d ( srcNodeStr[ iN ].normParam );
1651 double u = rgtSide->Parameter( srcNodeStr[ iN ].normParam, tgtEdge );
1652 newNodes[ iN ] = meshDS->AddNode( p.X(), p.Y(), p.Z() );
1653 meshDS->SetNodeOnEdge( newNodes[ iN ], tgtEdge, u );
1655 for ( size_t iN = 1; iN < srcNodeStr.size(); ++iN ) // add segments
1657 // find an EDGE to set a new segment
1658 std::pair<int, TopAbs_ShapeEnum> id2type =
1659 myHelper->GetMediumPos( newNodes[ iN-1 ], newNodes[ iN ] );
1660 if ( id2type.second != TopAbs_EDGE )
1662 // new nodes are on different EDGEs; put one of them on VERTEX
1663 const int edgeIndex = rgtSide->EdgeIndex( srcNodeStr[ iN-1 ].normParam );
1664 const double vertexParam = rgtSide->LastParameter( edgeIndex );
1665 TopoDS_Vertex vertex = rgtSide->LastVertex( edgeIndex );
1666 const SMDS_MeshNode* vn = SMESH_Algo::VertexNode( vertex, meshDS );
1667 const gp_Pnt p = BRep_Tool::Pnt( vertex );
1668 const int isPrev = ( Abs( srcNodeStr[ iN-1 ].normParam - vertexParam ) <
1669 Abs( srcNodeStr[ iN ].normParam - vertexParam ));
1670 meshDS->UnSetNodeOnShape( newNodes[ iN-isPrev ] );
1671 meshDS->SetNodeOnVertex ( newNodes[ iN-isPrev ], vertex );
1672 meshDS->MoveNode ( newNodes[ iN-isPrev ], p.X(), p.Y(), p.Z() );
1673 id2type.first = newNodes[ iN-(1-isPrev) ]->getshapeId();
1676 SMESH_MeshEditor::TListOfListOfNodes lln( 1, list< const SMDS_MeshNode* >() );
1677 lln.back().push_back ( vn );
1678 lln.back().push_front( newNodes[ iN-isPrev ] ); // to keep
1679 SMESH_MeshEditor( mesh ).MergeNodes( lln );
1682 SMDS_MeshElement* newEdge = myHelper->AddEdge( newNodes[ iN-1 ], newNodes[ iN ] );
1683 meshDS->SetMeshElementOnShape( newEdge, id2type.first );
1685 myHelper->SetElementsOnShape( true );
1686 for ( int i = 0; i < rgtSide->NbEdges(); ++i ) // update state of sub-meshes
1688 const TopoDS_Edge& E = rgtSide->Edge( i );
1689 SMESH_subMesh* tgtSM = mesh->GetSubMesh( E );
1690 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1693 // to continue projection from the just computed side as a source
1694 if ( !swapLeftRight && rgtSide->NbEdges() > 1 && w2q->second == iW )
1696 std::pair<int,int> wgt2quadKeyVal( w2q->first + 1, thePrism.myRightQuadIndex[ iW ]);
1697 wgt2quad.insert( wgt2quadKeyVal ); // it will be skipped by ++w2q
1698 wgt2quad.insert( wgt2quadKeyVal );
1699 w2q = wgt2quad.rbegin();
1704 // HOPE assigned hypotheses are OK, so that equal nb of segments will be generated
1705 //return toSM( error("Partial projection not implemented"));
1707 } // loop on quads of a composite wall side
1708 } // loop on the ordered wall sides
1712 for ( size_t iW = 0; iW != thePrism.myWallQuads.size(); ++iW )
1714 Prism_3D::TQuadList::const_iterator quad = thePrism.myWallQuads[iW].begin();
1715 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1717 const TopoDS_Face& face = (*quad)->face;
1718 SMESH_subMesh* fSM = mesh->GetSubMesh( face );
1719 if ( ! fSM->IsMeshComputed() )
1721 // Top EDGEs must be projections from the bottom ones
1722 // to compute structured quad mesh on wall FACEs
1723 // ---------------------------------------------------
1724 const TopoDS_Edge& botE = (*quad)->side[ QUAD_BOTTOM_SIDE ].grid->Edge(0);
1725 const TopoDS_Edge& topE = (*quad)->side[ QUAD_TOP_SIDE ].grid->Edge(0);
1726 SMESH_subMesh* botSM = mesh->GetSubMesh( botE );
1727 SMESH_subMesh* topSM = mesh->GetSubMesh( topE );
1728 SMESH_subMesh* srcSM = botSM;
1729 SMESH_subMesh* tgtSM = topSM;
1730 srcSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1731 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1732 if ( !srcSM->IsMeshComputed() && tgtSM->IsMeshComputed() )
1733 std::swap( srcSM, tgtSM );
1735 if ( !srcSM->IsMeshComputed() )
1737 DBGOUT( "COMPUTE H edge " << srcSM->GetId());
1738 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE ); // nodes on VERTEXes
1739 srcSM->ComputeStateEngine( SMESH_subMesh::COMPUTE ); // segments on the EDGE
1742 if ( tgtSM->IsMeshComputed() &&
1743 tgtSM->GetSubMeshDS()->NbNodes() != srcSM->GetSubMeshDS()->NbNodes() )
1745 // the top EDGE is computed differently than the bottom one,
1746 // try to clear a wrong mesh
1747 bool isAdjFaceMeshed = false;
1748 PShapeIteratorPtr fIt = myHelper->GetAncestors( tgtSM->GetSubShape(),
1749 *mesh, TopAbs_FACE );
1750 while ( const TopoDS_Shape* f = fIt->next() )
1751 if (( isAdjFaceMeshed = mesh->GetSubMesh( *f )->IsMeshComputed() ))
1753 if ( isAdjFaceMeshed )
1754 return toSM( error( TCom("Different nb of segment on logically horizontal edges #")
1755 << shapeID( botE ) << " and #"
1756 << shapeID( topE ) << ": "
1757 << tgtSM->GetSubMeshDS()->NbElements() << " != "
1758 << srcSM->GetSubMeshDS()->NbElements() ));
1759 tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
1761 if ( !tgtSM->IsMeshComputed() )
1763 // compute nodes on VERTEXes
1764 SMESH_subMeshIteratorPtr smIt = tgtSM->getDependsOnIterator(/*includeSelf=*/false);
1765 while ( smIt->more() )
1766 smIt->next()->ComputeStateEngine( SMESH_subMesh::COMPUTE );
1768 DBGOUT( "COMPUTE H edge (proj) " << tgtSM->GetId());
1769 projector1D->myHyp.SetSourceEdge( TopoDS::Edge( srcSM->GetSubShape() ));
1770 projector1D->InitComputeError();
1771 bool ok = projector1D->Compute( *mesh, tgtSM->GetSubShape() );
1774 SMESH_ComputeErrorPtr err = projector1D->GetComputeError();
1775 if ( err->IsOK() ) err->myName = COMPERR_ALGO_FAILED;
1776 tgtSM->GetComputeError() = err;
1780 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1783 // Compute quad mesh on wall FACEs
1784 // -------------------------------
1786 // make all EDGES meshed
1787 fSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1788 if ( !fSM->SubMeshesComputed() )
1789 return toSM( error( COMPERR_BAD_INPUT_MESH,
1790 "Not all edges have valid algorithm and hypothesis"));
1792 quadAlgo->InitComputeError();
1793 DBGOUT( "COMPUTE Quad face " << fSM->GetId());
1794 bool ok = quadAlgo->Compute( *mesh, face );
1795 fSM->GetComputeError() = quadAlgo->GetComputeError();
1798 fSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1800 if ( myHelper->GetIsQuadratic() )
1802 // fill myHelper with medium nodes built by quadAlgo
1803 for ( SMDS_ElemIteratorPtr fIt = fSM->GetSubMeshDS()->GetElements(); fIt->more(); )
1804 myHelper->AddTLinks( SMDS_Mesh::DownCast<SMDS_MeshFace>( fIt->next() ));
1812 //=======================================================================
1813 //function : findPropagationSource
1814 //purpose : Returns a source EDGE of propagation to a given EDGE
1815 //=======================================================================
1817 TopoDS_Edge StdMeshers_Prism_3D::findPropagationSource( const TopoDS_Edge& E )
1819 if ( myPropagChains )
1820 for ( size_t i = 0; !myPropagChains[i].IsEmpty(); ++i )
1821 if ( myPropagChains[i].Contains( E ))
1822 return TopoDS::Edge( myPropagChains[i].FindKey( 1 ));
1824 return TopoDS_Edge();
1827 //=======================================================================
1828 //function : makeQuadsForOutInProjection
1829 //purpose : Create artificial wall quads for vertical projection between
1830 // the outer and inner walls
1831 //=======================================================================
1833 void StdMeshers_Prism_3D::makeQuadsForOutInProjection( const Prism_3D::TPrismTopo& thePrism,
1834 multimap< int, int >& wgt2quad,
1835 map< int, FaceQuadStruct >& iQ2oiQuads)
1837 if ( thePrism.NbWires() <= 1 )
1840 std::set< int > doneWires; // processed wires
1842 SMESH_Mesh* mesh = myHelper->GetMesh();
1843 const bool isForward = true;
1844 const bool skipMedium = myHelper->GetIsQuadratic();
1846 // make a source side for all projections
1848 multimap< int, int >::reverse_iterator w2q = wgt2quad.rbegin();
1849 const int iQuad = w2q->second;
1850 const int iWire = getWireIndex( thePrism.myWallQuads[ iQuad ].front() );
1851 doneWires.insert( iWire );
1853 UVPtStructVec srcNodes;
1855 Prism_3D::TQuadList::const_iterator quad = thePrism.myWallQuads[ iQuad ].begin();
1856 for ( ; quad != thePrism.myWallQuads[ iQuad ].end(); ++quad )
1858 StdMeshers_FaceSidePtr lftSide = (*quad)->side[ QUAD_LEFT_SIDE ];
1860 // assure that all the source (left) EDGEs are meshed
1861 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1863 const TopoDS_Edge& srcE = lftSide->Edge(i);
1864 SMESH_subMesh* srcSM = mesh->GetSubMesh( srcE );
1865 if ( !srcSM->IsMeshComputed() ) {
1866 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1867 srcSM->ComputeStateEngine ( SMESH_subMesh::COMPUTE );
1869 if ( !srcSM->IsMeshComputed() )
1872 const UVPtStructVec& subNodes = lftSide->GetUVPtStruct();
1873 UVPtStructVec::const_iterator subBeg = subNodes.begin(), subEnd = subNodes.end();
1874 if ( !srcNodes.empty() ) ++subBeg;
1875 srcNodes.insert( srcNodes.end(), subBeg, subEnd );
1877 StdMeshers_FaceSidePtr srcSide = StdMeshers_FaceSide::New( srcNodes );
1881 list< TopoDS_Edge > sideEdges;
1883 for ( ++w2q; w2q != wgt2quad.rend(); ++w2q )
1885 const int iQuad = w2q->second;
1886 const Prism_3D::TQuadList& quads = thePrism.myWallQuads[ iQuad ];
1887 const int iWire = getWireIndex( quads.front() );
1888 if ( !doneWires.insert( iWire ).second )
1892 for ( quad = quads.begin(); quad != quads.end(); ++quad )
1894 StdMeshers_FaceSidePtr lftSide = (*quad)->side[ QUAD_LEFT_SIDE ];
1895 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1896 sideEdges.push_back( lftSide->Edge( i ));
1897 face = lftSide->Face();
1899 StdMeshers_FaceSidePtr tgtSide =
1900 StdMeshers_FaceSide::New( face, sideEdges, mesh, isForward, skipMedium, myHelper );
1902 FaceQuadStruct& newQuad = iQ2oiQuads[ iQuad ];
1903 newQuad.side.resize( 4 );
1904 newQuad.side[ QUAD_LEFT_SIDE ] = srcSide;
1905 newQuad.side[ QUAD_RIGHT_SIDE ] = tgtSide;
1907 wgt2quad.insert( *w2q ); // to process this quad after processing the newQuad
1911 //=======================================================================
1912 //function : Evaluate
1914 //=======================================================================
1916 bool StdMeshers_Prism_3D::Evaluate(SMESH_Mesh& theMesh,
1917 const TopoDS_Shape& theShape,
1918 MapShapeNbElems& aResMap)
1920 if ( theShape.ShapeType() == TopAbs_COMPOUND )
1923 for ( TopoDS_Iterator it( theShape ); it.More(); it.Next() )
1924 ok &= Evaluate( theMesh, it.Value(), aResMap );
1927 SMESH_MesherHelper helper( theMesh );
1929 myHelper->SetSubShape( theShape );
1931 // find face contains only triangles
1932 vector < SMESH_subMesh * >meshFaces;
1933 TopTools_SequenceOfShape aFaces;
1934 int NumBase = 0, i = 0, NbQFs = 0;
1935 for (TopExp_Explorer exp(theShape, TopAbs_FACE); exp.More(); exp.Next()) {
1937 aFaces.Append(exp.Current());
1938 SMESH_subMesh *aSubMesh = theMesh.GetSubMesh(exp.Current());
1939 meshFaces.push_back(aSubMesh);
1940 MapShapeNbElemsItr anIt = aResMap.find(meshFaces[i-1]);
1941 if( anIt==aResMap.end() )
1942 return toSM( error( "Submesh can not be evaluated"));
1944 std::vector<int> aVec = (*anIt).second;
1945 int nbtri = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1946 int nbqua = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1947 if( nbtri==0 && nbqua>0 ) {
1956 std::vector<int> aResVec(SMDSEntity_Last);
1957 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1958 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1959 aResMap.insert(std::make_pair(sm,aResVec));
1960 return toSM( error( "Submesh can not be evaluated" ));
1963 if(NumBase==0) NumBase = 1; // only quads => set 1 faces as base
1965 // find number of 1d elems for base face
1967 TopTools_MapOfShape Edges1;
1968 for (TopExp_Explorer exp(aFaces.Value(NumBase), TopAbs_EDGE); exp.More(); exp.Next()) {
1969 Edges1.Add(exp.Current());
1970 SMESH_subMesh *sm = theMesh.GetSubMesh(exp.Current());
1972 MapShapeNbElemsItr anIt = aResMap.find(sm);
1973 if( anIt == aResMap.end() ) continue;
1974 std::vector<int> aVec = (*anIt).second;
1975 nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
1978 // find face opposite to base face
1980 for(i=1; i<=6; i++) {
1981 if(i==NumBase) continue;
1982 bool IsOpposite = true;
1983 for(TopExp_Explorer exp(aFaces.Value(i), TopAbs_EDGE); exp.More(); exp.Next()) {
1984 if( Edges1.Contains(exp.Current()) ) {
1994 // find number of 2d elems on side faces
1996 for(i=1; i<=6; i++) {
1997 if( i==OppNum || i==NumBase ) continue;
1998 MapShapeNbElemsItr anIt = aResMap.find( meshFaces[i-1] );
1999 if( anIt == aResMap.end() ) continue;
2000 std::vector<int> aVec = (*anIt).second;
2001 nb2d += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
2004 MapShapeNbElemsItr anIt = aResMap.find( meshFaces[NumBase-1] );
2005 std::vector<int> aVec = (*anIt).second;
2006 bool IsQuadratic = (aVec[SMDSEntity_Quad_Triangle]>aVec[SMDSEntity_Triangle]) ||
2007 (aVec[SMDSEntity_Quad_Quadrangle]>aVec[SMDSEntity_Quadrangle]);
2008 int nb2d_face0_3 = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
2009 int nb2d_face0_4 = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
2010 int nb0d_face0 = aVec[SMDSEntity_Node];
2011 int nb1d_face0_int = ( nb2d_face0_3*3 + nb2d_face0_4*4 - nb1d ) / 2;
2013 std::vector<int> aResVec(SMDSEntity_Last);
2014 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
2016 aResVec[SMDSEntity_Quad_Penta] = nb2d_face0_3 * ( nb2d/nb1d );
2017 aResVec[SMDSEntity_Quad_Hexa] = nb2d_face0_4 * ( nb2d/nb1d );
2018 aResVec[SMDSEntity_Node] = nb0d_face0 * ( 2*nb2d/nb1d - 1 ) - nb1d_face0_int * nb2d/nb1d;
2021 aResVec[SMDSEntity_Node] = nb0d_face0 * ( nb2d/nb1d - 1 );
2022 aResVec[SMDSEntity_Penta] = nb2d_face0_3 * ( nb2d/nb1d );
2023 aResVec[SMDSEntity_Hexa] = nb2d_face0_4 * ( nb2d/nb1d );
2025 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
2026 aResMap.insert(std::make_pair(sm,aResVec));
2031 //================================================================================
2033 * \brief Create prisms
2034 * \param columns - columns of nodes generated from nodes of a mesh face
2035 * \param helper - helper initialized by mesh and shape to add prisms to
2037 //================================================================================
2039 bool StdMeshers_Prism_3D::AddPrisms( vector<const TNodeColumn*> & columns,
2040 SMESH_MesherHelper* helper)
2042 size_t nbNodes = columns.size();
2043 size_t nbZ = columns[0]->size();
2044 if ( nbZ < 2 ) return false;
2045 for ( size_t i = 1; i < nbNodes; ++i )
2046 if ( columns[i]->size() != nbZ )
2049 // find out orientation
2050 bool isForward = true;
2051 SMDS_VolumeTool vTool;
2053 switch ( nbNodes ) {
2055 SMDS_VolumeOfNodes tmpPenta ( (*columns[0])[z-1], // bottom
2058 (*columns[0])[z], // top
2061 vTool.Set( &tmpPenta );
2062 isForward = vTool.IsForward();
2066 SMDS_VolumeOfNodes tmpHex( (*columns[0])[z-1], (*columns[1])[z-1], // bottom
2067 (*columns[2])[z-1], (*columns[3])[z-1],
2068 (*columns[0])[z], (*columns[1])[z], // top
2069 (*columns[2])[z], (*columns[3])[z] );
2070 vTool.Set( &tmpHex );
2071 isForward = vTool.IsForward();
2075 const int di = (nbNodes+1) / 3;
2076 SMDS_VolumeOfNodes tmpVol ( (*columns[0] )[z-1],
2077 (*columns[di] )[z-1],
2078 (*columns[2*di])[z-1],
2081 (*columns[2*di])[z] );
2082 vTool.Set( &tmpVol );
2083 isForward = vTool.IsForward();
2086 // vertical loop on columns
2088 helper->SetElementsOnShape( true );
2090 switch ( nbNodes ) {
2092 case 3: { // ---------- pentahedra
2093 const int i1 = isForward ? 1 : 2;
2094 const int i2 = isForward ? 2 : 1;
2095 for ( z = 1; z < nbZ; ++z )
2096 helper->AddVolume( (*columns[0 ])[z-1], // bottom
2097 (*columns[i1])[z-1],
2098 (*columns[i2])[z-1],
2099 (*columns[0 ])[z], // top
2101 (*columns[i2])[z] );
2104 case 4: { // ---------- hexahedra
2105 const int i1 = isForward ? 1 : 3;
2106 const int i3 = isForward ? 3 : 1;
2107 for ( z = 1; z < nbZ; ++z )
2108 helper->AddVolume( (*columns[0])[z-1], (*columns[i1])[z-1], // bottom
2109 (*columns[2])[z-1], (*columns[i3])[z-1],
2110 (*columns[0])[z], (*columns[i1])[z], // top
2111 (*columns[2])[z], (*columns[i3])[z] );
2114 case 6: { // ---------- octahedra
2115 const int iBase1 = isForward ? -1 : 0;
2116 const int iBase2 = isForward ? 0 :-1;
2117 for ( z = 1; z < nbZ; ++z )
2118 helper->AddVolume( (*columns[0])[z+iBase1], (*columns[1])[z+iBase1], // bottom or top
2119 (*columns[2])[z+iBase1], (*columns[3])[z+iBase1],
2120 (*columns[4])[z+iBase1], (*columns[5])[z+iBase1],
2121 (*columns[0])[z+iBase2], (*columns[1])[z+iBase2], // top or bottom
2122 (*columns[2])[z+iBase2], (*columns[3])[z+iBase2],
2123 (*columns[4])[z+iBase2], (*columns[5])[z+iBase2] );
2126 default: // ---------- polyhedra
2127 vector<int> quantities( 2 + nbNodes, 4 );
2128 quantities[0] = quantities[1] = nbNodes;
2129 columns.resize( nbNodes + 1 );
2130 columns[ nbNodes ] = columns[ 0 ];
2131 const int i1 = isForward ? 1 : 3;
2132 const int i3 = isForward ? 3 : 1;
2133 const int iBase1 = isForward ? -1 : 0;
2134 const int iBase2 = isForward ? 0 :-1;
2135 vector<const SMDS_MeshNode*> nodes( 2*nbNodes + 4*nbNodes);
2136 for ( z = 1; z < nbZ; ++z )
2138 for ( size_t i = 0; i < nbNodes; ++i ) {
2139 nodes[ i ] = (*columns[ i ])[z+iBase1]; // bottom or top
2140 nodes[ 2*nbNodes-i-1 ] = (*columns[ i ])[z+iBase2]; // top or bottom
2142 int di = 2*nbNodes + 4*i;
2143 nodes[ di+0 ] = (*columns[i ])[z ];
2144 nodes[ di+i1] = (*columns[i+1])[z ];
2145 nodes[ di+2 ] = (*columns[i+1])[z-1];
2146 nodes[ di+i3] = (*columns[i ])[z-1];
2148 helper->AddPolyhedralVolume( nodes, quantities );
2151 } // switch ( nbNodes )
2156 //================================================================================
2158 * \brief Find correspondence between bottom and top nodes
2159 * If elements on the bottom and top faces are topologically different,
2160 * and projection is possible and allowed, perform the projection
2161 * \retval bool - is a success or not
2163 //================================================================================
2165 bool StdMeshers_Prism_3D::assocOrProjBottom2Top( const gp_Trsf & bottomToTopTrsf,
2166 const Prism_3D::TPrismTopo& thePrism)
2168 SMESH_subMesh * botSM = myHelper->GetMesh()->GetSubMesh( thePrism.myBottom );
2169 SMESH_subMesh * topSM = myHelper->GetMesh()->GetSubMesh( thePrism.myTop );
2171 SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
2172 SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
2174 if ( !botSMDS || botSMDS->NbElements() == 0 )
2176 _gen->Compute( *myHelper->GetMesh(), botSM->GetSubShape(), /*aShapeOnly=*/true );
2177 botSMDS = botSM->GetSubMeshDS();
2178 if ( !botSMDS || botSMDS->NbElements() == 0 )
2179 return toSM( error(TCom("No elements on face #") << botSM->GetId() ));
2182 bool needProject = !topSM->IsMeshComputed();
2183 if ( !needProject &&
2184 (botSMDS->NbElements() != topSMDS->NbElements() ||
2185 botSMDS->NbNodes() != topSMDS->NbNodes()))
2187 MESSAGE("nb elem bot " << botSMDS->NbElements() <<
2188 " top " << ( topSMDS ? topSMDS->NbElements() : 0 ));
2189 MESSAGE("nb node bot " << botSMDS->NbNodes() <<
2190 " top " << ( topSMDS ? topSMDS->NbNodes() : 0 ));
2191 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
2192 <<" and #"<< topSM->GetId() << " seems different" ));
2195 if ( 0/*needProject && !myProjectTriangles*/ )
2196 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
2197 <<" and #"<< topSM->GetId() << " seems different" ));
2198 ///RETURN_BAD_RESULT("Need to project but not allowed");
2200 NSProjUtils::TNodeNodeMap n2nMap;
2201 const NSProjUtils::TNodeNodeMap* n2nMapPtr = & n2nMap;
2204 if ( !projectBottomToTop( bottomToTopTrsf, thePrism ))
2206 n2nMapPtr = & TProjction2dAlgo::instance( this )->GetNodesMap();
2209 if ( !n2nMapPtr || (int) n2nMapPtr->size() < botSMDS->NbNodes() )
2211 // associate top and bottom faces
2212 NSProjUtils::TShapeShapeMap shape2ShapeMap;
2213 const bool sameTopo =
2214 NSProjUtils::FindSubShapeAssociation( thePrism.myBottom, myHelper->GetMesh(),
2215 thePrism.myTop, myHelper->GetMesh(),
2218 for ( size_t iQ = 0; iQ < thePrism.myWallQuads.size(); ++iQ )
2220 const Prism_3D::TQuadList& quadList = thePrism.myWallQuads[iQ];
2221 StdMeshers_FaceSidePtr botSide = quadList.front()->side[ QUAD_BOTTOM_SIDE ];
2222 StdMeshers_FaceSidePtr topSide = quadList.back ()->side[ QUAD_TOP_SIDE ];
2223 if ( botSide->NbEdges() == topSide->NbEdges() )
2225 for ( int iE = 0; iE < botSide->NbEdges(); ++iE )
2227 NSProjUtils::InsertAssociation( botSide->Edge( iE ),
2228 topSide->Edge( iE ), shape2ShapeMap );
2229 NSProjUtils::InsertAssociation( myHelper->IthVertex( 0, botSide->Edge( iE )),
2230 myHelper->IthVertex( 0, topSide->Edge( iE )),
2236 TopoDS_Vertex vb, vt;
2237 StdMeshers_FaceSidePtr sideB, sideT;
2238 vb = myHelper->IthVertex( 0, botSide->Edge( 0 ));
2239 vt = myHelper->IthVertex( 0, topSide->Edge( 0 ));
2240 sideB = quadList.front()->side[ QUAD_LEFT_SIDE ];
2241 sideT = quadList.back ()->side[ QUAD_LEFT_SIDE ];
2242 if ( vb.IsSame( sideB->FirstVertex() ) &&
2243 vt.IsSame( sideT->LastVertex() ))
2245 NSProjUtils::InsertAssociation( botSide->Edge( 0 ),
2246 topSide->Edge( 0 ), shape2ShapeMap );
2247 NSProjUtils::InsertAssociation( vb, vt, shape2ShapeMap );
2249 vb = myHelper->IthVertex( 1, botSide->Edge( botSide->NbEdges()-1 ));
2250 vt = myHelper->IthVertex( 1, topSide->Edge( topSide->NbEdges()-1 ));
2251 sideB = quadList.front()->side[ QUAD_RIGHT_SIDE ];
2252 sideT = quadList.back ()->side[ QUAD_RIGHT_SIDE ];
2253 if ( vb.IsSame( sideB->FirstVertex() ) &&
2254 vt.IsSame( sideT->LastVertex() ))
2256 NSProjUtils::InsertAssociation( botSide->Edge( botSide->NbEdges()-1 ),
2257 topSide->Edge( topSide->NbEdges()-1 ),
2259 NSProjUtils::InsertAssociation( vb, vt, shape2ShapeMap );
2264 // Find matching nodes of top and bottom faces
2265 n2nMapPtr = & n2nMap;
2266 if ( ! NSProjUtils::FindMatchingNodesOnFaces( thePrism.myBottom, myHelper->GetMesh(),
2267 thePrism.myTop, myHelper->GetMesh(),
2268 shape2ShapeMap, n2nMap ))
2271 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
2272 <<" and #"<< topSM->GetId() << " seems different" ));
2274 return toSM( error(TCom("Topology of faces #") << botSM->GetId()
2275 <<" and #"<< topSM->GetId() << " seems different" ));
2279 // Fill myBotToColumnMap
2281 int zSize = myBlock.VerticalSize();
2282 TNodeNodeMap::const_iterator bN_tN = n2nMapPtr->begin();
2283 for ( ; bN_tN != n2nMapPtr->end(); ++bN_tN )
2285 const SMDS_MeshNode* botNode = bN_tN->first;
2286 const SMDS_MeshNode* topNode = bN_tN->second;
2287 if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE &&
2288 myBlock.HasNodeColumn( botNode ))
2289 continue; // wall columns are contained in myBlock
2290 // create node column
2291 Prism_3D::TNode bN( botNode );
2292 TNode2ColumnMap::iterator bN_col =
2293 myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
2294 TNodeColumn & column = bN_col->second;
2295 column.resize( zSize, 0 );
2296 column.front() = botNode;
2297 column.back() = topNode;
2302 //================================================================================
2304 * \brief Remove faces from the top face and re-create them by projection from the bottom
2305 * \retval bool - a success or not
2307 //================================================================================
2309 bool StdMeshers_Prism_3D::projectBottomToTop( const gp_Trsf & bottomToTopTrsf,
2310 const Prism_3D::TPrismTopo& thePrism )
2312 if ( project2dMesh( thePrism.myBottom, thePrism.myTop ))
2316 NSProjUtils::TNodeNodeMap& n2nMap =
2317 (NSProjUtils::TNodeNodeMap&) TProjction2dAlgo::instance( this )->GetNodesMap();
2322 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2323 SMESH_subMesh * botSM = myHelper->GetMesh()->GetSubMesh( thePrism.myBottom );
2324 SMESH_subMesh * topSM = myHelper->GetMesh()->GetSubMesh( thePrism.myTop );
2326 SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
2327 SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
2329 if ( topSMDS && topSMDS->NbElements() > 0 )
2331 //topSM->ComputeStateEngine( SMESH_subMesh::CLEAN ); -- avoid propagation of events
2332 for ( SMDS_ElemIteratorPtr eIt = topSMDS->GetElements(); eIt->more(); )
2333 meshDS->RemoveFreeElement( eIt->next(), topSMDS, /*fromGroups=*/false );
2334 for ( SMDS_NodeIteratorPtr nIt = topSMDS->GetNodes(); nIt->more(); )
2335 meshDS->RemoveFreeNode( nIt->next(), topSMDS, /*fromGroups=*/false );
2338 const TopoDS_Face& botFace = thePrism.myBottom; // oriented within
2339 const TopoDS_Face& topFace = thePrism.myTop; // the 3D SHAPE
2340 int topFaceID = meshDS->ShapeToIndex( thePrism.myTop );
2342 SMESH_MesherHelper botHelper( *myHelper->GetMesh() );
2343 botHelper.SetSubShape( botFace );
2344 botHelper.ToFixNodeParameters( true );
2346 SMESH_MesherHelper topHelper( *myHelper->GetMesh() );
2347 topHelper.SetSubShape( topFace );
2348 topHelper.ToFixNodeParameters( true );
2349 double distXYZ[4], fixTol = 10 * topHelper.MaxTolerance( topFace );
2351 // Fill myBotToColumnMap
2353 int zSize = myBlock.VerticalSize();
2354 Prism_3D::TNode prevTNode;
2355 SMDS_NodeIteratorPtr nIt = botSMDS->GetNodes();
2356 while ( nIt->more() )
2358 const SMDS_MeshNode* botNode = nIt->next();
2359 const SMDS_MeshNode* topNode = 0;
2360 if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
2361 continue; // strange
2363 Prism_3D::TNode bN( botNode );
2364 if ( bottomToTopTrsf.Form() == gp_Identity )
2366 // compute bottom node params
2367 gp_XYZ paramHint(-1,-1,-1);
2368 if ( prevTNode.IsNeighbor( bN ))
2370 paramHint = prevTNode.GetParams();
2371 // double tol = 1e-2 * ( prevTNode.GetCoords() - bN.GetCoords() ).Modulus();
2372 // myBlock.SetTolerance( Min( myBlock.GetTolerance(), tol ));
2374 if ( !myBlock.ComputeParameters( bN.GetCoords(), bN.ChangeParams(),
2375 ID_BOT_FACE, paramHint ))
2376 return toSM( error(TCom("Can't compute normalized parameters for node ")
2377 << botNode->GetID() << " on the face #"<< botSM->GetId() ));
2379 // compute top node coords
2380 gp_XYZ topXYZ; gp_XY topUV;
2381 if ( !myBlock.FacePoint( ID_TOP_FACE, bN.GetParams(), topXYZ ) ||
2382 !myBlock.FaceUV ( ID_TOP_FACE, bN.GetParams(), topUV ))
2383 return toSM( error(TCom("Can't compute coordinates "
2384 "by normalized parameters on the face #")<< topSM->GetId() ));
2385 topNode = meshDS->AddNode( topXYZ.X(),topXYZ.Y(),topXYZ.Z() );
2386 meshDS->SetNodeOnFace( topNode, topFaceID, topUV.X(), topUV.Y() );
2388 else // use bottomToTopTrsf
2390 gp_XYZ coords = bN.GetCoords();
2391 bottomToTopTrsf.Transforms( coords );
2392 topNode = meshDS->AddNode( coords.X(), coords.Y(), coords.Z() );
2393 gp_XY topUV = botHelper.GetNodeUV( botFace, botNode, 0, &checkUV );
2394 meshDS->SetNodeOnFace( topNode, topFaceID, topUV.X(), topUV.Y() );
2396 if ( topHelper.CheckNodeUV( topFace, topNode, topUV, fixTol, /*force=*/false, distXYZ ) &&
2397 distXYZ[0] > fixTol && distXYZ[0] < fixTol * 1e+3 )
2398 meshDS->MoveNode( topNode, distXYZ[1], distXYZ[2], distXYZ[3] ); // transform can be inaccurate
2400 // create node column
2401 TNode2ColumnMap::iterator bN_col =
2402 myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
2403 TNodeColumn & column = bN_col->second;
2404 column.resize( zSize );
2405 column.front() = botNode;
2406 column.back() = topNode;
2408 n2nMap.insert( n2nMap.end(), make_pair( botNode, topNode ));
2410 if ( _computeCanceled )
2411 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
2416 const bool oldSetElemsOnShape = myHelper->SetElementsOnShape( false );
2418 // care of orientation;
2419 // if the bottom faces is orienetd OK then top faces must be reversed
2420 bool reverseTop = true;
2421 if ( myHelper->NbAncestors( botFace, *myBlock.Mesh(), TopAbs_SOLID ) > 1 )
2422 reverseTop = ! myHelper->IsReversedSubMesh( botFace );
2423 int iFrw, iRev, *iPtr = &( reverseTop ? iRev : iFrw );
2425 // loop on bottom mesh faces
2426 SMDS_ElemIteratorPtr faceIt = botSMDS->GetElements();
2427 vector< const SMDS_MeshNode* > nodes;
2428 while ( faceIt->more() )
2430 const SMDS_MeshElement* face = faceIt->next();
2431 if ( !face || face->GetType() != SMDSAbs_Face )
2434 // find top node in columns for each bottom node
2435 int nbNodes = face->NbCornerNodes();
2436 nodes.resize( nbNodes );
2437 for ( iFrw = 0, iRev = nbNodes-1; iFrw < nbNodes; ++iFrw, --iRev )
2439 const SMDS_MeshNode* n = face->GetNode( *iPtr );
2440 if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
2441 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.find( n );
2442 if ( bot_column == myBotToColumnMap.end() )
2443 return toSM( error(TCom("No nodes found above node ") << n->GetID() ));
2444 nodes[ iFrw ] = bot_column->second.back();
2447 const TNodeColumn* column = myBlock.GetNodeColumn( n );
2449 return toSM( error(TCom("No side nodes found above node ") << n->GetID() ));
2450 nodes[ iFrw ] = column->back();
2453 SMDS_MeshElement* newFace = 0;
2454 switch ( nbNodes ) {
2457 newFace = myHelper->AddFace(nodes[0], nodes[1], nodes[2]);
2461 newFace = myHelper->AddFace( nodes[0], nodes[1], nodes[2], nodes[3] );
2465 newFace = meshDS->AddPolygonalFace( nodes );
2468 meshDS->SetMeshElementOnShape( newFace, topFaceID );
2471 myHelper->SetElementsOnShape( oldSetElemsOnShape );
2473 // Check the projected mesh
2475 if ( thePrism.NbWires() > 1 && // there are holes
2476 topHelper.IsDistorted2D( topSM, /*checkUV=*/false ))
2478 SMESH_MeshEditor editor( topHelper.GetMesh() );
2480 // smooth in 2D or 3D?
2481 TopLoc_Location loc;
2482 Handle(Geom_Surface) surface = BRep_Tool::Surface( topFace, loc );
2483 bool isPlanar = GeomLib_IsPlanarSurface( surface ).IsPlanar();
2485 set<const SMDS_MeshNode*> fixedNodes;
2486 TIDSortedElemSet faces;
2487 for ( faceIt = topSMDS->GetElements(); faceIt->more(); )
2488 faces.insert( faces.end(), faceIt->next() );
2491 for ( int isCentroidal = 0; isCentroidal < 2; ++isCentroidal )
2493 SMESH_MeshEditor::SmoothMethod algo =
2494 isCentroidal ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
2496 int nbAttempts = isCentroidal ? 1 : 10;
2497 for ( int iAttemp = 0; iAttemp < nbAttempts; ++iAttemp )
2499 TIDSortedElemSet workFaces = faces;
2502 editor.Smooth( workFaces, fixedNodes, algo, /*nbIterations=*/ 10,
2503 /*theTgtAspectRatio=*/1.0, /*the2D=*/!isPlanar);
2505 if (( isOk = !topHelper.IsDistorted2D( topSM, /*checkUV=*/true )) &&
2511 return toSM( error( TCom("Projection from face #") << botSM->GetId()
2512 << " to face #" << topSM->GetId()
2513 << " failed: inverted elements created"));
2516 TProjction2dAlgo::instance( this )->SetEventListener( topSM );
2521 //=======================================================================
2522 //function : getSweepTolerance
2523 //purpose : Compute tolerance to pass to StdMeshers_Sweeper
2524 //=======================================================================
2526 double StdMeshers_Prism_3D::getSweepTolerance( const Prism_3D::TPrismTopo& thePrism )
2528 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2529 SMESHDS_SubMesh * sm[2] = { meshDS->MeshElements( thePrism.myBottom ),
2530 meshDS->MeshElements( thePrism.myTop ) };
2531 double minDist = 1e100;
2533 vector< SMESH_TNodeXYZ > nodes;
2534 for ( int iSM = 0; iSM < 2; ++iSM )
2536 if ( !sm[ iSM ]) continue;
2538 SMDS_ElemIteratorPtr fIt = sm[ iSM ]->GetElements();
2539 while ( fIt->more() )
2541 const SMDS_MeshElement* face = fIt->next();
2542 const int nbNodes = face->NbCornerNodes();
2543 SMDS_ElemIteratorPtr nIt = face->nodesIterator();
2545 nodes.resize( nbNodes + 1 );
2546 for ( int iN = 0; iN < nbNodes; ++iN )
2547 nodes[ iN ] = nIt->next();
2548 nodes.back() = nodes[0];
2552 for ( int iN = 0; iN < nbNodes; ++iN )
2554 if ( nodes[ iN ]._node->GetPosition()->GetDim() < 2 &&
2555 nodes[ iN+1 ]._node->GetPosition()->GetDim() < 2 )
2557 // it's a boundary link; measure distance of other
2558 // nodes to this link
2559 gp_XYZ linkDir = nodes[ iN ] - nodes[ iN+1 ];
2560 double linkLen = linkDir.Modulus();
2561 bool isDegen = ( linkLen < numeric_limits<double>::min() );
2562 if ( !isDegen ) linkDir /= linkLen;
2563 for ( int iN2 = 0; iN2 < nbNodes; ++iN2 ) // loop on other nodes
2565 if ( nodes[ iN2 ] == nodes[ iN ] ||
2566 nodes[ iN2 ] == nodes[ iN+1 ]) continue;
2569 dist2 = ( nodes[ iN ] - nodes[ iN2 ]).SquareModulus();
2573 dist2 = linkDir.CrossSquareMagnitude( nodes[ iN ] - nodes[ iN2 ]);
2575 if ( dist2 > numeric_limits<double>::min() )
2576 minDist = Min ( minDist, dist2 );
2579 // measure length link
2580 else if ( nodes[ iN ]._node < nodes[ iN+1 ]._node ) // not to measure same link twice
2582 dist2 = ( nodes[ iN ] - nodes[ iN+1 ]).SquareModulus();
2583 if ( dist2 > numeric_limits<double>::min() )
2584 minDist = Min ( minDist, dist2 );
2589 return 0.1 * Sqrt ( minDist );
2592 //=======================================================================
2593 //function : isSimpleQuad
2594 //purpose : check if the bottom FACE is meshable with nice quadrangles,
2595 // if so the block aproach can work rather fast.
2596 // This is a temporary mean caused by problems in StdMeshers_Sweeper
2597 //=======================================================================
2599 bool StdMeshers_Prism_3D::isSimpleBottom( const Prism_3D::TPrismTopo& thePrism )
2601 if ( thePrism.myNbEdgesInWires.front() != 4 )
2604 // analyse angles between edges
2605 double nbConcaveAng = 0, nbConvexAng = 0;
2606 TopoDS_Face reverseBottom = TopoDS::Face( thePrism.myBottom.Reversed() ); // see initPrism()
2607 TopoDS_Vertex commonV;
2608 const list< TopoDS_Edge >& botEdges = thePrism.myBottomEdges;
2609 list< TopoDS_Edge >::const_iterator edge = botEdges.begin();
2610 while ( edge != botEdges.end() )
2612 if ( SMESH_Algo::isDegenerated( *edge ))
2614 TopoDS_Edge e1 = *edge++;
2615 TopoDS_Edge e2 = ( edge == botEdges.end() ? botEdges.front() : *edge );
2616 if ( ! TopExp::CommonVertex( e1, e2, commonV ))
2618 e2 = botEdges.front();
2619 if ( ! TopExp::CommonVertex( e1, e2, commonV ))
2622 double angle = myHelper->GetAngle( e1, e2, reverseBottom, commonV );
2623 if ( angle < -5 * M_PI/180 )
2624 if ( ++nbConcaveAng > 1 )
2626 if ( angle > 85 * M_PI/180 )
2627 if ( ++nbConvexAng > 4 )
2633 //=======================================================================
2634 //function : allVerticalEdgesStraight
2635 //purpose : Defines if all "vertical" EDGEs are straight
2636 //=======================================================================
2638 bool StdMeshers_Prism_3D::allVerticalEdgesStraight( const Prism_3D::TPrismTopo& thePrism )
2640 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
2642 const Prism_3D::TQuadList& quads = thePrism.myWallQuads[i];
2643 Prism_3D::TQuadList::const_iterator quadIt = quads.begin();
2644 TopoDS_Edge prevQuadEdge;
2645 for ( ; quadIt != quads.end(); ++quadIt )
2647 StdMeshers_FaceSidePtr rightSide = (*quadIt)->side[ QUAD_RIGHT_SIDE ];
2649 if ( !prevQuadEdge.IsNull() &&
2650 !SMESH_Algo::IsContinuous( rightSide->Edge( 0 ), prevQuadEdge ))
2653 for ( int iE = 0; iE < rightSide->NbEdges(); ++iE )
2655 const TopoDS_Edge & rightE = rightSide->Edge( iE );
2656 if ( !SMESH_Algo::IsStraight( rightE, /*degenResult=*/true ))
2660 !SMESH_Algo::IsContinuous( rightSide->Edge( iE-1 ), rightE ))
2663 prevQuadEdge = rightE;
2670 //=======================================================================
2671 //function : project2dMesh
2672 //purpose : Project mesh faces from a source FACE of one prism (theSrcFace)
2673 // to a source FACE of another prism (theTgtFace)
2674 //=======================================================================
2676 bool StdMeshers_Prism_3D::project2dMesh(const TopoDS_Face& theSrcFace,
2677 const TopoDS_Face& theTgtFace)
2679 TProjction2dAlgo* projector2D = TProjction2dAlgo::instance( this );
2680 projector2D->myHyp.SetSourceFace( theSrcFace );
2681 bool ok = projector2D->Compute( *myHelper->GetMesh(), theTgtFace );
2683 SMESH_subMesh* tgtSM = myHelper->GetMesh()->GetSubMesh( theTgtFace );
2684 if ( !ok && tgtSM->GetSubMeshDS() ) {
2685 //tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN ); -- avoid propagation of events
2686 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2687 SMESHDS_SubMesh* tgtSMDS = tgtSM->GetSubMeshDS();
2688 for ( SMDS_ElemIteratorPtr eIt = tgtSMDS->GetElements(); eIt->more(); )
2689 meshDS->RemoveFreeElement( eIt->next(), tgtSMDS, /*fromGroups=*/false );
2690 for ( SMDS_NodeIteratorPtr nIt = tgtSMDS->GetNodes(); nIt->more(); )
2691 meshDS->RemoveFreeNode( nIt->next(), tgtSMDS, /*fromGroups=*/false );
2693 tgtSM->ComputeStateEngine ( SMESH_subMesh::CHECK_COMPUTE_STATE );
2694 tgtSM->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
2696 projector2D->SetEventListener( tgtSM );
2701 //================================================================================
2703 * \brief Set projection coordinates of a node to a face and it's sub-shapes
2704 * \param faceID - the face given by in-block ID
2705 * \param params - node normalized parameters
2706 * \retval bool - is a success
2708 //================================================================================
2710 bool StdMeshers_Prism_3D::setFaceAndEdgesXYZ( const int faceID, const gp_XYZ& params, int z )
2712 // find base and top edges of the face
2713 enum { BASE = 0, TOP, LEFT, RIGHT };
2714 vector< int > edgeVec; // 0-base, 1-top
2715 SMESH_Block::GetFaceEdgesIDs( faceID, edgeVec );
2717 myBlock.EdgePoint( edgeVec[ BASE ], params, myShapeXYZ[ edgeVec[ BASE ]]);
2718 myBlock.EdgePoint( edgeVec[ TOP ], params, myShapeXYZ[ edgeVec[ TOP ]]);
2720 SHOWYXZ("\nparams ", params);
2721 SHOWYXZ("TOP is " <<edgeVec[ TOP ], myShapeXYZ[ edgeVec[ TOP]]);
2722 SHOWYXZ("BASE is "<<edgeVec[ BASE], myShapeXYZ[ edgeVec[ BASE]]);
2724 if ( faceID == SMESH_Block::ID_Fx0z || faceID == SMESH_Block::ID_Fx1z )
2726 myBlock.EdgePoint( edgeVec[ LEFT ], params, myShapeXYZ[ edgeVec[ LEFT ]]);
2727 myBlock.EdgePoint( edgeVec[ RIGHT ], params, myShapeXYZ[ edgeVec[ RIGHT ]]);
2729 SHOWYXZ("VER "<<edgeVec[ LEFT], myShapeXYZ[ edgeVec[ LEFT]]);
2730 SHOWYXZ("VER "<<edgeVec[ RIGHT], myShapeXYZ[ edgeVec[ RIGHT]]);
2732 myBlock.FacePoint( faceID, params, myShapeXYZ[ faceID ]);
2733 SHOWYXZ("FacePoint "<<faceID, myShapeXYZ[ faceID]);
2738 //=======================================================================
2740 //purpose : If (!isOK), sets the error to a sub-mesh of a current SOLID
2741 //=======================================================================
2743 bool StdMeshers_Prism_3D::toSM( bool isOK )
2745 if ( mySetErrorToSM &&
2748 !myHelper->GetSubShape().IsNull() &&
2749 myHelper->GetSubShape().ShapeType() == TopAbs_SOLID)
2751 SMESH_subMesh* sm = myHelper->GetMesh()->GetSubMesh( myHelper->GetSubShape() );
2752 sm->GetComputeError() = this->GetComputeError();
2753 // clear error in order not to return it twice
2754 _error = COMPERR_OK;
2760 //=======================================================================
2761 //function : shapeID
2762 //purpose : Return index of a shape
2763 //=======================================================================
2765 int StdMeshers_Prism_3D::shapeID( const TopoDS_Shape& S )
2767 if ( S.IsNull() ) return 0;
2768 if ( !myHelper ) return -3;
2769 return myHelper->GetMeshDS()->ShapeToIndex( S );
2772 namespace // utils used by StdMeshers_Prism_3D::IsApplicable()
2774 struct EdgeWithNeighbors
2777 int _iBase; /* index in a WIRE with non-base EDGEs excluded */
2778 int _iL, _iR; /* used to connect edges in a base FACE */
2779 bool _isBase; /* is used in a base FACE */
2780 EdgeWithNeighbors(const TopoDS_Edge& E, int iE, int nbE, int shift, bool isBase ):
2781 _edge( E ), _iBase( iE + shift ),
2782 _iL( SMESH_MesherHelper::WrapIndex( iE-1, Max( 1, nbE )) + shift ),
2783 _iR( SMESH_MesherHelper::WrapIndex( iE+1, Max( 1, nbE )) + shift ),
2787 EdgeWithNeighbors() {}
2788 bool IsInternal() const { return !_edge.IsNull() && _edge.Orientation() == TopAbs_INTERNAL; }
2790 // PrismSide contains all FACEs linking a bottom EDGE with a top one.
2793 TopoDS_Face _face; // a currently treated upper FACE
2794 TopTools_IndexedMapOfShape *_faces; // all FACEs (pointer because of a private copy constructor)
2795 TopoDS_Edge _topEdge; // a current top EDGE
2796 vector< EdgeWithNeighbors >*_edges; // all EDGEs of _face
2797 int _iBotEdge; // index of _topEdge within _edges
2798 vector< bool > _isCheckedEdge; // mark EDGEs whose two owner FACEs found
2799 int _nbCheckedEdges; // nb of EDGEs whose location is defined
2800 PrismSide *_leftSide; // neighbor sides
2801 PrismSide *_rightSide;
2802 bool _isInternal; // whether this side raises from an INTERNAL EDGE
2803 void SetExcluded() { _leftSide = _rightSide = NULL; }
2804 bool IsExcluded() const { return !_leftSide; }
2805 const TopoDS_Edge& Edge( int i ) const
2807 return (*_edges)[ i ]._edge;
2809 int FindEdge( const TopoDS_Edge& E ) const
2811 for ( size_t i = 0; i < _edges->size(); ++i )
2812 if ( E.IsSame( Edge( i ))) return i;
2815 bool IsSideFace( const TopoDS_Shape& face, const bool checkNeighbors ) const
2817 if ( _faces->Contains( face )) // avoid returning true for a prism top FACE
2818 return ( !_face.IsNull() || !( face.IsSame( _faces->FindKey( _faces->Extent() ))));
2820 if ( checkNeighbors )
2821 return (( _leftSide && _leftSide->IsSideFace ( face, false )) ||
2822 ( _rightSide && _rightSide->IsSideFace( face, false )));
2827 //--------------------------------------------------------------------------------
2829 * \brief Return another faces sharing an edge
2831 const TopoDS_Face & getAnotherFace( const TopoDS_Face& face,
2832 const TopoDS_Edge& edge,
2833 TopTools_IndexedDataMapOfShapeListOfShape& facesOfEdge)
2835 TopTools_ListIteratorOfListOfShape faceIt( facesOfEdge.FindFromKey( edge ));
2836 for ( ; faceIt.More(); faceIt.Next() )
2837 if ( !face.IsSame( faceIt.Value() ))
2838 return TopoDS::Face( faceIt.Value() );
2842 //--------------------------------------------------------------------------------
2844 * \brief Return ordered edges of a face
2846 bool getEdges( const TopoDS_Face& face,
2847 vector< EdgeWithNeighbors > & edges,
2848 TopTools_IndexedDataMapOfShapeListOfShape& facesOfEdge,
2849 const bool noHolesAllowed)
2851 TopoDS_Face f = face;
2852 if ( f.Orientation() != TopAbs_FORWARD &&
2853 f.Orientation() != TopAbs_REVERSED )
2854 f.Orientation( TopAbs_FORWARD );
2855 list< TopoDS_Edge > ee;
2856 list< int > nbEdgesInWires;
2857 int nbW = SMESH_Block::GetOrderedEdges( f, ee, nbEdgesInWires );
2858 if ( nbW > 1 && noHolesAllowed )
2861 int iE, nbTot = 0, nbBase, iBase;
2862 list< TopoDS_Edge >::iterator e = ee.begin();
2863 list< int >::iterator nbE = nbEdgesInWires.begin();
2864 for ( ; nbE != nbEdgesInWires.end(); ++nbE )
2865 for ( iE = 0; iE < *nbE; ++e, ++iE )
2866 if ( SMESH_Algo::isDegenerated( *e )) // degenerated EDGE is never used
2868 e = --ee.erase( e );
2876 for ( nbE = nbEdgesInWires.begin(); nbE != nbEdgesInWires.end(); ++nbE )
2879 isBase.resize( *nbE );
2880 list< TopoDS_Edge >::iterator eIt = e;
2881 for ( iE = 0; iE < *nbE; ++eIt, ++iE )
2883 isBase[ iE ] = ( getAnotherFace( face, *eIt, facesOfEdge ) != face );
2884 nbBase += isBase[ iE ];
2886 for ( iBase = 0, iE = 0; iE < *nbE; ++e, ++iE )
2888 edges.push_back( EdgeWithNeighbors( *e, iBase, nbBase, nbTot, isBase[ iE ] ));
2889 iBase += isBase[ iE ];
2896 // IPAL53099. Set correct neighbors to INTERNAL EDGEs, which can be connected to
2897 // EDGEs of the outer WIRE but this fact can't be detected by their order.
2900 int iFirst = 0, iLast;
2901 for ( nbE = nbEdgesInWires.begin(); nbE != nbEdgesInWires.end(); ++nbE )
2903 iLast = iFirst + *nbE - 1;
2904 TopoDS_Vertex vv[2] = { SMESH_MesherHelper::IthVertex( 0, edges[ iFirst ]._edge ),
2905 SMESH_MesherHelper::IthVertex( 1, edges[ iLast ]._edge ) };
2906 bool isConnectOk = ( vv[0].IsSame( vv[1] ));
2909 edges[ iFirst ]._iL = edges[ iFirst ]._iBase; // connect to self
2910 edges[ iLast ]._iR = edges[ iLast ]._iBase;
2912 // look for an EDGE of the outer WIREs connected to vv
2913 TopoDS_Vertex v0, v1;
2914 for ( iE = 0; iE < iFirst; ++iE )
2916 v0 = SMESH_MesherHelper::IthVertex( 0, edges[ iE ]._edge );
2917 v1 = SMESH_MesherHelper::IthVertex( 1, edges[ iE ]._edge );
2918 if ( vv[0].IsSame( v0 ) || vv[0].IsSame( v1 ))
2919 edges[ iFirst ]._iL = edges[ iE ]._iBase;
2920 if ( vv[1].IsSame( v0 ) || vv[1].IsSame( v1 ))
2921 edges[ iLast ]._iR = edges[ iE ]._iBase;
2927 return edges.size();