1 // Copyright (C) 2007-2015 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>
55 #include <TopExp_Explorer.hxx>
56 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #include <TopTools_ListOfShape.hxx>
58 #include <TopTools_MapOfShape.hxx>
59 #include <TopTools_SequenceOfShape.hxx>
68 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
69 #define gpXYZ(n) SMESH_TNodeXYZ(n)
72 #define DBGOUT(msg) //cout << msg << endl;
73 #define SHOWYXZ(msg, xyz) \
74 //{ gp_Pnt p (xyz); cout << msg << " ("<< p.X() << "; " <<p.Y() << "; " <<p.Z() << ") " <<endl; }
77 #define SHOWYXZ(msg, xyz)
80 namespace NSProjUtils = StdMeshers_ProjectionUtils;
82 typedef SMESH_Comment TCom;
84 enum { ID_BOT_FACE = SMESH_Block::ID_Fxy0,
85 ID_TOP_FACE = SMESH_Block::ID_Fxy1,
86 BOTTOM_EDGE = 0, TOP_EDGE, V0_EDGE, V1_EDGE, // edge IDs in face
87 NB_WALL_FACES = 4 }; //
91 //=======================================================================
93 * \brief Quadrangle algorithm
95 struct TQuadrangleAlgo : public StdMeshers_Quadrangle_2D
97 TQuadrangleAlgo(int studyId, SMESH_Gen* gen)
98 : StdMeshers_Quadrangle_2D( gen->GetANewId(), studyId, gen)
101 static StdMeshers_Quadrangle_2D* instance( SMESH_Algo* fatherAlgo,
102 SMESH_MesherHelper* helper=0)
104 static TQuadrangleAlgo* algo = new TQuadrangleAlgo( fatherAlgo->GetStudyId(),
105 fatherAlgo->GetGen() );
108 algo->myProxyMesh->GetMesh() != helper->GetMesh() )
109 algo->myProxyMesh.reset( new SMESH_ProxyMesh( *helper->GetMesh() ));
111 algo->myQuadList.clear();
114 algo->_quadraticMesh = helper->GetIsQuadratic();
119 //=======================================================================
121 * \brief Algorithm projecting 1D mesh
123 struct TProjction1dAlgo : public StdMeshers_Projection_1D
125 StdMeshers_ProjectionSource1D myHyp;
127 TProjction1dAlgo(int studyId, SMESH_Gen* gen)
128 : StdMeshers_Projection_1D( gen->GetANewId(), studyId, gen),
129 myHyp( gen->GetANewId(), studyId, gen)
131 StdMeshers_Projection_1D::_sourceHypo = & myHyp;
133 static TProjction1dAlgo* instance( SMESH_Algo* fatherAlgo )
135 static TProjction1dAlgo* algo = new TProjction1dAlgo( fatherAlgo->GetStudyId(),
136 fatherAlgo->GetGen() );
140 //=======================================================================
142 * \brief Algorithm projecting 2D mesh
144 struct TProjction2dAlgo : public StdMeshers_Projection_1D2D
146 StdMeshers_ProjectionSource2D myHyp;
148 TProjction2dAlgo(int studyId, SMESH_Gen* gen)
149 : StdMeshers_Projection_1D2D( gen->GetANewId(), studyId, gen),
150 myHyp( gen->GetANewId(), studyId, gen)
152 StdMeshers_Projection_2D::_sourceHypo = & myHyp;
154 static TProjction2dAlgo* instance( SMESH_Algo* fatherAlgo )
156 static TProjction2dAlgo* algo = new TProjction2dAlgo( fatherAlgo->GetStudyId(),
157 fatherAlgo->GetGen() );
160 const NSProjUtils::TNodeNodeMap& GetNodesMap()
162 return _src2tgtNodes;
165 //=======================================================================
167 * \brief Returns already computed EDGEs
169 void getPrecomputedEdges( SMESH_MesherHelper& theHelper,
170 const TopoDS_Shape& theShape,
171 vector< TopoDS_Edge >& theEdges)
175 SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
178 TopTools_IndexedMapOfShape edges;
179 TopExp::MapShapes( theShape, TopAbs_EDGE, edges );
180 for ( int iE = 1; iE <= edges.Extent(); ++iE )
182 const TopoDS_Shape edge = edges( iE );
183 if (( ! ( sm = meshDS->MeshElements( edge ))) ||
184 ( sm->NbElements() == 0 ))
187 // there must not be FACEs meshed with triangles and sharing a computed EDGE
188 // as the precomputed EDGEs are used for propagation other to 'vertical' EDGEs
189 bool faceFound = false;
190 PShapeIteratorPtr faceIt =
191 theHelper.GetAncestors( edge, *theHelper.GetMesh(), TopAbs_FACE );
192 while ( const TopoDS_Shape* face = faceIt->next() )
194 if (( sm = meshDS->MeshElements( *face )) &&
195 ( sm->NbElements() > 0 ) &&
196 ( !theHelper.IsSameElemGeometry( sm, SMDSGeom_QUADRANGLE ) ))
202 theEdges.push_back( TopoDS::Edge( edge ));
206 //================================================================================
208 * \brief Make \a botE be the BOTTOM_SIDE of \a quad.
209 * Return false if the BOTTOM_SIDE is composite
211 //================================================================================
213 bool setBottomEdge( const TopoDS_Edge& botE,
214 FaceQuadStruct::Ptr& quad,
215 const TopoDS_Shape& face)
217 quad->side[ QUAD_TOP_SIDE ].grid->Reverse();
218 quad->side[ QUAD_LEFT_SIDE ].grid->Reverse();
220 bool isComposite = false;
221 for ( size_t i = 0; i < quad->side.size(); ++i )
223 StdMeshers_FaceSidePtr quadSide = quad->side[i];
224 for ( int iE = 0; iE < quadSide->NbEdges(); ++iE )
225 if ( botE.IsSame( quadSide->Edge( iE )))
227 if ( quadSide->NbEdges() > 1 )
228 isComposite = true; //return false;
230 i = quad->side.size(); // to quit from the outer loop
234 if ( edgeIndex != QUAD_BOTTOM_SIDE )
235 quad->shift( quad->side.size() - edgeIndex, /*keepUnitOri=*/false );
237 quad->face = TopoDS::Face( face );
242 //================================================================================
244 * \brief Return iterator pointing to node column for the given parameter
245 * \param columnsMap - node column map
246 * \param parameter - parameter
247 * \retval TParam2ColumnMap::iterator - result
249 * it returns closest left column
251 //================================================================================
253 TParam2ColumnIt getColumn( const TParam2ColumnMap* columnsMap,
254 const double parameter )
256 TParam2ColumnIt u_col = columnsMap->upper_bound( parameter );
257 if ( u_col != columnsMap->begin() )
259 return u_col; // return left column
262 //================================================================================
264 * \brief Return nodes around given parameter and a ratio
265 * \param column - node column
266 * \param param - parameter
267 * \param node1 - lower node
268 * \param node2 - upper node
269 * \retval double - ratio
271 //================================================================================
273 double getRAndNodes( const TNodeColumn* column,
275 const SMDS_MeshNode* & node1,
276 const SMDS_MeshNode* & node2)
278 if ( param >= 1.0 || column->size() == 1) {
279 node1 = node2 = column->back();
283 int i = int( param * ( column->size() - 1 ));
284 double u0 = double( i )/ double( column->size() - 1 );
285 double r = ( param - u0 ) * ( column->size() - 1 );
287 node1 = (*column)[ i ];
288 node2 = (*column)[ i + 1];
292 //================================================================================
294 * \brief Compute boundary parameters of face parts
295 * \param nbParts - nb of parts to split columns into
296 * \param columnsMap - node columns of the face to split
297 * \param params - computed parameters
299 //================================================================================
301 void splitParams( const int nbParts,
302 const TParam2ColumnMap* columnsMap,
303 vector< double > & params)
306 params.reserve( nbParts + 1 );
307 TParam2ColumnIt last_par_col = --columnsMap->end();
308 double par = columnsMap->begin()->first; // 0.
309 double parLast = last_par_col->first;
310 params.push_back( par );
311 for ( int i = 0; i < nbParts - 1; ++ i )
313 double partSize = ( parLast - par ) / double ( nbParts - i );
314 TParam2ColumnIt par_col = getColumn( columnsMap, par + partSize );
315 if ( par_col->first == par ) {
317 if ( par_col == last_par_col ) {
318 while ( i < nbParts - 1 )
319 params.push_back( par + partSize * i++ );
323 par = par_col->first;
324 params.push_back( par );
326 params.push_back( parLast ); // 1.
329 //================================================================================
331 * \brief Return coordinate system for z-th layer of nodes
333 //================================================================================
335 gp_Ax2 getLayerCoordSys(const int z,
336 const vector< const TNodeColumn* >& columns,
339 // gravity center of a layer
342 for ( size_t i = 0; i < columns.size(); ++i )
344 O += gpXYZ( (*columns[ i ])[ z ]);
345 if ( vertexCol < 0 &&
346 columns[ i ]->front()->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
353 int iPrev = columns.size()-1;
354 for ( size_t i = 0; i < columns.size(); ++i )
356 gp_Vec v1( O, gpXYZ( (*columns[ iPrev ])[ z ]));
357 gp_Vec v2( O, gpXYZ( (*columns[ i ] )[ z ]));
362 if ( vertexCol >= 0 )
364 O = gpXYZ( (*columns[ vertexCol ])[ z ]);
366 if ( xColumn < 0 || xColumn >= (int) columns.size() )
368 // select a column for X dir
370 for ( size_t i = 0; i < columns.size(); ++i )
372 double dist = ( O - gpXYZ((*columns[ i ])[ z ])).SquareModulus();
373 if ( dist > maxDist )
382 gp_Vec X( O, gpXYZ( (*columns[ xColumn ])[ z ]));
384 return gp_Ax2( O, Z, X);
387 //================================================================================
389 * \brief Removes submeshes that are or can be meshed with regular grid from given list
390 * \retval int - nb of removed submeshes
392 //================================================================================
394 int removeQuasiQuads(list< SMESH_subMesh* >& notQuadSubMesh,
395 SMESH_MesherHelper* helper,
396 StdMeshers_Quadrangle_2D* quadAlgo)
399 //SMESHDS_Mesh* mesh = notQuadSubMesh.front()->GetFather()->GetMeshDS();
400 list< SMESH_subMesh* >::iterator smIt = notQuadSubMesh.begin();
401 while ( smIt != notQuadSubMesh.end() )
403 SMESH_subMesh* faceSm = *smIt;
404 SMESHDS_SubMesh* faceSmDS = faceSm->GetSubMeshDS();
405 int nbQuads = faceSmDS ? faceSmDS->NbElements() : 0;
408 toRemove = helper->IsStructured( faceSm );
410 toRemove = ( quadAlgo->CheckNbEdges( *helper->GetMesh(),
411 faceSm->GetSubShape() ) != NULL );
412 nbRemoved += toRemove;
414 smIt = notQuadSubMesh.erase( smIt );
422 //================================================================================
424 * \brief Return and angle between two EDGEs
425 * \return double - the angle normalized so that
432 //================================================================================
434 // double normAngle(const TopoDS_Edge & E1, const TopoDS_Edge & E2, const TopoDS_Face & F)
436 // return SMESH_MesherHelper::GetAngle( E1, E2, F ) / ( 0.5 * M_PI );
439 //================================================================================
441 * Consider continuous straight EDGES as one side - mark them to unite
443 //================================================================================
445 int countNbSides( const Prism_3D::TPrismTopo & thePrism,
446 vector<int> & nbUnitePerEdge,
447 vector< double > & edgeLength)
449 int nbEdges = thePrism.myNbEdgesInWires.front(); // nb outer edges
450 int nbSides = nbEdges;
453 list< TopoDS_Edge >::const_iterator edgeIt = thePrism.myBottomEdges.begin();
454 std::advance( edgeIt, nbEdges-1 );
455 TopoDS_Edge prevE = *edgeIt;
456 // bool isPrevStraight = SMESH_Algo::IsStraight( prevE );
457 // int iPrev = nbEdges - 1;
459 // int iUnite = -1; // the first of united EDGEs
461 // analyse angles between EDGEs
463 vector< bool > isCorner( nbEdges );
464 edgeIt = thePrism.myBottomEdges.begin();
465 for ( int iE = 0; iE < nbEdges; ++iE, ++edgeIt )
467 const TopoDS_Edge& curE = *edgeIt;
468 edgeLength[ iE ] = SMESH_Algo::EdgeLength( curE );
470 // double normAngle = normAngle( prevE, curE, thePrism.myBottom );
471 // isCorner[ iE ] = false;
472 // if ( normAngle < 2.0 )
474 // if ( normAngle < 0.001 ) // straight or obtuse angle
476 // // unite EDGEs in order not to put a corner of the unit quadrangle at this VERTEX
479 // nbUnitePerEdge[ iUnite ]++;
480 // nbUnitePerEdge[ iE ] = -1;
485 // isCorner[ iE ] = true;
495 // define which of corners to put on a side of the unit quadrangle
497 // edgeIt = thePrism.myBottomEdges.begin();
498 // for ( int iE = 0; iE < nbEdges; ++iE, ++edgeIt )
500 // const TopoDS_Edge& curE = *edgeIt;
501 // edgeLength[ iE ] = SMESH_Algo::EdgeLength( curE );
503 // const bool isCurStraight = SMESH_Algo::IsStraight( curE );
504 // if ( isPrevStraight && isCurStraight && SMESH_Algo::IsContinuous( prevE, curE ))
508 // nbUnitePerEdge[ iUnite ]++;
509 // nbUnitePerEdge[ iE ] = -1;
517 // isPrevStraight = isCurStraight;
524 void pointsToPython(const std::vector<gp_XYZ>& p)
527 for ( size_t i = SMESH_Block::ID_V000; i < p.size(); ++i )
529 cout << "mesh.AddNode( " << p[i].X() << ", "<< p[i].Y() << ", "<< p[i].Z() << ") # " << i <<" " ;
530 SMESH_Block::DumpShapeID( i, cout ) << endl;
536 //=======================================================================
537 //function : StdMeshers_Prism_3D
539 //=======================================================================
541 StdMeshers_Prism_3D::StdMeshers_Prism_3D(int hypId, int studyId, SMESH_Gen* gen)
542 :SMESH_3D_Algo(hypId, studyId, gen)
545 _shapeType = (1 << TopAbs_SOLID); // 1 bit per shape type
546 _onlyUnaryInput = false; // mesh all SOLIDs at once
547 _requireDiscreteBoundary = false; // mesh FACEs and EDGEs by myself
548 _supportSubmeshes = true; // "source" FACE must be meshed by other algo
549 _neededLowerHyps[ 1 ] = true; // suppress warning on hiding a global 1D algo
550 _neededLowerHyps[ 2 ] = true; // suppress warning on hiding a global 2D algo
552 //myProjectTriangles = false;
553 mySetErrorToSM = true; // to pass an error to a sub-mesh of a current solid or not
556 //================================================================================
560 //================================================================================
562 StdMeshers_Prism_3D::~StdMeshers_Prism_3D()
565 //=======================================================================
566 //function : CheckHypothesis
568 //=======================================================================
570 bool StdMeshers_Prism_3D::CheckHypothesis(SMESH_Mesh& aMesh,
571 const TopoDS_Shape& aShape,
572 SMESH_Hypothesis::Hypothesis_Status& aStatus)
574 // Check shape geometry
576 aStatus = SMESH_Hypothesis::HYP_BAD_GEOMETRY;
578 // find not quadrangle faces
579 list< TopoDS_Shape > notQuadFaces;
580 int nbEdge, nbWire, nbFace = 0;
581 TopExp_Explorer exp( aShape, TopAbs_FACE );
582 for ( ; exp.More(); exp.Next() ) {
584 const TopoDS_Shape& face = exp.Current();
585 nbEdge = NSProjUtils::Count( face, TopAbs_EDGE, 0 );
586 nbWire = NSProjUtils::Count( face, TopAbs_WIRE, 0 );
587 if ( nbEdge!= 4 || nbWire!= 1 ) {
588 if ( !notQuadFaces.empty() ) {
589 if ( NSProjUtils::Count( notQuadFaces.back(), TopAbs_EDGE, 0 ) != nbEdge ||
590 NSProjUtils::Count( notQuadFaces.back(), TopAbs_WIRE, 0 ) != nbWire )
591 RETURN_BAD_RESULT("Different not quad faces");
593 notQuadFaces.push_back( face );
596 if ( !notQuadFaces.empty() )
598 if ( notQuadFaces.size() != 2 )
599 RETURN_BAD_RESULT("Bad nb not quad faces: " << notQuadFaces.size());
601 // check total nb faces
602 nbEdge = NSProjUtils::Count( notQuadFaces.back(), TopAbs_EDGE, 0 );
603 if ( nbFace != nbEdge + 2 )
604 RETURN_BAD_RESULT("Bad nb of faces: " << nbFace << " but must be " << nbEdge + 2);
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 );
622 int nbSolids = helper.Count( theShape, TopAbs_SOLID, /*skipSame=*/false );
626 TopTools_IndexedDataMapOfShapeListOfShape faceToSolids;
627 TopExp::MapShapesAndAncestors( theShape, TopAbs_FACE, TopAbs_SOLID, faceToSolids );
629 // look for meshed FACEs ("source" FACEs) that must be prism bottoms
630 list< TopoDS_Face > meshedFaces, notQuadMeshedFaces, notQuadFaces;
631 const bool meshHasQuads = ( theMesh.NbQuadrangles() > 0 );
632 //StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this );
633 for ( int iF = 1; iF <= faceToSolids.Extent(); ++iF )
635 const TopoDS_Face& face = TopoDS::Face( faceToSolids.FindKey( iF ));
636 SMESH_subMesh* faceSM = theMesh.GetSubMesh( face );
637 if ( !faceSM->IsEmpty() )
639 if ( !meshHasQuads ||
640 !helper.IsSameElemGeometry( faceSM->GetSubMeshDS(), SMDSGeom_QUADRANGLE ) ||
641 !helper.IsStructured( faceSM )
643 notQuadMeshedFaces.push_front( face );
644 else if ( myHelper->Count( face, TopAbs_EDGE, /*ignoreSame=*/false ) != 4 )
645 meshedFaces.push_front( face );
647 meshedFaces.push_back( face );
649 // not add not quadrilateral FACE as we can't compute it
650 // else if ( !quadAlgo->CheckNbEdges( theMesh, face ))
651 // // not add not quadrilateral FACE as it can be a prism side
652 // // else if ( myHelper->Count( face, TopAbs_EDGE, /*ignoreSame=*/false ) != 4 )
654 // notQuadFaces.push_back( face );
657 // notQuadFaces are of medium priority, put them before ordinary meshed faces
658 meshedFaces.splice( meshedFaces.begin(), notQuadFaces );
659 // notQuadMeshedFaces are of highest priority, put them before notQuadFaces
660 meshedFaces.splice( meshedFaces.begin(), notQuadMeshedFaces );
662 Prism_3D::TPrismTopo prism;
664 bool selectBottom = meshedFaces.empty();
668 TopoDS_Shape solid = TopExp_Explorer( theShape, TopAbs_SOLID ).Current();
669 if ( !meshedFaces.empty() )
670 prism.myBottom = meshedFaces.front();
671 return ( initPrism( prism, solid, selectBottom ) &&
675 // find propagation chains from already computed EDGEs
676 vector< TopoDS_Edge > computedEdges;
677 getPrecomputedEdges( helper, theShape, computedEdges );
678 myPropagChains = new TopTools_IndexedMapOfShape[ computedEdges.size() + 1 ];
679 SMESHUtils::ArrayDeleter< TopTools_IndexedMapOfShape > pcDel( myPropagChains );
680 for ( size_t i = 0, nb = 0; i < computedEdges.size(); ++i )
682 StdMeshers_ProjectionUtils::GetPropagationEdge( &theMesh, TopoDS_Edge(),
683 computedEdges[i], myPropagChains + nb );
684 if ( myPropagChains[ nb ].Extent() < 2 ) // an empty map is a termination sign
685 myPropagChains[ nb ].Clear();
690 TopTools_MapOfShape meshedSolids;
691 list< Prism_3D::TPrismTopo > meshedPrism;
692 list< TopoDS_Face > suspectSourceFaces;
693 TopTools_ListIteratorOfListOfShape solidIt;
695 while ( meshedSolids.Extent() < nbSolids )
697 if ( _computeCanceled )
698 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
700 // compute prisms having avident computed source FACE
701 while ( !meshedFaces.empty() )
703 TopoDS_Face face = meshedFaces.front();
704 meshedFaces.pop_front();
705 TopTools_ListOfShape& solidList = faceToSolids.ChangeFromKey( face );
706 while ( !solidList.IsEmpty() )
708 TopoDS_Shape solid = solidList.First();
709 solidList.RemoveFirst();
710 if ( meshedSolids.Add( solid ))
713 prism.myBottom = face;
714 if ( !initPrism( prism, solid, selectBottom ) ||
718 SMESHDS_SubMesh* smDS = theMesh.GetMeshDS()->MeshElements( prism.myTop );
719 if ( !myHelper->IsSameElemGeometry( smDS, SMDSGeom_QUADRANGLE ))
721 meshedFaces.push_front( prism.myTop );
725 suspectSourceFaces.push_back( prism.myTop );
727 meshedPrism.push_back( prism );
731 if ( meshedSolids.Extent() == nbSolids )
734 // below in the loop we try to find source FACEs somehow
736 // project mesh from source FACEs of computed prisms to
737 // prisms sharing wall FACEs
738 list< Prism_3D::TPrismTopo >::iterator prismIt = meshedPrism.begin();
739 for ( ; prismIt != meshedPrism.end(); ++prismIt )
741 for ( size_t iW = 0; iW < prismIt->myWallQuads.size(); ++iW )
743 Prism_3D::TQuadList::iterator wQuad = prismIt->myWallQuads[iW].begin();
744 for ( ; wQuad != prismIt->myWallQuads[iW].end(); ++ wQuad )
746 const TopoDS_Face& wFace = (*wQuad)->face;
747 TopTools_ListOfShape& solidList = faceToSolids.ChangeFromKey( wFace );
748 solidIt.Initialize( solidList );
749 while ( solidIt.More() )
751 const TopoDS_Shape& solid = solidIt.Value();
752 if ( meshedSolids.Contains( solid )) {
753 solidList.Remove( solidIt );
754 continue; // already computed prism
756 if ( myHelper->IsBlock( solid )) {
758 continue; // too trivial
760 // find a source FACE of the SOLID: it's a FACE sharing a bottom EDGE with wFace
761 const TopoDS_Edge& wEdge = (*wQuad)->side[ QUAD_TOP_SIDE ].grid->Edge(0);
762 PShapeIteratorPtr faceIt = myHelper->GetAncestors( wEdge, *myHelper->GetMesh(),
764 while ( const TopoDS_Shape* f = faceIt->next() )
766 const TopoDS_Face& candidateF = TopoDS::Face( *f );
767 if ( candidateF.IsSame( wFace )) continue;
768 // select a source FACE: prismIt->myBottom or prismIt->myTop
769 TopoDS_Face sourceF = prismIt->myBottom;
770 for ( TopExp_Explorer v( prismIt->myTop, TopAbs_VERTEX ); v.More(); v.Next() )
771 if ( myHelper->IsSubShape( v.Current(), candidateF )) {
772 sourceF = prismIt->myTop;
776 prism.myBottom = candidateF;
777 mySetErrorToSM = false;
778 if ( !myHelper->IsSubShape( candidateF, prismIt->myShape3D ) &&
779 myHelper ->IsSubShape( candidateF, solid ) &&
780 !myHelper->GetMesh()->GetSubMesh( candidateF )->IsMeshComputed() &&
781 initPrism( prism, solid, /*selectBottom=*/false ) &&
782 !myHelper->GetMesh()->GetSubMesh( prism.myTop )->IsMeshComputed() &&
783 !myHelper->GetMesh()->GetSubMesh( prism.myBottom )->IsMeshComputed() &&
784 project2dMesh( sourceF, prism.myBottom ))
786 mySetErrorToSM = true;
787 if ( !compute( prism ))
789 SMESHDS_SubMesh* smDS = theMesh.GetMeshDS()->MeshElements( prism.myTop );
790 if ( !myHelper->IsSameElemGeometry( smDS, SMDSGeom_QUADRANGLE ))
792 meshedFaces.push_front( prism.myTop );
793 meshedFaces.push_front( prism.myBottom );
794 selectBottom = false;
796 meshedPrism.push_back( prism );
797 meshedSolids.Add( solid );
801 mySetErrorToSM = true;
803 if ( meshedSolids.Contains( solid ))
804 solidList.Remove( solidIt );
810 if ( !meshedFaces.empty() )
811 break; // to compute prisms with avident sources
814 if ( meshedFaces.empty() )
816 meshedFaces.splice( meshedFaces.end(), suspectSourceFaces );
820 // find FACEs with local 1D hyps, which has to be computed by now,
821 // or at least any computed FACEs
822 if ( meshedFaces.empty() )
825 for ( int iF = 1; iF <= faceToSolids.Extent(); ++iF )
827 const TopoDS_Face& face = TopoDS::Face( faceToSolids.FindKey( iF ));
828 const TopTools_ListOfShape& solidList = faceToSolids.FindFromKey( face );
829 if ( solidList.IsEmpty() ) continue;
830 SMESH_subMesh* faceSM = theMesh.GetSubMesh( face );
831 if ( !faceSM->IsEmpty() )
833 int nbFaces = faceSM->GetSubMeshDS()->NbElements();
834 if ( prevNbFaces < nbFaces )
836 if ( !meshedFaces.empty() ) meshedFaces.pop_back();
837 meshedFaces.push_back( face ); // lower priority
839 prevNbFaces = nbFaces;
844 bool allSubMeComputed = true;
845 SMESH_subMeshIteratorPtr smIt = faceSM->getDependsOnIterator(false,true);
846 while ( smIt->more() && allSubMeComputed )
847 allSubMeComputed = smIt->next()->IsMeshComputed();
848 if ( allSubMeComputed )
850 faceSM->ComputeStateEngine( SMESH_subMesh::COMPUTE );
851 if ( !faceSM->IsEmpty() ) {
852 meshedFaces.push_front( face ); // higher priority
857 faceSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
865 // TODO. there are other ways to find out the source FACE:
866 // propagation, topological similarity, ect.
868 // simply try to mesh all not meshed SOLIDs
869 if ( meshedFaces.empty() )
871 for ( TopExp_Explorer solid( theShape, TopAbs_SOLID ); solid.More(); solid.Next() )
873 mySetErrorToSM = false;
875 if ( !meshedSolids.Contains( solid.Current() ) &&
876 initPrism( prism, solid.Current() ))
878 mySetErrorToSM = true;
879 if ( !compute( prism ))
881 meshedFaces.push_front( prism.myTop );
882 meshedFaces.push_front( prism.myBottom );
883 meshedPrism.push_back( prism );
884 meshedSolids.Add( solid.Current() );
887 mySetErrorToSM = true;
891 if ( meshedFaces.empty() ) // set same error to 10 not-computed solids
893 SMESH_ComputeErrorPtr err = SMESH_ComputeError::New
894 ( COMPERR_BAD_INPUT_MESH, "No meshed source face found", this );
896 const int maxNbErrors = 10; // limit nb errors not to overload the Compute dialog
897 TopExp_Explorer solid( theShape, TopAbs_SOLID );
898 for ( int i = 0; ( i < maxNbErrors && solid.More() ); ++i, solid.Next() )
899 if ( !meshedSolids.Contains( solid.Current() ))
901 SMESH_subMesh* sm = theMesh.GetSubMesh( solid.Current() );
902 sm->GetComputeError() = err;
907 return error( COMPERR_OK );
910 //================================================================================
912 * \brief Find wall faces by bottom edges
914 //================================================================================
916 bool StdMeshers_Prism_3D::getWallFaces( Prism_3D::TPrismTopo & thePrism,
917 const int totalNbFaces)
919 thePrism.myWallQuads.clear();
921 SMESH_Mesh* mesh = myHelper->GetMesh();
923 StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this, myHelper );
925 TopTools_MapOfShape faceMap;
926 TopTools_IndexedDataMapOfShapeListOfShape edgeToFaces;
927 TopExp::MapShapesAndAncestors( thePrism.myShape3D,
928 TopAbs_EDGE, TopAbs_FACE, edgeToFaces );
930 // ------------------------------
931 // Get the 1st row of wall FACEs
932 // ------------------------------
934 list< TopoDS_Edge >::iterator edge = thePrism.myBottomEdges.begin();
935 std::list< int >::iterator nbE = thePrism.myNbEdgesInWires.begin();
936 std::list< int > nbQuadsPerWire;
939 while ( edge != thePrism.myBottomEdges.end() )
942 if ( BRep_Tool::Curve( *edge, f,l ).IsNull() )
944 edge = thePrism.myBottomEdges.erase( edge );
950 TopTools_ListIteratorOfListOfShape faceIt( edgeToFaces.FindFromKey( *edge ));
951 for ( ; faceIt.More(); faceIt.Next() )
953 const TopoDS_Face& face = TopoDS::Face( faceIt.Value() );
954 if ( !thePrism.myBottom.IsSame( face ))
956 Prism_3D::TQuadList quadList( 1, quadAlgo->CheckNbEdges( *mesh, face ));
957 if ( !quadList.back() )
958 return toSM( error(TCom("Side face #") << shapeID( face )
959 << " not meshable with quadrangles"));
960 bool isCompositeBase = ! setBottomEdge( *edge, quadList.back(), face );
961 if ( isCompositeBase )
963 // it's OK if all EDGEs of the bottom side belongs to the bottom FACE
964 StdMeshers_FaceSidePtr botSide = quadList.back()->side[ QUAD_BOTTOM_SIDE ];
965 for ( int iE = 0; iE < botSide->NbEdges(); ++iE )
966 if ( !myHelper->IsSubShape( botSide->Edge(iE), thePrism.myBottom ))
967 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
969 if ( faceMap.Add( face ))
970 thePrism.myWallQuads.push_back( quadList );
980 int nbQuadPrev = nbQuadsPerWire.empty() ? 0 : nbQuadsPerWire.back();
981 nbQuadsPerWire.push_back( thePrism.myWallQuads.size() - nbQuadPrev );
985 // -------------------------
986 // Find the rest wall FACEs
987 // -------------------------
989 // Compose a vector of indixes of right neighbour FACE for each wall FACE
990 // that is not so evident in case of several WIREs in the bottom FACE
991 thePrism.myRightQuadIndex.clear();
992 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
994 thePrism.myRightQuadIndex.push_back( i+1 ); // OK for all but the last EDGE of a WIRE
996 list< int >::iterator nbQinW = nbQuadsPerWire.begin();
997 for ( int iLeft = 0; nbQinW != nbQuadsPerWire.end(); ++nbQinW )
999 thePrism.myRightQuadIndex[ iLeft + *nbQinW - 1 ] = iLeft; // for the last EDGE of a WIRE
1003 while ( totalNbFaces - faceMap.Extent() > 2 )
1005 // find wall FACEs adjacent to each of wallQuads by the right side EDGE
1008 nbKnownFaces = faceMap.Extent();
1009 StdMeshers_FaceSidePtr rightSide, topSide; // sides of the quad
1010 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1012 rightSide = thePrism.myWallQuads[i].back()->side[ QUAD_RIGHT_SIDE ];
1013 for ( int iE = 0; iE < rightSide->NbEdges(); ++iE ) // rightSide can be composite
1015 const TopoDS_Edge & rightE = rightSide->Edge( iE );
1016 TopTools_ListIteratorOfListOfShape face( edgeToFaces.FindFromKey( rightE ));
1017 for ( ; face.More(); face.Next() )
1018 if ( faceMap.Add( face.Value() ))
1020 // a new wall FACE encountered, store it in thePrism.myWallQuads
1021 const int iRight = thePrism.myRightQuadIndex[i];
1022 topSide = thePrism.myWallQuads[ iRight ].back()->side[ QUAD_TOP_SIDE ];
1023 const TopoDS_Edge& newBotE = topSide->Edge(0);
1024 const TopoDS_Shape& newWallF = face.Value();
1025 thePrism.myWallQuads[ iRight ].push_back( quadAlgo->CheckNbEdges( *mesh, newWallF ));
1026 if ( !thePrism.myWallQuads[ iRight ].back() )
1027 return toSM( error(TCom("Side face #") << shapeID( newWallF ) <<
1028 " not meshable with quadrangles"));
1029 if ( ! setBottomEdge( newBotE, thePrism.myWallQuads[ iRight ].back(), newWallF ))
1030 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
1034 } while ( nbKnownFaces != faceMap.Extent() );
1036 // find wall FACEs adjacent to each of thePrism.myWallQuads by the top side EDGE
1037 if ( totalNbFaces - faceMap.Extent() > 2 )
1039 const int nbFoundWalls = faceMap.Extent();
1040 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1042 StdMeshers_FaceSidePtr topSide = thePrism.myWallQuads[i].back()->side[ QUAD_TOP_SIDE ];
1043 const TopoDS_Edge & topE = topSide->Edge( 0 );
1044 if ( topSide->NbEdges() > 1 )
1045 return toSM( error(COMPERR_BAD_SHAPE, TCom("Side face #") <<
1046 shapeID( thePrism.myWallQuads[i].back()->face )
1047 << " has a composite top edge"));
1048 TopTools_ListIteratorOfListOfShape faceIt( edgeToFaces.FindFromKey( topE ));
1049 for ( ; faceIt.More(); faceIt.Next() )
1050 if ( faceMap.Add( faceIt.Value() ))
1052 // a new wall FACE encountered, store it in wallQuads
1053 thePrism.myWallQuads[ i ].push_back( quadAlgo->CheckNbEdges( *mesh, faceIt.Value() ));
1054 if ( !thePrism.myWallQuads[ i ].back() )
1055 return toSM( error(TCom("Side face #") << shapeID( faceIt.Value() ) <<
1056 " not meshable with quadrangles"));
1057 if ( ! setBottomEdge( topE, thePrism.myWallQuads[ i ].back(), faceIt.Value() ))
1058 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
1059 if ( totalNbFaces - faceMap.Extent() == 2 )
1061 i = thePrism.myWallQuads.size(); // to quit from the outer loop
1066 if ( nbFoundWalls == faceMap.Extent() )
1067 return toSM( error("Failed to find wall faces"));
1070 } // while ( totalNbFaces - faceMap.Extent() > 2 )
1072 // ------------------
1073 // Find the top FACE
1074 // ------------------
1076 if ( thePrism.myTop.IsNull() )
1078 // now only top and bottom FACEs are not in the faceMap
1079 faceMap.Add( thePrism.myBottom );
1080 for ( TopExp_Explorer f( thePrism.myShape3D, TopAbs_FACE ); f.More(); f.Next() )
1081 if ( !faceMap.Contains( f.Current() )) {
1082 thePrism.myTop = TopoDS::Face( f.Current() );
1085 if ( thePrism.myTop.IsNull() )
1086 return toSM( error("Top face not found"));
1089 // Check that the top FACE shares all the top EDGEs
1090 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1092 StdMeshers_FaceSidePtr topSide = thePrism.myWallQuads[i].back()->side[ QUAD_TOP_SIDE ];
1093 const TopoDS_Edge & topE = topSide->Edge( 0 );
1094 if ( !myHelper->IsSubShape( topE, thePrism.myTop ))
1095 return toSM( error( TCom("Wrong source face: #") << shapeID( thePrism.myBottom )));
1101 //=======================================================================
1102 //function : compute
1103 //purpose : Compute mesh on a SOLID
1104 //=======================================================================
1106 bool StdMeshers_Prism_3D::compute(const Prism_3D::TPrismTopo& thePrism)
1108 myHelper->IsQuadraticSubMesh( thePrism.myShape3D );
1109 if ( _computeCanceled )
1110 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
1112 // Assure the bottom is meshed
1113 SMESH_subMesh * botSM = myHelper->GetMesh()->GetSubMesh( thePrism.myBottom );
1114 if (( botSM->IsEmpty() ) &&
1115 ( ! botSM->GetAlgo() ||
1116 ! _gen->Compute( *botSM->GetFather(), botSM->GetSubShape(), /*shapeOnly=*/true )))
1117 return error( COMPERR_BAD_INPUT_MESH,
1118 TCom( "No mesher defined to compute the face #")
1119 << shapeID( thePrism.myBottom ));
1121 // Make all side FACEs of thePrism meshed with quads
1122 if ( !computeWalls( thePrism ))
1125 // Analyse mesh and geometry to find all block sub-shapes and submeshes
1126 // (after fixing IPAL52499 myBlock is used as a holder of boundary nodes
1127 // and for 2D projection in hard cases where StdMeshers_Projection_2D fails;
1128 // location of internal nodes is usually computed by StdMeshers_Sweeper)
1129 if ( !myBlock.Init( myHelper, thePrism ))
1130 return toSM( error( myBlock.GetError()));
1132 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
1134 int volumeID = meshDS->ShapeToIndex( thePrism.myShape3D );
1136 // Try to get gp_Trsf to get all nodes from bottom ones
1137 vector<gp_Trsf> trsf;
1138 gp_Trsf bottomToTopTrsf;
1139 // if ( !myBlock.GetLayersTransformation( trsf, thePrism ))
1141 // else if ( !trsf.empty() )
1142 // bottomToTopTrsf = trsf.back();
1144 // To compute coordinates of a node inside a block, it is necessary to know
1145 // 1. normalized parameters of the node by which
1146 // 2. coordinates of node projections on all block sub-shapes are computed
1148 // So we fill projections on vertices at once as they are same for all nodes
1149 myShapeXYZ.resize( myBlock.NbSubShapes() );
1150 for ( int iV = SMESH_Block::ID_FirstV; iV < SMESH_Block::ID_FirstE; ++iV ) {
1151 myBlock.VertexPoint( iV, myShapeXYZ[ iV ]);
1152 SHOWYXZ("V point " <<iV << " ", myShapeXYZ[ iV ]);
1155 // Projections on the top and bottom faces are taken from nodes existing
1156 // on these faces; find correspondence between bottom and top nodes
1158 myBotToColumnMap.clear();
1159 if ( !assocOrProjBottom2Top( bottomToTopTrsf, thePrism ) ) // it also fills myBotToColumnMap
1163 // Create nodes inside the block
1165 // use transformation (issue 0020680, IPAL0052499)
1166 StdMeshers_Sweeper sweeper;
1168 bool allowHighBndError;
1172 // load boundary nodes into sweeper
1174 list< TopoDS_Edge >::const_iterator edge = thePrism.myBottomEdges.begin();
1175 for ( ; edge != thePrism.myBottomEdges.end(); ++edge )
1177 int edgeID = meshDS->ShapeToIndex( *edge );
1178 TParam2ColumnMap* u2col = const_cast<TParam2ColumnMap*>
1179 ( myBlock.GetParam2ColumnMap( edgeID, dummy ));
1180 TParam2ColumnMap::iterator u2colIt = u2col->begin();
1181 for ( ; u2colIt != u2col->end(); ++u2colIt )
1182 sweeper.myBndColumns.push_back( & u2colIt->second );
1184 // load node columns inside the bottom face
1185 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.begin();
1186 for ( ; bot_column != myBotToColumnMap.end(); ++bot_column )
1187 sweeper.myIntColumns.push_back( & bot_column->second );
1189 tol = getSweepTolerance( thePrism );
1190 allowHighBndError = !isSimpleBottom( thePrism );
1193 if ( !myUseBlock && sweeper.ComputeNodes( *myHelper, tol, allowHighBndError ))
1196 else // use block approach
1198 // loop on nodes inside the bottom face
1199 Prism_3D::TNode prevBNode;
1200 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.begin();
1201 for ( ; bot_column != myBotToColumnMap.end(); ++bot_column )
1203 const Prism_3D::TNode& tBotNode = bot_column->first; // bottom TNode
1204 if ( tBotNode.GetPositionType() != SMDS_TOP_FACE )
1205 continue; // node is not inside the FACE
1207 // column nodes; middle part of the column are zero pointers
1208 TNodeColumn& column = bot_column->second;
1210 gp_XYZ botParams, topParams;
1211 if ( !tBotNode.HasParams() )
1213 // compute bottom node parameters
1214 gp_XYZ paramHint(-1,-1,-1);
1215 if ( prevBNode.IsNeighbor( tBotNode ))
1216 paramHint = prevBNode.GetParams();
1217 if ( !myBlock.ComputeParameters( tBotNode.GetCoords(), tBotNode.ChangeParams(),
1218 ID_BOT_FACE, paramHint ))
1219 return toSM( error(TCom("Can't compute normalized parameters for node ")
1220 << tBotNode.myNode->GetID() << " on the face #"
1221 << myBlock.SubMesh( ID_BOT_FACE )->GetId() ));
1222 prevBNode = tBotNode;
1224 botParams = topParams = tBotNode.GetParams();
1225 topParams.SetZ( 1 );
1227 // compute top node parameters
1228 if ( column.size() > 2 ) {
1229 gp_Pnt topCoords = gpXYZ( column.back() );
1230 if ( !myBlock.ComputeParameters( topCoords, topParams, ID_TOP_FACE, topParams ))
1231 return toSM( error(TCom("Can't compute normalized parameters ")
1232 << "for node " << column.back()->GetID()
1233 << " on the face #"<< column.back()->getshapeId() ));
1236 else // top nodes are created by projection using parameters
1238 botParams = topParams = tBotNode.GetParams();
1239 topParams.SetZ( 1 );
1242 myShapeXYZ[ ID_BOT_FACE ] = tBotNode.GetCoords();
1243 myShapeXYZ[ ID_TOP_FACE ] = gpXYZ( column.back() );
1246 TNodeColumn::iterator columnNodes = column.begin();
1247 for ( int z = 0; columnNodes != column.end(); ++columnNodes, ++z)
1249 const SMDS_MeshNode* & node = *columnNodes;
1250 if ( node ) continue; // skip bottom or top node
1252 // params of a node to create
1253 double rz = (double) z / (double) ( column.size() - 1 );
1254 gp_XYZ params = botParams * ( 1 - rz ) + topParams * rz;
1256 // set coords on all faces and nodes
1257 const int nbSideFaces = 4;
1258 int sideFaceIDs[nbSideFaces] = { SMESH_Block::ID_Fx0z,
1259 SMESH_Block::ID_Fx1z,
1260 SMESH_Block::ID_F0yz,
1261 SMESH_Block::ID_F1yz };
1262 for ( int iF = 0; iF < nbSideFaces; ++iF )
1263 if ( !setFaceAndEdgesXYZ( sideFaceIDs[ iF ], params, z ))
1266 // compute coords for a new node
1268 if ( !SMESH_Block::ShellPoint( params, myShapeXYZ, coords ))
1269 return toSM( error("Can't compute coordinates by normalized parameters"));
1271 // if ( !meshDS->MeshElements( volumeID ) ||
1272 // meshDS->MeshElements( volumeID )->NbNodes() == 0 )
1273 // pointsToPython(myShapeXYZ);
1274 SHOWYXZ("TOPFacePoint ",myShapeXYZ[ ID_TOP_FACE]);
1275 SHOWYXZ("BOT Node "<< tBotNode.myNode->GetID(),gpXYZ(tBotNode.myNode));
1276 SHOWYXZ("ShellPoint ",coords);
1279 node = meshDS->AddNode( coords.X(), coords.Y(), coords.Z() );
1280 meshDS->SetNodeInVolume( node, volumeID );
1282 if ( _computeCanceled )
1285 } // loop on bottom nodes
1290 SMESHDS_SubMesh* smDS = myBlock.SubMeshDS( ID_BOT_FACE );
1291 if ( !smDS ) return toSM( error(COMPERR_BAD_INPUT_MESH, "Null submesh"));
1293 // loop on bottom mesh faces
1294 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
1295 while ( faceIt->more() )
1297 const SMDS_MeshElement* face = faceIt->next();
1298 if ( !face || face->GetType() != SMDSAbs_Face )
1301 // find node columns for each node
1302 int nbNodes = face->NbCornerNodes();
1303 vector< const TNodeColumn* > columns( nbNodes );
1304 for ( int i = 0; i < nbNodes; ++i )
1306 const SMDS_MeshNode* n = face->GetNode( i );
1307 if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
1308 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.find( n );
1309 if ( bot_column == myBotToColumnMap.end() )
1310 return toSM( error(TCom("No nodes found above node ") << n->GetID() ));
1311 columns[ i ] = & bot_column->second;
1314 columns[ i ] = myBlock.GetNodeColumn( n );
1315 if ( !columns[ i ] )
1316 return toSM( error(TCom("No side nodes found above node ") << n->GetID() ));
1320 AddPrisms( columns, myHelper );
1322 } // loop on bottom mesh faces
1325 myBotToColumnMap.clear();
1328 // update state of sub-meshes (mostly in order to erase improper errors)
1329 SMESH_subMesh* sm = myHelper->GetMesh()->GetSubMesh( thePrism.myShape3D );
1330 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/false);
1331 while ( smIt->more() )
1334 sm->GetComputeError().reset();
1335 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1341 //=======================================================================
1342 //function : computeWalls
1343 //purpose : Compute 2D mesh on walls FACEs of a prism
1344 //=======================================================================
1346 bool StdMeshers_Prism_3D::computeWalls(const Prism_3D::TPrismTopo& thePrism)
1348 SMESH_Mesh* mesh = myHelper->GetMesh();
1349 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
1350 DBGOUT( endl << "COMPUTE Prism " << meshDS->ShapeToIndex( thePrism.myShape3D ));
1352 TProjction1dAlgo* projector1D = TProjction1dAlgo::instance( this );
1353 StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this, myHelper );
1355 // SMESH_HypoFilter hyp1dFilter( SMESH_HypoFilter::IsAlgo(),/*not=*/true);
1356 // hyp1dFilter.And( SMESH_HypoFilter::HasDim( 1 ));
1357 // hyp1dFilter.And( SMESH_HypoFilter::IsMoreLocalThan( thePrism.myShape3D, *mesh ));
1359 // Discretize equally 'vertical' EDGEs
1360 // -----------------------------------
1361 // find source FACE sides for projection: either already computed ones or
1362 // the 'most composite' ones
1363 const size_t nbWalls = thePrism.myWallQuads.size();
1364 vector< int > wgt( nbWalls, 0 ); // "weight" of a wall
1365 for ( size_t iW = 0; iW != nbWalls; ++iW )
1367 Prism_3D::TQuadList::const_iterator quad = thePrism.myWallQuads[iW].begin();
1368 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1370 StdMeshers_FaceSidePtr lftSide = (*quad)->side[ QUAD_LEFT_SIDE ];
1371 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1374 const TopoDS_Edge& E = lftSide->Edge(i);
1375 if ( mesh->GetSubMesh( E )->IsMeshComputed() )
1378 wgt[ myHelper->WrapIndex( iW+1, nbWalls)] += 10;
1379 wgt[ myHelper->WrapIndex( iW-1, nbWalls)] += 10;
1381 // else if ( mesh->GetHypothesis( E, hyp1dFilter, true )) // local hypothesis!
1385 // in quadratic mesh, pass ignoreMediumNodes to quad sides
1386 if ( myHelper->GetIsQuadratic() )
1388 quad = thePrism.myWallQuads[iW].begin();
1389 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1390 for ( int i = 0; i < NB_QUAD_SIDES; ++i )
1391 (*quad)->side[ i ].grid->SetIgnoreMediumNodes( true );
1394 multimap< int, int > wgt2quad;
1395 for ( size_t iW = 0; iW != nbWalls; ++iW )
1396 wgt2quad.insert( make_pair( wgt[ iW ], iW ));
1398 // Project 'vertical' EDGEs, from left to right
1399 multimap< int, int >::reverse_iterator w2q = wgt2quad.rbegin();
1400 for ( ; w2q != wgt2quad.rend(); ++w2q )
1402 const int iW = w2q->second;
1403 const Prism_3D::TQuadList& quads = thePrism.myWallQuads[ iW ];
1404 Prism_3D::TQuadList::const_iterator quad = quads.begin();
1405 for ( ; quad != quads.end(); ++quad )
1407 StdMeshers_FaceSidePtr rgtSide = (*quad)->side[ QUAD_RIGHT_SIDE ]; // tgt
1408 StdMeshers_FaceSidePtr lftSide = (*quad)->side[ QUAD_LEFT_SIDE ]; // src
1409 bool swapLeftRight = ( lftSide->NbSegments( /*update=*/true ) == 0 &&
1410 rgtSide->NbSegments( /*update=*/true ) > 0 );
1411 if ( swapLeftRight )
1412 std::swap( lftSide, rgtSide );
1414 // assure that all the source (left) EDGEs are meshed
1415 int nbSrcSegments = 0;
1416 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1418 const TopoDS_Edge& srcE = lftSide->Edge(i);
1419 SMESH_subMesh* srcSM = mesh->GetSubMesh( srcE );
1420 if ( !srcSM->IsMeshComputed() ) {
1421 DBGOUT( "COMPUTE V edge " << srcSM->GetId() );
1422 TopoDS_Edge prpgSrcE = findPropagationSource( srcE );
1423 if ( !prpgSrcE.IsNull() ) {
1424 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1425 projector1D->myHyp.SetSourceEdge( prpgSrcE );
1426 projector1D->Compute( *mesh, srcE );
1427 srcSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1430 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1431 srcSM->ComputeStateEngine ( SMESH_subMesh::COMPUTE );
1433 if ( !srcSM->IsMeshComputed() )
1434 return toSM( error( "Can't compute 1D mesh" ));
1436 nbSrcSegments += srcSM->GetSubMeshDS()->NbElements();
1438 // check target EDGEs
1439 int nbTgtMeshed = 0, nbTgtSegments = 0;
1440 vector< bool > isTgtEdgeComputed( rgtSide->NbEdges() );
1441 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1443 const TopoDS_Edge& tgtE = rgtSide->Edge(i);
1444 SMESH_subMesh* tgtSM = mesh->GetSubMesh( tgtE );
1445 if ( !( isTgtEdgeComputed[ i ] = tgtSM->IsMeshComputed() )) {
1446 tgtSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1447 tgtSM->ComputeStateEngine ( SMESH_subMesh::COMPUTE );
1449 if ( tgtSM->IsMeshComputed() ) {
1451 nbTgtSegments += tgtSM->GetSubMeshDS()->NbElements();
1454 if ( rgtSide->NbEdges() == nbTgtMeshed ) // all tgt EDGEs meshed
1456 if ( nbTgtSegments != nbSrcSegments )
1458 bool badMeshRemoved = false;
1459 // remove just computed segments
1460 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1461 if ( !isTgtEdgeComputed[ i ])
1463 const TopoDS_Edge& tgtE = rgtSide->Edge(i);
1464 SMESH_subMesh* tgtSM = mesh->GetSubMesh( tgtE );
1465 tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
1466 badMeshRemoved = true;
1469 if ( !badMeshRemoved )
1471 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1472 addBadInputElements( meshDS->MeshElements( lftSide->Edge( i )));
1473 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1474 addBadInputElements( meshDS->MeshElements( rgtSide->Edge( i )));
1475 return toSM( error( TCom("Different nb of segment on logically vertical edges #")
1476 << shapeID( lftSide->Edge(0) ) << " and #"
1477 << shapeID( rgtSide->Edge(0) ) << ": "
1478 << nbSrcSegments << " != " << nbTgtSegments ));
1481 else // if ( nbTgtSegments == nbSrcSegments )
1486 // Compute 'vertical projection'
1487 if ( nbTgtMeshed == 0 )
1489 // compute nodes on target VERTEXes
1490 const UVPtStructVec& srcNodeStr = lftSide->GetUVPtStruct();
1491 if ( srcNodeStr.size() == 0 )
1492 return toSM( error( TCom("Invalid node positions on edge #") <<
1493 shapeID( lftSide->Edge(0) )));
1494 vector< SMDS_MeshNode* > newNodes( srcNodeStr.size() );
1495 for ( int is2ndV = 0; is2ndV < 2; ++is2ndV )
1497 const TopoDS_Edge& E = rgtSide->Edge( is2ndV ? rgtSide->NbEdges()-1 : 0 );
1498 TopoDS_Vertex v = myHelper->IthVertex( is2ndV, E );
1499 mesh->GetSubMesh( v )->ComputeStateEngine( SMESH_subMesh::COMPUTE );
1500 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, meshDS );
1501 newNodes[ is2ndV ? 0 : newNodes.size()-1 ] = (SMDS_MeshNode*) n;
1504 // compute nodes on target EDGEs
1505 DBGOUT( "COMPUTE V edge (proj) " << shapeID( lftSide->Edge(0)));
1506 rgtSide->Reverse(); // direct it same as the lftSide
1507 myHelper->SetElementsOnShape( false ); // myHelper holds the prism shape
1508 TopoDS_Edge tgtEdge;
1509 for ( size_t iN = 1; iN < srcNodeStr.size()-1; ++iN ) // add nodes
1511 gp_Pnt p = rgtSide->Value3d ( srcNodeStr[ iN ].normParam );
1512 double u = rgtSide->Parameter( srcNodeStr[ iN ].normParam, tgtEdge );
1513 newNodes[ iN ] = meshDS->AddNode( p.X(), p.Y(), p.Z() );
1514 meshDS->SetNodeOnEdge( newNodes[ iN ], tgtEdge, u );
1516 for ( size_t iN = 1; iN < srcNodeStr.size(); ++iN ) // add segments
1518 // find an EDGE to set a new segment
1519 std::pair<int, TopAbs_ShapeEnum> id2type =
1520 myHelper->GetMediumPos( newNodes[ iN-1 ], newNodes[ iN ] );
1521 if ( id2type.second != TopAbs_EDGE )
1523 // new nodes are on different EDGEs; put one of them on VERTEX
1524 const int edgeIndex = rgtSide->EdgeIndex( srcNodeStr[ iN-1 ].normParam );
1525 const double vertexParam = rgtSide->LastParameter( edgeIndex );
1526 TopoDS_Vertex vertex = rgtSide->LastVertex( edgeIndex );
1527 const SMDS_MeshNode* vn = SMESH_Algo::VertexNode( vertex, meshDS );
1528 const gp_Pnt p = BRep_Tool::Pnt( vertex );
1529 const int isPrev = ( Abs( srcNodeStr[ iN-1 ].normParam - vertexParam ) <
1530 Abs( srcNodeStr[ iN ].normParam - vertexParam ));
1531 meshDS->UnSetNodeOnShape( newNodes[ iN-isPrev ] );
1532 meshDS->SetNodeOnVertex ( newNodes[ iN-isPrev ], vertex );
1533 meshDS->MoveNode ( newNodes[ iN-isPrev ], p.X(), p.Y(), p.Z() );
1534 id2type.first = newNodes[ iN-(1-isPrev) ]->getshapeId();
1537 SMESH_MeshEditor::TListOfListOfNodes lln( 1, list< const SMDS_MeshNode* >() );
1538 lln.back().push_back ( vn );
1539 lln.back().push_front( newNodes[ iN-isPrev ] ); // to keep
1540 SMESH_MeshEditor( mesh ).MergeNodes( lln );
1543 SMDS_MeshElement* newEdge = myHelper->AddEdge( newNodes[ iN-1 ], newNodes[ iN ] );
1544 meshDS->SetMeshElementOnShape( newEdge, id2type.first );
1546 myHelper->SetElementsOnShape( true );
1547 for ( int i = 0; i < rgtSide->NbEdges(); ++i ) // update state of sub-meshes
1549 const TopoDS_Edge& E = rgtSide->Edge( i );
1550 SMESH_subMesh* tgtSM = mesh->GetSubMesh( E );
1551 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1554 // to continue projection from the just computed side as a source
1555 if ( !swapLeftRight && rgtSide->NbEdges() > 1 && w2q->second == iW )
1557 std::pair<int,int> wgt2quadKeyVal( w2q->first + 1, thePrism.myRightQuadIndex[ iW ]);
1558 wgt2quad.insert( wgt2quadKeyVal ); // it will be skipped by ++w2q
1559 wgt2quad.insert( wgt2quadKeyVal );
1560 w2q = wgt2quad.rbegin();
1565 // HOPE assigned hypotheses are OK, so that equal nb of segments will be generated
1566 //return toSM( error("Partial projection not implemented"));
1568 } // loop on quads of a composite wall side
1569 } // loop on the ordered wall sides
1573 for ( size_t iW = 0; iW != thePrism.myWallQuads.size(); ++iW )
1575 Prism_3D::TQuadList::const_iterator quad = thePrism.myWallQuads[iW].begin();
1576 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1578 const TopoDS_Face& face = (*quad)->face;
1579 SMESH_subMesh* fSM = mesh->GetSubMesh( face );
1580 if ( ! fSM->IsMeshComputed() )
1582 // Top EDGEs must be projections from the bottom ones
1583 // to compute stuctured quad mesh on wall FACEs
1584 // ---------------------------------------------------
1585 const TopoDS_Edge& botE = (*quad)->side[ QUAD_BOTTOM_SIDE ].grid->Edge(0);
1586 const TopoDS_Edge& topE = (*quad)->side[ QUAD_TOP_SIDE ].grid->Edge(0);
1587 SMESH_subMesh* botSM = mesh->GetSubMesh( botE );
1588 SMESH_subMesh* topSM = mesh->GetSubMesh( topE );
1589 SMESH_subMesh* srcSM = botSM;
1590 SMESH_subMesh* tgtSM = topSM;
1591 srcSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1592 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1593 if ( !srcSM->IsMeshComputed() && tgtSM->IsMeshComputed() )
1594 std::swap( srcSM, tgtSM );
1596 if ( !srcSM->IsMeshComputed() )
1598 DBGOUT( "COMPUTE H edge " << srcSM->GetId());
1599 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE ); // nodes on VERTEXes
1600 srcSM->ComputeStateEngine( SMESH_subMesh::COMPUTE ); // segments on the EDGE
1603 if ( tgtSM->IsMeshComputed() &&
1604 tgtSM->GetSubMeshDS()->NbNodes() != srcSM->GetSubMeshDS()->NbNodes() )
1606 // the top EDGE is computed differently than the bottom one,
1607 // try to clear a wrong mesh
1608 bool isAdjFaceMeshed = false;
1609 PShapeIteratorPtr fIt = myHelper->GetAncestors( tgtSM->GetSubShape(),
1610 *mesh, TopAbs_FACE );
1611 while ( const TopoDS_Shape* f = fIt->next() )
1612 if (( isAdjFaceMeshed = mesh->GetSubMesh( *f )->IsMeshComputed() ))
1614 if ( isAdjFaceMeshed )
1615 return toSM( error( TCom("Different nb of segment on logically horizontal edges #")
1616 << shapeID( botE ) << " and #"
1617 << shapeID( topE ) << ": "
1618 << tgtSM->GetSubMeshDS()->NbElements() << " != "
1619 << srcSM->GetSubMeshDS()->NbElements() ));
1620 tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
1622 if ( !tgtSM->IsMeshComputed() )
1624 // compute nodes on VERTEXes
1625 SMESH_subMeshIteratorPtr smIt = tgtSM->getDependsOnIterator(/*includeSelf=*/false);
1626 while ( smIt->more() )
1627 smIt->next()->ComputeStateEngine( SMESH_subMesh::COMPUTE );
1629 DBGOUT( "COMPUTE H edge (proj) " << tgtSM->GetId());
1630 projector1D->myHyp.SetSourceEdge( TopoDS::Edge( srcSM->GetSubShape() ));
1631 projector1D->InitComputeError();
1632 bool ok = projector1D->Compute( *mesh, tgtSM->GetSubShape() );
1635 SMESH_ComputeErrorPtr err = projector1D->GetComputeError();
1636 if ( err->IsOK() ) err->myName = COMPERR_ALGO_FAILED;
1637 tgtSM->GetComputeError() = err;
1641 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1644 // Compute quad mesh on wall FACEs
1645 // -------------------------------
1647 // make all EDGES meshed
1648 fSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1649 if ( !fSM->SubMeshesComputed() )
1650 return toSM( error( COMPERR_BAD_INPUT_MESH,
1651 "Not all edges have valid algorithm and hypothesis"));
1653 quadAlgo->InitComputeError();
1654 DBGOUT( "COMPUTE Quad face " << fSM->GetId());
1655 bool ok = quadAlgo->Compute( *mesh, face );
1656 fSM->GetComputeError() = quadAlgo->GetComputeError();
1659 fSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1661 if ( myHelper->GetIsQuadratic() )
1663 // fill myHelper with medium nodes built by quadAlgo
1664 SMDS_ElemIteratorPtr fIt = fSM->GetSubMeshDS()->GetElements();
1665 while ( fIt->more() )
1666 myHelper->AddTLinks( dynamic_cast<const SMDS_MeshFace*>( fIt->next() ));
1674 //=======================================================================
1676 * \brief Returns a source EDGE of propagation to a given EDGE
1678 //=======================================================================
1680 TopoDS_Edge StdMeshers_Prism_3D::findPropagationSource( const TopoDS_Edge& E )
1682 if ( myPropagChains )
1683 for ( size_t i = 0; !myPropagChains[i].IsEmpty(); ++i )
1684 if ( myPropagChains[i].Contains( E ))
1685 return TopoDS::Edge( myPropagChains[i].FindKey( 1 ));
1687 return TopoDS_Edge();
1690 //=======================================================================
1691 //function : Evaluate
1693 //=======================================================================
1695 bool StdMeshers_Prism_3D::Evaluate(SMESH_Mesh& theMesh,
1696 const TopoDS_Shape& theShape,
1697 MapShapeNbElems& aResMap)
1699 if ( theShape.ShapeType() == TopAbs_COMPOUND )
1702 for ( TopoDS_Iterator it( theShape ); it.More(); it.Next() )
1703 ok &= Evaluate( theMesh, it.Value(), aResMap );
1706 SMESH_MesherHelper helper( theMesh );
1708 myHelper->SetSubShape( theShape );
1710 // find face contains only triangles
1711 vector < SMESH_subMesh * >meshFaces;
1712 TopTools_SequenceOfShape aFaces;
1713 int NumBase = 0, i = 0, NbQFs = 0;
1714 for (TopExp_Explorer exp(theShape, TopAbs_FACE); exp.More(); exp.Next()) {
1716 aFaces.Append(exp.Current());
1717 SMESH_subMesh *aSubMesh = theMesh.GetSubMesh(exp.Current());
1718 meshFaces.push_back(aSubMesh);
1719 MapShapeNbElemsItr anIt = aResMap.find(meshFaces[i-1]);
1720 if( anIt==aResMap.end() )
1721 return toSM( error( "Submesh can not be evaluated"));
1723 std::vector<int> aVec = (*anIt).second;
1724 int nbtri = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1725 int nbqua = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1726 if( nbtri==0 && nbqua>0 ) {
1735 std::vector<int> aResVec(SMDSEntity_Last);
1736 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1737 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1738 aResMap.insert(std::make_pair(sm,aResVec));
1739 return toSM( error( "Submesh can not be evaluated" ));
1742 if(NumBase==0) NumBase = 1; // only quads => set 1 faces as base
1744 // find number of 1d elems for base face
1746 TopTools_MapOfShape Edges1;
1747 for (TopExp_Explorer exp(aFaces.Value(NumBase), TopAbs_EDGE); exp.More(); exp.Next()) {
1748 Edges1.Add(exp.Current());
1749 SMESH_subMesh *sm = theMesh.GetSubMesh(exp.Current());
1751 MapShapeNbElemsItr anIt = aResMap.find(sm);
1752 if( anIt == aResMap.end() ) continue;
1753 std::vector<int> aVec = (*anIt).second;
1754 nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
1757 // find face opposite to base face
1759 for(i=1; i<=6; i++) {
1760 if(i==NumBase) continue;
1761 bool IsOpposite = true;
1762 for(TopExp_Explorer exp(aFaces.Value(i), TopAbs_EDGE); exp.More(); exp.Next()) {
1763 if( Edges1.Contains(exp.Current()) ) {
1773 // find number of 2d elems on side faces
1775 for(i=1; i<=6; i++) {
1776 if( i==OppNum || i==NumBase ) continue;
1777 MapShapeNbElemsItr anIt = aResMap.find( meshFaces[i-1] );
1778 if( anIt == aResMap.end() ) continue;
1779 std::vector<int> aVec = (*anIt).second;
1780 nb2d += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1783 MapShapeNbElemsItr anIt = aResMap.find( meshFaces[NumBase-1] );
1784 std::vector<int> aVec = (*anIt).second;
1785 bool IsQuadratic = (aVec[SMDSEntity_Quad_Triangle]>aVec[SMDSEntity_Triangle]) ||
1786 (aVec[SMDSEntity_Quad_Quadrangle]>aVec[SMDSEntity_Quadrangle]);
1787 int nb2d_face0_3 = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1788 int nb2d_face0_4 = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1789 int nb0d_face0 = aVec[SMDSEntity_Node];
1790 int nb1d_face0_int = ( nb2d_face0_3*3 + nb2d_face0_4*4 - nb1d ) / 2;
1792 std::vector<int> aResVec(SMDSEntity_Last);
1793 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1795 aResVec[SMDSEntity_Quad_Penta] = nb2d_face0_3 * ( nb2d/nb1d );
1796 aResVec[SMDSEntity_Quad_Hexa] = nb2d_face0_4 * ( nb2d/nb1d );
1797 aResVec[SMDSEntity_Node] = nb0d_face0 * ( 2*nb2d/nb1d - 1 ) - nb1d_face0_int * nb2d/nb1d;
1800 aResVec[SMDSEntity_Node] = nb0d_face0 * ( nb2d/nb1d - 1 );
1801 aResVec[SMDSEntity_Penta] = nb2d_face0_3 * ( nb2d/nb1d );
1802 aResVec[SMDSEntity_Hexa] = nb2d_face0_4 * ( nb2d/nb1d );
1804 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1805 aResMap.insert(std::make_pair(sm,aResVec));
1810 //================================================================================
1812 * \brief Create prisms
1813 * \param columns - columns of nodes generated from nodes of a mesh face
1814 * \param helper - helper initialized by mesh and shape to add prisms to
1816 //================================================================================
1818 void StdMeshers_Prism_3D::AddPrisms( vector<const TNodeColumn*> & columns,
1819 SMESH_MesherHelper* helper)
1821 int nbNodes = columns.size();
1822 int nbZ = columns[0]->size();
1823 if ( nbZ < 2 ) return;
1825 // find out orientation
1826 bool isForward = true;
1827 SMDS_VolumeTool vTool;
1829 switch ( nbNodes ) {
1831 SMDS_VolumeOfNodes tmpPenta ( (*columns[0])[z-1], // bottom
1834 (*columns[0])[z], // top
1837 vTool.Set( &tmpPenta );
1838 isForward = vTool.IsForward();
1842 SMDS_VolumeOfNodes tmpHex( (*columns[0])[z-1], (*columns[1])[z-1], // bottom
1843 (*columns[2])[z-1], (*columns[3])[z-1],
1844 (*columns[0])[z], (*columns[1])[z], // top
1845 (*columns[2])[z], (*columns[3])[z] );
1846 vTool.Set( &tmpHex );
1847 isForward = vTool.IsForward();
1851 const int di = (nbNodes+1) / 3;
1852 SMDS_VolumeOfNodes tmpVol ( (*columns[0] )[z-1],
1853 (*columns[di] )[z-1],
1854 (*columns[2*di])[z-1],
1857 (*columns[2*di])[z] );
1858 vTool.Set( &tmpVol );
1859 isForward = vTool.IsForward();
1862 // vertical loop on columns
1864 helper->SetElementsOnShape( true );
1866 switch ( nbNodes ) {
1868 case 3: { // ---------- pentahedra
1869 const int i1 = isForward ? 1 : 2;
1870 const int i2 = isForward ? 2 : 1;
1871 for ( z = 1; z < nbZ; ++z )
1872 helper->AddVolume( (*columns[0 ])[z-1], // bottom
1873 (*columns[i1])[z-1],
1874 (*columns[i2])[z-1],
1875 (*columns[0 ])[z], // top
1877 (*columns[i2])[z] );
1880 case 4: { // ---------- hexahedra
1881 const int i1 = isForward ? 1 : 3;
1882 const int i3 = isForward ? 3 : 1;
1883 for ( z = 1; z < nbZ; ++z )
1884 helper->AddVolume( (*columns[0])[z-1], (*columns[i1])[z-1], // bottom
1885 (*columns[2])[z-1], (*columns[i3])[z-1],
1886 (*columns[0])[z], (*columns[i1])[z], // top
1887 (*columns[2])[z], (*columns[i3])[z] );
1890 case 6: { // ---------- octahedra
1891 const int iBase1 = isForward ? -1 : 0;
1892 const int iBase2 = isForward ? 0 :-1;
1893 for ( z = 1; z < nbZ; ++z )
1894 helper->AddVolume( (*columns[0])[z+iBase1], (*columns[1])[z+iBase1], // bottom or top
1895 (*columns[2])[z+iBase1], (*columns[3])[z+iBase1],
1896 (*columns[4])[z+iBase1], (*columns[5])[z+iBase1],
1897 (*columns[0])[z+iBase2], (*columns[1])[z+iBase2], // top or bottom
1898 (*columns[2])[z+iBase2], (*columns[3])[z+iBase2],
1899 (*columns[4])[z+iBase2], (*columns[5])[z+iBase2] );
1902 default: // ---------- polyhedra
1903 vector<int> quantities( 2 + nbNodes, 4 );
1904 quantities[0] = quantities[1] = nbNodes;
1905 columns.resize( nbNodes + 1 );
1906 columns[ nbNodes ] = columns[ 0 ];
1907 const int i1 = isForward ? 1 : 3;
1908 const int i3 = isForward ? 3 : 1;
1909 const int iBase1 = isForward ? -1 : 0;
1910 const int iBase2 = isForward ? 0 :-1;
1911 vector<const SMDS_MeshNode*> nodes( 2*nbNodes + 4*nbNodes);
1912 for ( z = 1; z < nbZ; ++z )
1914 for ( int i = 0; i < nbNodes; ++i ) {
1915 nodes[ i ] = (*columns[ i ])[z+iBase1]; // bottom or top
1916 nodes[ 2*nbNodes-i-1 ] = (*columns[ i ])[z+iBase2]; // top or bottom
1918 int di = 2*nbNodes + 4*i;
1919 nodes[ di+0 ] = (*columns[i ])[z ];
1920 nodes[ di+i1] = (*columns[i+1])[z ];
1921 nodes[ di+2 ] = (*columns[i+1])[z-1];
1922 nodes[ di+i3] = (*columns[i ])[z-1];
1924 helper->AddPolyhedralVolume( nodes, quantities );
1927 } // switch ( nbNodes )
1930 //================================================================================
1932 * \brief Find correspondence between bottom and top nodes
1933 * If elements on the bottom and top faces are topologically different,
1934 * and projection is possible and allowed, perform the projection
1935 * \retval bool - is a success or not
1937 //================================================================================
1939 bool StdMeshers_Prism_3D::assocOrProjBottom2Top( const gp_Trsf & bottomToTopTrsf,
1940 const Prism_3D::TPrismTopo& thePrism)
1942 SMESH_subMesh * botSM = myHelper->GetMesh()->GetSubMesh( thePrism.myBottom );
1943 SMESH_subMesh * topSM = myHelper->GetMesh()->GetSubMesh( thePrism.myTop );
1945 SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
1946 SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
1948 if ( !botSMDS || botSMDS->NbElements() == 0 )
1950 _gen->Compute( *myHelper->GetMesh(), botSM->GetSubShape(), /*aShapeOnly=*/true );
1951 botSMDS = botSM->GetSubMeshDS();
1952 if ( !botSMDS || botSMDS->NbElements() == 0 )
1953 return toSM( error(TCom("No elements on face #") << botSM->GetId() ));
1956 bool needProject = !topSM->IsMeshComputed();
1957 if ( !needProject &&
1958 (botSMDS->NbElements() != topSMDS->NbElements() ||
1959 botSMDS->NbNodes() != topSMDS->NbNodes()))
1961 MESSAGE("nb elem bot " << botSMDS->NbElements() <<
1962 " top " << ( topSMDS ? topSMDS->NbElements() : 0 ));
1963 MESSAGE("nb node bot " << botSMDS->NbNodes() <<
1964 " top " << ( topSMDS ? topSMDS->NbNodes() : 0 ));
1965 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
1966 <<" and #"<< topSM->GetId() << " seems different" ));
1969 if ( 0/*needProject && !myProjectTriangles*/ )
1970 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
1971 <<" and #"<< topSM->GetId() << " seems different" ));
1972 ///RETURN_BAD_RESULT("Need to project but not allowed");
1974 NSProjUtils::TNodeNodeMap n2nMap;
1975 const NSProjUtils::TNodeNodeMap* n2nMapPtr = & n2nMap;
1978 if ( !projectBottomToTop( bottomToTopTrsf, thePrism ))
1980 n2nMapPtr = & TProjction2dAlgo::instance( this )->GetNodesMap();
1983 if ( !n2nMapPtr || (int) n2nMapPtr->size() < botSMDS->NbNodes() )
1985 // associate top and bottom faces
1986 NSProjUtils::TShapeShapeMap shape2ShapeMap;
1987 const bool sameTopo =
1988 NSProjUtils::FindSubShapeAssociation( thePrism.myBottom, myHelper->GetMesh(),
1989 thePrism.myTop, myHelper->GetMesh(),
1992 for ( size_t iQ = 0; iQ < thePrism.myWallQuads.size(); ++iQ )
1994 const Prism_3D::TQuadList& quadList = thePrism.myWallQuads[iQ];
1995 StdMeshers_FaceSidePtr botSide = quadList.front()->side[ QUAD_BOTTOM_SIDE ];
1996 StdMeshers_FaceSidePtr topSide = quadList.back ()->side[ QUAD_TOP_SIDE ];
1997 if ( botSide->NbEdges() == topSide->NbEdges() )
1999 for ( int iE = 0; iE < botSide->NbEdges(); ++iE )
2001 NSProjUtils::InsertAssociation( botSide->Edge( iE ),
2002 topSide->Edge( iE ), shape2ShapeMap );
2003 NSProjUtils::InsertAssociation( myHelper->IthVertex( 0, botSide->Edge( iE )),
2004 myHelper->IthVertex( 0, topSide->Edge( iE )),
2010 TopoDS_Vertex vb, vt;
2011 StdMeshers_FaceSidePtr sideB, sideT;
2012 vb = myHelper->IthVertex( 0, botSide->Edge( 0 ));
2013 vt = myHelper->IthVertex( 0, topSide->Edge( 0 ));
2014 sideB = quadList.front()->side[ QUAD_LEFT_SIDE ];
2015 sideT = quadList.back ()->side[ QUAD_LEFT_SIDE ];
2016 if ( vb.IsSame( sideB->FirstVertex() ) &&
2017 vt.IsSame( sideT->LastVertex() ))
2019 NSProjUtils::InsertAssociation( botSide->Edge( 0 ),
2020 topSide->Edge( 0 ), shape2ShapeMap );
2021 NSProjUtils::InsertAssociation( vb, vt, shape2ShapeMap );
2023 vb = myHelper->IthVertex( 1, botSide->Edge( botSide->NbEdges()-1 ));
2024 vt = myHelper->IthVertex( 1, topSide->Edge( topSide->NbEdges()-1 ));
2025 sideB = quadList.front()->side[ QUAD_RIGHT_SIDE ];
2026 sideT = quadList.back ()->side[ QUAD_RIGHT_SIDE ];
2027 if ( vb.IsSame( sideB->FirstVertex() ) &&
2028 vt.IsSame( sideT->LastVertex() ))
2030 NSProjUtils::InsertAssociation( botSide->Edge( botSide->NbEdges()-1 ),
2031 topSide->Edge( topSide->NbEdges()-1 ),
2033 NSProjUtils::InsertAssociation( vb, vt, shape2ShapeMap );
2038 // Find matching nodes of top and bottom faces
2039 n2nMapPtr = & n2nMap;
2040 if ( ! NSProjUtils::FindMatchingNodesOnFaces( thePrism.myBottom, myHelper->GetMesh(),
2041 thePrism.myTop, myHelper->GetMesh(),
2042 shape2ShapeMap, n2nMap ))
2045 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
2046 <<" and #"<< topSM->GetId() << " seems different" ));
2048 return toSM( error(TCom("Topology of faces #") << botSM->GetId()
2049 <<" and #"<< topSM->GetId() << " seems different" ));
2053 // Fill myBotToColumnMap
2055 int zSize = myBlock.VerticalSize();
2056 TNodeNodeMap::const_iterator bN_tN = n2nMapPtr->begin();
2057 for ( ; bN_tN != n2nMapPtr->end(); ++bN_tN )
2059 const SMDS_MeshNode* botNode = bN_tN->first;
2060 const SMDS_MeshNode* topNode = bN_tN->second;
2061 if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
2062 continue; // wall columns are contained in myBlock
2063 // create node column
2064 Prism_3D::TNode bN( botNode );
2065 TNode2ColumnMap::iterator bN_col =
2066 myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
2067 TNodeColumn & column = bN_col->second;
2068 column.resize( zSize );
2069 column.front() = botNode;
2070 column.back() = topNode;
2075 //================================================================================
2077 * \brief Remove faces from the top face and re-create them by projection from the bottom
2078 * \retval bool - a success or not
2080 //================================================================================
2082 bool StdMeshers_Prism_3D::projectBottomToTop( const gp_Trsf & bottomToTopTrsf,
2083 const Prism_3D::TPrismTopo& thePrism )
2085 if ( project2dMesh( thePrism.myBottom, thePrism.myTop ))
2089 NSProjUtils::TNodeNodeMap& n2nMap =
2090 (NSProjUtils::TNodeNodeMap&) TProjction2dAlgo::instance( this )->GetNodesMap();
2095 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2096 SMESH_subMesh * botSM = myHelper->GetMesh()->GetSubMesh( thePrism.myBottom );
2097 SMESH_subMesh * topSM = myHelper->GetMesh()->GetSubMesh( thePrism.myTop );
2099 SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
2100 SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
2102 if ( topSMDS && topSMDS->NbElements() > 0 )
2104 //topSM->ComputeStateEngine( SMESH_subMesh::CLEAN ); -- avoid propagation of events
2105 for ( SMDS_ElemIteratorPtr eIt = topSMDS->GetElements(); eIt->more(); )
2106 meshDS->RemoveFreeElement( eIt->next(), topSMDS, /*fromGroups=*/false );
2107 for ( SMDS_NodeIteratorPtr nIt = topSMDS->GetNodes(); nIt->more(); )
2108 meshDS->RemoveFreeNode( nIt->next(), topSMDS, /*fromGroups=*/false );
2111 const TopoDS_Face& botFace = thePrism.myBottom; // oriented within
2112 const TopoDS_Face& topFace = thePrism.myTop; // the 3D SHAPE
2113 int topFaceID = meshDS->ShapeToIndex( thePrism.myTop );
2115 SMESH_MesherHelper botHelper( *myHelper->GetMesh() );
2116 botHelper.SetSubShape( botFace );
2117 botHelper.ToFixNodeParameters( true );
2119 SMESH_MesherHelper topHelper( *myHelper->GetMesh() );
2120 topHelper.SetSubShape( topFace );
2121 topHelper.ToFixNodeParameters( true );
2122 double distXYZ[4], fixTol = 10 * topHelper.MaxTolerance( topFace );
2124 // Fill myBotToColumnMap
2126 int zSize = myBlock.VerticalSize();
2127 Prism_3D::TNode prevTNode;
2128 SMDS_NodeIteratorPtr nIt = botSMDS->GetNodes();
2129 while ( nIt->more() )
2131 const SMDS_MeshNode* botNode = nIt->next();
2132 const SMDS_MeshNode* topNode = 0;
2133 if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
2134 continue; // strange
2136 Prism_3D::TNode bN( botNode );
2137 if ( bottomToTopTrsf.Form() == gp_Identity )
2139 // compute bottom node params
2140 gp_XYZ paramHint(-1,-1,-1);
2141 if ( prevTNode.IsNeighbor( bN ))
2143 paramHint = prevTNode.GetParams();
2144 // double tol = 1e-2 * ( prevTNode.GetCoords() - bN.GetCoords() ).Modulus();
2145 // myBlock.SetTolerance( Min( myBlock.GetTolerance(), tol ));
2147 if ( !myBlock.ComputeParameters( bN.GetCoords(), bN.ChangeParams(),
2148 ID_BOT_FACE, paramHint ))
2149 return toSM( error(TCom("Can't compute normalized parameters for node ")
2150 << botNode->GetID() << " on the face #"<< botSM->GetId() ));
2152 // compute top node coords
2153 gp_XYZ topXYZ; gp_XY topUV;
2154 if ( !myBlock.FacePoint( ID_TOP_FACE, bN.GetParams(), topXYZ ) ||
2155 !myBlock.FaceUV ( ID_TOP_FACE, bN.GetParams(), topUV ))
2156 return toSM( error(TCom("Can't compute coordinates "
2157 "by normalized parameters on the face #")<< topSM->GetId() ));
2158 topNode = meshDS->AddNode( topXYZ.X(),topXYZ.Y(),topXYZ.Z() );
2159 meshDS->SetNodeOnFace( topNode, topFaceID, topUV.X(), topUV.Y() );
2161 else // use bottomToTopTrsf
2163 gp_XYZ coords = bN.GetCoords();
2164 bottomToTopTrsf.Transforms( coords );
2165 topNode = meshDS->AddNode( coords.X(), coords.Y(), coords.Z() );
2166 gp_XY topUV = botHelper.GetNodeUV( botFace, botNode, 0, &checkUV );
2167 meshDS->SetNodeOnFace( topNode, topFaceID, topUV.X(), topUV.Y() );
2169 if ( topHelper.CheckNodeUV( topFace, topNode, topUV, fixTol, /*force=*/false, distXYZ ) &&
2170 distXYZ[0] > fixTol && distXYZ[0] < fixTol * 1e+3 )
2171 meshDS->MoveNode( topNode, distXYZ[1], distXYZ[2], distXYZ[3] ); // transform can be inaccurate
2173 // create node column
2174 TNode2ColumnMap::iterator bN_col =
2175 myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
2176 TNodeColumn & column = bN_col->second;
2177 column.resize( zSize );
2178 column.front() = botNode;
2179 column.back() = topNode;
2181 n2nMap.insert( n2nMap.end(), make_pair( botNode, topNode ));
2183 if ( _computeCanceled )
2184 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
2189 const bool oldSetElemsOnShape = myHelper->SetElementsOnShape( false );
2191 // care of orientation;
2192 // if the bottom faces is orienetd OK then top faces must be reversed
2193 bool reverseTop = true;
2194 if ( myHelper->NbAncestors( botFace, *myBlock.Mesh(), TopAbs_SOLID ) > 1 )
2195 reverseTop = ! myHelper->IsReversedSubMesh( botFace );
2196 int iFrw, iRev, *iPtr = &( reverseTop ? iRev : iFrw );
2198 // loop on bottom mesh faces
2199 SMDS_ElemIteratorPtr faceIt = botSMDS->GetElements();
2200 vector< const SMDS_MeshNode* > nodes;
2201 while ( faceIt->more() )
2203 const SMDS_MeshElement* face = faceIt->next();
2204 if ( !face || face->GetType() != SMDSAbs_Face )
2207 // find top node in columns for each bottom node
2208 int nbNodes = face->NbCornerNodes();
2209 nodes.resize( nbNodes );
2210 for ( iFrw = 0, iRev = nbNodes-1; iFrw < nbNodes; ++iFrw, --iRev )
2212 const SMDS_MeshNode* n = face->GetNode( *iPtr );
2213 if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
2214 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.find( n );
2215 if ( bot_column == myBotToColumnMap.end() )
2216 return toSM( error(TCom("No nodes found above node ") << n->GetID() ));
2217 nodes[ iFrw ] = bot_column->second.back();
2220 const TNodeColumn* column = myBlock.GetNodeColumn( n );
2222 return toSM( error(TCom("No side nodes found above node ") << n->GetID() ));
2223 nodes[ iFrw ] = column->back();
2226 SMDS_MeshElement* newFace = 0;
2227 switch ( nbNodes ) {
2230 newFace = myHelper->AddFace(nodes[0], nodes[1], nodes[2]);
2234 newFace = myHelper->AddFace( nodes[0], nodes[1], nodes[2], nodes[3] );
2238 newFace = meshDS->AddPolygonalFace( nodes );
2241 meshDS->SetMeshElementOnShape( newFace, topFaceID );
2244 myHelper->SetElementsOnShape( oldSetElemsOnShape );
2246 // Check the projected mesh
2248 if ( thePrism.myNbEdgesInWires.size() > 1 && // there are holes
2249 topHelper.IsDistorted2D( topSM, /*checkUV=*/false ))
2251 SMESH_MeshEditor editor( topHelper.GetMesh() );
2253 // smooth in 2D or 3D?
2254 TopLoc_Location loc;
2255 Handle(Geom_Surface) surface = BRep_Tool::Surface( topFace, loc );
2256 bool isPlanar = GeomLib_IsPlanarSurface( surface ).IsPlanar();
2258 bool isFixed = false;
2259 set<const SMDS_MeshNode*> fixedNodes;
2260 for ( int iAttemp = 0; !isFixed && iAttemp < 10; ++iAttemp )
2262 TIDSortedElemSet faces;
2263 for ( faceIt = topSMDS->GetElements(); faceIt->more(); )
2264 faces.insert( faces.end(), faceIt->next() );
2266 SMESH_MeshEditor::SmoothMethod algo =
2267 iAttemp ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
2270 editor.Smooth( faces, fixedNodes, algo, /*nbIterations=*/ 10,
2271 /*theTgtAspectRatio=*/1.0, /*the2D=*/!isPlanar);
2273 isFixed = !topHelper.IsDistorted2D( topSM, /*checkUV=*/true );
2276 return toSM( error( TCom("Projection from face #") << botSM->GetId()
2277 << " to face #" << topSM->GetId()
2278 << " failed: inverted elements created"));
2284 //=======================================================================
2285 //function : getSweepTolerance
2286 //purpose : Compute tolerance to pass to StdMeshers_Sweeper
2287 //=======================================================================
2289 double StdMeshers_Prism_3D::getSweepTolerance( const Prism_3D::TPrismTopo& thePrism )
2291 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2292 SMESHDS_SubMesh * sm[2] = { meshDS->MeshElements( thePrism.myBottom ),
2293 meshDS->MeshElements( thePrism.myTop ) };
2294 double minDist = 1e100;
2296 vector< SMESH_TNodeXYZ > nodes;
2297 for ( int iSM = 0; iSM < 2; ++iSM )
2299 if ( !sm[ iSM ]) continue;
2301 SMDS_ElemIteratorPtr fIt = sm[ iSM ]->GetElements();
2302 while ( fIt->more() )
2304 const SMDS_MeshElement* face = fIt->next();
2305 const int nbNodes = face->NbCornerNodes();
2306 SMDS_ElemIteratorPtr nIt = face->nodesIterator();
2308 nodes.resize( nbNodes + 1 );
2309 for ( int iN = 0; iN < nbNodes; ++iN )
2310 nodes[ iN ] = nIt->next();
2311 nodes.back() = nodes[0];
2315 for ( int iN = 0; iN < nbNodes; ++iN )
2317 if ( nodes[ iN ]._node->GetPosition()->GetDim() < 2 &&
2318 nodes[ iN+1 ]._node->GetPosition()->GetDim() < 2 )
2320 // it's a boundary link; measure distance of other
2321 // nodes to this link
2322 gp_XYZ linkDir = nodes[ iN ] - nodes[ iN+1 ];
2323 double linkLen = linkDir.Modulus();
2324 bool isDegen = ( linkLen < numeric_limits<double>::min() );
2325 if ( !isDegen ) linkDir /= linkLen;
2326 for ( int iN2 = 0; iN2 < nbNodes; ++iN2 ) // loop on other nodes
2328 if ( nodes[ iN2 ] == nodes[ iN ] ||
2329 nodes[ iN2 ] == nodes[ iN+1 ]) continue;
2332 dist2 = ( nodes[ iN ] - nodes[ iN2 ]).SquareModulus();
2336 dist2 = linkDir.CrossSquareMagnitude( nodes[ iN ] - nodes[ iN2 ]);
2338 if ( dist2 > numeric_limits<double>::min() )
2339 minDist = Min ( minDist, dist2 );
2342 // measure length link
2343 else if ( nodes[ iN ]._node < nodes[ iN+1 ]._node ) // not to measure same link twice
2345 dist2 = ( nodes[ iN ] - nodes[ iN+1 ]).SquareModulus();
2346 if ( dist2 > numeric_limits<double>::min() )
2347 minDist = Min ( minDist, dist2 );
2352 return 0.1 * Sqrt ( minDist );
2355 //=======================================================================
2356 //function : isSimpleQuad
2357 //purpose : check if the bottom FACE is meshable with nice qudrangles,
2358 // if so the block aproach can work rather fast.
2359 // This is a temporary mean caused by problems in StdMeshers_Sweeper
2360 //=======================================================================
2362 bool StdMeshers_Prism_3D::isSimpleBottom( const Prism_3D::TPrismTopo& thePrism )
2364 // analyse angles between edges
2365 double nbConcaveAng = 0, nbConvexAng = 0;
2366 TopoDS_Face reverseBottom = TopoDS::Face( thePrism.myBottom.Reversed() ); // see initPrism()
2367 TopoDS_Vertex commonV;
2368 const list< TopoDS_Edge >& botEdges = thePrism.myBottomEdges;
2369 list< TopoDS_Edge >::const_iterator edge = botEdges.begin();
2370 while ( edge != botEdges.end() )
2372 if ( SMESH_Algo::isDegenerated( *edge ))
2374 TopoDS_Edge e1 = *edge++;
2375 TopoDS_Edge e2 = ( edge == botEdges.end() ? botEdges.front() : *edge );
2376 if ( ! TopExp::CommonVertex( e1, e2, commonV ))
2378 e2 = botEdges.front();
2379 if ( ! TopExp::CommonVertex( e1, e2, commonV ))
2382 double angle = myHelper->GetAngle( e1, e2, reverseBottom, commonV );
2383 if ( angle < -5 * M_PI/180 )
2384 if ( ++nbConcaveAng > 1 )
2386 if ( angle > 85 * M_PI/180 )
2387 if ( ++nbConvexAng > 4 )
2393 //=======================================================================
2394 //function : project2dMesh
2395 //purpose : Project mesh faces from a source FACE of one prism (theSrcFace)
2396 // to a source FACE of another prism (theTgtFace)
2397 //=======================================================================
2399 bool StdMeshers_Prism_3D::project2dMesh(const TopoDS_Face& theSrcFace,
2400 const TopoDS_Face& theTgtFace)
2402 TProjction2dAlgo* projector2D = TProjction2dAlgo::instance( this );
2403 projector2D->myHyp.SetSourceFace( theSrcFace );
2404 bool ok = projector2D->Compute( *myHelper->GetMesh(), theTgtFace );
2406 SMESH_subMesh* tgtSM = myHelper->GetMesh()->GetSubMesh( theTgtFace );
2407 if ( !ok && tgtSM->GetSubMeshDS() ) {
2408 //tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN ); -- avoid propagation of events
2409 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2410 SMESHDS_SubMesh* tgtSMDS = tgtSM->GetSubMeshDS();
2411 for ( SMDS_ElemIteratorPtr eIt = tgtSMDS->GetElements(); eIt->more(); )
2412 meshDS->RemoveFreeElement( eIt->next(), tgtSMDS, /*fromGroups=*/false );
2413 for ( SMDS_NodeIteratorPtr nIt = tgtSMDS->GetNodes(); nIt->more(); )
2414 meshDS->RemoveFreeNode( nIt->next(), tgtSMDS, /*fromGroups=*/false );
2416 tgtSM->ComputeStateEngine ( SMESH_subMesh::CHECK_COMPUTE_STATE );
2417 tgtSM->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
2422 //================================================================================
2424 * \brief Set projection coordinates of a node to a face and it's sub-shapes
2425 * \param faceID - the face given by in-block ID
2426 * \param params - node normalized parameters
2427 * \retval bool - is a success
2429 //================================================================================
2431 bool StdMeshers_Prism_3D::setFaceAndEdgesXYZ( const int faceID, const gp_XYZ& params, int z )
2433 // find base and top edges of the face
2434 enum { BASE = 0, TOP, LEFT, RIGHT };
2435 vector< int > edgeVec; // 0-base, 1-top
2436 SMESH_Block::GetFaceEdgesIDs( faceID, edgeVec );
2438 myBlock.EdgePoint( edgeVec[ BASE ], params, myShapeXYZ[ edgeVec[ BASE ]]);
2439 myBlock.EdgePoint( edgeVec[ TOP ], params, myShapeXYZ[ edgeVec[ TOP ]]);
2441 SHOWYXZ("\nparams ", params);
2442 SHOWYXZ("TOP is " <<edgeVec[ TOP ], myShapeXYZ[ edgeVec[ TOP]]);
2443 SHOWYXZ("BASE is "<<edgeVec[ BASE], myShapeXYZ[ edgeVec[ BASE]]);
2445 if ( faceID == SMESH_Block::ID_Fx0z || faceID == SMESH_Block::ID_Fx1z )
2447 myBlock.EdgePoint( edgeVec[ LEFT ], params, myShapeXYZ[ edgeVec[ LEFT ]]);
2448 myBlock.EdgePoint( edgeVec[ RIGHT ], params, myShapeXYZ[ edgeVec[ RIGHT ]]);
2450 SHOWYXZ("VER "<<edgeVec[ LEFT], myShapeXYZ[ edgeVec[ LEFT]]);
2451 SHOWYXZ("VER "<<edgeVec[ RIGHT], myShapeXYZ[ edgeVec[ RIGHT]]);
2453 myBlock.FacePoint( faceID, params, myShapeXYZ[ faceID ]);
2454 SHOWYXZ("FacePoint "<<faceID, myShapeXYZ[ faceID]);
2459 //=======================================================================
2461 //purpose : If (!isOK), sets the error to a sub-mesh of a current SOLID
2462 //=======================================================================
2464 bool StdMeshers_Prism_3D::toSM( bool isOK )
2466 if ( mySetErrorToSM &&
2469 !myHelper->GetSubShape().IsNull() &&
2470 myHelper->GetSubShape().ShapeType() == TopAbs_SOLID)
2472 SMESH_subMesh* sm = myHelper->GetMesh()->GetSubMesh( myHelper->GetSubShape() );
2473 sm->GetComputeError() = this->GetComputeError();
2474 // clear error in order not to return it twice
2475 _error = COMPERR_OK;
2481 //=======================================================================
2482 //function : shapeID
2483 //purpose : Return index of a shape
2484 //=======================================================================
2486 int StdMeshers_Prism_3D::shapeID( const TopoDS_Shape& S )
2488 if ( S.IsNull() ) return 0;
2489 if ( !myHelper ) return -3;
2490 return myHelper->GetMeshDS()->ShapeToIndex( S );
2493 namespace // utils used by StdMeshers_Prism_3D::IsApplicable()
2495 struct EdgeWithNeighbors
2499 EdgeWithNeighbors(const TopoDS_Edge& E, int iE, int nbE, int shift = 0 ):
2501 _iL( SMESH_MesherHelper::WrapIndex( iE-1, nbE ) + shift ),
2502 _iR( SMESH_MesherHelper::WrapIndex( iE+1, nbE ) + shift )
2505 EdgeWithNeighbors() {}
2510 TopTools_IndexedMapOfShape *_faces; // pointer because its copy constructor is private
2511 TopoDS_Edge _topEdge;
2512 vector< EdgeWithNeighbors >*_edges;
2514 vector< bool > _isCheckedEdge;
2515 int _nbCheckedEdges; // nb of EDGEs whose location is defined
2516 PrismSide *_leftSide;
2517 PrismSide *_rightSide;
2518 void SetExcluded() { _leftSide = _rightSide = NULL; }
2519 bool IsExcluded() const { return !_leftSide; }
2520 const TopoDS_Edge& Edge( int i ) const
2522 return (*_edges)[ i ]._edge;
2524 int FindEdge( const TopoDS_Edge& E ) const
2526 for ( size_t i = 0; i < _edges->size(); ++i )
2527 if ( E.IsSame( Edge( i ))) return i;
2530 bool IsSideFace( const TopoDS_Shape& face ) const
2532 if ( _faces->Contains( face )) // avoid returning true for a prism top FACE
2533 return ( !_face.IsNull() || !( face.IsSame( _faces->FindKey( _faces->Extent() ))));
2537 //--------------------------------------------------------------------------------
2539 * \brief Return ordered edges of a face
2541 bool getEdges( const TopoDS_Face& face,
2542 vector< EdgeWithNeighbors > & edges,
2543 const bool noHolesAllowed)
2545 list< TopoDS_Edge > ee;
2546 list< int > nbEdgesInWires;
2547 int nbW = SMESH_Block::GetOrderedEdges( face, ee, nbEdgesInWires );
2548 if ( nbW > 1 && noHolesAllowed )
2552 list< TopoDS_Edge >::iterator e = ee.begin();
2553 list< int >::iterator nbE = nbEdgesInWires.begin();
2554 for ( ; nbE != nbEdgesInWires.end(); ++nbE )
2555 for ( iE = 0; iE < *nbE; ++e, ++iE )
2556 if ( SMESH_Algo::isDegenerated( *e ))
2558 e = --ee.erase( e );
2564 e->Orientation( TopAbs_FORWARD ); // for operator==() to work
2569 for ( nbE = nbEdgesInWires.begin(); nbE != nbEdgesInWires.end(); ++nbE )
2571 for ( iE = 0; iE < *nbE; ++e, ++iE )
2572 edges.push_back( EdgeWithNeighbors( *e, iE, *nbE, nbTot ));
2575 return edges.size();
2577 //--------------------------------------------------------------------------------
2579 * \brief Return another faces sharing an edge
2581 const TopoDS_Face & getAnotherFace( const TopoDS_Face& face,
2582 const TopoDS_Edge& edge,
2583 TopTools_IndexedDataMapOfShapeListOfShape& facesOfEdge)
2585 TopTools_ListIteratorOfListOfShape faceIt( facesOfEdge.FindFromKey( edge ));
2586 for ( ; faceIt.More(); faceIt.Next() )
2587 if ( !face.IsSame( faceIt.Value() ))
2588 return TopoDS::Face( faceIt.Value() );
2592 //--------------------------------------------------------------------------------
2594 * \brief Return number of faces sharing given edges
2596 int nbAdjacentFaces( const std::vector< EdgeWithNeighbors >& edges,
2597 const TopTools_IndexedDataMapOfShapeListOfShape& facesOfEdge )
2599 TopTools_MapOfShape adjFaces;
2601 for ( size_t i = 0; i < edges.size(); ++i )
2603 TopTools_ListIteratorOfListOfShape faceIt( facesOfEdge.FindFromKey( edges[i]._edge ));
2604 for ( ; faceIt.More(); faceIt.Next() )
2605 adjFaces.Add( faceIt.Value() );
2607 return adjFaces.Extent();
2611 //================================================================================
2613 * \brief Return true if the algorithm can mesh this shape
2614 * \param [in] aShape - shape to check
2615 * \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
2616 * else, returns OK if at least one shape is OK
2618 //================================================================================
2620 bool StdMeshers_Prism_3D::IsApplicable(const TopoDS_Shape & shape, bool toCheckAll)
2622 TopExp_Explorer sExp( shape, TopAbs_SOLID );
2626 for ( ; sExp.More(); sExp.Next() )
2630 TopExp_Explorer shExp( sExp.Current(), TopAbs_SHELL );
2631 if ( shExp.More() ) {
2632 shell = shExp.Current();
2637 if ( shell.IsNull() ) {
2638 if ( toCheckAll ) return false;
2642 TopTools_IndexedMapOfShape allFaces;
2643 TopExp::MapShapes( shell, TopAbs_FACE, allFaces );
2644 if ( allFaces.Extent() < 3 ) {
2645 if ( toCheckAll ) return false;
2649 if ( allFaces.Extent() == 6 )
2651 TopTools_IndexedMapOfOrientedShape map;
2652 bool isBox = SMESH_Block::FindBlockShapes( TopoDS::Shell( shell ),
2653 TopoDS_Vertex(), TopoDS_Vertex(), map );
2655 if ( !toCheckAll ) return true;
2660 TopTools_IndexedMapOfShape allShapes;
2661 TopExp::MapShapes( shape, allShapes );
2664 TopTools_IndexedDataMapOfShapeListOfShape facesOfEdge;
2665 TopTools_ListIteratorOfListOfShape faceIt;
2666 TopExp::MapShapesAndAncestors( sExp.Current(), TopAbs_EDGE, TopAbs_FACE , facesOfEdge );
2667 if ( facesOfEdge.IsEmpty() ) {
2668 if ( toCheckAll ) return false;
2672 typedef vector< EdgeWithNeighbors > TEdgeWithNeighborsVec;
2673 vector< TEdgeWithNeighborsVec > faceEdgesVec( allFaces.Extent() + 1 );
2674 const size_t nbEdgesMax = facesOfEdge.Extent() * 2; // there can be seam EDGES
2675 TopTools_IndexedMapOfShape* facesOfSide = new TopTools_IndexedMapOfShape[ nbEdgesMax ];
2676 SMESHUtils::ArrayDeleter<TopTools_IndexedMapOfShape> delFacesOfSide( facesOfSide );
2678 // try to use each face as a bottom one
2679 bool prismDetected = false;
2680 vector< PrismSide > sides;
2681 for ( int iF = 1; iF < allFaces.Extent() && !prismDetected; ++iF )
2683 const TopoDS_Face& botF = TopoDS::Face( allFaces( iF ));
2685 TEdgeWithNeighborsVec& botEdges = faceEdgesVec[ iF ];
2686 if ( botEdges.empty() )
2687 if ( !getEdges( botF, botEdges, /*noHoles=*/false ))
2689 if ( allFaces.Extent()-1 <= (int) botEdges.size() )
2690 continue; // all faces are adjacent to botF - no top FACE
2692 // init data of side FACEs
2694 sides.resize( botEdges.size() );
2695 for ( size_t iS = 0; iS < botEdges.size(); ++iS )
2697 sides[ iS ]._topEdge = botEdges[ iS ]._edge;
2698 sides[ iS ]._face = botF;
2699 sides[ iS ]._leftSide = & sides[ botEdges[ iS ]._iR ];
2700 sides[ iS ]._rightSide = & sides[ botEdges[ iS ]._iL ];
2701 sides[ iS ]._faces = & facesOfSide[ iS ];
2702 sides[ iS ]._faces->Clear();
2705 bool isOK = true; // ok for a current botF
2706 bool isAdvanced = true; // is new data found in a current loop
2707 int nbFoundSideFaces = 0;
2708 for ( int iLoop = 0; isOK && isAdvanced; ++iLoop )
2711 for ( size_t iS = 0; iS < sides.size() && isOK; ++iS )
2713 PrismSide& side = sides[ iS ];
2714 if ( side._face.IsNull() )
2715 continue; // probably the prism top face is the last of side._faces
2717 if ( side._topEdge.IsNull() )
2719 // find vertical EDGEs --- EGDEs shared with neighbor side FACEs
2720 for ( int is2nd = 0; is2nd < 2 && isOK; ++is2nd ) // 2 adjacent neighbors
2722 int di = is2nd ? 1 : -1;
2723 const PrismSide* adjSide = is2nd ? side._rightSide : side._leftSide;
2724 for ( size_t i = 1; i < side._edges->size(); ++i )
2726 int iE = SMESH_MesherHelper::WrapIndex( i*di + side._iBotEdge, side._edges->size());
2727 if ( side._isCheckedEdge[ iE ] ) continue;
2728 const TopoDS_Edge& vertE = side.Edge( iE );
2729 const TopoDS_Shape& neighborF = getAnotherFace( side._face, vertE, facesOfEdge );
2730 bool isEdgeShared = adjSide->IsSideFace( neighborF );
2731 if ( isEdgeShared ) // vertE is shared with adjSide
2734 side._isCheckedEdge[ iE ] = true;
2735 side._nbCheckedEdges++;
2736 int nbNotCheckedE = side._edges->size() - side._nbCheckedEdges;
2737 if ( nbNotCheckedE == 1 )
2742 if ( i == 1 && iLoop == 0 ) isOK = false;
2748 int nbNotCheckedE = side._edges->size() - side._nbCheckedEdges;
2749 if ( nbNotCheckedE == 1 )
2751 vector<bool>::iterator ii = std::find( side._isCheckedEdge.begin(),
2752 side._isCheckedEdge.end(), false );
2753 if ( ii != side._isCheckedEdge.end() )
2755 size_t iE = std::distance( side._isCheckedEdge.begin(), ii );
2756 side._topEdge = side.Edge( iE );
2759 isOK = ( nbNotCheckedE >= 1 );
2761 else //if ( !side._topEdge.IsNull() )
2763 // get a next face of a side
2764 const TopoDS_Shape& f = getAnotherFace( side._face, side._topEdge, facesOfEdge );
2765 side._faces->Add( f );
2767 if ( f.IsSame( side._face ) || // _topEdge is a seam
2768 SMESH_MesherHelper::Count( f, TopAbs_WIRE, false ) != 1 )
2772 else if ( side._leftSide != & side ) // not closed side face
2774 if ( side._leftSide->_faces->Contains( f ))
2776 stop = true; // probably f is the prism top face
2777 side._leftSide->_face.Nullify();
2778 side._leftSide->_topEdge.Nullify();
2780 if ( side._rightSide->_faces->Contains( f ))
2782 stop = true; // probably f is the prism top face
2783 side._rightSide->_face.Nullify();
2784 side._rightSide->_topEdge.Nullify();
2789 side._face.Nullify();
2790 side._topEdge.Nullify();
2793 side._face = TopoDS::Face( f );
2794 int faceID = allFaces.FindIndex( side._face );
2795 side._edges = & faceEdgesVec[ faceID ];
2796 if ( side._edges->empty() )
2797 if ( !getEdges( side._face, * side._edges, /*noHoles=*/true ))
2799 const int nbE = side._edges->size();
2804 side._iBotEdge = side.FindEdge( side._topEdge );
2805 side._isCheckedEdge.clear();
2806 side._isCheckedEdge.resize( nbE, false );
2807 side._isCheckedEdge[ side._iBotEdge ] = true;
2808 side._nbCheckedEdges = 1; // bottom EDGE is known
2810 side._topEdge.Nullify();
2811 isOK = ( !side._edges->empty() || side._faces->Extent() > 1 );
2813 } //if ( !side._topEdge.IsNull() )
2815 } // loop on prism sides
2817 if ( nbFoundSideFaces > allFaces.Extent() )
2821 if ( iLoop > allFaces.Extent() * 10 )
2825 cerr << "BUG: infinite loop in StdMeshers_Prism_3D::IsApplicable()" << endl;
2828 } // while isAdvanced
2830 if ( isOK && sides[0]._faces->Extent() > 1 )
2832 const int nbFaces = sides[0]._faces->Extent();
2833 if ( botEdges.size() == 1 ) // cylinder
2835 prismDetected = ( nbFaces == allFaces.Extent()-1 );
2839 const TopoDS_Shape& topFace = sides[0]._faces->FindKey( nbFaces );
2841 for ( iS = 1; iS < sides.size(); ++iS )
2842 if ( ! sides[ iS ]._faces->Contains( topFace ))
2844 prismDetected = ( iS == sides.size() );
2847 } // loop on allFaces
2849 if ( !prismDetected && toCheckAll ) return false;
2850 if ( prismDetected && !toCheckAll ) return true;
2859 //================================================================================
2861 * \brief Return true if this node and other one belong to one face
2863 //================================================================================
2865 bool Prism_3D::TNode::IsNeighbor( const Prism_3D::TNode& other ) const
2867 if ( !other.myNode || !myNode ) return false;
2869 SMDS_ElemIteratorPtr fIt = other.myNode->GetInverseElementIterator(SMDSAbs_Face);
2870 while ( fIt->more() )
2871 if ( fIt->next()->GetNodeIndex( myNode ) >= 0 )
2876 //================================================================================
2878 * \brief Prism initialization
2880 //================================================================================
2882 void TPrismTopo::Clear()
2884 myShape3D.Nullify();
2887 myWallQuads.clear();
2888 myBottomEdges.clear();
2889 myNbEdgesInWires.clear();
2890 myWallQuads.clear();
2893 //================================================================================
2895 * \brief Set upside-down
2897 //================================================================================
2899 void TPrismTopo::SetUpsideDown()
2901 std::swap( myBottom, myTop );
2902 myBottomEdges.clear();
2903 std::reverse( myBottomEdges.begin(), myBottomEdges.end() );
2904 for ( size_t i = 0; i < myWallQuads.size(); ++i )
2906 myWallQuads[i].reverse();
2907 TQuadList::iterator q = myWallQuads[i].begin();
2908 for ( ; q != myWallQuads[i].end(); ++q )
2910 (*q)->shift( 2, /*keepUnitOri=*/true );
2912 myBottomEdges.push_back( myWallQuads[i].front()->side[ QUAD_BOTTOM_SIDE ].grid->Edge(0) );
2916 } // namespace Prism_3D
2918 //================================================================================
2920 * \brief Constructor. Initialization is needed
2922 //================================================================================
2924 StdMeshers_PrismAsBlock::StdMeshers_PrismAsBlock()