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>
65 // #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; }
69 #define DUMP_VERT(msg,v)
72 //================================================================================
73 // text for message about an internal error
74 #define ERR_LI(txt) SMESH_Comment(txt) << ":" << __LINE__
76 // order corresponds to right order of edges in CASCADE face
77 enum EQuadSides{ Q_BOTTOM=0, Q_RIGHT, Q_TOP, Q_LEFT, Q_CHILD, Q_PARENT };
79 enum EBoxSides{ B_BOTTOM=0, B_RIGHT, B_TOP, B_LEFT, B_FRONT, B_BACK, B_UNDEFINED };
81 //================================================================================
83 * \brief Convertor of a pair of integers to a sole index
88 _Indexer( int xSize, int ySize ): _xSize(xSize), _ySize(ySize) {}
89 int size() const { return _xSize * _ySize; }
90 int operator()(const int x, const int y) const { return y * _xSize + x; }
93 //================================================================================
95 * \brief Wrapper of a composite or an ordinary edge.
100 _FaceSide(const _FaceSide& other);
101 _FaceSide(const TopoDS_Edge& edge=TopoDS_Edge());
102 _FaceSide(const list<TopoDS_Edge>& edges);
103 _FaceSide* GetSide(const int i);
104 const _FaceSide* GetSide(const int i) const;
105 int size() const { return myChildren.size(); }
106 int NbVertices() const;
107 int NbCommonVertices( const TopTools_MapOfShape& VV ) const;
108 TopoDS_Vertex FirstVertex() const;
109 TopoDS_Vertex LastVertex() const;
110 TopoDS_Vertex Vertex(int i) const;
111 TopoDS_Edge Edge(int i) const;
112 bool Contain( const _FaceSide& side, int* which=0 ) const;
113 bool Contain( const TopoDS_Vertex& vertex ) const;
114 void AppendSide( const _FaceSide& side );
115 void SetBottomSide( int i );
116 int GetNbSegments(SMESH_Mesh& mesh) const;
117 bool StoreNodes(SMESH_Mesh& mesh, vector<const SMDS_MeshNode*>& myGrid, bool reverse );
118 void SetID(EQuadSides id) { myID = id; }
119 static inline const TopoDS_TShape* ptr(const TopoDS_Shape& theShape)
120 { return theShape.TShape().operator->(); }
127 list< _FaceSide > myChildren;
130 TopTools_MapOfShape myVertices;
132 EQuadSides myID; // debug
134 //================================================================================
136 * \brief Class corresponding to a meshed composite face of a box.
137 * Provides simplified access to it's sub-mesh data.
141 typedef list< _QuadFaceGrid > TChildren;
145 public: //** Methods to find and orient faces of 6 sides of the box **//
148 bool Init(const TopoDS_Face& f, SMESH_Mesh& mesh );
150 //!< try to unite self with other face
151 bool AddContinuousFace( const _QuadFaceGrid& f, const TopTools_MapOfShape& internalEdges );
153 //!< Try to set the side as bottom hirizontal side
154 bool SetBottomSide(const _FaceSide& side, int* sideIndex=0);
156 //!< Return face adjacent to zero-based i-th side of this face
157 _QuadFaceGrid* FindAdjacentForSide(int i, list<_QuadFaceGrid>& faces, EBoxSides id) const;
159 //!< Reverse edges in order to have the bottom edge going along axes of the unit box
160 void ReverseEdges(/*int e1, int e2*/);
162 bool IsComplex() const { return !myChildren.empty(); }
164 int NbChildren() const { return myChildren.size(); }
166 typedef SMDS_SetIterator< const _QuadFaceGrid&,
167 TChildren::const_iterator,
168 SMDS::SimpleAccessor<const _QuadFaceGrid&,TChildren::const_iterator>,
169 SMDS::PassAllValueFilter<_QuadFaceGrid> >
172 TChildIterator GetChildren() const
173 { return TChildIterator( myChildren.begin(), myChildren.end()); }
175 public: //** Loading and access to mesh **//
177 //!< Load nodes of a mesh
178 bool LoadGrid( SMESH_Mesh& mesh );
180 //!< Return number of segments on the hirizontal sides
181 int GetNbHoriSegments(SMESH_Mesh& mesh, bool withBrothers=false) const;
183 //!< Return number of segments on the vertical sides
184 int GetNbVertSegments(SMESH_Mesh& mesh, bool withBrothers=false) const;
186 //!< Return edge on the hirizontal bottom sides
187 int GetHoriEdges(vector<TopoDS_Edge> & edges) const;
189 //!< Return a node by its position
190 const SMDS_MeshNode* GetNode(int iHori, int iVert) const;
192 //!< Return node coordinates by its position
193 gp_XYZ GetXYZ(int iHori, int iVert) const;
195 public: //** Access to member fields **//
197 //!< Return i-th face side (0<i<4)
198 const _FaceSide& GetSide(int i) const;
200 //!< Return it's face, NULL if it is composite
201 TopoDS_Face GetFace() const { return myFace; }
203 //!< Return normal to the face at vertex v
204 bool GetNormal( const TopoDS_Vertex& v, gp_Vec& n ) const;
206 SMESH_ComputeErrorPtr GetError() const { return myError; }
208 void SetID(EBoxSides id) { myID = id; }
210 void DumpGrid() const;
212 void DumpVertices() const;
216 bool error(const std::string& text, int code = COMPERR_ALGO_FAILED)
217 { myError = SMESH_ComputeError::New( code, text ); return false; }
219 bool error(const SMESH_ComputeErrorPtr& err)
220 { myError = err; return ( !myError || myError->IsOK() ); }
222 bool loadCompositeGrid(SMESH_Mesh& mesh);
224 bool fillGrid(SMESH_Mesh& theMesh,
225 vector<const SMDS_MeshNode*> & theGrid,
226 const _Indexer& theIndexer,
230 bool locateChildren();
232 void setBrothers( set< _QuadFaceGrid* >& notLocatedBrothers );
238 TChildren myChildren;
240 _QuadFaceGrid* myLeftBottomChild;
241 _QuadFaceGrid* myRightBrother;
242 _QuadFaceGrid* myUpBrother;
245 vector<const SMDS_MeshNode*> myGrid;
247 SMESH_ComputeErrorPtr myError;
249 EBoxSides myID; // debug
252 //================================================================================
256 //================================================================================
258 StdMeshers_CompositeHexa_3D::StdMeshers_CompositeHexa_3D(int hypId, int studyId, SMESH_Gen* gen)
259 :SMESH_3D_Algo(hypId, studyId, gen)
261 _name = "CompositeHexa_3D";
262 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID); // 1 bit /shape type
265 //================================================================================
267 * \brief always return true
269 //================================================================================
271 bool StdMeshers_CompositeHexa_3D::CheckHypothesis(SMESH_Mesh& aMesh,
272 const TopoDS_Shape& aShape,
273 Hypothesis_Status& aStatus)
282 //================================================================================
284 * \brief Checks structure of a quadrangular mesh at the common VERTEX of two EDGEs.
285 * Returns true if there are two quadrangles near the VERTEX.
287 //================================================================================
289 bool isContinuousMesh(TopoDS_Edge E1,
291 const TopoDS_Face& F,
292 const SMESH_Mesh& mesh)
294 if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
295 E1.Orientation( TopAbs_FORWARD );
296 if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
297 E2.Orientation( TopAbs_FORWARD );
300 if ( !TopExp::CommonVertex( E1, E2, V )) return false;
302 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
303 if ( !n ) return false;
305 SMESHDS_SubMesh* sm = mesh.GetSubMeshContaining( F )->GetSubMeshDS();
306 if ( !sm ) return false;
309 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
310 while ( fIt->more() )
312 const SMDS_MeshElement* f = fIt->next();
313 if ( !sm->Contains( f )) continue;
315 if ( f->NbCornerNodes() == 4 )
323 //================================================================================
325 * \brief Finds VERTEXes located at block corners
327 //================================================================================
329 void getBlockCorners( SMESH_Mesh& mesh,
330 const TopoDS_Shape& shape,
331 TopTools_MapOfShape& cornerVV)
333 set<int> faceIDs; // ids of FACEs in the shape
335 for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next() )
336 faceIDs.insert( mesh.GetMeshDS()->ShapeToIndex( exp.Current() ));
338 TopTools_MapOfShape checkedVV;
339 for ( exp.Init( shape, TopAbs_VERTEX ); exp.More(); exp.Next() )
341 TopoDS_Vertex V = TopoDS::Vertex( exp.Current() );
342 if ( !checkedVV.Add( V )) continue;
344 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
348 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
349 while ( fIt->more() )
351 const SMDS_MeshElement* f = fIt->next();
352 if ( !faceIDs.count( f->getshapeId() )) continue;
354 if ( f->NbCornerNodes() == 4 )
364 //================================================================================
366 * \brief Return EDGEs dividing one box side
368 //================================================================================
370 bool getInternalEdges( SMESH_Mesh& mesh,
371 const TopoDS_Shape& shape,
372 const TopTools_MapOfShape& cornerVV,
373 TopTools_MapOfShape& internEE)
375 TopTools_IndexedMapOfShape subEE, subFF;
376 TopExp::MapShapes( shape, TopAbs_EDGE, subEE );
377 TopExp::MapShapes( shape, TopAbs_FACE, subFF );
380 TopTools_MapOfShape subChecked/*, ridgeEE*/;
381 TopTools_MapIteratorOfMapOfShape vIt( cornerVV );
382 for ( ; vIt.More(); vIt.Next() )
384 TopoDS_Shape V0 = vIt.Key();
385 // walk from one corner VERTEX to another along ridge EDGEs
386 PShapeIteratorPtr riIt = SMESH_MesherHelper::GetAncestors( V0, mesh, TopAbs_EDGE );
387 while ( const TopoDS_Shape* riE = riIt->next() )
389 if ( !subEE.Contains( *riE ) || !subChecked.Add( *riE ))
391 TopoDS_Edge ridgeE = TopoDS::Edge( *riE );
392 while ( !ridgeE.IsNull() )
394 TopExp::Vertices( ridgeE, VV[0], VV[1] );
395 TopoDS_Shape V1 = VV[ V0.IsSame( VV[0] )];
396 if ( cornerVV.Contains( V1 ) )
397 break; // ridgeE reached a corner VERTEX
399 // detect internal EDGEs among those sharing V1. There can be 2, 3 or 4 EDGEs and
400 // number of internal EDGEs is N-2
401 TopoDS_Shape nextRidgeE;
402 PShapeIteratorPtr eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
403 while ( const TopoDS_Shape* E = eIt->next() )
405 if ( E->IsSame( ridgeE ) || !subEE.Contains( *E ) || !subChecked.Add( *E ))
407 // look for FACEs sharing both E and ridgeE
408 PShapeIteratorPtr fIt = SMESH_MesherHelper::GetAncestors( *E, mesh, TopAbs_FACE );
409 while ( const TopoDS_Shape* F = fIt->next() )
411 if ( !SMESH_MesherHelper::IsSubShape( ridgeE, *F ))
413 if ( isContinuousMesh( ridgeE, TopoDS::Edge( *E ), TopoDS::Face( *F ), mesh ))
424 // look for the next ridge EDGE ending at V1
425 if ( nextRidgeE.IsNull() )
427 eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
428 while ( const TopoDS_Shape* E = eIt->next() )
429 if ( !ridgeE.IsSame( *E ) && !internEE.Contains( *E ) && subEE.Contains( *E ))
435 ridgeE = TopoDS::Edge( nextRidgeE );
438 if ( ridgeE.IsNull() )
440 } // check EDGEs around the last VERTEX of ridgeE
441 } // loop on ridge EDGEs around a corner VERTEX
442 } // loop on on corner VERTEXes
445 } // getInternalEdges()
448 //================================================================================
450 * \brief Tries to find 6 sides of a box
452 //================================================================================
454 bool StdMeshers_CompositeHexa_3D::findBoxFaces( const TopoDS_Shape& shape,
455 list< _QuadFaceGrid >& boxFaces,
457 _QuadFaceGrid * & fBottom,
458 _QuadFaceGrid * & fTop,
459 _QuadFaceGrid * & fFront,
460 _QuadFaceGrid * & fBack,
461 _QuadFaceGrid * & fLeft,
462 _QuadFaceGrid * & fRight)
464 TopTools_MapOfShape cornerVertices;
465 getBlockCorners( mesh, shape, cornerVertices );
466 if ( cornerVertices.Extent() != 8 )
467 return error( COMPERR_BAD_INPUT_MESH, "Can't find 8 corners of a block by 2D mesh" );
468 TopTools_MapOfShape internalEdges;
469 if ( !getInternalEdges( mesh, shape, cornerVertices, internalEdges ))
470 return error( COMPERR_BAD_INPUT_MESH, "2D mesh is not suitable for i,j,k hexa meshing" );
472 list< _QuadFaceGrid >::iterator boxFace;
475 for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next(), ++nbFaces )
478 if ( !f.Init( TopoDS::Face( exp.Current() ), mesh ))
479 return error (COMPERR_BAD_SHAPE);
481 _QuadFaceGrid* prevContinuous = 0;
482 for ( boxFace = boxFaces.begin(); boxFace != boxFaces.end(); ++boxFace )
484 if ( prevContinuous )
486 if ( prevContinuous->AddContinuousFace( *boxFace, internalEdges ))
487 boxFace = --boxFaces.erase( boxFace );
489 else if ( boxFace->AddContinuousFace( f, internalEdges ))
491 prevContinuous = & (*boxFace);
494 if ( !prevContinuous )
495 boxFaces.push_back( f );
497 // Check what we have
498 if ( boxFaces.size() != 6 && nbFaces != 6)
501 SMESH_Comment("Can't find 6 sides of a box. Number of found sides - ")<<boxFaces.size());
503 if ( boxFaces.size() != 6 && nbFaces == 6 ) { // strange ordinary box with continuous faces
504 boxFaces.resize( 6 );
505 boxFace = boxFaces.begin();
506 for ( exp.Init( shape, TopAbs_FACE); exp.More(); exp.Next(), ++boxFace )
507 boxFace->Init( TopoDS::Face( exp.Current() ), mesh );
509 // ----------------------------------------
510 // Find out position of faces within a box
511 // ----------------------------------------
512 // start from a bottom face
513 fBottom = &boxFaces.front();
514 fBottom->SetID( B_BOTTOM );
515 // find vertical faces
516 fFront = fBottom->FindAdjacentForSide( Q_BOTTOM, boxFaces, B_FRONT );
517 fLeft = fBottom->FindAdjacentForSide( Q_RIGHT, boxFaces, B_LEFT );
518 fBack = fBottom->FindAdjacentForSide( Q_TOP, boxFaces, B_BACK );
519 fRight = fBottom->FindAdjacentForSide( Q_LEFT, boxFaces, B_RIGHT );
521 if ( !fFront || !fBack || !fLeft || !fRight )
522 return error(COMPERR_BAD_SHAPE);
525 for ( boxFace = ++boxFaces.begin(); boxFace != boxFaces.end() && !fTop; ++boxFace )
528 fTop->SetID( B_TOP );
529 if ( fTop==fFront || fTop==fLeft || fTop==fBack || fTop==fRight )
532 // set bottom of the top side
533 if ( !fTop->SetBottomSide( fFront->GetSide( Q_TOP ) )) {
534 if ( !fFront->IsComplex() )
535 return error( ERR_LI("Error in StdMeshers_CompositeHexa_3D::Compute()"));
537 _QuadFaceGrid::TChildIterator chIt = fFront->GetChildren();
538 while ( chIt.more() ) {
539 const _QuadFaceGrid& frontChild = chIt.next();
540 if ( fTop->SetBottomSide( frontChild.GetSide( Q_TOP )))
546 return error(COMPERR_BAD_SHAPE);
548 // orient bottom egde of faces along axes of the unit box
549 fBottom->ReverseEdges();
550 fBack ->ReverseEdges();
551 fLeft ->ReverseEdges();
556 //================================================================================
558 * \brief Computes hexahedral mesh on a box with composite sides
559 * \param aMesh - mesh to compute
560 * \param aShape - shape to mesh
561 * \retval bool - succes sign
563 //================================================================================
565 bool StdMeshers_CompositeHexa_3D::Compute(SMESH_Mesh& theMesh,
566 const TopoDS_Shape& theShape)
568 SMESH_MesherHelper helper( theMesh );
569 _quadraticMesh = helper.IsQuadraticSubMesh( theShape );
570 helper.SetElementsOnShape( true );
572 // -------------------------
573 // Try to find 6 side faces
574 // -------------------------
575 list< _QuadFaceGrid > boxFaceContainer;
576 _QuadFaceGrid *fBottom, *fTop, *fFront, *fBack, *fLeft, *fRight;
577 if ( ! findBoxFaces( theShape, boxFaceContainer, theMesh,
578 fBottom, fTop, fFront, fBack, fLeft, fRight))
581 // ------------------------------------------
582 // Fill columns of nodes with existing nodes
583 // ------------------------------------------
585 // let faces load their grids
586 if ( !fBottom->LoadGrid( theMesh )) return error( fBottom->GetError() );
587 if ( !fBack ->LoadGrid( theMesh )) return error( fBack ->GetError() );
588 if ( !fLeft ->LoadGrid( theMesh )) return error( fLeft ->GetError() );
589 if ( !fFront ->LoadGrid( theMesh )) return error( fFront ->GetError() );
590 if ( !fRight ->LoadGrid( theMesh )) return error( fRight ->GetError() );
591 if ( !fTop ->LoadGrid( theMesh )) return error( fTop ->GetError() );
593 int x, xSize = fBottom->GetNbHoriSegments(theMesh) + 1, X = xSize - 1;
594 int y, ySize = fBottom->GetNbVertSegments(theMesh) + 1, Y = ySize - 1;
595 int z, zSize = fFront ->GetNbVertSegments(theMesh) + 1, Z = zSize - 1;
596 _Indexer colIndex( xSize, ySize );
597 vector< vector< const SMDS_MeshNode* > > columns( colIndex.size() );
599 // fill node columns by front and back box sides
600 for ( x = 0; x < xSize; ++x ) {
601 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( x, 0 )];
602 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( x, Y )];
603 column0.resize( zSize );
604 column1.resize( zSize );
605 for ( z = 0; z < zSize; ++z ) {
606 column0[ z ] = fFront->GetNode( x, z );
607 column1[ z ] = fBack ->GetNode( x, z );
610 // fill node columns by left and right box sides
611 for ( y = 1; y < ySize-1; ++y ) {
612 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( 0, y )];
613 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( X, y )];
614 column0.resize( zSize );
615 column1.resize( zSize );
616 for ( z = 0; z < zSize; ++z ) {
617 column0[ z ] = fLeft ->GetNode( y, z );
618 column1[ z ] = fRight->GetNode( y, z );
621 // get nodes from top and bottom box sides
622 for ( x = 1; x < xSize-1; ++x ) {
623 for ( y = 1; y < ySize-1; ++y ) {
624 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
625 column.resize( zSize );
626 column.front() = fBottom->GetNode( x, y );
627 column.back() = fTop ->GetNode( x, y );
631 // ----------------------------
632 // Add internal nodes of a box
633 // ----------------------------
634 // projection points of internal nodes on box sub-shapes by which
635 // coordinates of internal nodes are computed
636 vector<gp_XYZ> pointsOnShapes( SMESH_Block::ID_Shell );
638 // projections on vertices are constant
639 pointsOnShapes[ SMESH_Block::ID_V000 ] = fBottom->GetXYZ( 0, 0 );
640 pointsOnShapes[ SMESH_Block::ID_V100 ] = fBottom->GetXYZ( X, 0 );
641 pointsOnShapes[ SMESH_Block::ID_V010 ] = fBottom->GetXYZ( 0, Y );
642 pointsOnShapes[ SMESH_Block::ID_V110 ] = fBottom->GetXYZ( X, Y );
643 pointsOnShapes[ SMESH_Block::ID_V001 ] = fTop->GetXYZ( 0, 0 );
644 pointsOnShapes[ SMESH_Block::ID_V101 ] = fTop->GetXYZ( X, 0 );
645 pointsOnShapes[ SMESH_Block::ID_V011 ] = fTop->GetXYZ( 0, Y );
646 pointsOnShapes[ SMESH_Block::ID_V111 ] = fTop->GetXYZ( X, Y );
648 for ( x = 1; x < xSize-1; ++x )
650 gp_XYZ params; // normalized parameters of internal node within a unit box
651 params.SetCoord( 1, x / double(X) );
652 for ( y = 1; y < ySize-1; ++y )
654 params.SetCoord( 2, y / double(Y) );
655 // column to fill during z loop
656 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
657 // points projections on horizontal edges
658 pointsOnShapes[ SMESH_Block::ID_Ex00 ] = fBottom->GetXYZ( x, 0 );
659 pointsOnShapes[ SMESH_Block::ID_Ex10 ] = fBottom->GetXYZ( x, Y );
660 pointsOnShapes[ SMESH_Block::ID_E0y0 ] = fBottom->GetXYZ( 0, y );
661 pointsOnShapes[ SMESH_Block::ID_E1y0 ] = fBottom->GetXYZ( X, y );
662 pointsOnShapes[ SMESH_Block::ID_Ex01 ] = fTop->GetXYZ( x, 0 );
663 pointsOnShapes[ SMESH_Block::ID_Ex11 ] = fTop->GetXYZ( x, Y );
664 pointsOnShapes[ SMESH_Block::ID_E0y1 ] = fTop->GetXYZ( 0, y );
665 pointsOnShapes[ SMESH_Block::ID_E1y1 ] = fTop->GetXYZ( X, y );
666 // points projections on horizontal faces
667 pointsOnShapes[ SMESH_Block::ID_Fxy0 ] = fBottom->GetXYZ( x, y );
668 pointsOnShapes[ SMESH_Block::ID_Fxy1 ] = fTop ->GetXYZ( x, y );
669 for ( z = 1; z < zSize-1; ++z ) // z loop
671 params.SetCoord( 3, z / double(Z) );
672 // point projections on vertical edges
673 pointsOnShapes[ SMESH_Block::ID_E00z ] = fFront->GetXYZ( 0, z );
674 pointsOnShapes[ SMESH_Block::ID_E10z ] = fFront->GetXYZ( X, z );
675 pointsOnShapes[ SMESH_Block::ID_E01z ] = fBack->GetXYZ( 0, z );
676 pointsOnShapes[ SMESH_Block::ID_E11z ] = fBack->GetXYZ( X, z );
677 // point projections on vertical faces
678 pointsOnShapes[ SMESH_Block::ID_Fx0z ] = fFront->GetXYZ( x, z );
679 pointsOnShapes[ SMESH_Block::ID_Fx1z ] = fBack ->GetXYZ( x, z );
680 pointsOnShapes[ SMESH_Block::ID_F0yz ] = fLeft ->GetXYZ( y, z );
681 pointsOnShapes[ SMESH_Block::ID_F1yz ] = fRight->GetXYZ( y, z );
683 // compute internal node coordinates
685 SMESH_Block::ShellPoint( params, pointsOnShapes, coords );
686 column[ z ] = helper.AddNode( coords.X(), coords.Y(), coords.Z() );
690 //cout << "----------------------------------------------------------------------"<<endl;
691 //for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id)
693 // gp_XYZ p = pointsOnShapes[ id ];
694 // SMESH_Block::DumpShapeID( id,cout)<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;
696 //cout << "Params: ( "<< params.X()<<", "<<params.Y()<<", "<<params.Z()<<" )"<<endl;
697 //cout << "coords: ( "<< coords.X()<<", "<<coords.Y()<<", "<<coords.Z()<<" )"<<endl;
702 // faces no more needed, free memory
703 boxFaceContainer.clear();
708 for ( x = 0; x < xSize-1; ++x ) {
709 for ( y = 0; y < ySize-1; ++y ) {
710 vector< const SMDS_MeshNode* >& col00 = columns[ colIndex( x, y )];
711 vector< const SMDS_MeshNode* >& col10 = columns[ colIndex( x+1, y )];
712 vector< const SMDS_MeshNode* >& col01 = columns[ colIndex( x, y+1 )];
713 vector< const SMDS_MeshNode* >& col11 = columns[ colIndex( x+1, y+1 )];
714 for ( z = 0; z < zSize-1; ++z )
716 // bottom face normal of a hexa mush point outside the volume
717 helper.AddVolume(col00[z], col01[z], col11[z], col10[z],
718 col00[z+1], col01[z+1], col11[z+1], col10[z+1]);
725 //================================================================================
729 //================================================================================
731 bool StdMeshers_CompositeHexa_3D::Evaluate(SMESH_Mesh& theMesh,
732 const TopoDS_Shape& theShape,
733 MapShapeNbElems& aResMap)
735 // -------------------------
736 // Try to find 6 side faces
737 // -------------------------
738 list< _QuadFaceGrid > boxFaceContainer;
739 _QuadFaceGrid *fBottom, *fTop, *fFront, *fBack, *fLeft, *fRight;
740 if ( ! findBoxFaces( theShape, boxFaceContainer, theMesh,
741 fBottom, fTop, fFront, fBack, fLeft, fRight))
744 // Find a less complex side
745 _QuadFaceGrid * lessComplexSide = & boxFaceContainer.front();
746 list< _QuadFaceGrid >::iterator face = boxFaceContainer.begin();
747 for ( ++face; face != boxFaceContainer.end() && lessComplexSide->IsComplex(); ++face )
748 if ( face->NbChildren() < lessComplexSide->NbChildren() )
749 lessComplexSide = & *face;
751 // Get an 1D size of lessComplexSide
753 vector<TopoDS_Edge> edges;
754 if ( !lessComplexSide->GetHoriEdges(edges) )
756 for ( size_t i = 0; i < edges.size(); ++i )
758 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( edges[i] )];
759 if ( !nbElems.empty() )
760 nbSeg1 += Max( nbElems[ SMDSEntity_Edge ], nbElems[ SMDSEntity_Quad_Edge ]);
763 // Get an 1D size of a box side ortogonal to lessComplexSide
765 _QuadFaceGrid* ortoSide =
766 lessComplexSide->FindAdjacentForSide( Q_LEFT, boxFaceContainer, B_UNDEFINED );
768 if ( !ortoSide || !ortoSide->GetHoriEdges(edges) ) return false;
769 for ( size_t i = 0; i < edges.size(); ++i )
771 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( edges[i] )];
772 if ( !nbElems.empty() )
773 nbSeg2 += Max( nbElems[ SMDSEntity_Edge ], nbElems[ SMDSEntity_Quad_Edge ]);
776 // Get an 2D size of a box side ortogonal to lessComplexSide
777 int nbFaces = 0, nbQuadFace = 0;
778 list< TopoDS_Face > sideFaces;
779 if ( ortoSide->IsComplex() )
780 for ( _QuadFaceGrid::TChildIterator child = ortoSide->GetChildren(); child.more(); )
781 sideFaces.push_back( child.next().GetFace() );
783 sideFaces.push_back( ortoSide->GetFace() );
785 list< TopoDS_Face >::iterator f = sideFaces.begin();
786 for ( ; f != sideFaces.end(); ++f )
788 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( *f )];
789 if ( !nbElems.empty() )
791 nbFaces = nbElems[ SMDSEntity_Quadrangle ];
792 nbQuadFace = nbElems[ SMDSEntity_Quad_Quadrangle ];
796 // Fill nb of elements
797 vector<int> aResVec(SMDSEntity_Last,0);
798 int nbSeg3 = ( nbFaces + nbQuadFace ) / nbSeg2;
799 aResVec[SMDSEntity_Node] = (nbSeg1-1) * (nbSeg2-1) * (nbSeg3-1);
800 aResVec[SMDSEntity_Hexa] = nbSeg1 * nbFaces;
801 aResVec[SMDSEntity_Quad_Hexa] = nbSeg1 * nbQuadFace;
803 aResMap.insert( make_pair( theMesh.GetSubMesh(theShape), aResVec ));
809 //================================================================================
811 * \brief constructor of non-initialized _QuadFaceGrid
813 //================================================================================
815 _QuadFaceGrid::_QuadFaceGrid():
816 myReverse(false), myRightBrother(0), myUpBrother(0), myIndexer(0,0), myID(B_UNDEFINED)
820 //================================================================================
822 * \brief Initialization
824 //================================================================================
826 bool _QuadFaceGrid::Init(const TopoDS_Face& f, SMESH_Mesh& mesh)
829 mySides = _FaceSide();
831 myLeftBottomChild = myRightBrother = myUpBrother = 0;
834 //if ( myFace.Orientation() != TopAbs_FORWARD )
837 list< TopoDS_Edge > edges;
838 list< int > nbEdgesInWire;
839 int nbWire = SMESH_Block::GetOrderedEdges (myFace, edges, nbEdgesInWire);
843 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
844 if ( nbEdgesInWire.front() == 4 ) // exactly 4 edges
846 for ( ; edgeIt != edges.end(); ++edgeIt )
847 mySides.AppendSide( _FaceSide( *edgeIt ));
849 else if ( nbEdgesInWire.front() > 4 ) { // more than 4 edges - try to unite some
850 list< TopoDS_Edge > sideEdges;
851 while ( !edges.empty()) {
853 sideEdges.splice( sideEdges.end(), edges, edges.begin());// edges.front()->sideEdges.back()
854 while ( !edges.empty() ) {
855 if ( SMESH_Algo::IsContinuous( sideEdges.back(), edges.front() )) {
856 sideEdges.splice( sideEdges.end(), edges, edges.begin());
858 else if ( SMESH_Algo::IsContinuous( sideEdges.front(), edges.back() )) {
859 sideEdges.splice( sideEdges.begin(), edges, --edges.end());
861 else if ( isContinuousMesh( sideEdges.back(), edges.front(), f, mesh )) {
862 sideEdges.splice( sideEdges.end(), edges, edges.begin());
864 else if ( isContinuousMesh( sideEdges.front(), edges.back(), f, mesh )) {
865 sideEdges.splice( sideEdges.begin(), edges, --edges.end());
871 mySides.AppendSide( _FaceSide( sideEdges ));
874 if (mySides.size() != 4)
878 mySides.GetSide( Q_BOTTOM )->SetID( Q_BOTTOM );
879 mySides.GetSide( Q_RIGHT )->SetID( Q_RIGHT );
880 mySides.GetSide( Q_TOP )->SetID( Q_TOP );
881 mySides.GetSide( Q_LEFT )->SetID( Q_LEFT );
887 //================================================================================
889 * \brief Try to unite self with other ordinary face
891 //================================================================================
893 bool _QuadFaceGrid::AddContinuousFace( const _QuadFaceGrid& other,
894 const TopTools_MapOfShape& internalEdges)
896 for ( int i = 0; i < 4; ++i )
898 const _FaceSide& otherSide = other.GetSide( i );
900 if ( mySides.Contain( otherSide, &iMyCommon ) )
902 if ( internalEdges.Contains( otherSide.Edge( 0 )))
904 DUMP_VERT("Cont 1", mySides.GetSide(iMyCommon)->FirstVertex());
905 DUMP_VERT("Cont 2", mySides.GetSide(iMyCommon)->LastVertex());
906 DUMP_VERT("Cont 3", otherSide.FirstVertex());
907 DUMP_VERT("Cont 4", otherSide.LastVertex());
908 if ( myChildren.empty() ) {
909 myChildren.push_back( *this );
913 // orient new children equally
914 int otherBottomIndex = ( 4 + i - iMyCommon + 2 ) % 4;
915 if ( other.IsComplex() )
916 for ( TChildIterator children = other.GetChildren(); children.more(); ) {
917 myChildren.push_back( children.next() );
918 myChildren.back().SetBottomSide( myChildren.back().GetSide( otherBottomIndex ));
921 myChildren.push_back( other );
922 myChildren.back().SetBottomSide( myChildren.back().GetSide( otherBottomIndex ));
925 myLeftBottomChild = 0;
927 // collect vertices in mySides
928 if ( other.IsComplex() )
929 for ( TChildIterator children = other.GetChildren(); children.more(); )
931 const _QuadFaceGrid& child = children.next();
932 for ( int i = 0; i < 4; ++i )
933 mySides.AppendSide( child.GetSide(i) );
936 for ( int i = 0; i < 4; ++i )
937 mySides.AppendSide( other.GetSide(i) );
946 //================================================================================
948 * \brief Try to set the side as bottom hirizontal side
950 //================================================================================
952 bool _QuadFaceGrid::SetBottomSide(const _FaceSide& bottom, int* sideIndex)
954 myLeftBottomChild = myRightBrother = myUpBrother = 0;
957 if ( myChildren.empty() )
959 if ( mySides.Contain( bottom, &myBottomIndex )) {
960 mySides.SetBottomSide( myBottomIndex );
962 *sideIndex = myBottomIndex;
968 TChildren::iterator childFace = myChildren.begin(), childEnd = myChildren.end();
969 for ( ; childFace != childEnd; ++childFace )
971 if ( childFace->SetBottomSide( bottom, &myBottomIndex ))
973 TChildren::iterator orientedCild = childFace;
974 for ( childFace = myChildren.begin(); childFace != childEnd; ++childFace ) {
975 if ( childFace != orientedCild )
976 childFace->SetBottomSide( childFace->GetSide( myBottomIndex ));
979 *sideIndex = myBottomIndex;
987 //================================================================================
989 * \brief Return face adjacent to i-th side of this face, (0<i<4)
991 //================================================================================
993 _QuadFaceGrid* _QuadFaceGrid::FindAdjacentForSide(int i,
994 list<_QuadFaceGrid>& faces,
997 const _FaceSide & iSide = GetSide( i );
998 list< _QuadFaceGrid >::iterator boxFace = faces.begin();
999 for ( ; boxFace != faces.end(); ++boxFace )
1001 _QuadFaceGrid* f = & (*boxFace);
1002 if ( f != this && f->SetBottomSide( iSide ))
1003 return f->SetID( id ), f;
1005 return (_QuadFaceGrid*) 0;
1008 //================================================================================
1010 * \brief Return i-th side
1012 //================================================================================
1014 const _FaceSide& _QuadFaceGrid::GetSide(int i) const
1016 if ( myChildren.empty() )
1017 return *mySides.GetSide(i);
1019 _QuadFaceGrid* me = const_cast<_QuadFaceGrid*>(this);
1020 if ( !me->locateChildren() || !myLeftBottomChild )
1021 return *mySides.GetSide(i);
1023 const _QuadFaceGrid* child = myLeftBottomChild;
1029 while ( child->myRightBrother )
1030 child = child->myRightBrother;
1033 while ( child->myUpBrother )
1034 child = child->myUpBrother;
1038 return child->GetSide( i );
1041 //================================================================================
1043 * \brief Reverse edges in order to have them oriented along axes of the unit box
1045 //================================================================================
1047 void _QuadFaceGrid::ReverseEdges(/*int e1, int e2*/)
1049 myReverse = !myReverse;
1052 // if ( !myFace.IsNull() )
1053 // TopAbs::Print(myFace.Orientation(), cout);
1056 if ( myChildren.empty() )
1058 // mySides.GetSide( e1 )->Reverse();
1059 // mySides.GetSide( e2 )->Reverse();
1065 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1066 for ( ; child != childEnd; ++child )
1067 child->ReverseEdges( /*e1, e2*/ );
1071 //================================================================================
1073 * \brief Load nodes of a mesh
1075 //================================================================================
1077 bool _QuadFaceGrid::LoadGrid( SMESH_Mesh& mesh )
1079 if ( !myChildren.empty() )
1081 // Let child faces load their grids
1082 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1083 for ( ; child != childEnd; ++child ) {
1084 child->SetID( myID );
1085 if ( !child->LoadGrid( mesh ) )
1086 return error( child->GetError() );
1088 // Fill myGrid with nodes of patches
1089 return loadCompositeGrid( mesh );
1092 // ---------------------------------------
1093 // Fill myGrid with nodes bound to myFace
1094 // ---------------------------------------
1096 if ( !myGrid.empty() )
1099 SMESHDS_SubMesh* faceSubMesh = mesh.GetSubMesh( myFace )->GetSubMeshDS();
1100 // check that all faces are quadrangular
1101 SMDS_ElemIteratorPtr fIt = faceSubMesh->GetElements();
1102 while ( fIt->more() )
1103 if ( fIt->next()->NbNodes() % 4 > 0 )
1104 return error("Non-quadrangular mesh faces are not allowed on sides of a composite block");
1106 myIndexer._xSize = 1 + mySides.GetSide( Q_BOTTOM )->GetNbSegments( mesh );
1107 myIndexer._ySize = 1 + mySides.GetSide( Q_LEFT )->GetNbSegments( mesh );
1109 myGrid.resize( myIndexer.size() );
1111 // strore nodes bound to the bottom edge
1112 mySides.GetSide( Q_BOTTOM )->StoreNodes( mesh, myGrid, myReverse );
1114 // store the rest nodes row by row
1116 TIDSortedElemSet emptySet, avoidSet;
1117 const SMDS_MeshElement* firstQuad = 0; // most left face above the last row of found nodes
1119 int nbFoundNodes = myIndexer._xSize;
1120 while ( nbFoundNodes != (int) myGrid.size() )
1122 // first and last nodes of the last filled row of nodes
1123 const SMDS_MeshNode* n1down = myGrid[ nbFoundNodes - myIndexer._xSize ];
1124 const SMDS_MeshNode* n2down = myGrid[ nbFoundNodes - myIndexer._xSize + 1];
1125 const SMDS_MeshNode* n1downLast = myGrid[ nbFoundNodes-1 ];
1127 // find the first face above the row by the first two left nodes
1135 firstQuad = SMESH_MeshAlgos::FindFaceInSet( n1down, n2down, emptySet, avoidSet);
1136 while ( firstQuad && !faceSubMesh->Contains( firstQuad )) {
1137 avoidSet.insert( firstQuad );
1138 firstQuad = SMESH_MeshAlgos::FindFaceInSet( n1down, n2down, emptySet, avoidSet);
1140 if ( !firstQuad || !faceSubMesh->Contains( firstQuad ))
1141 return error(ERR_LI("Error in _QuadFaceGrid::LoadGrid()"));
1143 // find the node of quad bound to the left geom edge
1144 int i2down = firstQuad->GetNodeIndex( n2down );
1145 const SMDS_MeshNode* n1up = firstQuad->GetNode(( i2down+2 ) % 4 );
1146 myGrid[ nbFoundNodes++ ] = n1up;
1147 // the 4-the node of the first quad
1148 int i1down = firstQuad->GetNodeIndex( n1down );
1149 const SMDS_MeshNode* n2up = firstQuad->GetNode(( i1down+2 ) % 4 );
1150 myGrid[ nbFoundNodes++ ] = n2up;
1154 const SMDS_MeshElement* quad = firstQuad;
1156 // find the rest nodes by remaining faces above the row
1164 while ( n1down != n1downLast )
1167 avoidSet.clear(); avoidSet.insert( quad );
1168 quad = SMESH_MeshAlgos::FindFaceInSet( n1down, n1up, emptySet, avoidSet );
1169 if ( !quad || quad->NbNodes() % 4 > 0)
1170 return error(ERR_LI("Error in _QuadFaceGrid::LoadGrid()"));
1173 if ( quad->GetNode( i1down ) != n1down ) // check already found index
1174 i1down = quad->GetNodeIndex( n1down );
1175 n2up = quad->GetNode(( i1down+2 ) % 4 );
1176 myGrid[ nbFoundNodes++ ] = n2up;
1178 n1down = myGrid[ nbFoundNodes - myIndexer._xSize - 1 ];
1181 avoidSet.clear(); avoidSet.insert( firstQuad );
1183 DumpGrid(); // debug
1188 //================================================================================
1190 * \brief Find out mutual location of children: find their right and up brothers
1192 //================================================================================
1194 bool _QuadFaceGrid::locateChildren()
1196 if ( myLeftBottomChild )
1199 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1201 // find a child sharing it's first bottom vertex with no other brother
1202 myLeftBottomChild = 0;
1203 for ( ; !myLeftBottomChild && child != childEnd; ++child )
1205 TopoDS_Vertex leftVertex = child->GetSide( Q_BOTTOM ).FirstVertex();
1206 bool sharedVertex = false;
1207 TChildren::iterator otherChild = myChildren.begin();
1208 for ( ; otherChild != childEnd && !sharedVertex; ++otherChild )
1209 if ( otherChild != child )
1210 sharedVertex = otherChild->mySides.Contain( leftVertex );
1211 if ( !sharedVertex ) {
1212 myLeftBottomChild = & (*child);
1213 DUMP_VERT("0 left bottom Vertex: ",leftVertex );
1216 if (!myLeftBottomChild)
1217 return error(ERR_LI("Error in locateChildren()"));
1219 set< _QuadFaceGrid* > notLocatedChilren;
1220 for (child = myChildren.begin() ; child != childEnd; ++child )
1221 notLocatedChilren.insert( & (*child));
1223 // connect myLeftBottomChild to it's right and upper brothers
1224 notLocatedChilren.erase( myLeftBottomChild );
1225 myLeftBottomChild->setBrothers( notLocatedChilren );
1226 if ( !notLocatedChilren.empty() )
1227 return error(ERR_LI("Error in locateChildren()"));
1232 //================================================================================
1234 * \brief Fill myGrid with nodes of patches
1236 //================================================================================
1238 bool _QuadFaceGrid::loadCompositeGrid(SMESH_Mesh& mesh)
1240 // Find out mutual location of children: find their right and up brothers
1241 if ( !locateChildren() )
1244 // Load nodes according to mutual location of children
1247 myIndexer._xSize = 1 + myLeftBottomChild->GetNbHoriSegments(mesh, /*withBrothers=*/true);
1248 myIndexer._ySize = 1 + myLeftBottomChild->GetNbVertSegments(mesh, /*withBrothers=*/true);
1250 myGrid.resize( myIndexer.size() );
1252 int fromX = myReverse ? myIndexer._xSize : 0;
1253 if (!myLeftBottomChild->fillGrid( mesh, myGrid, myIndexer, fromX, 0 ))
1254 return error( myLeftBottomChild->GetError() );
1261 //================================================================================
1263 * \brief Find right an upper brothers among notLocatedBrothers
1265 //================================================================================
1267 void _QuadFaceGrid::setBrothers( set< _QuadFaceGrid* >& notLocatedBrothers )
1269 if ( !notLocatedBrothers.empty() )
1271 // find right brother
1272 TopoDS_Vertex rightVertex = GetSide( Q_BOTTOM ).LastVertex();
1273 DUMP_VERT("1 right bottom Vertex: ",rightVertex );
1274 set< _QuadFaceGrid* >::iterator brIt, brEnd = notLocatedBrothers.end();
1275 for ( brIt = notLocatedBrothers.begin(); brIt != brEnd; ++brIt )
1277 _QuadFaceGrid* brother = *brIt;
1278 TopoDS_Vertex brotherLeftVertex = brother->GetSide( Q_BOTTOM ).FirstVertex();
1279 DUMP_VERT( "brother left bottom: ", brotherLeftVertex );
1280 if ( rightVertex.IsSame( brotherLeftVertex )) {
1281 myRightBrother = brother;
1282 notLocatedBrothers.erase( brIt );
1286 // find upper brother
1287 TopoDS_Vertex upVertex = GetSide( Q_LEFT ).FirstVertex();
1288 DUMP_VERT("1 left up Vertex: ",upVertex);
1289 brIt = notLocatedBrothers.begin(), brEnd = notLocatedBrothers.end();
1290 for ( ; brIt != brEnd; ++brIt )
1292 _QuadFaceGrid* brother = *brIt;
1293 TopoDS_Vertex brotherLeftVertex = brother->GetSide( Q_BOTTOM ).FirstVertex();
1294 DUMP_VERT("brother left bottom: ", brotherLeftVertex);
1295 if ( upVertex.IsSame( brotherLeftVertex )) {
1296 myUpBrother = brother;
1297 notLocatedBrothers.erase( myUpBrother );
1302 if ( myRightBrother )
1303 myRightBrother->setBrothers( notLocatedBrothers );
1305 myUpBrother->setBrothers( notLocatedBrothers );
1309 //================================================================================
1311 * \brief Store nodes of a simple face into grid starting from (x,y) position
1313 //================================================================================
1315 bool _QuadFaceGrid::fillGrid(SMESH_Mesh& theMesh,
1316 vector<const SMDS_MeshNode*> & theGrid,
1317 const _Indexer& theIndexer,
1321 if ( myGrid.empty() && !LoadGrid( theMesh ))
1324 // store my own grid in the global grid
1326 int fromX = myReverse ? theX - myIndexer._xSize: theX;
1328 for ( int i = 0, x = fromX; i < myIndexer._xSize; ++i, ++x )
1329 for ( int j = 0, y = theY; j < myIndexer._ySize; ++j, ++y )
1330 theGrid[ theIndexer( x, y )] = myGrid[ myIndexer( i, j )];
1332 // store grids of my right and upper brothers
1334 if ( myRightBrother )
1339 fromX += myIndexer._xSize - 1;
1340 if ( !myRightBrother->fillGrid( theMesh, theGrid, theIndexer, fromX, theY ))
1341 return error( myRightBrother->GetError() );
1345 if ( !myUpBrother->fillGrid( theMesh, theGrid, theIndexer,
1346 theX, theY + myIndexer._ySize - 1))
1347 return error( myUpBrother->GetError() );
1352 //================================================================================
1354 * \brief Return number of segments on the hirizontal sides
1356 //================================================================================
1358 int _QuadFaceGrid::GetNbHoriSegments(SMESH_Mesh& mesh, bool withBrothers) const
1361 if ( myLeftBottomChild )
1363 nbSegs += myLeftBottomChild->GetNbHoriSegments( mesh, true );
1367 nbSegs = mySides.GetSide( Q_BOTTOM )->GetNbSegments(mesh);
1368 if ( withBrothers && myRightBrother )
1369 nbSegs += myRightBrother->GetNbHoriSegments( mesh, withBrothers );
1374 //================================================================================
1376 * \brief Return number of segments on the vertical sides
1378 //================================================================================
1380 int _QuadFaceGrid::GetNbVertSegments(SMESH_Mesh& mesh, bool withBrothers) const
1383 if ( myLeftBottomChild )
1385 nbSegs += myLeftBottomChild->GetNbVertSegments( mesh, true );
1389 nbSegs = mySides.GetSide( Q_LEFT )->GetNbSegments(mesh);
1390 if ( withBrothers && myUpBrother )
1391 nbSegs += myUpBrother->GetNbVertSegments( mesh, withBrothers );
1396 //================================================================================
1398 * \brief Return edge on the hirizontal bottom sides
1400 //================================================================================
1402 int _QuadFaceGrid::GetHoriEdges(vector<TopoDS_Edge> & edges) const
1404 if ( myLeftBottomChild )
1406 return myLeftBottomChild->GetHoriEdges( edges );
1410 const _FaceSide* bottom = mySides.GetSide( Q_BOTTOM );
1413 TopoDS_Edge e = bottom->Edge( i++ );
1417 edges.push_back( e );
1419 if ( myRightBrother )
1420 myRightBrother->GetHoriEdges( edges );
1422 return edges.size();
1425 //================================================================================
1427 * \brief Return a node by its position
1429 //================================================================================
1431 const SMDS_MeshNode* _QuadFaceGrid::GetNode(int iHori, int iVert) const
1433 return myGrid[ myIndexer( iHori, iVert )];
1436 //================================================================================
1438 * \brief Return node coordinates by its position
1440 //================================================================================
1442 gp_XYZ _QuadFaceGrid::GetXYZ(int iHori, int iVert) const
1444 const SMDS_MeshNode* n = myGrid[ myIndexer( iHori, iVert )];
1445 return gp_XYZ( n->X(), n->Y(), n->Z() );
1448 //================================================================================
1450 * \brief Return normal to the face at vertex v
1452 //================================================================================
1454 bool _QuadFaceGrid::GetNormal( const TopoDS_Vertex& v, gp_Vec& n ) const
1456 if ( myChildren.empty() )
1458 if ( mySides.Contain( v )) {
1460 gp_Pnt2d uv = BRep_Tool::Parameters( v, myFace );
1461 BRepAdaptor_Surface surface( myFace );
1462 gp_Pnt p; gp_Vec d1u, d1v;
1463 surface.D1( uv.X(), uv.Y(), p, d1u, d1v );
1464 n = d1u.Crossed( d1v );
1467 catch (Standard_Failure) {
1474 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1475 for ( ; child != childEnd; ++child )
1476 if ( child->GetNormal( v, n ))
1482 //================================================================================
1484 * \brief Dumps coordinates of grid nodes
1486 //================================================================================
1488 void _QuadFaceGrid::DumpGrid() const
1491 const char* names[] = { "B_BOTTOM", "B_RIGHT", "B_TOP", "B_LEFT", "B_FRONT", "B_BACK" };
1492 cout << "****** Face " << names[ myID ] << endl;
1494 if ( myChildren.empty() || !myGrid.empty() )
1496 cout << "x size: " << myIndexer._xSize << "; y size: " << myIndexer._ySize << endl;
1497 for ( int y = 0; y < myIndexer._ySize; ++y ) {
1498 cout << "-- row " << y << endl;
1499 for ( int x = 0; x < myIndexer._xSize; ++x ) {
1500 const SMDS_MeshNode* n = myGrid[ myIndexer( x, y ) ];
1501 cout << x << " ( " << n->X() << ", " << n->Y() << ", " << n->Z() << " )" << endl;
1507 cout << "Nb children: " << myChildren.size() << endl;
1508 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1509 for ( int i=0; child != childEnd; ++child, ++i ) {
1510 cout << " *** SUBFACE " << i+1 << endl;
1511 ((_QuadFaceGrid&)(*child)).SetID( myID );
1518 //================================================================================
1520 * \brief Dump vertices
1522 //================================================================================
1524 void _QuadFaceGrid::DumpVertices() const
1527 cout << "****** Face ";
1528 const char* names[] = { "B_BOTTOM", "B_RIGHT", "B_TOP", "B_LEFT", "B_FRONT", "B_BACK" };
1529 if ( myID >= B_BOTTOM && myID < B_BACK )
1530 cout << names[ myID ] << endl;
1532 cout << "UNDEFINED" << endl;
1534 if ( myChildren.empty() )
1536 for ( int i = 0; i < 4; ++i )
1538 cout << " Side "; mySides.GetSide( i )->Dump();
1543 cout << "-- Nb children: " << myChildren.size() << endl;
1544 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1545 for ( int i=0; child != childEnd; ++child, ++i ) {
1546 cout << " *** SUBFACE " << i+1 << endl;
1547 ((_QuadFaceGrid&)(*child)).SetID( myID );
1548 child->DumpVertices();
1554 //=======================================================================
1555 //function : _FaceSide
1556 //purpose : copy constructor
1557 //=======================================================================
1559 _FaceSide::_FaceSide(const _FaceSide& other)
1561 myEdge = other.myEdge;
1562 myChildren = other.myChildren;
1563 myNbChildren = other.myNbChildren;
1564 myVertices.Assign( other.myVertices );
1568 //================================================================================
1570 * \brief Construct a face side of one edge
1572 //================================================================================
1574 _FaceSide::_FaceSide(const TopoDS_Edge& edge):
1575 myEdge( edge ), myNbChildren(0)
1577 if ( !edge.IsNull() )
1578 for ( TopExp_Explorer exp( edge, TopAbs_VERTEX ); exp.More(); exp.Next() )
1579 //myVertices.insert( ptr ( exp.Current() ));
1580 myVertices.Add( exp.Current() );
1583 //================================================================================
1585 * \brief Construct a face side of several edges
1587 //================================================================================
1589 _FaceSide::_FaceSide(const list<TopoDS_Edge>& edges):
1592 list<TopoDS_Edge>::const_iterator edge = edges.begin(), eEnd = edges.end();
1593 for ( ; edge != eEnd; ++edge ) {
1594 myChildren.push_back( _FaceSide( *edge ));
1596 // myVertices.insert( myChildren.back().myVertices.begin(),
1597 // myChildren.back().myVertices.end() );
1598 myVertices.Add( myChildren.back().FirstVertex() );
1599 myVertices.Add( myChildren.back().LastVertex() );
1600 myChildren.back().SetID( Q_CHILD ); // not to splice them
1604 //=======================================================================
1605 //function : GetSide
1607 //=======================================================================
1609 _FaceSide* _FaceSide::GetSide(const int i)
1611 if ( i >= myNbChildren )
1614 list< _FaceSide >::iterator side = myChildren.begin();
1616 std::advance( side, i );
1620 //=======================================================================
1621 //function : GetSide
1623 //=======================================================================
1625 const _FaceSide* _FaceSide::GetSide(const int i) const
1627 return const_cast< _FaceSide* >(this)->GetSide(i);
1630 //=======================================================================
1631 //function : NbVertices
1632 //purpose : return nb of vertices in the side
1633 //=======================================================================
1635 int _FaceSide::NbVertices() const
1637 if ( myChildren.empty() )
1638 return myVertices.Extent();
1640 return myNbChildren + 1;
1643 //=======================================================================
1644 //function : NbCommonVertices
1645 //purpose : Returns number of my vertices common with the given ones
1646 //=======================================================================
1648 int _FaceSide::NbCommonVertices( const TopTools_MapOfShape& VV ) const
1651 TopTools_MapIteratorOfMapOfShape vIt ( myVertices );
1652 for ( ; vIt.More(); vIt.Next() )
1653 nbCommon += ( VV.Contains( vIt.Key() ));
1658 //=======================================================================
1659 //function : FirstVertex
1661 //=======================================================================
1663 TopoDS_Vertex _FaceSide::FirstVertex() const
1665 if ( myChildren.empty() )
1666 return TopExp::FirstVertex( myEdge, Standard_True );
1668 return myChildren.front().FirstVertex();
1671 //=======================================================================
1672 //function : LastVertex
1674 //=======================================================================
1676 TopoDS_Vertex _FaceSide::LastVertex() const
1678 if ( myChildren.empty() )
1679 return TopExp::LastVertex( myEdge, Standard_True );
1681 return myChildren.back().LastVertex();
1684 //=======================================================================
1687 //=======================================================================
1689 TopoDS_Vertex _FaceSide::Vertex(int i) const
1691 if ( myChildren.empty() )
1692 return i ? LastVertex() : FirstVertex();
1694 if ( i >= myNbChildren )
1695 return myChildren.back().LastVertex();
1697 return GetSide(i)->FirstVertex();
1700 //================================================================================
1702 * \brief Return i-the zero-based edge of the side
1704 //================================================================================
1706 TopoDS_Edge _FaceSide::Edge(int i) const
1708 if ( i == 0 && !myEdge.IsNull() )
1711 if ( const _FaceSide* iSide = GetSide( i ))
1712 return iSide->myEdge;
1714 return TopoDS_Edge();
1717 //=======================================================================
1718 //function : Contain
1720 //=======================================================================
1722 bool _FaceSide::Contain( const _FaceSide& side, int* which ) const
1724 if ( !which || myChildren.empty() )
1729 TopTools_MapIteratorOfMapOfShape vIt ( side.myVertices );
1730 for ( ; vIt.More(); vIt.Next() )
1731 nbCommon += ( myVertices.Contains( vIt.Key() ));
1732 return (nbCommon > 1);
1734 list< _FaceSide >::const_iterator mySide = myChildren.begin(), sideEnd = myChildren.end();
1735 for ( int i = 0; mySide != sideEnd; ++mySide, ++i ) {
1736 if ( mySide->Contain( side )) {
1744 //=======================================================================
1745 //function : Contain
1747 //=======================================================================
1749 bool _FaceSide::Contain( const TopoDS_Vertex& vertex ) const
1751 return myVertices.Contains( vertex );
1754 //=======================================================================
1755 //function : AppendSide
1757 //=======================================================================
1759 void _FaceSide::AppendSide( const _FaceSide& side )
1761 if ( !myEdge.IsNull() )
1763 myChildren.push_back( *this );
1767 myChildren.push_back( side );
1769 TopTools_MapIteratorOfMapOfShape vIt ( side.myVertices );
1770 for ( ; vIt.More(); vIt.Next() )
1771 myVertices.Add( vIt.Key() );
1774 myChildren.back().SetID( EQuadSides( myNbChildren-1 ));
1777 //=======================================================================
1778 //function : SetBottomSide
1780 //=======================================================================
1782 void _FaceSide::SetBottomSide( int i )
1784 if ( i > 0 && myID == Q_PARENT ) {
1785 list< _FaceSide >::iterator sideEnd, side = myChildren.begin();
1786 std::advance( side, i );
1787 myChildren.splice( myChildren.begin(), myChildren, side, myChildren.end() );
1789 side = myChildren.begin(), sideEnd = myChildren.end();
1790 for ( int i = 0; side != sideEnd; ++side, ++i ) {
1791 side->SetID( EQuadSides(i) );
1792 side->SetBottomSide(i);
1797 //=======================================================================
1798 //function : GetNbSegments
1800 //=======================================================================
1802 int _FaceSide::GetNbSegments(SMESH_Mesh& mesh) const
1805 if ( myChildren.empty() )
1807 nb = mesh.GetSubMesh(myEdge)->GetSubMeshDS()->NbElements();
1811 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1812 for ( ; side != sideEnd; ++side )
1813 nb += side->GetNbSegments(mesh);
1818 //=======================================================================
1819 //function : StoreNodes
1821 //=======================================================================
1823 bool _FaceSide::StoreNodes(SMESH_Mesh& mesh,
1824 vector<const SMDS_MeshNode*>& myGrid,
1827 list< TopoDS_Edge > edges;
1828 if ( myChildren.empty() )
1830 edges.push_back( myEdge );
1834 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1835 for ( ; side != sideEnd; ++side )
1837 edges.push_front( side->myEdge );
1839 edges.push_back ( side->myEdge );
1842 list< TopoDS_Edge >::iterator edge = edges.begin(), eEnd = edges.end();
1843 for ( ; edge != eEnd; ++edge )
1845 map< double, const SMDS_MeshNode* > nodes;
1846 bool ok = SMESH_Algo::GetSortedNodesOnEdge( mesh.GetMeshDS(),
1848 /*ignoreMediumNodes=*/true,
1850 if ( !ok ) return false;
1852 bool forward = ( edge->Orientation() == TopAbs_FORWARD );
1853 if ( reverse ) forward = !forward;
1856 map< double, const SMDS_MeshNode* >::iterator u_node, nEnd = nodes.end();
1857 for ( u_node = nodes.begin(); u_node != nEnd; ++u_node )
1858 myGrid[ nbNodes++ ] = u_node->second;
1862 map< double, const SMDS_MeshNode* >::reverse_iterator u_node, nEnd = nodes.rend();
1863 for ( u_node = nodes.rbegin(); u_node != nEnd; ++u_node )
1864 myGrid[ nbNodes++ ] = u_node->second;
1866 nbNodes--; // node on vertex present in two adjacent edges
1871 //=======================================================================
1873 //purpose : dump end vertices
1874 //=======================================================================
1876 void _FaceSide::Dump() const
1878 if ( myChildren.empty() )
1880 const char* sideNames[] = { "Q_BOTTOM", "Q_RIGHT", "Q_TOP", "Q_LEFT", "Q_CHILD", "Q_PARENT" };
1881 if ( myID >= Q_BOTTOM && myID < Q_PARENT )
1882 cout << sideNames[ myID ] << endl;
1884 cout << "<UNDEFINED ID>" << endl;
1885 TopoDS_Vertex f = FirstVertex();
1886 TopoDS_Vertex l = LastVertex();
1887 gp_Pnt pf = BRep_Tool::Pnt(f);
1888 gp_Pnt pl = BRep_Tool::Pnt(l);
1889 cout << "\t ( "<< ptr( f ) << " - " << ptr( l )<< " )"
1890 << "\t ( "<< pf.X()<<", "<<pf.Y()<<", "<<pf.Z()<<" ) - "
1891 << " ( "<< pl.X()<<", "<<pl.Y()<<", "<<pl.Z()<<" )"<<endl;
1895 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1896 for ( ; side != sideEnd; ++side ) {