1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // SMESH SMESH : implementaion of SMESH idl descriptions
21 // File : StdMeshers_CompositeHexa_3D.cxx
23 // Created : Tue Nov 25 11:04:59 2008
24 // Author : Edward AGAPOV (eap)
26 #include "StdMeshers_CompositeHexa_3D.hxx"
28 #include "SMDS_Mesh.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMESH_Block.hxx"
32 #include "SMESH_Comment.hxx"
33 #include "SMESH_ComputeError.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_MeshAlgos.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "SMESH_subMesh.hxx"
39 #include <BRepAdaptor_Surface.hxx>
40 #include <BRep_Tool.hxx>
41 #include <Standard_ErrorHandler.hxx>
42 #include <Standard_Failure.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TopTools_MapIteratorOfMapOfShape.hxx>
46 #include <TopTools_MapOfShape.hxx>
47 #include <TopTools_SequenceOfShape.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Vertex.hxx>
53 #include <gp_Pnt2d.hxx>
66 // #define DUMP_VERT(msg,V) { TopoDS_Vertex v = V; gp_Pnt p = BRep_Tool::Pnt(v); cout << msg << "( "<< p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl; }
70 #define DUMP_VERT(msg,v)
73 //================================================================================
74 // text for message about an internal error
75 #define ERR_LI(txt) SMESH_Comment(txt) << ":" << __LINE__
77 // order corresponds to right order of edges in CASCADE face
78 enum EQuadSides{ Q_BOTTOM=0, Q_RIGHT, Q_TOP, Q_LEFT, Q_CHILD, Q_PARENT };
80 enum EBoxSides{ B_BOTTOM=0, B_RIGHT, B_TOP, B_LEFT, B_FRONT, B_BACK, B_UNDEFINED };
82 enum EAxes{ COO_X=1, COO_Y, COO_Z };
84 //================================================================================
86 * \brief Convertor of a pair of integers to a sole index
91 _Indexer( int xSize, int ySize ): _xSize(xSize), _ySize(ySize) {}
92 int size() const { return _xSize * _ySize; }
93 int operator()(const int x, const int y) const { return y * _xSize + x; }
96 //================================================================================
98 * \brief Wrapper of a composite or an ordinary edge.
103 _FaceSide(const _FaceSide& other);
104 _FaceSide(const TopoDS_Edge& edge=TopoDS_Edge());
105 _FaceSide(const list<TopoDS_Edge>& edges);
106 _FaceSide* GetSide(const int i);
107 const _FaceSide* GetSide(const int i) const;
108 int size() const { return myChildren.size(); }
109 int NbVertices() const;
110 int NbCommonVertices( const TopTools_MapOfShape& VV ) const;
111 TopoDS_Vertex FirstVertex() const;
112 TopoDS_Vertex LastVertex() const;
113 TopoDS_Vertex Vertex(int i) const;
114 TopoDS_Edge Edge(int i) const;
115 bool Contain( const _FaceSide& side, int* which=0 ) const;
116 bool Contain( const TopoDS_Vertex& vertex ) const;
117 void AppendSide( const _FaceSide& side );
118 void SetBottomSide( int i );
119 int GetNbSegments(SMESH_Mesh& mesh) const;
120 bool StoreNodes(SMESH_Mesh& mesh, vector<const SMDS_MeshNode*>& myGrid, bool reverse );
121 void SetID(EQuadSides id) { myID = id; }
122 static inline const TopoDS_TShape* ptr(const TopoDS_Shape& theShape)
123 { return theShape.TShape().operator->(); }
130 list< _FaceSide > myChildren;
133 TopTools_MapOfShape myVertices;
135 EQuadSides myID; // debug
137 //================================================================================
139 * \brief Class corresponding to a meshed composite face of a box.
140 * Provides simplified access to it's sub-mesh data.
144 typedef list< _QuadFaceGrid > TChildren;
148 public: //** Methods to find and orient faces of 6 sides of the box **//
151 bool Init(const TopoDS_Face& f, SMESH_Mesh& mesh );
153 //!< try to unite self with other face
154 bool AddContinuousFace( const _QuadFaceGrid& f, const TopTools_MapOfShape& internalEdges );
156 //!< Try to set the side as bottom hirizontal side
157 bool SetBottomSide(const _FaceSide& side, int* sideIndex=0);
159 //!< Return face adjacent to zero-based i-th side of this face
160 _QuadFaceGrid* FindAdjacentForSide(int i, list<_QuadFaceGrid>& faces, EBoxSides id) const;
162 //!< Reverse edges in order to have the bottom edge going along axes of the unit box
165 bool IsComplex() const { return !myChildren.empty(); }
167 int NbChildren() const { return myChildren.size(); }
169 typedef SMDS_SetIterator< const _QuadFaceGrid&,
170 TChildren::const_iterator,
171 SMDS::SimpleAccessor<const _QuadFaceGrid&,TChildren::const_iterator>,
172 SMDS::PassAllValueFilter<_QuadFaceGrid> >
175 TChildIterator GetChildren() const
176 { return TChildIterator( myChildren.begin(), myChildren.end()); }
178 public: //** Loading and access to mesh **//
180 //!< Load nodes of a mesh
181 bool LoadGrid( SMESH_Mesh& mesh );
183 //!< Computes normalized parameters of nodes of myGrid
184 void ComputeIJK( int i1, int i2, double v3 );
186 //!< Return number of segments on the hirizontal sides
187 int GetNbHoriSegments(SMESH_Mesh& mesh, bool withBrothers=false) const;
189 //!< Return number of segments on the vertical sides
190 int GetNbVertSegments(SMESH_Mesh& mesh, bool withBrothers=false) const;
192 //!< Return edge on the hirizontal bottom sides
193 int GetHoriEdges(vector<TopoDS_Edge> & edges) const;
195 //!< Return a node by its position
196 const SMDS_MeshNode* GetNode(int iHori, int iVert) const;
198 //!< Return node coordinates by its position
199 gp_XYZ GetXYZ(int iHori, int iVert) const;
201 //!< Return normalized parameters of nodes within the unitary cube
202 gp_XYZ& GetIJK(int iCol, int iRow) { return myIJK[ myIndexer( iCol, iRow )]; }
204 public: //** Access to member fields **//
206 //!< Return i-th face side (0<i<4)
207 const _FaceSide& GetSide(int i) const;
209 //!< Return it's face, NULL if it is composite
210 TopoDS_Face GetFace() const { return myFace; }
212 //!< Return normal to the face at vertex v
213 bool GetNormal( const TopoDS_Vertex& v, gp_Vec& n ) const;
215 SMESH_ComputeErrorPtr GetError() const { return myError; }
217 void SetID(EBoxSides id) { myID = id; }
219 void DumpGrid() const;
221 void DumpVertices() const;
225 bool error(const std::string& text, int code = COMPERR_ALGO_FAILED)
226 { myError = SMESH_ComputeError::New( code, text ); return false; }
228 bool error(const SMESH_ComputeErrorPtr& err)
229 { myError = err; return ( !myError || myError->IsOK() ); }
231 bool loadCompositeGrid(SMESH_Mesh& mesh);
233 bool fillGrid(SMESH_Mesh& theMesh,
234 vector<const SMDS_MeshNode*> & theGrid,
235 const _Indexer& theIndexer,
239 bool locateChildren();
241 void setBrothers( set< _QuadFaceGrid* >& notLocatedBrothers );
247 TChildren myChildren;
249 _QuadFaceGrid* myLeftBottomChild;
250 _QuadFaceGrid* myRightBrother;
251 _QuadFaceGrid* myUpBrother;
254 vector<const SMDS_MeshNode*> myGrid;
255 vector<gp_XYZ> myIJK; // normalized parameters of nodes
257 SMESH_ComputeErrorPtr myError;
259 EBoxSides myID; // debug
262 //================================================================================
266 //================================================================================
268 StdMeshers_CompositeHexa_3D::StdMeshers_CompositeHexa_3D(int hypId, int studyId, SMESH_Gen* gen)
269 :SMESH_3D_Algo(hypId, studyId, gen)
271 _name = "CompositeHexa_3D";
272 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID); // 1 bit /shape type
275 //================================================================================
277 * \brief always return true
279 //================================================================================
281 bool StdMeshers_CompositeHexa_3D::CheckHypothesis(SMESH_Mesh& aMesh,
282 const TopoDS_Shape& aShape,
283 Hypothesis_Status& aStatus)
292 //================================================================================
294 * \brief Checks structure of a quadrangular mesh at the common VERTEX of two EDGEs.
295 * Returns true if there are two quadrangles near the VERTEX.
297 //================================================================================
299 bool isContinuousMesh(TopoDS_Edge E1,
301 const TopoDS_Face& F,
302 const SMESH_Mesh& mesh)
304 if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
305 E1.Orientation( TopAbs_FORWARD );
306 if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
307 E2.Orientation( TopAbs_FORWARD );
310 if ( !TopExp::CommonVertex( E1, E2, V )) return false;
312 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
313 if ( !n ) return false;
315 SMESHDS_SubMesh* sm = mesh.GetSubMeshContaining( F )->GetSubMeshDS();
316 if ( !sm ) return false;
319 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
320 while ( fIt->more() )
322 const SMDS_MeshElement* f = fIt->next();
323 if ( !sm->Contains( f )) continue;
325 if ( f->NbCornerNodes() == 4 )
333 //================================================================================
335 * \brief Finds VERTEXes located at block corners
337 //================================================================================
339 void getBlockCorners( SMESH_Mesh& mesh,
340 const TopoDS_Shape& shape,
341 TopTools_MapOfShape& cornerVV)
343 set<int> faceIDs; // ids of FACEs in the shape
345 for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next() )
346 faceIDs.insert( mesh.GetMeshDS()->ShapeToIndex( exp.Current() ));
348 TopTools_MapOfShape checkedVV;
349 for ( exp.Init( shape, TopAbs_VERTEX ); exp.More(); exp.Next() )
351 TopoDS_Vertex V = TopoDS::Vertex( exp.Current() );
352 if ( !checkedVV.Add( V )) continue;
354 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
358 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
359 while ( fIt->more() )
361 const SMDS_MeshElement* f = fIt->next();
362 if ( !faceIDs.count( f->getshapeId() )) continue;
364 if ( f->NbCornerNodes() == 4 )
374 //================================================================================
376 * \brief Return EDGEs dividing one box side
378 //================================================================================
380 bool getInternalEdges( SMESH_Mesh& mesh,
381 const TopoDS_Shape& shape,
382 const TopTools_MapOfShape& cornerVV,
383 TopTools_MapOfShape& internEE)
385 TopTools_IndexedMapOfShape subEE, subFF;
386 TopExp::MapShapes( shape, TopAbs_EDGE, subEE );
387 TopExp::MapShapes( shape, TopAbs_FACE, subFF );
390 TopTools_MapOfShape subChecked/*, ridgeEE*/;
391 TopTools_MapIteratorOfMapOfShape vIt( cornerVV );
392 for ( ; vIt.More(); vIt.Next() )
394 TopoDS_Shape V0 = vIt.Key();
395 // walk from one corner VERTEX to another along ridge EDGEs
396 PShapeIteratorPtr riIt = SMESH_MesherHelper::GetAncestors( V0, mesh, TopAbs_EDGE );
397 while ( const TopoDS_Shape* riE = riIt->next() )
399 if ( !subEE.Contains( *riE ) || !subChecked.Add( *riE ))
401 TopoDS_Edge ridgeE = TopoDS::Edge( *riE );
402 while ( !ridgeE.IsNull() )
404 TopExp::Vertices( ridgeE, VV[0], VV[1] );
405 TopoDS_Shape V1 = VV[ V0.IsSame( VV[0] )];
406 if ( cornerVV.Contains( V1 ) )
407 break; // ridgeE reached a corner VERTEX
409 // detect internal EDGEs among those sharing V1. There can be 2, 3 or 4 EDGEs and
410 // number of internal EDGEs is N-2
411 TopoDS_Shape nextRidgeE;
412 PShapeIteratorPtr eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
413 while ( const TopoDS_Shape* E = eIt->next() )
415 if ( E->IsSame( ridgeE ) || !subEE.Contains( *E ) || !subChecked.Add( *E ))
417 // look for FACEs sharing both E and ridgeE
418 PShapeIteratorPtr fIt = SMESH_MesherHelper::GetAncestors( *E, mesh, TopAbs_FACE );
419 while ( const TopoDS_Shape* F = fIt->next() )
421 if ( !SMESH_MesherHelper::IsSubShape( ridgeE, *F ))
423 if ( isContinuousMesh( ridgeE, TopoDS::Edge( *E ), TopoDS::Face( *F ), mesh ))
434 // look for the next ridge EDGE ending at V1
435 if ( nextRidgeE.IsNull() )
437 eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
438 while ( const TopoDS_Shape* E = eIt->next() )
439 if ( !ridgeE.IsSame( *E ) && !internEE.Contains( *E ) && subEE.Contains( *E ))
445 ridgeE = TopoDS::Edge( nextRidgeE );
448 if ( ridgeE.IsNull() )
450 } // check EDGEs around the last VERTEX of ridgeE
451 } // loop on ridge EDGEs around a corner VERTEX
452 } // loop on on corner VERTEXes
455 } // getInternalEdges()
458 //================================================================================
460 * \brief Tries to find 6 sides of a box
462 //================================================================================
464 bool StdMeshers_CompositeHexa_3D::findBoxFaces( const TopoDS_Shape& shape,
465 list< _QuadFaceGrid >& boxFaces,
467 _QuadFaceGrid * & fBottom,
468 _QuadFaceGrid * & fTop,
469 _QuadFaceGrid * & fFront,
470 _QuadFaceGrid * & fBack,
471 _QuadFaceGrid * & fLeft,
472 _QuadFaceGrid * & fRight)
474 TopTools_MapOfShape cornerVertices;
475 getBlockCorners( mesh, shape, cornerVertices );
476 if ( cornerVertices.Extent() != 8 )
477 return error( COMPERR_BAD_INPUT_MESH, "Can't find 8 corners of a block by 2D mesh" );
478 TopTools_MapOfShape internalEdges;
479 if ( !getInternalEdges( mesh, shape, cornerVertices, internalEdges ))
480 return error( COMPERR_BAD_INPUT_MESH, "2D mesh is not suitable for i,j,k hexa meshing" );
482 list< _QuadFaceGrid >::iterator boxFace;
485 for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next(), ++nbFaces )
488 if ( !f.Init( TopoDS::Face( exp.Current() ), mesh ))
489 return error (COMPERR_BAD_SHAPE);
491 _QuadFaceGrid* prevContinuous = 0;
492 for ( boxFace = boxFaces.begin(); boxFace != boxFaces.end(); ++boxFace )
494 if ( prevContinuous )
496 if ( prevContinuous->AddContinuousFace( *boxFace, internalEdges ))
497 boxFace = --boxFaces.erase( boxFace );
499 else if ( boxFace->AddContinuousFace( f, internalEdges ))
501 prevContinuous = & (*boxFace);
504 if ( !prevContinuous )
505 boxFaces.push_back( f );
507 // Check what we have
508 if ( boxFaces.size() != 6 && nbFaces != 6)
511 SMESH_Comment("Can't find 6 sides of a box. Number of found sides - ")<<boxFaces.size());
513 if ( boxFaces.size() != 6 && nbFaces == 6 ) { // strange ordinary box with continuous faces
514 boxFaces.resize( 6 );
515 boxFace = boxFaces.begin();
516 for ( exp.Init( shape, TopAbs_FACE); exp.More(); exp.Next(), ++boxFace )
517 boxFace->Init( TopoDS::Face( exp.Current() ), mesh );
519 // ----------------------------------------
520 // Find out position of faces within a box
521 // ----------------------------------------
522 // start from a bottom face
523 fBottom = &boxFaces.front();
524 fBottom->SetID( B_BOTTOM );
525 // find vertical faces
526 fFront = fBottom->FindAdjacentForSide( Q_BOTTOM, boxFaces, B_FRONT );
527 fLeft = fBottom->FindAdjacentForSide( Q_RIGHT, boxFaces, B_LEFT );
528 fBack = fBottom->FindAdjacentForSide( Q_TOP, boxFaces, B_BACK );
529 fRight = fBottom->FindAdjacentForSide( Q_LEFT, boxFaces, B_RIGHT );
531 if ( !fFront || !fBack || !fLeft || !fRight )
532 return error(COMPERR_BAD_SHAPE);
535 for ( boxFace = ++boxFaces.begin(); boxFace != boxFaces.end() && !fTop; ++boxFace )
538 fTop->SetID( B_TOP );
539 if ( fTop==fFront || fTop==fLeft || fTop==fBack || fTop==fRight )
542 // set bottom of the top side
543 if ( !fTop->SetBottomSide( fFront->GetSide( Q_TOP ) )) {
544 if ( !fFront->IsComplex() )
545 return error( ERR_LI("Error in StdMeshers_CompositeHexa_3D::Compute()"));
547 _QuadFaceGrid::TChildIterator chIt = fFront->GetChildren();
548 while ( chIt.more() ) {
549 const _QuadFaceGrid& frontChild = chIt.next();
550 if ( fTop->SetBottomSide( frontChild.GetSide( Q_TOP )))
556 return error(COMPERR_BAD_SHAPE);
558 // orient bottom egde of faces along axes of the unit box
559 fBottom->ReverseEdges();
560 fBack ->ReverseEdges();
561 fLeft ->ReverseEdges();
566 //================================================================================
568 * \brief Computes hexahedral mesh on a box with composite sides
569 * \param aMesh - mesh to compute
570 * \param aShape - shape to mesh
571 * \retval bool - succes sign
573 //================================================================================
575 bool StdMeshers_CompositeHexa_3D::Compute(SMESH_Mesh& theMesh,
576 const TopoDS_Shape& theShape)
578 SMESH_MesherHelper helper( theMesh );
579 _quadraticMesh = helper.IsQuadraticSubMesh( theShape );
580 helper.SetElementsOnShape( true );
582 // -------------------------
583 // Try to find 6 side faces
584 // -------------------------
585 list< _QuadFaceGrid > boxFaceContainer;
586 _QuadFaceGrid *fBottom, *fTop, *fFront, *fBack, *fLeft, *fRight;
587 if ( ! findBoxFaces( theShape, boxFaceContainer, theMesh,
588 fBottom, fTop, fFront, fBack, fLeft, fRight))
591 // ------------------------------------------
592 // Fill columns of nodes with existing nodes
593 // ------------------------------------------
595 // let faces load their grids
596 if ( !fBottom->LoadGrid( theMesh )) return error( fBottom->GetError() );
597 if ( !fBack ->LoadGrid( theMesh )) return error( fBack ->GetError() );
598 if ( !fLeft ->LoadGrid( theMesh )) return error( fLeft ->GetError() );
599 if ( !fFront ->LoadGrid( theMesh )) return error( fFront ->GetError() );
600 if ( !fRight ->LoadGrid( theMesh )) return error( fRight ->GetError() );
601 if ( !fTop ->LoadGrid( theMesh )) return error( fTop ->GetError() );
603 // compute normalized parameters of nodes on sides (PAL23189)
604 fBottom->ComputeIJK( COO_X, COO_Y, /*z=*/0. );
605 fBack ->ComputeIJK( COO_X, COO_Z, /*y=*/1. );
606 fLeft ->ComputeIJK( COO_Y, COO_Z, /*x=*/0. );
607 fFront ->ComputeIJK( COO_X, COO_Z, /*y=*/0. );
608 fRight ->ComputeIJK( COO_Y, COO_Z, /*x=*/1. );
609 fTop ->ComputeIJK( COO_X, COO_Y, /*z=*/1. );
611 int x, xSize = fBottom->GetNbHoriSegments(theMesh) + 1, X = xSize - 1;
612 int y, ySize = fBottom->GetNbVertSegments(theMesh) + 1, Y = ySize - 1;
613 int z, zSize = fFront ->GetNbVertSegments(theMesh) + 1, Z = zSize - 1;
614 _Indexer colIndex( xSize, ySize );
615 vector< vector< const SMDS_MeshNode* > > columns( colIndex.size() );
617 // fill node columns by front and back box sides
618 for ( x = 0; x < xSize; ++x ) {
619 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( x, 0 )];
620 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( x, Y )];
621 column0.resize( zSize );
622 column1.resize( zSize );
623 for ( z = 0; z < zSize; ++z ) {
624 column0[ z ] = fFront->GetNode( x, z );
625 column1[ z ] = fBack ->GetNode( x, z );
628 // fill node columns by left and right box sides
629 for ( y = 1; y < ySize-1; ++y ) {
630 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( 0, y )];
631 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( X, y )];
632 column0.resize( zSize );
633 column1.resize( zSize );
634 for ( z = 0; z < zSize; ++z ) {
635 column0[ z ] = fLeft ->GetNode( y, z );
636 column1[ z ] = fRight->GetNode( y, z );
639 // get nodes from top and bottom box sides
640 for ( x = 1; x < xSize-1; ++x ) {
641 for ( y = 1; y < ySize-1; ++y ) {
642 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
643 column.resize( zSize );
644 column.front() = fBottom->GetNode( x, y );
645 column.back() = fTop ->GetNode( x, y );
649 // ----------------------------
650 // Add internal nodes of a box
651 // ----------------------------
652 // projection points of internal nodes on box sub-shapes by which
653 // coordinates of internal nodes are computed
654 vector<gp_XYZ> pointsOnShapes( SMESH_Block::ID_Shell );
656 // projections on vertices are constant
657 pointsOnShapes[ SMESH_Block::ID_V000 ] = fBottom->GetXYZ( 0, 0 );
658 pointsOnShapes[ SMESH_Block::ID_V100 ] = fBottom->GetXYZ( X, 0 );
659 pointsOnShapes[ SMESH_Block::ID_V010 ] = fBottom->GetXYZ( 0, Y );
660 pointsOnShapes[ SMESH_Block::ID_V110 ] = fBottom->GetXYZ( X, Y );
661 pointsOnShapes[ SMESH_Block::ID_V001 ] = fTop->GetXYZ( 0, 0 );
662 pointsOnShapes[ SMESH_Block::ID_V101 ] = fTop->GetXYZ( X, 0 );
663 pointsOnShapes[ SMESH_Block::ID_V011 ] = fTop->GetXYZ( 0, Y );
664 pointsOnShapes[ SMESH_Block::ID_V111 ] = fTop->GetXYZ( X, Y );
666 gp_XYZ params; // normalized parameters of an internal node within the unit box
668 for ( x = 1; x < xSize-1; ++x )
670 const double rX = x / double(X);
671 for ( y = 1; y < ySize-1; ++y )
673 const double rY = y / double(Y);
674 // column to fill during z loop
675 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
676 // points projections on horizontal edges
677 pointsOnShapes[ SMESH_Block::ID_Ex00 ] = fBottom->GetXYZ( x, 0 );
678 pointsOnShapes[ SMESH_Block::ID_Ex10 ] = fBottom->GetXYZ( x, Y );
679 pointsOnShapes[ SMESH_Block::ID_E0y0 ] = fBottom->GetXYZ( 0, y );
680 pointsOnShapes[ SMESH_Block::ID_E1y0 ] = fBottom->GetXYZ( X, y );
681 pointsOnShapes[ SMESH_Block::ID_Ex01 ] = fTop->GetXYZ( x, 0 );
682 pointsOnShapes[ SMESH_Block::ID_Ex11 ] = fTop->GetXYZ( x, Y );
683 pointsOnShapes[ SMESH_Block::ID_E0y1 ] = fTop->GetXYZ( 0, y );
684 pointsOnShapes[ SMESH_Block::ID_E1y1 ] = fTop->GetXYZ( X, y );
685 // points projections on horizontal faces
686 pointsOnShapes[ SMESH_Block::ID_Fxy0 ] = fBottom->GetXYZ( x, y );
687 pointsOnShapes[ SMESH_Block::ID_Fxy1 ] = fTop ->GetXYZ( x, y );
688 for ( z = 1; z < zSize-1; ++z ) // z loop
690 // compute normalized parameters of an internal node within the unit box
691 const double rZ = z / double(Z);
692 const gp_XYZ& pBo = fBottom->GetIJK( x, y );
693 const gp_XYZ& pTo = fTop ->GetIJK( x, y );
694 const gp_XYZ& pFr = fFront ->GetIJK( x, z );
695 const gp_XYZ& pBa = fBack ->GetIJK( x, z );
696 const gp_XYZ& pLe = fLeft ->GetIJK( y, z );
697 const gp_XYZ& pRi = fRight ->GetIJK( y, z );
698 params.SetCoord( 1, 0.5 * ( pBo.X() * ( 1. - rZ ) + pTo.X() * rZ +
699 pFr.X() * ( 1. - rY ) + pBa.X() * rY ));
700 params.SetCoord( 2, 0.5 * ( pBo.Y() * ( 1. - rZ ) + pTo.Y() * rZ +
701 pLe.Y() * ( 1. - rX ) + pRi.Y() * rX ));
702 params.SetCoord( 3, 0.5 * ( pFr.Z() * ( 1. - rY ) + pBa.Z() * rY +
703 pLe.Z() * ( 1. - rX ) + pRi.Z() * rX ));
705 // point projections on vertical edges
706 pointsOnShapes[ SMESH_Block::ID_E00z ] = fFront->GetXYZ( 0, z );
707 pointsOnShapes[ SMESH_Block::ID_E10z ] = fFront->GetXYZ( X, z );
708 pointsOnShapes[ SMESH_Block::ID_E01z ] = fBack->GetXYZ( 0, z );
709 pointsOnShapes[ SMESH_Block::ID_E11z ] = fBack->GetXYZ( X, z );
710 // point projections on vertical faces
711 pointsOnShapes[ SMESH_Block::ID_Fx0z ] = fFront->GetXYZ( x, z );
712 pointsOnShapes[ SMESH_Block::ID_Fx1z ] = fBack ->GetXYZ( x, z );
713 pointsOnShapes[ SMESH_Block::ID_F0yz ] = fLeft ->GetXYZ( y, z );
714 pointsOnShapes[ SMESH_Block::ID_F1yz ] = fRight->GetXYZ( y, z );
716 // compute internal node coordinates
718 SMESH_Block::ShellPoint( params, pointsOnShapes, coords );
719 column[ z ] = helper.AddNode( coords.X(), coords.Y(), coords.Z() );
723 //cout << "----------------------------------------------------------------------"<<endl;
724 //for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id)
726 // gp_XYZ p = pointsOnShapes[ id ];
727 // SMESH_Block::DumpShapeID( id,cout)<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;
729 //cout << "Params: ( "<< params.X()<<", "<<params.Y()<<", "<<params.Z()<<" )"<<endl;
730 //cout << "coords: ( "<< coords.X()<<", "<<coords.Y()<<", "<<coords.Z()<<" )"<<endl;
735 // faces no more needed, free memory
736 boxFaceContainer.clear();
741 for ( x = 0; x < xSize-1; ++x ) {
742 for ( y = 0; y < ySize-1; ++y ) {
743 vector< const SMDS_MeshNode* >& col00 = columns[ colIndex( x, y )];
744 vector< const SMDS_MeshNode* >& col10 = columns[ colIndex( x+1, y )];
745 vector< const SMDS_MeshNode* >& col01 = columns[ colIndex( x, y+1 )];
746 vector< const SMDS_MeshNode* >& col11 = columns[ colIndex( x+1, y+1 )];
747 for ( z = 0; z < zSize-1; ++z )
749 // bottom face normal of a hexa mush point outside the volume
750 helper.AddVolume(col00[z], col01[z], col11[z], col10[z],
751 col00[z+1], col01[z+1], col11[z+1], col10[z+1]);
758 //================================================================================
762 //================================================================================
764 bool StdMeshers_CompositeHexa_3D::Evaluate(SMESH_Mesh& theMesh,
765 const TopoDS_Shape& theShape,
766 MapShapeNbElems& aResMap)
768 // -------------------------
769 // Try to find 6 side faces
770 // -------------------------
771 list< _QuadFaceGrid > boxFaceContainer;
772 _QuadFaceGrid *fBottom, *fTop, *fFront, *fBack, *fLeft, *fRight;
773 if ( ! findBoxFaces( theShape, boxFaceContainer, theMesh,
774 fBottom, fTop, fFront, fBack, fLeft, fRight))
777 // Find a less complex side
778 _QuadFaceGrid * lessComplexSide = & boxFaceContainer.front();
779 list< _QuadFaceGrid >::iterator face = boxFaceContainer.begin();
780 for ( ++face; face != boxFaceContainer.end() && lessComplexSide->IsComplex(); ++face )
781 if ( face->NbChildren() < lessComplexSide->NbChildren() )
782 lessComplexSide = & *face;
784 // Get an 1D size of lessComplexSide
786 vector<TopoDS_Edge> edges;
787 if ( !lessComplexSide->GetHoriEdges(edges) )
789 for ( size_t i = 0; i < edges.size(); ++i )
791 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( edges[i] )];
792 if ( !nbElems.empty() )
793 nbSeg1 += Max( nbElems[ SMDSEntity_Edge ], nbElems[ SMDSEntity_Quad_Edge ]);
796 // Get an 1D size of a box side ortogonal to lessComplexSide
798 _QuadFaceGrid* ortoSide =
799 lessComplexSide->FindAdjacentForSide( Q_LEFT, boxFaceContainer, B_UNDEFINED );
801 if ( !ortoSide || !ortoSide->GetHoriEdges(edges) ) return false;
802 for ( size_t i = 0; i < edges.size(); ++i )
804 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( edges[i] )];
805 if ( !nbElems.empty() )
806 nbSeg2 += Max( nbElems[ SMDSEntity_Edge ], nbElems[ SMDSEntity_Quad_Edge ]);
809 // Get an 2D size of a box side ortogonal to lessComplexSide
810 int nbFaces = 0, nbQuadFace = 0;
811 list< TopoDS_Face > sideFaces;
812 if ( ortoSide->IsComplex() )
813 for ( _QuadFaceGrid::TChildIterator child = ortoSide->GetChildren(); child.more(); )
814 sideFaces.push_back( child.next().GetFace() );
816 sideFaces.push_back( ortoSide->GetFace() );
818 list< TopoDS_Face >::iterator f = sideFaces.begin();
819 for ( ; f != sideFaces.end(); ++f )
821 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( *f )];
822 if ( !nbElems.empty() )
824 nbFaces = nbElems[ SMDSEntity_Quadrangle ];
825 nbQuadFace = nbElems[ SMDSEntity_Quad_Quadrangle ];
829 // Fill nb of elements
830 vector<int> aResVec(SMDSEntity_Last,0);
831 int nbSeg3 = ( nbFaces + nbQuadFace ) / nbSeg2;
832 aResVec[SMDSEntity_Node] = (nbSeg1-1) * (nbSeg2-1) * (nbSeg3-1);
833 aResVec[SMDSEntity_Hexa] = nbSeg1 * nbFaces;
834 aResVec[SMDSEntity_Quad_Hexa] = nbSeg1 * nbQuadFace;
836 aResMap.insert( make_pair( theMesh.GetSubMesh(theShape), aResVec ));
842 //================================================================================
844 * \brief constructor of non-initialized _QuadFaceGrid
846 //================================================================================
848 _QuadFaceGrid::_QuadFaceGrid():
849 myReverse(false), myRightBrother(0), myUpBrother(0), myIndexer(0,0), myID(B_UNDEFINED)
853 //================================================================================
855 * \brief Initialization
857 //================================================================================
859 bool _QuadFaceGrid::Init(const TopoDS_Face& f, SMESH_Mesh& mesh)
862 mySides = _FaceSide();
864 myLeftBottomChild = myRightBrother = myUpBrother = 0;
867 //if ( myFace.Orientation() != TopAbs_FORWARD )
870 list< TopoDS_Edge > edges;
871 list< int > nbEdgesInWire;
872 int nbWire = SMESH_Block::GetOrderedEdges (myFace, edges, nbEdgesInWire);
876 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
877 if ( nbEdgesInWire.front() == 4 ) // exactly 4 edges
879 for ( ; edgeIt != edges.end(); ++edgeIt )
880 mySides.AppendSide( _FaceSide( *edgeIt ));
882 else if ( nbEdgesInWire.front() > 4 ) { // more than 4 edges - try to unite some
883 list< TopoDS_Edge > sideEdges;
884 while ( !edges.empty()) {
886 sideEdges.splice( sideEdges.end(), edges, edges.begin());// edges.front()->sideEdges.back()
887 while ( !edges.empty() ) {
888 if ( SMESH_Algo::IsContinuous( sideEdges.back(), edges.front() )) {
889 sideEdges.splice( sideEdges.end(), edges, edges.begin());
891 else if ( SMESH_Algo::IsContinuous( sideEdges.front(), edges.back() )) {
892 sideEdges.splice( sideEdges.begin(), edges, --edges.end());
894 else if ( isContinuousMesh( sideEdges.back(), edges.front(), f, mesh )) {
895 sideEdges.splice( sideEdges.end(), edges, edges.begin());
897 else if ( isContinuousMesh( sideEdges.front(), edges.back(), f, mesh )) {
898 sideEdges.splice( sideEdges.begin(), edges, --edges.end());
904 mySides.AppendSide( _FaceSide( sideEdges ));
907 if (mySides.size() != 4)
911 mySides.GetSide( Q_BOTTOM )->SetID( Q_BOTTOM );
912 mySides.GetSide( Q_RIGHT )->SetID( Q_RIGHT );
913 mySides.GetSide( Q_TOP )->SetID( Q_TOP );
914 mySides.GetSide( Q_LEFT )->SetID( Q_LEFT );
920 //================================================================================
922 * \brief Try to unite self with other ordinary face
924 //================================================================================
926 bool _QuadFaceGrid::AddContinuousFace( const _QuadFaceGrid& other,
927 const TopTools_MapOfShape& internalEdges)
929 for ( int i = 0; i < 4; ++i )
931 const _FaceSide& otherSide = other.GetSide( i );
933 if ( mySides.Contain( otherSide, &iMyCommon ))
935 if ( internalEdges.Contains( otherSide.Edge( 0 )))
937 DUMP_VERT("Cont 1", mySides.GetSide(iMyCommon)->FirstVertex());
938 DUMP_VERT("Cont 2", mySides.GetSide(iMyCommon)->LastVertex());
939 DUMP_VERT("Cont 3", otherSide.FirstVertex());
940 DUMP_VERT("Cont 4", otherSide.LastVertex());
942 if ( myChildren.empty() )
944 myChildren.push_back( *this );
947 else // find iMyCommon in myChildren
949 for ( TChildIterator children = GetChildren(); children.more(); ) {
950 const _QuadFaceGrid& child = children.next();
951 if ( child.mySides.Contain( otherSide, &iMyCommon ))
956 // orient new children equally
957 int otherBottomIndex = SMESH_MesherHelper::WrapIndex( i - iMyCommon + 2, 4 );
958 if ( other.IsComplex() )
959 for ( TChildIterator children = other.GetChildren(); children.more(); ) {
960 myChildren.push_back( children.next() );
961 myChildren.back().SetBottomSide( myChildren.back().GetSide( otherBottomIndex ));
964 myChildren.push_back( other );
965 myChildren.back().SetBottomSide( myChildren.back().GetSide( otherBottomIndex ));
968 myLeftBottomChild = 0;
970 // collect vertices in mySides
971 if ( other.IsComplex() )
972 for ( TChildIterator children = other.GetChildren(); children.more(); )
974 const _QuadFaceGrid& child = children.next();
975 for ( int i = 0; i < 4; ++i )
976 mySides.AppendSide( child.GetSide(i) );
979 for ( int i = 0; i < 4; ++i )
980 mySides.AppendSide( other.GetSide(i) );
989 //================================================================================
991 * \brief Try to set the side as bottom hirizontal side
993 //================================================================================
995 bool _QuadFaceGrid::SetBottomSide(const _FaceSide& bottom, int* sideIndex)
997 myLeftBottomChild = myRightBrother = myUpBrother = 0;
1000 if ( myChildren.empty() )
1002 if ( mySides.Contain( bottom, &myBottomIndex )) {
1003 mySides.SetBottomSide( myBottomIndex );
1005 *sideIndex = myBottomIndex;
1011 TChildren::iterator childFace = myChildren.begin(), childEnd = myChildren.end();
1012 for ( ; childFace != childEnd; ++childFace )
1014 if ( childFace->SetBottomSide( bottom, &myBottomIndex ))
1016 TChildren::iterator orientedChild = childFace;
1017 for ( childFace = myChildren.begin(); childFace != childEnd; ++childFace ) {
1018 if ( childFace != orientedChild )
1019 childFace->SetBottomSide( childFace->GetSide( myBottomIndex ));
1022 *sideIndex = myBottomIndex;
1030 //================================================================================
1032 * \brief Return face adjacent to i-th side of this face, (0<i<4)
1034 //================================================================================
1036 _QuadFaceGrid* _QuadFaceGrid::FindAdjacentForSide(int i,
1037 list<_QuadFaceGrid>& faces,
1040 const _FaceSide & iSide = GetSide( i );
1041 list< _QuadFaceGrid >::iterator boxFace = faces.begin();
1042 for ( ; boxFace != faces.end(); ++boxFace )
1044 _QuadFaceGrid* f = & (*boxFace);
1045 if ( f != this && f->SetBottomSide( iSide ))
1046 return f->SetID( id ), f;
1048 return (_QuadFaceGrid*) 0;
1051 //================================================================================
1053 * \brief Return i-th side
1055 //================================================================================
1057 const _FaceSide& _QuadFaceGrid::GetSide(int i) const
1059 if ( myChildren.empty() )
1060 return *mySides.GetSide(i);
1062 _QuadFaceGrid* me = const_cast<_QuadFaceGrid*>(this);
1063 if ( !me->locateChildren() || !myLeftBottomChild )
1064 return *mySides.GetSide(i);
1066 const _QuadFaceGrid* child = myLeftBottomChild;
1072 while ( child->myRightBrother )
1073 child = child->myRightBrother;
1076 while ( child->myUpBrother )
1077 child = child->myUpBrother;
1081 return child->GetSide( i );
1084 //================================================================================
1086 * \brief Reverse edges in order to have them oriented along axes of the unit box
1088 //================================================================================
1090 void _QuadFaceGrid::ReverseEdges()
1092 myReverse = !myReverse;
1095 // if ( !myFace.IsNull() )
1096 // TopAbs::Print(myFace.Orientation(), cout);
1099 if ( myChildren.empty() )
1106 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1107 for ( ; child != childEnd; ++child )
1108 child->ReverseEdges();
1112 //================================================================================
1114 * \brief Load nodes of a mesh
1116 //================================================================================
1118 bool _QuadFaceGrid::LoadGrid( SMESH_Mesh& mesh )
1120 if ( !myChildren.empty() )
1122 // Let child faces load their grids
1123 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1124 for ( ; child != childEnd; ++child ) {
1125 child->SetID( myID );
1126 if ( !child->LoadGrid( mesh ) )
1127 return error( child->GetError() );
1129 // Fill myGrid with nodes of patches
1130 return loadCompositeGrid( mesh );
1133 // ---------------------------------------
1134 // Fill myGrid with nodes bound to myFace
1135 // ---------------------------------------
1137 if ( !myGrid.empty() )
1140 SMESHDS_SubMesh* faceSubMesh = mesh.GetSubMesh( myFace )->GetSubMeshDS();
1141 // check that all faces are quadrangular
1142 SMDS_ElemIteratorPtr fIt = faceSubMesh->GetElements();
1143 while ( fIt->more() )
1144 if ( fIt->next()->NbNodes() % 4 > 0 )
1145 return error("Non-quadrangular mesh faces are not allowed on sides of a composite block");
1147 myIndexer._xSize = 1 + mySides.GetSide( Q_BOTTOM )->GetNbSegments( mesh );
1148 myIndexer._ySize = 1 + mySides.GetSide( Q_LEFT )->GetNbSegments( mesh );
1150 myGrid.resize( myIndexer.size() );
1152 // strore nodes bound to the bottom edge
1153 mySides.GetSide( Q_BOTTOM )->StoreNodes( mesh, myGrid, myReverse );
1155 // store the rest nodes row by row
1157 TIDSortedElemSet emptySet, avoidSet;
1158 const SMDS_MeshElement* firstQuad = 0; // most left face above the last row of found nodes
1160 size_t nbFoundNodes = myIndexer._xSize;
1161 while ( nbFoundNodes != myGrid.size() )
1163 // first and last nodes of the last filled row of nodes
1164 const SMDS_MeshNode* n1down = myGrid[ nbFoundNodes - myIndexer._xSize ];
1165 const SMDS_MeshNode* n2down = myGrid[ nbFoundNodes - myIndexer._xSize + 1];
1166 const SMDS_MeshNode* n1downLast = myGrid[ nbFoundNodes-1 ];
1168 // find the first face above the row by the first two left nodes
1176 firstQuad = SMESH_MeshAlgos::FindFaceInSet( n1down, n2down, emptySet, avoidSet);
1177 while ( firstQuad && !faceSubMesh->Contains( firstQuad )) {
1178 avoidSet.insert( firstQuad );
1179 firstQuad = SMESH_MeshAlgos::FindFaceInSet( n1down, n2down, emptySet, avoidSet);
1181 if ( !firstQuad || !faceSubMesh->Contains( firstQuad ))
1182 return error(ERR_LI("Error in _QuadFaceGrid::LoadGrid()"));
1184 // find the node of quad bound to the left geom edge
1185 int i2down = firstQuad->GetNodeIndex( n2down );
1186 const SMDS_MeshNode* n1up = firstQuad->GetNode(( i2down+2 ) % 4 );
1187 myGrid[ nbFoundNodes++ ] = n1up;
1188 // the 4-the node of the first quad
1189 int i1down = firstQuad->GetNodeIndex( n1down );
1190 const SMDS_MeshNode* n2up = firstQuad->GetNode(( i1down+2 ) % 4 );
1191 myGrid[ nbFoundNodes++ ] = n2up;
1195 const SMDS_MeshElement* quad = firstQuad;
1197 // find the rest nodes by remaining faces above the row
1205 while ( n1down != n1downLast )
1208 avoidSet.clear(); avoidSet.insert( quad );
1209 quad = SMESH_MeshAlgos::FindFaceInSet( n1down, n1up, emptySet, avoidSet );
1210 if ( !quad || quad->NbNodes() % 4 > 0)
1211 return error(ERR_LI("Error in _QuadFaceGrid::LoadGrid()"));
1214 if ( quad->GetNode( i1down ) != n1down ) // check already found index
1215 i1down = quad->GetNodeIndex( n1down );
1216 n2up = quad->GetNode(( i1down+2 ) % 4 );
1217 myGrid[ nbFoundNodes++ ] = n2up;
1219 n1down = myGrid[ nbFoundNodes - myIndexer._xSize - 1 ];
1222 avoidSet.clear(); avoidSet.insert( firstQuad );
1224 DumpGrid(); // debug
1229 //================================================================================
1231 * \brief Fill myIJK with normalized parameters of nodes in myGrid
1232 * \param [in] i1 - coordinate index along rows of myGrid
1233 * \param [in] i2 - coordinate index along columns of myGrid
1234 * \param [in] v3 - value of the constant parameter
1236 //================================================================================
1238 void _QuadFaceGrid::ComputeIJK( int i1, int i2, double v3 )
1240 gp_XYZ ijk( v3, v3, v3 );
1241 myIJK.resize( myIndexer.size(), ijk );
1243 const size_t nbCol = myIndexer._xSize;
1244 const size_t nbRow = myIndexer._ySize;
1246 vector< double > len( nbRow );
1248 for ( size_t i = 0; i < nbCol; ++i )
1250 gp_Pnt pPrev = GetXYZ( i, 0 );
1251 for ( size_t j = 1; j < nbRow; ++j )
1253 gp_Pnt p = GetXYZ( i, j );
1254 len[ j ] = len[ j-1 ] + p.Distance( pPrev );
1257 for ( size_t j = 0; j < nbRow; ++j )
1258 GetIJK( i, j ).SetCoord( i2, len[ j ]/len.back() );
1261 len.resize( nbCol );
1262 for ( size_t j = 0; j < nbRow; ++j )
1264 gp_Pnt pPrev = GetXYZ( 0, j );
1265 for ( size_t i = 1; i < nbCol; ++i )
1267 gp_Pnt p = GetXYZ( i, j );
1268 len[ i ] = len[ i-1 ] + p.Distance( pPrev );
1271 for ( size_t i = 0; i < nbCol; ++i )
1272 GetIJK( i, j ).SetCoord( i1, len[ i ]/len.back() );
1276 //================================================================================
1278 * \brief Find out mutual location of children: find their right and up brothers
1280 //================================================================================
1282 bool _QuadFaceGrid::locateChildren()
1284 if ( myLeftBottomChild )
1287 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1289 // find a child sharing it's first bottom vertex with no other brother
1290 myLeftBottomChild = 0;
1291 for ( ; !myLeftBottomChild && child != childEnd; ++child )
1293 TopoDS_Vertex leftVertex = child->GetSide( Q_BOTTOM ).FirstVertex();
1294 bool sharedVertex = false;
1295 TChildren::iterator otherChild = myChildren.begin();
1296 for ( ; otherChild != childEnd && !sharedVertex; ++otherChild )
1297 if ( otherChild != child )
1298 sharedVertex = otherChild->mySides.Contain( leftVertex );
1299 if ( !sharedVertex ) {
1300 myLeftBottomChild = & (*child);
1301 DUMP_VERT("0 left bottom Vertex: ",leftVertex );
1304 if (!myLeftBottomChild)
1305 return error(ERR_LI("Error in locateChildren()"));
1307 set< _QuadFaceGrid* > notLocatedChilren;
1308 for (child = myChildren.begin() ; child != childEnd; ++child )
1309 notLocatedChilren.insert( & (*child));
1311 // connect myLeftBottomChild to it's right and upper brothers
1312 notLocatedChilren.erase( myLeftBottomChild );
1313 myLeftBottomChild->setBrothers( notLocatedChilren );
1314 if ( !notLocatedChilren.empty() )
1315 return error(ERR_LI("Error in locateChildren()"));
1320 //================================================================================
1322 * \brief Fill myGrid with nodes of patches
1324 //================================================================================
1326 bool _QuadFaceGrid::loadCompositeGrid(SMESH_Mesh& mesh)
1328 // Find out mutual location of children: find their right and up brothers
1329 if ( !locateChildren() )
1332 // Load nodes according to mutual location of children
1335 myIndexer._xSize = 1 + myLeftBottomChild->GetNbHoriSegments(mesh, /*withBrothers=*/true);
1336 myIndexer._ySize = 1 + myLeftBottomChild->GetNbVertSegments(mesh, /*withBrothers=*/true);
1338 myGrid.resize( myIndexer.size() );
1340 int fromX = myReverse ? myIndexer._xSize : 0;
1341 if (!myLeftBottomChild->fillGrid( mesh, myGrid, myIndexer, fromX, 0 ))
1342 return error( myLeftBottomChild->GetError() );
1349 //================================================================================
1351 * \brief Find right an upper brothers among notLocatedBrothers
1353 //================================================================================
1355 void _QuadFaceGrid::setBrothers( set< _QuadFaceGrid* >& notLocatedBrothers )
1357 if ( !notLocatedBrothers.empty() )
1359 // find right brother
1360 TopoDS_Vertex rightVertex = GetSide( Q_BOTTOM ).LastVertex();
1361 DUMP_VERT("1 right bottom Vertex: ",rightVertex );
1362 set< _QuadFaceGrid* >::iterator brIt, brEnd = notLocatedBrothers.end();
1363 for ( brIt = notLocatedBrothers.begin(); brIt != brEnd; ++brIt )
1365 _QuadFaceGrid* brother = *brIt;
1366 TopoDS_Vertex brotherLeftVertex = brother->GetSide( Q_BOTTOM ).FirstVertex();
1367 DUMP_VERT( "brother left bottom: ", brotherLeftVertex );
1368 if ( rightVertex.IsSame( brotherLeftVertex )) {
1369 myRightBrother = brother;
1370 notLocatedBrothers.erase( brIt );
1374 // find upper brother
1375 TopoDS_Vertex upVertex = GetSide( Q_LEFT ).FirstVertex();
1376 DUMP_VERT("1 left up Vertex: ",upVertex);
1377 brIt = notLocatedBrothers.begin(), brEnd = notLocatedBrothers.end();
1378 for ( ; brIt != brEnd; ++brIt )
1380 _QuadFaceGrid* brother = *brIt;
1381 TopoDS_Vertex brotherLeftVertex = brother->GetSide( Q_BOTTOM ).FirstVertex();
1382 DUMP_VERT("brother left bottom: ", brotherLeftVertex);
1383 if ( upVertex.IsSame( brotherLeftVertex )) {
1384 myUpBrother = brother;
1385 notLocatedBrothers.erase( myUpBrother );
1390 if ( myRightBrother )
1391 myRightBrother->setBrothers( notLocatedBrothers );
1393 myUpBrother->setBrothers( notLocatedBrothers );
1397 //================================================================================
1399 * \brief Store nodes of a simple face into grid starting from (x,y) position
1401 //================================================================================
1403 bool _QuadFaceGrid::fillGrid(SMESH_Mesh& theMesh,
1404 vector<const SMDS_MeshNode*> & theGrid,
1405 const _Indexer& theIndexer,
1409 if ( myGrid.empty() && !LoadGrid( theMesh ))
1412 // store my own grid in the global grid
1414 int fromX = myReverse ? theX - myIndexer._xSize: theX;
1416 for ( int i = 0, x = fromX; i < myIndexer._xSize; ++i, ++x )
1417 for ( int j = 0, y = theY; j < myIndexer._ySize; ++j, ++y )
1418 theGrid[ theIndexer( x, y )] = myGrid[ myIndexer( i, j )];
1420 // store grids of my right and upper brothers
1422 if ( myRightBrother )
1427 fromX += myIndexer._xSize - 1;
1428 if ( !myRightBrother->fillGrid( theMesh, theGrid, theIndexer, fromX, theY ))
1429 return error( myRightBrother->GetError() );
1433 if ( !myUpBrother->fillGrid( theMesh, theGrid, theIndexer,
1434 theX, theY + myIndexer._ySize - 1))
1435 return error( myUpBrother->GetError() );
1440 //================================================================================
1442 * \brief Return number of segments on the hirizontal sides
1444 //================================================================================
1446 int _QuadFaceGrid::GetNbHoriSegments(SMESH_Mesh& mesh, bool withBrothers) const
1449 if ( myLeftBottomChild )
1451 nbSegs += myLeftBottomChild->GetNbHoriSegments( mesh, true );
1455 nbSegs = mySides.GetSide( Q_BOTTOM )->GetNbSegments(mesh);
1456 if ( withBrothers && myRightBrother )
1457 nbSegs += myRightBrother->GetNbHoriSegments( mesh, withBrothers );
1462 //================================================================================
1464 * \brief Return number of segments on the vertical sides
1466 //================================================================================
1468 int _QuadFaceGrid::GetNbVertSegments(SMESH_Mesh& mesh, bool withBrothers) const
1471 if ( myLeftBottomChild )
1473 nbSegs += myLeftBottomChild->GetNbVertSegments( mesh, true );
1477 nbSegs = mySides.GetSide( Q_LEFT )->GetNbSegments(mesh);
1478 if ( withBrothers && myUpBrother )
1479 nbSegs += myUpBrother->GetNbVertSegments( mesh, withBrothers );
1484 //================================================================================
1486 * \brief Return edge on the hirizontal bottom sides
1488 //================================================================================
1490 int _QuadFaceGrid::GetHoriEdges(vector<TopoDS_Edge> & edges) const
1492 if ( myLeftBottomChild )
1494 return myLeftBottomChild->GetHoriEdges( edges );
1498 const _FaceSide* bottom = mySides.GetSide( Q_BOTTOM );
1501 TopoDS_Edge e = bottom->Edge( i++ );
1505 edges.push_back( e );
1507 if ( myRightBrother )
1508 myRightBrother->GetHoriEdges( edges );
1510 return edges.size();
1513 //================================================================================
1515 * \brief Return a node by its position
1517 //================================================================================
1519 const SMDS_MeshNode* _QuadFaceGrid::GetNode(int iHori, int iVert) const
1521 return myGrid[ myIndexer( iHori, iVert )];
1524 //================================================================================
1526 * \brief Return node coordinates by its position
1528 //================================================================================
1530 gp_XYZ _QuadFaceGrid::GetXYZ(int iHori, int iVert) const
1532 SMESH_TNodeXYZ xyz = myGrid[ myIndexer( iHori, iVert )];
1536 //================================================================================
1538 * \brief Return normal to the face at vertex v
1540 //================================================================================
1542 bool _QuadFaceGrid::GetNormal( const TopoDS_Vertex& v, gp_Vec& n ) const
1544 if ( myChildren.empty() )
1546 if ( mySides.Contain( v )) {
1548 gp_Pnt2d uv = BRep_Tool::Parameters( v, myFace );
1549 BRepAdaptor_Surface surface( myFace );
1550 gp_Pnt p; gp_Vec d1u, d1v;
1551 surface.D1( uv.X(), uv.Y(), p, d1u, d1v );
1552 n = d1u.Crossed( d1v );
1555 catch (Standard_Failure) {
1562 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1563 for ( ; child != childEnd; ++child )
1564 if ( child->GetNormal( v, n ))
1570 //================================================================================
1572 * \brief Dumps coordinates of grid nodes
1574 //================================================================================
1576 void _QuadFaceGrid::DumpGrid() const
1579 const char* names[] = { "B_BOTTOM", "B_RIGHT", "B_TOP", "B_LEFT", "B_FRONT", "B_BACK" };
1580 cout << "****** Face " << names[ myID ] << endl;
1582 if ( myChildren.empty() || !myGrid.empty() )
1584 cout << "x size: " << myIndexer._xSize << "; y size: " << myIndexer._ySize << endl;
1585 for ( int y = 0; y < myIndexer._ySize; ++y ) {
1586 cout << "-- row " << y << endl;
1587 for ( int x = 0; x < myIndexer._xSize; ++x ) {
1588 const SMDS_MeshNode* n = myGrid[ myIndexer( x, y ) ];
1589 cout << x << " ( " << n->X() << ", " << n->Y() << ", " << n->Z() << " )" << endl;
1595 cout << "Nb children: " << myChildren.size() << endl;
1596 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1597 for ( int i=0; child != childEnd; ++child, ++i ) {
1598 cout << " *** SUBFACE " << i+1 << endl;
1599 ((_QuadFaceGrid&)(*child)).SetID( myID );
1606 //================================================================================
1608 * \brief Dump vertices
1610 //================================================================================
1612 void _QuadFaceGrid::DumpVertices() const
1615 cout << "****** Face ";
1616 const char* names[] = { "B_BOTTOM", "B_RIGHT", "B_TOP", "B_LEFT", "B_FRONT", "B_BACK" };
1617 if ( myID >= B_BOTTOM && myID < B_BACK )
1618 cout << names[ myID ] << endl;
1620 cout << "UNDEFINED" << endl;
1622 if ( myChildren.empty() )
1624 for ( int i = 0; i < 4; ++i )
1626 cout << " Side "; mySides.GetSide( i )->Dump();
1631 cout << "-- Nb children: " << myChildren.size() << endl;
1632 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1633 for ( int i=0; child != childEnd; ++child, ++i ) {
1634 cout << " *** SUBFACE " << i+1 << endl;
1635 ((_QuadFaceGrid&)(*child)).SetID( myID );
1636 child->DumpVertices();
1642 //=======================================================================
1643 //function : _FaceSide
1644 //purpose : copy constructor
1645 //=======================================================================
1647 _FaceSide::_FaceSide(const _FaceSide& other)
1649 myEdge = other.myEdge;
1650 myChildren = other.myChildren;
1651 myNbChildren = other.myNbChildren;
1652 myVertices.Assign( other.myVertices );
1656 //================================================================================
1658 * \brief Construct a face side of one edge
1660 //================================================================================
1662 _FaceSide::_FaceSide(const TopoDS_Edge& edge):
1663 myEdge( edge ), myNbChildren(0)
1665 if ( !edge.IsNull() )
1666 for ( TopExp_Explorer exp( edge, TopAbs_VERTEX ); exp.More(); exp.Next() )
1667 //myVertices.insert( ptr ( exp.Current() ));
1668 myVertices.Add( exp.Current() );
1671 //================================================================================
1673 * \brief Construct a face side of several edges
1675 //================================================================================
1677 _FaceSide::_FaceSide(const list<TopoDS_Edge>& edges):
1680 list<TopoDS_Edge>::const_iterator edge = edges.begin(), eEnd = edges.end();
1681 for ( ; edge != eEnd; ++edge ) {
1682 myChildren.push_back( _FaceSide( *edge ));
1684 myVertices.Add( myChildren.back().FirstVertex() );
1685 myVertices.Add( myChildren.back().LastVertex() );
1686 myChildren.back().SetID( Q_CHILD ); // not to splice them
1690 //=======================================================================
1691 //function : GetSide
1693 //=======================================================================
1695 _FaceSide* _FaceSide::GetSide(const int i)
1697 if ( i >= myNbChildren )
1700 list< _FaceSide >::iterator side = myChildren.begin();
1702 std::advance( side, i );
1706 //=======================================================================
1707 //function : GetSide
1709 //=======================================================================
1711 const _FaceSide* _FaceSide::GetSide(const int i) const
1713 return const_cast< _FaceSide* >(this)->GetSide(i);
1716 //=======================================================================
1717 //function : NbVertices
1718 //purpose : return nb of vertices in the side
1719 //=======================================================================
1721 int _FaceSide::NbVertices() const
1723 if ( myChildren.empty() )
1724 return myVertices.Extent();
1726 return myNbChildren + 1;
1729 //=======================================================================
1730 //function : NbCommonVertices
1731 //purpose : Returns number of my vertices common with the given ones
1732 //=======================================================================
1734 int _FaceSide::NbCommonVertices( const TopTools_MapOfShape& VV ) const
1737 TopTools_MapIteratorOfMapOfShape vIt ( myVertices );
1738 for ( ; vIt.More(); vIt.Next() )
1739 nbCommon += ( VV.Contains( vIt.Key() ));
1744 //=======================================================================
1745 //function : FirstVertex
1747 //=======================================================================
1749 TopoDS_Vertex _FaceSide::FirstVertex() const
1751 if ( myChildren.empty() )
1752 return TopExp::FirstVertex( myEdge, Standard_True );
1754 return myChildren.front().FirstVertex();
1757 //=======================================================================
1758 //function : LastVertex
1760 //=======================================================================
1762 TopoDS_Vertex _FaceSide::LastVertex() const
1764 if ( myChildren.empty() )
1765 return TopExp::LastVertex( myEdge, Standard_True );
1767 return myChildren.back().LastVertex();
1770 //=======================================================================
1773 //=======================================================================
1775 TopoDS_Vertex _FaceSide::Vertex(int i) const
1777 if ( myChildren.empty() )
1778 return i ? LastVertex() : FirstVertex();
1780 if ( i >= myNbChildren )
1781 return myChildren.back().LastVertex();
1783 return GetSide(i)->FirstVertex();
1786 //================================================================================
1788 * \brief Return i-the zero-based edge of the side
1790 //================================================================================
1792 TopoDS_Edge _FaceSide::Edge(int i) const
1794 if ( i == 0 && !myEdge.IsNull() )
1797 if ( const _FaceSide* iSide = GetSide( i ))
1798 return iSide->myEdge;
1800 return TopoDS_Edge();
1803 //=======================================================================
1804 //function : Contain
1806 //=======================================================================
1808 bool _FaceSide::Contain( const _FaceSide& side, int* which ) const
1810 if ( !which || myChildren.empty() )
1815 TopTools_MapIteratorOfMapOfShape vIt ( side.myVertices );
1816 for ( ; vIt.More(); vIt.Next() )
1817 nbCommon += ( myVertices.Contains( vIt.Key() ));
1818 return (nbCommon > 1);
1820 list< _FaceSide >::const_iterator mySide = myChildren.begin(), sideEnd = myChildren.end();
1821 for ( int i = 0; mySide != sideEnd; ++mySide, ++i ) {
1822 if ( mySide->Contain( side )) {
1830 //=======================================================================
1831 //function : Contain
1833 //=======================================================================
1835 bool _FaceSide::Contain( const TopoDS_Vertex& vertex ) const
1837 return myVertices.Contains( vertex );
1840 //=======================================================================
1841 //function : AppendSide
1843 //=======================================================================
1845 void _FaceSide::AppendSide( const _FaceSide& side )
1847 if ( !myEdge.IsNull() )
1849 myChildren.push_back( *this );
1853 myChildren.push_back( side );
1855 TopTools_MapIteratorOfMapOfShape vIt ( side.myVertices );
1856 for ( ; vIt.More(); vIt.Next() )
1857 myVertices.Add( vIt.Key() );
1860 myChildren.back().SetID( EQuadSides( myNbChildren-1 ));
1863 //=======================================================================
1864 //function : SetBottomSide
1866 //=======================================================================
1868 void _FaceSide::SetBottomSide( int i )
1870 if ( i > 0 && myID == Q_PARENT ) {
1871 list< _FaceSide >::iterator sideEnd, side = myChildren.begin();
1872 std::advance( side, i );
1873 myChildren.splice( myChildren.begin(), myChildren, side, myChildren.end() );
1875 side = myChildren.begin(), sideEnd = myChildren.end();
1876 for ( int i = 0; side != sideEnd; ++side, ++i ) {
1877 side->SetID( EQuadSides(i) );
1878 side->SetBottomSide(i);
1883 //=======================================================================
1884 //function : GetNbSegments
1886 //=======================================================================
1888 int _FaceSide::GetNbSegments(SMESH_Mesh& mesh) const
1891 if ( myChildren.empty() )
1893 nb = mesh.GetSubMesh(myEdge)->GetSubMeshDS()->NbElements();
1897 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1898 for ( ; side != sideEnd; ++side )
1899 nb += side->GetNbSegments(mesh);
1904 //=======================================================================
1905 //function : StoreNodes
1907 //=======================================================================
1909 bool _FaceSide::StoreNodes(SMESH_Mesh& mesh,
1910 vector<const SMDS_MeshNode*>& myGrid,
1913 list< TopoDS_Edge > edges;
1914 if ( myChildren.empty() )
1916 edges.push_back( myEdge );
1920 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1921 for ( ; side != sideEnd; ++side )
1923 edges.push_front( side->myEdge );
1925 edges.push_back ( side->myEdge );
1928 list< TopoDS_Edge >::iterator edge = edges.begin(), eEnd = edges.end();
1929 for ( ; edge != eEnd; ++edge )
1931 map< double, const SMDS_MeshNode* > nodes;
1932 bool ok = SMESH_Algo::GetSortedNodesOnEdge( mesh.GetMeshDS(),
1934 /*ignoreMediumNodes=*/true,
1936 if ( !ok ) return false;
1938 bool forward = ( edge->Orientation() == TopAbs_FORWARD );
1939 if ( reverse ) forward = !forward;
1942 map< double, const SMDS_MeshNode* >::iterator u_node, nEnd = nodes.end();
1943 for ( u_node = nodes.begin(); u_node != nEnd; ++u_node )
1944 myGrid[ nbNodes++ ] = u_node->second;
1948 map< double, const SMDS_MeshNode* >::reverse_iterator u_node, nEnd = nodes.rend();
1949 for ( u_node = nodes.rbegin(); u_node != nEnd; ++u_node )
1950 myGrid[ nbNodes++ ] = u_node->second;
1952 nbNodes--; // node on vertex present in two adjacent edges
1957 //=======================================================================
1959 //purpose : dump end vertices
1960 //=======================================================================
1962 void _FaceSide::Dump() const
1964 if ( myChildren.empty() )
1966 const char* sideNames[] = { "Q_BOTTOM", "Q_RIGHT", "Q_TOP", "Q_LEFT", "Q_CHILD", "Q_PARENT" };
1967 if ( myID >= Q_BOTTOM && myID < Q_PARENT )
1968 cout << sideNames[ myID ] << endl;
1970 cout << "<UNDEFINED ID>" << endl;
1971 TopoDS_Vertex f = FirstVertex();
1972 TopoDS_Vertex l = LastVertex();
1973 gp_Pnt pf = BRep_Tool::Pnt(f);
1974 gp_Pnt pl = BRep_Tool::Pnt(l);
1975 cout << "\t ( "<< ptr( f ) << " - " << ptr( l )<< " )"
1976 << "\t ( "<< pf.X()<<", "<<pf.Y()<<", "<<pf.Z()<<" ) - "
1977 << " ( "<< pl.X()<<", "<<pl.Y()<<", "<<pl.Z()<<" )"<<endl;
1981 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1982 for ( ; side != sideEnd; ++side ) {