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>
69 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
70 #define gpXYZ(n) SMESH_TNodeXYZ(n)
73 #define DBGOUT(msg) //cout << msg << endl;
74 #define SHOWYXZ(msg, xyz) \
75 //{ gp_Pnt p (xyz); cout << msg << " ("<< p.X() << "; " <<p.Y() << "; " <<p.Z() << ") " <<endl; }
78 #define SHOWYXZ(msg, xyz)
81 namespace NSProjUtils = StdMeshers_ProjectionUtils;
83 typedef SMESH_Comment TCom;
85 enum { ID_BOT_FACE = SMESH_Block::ID_Fxy0,
86 ID_TOP_FACE = SMESH_Block::ID_Fxy1,
87 BOTTOM_EDGE = 0, TOP_EDGE, V0_EDGE, V1_EDGE, // edge IDs in face
88 NB_WALL_FACES = 4 }; //
92 //=======================================================================
94 * \brief Quadrangle algorithm
96 struct TQuadrangleAlgo : public StdMeshers_Quadrangle_2D
98 TQuadrangleAlgo(int studyId, SMESH_Gen* gen)
99 : StdMeshers_Quadrangle_2D( gen->GetANewId(), studyId, gen)
102 static StdMeshers_Quadrangle_2D* instance( SMESH_Algo* fatherAlgo,
103 SMESH_MesherHelper* helper=0)
105 static TQuadrangleAlgo* algo = new TQuadrangleAlgo( fatherAlgo->GetStudyId(),
106 fatherAlgo->GetGen() );
109 algo->myProxyMesh->GetMesh() != helper->GetMesh() )
110 algo->myProxyMesh.reset( new SMESH_ProxyMesh( *helper->GetMesh() ));
112 algo->myQuadList.clear();
115 algo->_quadraticMesh = helper->GetIsQuadratic();
120 //=======================================================================
122 * \brief Algorithm projecting 1D mesh
124 struct TProjction1dAlgo : public StdMeshers_Projection_1D
126 StdMeshers_ProjectionSource1D myHyp;
128 TProjction1dAlgo(int studyId, SMESH_Gen* gen)
129 : StdMeshers_Projection_1D( gen->GetANewId(), studyId, gen),
130 myHyp( gen->GetANewId(), studyId, gen)
132 StdMeshers_Projection_1D::_sourceHypo = & myHyp;
134 static TProjction1dAlgo* instance( SMESH_Algo* fatherAlgo )
136 static TProjction1dAlgo* algo = new TProjction1dAlgo( fatherAlgo->GetStudyId(),
137 fatherAlgo->GetGen() );
141 //=======================================================================
143 * \brief Algorithm projecting 2D mesh
145 struct TProjction2dAlgo : public StdMeshers_Projection_1D2D
147 StdMeshers_ProjectionSource2D myHyp;
149 TProjction2dAlgo(int studyId, SMESH_Gen* gen)
150 : StdMeshers_Projection_1D2D( gen->GetANewId(), studyId, gen),
151 myHyp( gen->GetANewId(), studyId, gen)
153 StdMeshers_Projection_2D::_sourceHypo = & myHyp;
155 static TProjction2dAlgo* instance( SMESH_Algo* fatherAlgo )
157 static TProjction2dAlgo* algo = new TProjction2dAlgo( fatherAlgo->GetStudyId(),
158 fatherAlgo->GetGen() );
161 const NSProjUtils::TNodeNodeMap& GetNodesMap()
163 return _src2tgtNodes;
166 //=======================================================================
168 * \brief Returns already computed EDGEs
170 void getPrecomputedEdges( SMESH_MesherHelper& theHelper,
171 const TopoDS_Shape& theShape,
172 vector< TopoDS_Edge >& theEdges)
176 SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
179 TopTools_IndexedMapOfShape edges;
180 TopExp::MapShapes( theShape, TopAbs_EDGE, edges );
181 for ( int iE = 1; iE <= edges.Extent(); ++iE )
183 const TopoDS_Shape edge = edges( iE );
184 if (( ! ( sm = meshDS->MeshElements( edge ))) ||
185 ( sm->NbElements() == 0 ))
188 // there must not be FACEs meshed with triangles and sharing a computed EDGE
189 // as the precomputed EDGEs are used for propagation other to 'vertical' EDGEs
190 bool faceFound = false;
191 PShapeIteratorPtr faceIt =
192 theHelper.GetAncestors( edge, *theHelper.GetMesh(), TopAbs_FACE );
193 while ( const TopoDS_Shape* face = faceIt->next() )
195 if (( sm = meshDS->MeshElements( *face )) &&
196 ( sm->NbElements() > 0 ) &&
197 ( !theHelper.IsSameElemGeometry( sm, SMDSGeom_QUADRANGLE ) ))
203 theEdges.push_back( TopoDS::Edge( edge ));
207 //================================================================================
209 * \brief Make \a botE be the BOTTOM_SIDE of \a quad.
210 * Return false if the BOTTOM_SIDE is composite
212 //================================================================================
214 bool setBottomEdge( const TopoDS_Edge& botE,
215 FaceQuadStruct::Ptr& quad,
216 const TopoDS_Shape& face)
218 quad->side[ QUAD_TOP_SIDE ].grid->Reverse();
219 quad->side[ QUAD_LEFT_SIDE ].grid->Reverse();
221 bool isComposite = false;
222 for ( size_t i = 0; i < quad->side.size(); ++i )
224 StdMeshers_FaceSidePtr quadSide = quad->side[i];
225 for ( int iE = 0; iE < quadSide->NbEdges(); ++iE )
226 if ( botE.IsSame( quadSide->Edge( iE )))
228 if ( quadSide->NbEdges() > 1 )
229 isComposite = true; //return false;
231 i = quad->side.size(); // to quit from the outer loop
235 if ( edgeIndex != QUAD_BOTTOM_SIDE )
236 quad->shift( quad->side.size() - edgeIndex, /*keepUnitOri=*/false );
238 quad->face = TopoDS::Face( face );
243 //================================================================================
245 * \brief Return iterator pointing to node column for the given parameter
246 * \param columnsMap - node column map
247 * \param parameter - parameter
248 * \retval TParam2ColumnMap::iterator - result
250 * it returns closest left column
252 //================================================================================
254 TParam2ColumnIt getColumn( const TParam2ColumnMap* columnsMap,
255 const double parameter )
257 TParam2ColumnIt u_col = columnsMap->upper_bound( parameter );
258 if ( u_col != columnsMap->begin() )
260 return u_col; // return left column
263 //================================================================================
265 * \brief Return nodes around given parameter and a ratio
266 * \param column - node column
267 * \param param - parameter
268 * \param node1 - lower node
269 * \param node2 - upper node
270 * \retval double - ratio
272 //================================================================================
274 double getRAndNodes( const TNodeColumn* column,
276 const SMDS_MeshNode* & node1,
277 const SMDS_MeshNode* & node2)
279 if ( param >= 1.0 || column->size() == 1) {
280 node1 = node2 = column->back();
284 int i = int( param * ( column->size() - 1 ));
285 double u0 = double( i )/ double( column->size() - 1 );
286 double r = ( param - u0 ) * ( column->size() - 1 );
288 node1 = (*column)[ i ];
289 node2 = (*column)[ i + 1];
293 //================================================================================
295 * \brief Compute boundary parameters of face parts
296 * \param nbParts - nb of parts to split columns into
297 * \param columnsMap - node columns of the face to split
298 * \param params - computed parameters
300 //================================================================================
302 void splitParams( const int nbParts,
303 const TParam2ColumnMap* columnsMap,
304 vector< double > & params)
307 params.reserve( nbParts + 1 );
308 TParam2ColumnIt last_par_col = --columnsMap->end();
309 double par = columnsMap->begin()->first; // 0.
310 double parLast = last_par_col->first;
311 params.push_back( par );
312 for ( int i = 0; i < nbParts - 1; ++ i )
314 double partSize = ( parLast - par ) / double ( nbParts - i );
315 TParam2ColumnIt par_col = getColumn( columnsMap, par + partSize );
316 if ( par_col->first == par ) {
318 if ( par_col == last_par_col ) {
319 while ( i < nbParts - 1 )
320 params.push_back( par + partSize * i++ );
324 par = par_col->first;
325 params.push_back( par );
327 params.push_back( parLast ); // 1.
330 //================================================================================
332 * \brief Return coordinate system for z-th layer of nodes
334 //================================================================================
336 gp_Ax2 getLayerCoordSys(const int z,
337 const vector< const TNodeColumn* >& columns,
340 // gravity center of a layer
343 for ( size_t i = 0; i < columns.size(); ++i )
345 O += gpXYZ( (*columns[ i ])[ z ]);
346 if ( vertexCol < 0 &&
347 columns[ i ]->front()->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
354 int iPrev = columns.size()-1;
355 for ( size_t i = 0; i < columns.size(); ++i )
357 gp_Vec v1( O, gpXYZ( (*columns[ iPrev ])[ z ]));
358 gp_Vec v2( O, gpXYZ( (*columns[ i ] )[ z ]));
363 if ( vertexCol >= 0 )
365 O = gpXYZ( (*columns[ vertexCol ])[ z ]);
367 if ( xColumn < 0 || xColumn >= (int) columns.size() )
369 // select a column for X dir
371 for ( size_t i = 0; i < columns.size(); ++i )
373 double dist = ( O - gpXYZ((*columns[ i ])[ z ])).SquareModulus();
374 if ( dist > maxDist )
383 gp_Vec X( O, gpXYZ( (*columns[ xColumn ])[ z ]));
385 return gp_Ax2( O, Z, X);
388 //================================================================================
390 * \brief Removes submeshes that are or can be meshed with regular grid from given list
391 * \retval int - nb of removed submeshes
393 //================================================================================
395 int removeQuasiQuads(list< SMESH_subMesh* >& notQuadSubMesh,
396 SMESH_MesherHelper* helper,
397 StdMeshers_Quadrangle_2D* quadAlgo)
400 //SMESHDS_Mesh* mesh = notQuadSubMesh.front()->GetFather()->GetMeshDS();
401 list< SMESH_subMesh* >::iterator smIt = notQuadSubMesh.begin();
402 while ( smIt != notQuadSubMesh.end() )
404 SMESH_subMesh* faceSm = *smIt;
405 SMESHDS_SubMesh* faceSmDS = faceSm->GetSubMeshDS();
406 int nbQuads = faceSmDS ? faceSmDS->NbElements() : 0;
409 toRemove = helper->IsStructured( faceSm );
411 toRemove = ( quadAlgo->CheckNbEdges( *helper->GetMesh(),
412 faceSm->GetSubShape() ) != NULL );
413 nbRemoved += toRemove;
415 smIt = notQuadSubMesh.erase( smIt );
423 //================================================================================
425 * \brief Return and angle between two EDGEs
426 * \return double - the angle normalized so that
433 //================================================================================
435 // double normAngle(const TopoDS_Edge & E1, const TopoDS_Edge & E2, const TopoDS_Face & F)
437 // return SMESH_MesherHelper::GetAngle( E1, E2, F ) / ( 0.5 * M_PI );
440 //================================================================================
442 * Consider continuous straight EDGES as one side - mark them to unite
444 //================================================================================
446 int countNbSides( const Prism_3D::TPrismTopo & thePrism,
447 vector<int> & nbUnitePerEdge,
448 vector< double > & edgeLength)
450 int nbEdges = thePrism.myNbEdgesInWires.front(); // nb outer edges
451 int nbSides = nbEdges;
454 list< TopoDS_Edge >::const_iterator edgeIt = thePrism.myBottomEdges.begin();
455 std::advance( edgeIt, nbEdges-1 );
456 TopoDS_Edge prevE = *edgeIt;
457 // bool isPrevStraight = SMESH_Algo::IsStraight( prevE );
458 // int iPrev = nbEdges - 1;
460 // int iUnite = -1; // the first of united EDGEs
462 // analyse angles between EDGEs
464 vector< bool > isCorner( nbEdges );
465 edgeIt = thePrism.myBottomEdges.begin();
466 for ( int iE = 0; iE < nbEdges; ++iE, ++edgeIt )
468 const TopoDS_Edge& curE = *edgeIt;
469 edgeLength[ iE ] = SMESH_Algo::EdgeLength( curE );
471 // double normAngle = normAngle( prevE, curE, thePrism.myBottom );
472 // isCorner[ iE ] = false;
473 // if ( normAngle < 2.0 )
475 // if ( normAngle < 0.001 ) // straight or obtuse angle
477 // // unite EDGEs in order not to put a corner of the unit quadrangle at this VERTEX
480 // nbUnitePerEdge[ iUnite ]++;
481 // nbUnitePerEdge[ iE ] = -1;
486 // isCorner[ iE ] = true;
496 // define which of corners to put on a side of the unit quadrangle
498 // edgeIt = thePrism.myBottomEdges.begin();
499 // for ( int iE = 0; iE < nbEdges; ++iE, ++edgeIt )
501 // const TopoDS_Edge& curE = *edgeIt;
502 // edgeLength[ iE ] = SMESH_Algo::EdgeLength( curE );
504 // const bool isCurStraight = SMESH_Algo::IsStraight( curE );
505 // if ( isPrevStraight && isCurStraight && SMESH_Algo::IsContinuous( prevE, curE ))
509 // nbUnitePerEdge[ iUnite ]++;
510 // nbUnitePerEdge[ iE ] = -1;
518 // isPrevStraight = isCurStraight;
525 void pointsToPython(const std::vector<gp_XYZ>& p)
528 for ( size_t i = SMESH_Block::ID_V000; i < p.size(); ++i )
530 cout << "mesh.AddNode( " << p[i].X() << ", "<< p[i].Y() << ", "<< p[i].Z() << ") # " << i <<" " ;
531 SMESH_Block::DumpShapeID( i, cout ) << endl;
537 //=======================================================================
538 //function : StdMeshers_Prism_3D
540 //=======================================================================
542 StdMeshers_Prism_3D::StdMeshers_Prism_3D(int hypId, int studyId, SMESH_Gen* gen)
543 :SMESH_3D_Algo(hypId, studyId, gen)
546 _shapeType = (1 << TopAbs_SOLID); // 1 bit per shape type
547 _onlyUnaryInput = false; // mesh all SOLIDs at once
548 _requireDiscreteBoundary = false; // mesh FACEs and EDGEs by myself
549 _supportSubmeshes = true; // "source" FACE must be meshed by other algo
550 _neededLowerHyps[ 1 ] = true; // suppress warning on hiding a global 1D algo
551 _neededLowerHyps[ 2 ] = true; // suppress warning on hiding a global 2D algo
553 //myProjectTriangles = false;
554 mySetErrorToSM = true; // to pass an error to a sub-mesh of a current solid or not
557 //================================================================================
561 //================================================================================
563 StdMeshers_Prism_3D::~StdMeshers_Prism_3D()
566 //=======================================================================
567 //function : CheckHypothesis
569 //=======================================================================
571 bool StdMeshers_Prism_3D::CheckHypothesis(SMESH_Mesh& aMesh,
572 const TopoDS_Shape& aShape,
573 SMESH_Hypothesis::Hypothesis_Status& aStatus)
575 // Check shape geometry
577 aStatus = SMESH_Hypothesis::HYP_BAD_GEOMETRY;
579 // find not quadrangle faces
580 list< TopoDS_Shape > notQuadFaces;
581 int nbEdge, nbWire, nbFace = 0;
582 TopExp_Explorer exp( aShape, TopAbs_FACE );
583 for ( ; exp.More(); exp.Next() ) {
585 const TopoDS_Shape& face = exp.Current();
586 nbEdge = NSProjUtils::Count( face, TopAbs_EDGE, 0 );
587 nbWire = NSProjUtils::Count( face, TopAbs_WIRE, 0 );
588 if ( nbEdge!= 4 || nbWire!= 1 ) {
589 if ( !notQuadFaces.empty() ) {
590 if ( NSProjUtils::Count( notQuadFaces.back(), TopAbs_EDGE, 0 ) != nbEdge ||
591 NSProjUtils::Count( notQuadFaces.back(), TopAbs_WIRE, 0 ) != nbWire )
592 RETURN_BAD_RESULT("Different not quad faces");
594 notQuadFaces.push_back( face );
597 if ( !notQuadFaces.empty() )
599 if ( notQuadFaces.size() != 2 )
600 RETURN_BAD_RESULT("Bad nb not quad faces: " << notQuadFaces.size());
602 // check total nb faces
603 nbEdge = NSProjUtils::Count( notQuadFaces.back(), TopAbs_EDGE, 0 );
604 if ( nbFace != nbEdge + 2 )
605 RETURN_BAD_RESULT("Bad nb of faces: " << nbFace << " but must be " << nbEdge + 2);
609 aStatus = SMESH_Hypothesis::HYP_OK;
613 //=======================================================================
615 //purpose : Compute mesh on a COMPOUND of SOLIDs
616 //=======================================================================
618 bool StdMeshers_Prism_3D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
620 SMESH_MesherHelper helper( theMesh );
623 int nbSolids = helper.Count( theShape, TopAbs_SOLID, /*skipSame=*/false );
627 TopTools_IndexedDataMapOfShapeListOfShape faceToSolids;
628 TopExp::MapShapesAndAncestors( theShape, TopAbs_FACE, TopAbs_SOLID, faceToSolids );
630 // look for meshed FACEs ("source" FACEs) that must be prism bottoms
631 list< TopoDS_Face > meshedFaces, notQuadMeshedFaces, notQuadFaces;
632 const bool meshHasQuads = ( theMesh.NbQuadrangles() > 0 );
633 //StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this );
634 for ( int iF = 1; iF <= faceToSolids.Extent(); ++iF )
636 const TopoDS_Face& face = TopoDS::Face( faceToSolids.FindKey( iF ));
637 SMESH_subMesh* faceSM = theMesh.GetSubMesh( face );
638 if ( !faceSM->IsEmpty() )
640 if ( !meshHasQuads ||
641 !helper.IsSameElemGeometry( faceSM->GetSubMeshDS(), SMDSGeom_QUADRANGLE ) ||
642 !helper.IsStructured( faceSM )
644 notQuadMeshedFaces.push_front( face );
645 else if ( myHelper->Count( face, TopAbs_EDGE, /*ignoreSame=*/false ) != 4 )
646 meshedFaces.push_front( face );
648 meshedFaces.push_back( face );
650 // not add not quadrilateral FACE as we can't compute it
651 // else if ( !quadAlgo->CheckNbEdges( theMesh, face ))
652 // // not add not quadrilateral FACE as it can be a prism side
653 // // else if ( myHelper->Count( face, TopAbs_EDGE, /*ignoreSame=*/false ) != 4 )
655 // notQuadFaces.push_back( face );
658 // notQuadFaces are of medium priority, put them before ordinary meshed faces
659 meshedFaces.splice( meshedFaces.begin(), notQuadFaces );
660 // notQuadMeshedFaces are of highest priority, put them before notQuadFaces
661 meshedFaces.splice( meshedFaces.begin(), notQuadMeshedFaces );
663 Prism_3D::TPrismTopo prism;
665 bool selectBottom = meshedFaces.empty();
669 TopoDS_Shape solid = TopExp_Explorer( theShape, TopAbs_SOLID ).Current();
670 if ( !meshedFaces.empty() )
671 prism.myBottom = meshedFaces.front();
672 return ( initPrism( prism, solid, selectBottom ) &&
676 // find propagation chains from already computed EDGEs
677 vector< TopoDS_Edge > computedEdges;
678 getPrecomputedEdges( helper, theShape, computedEdges );
679 myPropagChains = new TopTools_IndexedMapOfShape[ computedEdges.size() + 1 ];
680 SMESHUtils::ArrayDeleter< TopTools_IndexedMapOfShape > pcDel( myPropagChains );
681 for ( size_t i = 0, nb = 0; i < computedEdges.size(); ++i )
683 StdMeshers_ProjectionUtils::GetPropagationEdge( &theMesh, TopoDS_Edge(),
684 computedEdges[i], myPropagChains + nb );
685 if ( myPropagChains[ nb ].Extent() < 2 ) // an empty map is a termination sign
686 myPropagChains[ nb ].Clear();
691 TopTools_MapOfShape meshedSolids;
692 list< Prism_3D::TPrismTopo > meshedPrism;
693 list< TopoDS_Face > suspectSourceFaces;
694 TopTools_ListIteratorOfListOfShape solidIt;
696 while ( meshedSolids.Extent() < nbSolids )
698 if ( _computeCanceled )
699 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
701 // compute prisms having avident computed source FACE
702 while ( !meshedFaces.empty() )
704 TopoDS_Face face = meshedFaces.front();
705 meshedFaces.pop_front();
706 TopTools_ListOfShape& solidList = faceToSolids.ChangeFromKey( face );
707 while ( !solidList.IsEmpty() )
709 TopoDS_Shape solid = solidList.First();
710 solidList.RemoveFirst();
711 if ( meshedSolids.Add( solid ))
714 prism.myBottom = face;
715 if ( !initPrism( prism, solid, selectBottom ) ||
719 SMESHDS_SubMesh* smDS = theMesh.GetMeshDS()->MeshElements( prism.myTop );
720 if ( !myHelper->IsSameElemGeometry( smDS, SMDSGeom_QUADRANGLE ))
722 meshedFaces.push_front( prism.myTop );
726 suspectSourceFaces.push_back( prism.myTop );
728 meshedPrism.push_back( prism );
732 if ( meshedSolids.Extent() == nbSolids )
735 // below in the loop we try to find source FACEs somehow
737 // project mesh from source FACEs of computed prisms to
738 // prisms sharing wall FACEs
739 list< Prism_3D::TPrismTopo >::iterator prismIt = meshedPrism.begin();
740 for ( ; prismIt != meshedPrism.end(); ++prismIt )
742 for ( size_t iW = 0; iW < prismIt->myWallQuads.size(); ++iW )
744 Prism_3D::TQuadList::iterator wQuad = prismIt->myWallQuads[iW].begin();
745 for ( ; wQuad != prismIt->myWallQuads[iW].end(); ++ wQuad )
747 const TopoDS_Face& wFace = (*wQuad)->face;
748 TopTools_ListOfShape& solidList = faceToSolids.ChangeFromKey( wFace );
749 solidIt.Initialize( solidList );
750 while ( solidIt.More() )
752 const TopoDS_Shape& solid = solidIt.Value();
753 if ( meshedSolids.Contains( solid )) {
754 solidList.Remove( solidIt );
755 continue; // already computed prism
757 if ( myHelper->IsBlock( solid )) {
759 continue; // too trivial
761 // find a source FACE of the SOLID: it's a FACE sharing a bottom EDGE with wFace
762 const TopoDS_Edge& wEdge = (*wQuad)->side[ QUAD_TOP_SIDE ].grid->Edge(0);
763 PShapeIteratorPtr faceIt = myHelper->GetAncestors( wEdge, *myHelper->GetMesh(),
765 while ( const TopoDS_Shape* f = faceIt->next() )
767 const TopoDS_Face& candidateF = TopoDS::Face( *f );
768 if ( candidateF.IsSame( wFace )) continue;
769 // select a source FACE: prismIt->myBottom or prismIt->myTop
770 TopoDS_Face sourceF = prismIt->myBottom;
771 for ( TopExp_Explorer v( prismIt->myTop, TopAbs_VERTEX ); v.More(); v.Next() )
772 if ( myHelper->IsSubShape( v.Current(), candidateF )) {
773 sourceF = prismIt->myTop;
777 prism.myBottom = candidateF;
778 mySetErrorToSM = false;
779 if ( !myHelper->IsSubShape( candidateF, prismIt->myShape3D ) &&
780 myHelper ->IsSubShape( candidateF, solid ) &&
781 !myHelper->GetMesh()->GetSubMesh( candidateF )->IsMeshComputed() &&
782 initPrism( prism, solid, /*selectBottom=*/false ) &&
783 !myHelper->GetMesh()->GetSubMesh( prism.myTop )->IsMeshComputed() &&
784 !myHelper->GetMesh()->GetSubMesh( prism.myBottom )->IsMeshComputed() &&
785 project2dMesh( sourceF, prism.myBottom ))
787 mySetErrorToSM = true;
788 if ( !compute( prism ))
790 SMESHDS_SubMesh* smDS = theMesh.GetMeshDS()->MeshElements( prism.myTop );
791 if ( !myHelper->IsSameElemGeometry( smDS, SMDSGeom_QUADRANGLE ))
793 meshedFaces.push_front( prism.myTop );
794 meshedFaces.push_front( prism.myBottom );
795 selectBottom = false;
797 meshedPrism.push_back( prism );
798 meshedSolids.Add( solid );
802 mySetErrorToSM = true;
804 if ( meshedSolids.Contains( solid ))
805 solidList.Remove( solidIt );
811 if ( !meshedFaces.empty() )
812 break; // to compute prisms with avident sources
815 if ( meshedFaces.empty() )
817 meshedFaces.splice( meshedFaces.end(), suspectSourceFaces );
821 // find FACEs with local 1D hyps, which has to be computed by now,
822 // or at least any computed FACEs
823 if ( meshedFaces.empty() )
826 for ( int iF = 1; iF <= faceToSolids.Extent(); ++iF )
828 const TopoDS_Face& face = TopoDS::Face( faceToSolids.FindKey( iF ));
829 const TopTools_ListOfShape& solidList = faceToSolids.FindFromKey( face );
830 if ( solidList.IsEmpty() ) continue;
831 SMESH_subMesh* faceSM = theMesh.GetSubMesh( face );
832 if ( !faceSM->IsEmpty() )
834 int nbFaces = faceSM->GetSubMeshDS()->NbElements();
835 if ( prevNbFaces < nbFaces )
837 if ( !meshedFaces.empty() ) meshedFaces.pop_back();
838 meshedFaces.push_back( face ); // lower priority
840 prevNbFaces = nbFaces;
845 bool allSubMeComputed = true;
846 SMESH_subMeshIteratorPtr smIt = faceSM->getDependsOnIterator(false,true);
847 while ( smIt->more() && allSubMeComputed )
848 allSubMeComputed = smIt->next()->IsMeshComputed();
849 if ( allSubMeComputed )
851 faceSM->ComputeStateEngine( SMESH_subMesh::COMPUTE );
852 if ( !faceSM->IsEmpty() ) {
853 meshedFaces.push_front( face ); // higher priority
858 faceSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
866 // TODO. there are other ways to find out the source FACE:
867 // propagation, topological similarity, ect.
869 // simply try to mesh all not meshed SOLIDs
870 if ( meshedFaces.empty() )
872 for ( TopExp_Explorer solid( theShape, TopAbs_SOLID ); solid.More(); solid.Next() )
874 mySetErrorToSM = false;
876 if ( !meshedSolids.Contains( solid.Current() ) &&
877 initPrism( prism, solid.Current() ))
879 mySetErrorToSM = true;
880 if ( !compute( prism ))
882 meshedFaces.push_front( prism.myTop );
883 meshedFaces.push_front( prism.myBottom );
884 meshedPrism.push_back( prism );
885 meshedSolids.Add( solid.Current() );
888 mySetErrorToSM = true;
892 if ( meshedFaces.empty() ) // set same error to 10 not-computed solids
894 SMESH_ComputeErrorPtr err = SMESH_ComputeError::New
895 ( COMPERR_BAD_INPUT_MESH, "No meshed source face found", this );
897 const int maxNbErrors = 10; // limit nb errors not to overload the Compute dialog
898 TopExp_Explorer solid( theShape, TopAbs_SOLID );
899 for ( int i = 0; ( i < maxNbErrors && solid.More() ); ++i, solid.Next() )
900 if ( !meshedSolids.Contains( solid.Current() ))
902 SMESH_subMesh* sm = theMesh.GetSubMesh( solid.Current() );
903 sm->GetComputeError() = err;
908 return error( COMPERR_OK );
911 //================================================================================
913 * \brief Find wall faces by bottom edges
915 //================================================================================
917 bool StdMeshers_Prism_3D::getWallFaces( Prism_3D::TPrismTopo & thePrism,
918 const int totalNbFaces)
920 thePrism.myWallQuads.clear();
922 SMESH_Mesh* mesh = myHelper->GetMesh();
924 StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this, myHelper );
926 TopTools_MapOfShape faceMap;
927 TopTools_IndexedDataMapOfShapeListOfShape edgeToFaces;
928 TopExp::MapShapesAndAncestors( thePrism.myShape3D,
929 TopAbs_EDGE, TopAbs_FACE, edgeToFaces );
931 // ------------------------------
932 // Get the 1st row of wall FACEs
933 // ------------------------------
935 list< TopoDS_Edge >::iterator edge = thePrism.myBottomEdges.begin();
936 std::list< int >::iterator nbE = thePrism.myNbEdgesInWires.begin();
937 std::list< int > nbQuadsPerWire;
940 while ( edge != thePrism.myBottomEdges.end() )
943 if ( BRep_Tool::Curve( *edge, f,l ).IsNull() )
945 edge = thePrism.myBottomEdges.erase( edge );
951 TopTools_ListIteratorOfListOfShape faceIt( edgeToFaces.FindFromKey( *edge ));
952 for ( ; faceIt.More(); faceIt.Next() )
954 const TopoDS_Face& face = TopoDS::Face( faceIt.Value() );
955 if ( !thePrism.myBottom.IsSame( face ))
957 Prism_3D::TQuadList quadList( 1, quadAlgo->CheckNbEdges( *mesh, face ));
958 if ( !quadList.back() )
959 return toSM( error(TCom("Side face #") << shapeID( face )
960 << " not meshable with quadrangles"));
961 bool isCompositeBase = ! setBottomEdge( *edge, quadList.back(), face );
962 if ( isCompositeBase )
964 // it's OK if all EDGEs of the bottom side belongs to the bottom FACE
965 StdMeshers_FaceSidePtr botSide = quadList.back()->side[ QUAD_BOTTOM_SIDE ];
966 for ( int iE = 0; iE < botSide->NbEdges(); ++iE )
967 if ( !myHelper->IsSubShape( botSide->Edge(iE), thePrism.myBottom ))
968 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
970 if ( faceMap.Add( face ))
971 thePrism.myWallQuads.push_back( quadList );
981 int nbQuadPrev = std::accumulate( nbQuadsPerWire.begin(), nbQuadsPerWire.end(), 0 );
982 nbQuadsPerWire.push_back( thePrism.myWallQuads.size() - nbQuadPrev );
986 // -------------------------
987 // Find the rest wall FACEs
988 // -------------------------
990 // Compose a vector of indixes of right neighbour FACE for each wall FACE
991 // that is not so evident in case of several WIREs in the bottom FACE
992 thePrism.myRightQuadIndex.clear();
993 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
995 thePrism.myRightQuadIndex.push_back( i+1 ); // OK for all but the last EDGE of a WIRE
997 list< int >::iterator nbQinW = nbQuadsPerWire.begin();
998 for ( int iLeft = 0; nbQinW != nbQuadsPerWire.end(); ++nbQinW )
1000 thePrism.myRightQuadIndex[ iLeft + *nbQinW - 1 ] = iLeft; // for the last EDGE of a WIRE
1004 while ( totalNbFaces - faceMap.Extent() > 2 )
1006 // find wall FACEs adjacent to each of wallQuads by the right side EDGE
1009 nbKnownFaces = faceMap.Extent();
1010 StdMeshers_FaceSidePtr rightSide, topSide; // sides of the quad
1011 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1013 rightSide = thePrism.myWallQuads[i].back()->side[ QUAD_RIGHT_SIDE ];
1014 for ( int iE = 0; iE < rightSide->NbEdges(); ++iE ) // rightSide can be composite
1016 const TopoDS_Edge & rightE = rightSide->Edge( iE );
1017 TopTools_ListIteratorOfListOfShape face( edgeToFaces.FindFromKey( rightE ));
1018 for ( ; face.More(); face.Next() )
1019 if ( faceMap.Add( face.Value() ))
1021 // a new wall FACE encountered, store it in thePrism.myWallQuads
1022 const int iRight = thePrism.myRightQuadIndex[i];
1023 topSide = thePrism.myWallQuads[ iRight ].back()->side[ QUAD_TOP_SIDE ];
1024 const TopoDS_Edge& newBotE = topSide->Edge(0);
1025 const TopoDS_Shape& newWallF = face.Value();
1026 thePrism.myWallQuads[ iRight ].push_back( quadAlgo->CheckNbEdges( *mesh, newWallF ));
1027 if ( !thePrism.myWallQuads[ iRight ].back() )
1028 return toSM( error(TCom("Side face #") << shapeID( newWallF ) <<
1029 " not meshable with quadrangles"));
1030 if ( ! setBottomEdge( newBotE, thePrism.myWallQuads[ iRight ].back(), newWallF ))
1031 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
1035 } while ( nbKnownFaces != faceMap.Extent() );
1037 // find wall FACEs adjacent to each of thePrism.myWallQuads by the top side EDGE
1038 if ( totalNbFaces - faceMap.Extent() > 2 )
1040 const int nbFoundWalls = faceMap.Extent();
1041 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1043 StdMeshers_FaceSidePtr topSide = thePrism.myWallQuads[i].back()->side[ QUAD_TOP_SIDE ];
1044 const TopoDS_Edge & topE = topSide->Edge( 0 );
1045 if ( topSide->NbEdges() > 1 )
1046 return toSM( error(COMPERR_BAD_SHAPE, TCom("Side face #") <<
1047 shapeID( thePrism.myWallQuads[i].back()->face )
1048 << " has a composite top edge"));
1049 TopTools_ListIteratorOfListOfShape faceIt( edgeToFaces.FindFromKey( topE ));
1050 for ( ; faceIt.More(); faceIt.Next() )
1051 if ( faceMap.Add( faceIt.Value() ))
1053 // a new wall FACE encountered, store it in wallQuads
1054 thePrism.myWallQuads[ i ].push_back( quadAlgo->CheckNbEdges( *mesh, faceIt.Value() ));
1055 if ( !thePrism.myWallQuads[ i ].back() )
1056 return toSM( error(TCom("Side face #") << shapeID( faceIt.Value() ) <<
1057 " not meshable with quadrangles"));
1058 if ( ! setBottomEdge( topE, thePrism.myWallQuads[ i ].back(), faceIt.Value() ))
1059 return toSM( error(TCom("Composite 'horizontal' edges are not supported")));
1060 if ( totalNbFaces - faceMap.Extent() == 2 )
1062 i = thePrism.myWallQuads.size(); // to quit from the outer loop
1067 if ( nbFoundWalls == faceMap.Extent() )
1068 return toSM( error("Failed to find wall faces"));
1071 } // while ( totalNbFaces - faceMap.Extent() > 2 )
1073 // ------------------
1074 // Find the top FACE
1075 // ------------------
1077 if ( thePrism.myTop.IsNull() )
1079 // now only top and bottom FACEs are not in the faceMap
1080 faceMap.Add( thePrism.myBottom );
1081 for ( TopExp_Explorer f( thePrism.myShape3D, TopAbs_FACE ); f.More(); f.Next() )
1082 if ( !faceMap.Contains( f.Current() )) {
1083 thePrism.myTop = TopoDS::Face( f.Current() );
1086 if ( thePrism.myTop.IsNull() )
1087 return toSM( error("Top face not found"));
1090 // Check that the top FACE shares all the top EDGEs
1091 for ( size_t i = 0; i < thePrism.myWallQuads.size(); ++i )
1093 StdMeshers_FaceSidePtr topSide = thePrism.myWallQuads[i].back()->side[ QUAD_TOP_SIDE ];
1094 const TopoDS_Edge & topE = topSide->Edge( 0 );
1095 if ( !myHelper->IsSubShape( topE, thePrism.myTop ))
1096 return toSM( error( TCom("Wrong source face: #") << shapeID( thePrism.myBottom )));
1102 //=======================================================================
1103 //function : compute
1104 //purpose : Compute mesh on a SOLID
1105 //=======================================================================
1107 bool StdMeshers_Prism_3D::compute(const Prism_3D::TPrismTopo& thePrism)
1109 myHelper->IsQuadraticSubMesh( thePrism.myShape3D );
1110 if ( _computeCanceled )
1111 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
1113 // Assure the bottom is meshed
1114 SMESH_subMesh * botSM = myHelper->GetMesh()->GetSubMesh( thePrism.myBottom );
1115 if (( botSM->IsEmpty() ) &&
1116 ( ! botSM->GetAlgo() ||
1117 ! _gen->Compute( *botSM->GetFather(), botSM->GetSubShape(), /*shapeOnly=*/true )))
1118 return error( COMPERR_BAD_INPUT_MESH,
1119 TCom( "No mesher defined to compute the face #")
1120 << shapeID( thePrism.myBottom ));
1122 // Make all side FACEs of thePrism meshed with quads
1123 if ( !computeWalls( thePrism ))
1126 // Analyse mesh and geometry to find all block sub-shapes and submeshes
1127 // (after fixing IPAL52499 myBlock is used as a holder of boundary nodes
1128 // and for 2D projection in hard cases where StdMeshers_Projection_2D fails;
1129 // location of internal nodes is usually computed by StdMeshers_Sweeper)
1130 if ( !myBlock.Init( myHelper, thePrism ))
1131 return toSM( error( myBlock.GetError()));
1133 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
1135 int volumeID = meshDS->ShapeToIndex( thePrism.myShape3D );
1137 // Try to get gp_Trsf to get all nodes from bottom ones
1138 vector<gp_Trsf> trsf;
1139 gp_Trsf bottomToTopTrsf;
1140 // if ( !myBlock.GetLayersTransformation( trsf, thePrism ))
1142 // else if ( !trsf.empty() )
1143 // bottomToTopTrsf = trsf.back();
1145 // To compute coordinates of a node inside a block, it is necessary to know
1146 // 1. normalized parameters of the node by which
1147 // 2. coordinates of node projections on all block sub-shapes are computed
1149 // So we fill projections on vertices at once as they are same for all nodes
1150 myShapeXYZ.resize( myBlock.NbSubShapes() );
1151 for ( int iV = SMESH_Block::ID_FirstV; iV < SMESH_Block::ID_FirstE; ++iV ) {
1152 myBlock.VertexPoint( iV, myShapeXYZ[ iV ]);
1153 SHOWYXZ("V point " <<iV << " ", myShapeXYZ[ iV ]);
1156 // Projections on the top and bottom faces are taken from nodes existing
1157 // on these faces; find correspondence between bottom and top nodes
1159 myBotToColumnMap.clear();
1160 if ( !assocOrProjBottom2Top( bottomToTopTrsf, thePrism ) ) // it also fills myBotToColumnMap
1164 // Create nodes inside the block
1166 // use transformation (issue 0020680, IPAL0052499)
1167 StdMeshers_Sweeper sweeper;
1169 bool allowHighBndError;
1173 // load boundary nodes into sweeper
1175 list< TopoDS_Edge >::const_iterator edge = thePrism.myBottomEdges.begin();
1176 for ( ; edge != thePrism.myBottomEdges.end(); ++edge )
1178 int edgeID = meshDS->ShapeToIndex( *edge );
1179 TParam2ColumnMap* u2col = const_cast<TParam2ColumnMap*>
1180 ( myBlock.GetParam2ColumnMap( edgeID, dummy ));
1181 TParam2ColumnMap::iterator u2colIt = u2col->begin();
1182 for ( ; u2colIt != u2col->end(); ++u2colIt )
1183 sweeper.myBndColumns.push_back( & u2colIt->second );
1185 // load node columns inside the bottom face
1186 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.begin();
1187 for ( ; bot_column != myBotToColumnMap.end(); ++bot_column )
1188 sweeper.myIntColumns.push_back( & bot_column->second );
1190 tol = getSweepTolerance( thePrism );
1191 allowHighBndError = !isSimpleBottom( thePrism );
1194 if ( !myUseBlock && sweeper.ComputeNodes( *myHelper, tol, allowHighBndError ))
1197 else // use block approach
1199 // loop on nodes inside the bottom face
1200 Prism_3D::TNode prevBNode;
1201 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.begin();
1202 for ( ; bot_column != myBotToColumnMap.end(); ++bot_column )
1204 const Prism_3D::TNode& tBotNode = bot_column->first; // bottom TNode
1205 if ( tBotNode.GetPositionType() != SMDS_TOP_FACE )
1206 continue; // node is not inside the FACE
1208 // column nodes; middle part of the column are zero pointers
1209 TNodeColumn& column = bot_column->second;
1211 gp_XYZ botParams, topParams;
1212 if ( !tBotNode.HasParams() )
1214 // compute bottom node parameters
1215 gp_XYZ paramHint(-1,-1,-1);
1216 if ( prevBNode.IsNeighbor( tBotNode ))
1217 paramHint = prevBNode.GetParams();
1218 if ( !myBlock.ComputeParameters( tBotNode.GetCoords(), tBotNode.ChangeParams(),
1219 ID_BOT_FACE, paramHint ))
1220 return toSM( error(TCom("Can't compute normalized parameters for node ")
1221 << tBotNode.myNode->GetID() << " on the face #"
1222 << myBlock.SubMesh( ID_BOT_FACE )->GetId() ));
1223 prevBNode = tBotNode;
1225 botParams = topParams = tBotNode.GetParams();
1226 topParams.SetZ( 1 );
1228 // compute top node parameters
1229 if ( column.size() > 2 ) {
1230 gp_Pnt topCoords = gpXYZ( column.back() );
1231 if ( !myBlock.ComputeParameters( topCoords, topParams, ID_TOP_FACE, topParams ))
1232 return toSM( error(TCom("Can't compute normalized parameters ")
1233 << "for node " << column.back()->GetID()
1234 << " on the face #"<< column.back()->getshapeId() ));
1237 else // top nodes are created by projection using parameters
1239 botParams = topParams = tBotNode.GetParams();
1240 topParams.SetZ( 1 );
1243 myShapeXYZ[ ID_BOT_FACE ] = tBotNode.GetCoords();
1244 myShapeXYZ[ ID_TOP_FACE ] = gpXYZ( column.back() );
1247 TNodeColumn::iterator columnNodes = column.begin();
1248 for ( int z = 0; columnNodes != column.end(); ++columnNodes, ++z)
1250 const SMDS_MeshNode* & node = *columnNodes;
1251 if ( node ) continue; // skip bottom or top node
1253 // params of a node to create
1254 double rz = (double) z / (double) ( column.size() - 1 );
1255 gp_XYZ params = botParams * ( 1 - rz ) + topParams * rz;
1257 // set coords on all faces and nodes
1258 const int nbSideFaces = 4;
1259 int sideFaceIDs[nbSideFaces] = { SMESH_Block::ID_Fx0z,
1260 SMESH_Block::ID_Fx1z,
1261 SMESH_Block::ID_F0yz,
1262 SMESH_Block::ID_F1yz };
1263 for ( int iF = 0; iF < nbSideFaces; ++iF )
1264 if ( !setFaceAndEdgesXYZ( sideFaceIDs[ iF ], params, z ))
1267 // compute coords for a new node
1269 if ( !SMESH_Block::ShellPoint( params, myShapeXYZ, coords ))
1270 return toSM( error("Can't compute coordinates by normalized parameters"));
1272 // if ( !meshDS->MeshElements( volumeID ) ||
1273 // meshDS->MeshElements( volumeID )->NbNodes() == 0 )
1274 // pointsToPython(myShapeXYZ);
1275 SHOWYXZ("TOPFacePoint ",myShapeXYZ[ ID_TOP_FACE]);
1276 SHOWYXZ("BOT Node "<< tBotNode.myNode->GetID(),gpXYZ(tBotNode.myNode));
1277 SHOWYXZ("ShellPoint ",coords);
1280 node = meshDS->AddNode( coords.X(), coords.Y(), coords.Z() );
1281 meshDS->SetNodeInVolume( node, volumeID );
1283 if ( _computeCanceled )
1286 } // loop on bottom nodes
1291 SMESHDS_SubMesh* smDS = myBlock.SubMeshDS( ID_BOT_FACE );
1292 if ( !smDS ) return toSM( error(COMPERR_BAD_INPUT_MESH, "Null submesh"));
1294 // loop on bottom mesh faces
1295 vector< const TNodeColumn* > columns;
1296 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
1297 while ( faceIt->more() )
1299 const SMDS_MeshElement* face = faceIt->next();
1300 if ( !face || face->GetType() != SMDSAbs_Face )
1303 // find node columns for each node
1304 int nbNodes = face->NbCornerNodes();
1305 columns.resize( nbNodes );
1306 for ( int i = 0; i < nbNodes; ++i )
1308 const SMDS_MeshNode* n = face->GetNode( i );
1309 if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
1310 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.find( n );
1311 if ( bot_column == myBotToColumnMap.end() )
1312 return toSM( error(TCom("No nodes found above node ") << n->GetID() ));
1313 columns[ i ] = & bot_column->second;
1316 columns[ i ] = myBlock.GetNodeColumn( n );
1317 if ( !columns[ i ] )
1318 return toSM( error(TCom("No side nodes found above node ") << n->GetID() ));
1322 if ( !AddPrisms( columns, myHelper ))
1323 return toSM( error("Different 'vertical' discretization"));
1325 } // loop on bottom mesh faces
1328 myBotToColumnMap.clear();
1331 // update state of sub-meshes (mostly in order to erase improper errors)
1332 SMESH_subMesh* sm = myHelper->GetMesh()->GetSubMesh( thePrism.myShape3D );
1333 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/false);
1334 while ( smIt->more() )
1337 sm->GetComputeError().reset();
1338 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1344 //=======================================================================
1345 //function : computeWalls
1346 //purpose : Compute 2D mesh on walls FACEs of a prism
1347 //=======================================================================
1349 bool StdMeshers_Prism_3D::computeWalls(const Prism_3D::TPrismTopo& thePrism)
1351 SMESH_Mesh* mesh = myHelper->GetMesh();
1352 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
1353 DBGOUT( endl << "COMPUTE Prism " << meshDS->ShapeToIndex( thePrism.myShape3D ));
1355 TProjction1dAlgo* projector1D = TProjction1dAlgo::instance( this );
1356 StdMeshers_Quadrangle_2D* quadAlgo = TQuadrangleAlgo::instance( this, myHelper );
1358 // SMESH_HypoFilter hyp1dFilter( SMESH_HypoFilter::IsAlgo(),/*not=*/true);
1359 // hyp1dFilter.And( SMESH_HypoFilter::HasDim( 1 ));
1360 // hyp1dFilter.And( SMESH_HypoFilter::IsMoreLocalThan( thePrism.myShape3D, *mesh ));
1362 // Discretize equally 'vertical' EDGEs
1363 // -----------------------------------
1364 // find source FACE sides for projection: either already computed ones or
1365 // the 'most composite' ones
1366 const size_t nbWalls = thePrism.myWallQuads.size();
1367 vector< int > wgt( nbWalls, 0 ); // "weight" of a wall
1368 for ( size_t iW = 0; iW != nbWalls; ++iW )
1370 Prism_3D::TQuadList::const_iterator quad = thePrism.myWallQuads[iW].begin();
1371 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1373 StdMeshers_FaceSidePtr lftSide = (*quad)->side[ QUAD_LEFT_SIDE ];
1374 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1377 const TopoDS_Edge& E = lftSide->Edge(i);
1378 if ( mesh->GetSubMesh( E )->IsMeshComputed() )
1381 wgt[ myHelper->WrapIndex( iW+1, nbWalls)] += 10;
1382 wgt[ myHelper->WrapIndex( iW-1, nbWalls)] += 10;
1384 // else if ( mesh->GetHypothesis( E, hyp1dFilter, true )) // local hypothesis!
1388 // in quadratic mesh, pass ignoreMediumNodes to quad sides
1389 if ( myHelper->GetIsQuadratic() )
1391 quad = thePrism.myWallQuads[iW].begin();
1392 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1393 for ( int i = 0; i < NB_QUAD_SIDES; ++i )
1394 (*quad)->side[ i ].grid->SetIgnoreMediumNodes( true );
1397 multimap< int, int > wgt2quad;
1398 for ( size_t iW = 0; iW != nbWalls; ++iW )
1399 wgt2quad.insert( make_pair( wgt[ iW ], iW ));
1401 // Project 'vertical' EDGEs, from left to right
1402 multimap< int, int >::reverse_iterator w2q = wgt2quad.rbegin();
1403 for ( ; w2q != wgt2quad.rend(); ++w2q )
1405 const int iW = w2q->second;
1406 const Prism_3D::TQuadList& quads = thePrism.myWallQuads[ iW ];
1407 Prism_3D::TQuadList::const_iterator quad = quads.begin();
1408 for ( ; quad != quads.end(); ++quad )
1410 StdMeshers_FaceSidePtr rgtSide = (*quad)->side[ QUAD_RIGHT_SIDE ]; // tgt
1411 StdMeshers_FaceSidePtr lftSide = (*quad)->side[ QUAD_LEFT_SIDE ]; // src
1412 bool swapLeftRight = ( lftSide->NbSegments( /*update=*/true ) == 0 &&
1413 rgtSide->NbSegments( /*update=*/true ) > 0 );
1414 if ( swapLeftRight )
1415 std::swap( lftSide, rgtSide );
1417 // assure that all the source (left) EDGEs are meshed
1418 int nbSrcSegments = 0;
1419 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1421 const TopoDS_Edge& srcE = lftSide->Edge(i);
1422 SMESH_subMesh* srcSM = mesh->GetSubMesh( srcE );
1423 if ( !srcSM->IsMeshComputed() ) {
1424 DBGOUT( "COMPUTE V edge " << srcSM->GetId() );
1425 TopoDS_Edge prpgSrcE = findPropagationSource( srcE );
1426 if ( !prpgSrcE.IsNull() ) {
1427 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1428 projector1D->myHyp.SetSourceEdge( prpgSrcE );
1429 projector1D->Compute( *mesh, srcE );
1430 srcSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1433 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1434 srcSM->ComputeStateEngine ( SMESH_subMesh::COMPUTE );
1436 if ( !srcSM->IsMeshComputed() )
1437 return toSM( error( "Can't compute 1D mesh" ));
1439 nbSrcSegments += srcSM->GetSubMeshDS()->NbElements();
1441 // check target EDGEs
1442 int nbTgtMeshed = 0, nbTgtSegments = 0;
1443 vector< bool > isTgtEdgeComputed( rgtSide->NbEdges() );
1444 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1446 const TopoDS_Edge& tgtE = rgtSide->Edge(i);
1447 SMESH_subMesh* tgtSM = mesh->GetSubMesh( tgtE );
1448 if ( !( isTgtEdgeComputed[ i ] = tgtSM->IsMeshComputed() )) {
1449 tgtSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1450 tgtSM->ComputeStateEngine ( SMESH_subMesh::COMPUTE );
1452 if ( tgtSM->IsMeshComputed() ) {
1454 nbTgtSegments += tgtSM->GetSubMeshDS()->NbElements();
1457 if ( rgtSide->NbEdges() == nbTgtMeshed ) // all tgt EDGEs meshed
1459 if ( nbTgtSegments != nbSrcSegments )
1461 bool badMeshRemoved = false;
1462 // remove just computed segments
1463 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1464 if ( !isTgtEdgeComputed[ i ])
1466 const TopoDS_Edge& tgtE = rgtSide->Edge(i);
1467 SMESH_subMesh* tgtSM = mesh->GetSubMesh( tgtE );
1468 tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
1469 badMeshRemoved = true;
1472 if ( !badMeshRemoved )
1474 for ( int i = 0; i < lftSide->NbEdges(); ++i )
1475 addBadInputElements( meshDS->MeshElements( lftSide->Edge( i )));
1476 for ( int i = 0; i < rgtSide->NbEdges(); ++i )
1477 addBadInputElements( meshDS->MeshElements( rgtSide->Edge( i )));
1478 return toSM( error( TCom("Different nb of segment on logically vertical edges #")
1479 << shapeID( lftSide->Edge(0) ) << " and #"
1480 << shapeID( rgtSide->Edge(0) ) << ": "
1481 << nbSrcSegments << " != " << nbTgtSegments ));
1484 else // if ( nbTgtSegments == nbSrcSegments )
1489 // Compute 'vertical projection'
1490 if ( nbTgtMeshed == 0 )
1492 // compute nodes on target VERTEXes
1493 const UVPtStructVec& srcNodeStr = lftSide->GetUVPtStruct();
1494 if ( srcNodeStr.size() == 0 )
1495 return toSM( error( TCom("Invalid node positions on edge #") <<
1496 shapeID( lftSide->Edge(0) )));
1497 vector< SMDS_MeshNode* > newNodes( srcNodeStr.size() );
1498 for ( int is2ndV = 0; is2ndV < 2; ++is2ndV )
1500 const TopoDS_Edge& E = rgtSide->Edge( is2ndV ? rgtSide->NbEdges()-1 : 0 );
1501 TopoDS_Vertex v = myHelper->IthVertex( is2ndV, E );
1502 mesh->GetSubMesh( v )->ComputeStateEngine( SMESH_subMesh::COMPUTE );
1503 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, meshDS );
1504 newNodes[ is2ndV ? 0 : newNodes.size()-1 ] = (SMDS_MeshNode*) n;
1507 // compute nodes on target EDGEs
1508 DBGOUT( "COMPUTE V edge (proj) " << shapeID( lftSide->Edge(0)));
1509 rgtSide->Reverse(); // direct it same as the lftSide
1510 myHelper->SetElementsOnShape( false ); // myHelper holds the prism shape
1511 TopoDS_Edge tgtEdge;
1512 for ( size_t iN = 1; iN < srcNodeStr.size()-1; ++iN ) // add nodes
1514 gp_Pnt p = rgtSide->Value3d ( srcNodeStr[ iN ].normParam );
1515 double u = rgtSide->Parameter( srcNodeStr[ iN ].normParam, tgtEdge );
1516 newNodes[ iN ] = meshDS->AddNode( p.X(), p.Y(), p.Z() );
1517 meshDS->SetNodeOnEdge( newNodes[ iN ], tgtEdge, u );
1519 for ( size_t iN = 1; iN < srcNodeStr.size(); ++iN ) // add segments
1521 // find an EDGE to set a new segment
1522 std::pair<int, TopAbs_ShapeEnum> id2type =
1523 myHelper->GetMediumPos( newNodes[ iN-1 ], newNodes[ iN ] );
1524 if ( id2type.second != TopAbs_EDGE )
1526 // new nodes are on different EDGEs; put one of them on VERTEX
1527 const int edgeIndex = rgtSide->EdgeIndex( srcNodeStr[ iN-1 ].normParam );
1528 const double vertexParam = rgtSide->LastParameter( edgeIndex );
1529 TopoDS_Vertex vertex = rgtSide->LastVertex( edgeIndex );
1530 const SMDS_MeshNode* vn = SMESH_Algo::VertexNode( vertex, meshDS );
1531 const gp_Pnt p = BRep_Tool::Pnt( vertex );
1532 const int isPrev = ( Abs( srcNodeStr[ iN-1 ].normParam - vertexParam ) <
1533 Abs( srcNodeStr[ iN ].normParam - vertexParam ));
1534 meshDS->UnSetNodeOnShape( newNodes[ iN-isPrev ] );
1535 meshDS->SetNodeOnVertex ( newNodes[ iN-isPrev ], vertex );
1536 meshDS->MoveNode ( newNodes[ iN-isPrev ], p.X(), p.Y(), p.Z() );
1537 id2type.first = newNodes[ iN-(1-isPrev) ]->getshapeId();
1540 SMESH_MeshEditor::TListOfListOfNodes lln( 1, list< const SMDS_MeshNode* >() );
1541 lln.back().push_back ( vn );
1542 lln.back().push_front( newNodes[ iN-isPrev ] ); // to keep
1543 SMESH_MeshEditor( mesh ).MergeNodes( lln );
1546 SMDS_MeshElement* newEdge = myHelper->AddEdge( newNodes[ iN-1 ], newNodes[ iN ] );
1547 meshDS->SetMeshElementOnShape( newEdge, id2type.first );
1549 myHelper->SetElementsOnShape( true );
1550 for ( int i = 0; i < rgtSide->NbEdges(); ++i ) // update state of sub-meshes
1552 const TopoDS_Edge& E = rgtSide->Edge( i );
1553 SMESH_subMesh* tgtSM = mesh->GetSubMesh( E );
1554 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1557 // to continue projection from the just computed side as a source
1558 if ( !swapLeftRight && rgtSide->NbEdges() > 1 && w2q->second == iW )
1560 std::pair<int,int> wgt2quadKeyVal( w2q->first + 1, thePrism.myRightQuadIndex[ iW ]);
1561 wgt2quad.insert( wgt2quadKeyVal ); // it will be skipped by ++w2q
1562 wgt2quad.insert( wgt2quadKeyVal );
1563 w2q = wgt2quad.rbegin();
1568 // HOPE assigned hypotheses are OK, so that equal nb of segments will be generated
1569 //return toSM( error("Partial projection not implemented"));
1571 } // loop on quads of a composite wall side
1572 } // loop on the ordered wall sides
1576 for ( size_t iW = 0; iW != thePrism.myWallQuads.size(); ++iW )
1578 Prism_3D::TQuadList::const_iterator quad = thePrism.myWallQuads[iW].begin();
1579 for ( ; quad != thePrism.myWallQuads[iW].end(); ++quad )
1581 const TopoDS_Face& face = (*quad)->face;
1582 SMESH_subMesh* fSM = mesh->GetSubMesh( face );
1583 if ( ! fSM->IsMeshComputed() )
1585 // Top EDGEs must be projections from the bottom ones
1586 // to compute stuctured quad mesh on wall FACEs
1587 // ---------------------------------------------------
1588 const TopoDS_Edge& botE = (*quad)->side[ QUAD_BOTTOM_SIDE ].grid->Edge(0);
1589 const TopoDS_Edge& topE = (*quad)->side[ QUAD_TOP_SIDE ].grid->Edge(0);
1590 SMESH_subMesh* botSM = mesh->GetSubMesh( botE );
1591 SMESH_subMesh* topSM = mesh->GetSubMesh( topE );
1592 SMESH_subMesh* srcSM = botSM;
1593 SMESH_subMesh* tgtSM = topSM;
1594 srcSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1595 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1596 if ( !srcSM->IsMeshComputed() && tgtSM->IsMeshComputed() )
1597 std::swap( srcSM, tgtSM );
1599 if ( !srcSM->IsMeshComputed() )
1601 DBGOUT( "COMPUTE H edge " << srcSM->GetId());
1602 srcSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE ); // nodes on VERTEXes
1603 srcSM->ComputeStateEngine( SMESH_subMesh::COMPUTE ); // segments on the EDGE
1606 if ( tgtSM->IsMeshComputed() &&
1607 tgtSM->GetSubMeshDS()->NbNodes() != srcSM->GetSubMeshDS()->NbNodes() )
1609 // the top EDGE is computed differently than the bottom one,
1610 // try to clear a wrong mesh
1611 bool isAdjFaceMeshed = false;
1612 PShapeIteratorPtr fIt = myHelper->GetAncestors( tgtSM->GetSubShape(),
1613 *mesh, TopAbs_FACE );
1614 while ( const TopoDS_Shape* f = fIt->next() )
1615 if (( isAdjFaceMeshed = mesh->GetSubMesh( *f )->IsMeshComputed() ))
1617 if ( isAdjFaceMeshed )
1618 return toSM( error( TCom("Different nb of segment on logically horizontal edges #")
1619 << shapeID( botE ) << " and #"
1620 << shapeID( topE ) << ": "
1621 << tgtSM->GetSubMeshDS()->NbElements() << " != "
1622 << srcSM->GetSubMeshDS()->NbElements() ));
1623 tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
1625 if ( !tgtSM->IsMeshComputed() )
1627 // compute nodes on VERTEXes
1628 SMESH_subMeshIteratorPtr smIt = tgtSM->getDependsOnIterator(/*includeSelf=*/false);
1629 while ( smIt->more() )
1630 smIt->next()->ComputeStateEngine( SMESH_subMesh::COMPUTE );
1632 DBGOUT( "COMPUTE H edge (proj) " << tgtSM->GetId());
1633 projector1D->myHyp.SetSourceEdge( TopoDS::Edge( srcSM->GetSubShape() ));
1634 projector1D->InitComputeError();
1635 bool ok = projector1D->Compute( *mesh, tgtSM->GetSubShape() );
1638 SMESH_ComputeErrorPtr err = projector1D->GetComputeError();
1639 if ( err->IsOK() ) err->myName = COMPERR_ALGO_FAILED;
1640 tgtSM->GetComputeError() = err;
1644 tgtSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1647 // Compute quad mesh on wall FACEs
1648 // -------------------------------
1650 // make all EDGES meshed
1651 fSM->ComputeSubMeshStateEngine( SMESH_subMesh::COMPUTE );
1652 if ( !fSM->SubMeshesComputed() )
1653 return toSM( error( COMPERR_BAD_INPUT_MESH,
1654 "Not all edges have valid algorithm and hypothesis"));
1656 quadAlgo->InitComputeError();
1657 DBGOUT( "COMPUTE Quad face " << fSM->GetId());
1658 bool ok = quadAlgo->Compute( *mesh, face );
1659 fSM->GetComputeError() = quadAlgo->GetComputeError();
1662 fSM->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1664 if ( myHelper->GetIsQuadratic() )
1666 // fill myHelper with medium nodes built by quadAlgo
1667 SMDS_ElemIteratorPtr fIt = fSM->GetSubMeshDS()->GetElements();
1668 while ( fIt->more() )
1669 myHelper->AddTLinks( dynamic_cast<const SMDS_MeshFace*>( fIt->next() ));
1677 //=======================================================================
1679 * \brief Returns a source EDGE of propagation to a given EDGE
1681 //=======================================================================
1683 TopoDS_Edge StdMeshers_Prism_3D::findPropagationSource( const TopoDS_Edge& E )
1685 if ( myPropagChains )
1686 for ( size_t i = 0; !myPropagChains[i].IsEmpty(); ++i )
1687 if ( myPropagChains[i].Contains( E ))
1688 return TopoDS::Edge( myPropagChains[i].FindKey( 1 ));
1690 return TopoDS_Edge();
1693 //=======================================================================
1694 //function : Evaluate
1696 //=======================================================================
1698 bool StdMeshers_Prism_3D::Evaluate(SMESH_Mesh& theMesh,
1699 const TopoDS_Shape& theShape,
1700 MapShapeNbElems& aResMap)
1702 if ( theShape.ShapeType() == TopAbs_COMPOUND )
1705 for ( TopoDS_Iterator it( theShape ); it.More(); it.Next() )
1706 ok &= Evaluate( theMesh, it.Value(), aResMap );
1709 SMESH_MesherHelper helper( theMesh );
1711 myHelper->SetSubShape( theShape );
1713 // find face contains only triangles
1714 vector < SMESH_subMesh * >meshFaces;
1715 TopTools_SequenceOfShape aFaces;
1716 int NumBase = 0, i = 0, NbQFs = 0;
1717 for (TopExp_Explorer exp(theShape, TopAbs_FACE); exp.More(); exp.Next()) {
1719 aFaces.Append(exp.Current());
1720 SMESH_subMesh *aSubMesh = theMesh.GetSubMesh(exp.Current());
1721 meshFaces.push_back(aSubMesh);
1722 MapShapeNbElemsItr anIt = aResMap.find(meshFaces[i-1]);
1723 if( anIt==aResMap.end() )
1724 return toSM( error( "Submesh can not be evaluated"));
1726 std::vector<int> aVec = (*anIt).second;
1727 int nbtri = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1728 int nbqua = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1729 if( nbtri==0 && nbqua>0 ) {
1738 std::vector<int> aResVec(SMDSEntity_Last);
1739 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1740 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1741 aResMap.insert(std::make_pair(sm,aResVec));
1742 return toSM( error( "Submesh can not be evaluated" ));
1745 if(NumBase==0) NumBase = 1; // only quads => set 1 faces as base
1747 // find number of 1d elems for base face
1749 TopTools_MapOfShape Edges1;
1750 for (TopExp_Explorer exp(aFaces.Value(NumBase), TopAbs_EDGE); exp.More(); exp.Next()) {
1751 Edges1.Add(exp.Current());
1752 SMESH_subMesh *sm = theMesh.GetSubMesh(exp.Current());
1754 MapShapeNbElemsItr anIt = aResMap.find(sm);
1755 if( anIt == aResMap.end() ) continue;
1756 std::vector<int> aVec = (*anIt).second;
1757 nb1d += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
1760 // find face opposite to base face
1762 for(i=1; i<=6; i++) {
1763 if(i==NumBase) continue;
1764 bool IsOpposite = true;
1765 for(TopExp_Explorer exp(aFaces.Value(i), TopAbs_EDGE); exp.More(); exp.Next()) {
1766 if( Edges1.Contains(exp.Current()) ) {
1776 // find number of 2d elems on side faces
1778 for(i=1; i<=6; i++) {
1779 if( i==OppNum || i==NumBase ) continue;
1780 MapShapeNbElemsItr anIt = aResMap.find( meshFaces[i-1] );
1781 if( anIt == aResMap.end() ) continue;
1782 std::vector<int> aVec = (*anIt).second;
1783 nb2d += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1786 MapShapeNbElemsItr anIt = aResMap.find( meshFaces[NumBase-1] );
1787 std::vector<int> aVec = (*anIt).second;
1788 bool IsQuadratic = (aVec[SMDSEntity_Quad_Triangle]>aVec[SMDSEntity_Triangle]) ||
1789 (aVec[SMDSEntity_Quad_Quadrangle]>aVec[SMDSEntity_Quadrangle]);
1790 int nb2d_face0_3 = Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1791 int nb2d_face0_4 = Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1792 int nb0d_face0 = aVec[SMDSEntity_Node];
1793 int nb1d_face0_int = ( nb2d_face0_3*3 + nb2d_face0_4*4 - nb1d ) / 2;
1795 std::vector<int> aResVec(SMDSEntity_Last);
1796 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aResVec[i] = 0;
1798 aResVec[SMDSEntity_Quad_Penta] = nb2d_face0_3 * ( nb2d/nb1d );
1799 aResVec[SMDSEntity_Quad_Hexa] = nb2d_face0_4 * ( nb2d/nb1d );
1800 aResVec[SMDSEntity_Node] = nb0d_face0 * ( 2*nb2d/nb1d - 1 ) - nb1d_face0_int * nb2d/nb1d;
1803 aResVec[SMDSEntity_Node] = nb0d_face0 * ( nb2d/nb1d - 1 );
1804 aResVec[SMDSEntity_Penta] = nb2d_face0_3 * ( nb2d/nb1d );
1805 aResVec[SMDSEntity_Hexa] = nb2d_face0_4 * ( nb2d/nb1d );
1807 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1808 aResMap.insert(std::make_pair(sm,aResVec));
1813 //================================================================================
1815 * \brief Create prisms
1816 * \param columns - columns of nodes generated from nodes of a mesh face
1817 * \param helper - helper initialized by mesh and shape to add prisms to
1819 //================================================================================
1821 bool StdMeshers_Prism_3D::AddPrisms( vector<const TNodeColumn*> & columns,
1822 SMESH_MesherHelper* helper)
1824 size_t nbNodes = columns.size();
1825 size_t nbZ = columns[0]->size();
1826 if ( nbZ < 2 ) return false;
1827 for ( size_t i = 1; i < nbNodes; ++i )
1828 if ( columns[i]->size() != nbZ )
1831 // find out orientation
1832 bool isForward = true;
1833 SMDS_VolumeTool vTool;
1835 switch ( nbNodes ) {
1837 SMDS_VolumeOfNodes tmpPenta ( (*columns[0])[z-1], // bottom
1840 (*columns[0])[z], // top
1843 vTool.Set( &tmpPenta );
1844 isForward = vTool.IsForward();
1848 SMDS_VolumeOfNodes tmpHex( (*columns[0])[z-1], (*columns[1])[z-1], // bottom
1849 (*columns[2])[z-1], (*columns[3])[z-1],
1850 (*columns[0])[z], (*columns[1])[z], // top
1851 (*columns[2])[z], (*columns[3])[z] );
1852 vTool.Set( &tmpHex );
1853 isForward = vTool.IsForward();
1857 const int di = (nbNodes+1) / 3;
1858 SMDS_VolumeOfNodes tmpVol ( (*columns[0] )[z-1],
1859 (*columns[di] )[z-1],
1860 (*columns[2*di])[z-1],
1863 (*columns[2*di])[z] );
1864 vTool.Set( &tmpVol );
1865 isForward = vTool.IsForward();
1868 // vertical loop on columns
1870 helper->SetElementsOnShape( true );
1872 switch ( nbNodes ) {
1874 case 3: { // ---------- pentahedra
1875 const int i1 = isForward ? 1 : 2;
1876 const int i2 = isForward ? 2 : 1;
1877 for ( z = 1; z < nbZ; ++z )
1878 helper->AddVolume( (*columns[0 ])[z-1], // bottom
1879 (*columns[i1])[z-1],
1880 (*columns[i2])[z-1],
1881 (*columns[0 ])[z], // top
1883 (*columns[i2])[z] );
1886 case 4: { // ---------- hexahedra
1887 const int i1 = isForward ? 1 : 3;
1888 const int i3 = isForward ? 3 : 1;
1889 for ( z = 1; z < nbZ; ++z )
1890 helper->AddVolume( (*columns[0])[z-1], (*columns[i1])[z-1], // bottom
1891 (*columns[2])[z-1], (*columns[i3])[z-1],
1892 (*columns[0])[z], (*columns[i1])[z], // top
1893 (*columns[2])[z], (*columns[i3])[z] );
1896 case 6: { // ---------- octahedra
1897 const int iBase1 = isForward ? -1 : 0;
1898 const int iBase2 = isForward ? 0 :-1;
1899 for ( z = 1; z < nbZ; ++z )
1900 helper->AddVolume( (*columns[0])[z+iBase1], (*columns[1])[z+iBase1], // bottom or top
1901 (*columns[2])[z+iBase1], (*columns[3])[z+iBase1],
1902 (*columns[4])[z+iBase1], (*columns[5])[z+iBase1],
1903 (*columns[0])[z+iBase2], (*columns[1])[z+iBase2], // top or bottom
1904 (*columns[2])[z+iBase2], (*columns[3])[z+iBase2],
1905 (*columns[4])[z+iBase2], (*columns[5])[z+iBase2] );
1908 default: // ---------- polyhedra
1909 vector<int> quantities( 2 + nbNodes, 4 );
1910 quantities[0] = quantities[1] = nbNodes;
1911 columns.resize( nbNodes + 1 );
1912 columns[ nbNodes ] = columns[ 0 ];
1913 const int i1 = isForward ? 1 : 3;
1914 const int i3 = isForward ? 3 : 1;
1915 const int iBase1 = isForward ? -1 : 0;
1916 const int iBase2 = isForward ? 0 :-1;
1917 vector<const SMDS_MeshNode*> nodes( 2*nbNodes + 4*nbNodes);
1918 for ( z = 1; z < nbZ; ++z )
1920 for ( size_t i = 0; i < nbNodes; ++i ) {
1921 nodes[ i ] = (*columns[ i ])[z+iBase1]; // bottom or top
1922 nodes[ 2*nbNodes-i-1 ] = (*columns[ i ])[z+iBase2]; // top or bottom
1924 int di = 2*nbNodes + 4*i;
1925 nodes[ di+0 ] = (*columns[i ])[z ];
1926 nodes[ di+i1] = (*columns[i+1])[z ];
1927 nodes[ di+2 ] = (*columns[i+1])[z-1];
1928 nodes[ di+i3] = (*columns[i ])[z-1];
1930 helper->AddPolyhedralVolume( nodes, quantities );
1933 } // switch ( nbNodes )
1938 //================================================================================
1940 * \brief Find correspondence between bottom and top nodes
1941 * If elements on the bottom and top faces are topologically different,
1942 * and projection is possible and allowed, perform the projection
1943 * \retval bool - is a success or not
1945 //================================================================================
1947 bool StdMeshers_Prism_3D::assocOrProjBottom2Top( const gp_Trsf & bottomToTopTrsf,
1948 const Prism_3D::TPrismTopo& thePrism)
1950 SMESH_subMesh * botSM = myHelper->GetMesh()->GetSubMesh( thePrism.myBottom );
1951 SMESH_subMesh * topSM = myHelper->GetMesh()->GetSubMesh( thePrism.myTop );
1953 SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
1954 SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
1956 if ( !botSMDS || botSMDS->NbElements() == 0 )
1958 _gen->Compute( *myHelper->GetMesh(), botSM->GetSubShape(), /*aShapeOnly=*/true );
1959 botSMDS = botSM->GetSubMeshDS();
1960 if ( !botSMDS || botSMDS->NbElements() == 0 )
1961 return toSM( error(TCom("No elements on face #") << botSM->GetId() ));
1964 bool needProject = !topSM->IsMeshComputed();
1965 if ( !needProject &&
1966 (botSMDS->NbElements() != topSMDS->NbElements() ||
1967 botSMDS->NbNodes() != topSMDS->NbNodes()))
1969 MESSAGE("nb elem bot " << botSMDS->NbElements() <<
1970 " top " << ( topSMDS ? topSMDS->NbElements() : 0 ));
1971 MESSAGE("nb node bot " << botSMDS->NbNodes() <<
1972 " top " << ( topSMDS ? topSMDS->NbNodes() : 0 ));
1973 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
1974 <<" and #"<< topSM->GetId() << " seems different" ));
1977 if ( 0/*needProject && !myProjectTriangles*/ )
1978 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
1979 <<" and #"<< topSM->GetId() << " seems different" ));
1980 ///RETURN_BAD_RESULT("Need to project but not allowed");
1982 NSProjUtils::TNodeNodeMap n2nMap;
1983 const NSProjUtils::TNodeNodeMap* n2nMapPtr = & n2nMap;
1986 if ( !projectBottomToTop( bottomToTopTrsf, thePrism ))
1988 n2nMapPtr = & TProjction2dAlgo::instance( this )->GetNodesMap();
1991 if ( !n2nMapPtr || (int) n2nMapPtr->size() < botSMDS->NbNodes() )
1993 // associate top and bottom faces
1994 NSProjUtils::TShapeShapeMap shape2ShapeMap;
1995 const bool sameTopo =
1996 NSProjUtils::FindSubShapeAssociation( thePrism.myBottom, myHelper->GetMesh(),
1997 thePrism.myTop, myHelper->GetMesh(),
2000 for ( size_t iQ = 0; iQ < thePrism.myWallQuads.size(); ++iQ )
2002 const Prism_3D::TQuadList& quadList = thePrism.myWallQuads[iQ];
2003 StdMeshers_FaceSidePtr botSide = quadList.front()->side[ QUAD_BOTTOM_SIDE ];
2004 StdMeshers_FaceSidePtr topSide = quadList.back ()->side[ QUAD_TOP_SIDE ];
2005 if ( botSide->NbEdges() == topSide->NbEdges() )
2007 for ( int iE = 0; iE < botSide->NbEdges(); ++iE )
2009 NSProjUtils::InsertAssociation( botSide->Edge( iE ),
2010 topSide->Edge( iE ), shape2ShapeMap );
2011 NSProjUtils::InsertAssociation( myHelper->IthVertex( 0, botSide->Edge( iE )),
2012 myHelper->IthVertex( 0, topSide->Edge( iE )),
2018 TopoDS_Vertex vb, vt;
2019 StdMeshers_FaceSidePtr sideB, sideT;
2020 vb = myHelper->IthVertex( 0, botSide->Edge( 0 ));
2021 vt = myHelper->IthVertex( 0, topSide->Edge( 0 ));
2022 sideB = quadList.front()->side[ QUAD_LEFT_SIDE ];
2023 sideT = quadList.back ()->side[ QUAD_LEFT_SIDE ];
2024 if ( vb.IsSame( sideB->FirstVertex() ) &&
2025 vt.IsSame( sideT->LastVertex() ))
2027 NSProjUtils::InsertAssociation( botSide->Edge( 0 ),
2028 topSide->Edge( 0 ), shape2ShapeMap );
2029 NSProjUtils::InsertAssociation( vb, vt, shape2ShapeMap );
2031 vb = myHelper->IthVertex( 1, botSide->Edge( botSide->NbEdges()-1 ));
2032 vt = myHelper->IthVertex( 1, topSide->Edge( topSide->NbEdges()-1 ));
2033 sideB = quadList.front()->side[ QUAD_RIGHT_SIDE ];
2034 sideT = quadList.back ()->side[ QUAD_RIGHT_SIDE ];
2035 if ( vb.IsSame( sideB->FirstVertex() ) &&
2036 vt.IsSame( sideT->LastVertex() ))
2038 NSProjUtils::InsertAssociation( botSide->Edge( botSide->NbEdges()-1 ),
2039 topSide->Edge( topSide->NbEdges()-1 ),
2041 NSProjUtils::InsertAssociation( vb, vt, shape2ShapeMap );
2046 // Find matching nodes of top and bottom faces
2047 n2nMapPtr = & n2nMap;
2048 if ( ! NSProjUtils::FindMatchingNodesOnFaces( thePrism.myBottom, myHelper->GetMesh(),
2049 thePrism.myTop, myHelper->GetMesh(),
2050 shape2ShapeMap, n2nMap ))
2053 return toSM( error(TCom("Mesh on faces #") << botSM->GetId()
2054 <<" and #"<< topSM->GetId() << " seems different" ));
2056 return toSM( error(TCom("Topology of faces #") << botSM->GetId()
2057 <<" and #"<< topSM->GetId() << " seems different" ));
2061 // Fill myBotToColumnMap
2063 int zSize = myBlock.VerticalSize();
2064 TNodeNodeMap::const_iterator bN_tN = n2nMapPtr->begin();
2065 for ( ; bN_tN != n2nMapPtr->end(); ++bN_tN )
2067 const SMDS_MeshNode* botNode = bN_tN->first;
2068 const SMDS_MeshNode* topNode = bN_tN->second;
2069 if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
2070 continue; // wall columns are contained in myBlock
2071 // create node column
2072 Prism_3D::TNode bN( botNode );
2073 TNode2ColumnMap::iterator bN_col =
2074 myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
2075 TNodeColumn & column = bN_col->second;
2076 column.resize( zSize );
2077 column.front() = botNode;
2078 column.back() = topNode;
2083 //================================================================================
2085 * \brief Remove faces from the top face and re-create them by projection from the bottom
2086 * \retval bool - a success or not
2088 //================================================================================
2090 bool StdMeshers_Prism_3D::projectBottomToTop( const gp_Trsf & bottomToTopTrsf,
2091 const Prism_3D::TPrismTopo& thePrism )
2093 if ( project2dMesh( thePrism.myBottom, thePrism.myTop ))
2097 NSProjUtils::TNodeNodeMap& n2nMap =
2098 (NSProjUtils::TNodeNodeMap&) TProjction2dAlgo::instance( this )->GetNodesMap();
2103 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2104 SMESH_subMesh * botSM = myHelper->GetMesh()->GetSubMesh( thePrism.myBottom );
2105 SMESH_subMesh * topSM = myHelper->GetMesh()->GetSubMesh( thePrism.myTop );
2107 SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
2108 SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
2110 if ( topSMDS && topSMDS->NbElements() > 0 )
2112 //topSM->ComputeStateEngine( SMESH_subMesh::CLEAN ); -- avoid propagation of events
2113 for ( SMDS_ElemIteratorPtr eIt = topSMDS->GetElements(); eIt->more(); )
2114 meshDS->RemoveFreeElement( eIt->next(), topSMDS, /*fromGroups=*/false );
2115 for ( SMDS_NodeIteratorPtr nIt = topSMDS->GetNodes(); nIt->more(); )
2116 meshDS->RemoveFreeNode( nIt->next(), topSMDS, /*fromGroups=*/false );
2119 const TopoDS_Face& botFace = thePrism.myBottom; // oriented within
2120 const TopoDS_Face& topFace = thePrism.myTop; // the 3D SHAPE
2121 int topFaceID = meshDS->ShapeToIndex( thePrism.myTop );
2123 SMESH_MesherHelper botHelper( *myHelper->GetMesh() );
2124 botHelper.SetSubShape( botFace );
2125 botHelper.ToFixNodeParameters( true );
2127 SMESH_MesherHelper topHelper( *myHelper->GetMesh() );
2128 topHelper.SetSubShape( topFace );
2129 topHelper.ToFixNodeParameters( true );
2130 double distXYZ[4], fixTol = 10 * topHelper.MaxTolerance( topFace );
2132 // Fill myBotToColumnMap
2134 int zSize = myBlock.VerticalSize();
2135 Prism_3D::TNode prevTNode;
2136 SMDS_NodeIteratorPtr nIt = botSMDS->GetNodes();
2137 while ( nIt->more() )
2139 const SMDS_MeshNode* botNode = nIt->next();
2140 const SMDS_MeshNode* topNode = 0;
2141 if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
2142 continue; // strange
2144 Prism_3D::TNode bN( botNode );
2145 if ( bottomToTopTrsf.Form() == gp_Identity )
2147 // compute bottom node params
2148 gp_XYZ paramHint(-1,-1,-1);
2149 if ( prevTNode.IsNeighbor( bN ))
2151 paramHint = prevTNode.GetParams();
2152 // double tol = 1e-2 * ( prevTNode.GetCoords() - bN.GetCoords() ).Modulus();
2153 // myBlock.SetTolerance( Min( myBlock.GetTolerance(), tol ));
2155 if ( !myBlock.ComputeParameters( bN.GetCoords(), bN.ChangeParams(),
2156 ID_BOT_FACE, paramHint ))
2157 return toSM( error(TCom("Can't compute normalized parameters for node ")
2158 << botNode->GetID() << " on the face #"<< botSM->GetId() ));
2160 // compute top node coords
2161 gp_XYZ topXYZ; gp_XY topUV;
2162 if ( !myBlock.FacePoint( ID_TOP_FACE, bN.GetParams(), topXYZ ) ||
2163 !myBlock.FaceUV ( ID_TOP_FACE, bN.GetParams(), topUV ))
2164 return toSM( error(TCom("Can't compute coordinates "
2165 "by normalized parameters on the face #")<< topSM->GetId() ));
2166 topNode = meshDS->AddNode( topXYZ.X(),topXYZ.Y(),topXYZ.Z() );
2167 meshDS->SetNodeOnFace( topNode, topFaceID, topUV.X(), topUV.Y() );
2169 else // use bottomToTopTrsf
2171 gp_XYZ coords = bN.GetCoords();
2172 bottomToTopTrsf.Transforms( coords );
2173 topNode = meshDS->AddNode( coords.X(), coords.Y(), coords.Z() );
2174 gp_XY topUV = botHelper.GetNodeUV( botFace, botNode, 0, &checkUV );
2175 meshDS->SetNodeOnFace( topNode, topFaceID, topUV.X(), topUV.Y() );
2177 if ( topHelper.CheckNodeUV( topFace, topNode, topUV, fixTol, /*force=*/false, distXYZ ) &&
2178 distXYZ[0] > fixTol && distXYZ[0] < fixTol * 1e+3 )
2179 meshDS->MoveNode( topNode, distXYZ[1], distXYZ[2], distXYZ[3] ); // transform can be inaccurate
2181 // create node column
2182 TNode2ColumnMap::iterator bN_col =
2183 myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
2184 TNodeColumn & column = bN_col->second;
2185 column.resize( zSize );
2186 column.front() = botNode;
2187 column.back() = topNode;
2189 n2nMap.insert( n2nMap.end(), make_pair( botNode, topNode ));
2191 if ( _computeCanceled )
2192 return toSM( error( SMESH_ComputeError::New(COMPERR_CANCELED)));
2197 const bool oldSetElemsOnShape = myHelper->SetElementsOnShape( false );
2199 // care of orientation;
2200 // if the bottom faces is orienetd OK then top faces must be reversed
2201 bool reverseTop = true;
2202 if ( myHelper->NbAncestors( botFace, *myBlock.Mesh(), TopAbs_SOLID ) > 1 )
2203 reverseTop = ! myHelper->IsReversedSubMesh( botFace );
2204 int iFrw, iRev, *iPtr = &( reverseTop ? iRev : iFrw );
2206 // loop on bottom mesh faces
2207 SMDS_ElemIteratorPtr faceIt = botSMDS->GetElements();
2208 vector< const SMDS_MeshNode* > nodes;
2209 while ( faceIt->more() )
2211 const SMDS_MeshElement* face = faceIt->next();
2212 if ( !face || face->GetType() != SMDSAbs_Face )
2215 // find top node in columns for each bottom node
2216 int nbNodes = face->NbCornerNodes();
2217 nodes.resize( nbNodes );
2218 for ( iFrw = 0, iRev = nbNodes-1; iFrw < nbNodes; ++iFrw, --iRev )
2220 const SMDS_MeshNode* n = face->GetNode( *iPtr );
2221 if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
2222 TNode2ColumnMap::iterator bot_column = myBotToColumnMap.find( n );
2223 if ( bot_column == myBotToColumnMap.end() )
2224 return toSM( error(TCom("No nodes found above node ") << n->GetID() ));
2225 nodes[ iFrw ] = bot_column->second.back();
2228 const TNodeColumn* column = myBlock.GetNodeColumn( n );
2230 return toSM( error(TCom("No side nodes found above node ") << n->GetID() ));
2231 nodes[ iFrw ] = column->back();
2234 SMDS_MeshElement* newFace = 0;
2235 switch ( nbNodes ) {
2238 newFace = myHelper->AddFace(nodes[0], nodes[1], nodes[2]);
2242 newFace = myHelper->AddFace( nodes[0], nodes[1], nodes[2], nodes[3] );
2246 newFace = meshDS->AddPolygonalFace( nodes );
2249 meshDS->SetMeshElementOnShape( newFace, topFaceID );
2252 myHelper->SetElementsOnShape( oldSetElemsOnShape );
2254 // Check the projected mesh
2256 if ( thePrism.myNbEdgesInWires.size() > 1 && // there are holes
2257 topHelper.IsDistorted2D( topSM, /*checkUV=*/false ))
2259 SMESH_MeshEditor editor( topHelper.GetMesh() );
2261 // smooth in 2D or 3D?
2262 TopLoc_Location loc;
2263 Handle(Geom_Surface) surface = BRep_Tool::Surface( topFace, loc );
2264 bool isPlanar = GeomLib_IsPlanarSurface( surface ).IsPlanar();
2266 bool isFixed = false;
2267 set<const SMDS_MeshNode*> fixedNodes;
2268 for ( int iAttemp = 0; !isFixed && iAttemp < 10; ++iAttemp )
2270 TIDSortedElemSet faces;
2271 for ( faceIt = topSMDS->GetElements(); faceIt->more(); )
2272 faces.insert( faces.end(), faceIt->next() );
2274 SMESH_MeshEditor::SmoothMethod algo =
2275 iAttemp ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
2278 editor.Smooth( faces, fixedNodes, algo, /*nbIterations=*/ 10,
2279 /*theTgtAspectRatio=*/1.0, /*the2D=*/!isPlanar);
2281 isFixed = !topHelper.IsDistorted2D( topSM, /*checkUV=*/true );
2284 return toSM( error( TCom("Projection from face #") << botSM->GetId()
2285 << " to face #" << topSM->GetId()
2286 << " failed: inverted elements created"));
2292 //=======================================================================
2293 //function : getSweepTolerance
2294 //purpose : Compute tolerance to pass to StdMeshers_Sweeper
2295 //=======================================================================
2297 double StdMeshers_Prism_3D::getSweepTolerance( const Prism_3D::TPrismTopo& thePrism )
2299 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2300 SMESHDS_SubMesh * sm[2] = { meshDS->MeshElements( thePrism.myBottom ),
2301 meshDS->MeshElements( thePrism.myTop ) };
2302 double minDist = 1e100;
2304 vector< SMESH_TNodeXYZ > nodes;
2305 for ( int iSM = 0; iSM < 2; ++iSM )
2307 if ( !sm[ iSM ]) continue;
2309 SMDS_ElemIteratorPtr fIt = sm[ iSM ]->GetElements();
2310 while ( fIt->more() )
2312 const SMDS_MeshElement* face = fIt->next();
2313 const int nbNodes = face->NbCornerNodes();
2314 SMDS_ElemIteratorPtr nIt = face->nodesIterator();
2316 nodes.resize( nbNodes + 1 );
2317 for ( int iN = 0; iN < nbNodes; ++iN )
2318 nodes[ iN ] = nIt->next();
2319 nodes.back() = nodes[0];
2323 for ( int iN = 0; iN < nbNodes; ++iN )
2325 if ( nodes[ iN ]._node->GetPosition()->GetDim() < 2 &&
2326 nodes[ iN+1 ]._node->GetPosition()->GetDim() < 2 )
2328 // it's a boundary link; measure distance of other
2329 // nodes to this link
2330 gp_XYZ linkDir = nodes[ iN ] - nodes[ iN+1 ];
2331 double linkLen = linkDir.Modulus();
2332 bool isDegen = ( linkLen < numeric_limits<double>::min() );
2333 if ( !isDegen ) linkDir /= linkLen;
2334 for ( int iN2 = 0; iN2 < nbNodes; ++iN2 ) // loop on other nodes
2336 if ( nodes[ iN2 ] == nodes[ iN ] ||
2337 nodes[ iN2 ] == nodes[ iN+1 ]) continue;
2340 dist2 = ( nodes[ iN ] - nodes[ iN2 ]).SquareModulus();
2344 dist2 = linkDir.CrossSquareMagnitude( nodes[ iN ] - nodes[ iN2 ]);
2346 if ( dist2 > numeric_limits<double>::min() )
2347 minDist = Min ( minDist, dist2 );
2350 // measure length link
2351 else if ( nodes[ iN ]._node < nodes[ iN+1 ]._node ) // not to measure same link twice
2353 dist2 = ( nodes[ iN ] - nodes[ iN+1 ]).SquareModulus();
2354 if ( dist2 > numeric_limits<double>::min() )
2355 minDist = Min ( minDist, dist2 );
2360 return 0.1 * Sqrt ( minDist );
2363 //=======================================================================
2364 //function : isSimpleQuad
2365 //purpose : check if the bottom FACE is meshable with nice qudrangles,
2366 // if so the block aproach can work rather fast.
2367 // This is a temporary mean caused by problems in StdMeshers_Sweeper
2368 //=======================================================================
2370 bool StdMeshers_Prism_3D::isSimpleBottom( const Prism_3D::TPrismTopo& thePrism )
2372 // analyse angles between edges
2373 double nbConcaveAng = 0, nbConvexAng = 0;
2374 TopoDS_Face reverseBottom = TopoDS::Face( thePrism.myBottom.Reversed() ); // see initPrism()
2375 TopoDS_Vertex commonV;
2376 const list< TopoDS_Edge >& botEdges = thePrism.myBottomEdges;
2377 list< TopoDS_Edge >::const_iterator edge = botEdges.begin();
2378 while ( edge != botEdges.end() )
2380 if ( SMESH_Algo::isDegenerated( *edge ))
2382 TopoDS_Edge e1 = *edge++;
2383 TopoDS_Edge e2 = ( edge == botEdges.end() ? botEdges.front() : *edge );
2384 if ( ! TopExp::CommonVertex( e1, e2, commonV ))
2386 e2 = botEdges.front();
2387 if ( ! TopExp::CommonVertex( e1, e2, commonV ))
2390 double angle = myHelper->GetAngle( e1, e2, reverseBottom, commonV );
2391 if ( angle < -5 * M_PI/180 )
2392 if ( ++nbConcaveAng > 1 )
2394 if ( angle > 85 * M_PI/180 )
2395 if ( ++nbConvexAng > 4 )
2401 //=======================================================================
2402 //function : project2dMesh
2403 //purpose : Project mesh faces from a source FACE of one prism (theSrcFace)
2404 // to a source FACE of another prism (theTgtFace)
2405 //=======================================================================
2407 bool StdMeshers_Prism_3D::project2dMesh(const TopoDS_Face& theSrcFace,
2408 const TopoDS_Face& theTgtFace)
2410 TProjction2dAlgo* projector2D = TProjction2dAlgo::instance( this );
2411 projector2D->myHyp.SetSourceFace( theSrcFace );
2412 bool ok = projector2D->Compute( *myHelper->GetMesh(), theTgtFace );
2414 SMESH_subMesh* tgtSM = myHelper->GetMesh()->GetSubMesh( theTgtFace );
2415 if ( !ok && tgtSM->GetSubMeshDS() ) {
2416 //tgtSM->ComputeStateEngine( SMESH_subMesh::CLEAN ); -- avoid propagation of events
2417 SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
2418 SMESHDS_SubMesh* tgtSMDS = tgtSM->GetSubMeshDS();
2419 for ( SMDS_ElemIteratorPtr eIt = tgtSMDS->GetElements(); eIt->more(); )
2420 meshDS->RemoveFreeElement( eIt->next(), tgtSMDS, /*fromGroups=*/false );
2421 for ( SMDS_NodeIteratorPtr nIt = tgtSMDS->GetNodes(); nIt->more(); )
2422 meshDS->RemoveFreeNode( nIt->next(), tgtSMDS, /*fromGroups=*/false );
2424 tgtSM->ComputeStateEngine ( SMESH_subMesh::CHECK_COMPUTE_STATE );
2425 tgtSM->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
2430 //================================================================================
2432 * \brief Set projection coordinates of a node to a face and it's sub-shapes
2433 * \param faceID - the face given by in-block ID
2434 * \param params - node normalized parameters
2435 * \retval bool - is a success
2437 //================================================================================
2439 bool StdMeshers_Prism_3D::setFaceAndEdgesXYZ( const int faceID, const gp_XYZ& params, int z )
2441 // find base and top edges of the face
2442 enum { BASE = 0, TOP, LEFT, RIGHT };
2443 vector< int > edgeVec; // 0-base, 1-top
2444 SMESH_Block::GetFaceEdgesIDs( faceID, edgeVec );
2446 myBlock.EdgePoint( edgeVec[ BASE ], params, myShapeXYZ[ edgeVec[ BASE ]]);
2447 myBlock.EdgePoint( edgeVec[ TOP ], params, myShapeXYZ[ edgeVec[ TOP ]]);
2449 SHOWYXZ("\nparams ", params);
2450 SHOWYXZ("TOP is " <<edgeVec[ TOP ], myShapeXYZ[ edgeVec[ TOP]]);
2451 SHOWYXZ("BASE is "<<edgeVec[ BASE], myShapeXYZ[ edgeVec[ BASE]]);
2453 if ( faceID == SMESH_Block::ID_Fx0z || faceID == SMESH_Block::ID_Fx1z )
2455 myBlock.EdgePoint( edgeVec[ LEFT ], params, myShapeXYZ[ edgeVec[ LEFT ]]);
2456 myBlock.EdgePoint( edgeVec[ RIGHT ], params, myShapeXYZ[ edgeVec[ RIGHT ]]);
2458 SHOWYXZ("VER "<<edgeVec[ LEFT], myShapeXYZ[ edgeVec[ LEFT]]);
2459 SHOWYXZ("VER "<<edgeVec[ RIGHT], myShapeXYZ[ edgeVec[ RIGHT]]);
2461 myBlock.FacePoint( faceID, params, myShapeXYZ[ faceID ]);
2462 SHOWYXZ("FacePoint "<<faceID, myShapeXYZ[ faceID]);
2467 //=======================================================================
2469 //purpose : If (!isOK), sets the error to a sub-mesh of a current SOLID
2470 //=======================================================================
2472 bool StdMeshers_Prism_3D::toSM( bool isOK )
2474 if ( mySetErrorToSM &&
2477 !myHelper->GetSubShape().IsNull() &&
2478 myHelper->GetSubShape().ShapeType() == TopAbs_SOLID)
2480 SMESH_subMesh* sm = myHelper->GetMesh()->GetSubMesh( myHelper->GetSubShape() );
2481 sm->GetComputeError() = this->GetComputeError();
2482 // clear error in order not to return it twice
2483 _error = COMPERR_OK;
2489 //=======================================================================
2490 //function : shapeID
2491 //purpose : Return index of a shape
2492 //=======================================================================
2494 int StdMeshers_Prism_3D::shapeID( const TopoDS_Shape& S )
2496 if ( S.IsNull() ) return 0;
2497 if ( !myHelper ) return -3;
2498 return myHelper->GetMeshDS()->ShapeToIndex( S );
2501 namespace // utils used by StdMeshers_Prism_3D::IsApplicable()
2503 struct EdgeWithNeighbors
2507 EdgeWithNeighbors(const TopoDS_Edge& E, int iE, int nbE, int shift = 0 ):
2509 _iL( SMESH_MesherHelper::WrapIndex( iE-1, nbE ) + shift ),
2510 _iR( SMESH_MesherHelper::WrapIndex( iE+1, nbE ) + shift )
2513 EdgeWithNeighbors() {}
2518 TopTools_IndexedMapOfShape *_faces; // pointer because its copy constructor is private
2519 TopoDS_Edge _topEdge;
2520 vector< EdgeWithNeighbors >*_edges;
2522 vector< bool > _isCheckedEdge;
2523 int _nbCheckedEdges; // nb of EDGEs whose location is defined
2524 PrismSide *_leftSide;
2525 PrismSide *_rightSide;
2526 void SetExcluded() { _leftSide = _rightSide = NULL; }
2527 bool IsExcluded() const { return !_leftSide; }
2528 const TopoDS_Edge& Edge( int i ) const
2530 return (*_edges)[ i ]._edge;
2532 int FindEdge( const TopoDS_Edge& E ) const
2534 for ( size_t i = 0; i < _edges->size(); ++i )
2535 if ( E.IsSame( Edge( i ))) return i;
2538 bool IsSideFace( const TopoDS_Shape& face ) const
2540 if ( _faces->Contains( face )) // avoid returning true for a prism top FACE
2541 return ( !_face.IsNull() || !( face.IsSame( _faces->FindKey( _faces->Extent() ))));
2545 //--------------------------------------------------------------------------------
2547 * \brief Return ordered edges of a face
2549 bool getEdges( const TopoDS_Face& face,
2550 vector< EdgeWithNeighbors > & edges,
2551 const bool noHolesAllowed)
2553 list< TopoDS_Edge > ee;
2554 list< int > nbEdgesInWires;
2555 int nbW = SMESH_Block::GetOrderedEdges( face, ee, nbEdgesInWires );
2556 if ( nbW > 1 && noHolesAllowed )
2560 list< TopoDS_Edge >::iterator e = ee.begin();
2561 list< int >::iterator nbE = nbEdgesInWires.begin();
2562 for ( ; nbE != nbEdgesInWires.end(); ++nbE )
2563 for ( iE = 0; iE < *nbE; ++e, ++iE )
2564 if ( SMESH_Algo::isDegenerated( *e ))
2566 e = --ee.erase( e );
2572 e->Orientation( TopAbs_FORWARD ); // for operator==() to work
2577 for ( nbE = nbEdgesInWires.begin(); nbE != nbEdgesInWires.end(); ++nbE )
2579 for ( iE = 0; iE < *nbE; ++e, ++iE )
2580 edges.push_back( EdgeWithNeighbors( *e, iE, *nbE, nbTot ));
2583 return edges.size();
2585 //--------------------------------------------------------------------------------
2587 * \brief Return another faces sharing an edge
2589 const TopoDS_Face & getAnotherFace( const TopoDS_Face& face,
2590 const TopoDS_Edge& edge,
2591 TopTools_IndexedDataMapOfShapeListOfShape& facesOfEdge)
2593 TopTools_ListIteratorOfListOfShape faceIt( facesOfEdge.FindFromKey( edge ));
2594 for ( ; faceIt.More(); faceIt.Next() )
2595 if ( !face.IsSame( faceIt.Value() ))
2596 return TopoDS::Face( faceIt.Value() );
2600 //--------------------------------------------------------------------------------
2602 * \brief Return number of faces sharing given edges
2604 int nbAdjacentFaces( const std::vector< EdgeWithNeighbors >& edges,
2605 const TopTools_IndexedDataMapOfShapeListOfShape& facesOfEdge )
2607 TopTools_MapOfShape adjFaces;
2609 for ( size_t i = 0; i < edges.size(); ++i )
2611 TopTools_ListIteratorOfListOfShape faceIt( facesOfEdge.FindFromKey( edges[i]._edge ));
2612 for ( ; faceIt.More(); faceIt.Next() )
2613 adjFaces.Add( faceIt.Value() );
2615 return adjFaces.Extent();
2619 //================================================================================
2621 * \brief Return true if the algorithm can mesh this shape
2622 * \param [in] aShape - shape to check
2623 * \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
2624 * else, returns OK if at least one shape is OK
2626 //================================================================================
2628 bool StdMeshers_Prism_3D::IsApplicable(const TopoDS_Shape & shape, bool toCheckAll)
2630 TopExp_Explorer sExp( shape, TopAbs_SOLID );
2634 for ( ; sExp.More(); sExp.Next() )
2638 TopExp_Explorer shExp( sExp.Current(), TopAbs_SHELL );
2639 if ( shExp.More() ) {
2640 shell = shExp.Current();
2645 if ( shell.IsNull() ) {
2646 if ( toCheckAll ) return false;
2650 TopTools_IndexedMapOfShape allFaces;
2651 TopExp::MapShapes( shell, TopAbs_FACE, allFaces );
2652 if ( allFaces.Extent() < 3 ) {
2653 if ( toCheckAll ) return false;
2657 if ( allFaces.Extent() == 6 )
2659 TopTools_IndexedMapOfOrientedShape map;
2660 bool isBox = SMESH_Block::FindBlockShapes( TopoDS::Shell( shell ),
2661 TopoDS_Vertex(), TopoDS_Vertex(), map );
2663 if ( !toCheckAll ) return true;
2668 TopTools_IndexedMapOfShape allShapes;
2669 TopExp::MapShapes( shape, allShapes );
2672 TopTools_IndexedDataMapOfShapeListOfShape facesOfEdge;
2673 TopTools_ListIteratorOfListOfShape faceIt;
2674 TopExp::MapShapesAndAncestors( sExp.Current(), TopAbs_EDGE, TopAbs_FACE , facesOfEdge );
2675 if ( facesOfEdge.IsEmpty() ) {
2676 if ( toCheckAll ) return false;
2680 typedef vector< EdgeWithNeighbors > TEdgeWithNeighborsVec;
2681 vector< TEdgeWithNeighborsVec > faceEdgesVec( allFaces.Extent() + 1 );
2682 const size_t nbEdgesMax = facesOfEdge.Extent() * 2; // there can be seam EDGES
2683 TopTools_IndexedMapOfShape* facesOfSide = new TopTools_IndexedMapOfShape[ nbEdgesMax ];
2684 SMESHUtils::ArrayDeleter<TopTools_IndexedMapOfShape> delFacesOfSide( facesOfSide );
2686 // try to use each face as a bottom one
2687 bool prismDetected = false;
2688 vector< PrismSide > sides;
2689 for ( int iF = 1; iF < allFaces.Extent() && !prismDetected; ++iF )
2691 const TopoDS_Face& botF = TopoDS::Face( allFaces( iF ));
2693 TEdgeWithNeighborsVec& botEdges = faceEdgesVec[ iF ];
2694 if ( botEdges.empty() )
2695 if ( !getEdges( botF, botEdges, /*noHoles=*/false ))
2697 if ( allFaces.Extent()-1 <= (int) botEdges.size() )
2698 continue; // all faces are adjacent to botF - no top FACE
2700 // init data of side FACEs
2702 sides.resize( botEdges.size() );
2703 for ( size_t iS = 0; iS < botEdges.size(); ++iS )
2705 sides[ iS ]._topEdge = botEdges[ iS ]._edge;
2706 sides[ iS ]._face = botF;
2707 sides[ iS ]._leftSide = & sides[ botEdges[ iS ]._iR ];
2708 sides[ iS ]._rightSide = & sides[ botEdges[ iS ]._iL ];
2709 sides[ iS ]._faces = & facesOfSide[ iS ];
2710 sides[ iS ]._faces->Clear();
2713 bool isOK = true; // ok for a current botF
2714 bool isAdvanced = true; // is new data found in a current loop
2715 int nbFoundSideFaces = 0;
2716 for ( int iLoop = 0; isOK && isAdvanced; ++iLoop )
2719 for ( size_t iS = 0; iS < sides.size() && isOK; ++iS )
2721 PrismSide& side = sides[ iS ];
2722 if ( side._face.IsNull() )
2723 continue; // probably the prism top face is the last of side._faces
2725 if ( side._topEdge.IsNull() )
2727 // find vertical EDGEs --- EGDEs shared with neighbor side FACEs
2728 for ( int is2nd = 0; is2nd < 2 && isOK; ++is2nd ) // 2 adjacent neighbors
2730 int di = is2nd ? 1 : -1;
2731 const PrismSide* adjSide = is2nd ? side._rightSide : side._leftSide;
2732 for ( size_t i = 1; i < side._edges->size(); ++i )
2734 int iE = SMESH_MesherHelper::WrapIndex( i*di + side._iBotEdge, side._edges->size());
2735 if ( side._isCheckedEdge[ iE ] ) continue;
2736 const TopoDS_Edge& vertE = side.Edge( iE );
2737 const TopoDS_Shape& neighborF = getAnotherFace( side._face, vertE, facesOfEdge );
2738 bool isEdgeShared = adjSide->IsSideFace( neighborF );
2739 if ( isEdgeShared ) // vertE is shared with adjSide
2742 side._isCheckedEdge[ iE ] = true;
2743 side._nbCheckedEdges++;
2744 int nbNotCheckedE = side._edges->size() - side._nbCheckedEdges;
2745 if ( nbNotCheckedE == 1 )
2750 if ( i == 1 && iLoop == 0 ) isOK = false;
2756 int nbNotCheckedE = side._edges->size() - side._nbCheckedEdges;
2757 if ( nbNotCheckedE == 1 )
2759 vector<bool>::iterator ii = std::find( side._isCheckedEdge.begin(),
2760 side._isCheckedEdge.end(), false );
2761 if ( ii != side._isCheckedEdge.end() )
2763 size_t iE = std::distance( side._isCheckedEdge.begin(), ii );
2764 side._topEdge = side.Edge( iE );
2767 isOK = ( nbNotCheckedE >= 1 );
2769 else //if ( !side._topEdge.IsNull() )
2771 // get a next face of a side
2772 const TopoDS_Shape& f = getAnotherFace( side._face, side._topEdge, facesOfEdge );
2773 side._faces->Add( f );
2775 if ( f.IsSame( side._face ) || // _topEdge is a seam
2776 SMESH_MesherHelper::Count( f, TopAbs_WIRE, false ) != 1 )
2780 else if ( side._leftSide != & side ) // not closed side face
2782 if ( side._leftSide->_faces->Contains( f ))
2784 stop = true; // probably f is the prism top face
2785 side._leftSide->_face.Nullify();
2786 side._leftSide->_topEdge.Nullify();
2788 if ( side._rightSide->_faces->Contains( f ))
2790 stop = true; // probably f is the prism top face
2791 side._rightSide->_face.Nullify();
2792 side._rightSide->_topEdge.Nullify();
2797 side._face.Nullify();
2798 side._topEdge.Nullify();
2801 side._face = TopoDS::Face( f );
2802 int faceID = allFaces.FindIndex( side._face );
2803 side._edges = & faceEdgesVec[ faceID ];
2804 if ( side._edges->empty() )
2805 if ( !getEdges( side._face, * side._edges, /*noHoles=*/true ))
2807 const int nbE = side._edges->size();
2812 side._iBotEdge = side.FindEdge( side._topEdge );
2813 side._isCheckedEdge.clear();
2814 side._isCheckedEdge.resize( nbE, false );
2815 side._isCheckedEdge[ side._iBotEdge ] = true;
2816 side._nbCheckedEdges = 1; // bottom EDGE is known
2818 side._topEdge.Nullify();
2819 isOK = ( !side._edges->empty() || side._faces->Extent() > 1 );
2821 } //if ( !side._topEdge.IsNull() )
2823 } // loop on prism sides
2825 if ( nbFoundSideFaces > allFaces.Extent() )
2829 if ( iLoop > allFaces.Extent() * 10 )
2833 cerr << "BUG: infinite loop in StdMeshers_Prism_3D::IsApplicable()" << endl;
2836 } // while isAdvanced
2838 if ( isOK && sides[0]._faces->Extent() > 1 )
2840 const int nbFaces = sides[0]._faces->Extent();
2841 if ( botEdges.size() == 1 ) // cylinder
2843 prismDetected = ( nbFaces == allFaces.Extent()-1 );
2847 const TopoDS_Shape& topFace = sides[0]._faces->FindKey( nbFaces );
2849 for ( iS = 1; iS < sides.size(); ++iS )
2850 if ( ! sides[ iS ]._faces->Contains( topFace ))
2852 prismDetected = ( iS == sides.size() );
2855 } // loop on allFaces
2857 if ( !prismDetected && toCheckAll ) return false;
2858 if ( prismDetected && !toCheckAll ) return true;
2867 //================================================================================
2869 * \brief Return true if this node and other one belong to one face
2871 //================================================================================
2873 bool Prism_3D::TNode::IsNeighbor( const Prism_3D::TNode& other ) const
2875 if ( !other.myNode || !myNode ) return false;
2877 SMDS_ElemIteratorPtr fIt = other.myNode->GetInverseElementIterator(SMDSAbs_Face);
2878 while ( fIt->more() )
2879 if ( fIt->next()->GetNodeIndex( myNode ) >= 0 )
2884 //================================================================================
2886 * \brief Prism initialization
2888 //================================================================================
2890 void TPrismTopo::Clear()
2892 myShape3D.Nullify();
2895 myWallQuads.clear();
2896 myBottomEdges.clear();
2897 myNbEdgesInWires.clear();
2898 myWallQuads.clear();
2901 //================================================================================
2903 * \brief Set upside-down
2905 //================================================================================
2907 void TPrismTopo::SetUpsideDown()
2909 std::swap( myBottom, myTop );
2910 myBottomEdges.clear();
2911 std::reverse( myBottomEdges.begin(), myBottomEdges.end() );
2912 for ( size_t i = 0; i < myWallQuads.size(); ++i )
2914 myWallQuads[i].reverse();
2915 TQuadList::iterator q = myWallQuads[i].begin();
2916 for ( ; q != myWallQuads[i].end(); ++q )
2918 (*q)->shift( 2, /*keepUnitOri=*/true );
2920 myBottomEdges.push_back( myWallQuads[i].front()->side[ QUAD_BOTTOM_SIDE ].grid->Edge(0) );
2924 } // namespace Prism_3D
2926 //================================================================================
2928 * \brief Constructor. Initialization is needed