1 // Copyright (C) 2007-2019 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 : implementation 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 "SMESHDS_Mesh.hxx"
32 #include "SMESHDS_SubMesh.hxx"
33 #include "SMESH_Block.hxx"
34 #include "SMESH_Comment.hxx"
35 #include "SMESH_ComputeError.hxx"
36 #include "SMESH_Mesh.hxx"
37 #include "SMESH_MeshAlgos.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMESH_subMesh.hxx"
41 #include <BRepAdaptor_Surface.hxx>
42 #include <BRep_Tool.hxx>
43 #include <Standard_ErrorHandler.hxx>
44 #include <Standard_Failure.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_MapIteratorOfMapOfShape.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_SequenceOfShape.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Face.hxx>
53 #include <TopoDS_Vertex.hxx>
55 #include <gp_Pnt2d.hxx>
68 // #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; }
72 #define DUMP_VERT(msg,v)
75 //================================================================================
76 // text for message about an internal error
77 #define ERR_LI(txt) SMESH_Comment(txt) << ":" << __LINE__
79 // order corresponds to right order of edges in CASCADE face
80 enum EQuadSides{ Q_BOTTOM=0, Q_RIGHT, Q_TOP, Q_LEFT, Q_CHILD, Q_PARENT };
82 enum EBoxSides{ B_BOTTOM=0, B_RIGHT, B_TOP, B_LEFT, B_FRONT, B_BACK, B_UNDEFINED };
84 enum EAxes{ COO_X=1, COO_Y, COO_Z };
86 //================================================================================
88 * \brief Converter of a pair of integers to a sole index
93 _Indexer( int xSize, int ySize ): _xSize(xSize), _ySize(ySize) {}
94 int size() const { return _xSize * _ySize; }
95 int operator()(const int x, const int y) const { return y * _xSize + x; }
98 //================================================================================
100 * \brief Wrapper of a composite or an ordinary edge.
105 _FaceSide(const _FaceSide& other);
106 _FaceSide(const TopoDS_Edge& edge=TopoDS_Edge());
107 _FaceSide(const list<TopoDS_Edge>& edges);
108 _FaceSide* GetSide(const int i);
109 const _FaceSide* GetSide(const int i) const;
110 int size() const { return myChildren.size(); }
111 int NbVertices() const;
112 int NbCommonVertices( const TopTools_MapOfShape& VV ) const;
113 TopoDS_Vertex FirstVertex() const;
114 TopoDS_Vertex LastVertex() const;
115 TopoDS_Vertex Vertex(int i) const;
116 TopoDS_Edge Edge(int i) const;
117 bool Contain( const _FaceSide& side, int* which=0 ) const;
118 bool Contain( const TopoDS_Vertex& vertex ) const;
119 void AppendSide( const _FaceSide& side );
120 void SetBottomSide( int i );
121 int GetNbSegments(SMESH_Mesh& mesh) const;
122 bool StoreNodes(SMESH_Mesh& mesh, vector<const SMDS_MeshNode*>& myGrid, bool reverse );
123 void SetID(EQuadSides id) { myID = id; }
124 static inline const TopoDS_TShape* ptr(const TopoDS_Shape& theShape)
125 { return theShape.TShape().operator->(); }
132 list< _FaceSide > myChildren;
135 TopTools_MapOfShape myVertices;
137 EQuadSides myID; // debug
139 //================================================================================
141 * \brief Class corresponding to a meshed composite face of a box.
142 * Provides simplified access to it's sub-mesh data.
146 typedef list< _QuadFaceGrid > TChildren;
150 public: //** Methods to find and orient faces of 6 sides of the box **//
153 bool Init(const TopoDS_Face& f, SMESH_Mesh& mesh );
155 //!< try to unite self with other face
156 bool AddContinuousFace( const _QuadFaceGrid& f, const TopTools_MapOfShape& internalEdges );
158 //!< Try to set the side as bottom hirizontal side
159 bool SetBottomSide(const _FaceSide& side, int* sideIndex=0);
161 //!< Return face adjacent to zero-based i-th side of this face
162 _QuadFaceGrid* FindAdjacentForSide(int i, list<_QuadFaceGrid>& faces, EBoxSides id) const;
164 //!< Reverse edges in order to have the bottom edge going along axes of the unit box
167 bool IsComplex() const { return !myChildren.empty(); }
169 int NbChildren() const { return myChildren.size(); }
171 typedef SMDS_SetIterator< const _QuadFaceGrid&,
172 TChildren::const_iterator,
173 SMDS::SimpleAccessor<const _QuadFaceGrid&,TChildren::const_iterator>,
174 SMDS::PassAllValueFilter<_QuadFaceGrid> >
177 TChildIterator GetChildren() const
178 { return TChildIterator( myChildren.begin(), myChildren.end()); }
180 public: //** Loading and access to mesh **//
182 //!< Load nodes of a mesh
183 bool LoadGrid( SMESH_Mesh& mesh );
185 //!< Computes normalized parameters of nodes of myGrid
186 void ComputeIJK( int i1, int i2, double v3 );
188 //!< Return number of segments on the hirizontal sides
189 int GetNbHoriSegments(SMESH_Mesh& mesh, bool withBrothers=false) const;
191 //!< Return number of segments on the vertical sides
192 int GetNbVertSegments(SMESH_Mesh& mesh, bool withBrothers=false) const;
194 //!< Return edge on the hirizontal bottom sides
195 int GetHoriEdges(vector<TopoDS_Edge> & edges) const;
197 //!< Return a node by its position
198 const SMDS_MeshNode* GetNode(int iHori, int iVert) const;
200 //!< Return node coordinates by its position
201 gp_XYZ GetXYZ(int iHori, int iVert) const;
203 //!< Return normalized parameters of nodes within the unitary cube
204 gp_XYZ& GetIJK(int iCol, int iRow) { return myIJK[ myIndexer( iCol, iRow )]; }
206 public: //** Access to member fields **//
208 //!< Return i-th face side (0<i<4)
209 const _FaceSide& GetSide(int i) const;
211 //!< Return it's face, NULL if it is composite
212 TopoDS_Face GetFace() const { return myFace; }
214 //!< Return normal to the face at vertex v
215 bool GetNormal( const TopoDS_Vertex& v, gp_Vec& n ) const;
217 SMESH_ComputeErrorPtr GetError() const { return myError; }
219 void SetID(EBoxSides id) { myID = id; }
221 void DumpGrid() const;
223 void DumpVertices() const;
227 bool error(const std::string& text, int code = COMPERR_ALGO_FAILED)
228 { myError = SMESH_ComputeError::New( code, text ); return false; }
230 bool error(const SMESH_ComputeErrorPtr& err)
231 { myError = err; return ( !myError || myError->IsOK() ); }
233 bool loadCompositeGrid(SMESH_Mesh& mesh);
235 bool fillGrid(SMESH_Mesh& theMesh,
236 vector<const SMDS_MeshNode*> & theGrid,
237 const _Indexer& theIndexer,
241 bool locateChildren();
243 void setBrothers( set< _QuadFaceGrid* >& notLocatedBrothers );
249 TChildren myChildren;
251 _QuadFaceGrid* myLeftBottomChild;
252 _QuadFaceGrid* myRightBrother;
253 _QuadFaceGrid* myUpBrother;
256 vector<const SMDS_MeshNode*> myGrid;
257 vector<gp_XYZ> myIJK; // normalized parameters of nodes
259 SMESH_ComputeErrorPtr myError;
261 EBoxSides myID; // debug
264 //================================================================================
268 //================================================================================
270 StdMeshers_CompositeHexa_3D::StdMeshers_CompositeHexa_3D(int hypId, SMESH_Gen* gen)
271 :SMESH_3D_Algo(hypId, gen)
273 _name = "CompositeHexa_3D";
274 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID); // 1 bit /shape type
277 //================================================================================
279 * \brief always return true
281 //================================================================================
283 bool StdMeshers_CompositeHexa_3D::CheckHypothesis(SMESH_Mesh& aMesh,
284 const TopoDS_Shape& aShape,
285 Hypothesis_Status& aStatus)
294 //================================================================================
296 * \brief Checks structure of a quadrangular mesh at the common VERTEX of two EDGEs.
297 * Returns true if there are two quadrangles near the VERTEX.
299 //================================================================================
301 bool isContinuousMesh(TopoDS_Edge E1,
303 const TopoDS_Face& F,
304 const SMESH_Mesh& mesh)
306 if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
307 E1.Orientation( TopAbs_FORWARD );
308 if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
309 E2.Orientation( TopAbs_FORWARD );
312 if ( !TopExp::CommonVertex( E1, E2, V )) return false;
314 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
315 if ( !n ) return SMESH_Algo::IsContinuous( E1, E2 ); // meshed by "composite segment"
317 SMESHDS_SubMesh* sm = mesh.GetSubMeshContaining( F )->GetSubMeshDS();
318 if ( !sm ) return false;
321 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
323 return SMESH_Algo::IsContinuous( E1, E2 ); // meshed by "composite segment"
324 while ( fIt->more() )
326 const SMDS_MeshElement* f = fIt->next();
327 if ( !sm->Contains( f )) continue;
329 if ( f->NbCornerNodes() == 4 )
337 //================================================================================
339 * \brief Return true if a vertex holds a node and this node is used by some quadrangle
341 //================================================================================
343 // bool isMeshedVertex( TopoDS_Vertex& V,
344 // const SMESH_Mesh& mesh )
346 // const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
347 // if ( !n ) return false;
349 // SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
350 // while ( fIt->more() )
352 // const SMDS_MeshElement* f = fIt->next();
353 // if ( f->NbCornerNodes() == 4 )
359 //================================================================================
361 * \brief Finds VERTEXes located at block corners
363 //================================================================================
365 void getBlockCorners( SMESH_Mesh& mesh,
366 const TopoDS_Shape& shape,
367 TopTools_MapOfShape& cornerVV)
369 set<int> faceIDs; // ids of FACEs in the shape
371 for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next() )
372 faceIDs.insert( mesh.GetMeshDS()->ShapeToIndex( exp.Current() ));
374 TopTools_MapOfShape checkedVV;
375 for ( exp.Init( shape, TopAbs_VERTEX ); exp.More(); exp.Next() )
377 TopoDS_Vertex V = TopoDS::Vertex( exp.Current() );
378 if ( !checkedVV.Add( V )) continue;
380 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( V, mesh.GetMeshDS() );
384 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
385 while ( fIt->more() )
387 const SMDS_MeshElement* f = fIt->next();
388 if ( !faceIDs.count( f->getshapeId() )) continue;
390 if ( f->NbCornerNodes() == 4 )
400 //================================================================================
402 * \brief Return EDGEs dividing one box side
404 //================================================================================
406 bool getInternalEdges( SMESH_Mesh& mesh,
407 const TopoDS_Shape& shape,
408 const TopTools_MapOfShape& cornerVV,
409 TopTools_MapOfShape& internEE)
411 TopTools_IndexedMapOfShape subEE;
412 TopExp::MapShapes( shape, TopAbs_EDGE, subEE );
413 //TopExp::MapShapes( shape, TopAbs_FACE, subFF );
416 TopTools_MapOfShape subChecked, ridgeEE;
417 TopTools_MapIteratorOfMapOfShape vIt( cornerVV );
418 for ( ; vIt.More(); vIt.Next() )
420 TopoDS_Shape V0 = vIt.Key();
421 // walk from one corner VERTEX to another along ridge EDGEs
422 PShapeIteratorPtr riIt = SMESH_MesherHelper::GetAncestors( V0, mesh, TopAbs_EDGE );
423 while ( const TopoDS_Shape* riE = riIt->next() )
425 if ( !subEE.Contains( *riE ) || !subChecked.Add( *riE ))
427 TopoDS_Edge ridgeE = TopoDS::Edge( *riE );
428 while ( !ridgeE.IsNull() )
430 if ( !ridgeEE.Add( ridgeE ))
432 TopExp::Vertices( ridgeE, VV[0], VV[1] );
433 TopoDS_Shape V1 = VV[ V0.IsSame( VV[0] )];
434 if ( cornerVV.Contains( V1 ) )
435 break; // ridgeE reached a corner VERTEX
437 // detect internal EDGEs among those sharing V1. There can be 2, 3 or 4 EDGEs and
438 // number of internal EDGEs is N-2
439 TopoDS_Shape nextRidgeE;
440 PShapeIteratorPtr eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
441 while ( const TopoDS_Shape* E = eIt->next() )
443 if ( E->IsSame( ridgeE ) || !subEE.Contains( *E ) || !subChecked.Add( *E ))
445 // look for FACEs sharing both E and ridgeE
446 PShapeIteratorPtr fIt = SMESH_MesherHelper::GetAncestors( *E, mesh, TopAbs_FACE );
447 while ( const TopoDS_Shape* F = fIt->next() )
449 if ( !SMESH_MesherHelper::IsSubShape( ridgeE, *F ))
451 if ( isContinuousMesh( ridgeE, TopoDS::Edge( *E ), TopoDS::Face( *F ), mesh ))
462 // look for the next ridge EDGE ending at V1
463 if ( nextRidgeE.IsNull() )
465 eIt = SMESH_MesherHelper::GetAncestors( V1, mesh, TopAbs_EDGE );
466 while ( const TopoDS_Shape* E = eIt->next() )
467 if ( !ridgeE.IsSame( *E ) && !internEE.Contains( *E ) && subEE.Contains( *E ))
473 ridgeE = TopoDS::Edge( nextRidgeE );
476 if ( ridgeE.IsNull() )
478 } // check EDGEs around the last VERTEX of ridgeE
479 } // loop on ridge EDGEs around a corner VERTEX
480 } // loop on on corner VERTEXes
482 if ( subEE.Extent() > ridgeEE.Extent() + internEE.Extent() ) // PAL23269
483 for ( int i = 1; i < subEE.Extent(); ++i )
484 if ( !ridgeEE.Contains( subEE(i) ))
485 internEE.Add( subEE(i) );
488 } // getInternalEdges()
491 //================================================================================
493 * \brief Tries to find 6 sides of a box
495 //================================================================================
497 bool StdMeshers_CompositeHexa_3D::findBoxFaces( const TopoDS_Shape& shape,
498 list< _QuadFaceGrid >& boxFaces,
500 _QuadFaceGrid * & fBottom,
501 _QuadFaceGrid * & fTop,
502 _QuadFaceGrid * & fFront,
503 _QuadFaceGrid * & fBack,
504 _QuadFaceGrid * & fLeft,
505 _QuadFaceGrid * & fRight)
507 TopTools_MapOfShape cornerVertices;
508 getBlockCorners( mesh, shape, cornerVertices );
509 if ( cornerVertices.Extent() != 8 )
510 return error( COMPERR_BAD_INPUT_MESH, "Can't find 8 corners of a block by 2D mesh" );
511 TopTools_MapOfShape internalEdges;
512 if ( !getInternalEdges( mesh, shape, cornerVertices, internalEdges ))
513 return error( COMPERR_BAD_INPUT_MESH, "2D mesh is not suitable for i,j,k hexa meshing" );
515 list< _QuadFaceGrid >::iterator boxFace;
518 for ( exp.Init( shape, TopAbs_FACE ); exp.More(); exp.Next(), ++nbFaces )
521 if ( !f.Init( TopoDS::Face( exp.Current() ), mesh ))
522 return error (COMPERR_BAD_SHAPE);
524 _QuadFaceGrid* prevContinuous = 0;
525 for ( boxFace = boxFaces.begin(); boxFace != boxFaces.end(); ++boxFace )
527 if ( prevContinuous )
529 if ( prevContinuous->AddContinuousFace( *boxFace, internalEdges ))
530 boxFace = --boxFaces.erase( boxFace );
532 else if ( boxFace->AddContinuousFace( f, internalEdges ))
534 prevContinuous = & (*boxFace);
537 if ( !prevContinuous )
538 boxFaces.push_back( f );
540 // Check what we have
541 if ( boxFaces.size() != 6 && nbFaces != 6)
544 SMESH_Comment("Can't find 6 sides of a box. Number of found sides - ")<<boxFaces.size());
546 if ( boxFaces.size() != 6 && nbFaces == 6 ) { // strange ordinary box with continuous faces
547 boxFaces.resize( 6 );
548 boxFace = boxFaces.begin();
549 for ( exp.Init( shape, TopAbs_FACE); exp.More(); exp.Next(), ++boxFace )
550 boxFace->Init( TopoDS::Face( exp.Current() ), mesh );
552 // ----------------------------------------
553 // Find out position of faces within a box
554 // ----------------------------------------
555 // start from a bottom face
556 fBottom = &boxFaces.front();
557 fBottom->SetID( B_BOTTOM );
558 // find vertical faces
559 fFront = fBottom->FindAdjacentForSide( Q_BOTTOM, boxFaces, B_FRONT );
560 fLeft = fBottom->FindAdjacentForSide( Q_RIGHT, boxFaces, B_LEFT );
561 fBack = fBottom->FindAdjacentForSide( Q_TOP, boxFaces, B_BACK );
562 fRight = fBottom->FindAdjacentForSide( Q_LEFT, boxFaces, B_RIGHT );
564 if ( !fFront || !fBack || !fLeft || !fRight )
565 return error(COMPERR_BAD_SHAPE);
568 for ( boxFace = ++boxFaces.begin(); boxFace != boxFaces.end() && !fTop; ++boxFace )
571 fTop->SetID( B_TOP );
572 if ( fTop==fFront || fTop==fLeft || fTop==fBack || fTop==fRight )
575 // set bottom of the top side
576 if ( !fTop->SetBottomSide( fFront->GetSide( Q_TOP ) )) {
577 if ( !fFront->IsComplex() )
578 return error( ERR_LI("Error in StdMeshers_CompositeHexa_3D::Compute()"));
580 _QuadFaceGrid::TChildIterator chIt = fFront->GetChildren();
581 while ( chIt.more() ) {
582 const _QuadFaceGrid& frontChild = chIt.next();
583 if ( fTop->SetBottomSide( frontChild.GetSide( Q_TOP )))
589 return error(COMPERR_BAD_SHAPE);
591 // orient bottom edge of faces along axes of the unit box
592 fBottom->ReverseEdges();
593 fBack ->ReverseEdges();
594 fLeft ->ReverseEdges();
599 //================================================================================
601 * \brief Computes hexahedral mesh on a box with composite sides
602 * \param aMesh - mesh to compute
603 * \param aShape - shape to mesh
604 * \retval bool - success sign
606 //================================================================================
608 bool StdMeshers_CompositeHexa_3D::Compute(SMESH_Mesh& theMesh,
609 const TopoDS_Shape& theShape)
611 SMESH_MesherHelper helper( theMesh );
612 _quadraticMesh = helper.IsQuadraticSubMesh( theShape );
613 helper.SetElementsOnShape( true );
615 // -------------------------
616 // Try to find 6 side faces
617 // -------------------------
618 list< _QuadFaceGrid > boxFaceContainer;
619 _QuadFaceGrid *fBottom, *fTop, *fFront, *fBack, *fLeft, *fRight;
620 if ( ! findBoxFaces( theShape, boxFaceContainer, theMesh,
621 fBottom, fTop, fFront, fBack, fLeft, fRight))
624 // ------------------------------------------
625 // Fill columns of nodes with existing nodes
626 // ------------------------------------------
628 // let faces load their grids
629 if ( !fBottom->LoadGrid( theMesh )) return error( fBottom->GetError() );
630 if ( !fBack ->LoadGrid( theMesh )) return error( fBack ->GetError() );
631 if ( !fLeft ->LoadGrid( theMesh )) return error( fLeft ->GetError() );
632 if ( !fFront ->LoadGrid( theMesh )) return error( fFront ->GetError() );
633 if ( !fRight ->LoadGrid( theMesh )) return error( fRight ->GetError() );
634 if ( !fTop ->LoadGrid( theMesh )) return error( fTop ->GetError() );
636 // compute normalized parameters of nodes on sides (PAL23189)
637 fBottom->ComputeIJK( COO_X, COO_Y, /*z=*/0. );
638 fBack ->ComputeIJK( COO_X, COO_Z, /*y=*/1. );
639 fLeft ->ComputeIJK( COO_Y, COO_Z, /*x=*/0. );
640 fFront ->ComputeIJK( COO_X, COO_Z, /*y=*/0. );
641 fRight ->ComputeIJK( COO_Y, COO_Z, /*x=*/1. );
642 fTop ->ComputeIJK( COO_X, COO_Y, /*z=*/1. );
644 int x, xSize = fBottom->GetNbHoriSegments(theMesh) + 1, X = xSize - 1;
645 int y, ySize = fBottom->GetNbVertSegments(theMesh) + 1, Y = ySize - 1;
646 int z, zSize = fFront ->GetNbVertSegments(theMesh) + 1, Z = zSize - 1;
647 _Indexer colIndex( xSize, ySize );
648 vector< vector< const SMDS_MeshNode* > > columns( colIndex.size() );
650 // fill node columns by front and back box sides
651 for ( x = 0; x < xSize; ++x ) {
652 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( x, 0 )];
653 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( x, Y )];
654 column0.resize( zSize );
655 column1.resize( zSize );
656 for ( z = 0; z < zSize; ++z ) {
657 column0[ z ] = fFront->GetNode( x, z );
658 column1[ z ] = fBack ->GetNode( x, z );
661 // fill node columns by left and right box sides
662 for ( y = 1; y < ySize-1; ++y ) {
663 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( 0, y )];
664 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( X, y )];
665 column0.resize( zSize );
666 column1.resize( zSize );
667 for ( z = 0; z < zSize; ++z ) {
668 column0[ z ] = fLeft ->GetNode( y, z );
669 column1[ z ] = fRight->GetNode( y, z );
672 // get nodes from top and bottom box sides
673 for ( x = 1; x < xSize-1; ++x ) {
674 for ( y = 1; y < ySize-1; ++y ) {
675 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
676 column.resize( zSize );
677 column.front() = fBottom->GetNode( x, y );
678 column.back() = fTop ->GetNode( x, y );
682 // ----------------------------
683 // Add internal nodes of a box
684 // ----------------------------
685 // projection points of internal nodes on box sub-shapes by which
686 // coordinates of internal nodes are computed
687 vector<gp_XYZ> pointsOnShapes( SMESH_Block::ID_Shell );
689 // projections on vertices are constant
690 pointsOnShapes[ SMESH_Block::ID_V000 ] = fBottom->GetXYZ( 0, 0 );
691 pointsOnShapes[ SMESH_Block::ID_V100 ] = fBottom->GetXYZ( X, 0 );
692 pointsOnShapes[ SMESH_Block::ID_V010 ] = fBottom->GetXYZ( 0, Y );
693 pointsOnShapes[ SMESH_Block::ID_V110 ] = fBottom->GetXYZ( X, Y );
694 pointsOnShapes[ SMESH_Block::ID_V001 ] = fTop->GetXYZ( 0, 0 );
695 pointsOnShapes[ SMESH_Block::ID_V101 ] = fTop->GetXYZ( X, 0 );
696 pointsOnShapes[ SMESH_Block::ID_V011 ] = fTop->GetXYZ( 0, Y );
697 pointsOnShapes[ SMESH_Block::ID_V111 ] = fTop->GetXYZ( X, Y );
699 gp_XYZ params; // normalized parameters of an internal node within the unit box
701 for ( x = 1; x < xSize-1; ++x )
703 const double rX = x / double(X);
704 for ( y = 1; y < ySize-1; ++y )
706 const double rY = y / double(Y);
707 // column to fill during z loop
708 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
709 // points projections on horizontal edges
710 pointsOnShapes[ SMESH_Block::ID_Ex00 ] = fBottom->GetXYZ( x, 0 );
711 pointsOnShapes[ SMESH_Block::ID_Ex10 ] = fBottom->GetXYZ( x, Y );
712 pointsOnShapes[ SMESH_Block::ID_E0y0 ] = fBottom->GetXYZ( 0, y );
713 pointsOnShapes[ SMESH_Block::ID_E1y0 ] = fBottom->GetXYZ( X, y );
714 pointsOnShapes[ SMESH_Block::ID_Ex01 ] = fTop->GetXYZ( x, 0 );
715 pointsOnShapes[ SMESH_Block::ID_Ex11 ] = fTop->GetXYZ( x, Y );
716 pointsOnShapes[ SMESH_Block::ID_E0y1 ] = fTop->GetXYZ( 0, y );
717 pointsOnShapes[ SMESH_Block::ID_E1y1 ] = fTop->GetXYZ( X, y );
718 // points projections on horizontal faces
719 pointsOnShapes[ SMESH_Block::ID_Fxy0 ] = fBottom->GetXYZ( x, y );
720 pointsOnShapes[ SMESH_Block::ID_Fxy1 ] = fTop ->GetXYZ( x, y );
721 for ( z = 1; z < zSize-1; ++z ) // z loop
723 // compute normalized parameters of an internal node within the unit box
724 const double rZ = z / double(Z);
725 const gp_XYZ& pBo = fBottom->GetIJK( x, y );
726 const gp_XYZ& pTo = fTop ->GetIJK( x, y );
727 const gp_XYZ& pFr = fFront ->GetIJK( x, z );
728 const gp_XYZ& pBa = fBack ->GetIJK( x, z );
729 const gp_XYZ& pLe = fLeft ->GetIJK( y, z );
730 const gp_XYZ& pRi = fRight ->GetIJK( y, z );
731 params.SetCoord( 1, 0.5 * ( pBo.X() * ( 1. - rZ ) + pTo.X() * rZ +
732 pFr.X() * ( 1. - rY ) + pBa.X() * rY ));
733 params.SetCoord( 2, 0.5 * ( pBo.Y() * ( 1. - rZ ) + pTo.Y() * rZ +
734 pLe.Y() * ( 1. - rX ) + pRi.Y() * rX ));
735 params.SetCoord( 3, 0.5 * ( pFr.Z() * ( 1. - rY ) + pBa.Z() * rY +
736 pLe.Z() * ( 1. - rX ) + pRi.Z() * rX ));
738 // point projections on vertical edges
739 pointsOnShapes[ SMESH_Block::ID_E00z ] = fFront->GetXYZ( 0, z );
740 pointsOnShapes[ SMESH_Block::ID_E10z ] = fFront->GetXYZ( X, z );
741 pointsOnShapes[ SMESH_Block::ID_E01z ] = fBack->GetXYZ( 0, z );
742 pointsOnShapes[ SMESH_Block::ID_E11z ] = fBack->GetXYZ( X, z );
743 // point projections on vertical faces
744 pointsOnShapes[ SMESH_Block::ID_Fx0z ] = fFront->GetXYZ( x, z );
745 pointsOnShapes[ SMESH_Block::ID_Fx1z ] = fBack ->GetXYZ( x, z );
746 pointsOnShapes[ SMESH_Block::ID_F0yz ] = fLeft ->GetXYZ( y, z );
747 pointsOnShapes[ SMESH_Block::ID_F1yz ] = fRight->GetXYZ( y, z );
749 // compute internal node coordinates
751 SMESH_Block::ShellPoint( params, pointsOnShapes, coords );
752 column[ z ] = helper.AddNode( coords.X(), coords.Y(), coords.Z() );
756 //cout << "----------------------------------------------------------------------"<<endl;
757 //for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id)
759 // gp_XYZ p = pointsOnShapes[ id ];
760 // SMESH_Block::DumpShapeID( id,cout)<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;
762 //cout << "Params: ( "<< params.X()<<", "<<params.Y()<<", "<<params.Z()<<" )"<<endl;
763 //cout << "coords: ( "<< coords.X()<<", "<<coords.Y()<<", "<<coords.Z()<<" )"<<endl;
768 // faces no more needed, free memory
769 boxFaceContainer.clear();
774 for ( x = 0; x < xSize-1; ++x ) {
775 for ( y = 0; y < ySize-1; ++y ) {
776 vector< const SMDS_MeshNode* >& col00 = columns[ colIndex( x, y )];
777 vector< const SMDS_MeshNode* >& col10 = columns[ colIndex( x+1, y )];
778 vector< const SMDS_MeshNode* >& col01 = columns[ colIndex( x, y+1 )];
779 vector< const SMDS_MeshNode* >& col11 = columns[ colIndex( x+1, y+1 )];
780 for ( z = 0; z < zSize-1; ++z )
782 // bottom face normal of a hexa mush point outside the volume
783 helper.AddVolume(col00[z], col01[z], col11[z], col10[z],
784 col00[z+1], col01[z+1], col11[z+1], col10[z+1]);
791 //================================================================================
795 //================================================================================
797 bool StdMeshers_CompositeHexa_3D::Evaluate(SMESH_Mesh& theMesh,
798 const TopoDS_Shape& theShape,
799 MapShapeNbElems& aResMap)
801 // -------------------------
802 // Try to find 6 side faces
803 // -------------------------
804 list< _QuadFaceGrid > boxFaceContainer;
805 _QuadFaceGrid *fBottom, *fTop, *fFront, *fBack, *fLeft, *fRight;
806 if ( ! findBoxFaces( theShape, boxFaceContainer, theMesh,
807 fBottom, fTop, fFront, fBack, fLeft, fRight))
810 // Find a less complex side
811 _QuadFaceGrid * lessComplexSide = & boxFaceContainer.front();
812 list< _QuadFaceGrid >::iterator face = boxFaceContainer.begin();
813 for ( ++face; face != boxFaceContainer.end() && lessComplexSide->IsComplex(); ++face )
814 if ( face->NbChildren() < lessComplexSide->NbChildren() )
815 lessComplexSide = & *face;
817 // Get an 1D size of lessComplexSide
819 vector<TopoDS_Edge> edges;
820 if ( !lessComplexSide->GetHoriEdges(edges) )
822 for ( size_t i = 0; i < edges.size(); ++i )
824 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( edges[i] )];
825 if ( !nbElems.empty() )
826 nbSeg1 += Max( nbElems[ SMDSEntity_Edge ], nbElems[ SMDSEntity_Quad_Edge ]);
829 // Get an 1D size of a box side orthogonal to lessComplexSide
831 _QuadFaceGrid* ortoSide =
832 lessComplexSide->FindAdjacentForSide( Q_LEFT, boxFaceContainer, B_UNDEFINED );
834 if ( !ortoSide || !ortoSide->GetHoriEdges(edges) ) return false;
835 for ( size_t i = 0; i < edges.size(); ++i )
837 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( edges[i] )];
838 if ( !nbElems.empty() )
839 nbSeg2 += Max( nbElems[ SMDSEntity_Edge ], nbElems[ SMDSEntity_Quad_Edge ]);
842 // Get an 2D size of a box side orthogonal to lessComplexSide
843 int nbFaces = 0, nbQuadFace = 0;
844 list< TopoDS_Face > sideFaces;
845 if ( ortoSide->IsComplex() )
846 for ( _QuadFaceGrid::TChildIterator child = ortoSide->GetChildren(); child.more(); )
847 sideFaces.push_back( child.next().GetFace() );
849 sideFaces.push_back( ortoSide->GetFace() );
851 list< TopoDS_Face >::iterator f = sideFaces.begin();
852 for ( ; f != sideFaces.end(); ++f )
854 const vector<int>& nbElems = aResMap[ theMesh.GetSubMesh( *f )];
855 if ( !nbElems.empty() )
857 nbFaces = nbElems[ SMDSEntity_Quadrangle ];
858 nbQuadFace = nbElems[ SMDSEntity_Quad_Quadrangle ];
862 // Fill nb of elements
863 vector<int> aResVec(SMDSEntity_Last,0);
864 int nbSeg3 = ( nbFaces + nbQuadFace ) / nbSeg2;
865 aResVec[SMDSEntity_Node] = (nbSeg1-1) * (nbSeg2-1) * (nbSeg3-1);
866 aResVec[SMDSEntity_Hexa] = nbSeg1 * nbFaces;
867 aResVec[SMDSEntity_Quad_Hexa] = nbSeg1 * nbQuadFace;
869 aResMap.insert( make_pair( theMesh.GetSubMesh(theShape), aResVec ));
875 //================================================================================
877 * \brief constructor of non-initialized _QuadFaceGrid
879 //================================================================================
881 _QuadFaceGrid::_QuadFaceGrid():
882 myReverse(false), myRightBrother(0), myUpBrother(0), myIndexer(0,0), myID(B_UNDEFINED)
886 //================================================================================
888 * \brief Initialization
890 //================================================================================
892 bool _QuadFaceGrid::Init(const TopoDS_Face& f, SMESH_Mesh& mesh)
895 mySides = _FaceSide();
897 myLeftBottomChild = myRightBrother = myUpBrother = 0;
900 //if ( myFace.Orientation() != TopAbs_FORWARD )
903 list< TopoDS_Edge > edges;
904 list< int > nbEdgesInWire;
905 int nbWire = SMESH_Block::GetOrderedEdges (myFace, edges, nbEdgesInWire);
909 list< TopoDS_Edge >::iterator edgeIt = edges.begin();
910 if ( nbEdgesInWire.front() == 4 ) // exactly 4 edges
912 for ( ; edgeIt != edges.end(); ++edgeIt )
913 mySides.AppendSide( _FaceSide( *edgeIt ));
915 else if ( nbEdgesInWire.front() > 4 ) { // more than 4 edges - try to unite some
916 list< TopoDS_Edge > sideEdges;
917 while ( !edges.empty()) {
919 sideEdges.splice( sideEdges.end(), edges, edges.begin());// edges.front()->sideEdges.back()
920 if ( SMESH_Algo::isDegenerated( sideEdges.back() ))
922 while ( !edges.empty() ) {
923 if ( isContinuousMesh( sideEdges.back(), edges.front(), f, mesh )) {
924 sideEdges.splice( sideEdges.end(), edges, edges.begin());
926 else if ( isContinuousMesh( sideEdges.front(), edges.back(), f, mesh )) {
927 sideEdges.splice( sideEdges.begin(), edges, --edges.end());
933 mySides.AppendSide( _FaceSide( sideEdges ));
936 if (mySides.size() != 4)
940 mySides.GetSide( Q_BOTTOM )->SetID( Q_BOTTOM );
941 mySides.GetSide( Q_RIGHT )->SetID( Q_RIGHT );
942 mySides.GetSide( Q_TOP )->SetID( Q_TOP );
943 mySides.GetSide( Q_LEFT )->SetID( Q_LEFT );
949 //================================================================================
951 * \brief Try to unite self with other ordinary face
953 //================================================================================
955 bool _QuadFaceGrid::AddContinuousFace( const _QuadFaceGrid& other,
956 const TopTools_MapOfShape& internalEdges)
958 for ( int i = 0; i < 4; ++i )
960 const _FaceSide& otherSide = other.GetSide( i );
962 if ( mySides.Contain( otherSide, &iMyCommon ))
964 if ( internalEdges.Contains( otherSide.Edge( 0 )))
966 DUMP_VERT("Cont 1", mySides.GetSide(iMyCommon)->FirstVertex());
967 DUMP_VERT("Cont 2", mySides.GetSide(iMyCommon)->LastVertex());
968 DUMP_VERT("Cont 3", otherSide.FirstVertex());
969 DUMP_VERT("Cont 4", otherSide.LastVertex());
971 if ( myChildren.empty() )
973 myChildren.push_back( *this );
976 else // find iMyCommon in myChildren
978 for ( TChildIterator children = GetChildren(); children.more(); ) {
979 const _QuadFaceGrid& child = children.next();
980 if ( child.mySides.Contain( otherSide, &iMyCommon ))
985 // orient new children equally
986 int otherBottomIndex = SMESH_MesherHelper::WrapIndex( i - iMyCommon + 2, 4 );
987 if ( other.IsComplex() )
988 for ( TChildIterator children = other.GetChildren(); children.more(); ) {
989 myChildren.push_back( children.next() );
990 myChildren.back().SetBottomSide( myChildren.back().GetSide( otherBottomIndex ));
993 myChildren.push_back( other );
994 myChildren.back().SetBottomSide( myChildren.back().GetSide( otherBottomIndex ));
997 myLeftBottomChild = 0;
999 // collect vertices in mySides
1000 if ( other.IsComplex() )
1001 for ( TChildIterator children = other.GetChildren(); children.more(); )
1003 const _QuadFaceGrid& child = children.next();
1004 for ( int i = 0; i < 4; ++i )
1005 mySides.AppendSide( child.GetSide(i) );
1008 for ( int i = 0; i < 4; ++i )
1009 mySides.AppendSide( other.GetSide(i) );
1018 //================================================================================
1020 * \brief Try to set the side as bottom hirizontal side
1022 //================================================================================
1024 bool _QuadFaceGrid::SetBottomSide(const _FaceSide& bottom, int* sideIndex)
1026 myLeftBottomChild = myRightBrother = myUpBrother = 0;
1029 if ( myChildren.empty() )
1031 if ( mySides.Contain( bottom, &myBottomIndex )) {
1032 mySides.SetBottomSide( myBottomIndex );
1034 *sideIndex = myBottomIndex;
1040 TChildren::iterator childFace = myChildren.begin(), childEnd = myChildren.end();
1041 for ( ; childFace != childEnd; ++childFace )
1043 if ( childFace->SetBottomSide( bottom, &myBottomIndex ))
1045 TChildren::iterator orientedChild = childFace;
1046 for ( childFace = myChildren.begin(); childFace != childEnd; ++childFace ) {
1047 if ( childFace != orientedChild )
1048 childFace->SetBottomSide( childFace->GetSide( myBottomIndex ));
1051 *sideIndex = myBottomIndex;
1059 //================================================================================
1061 * \brief Return face adjacent to i-th side of this face, (0<i<4)
1063 //================================================================================
1065 _QuadFaceGrid* _QuadFaceGrid::FindAdjacentForSide(int i,
1066 list<_QuadFaceGrid>& faces,
1069 const _FaceSide & iSide = GetSide( i );
1070 list< _QuadFaceGrid >::iterator boxFace = faces.begin();
1071 for ( ; boxFace != faces.end(); ++boxFace )
1073 _QuadFaceGrid* f = & (*boxFace);
1074 if ( f != this && f->SetBottomSide( iSide ))
1075 return f->SetID( id ), f;
1077 return (_QuadFaceGrid*) 0;
1080 //================================================================================
1082 * \brief Return i-th side
1084 //================================================================================
1086 const _FaceSide& _QuadFaceGrid::GetSide(int i) const
1088 if ( myChildren.empty() )
1089 return *mySides.GetSide(i);
1091 _QuadFaceGrid* me = const_cast<_QuadFaceGrid*>(this);
1092 if ( !me->locateChildren() || !myLeftBottomChild )
1093 return *mySides.GetSide(i);
1095 const _QuadFaceGrid* child = myLeftBottomChild;
1101 while ( child->myRightBrother )
1102 child = child->myRightBrother;
1105 while ( child->myUpBrother )
1106 child = child->myUpBrother;
1110 return child->GetSide( i );
1113 //================================================================================
1115 * \brief Reverse edges in order to have them oriented along axes of the unit box
1117 //================================================================================
1119 void _QuadFaceGrid::ReverseEdges()
1121 myReverse = !myReverse;
1124 // if ( !myFace.IsNull() )
1125 // TopAbs::Print(myFace.Orientation(), cout);
1128 if ( myChildren.empty() )
1135 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1136 for ( ; child != childEnd; ++child )
1137 child->ReverseEdges();
1141 //================================================================================
1143 * \brief Load nodes of a mesh
1145 //================================================================================
1147 bool _QuadFaceGrid::LoadGrid( SMESH_Mesh& mesh )
1149 if ( !myChildren.empty() )
1151 // Let child faces load their grids
1152 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1153 for ( ; child != childEnd; ++child ) {
1154 child->SetID( myID );
1155 if ( !child->LoadGrid( mesh ) )
1156 return error( child->GetError() );
1158 // Fill myGrid with nodes of patches
1159 return loadCompositeGrid( mesh );
1162 // ---------------------------------------
1163 // Fill myGrid with nodes bound to myFace
1164 // ---------------------------------------
1166 if ( !myGrid.empty() )
1169 SMESHDS_SubMesh* faceSubMesh = mesh.GetSubMesh( myFace )->GetSubMeshDS();
1170 // check that all faces are quadrangular
1171 SMDS_ElemIteratorPtr fIt = faceSubMesh->GetElements();
1172 while ( fIt->more() )
1173 if ( fIt->next()->NbNodes() % 4 > 0 )
1174 return error("Non-quadrangular mesh faces are not allowed on sides of a composite block");
1176 myIndexer._xSize = 1 + mySides.GetSide( Q_BOTTOM )->GetNbSegments( mesh );
1177 myIndexer._ySize = 1 + mySides.GetSide( Q_LEFT )->GetNbSegments( mesh );
1179 myGrid.resize( myIndexer.size() );
1181 // store nodes bound to the bottom edge
1182 mySides.GetSide( Q_BOTTOM )->StoreNodes( mesh, myGrid, myReverse );
1184 // store the rest nodes row by row
1186 TIDSortedElemSet emptySet, avoidSet;
1187 const SMDS_MeshElement* firstQuad = 0; // most left face above the last row of found nodes
1189 size_t nbFoundNodes = myIndexer._xSize;
1190 while ( nbFoundNodes != myGrid.size() )
1192 // first and last nodes of the last filled row of nodes
1193 const SMDS_MeshNode* n1down = myGrid[ nbFoundNodes - myIndexer._xSize ];
1194 const SMDS_MeshNode* n2down = myGrid[ nbFoundNodes - myIndexer._xSize + 1];
1195 const SMDS_MeshNode* n1downLast = myGrid[ nbFoundNodes-1 ];
1197 // find the first face above the row by the first two left nodes
1205 firstQuad = SMESH_MeshAlgos::FindFaceInSet( n1down, n2down, emptySet, avoidSet);
1206 while ( firstQuad && !faceSubMesh->Contains( firstQuad )) {
1207 avoidSet.insert( firstQuad );
1208 firstQuad = SMESH_MeshAlgos::FindFaceInSet( n1down, n2down, emptySet, avoidSet);
1210 if ( !firstQuad || !faceSubMesh->Contains( firstQuad ))
1211 return error(ERR_LI("Error in _QuadFaceGrid::LoadGrid()"));
1213 // find the node of quad bound to the left geom edge
1214 int i2down = firstQuad->GetNodeIndex( n2down );
1215 const SMDS_MeshNode* n1up = firstQuad->GetNode(( i2down+2 ) % 4 );
1216 myGrid[ nbFoundNodes++ ] = n1up;
1217 // the 4-the node of the first quad
1218 int i1down = firstQuad->GetNodeIndex( n1down );
1219 const SMDS_MeshNode* n2up = firstQuad->GetNode(( i1down+2 ) % 4 );
1220 myGrid[ nbFoundNodes++ ] = n2up;
1224 const SMDS_MeshElement* quad = firstQuad;
1226 // find the rest nodes by remaining faces above the row
1234 while ( n1down != n1downLast )
1237 avoidSet.clear(); avoidSet.insert( quad );
1238 quad = SMESH_MeshAlgos::FindFaceInSet( n1down, n1up, emptySet, avoidSet );
1239 if ( !quad || quad->NbNodes() % 4 > 0)
1240 return error(ERR_LI("Error in _QuadFaceGrid::LoadGrid()"));
1243 if ( quad->GetNode( i1down ) != n1down ) // check already found index
1244 i1down = quad->GetNodeIndex( n1down );
1245 n2up = quad->GetNode(( i1down+2 ) % 4 );
1246 myGrid[ nbFoundNodes++ ] = n2up;
1248 n1down = myGrid[ nbFoundNodes - myIndexer._xSize - 1 ];
1251 avoidSet.clear(); avoidSet.insert( firstQuad );
1253 DumpGrid(); // debug
1258 //================================================================================
1260 * \brief Fill myIJK with normalized parameters of nodes in myGrid
1261 * \param [in] i1 - coordinate index along rows of myGrid
1262 * \param [in] i2 - coordinate index along columns of myGrid
1263 * \param [in] v3 - value of the constant parameter
1265 //================================================================================
1267 void _QuadFaceGrid::ComputeIJK( int i1, int i2, double v3 )
1269 gp_XYZ ijk( v3, v3, v3 );
1270 myIJK.resize( myIndexer.size(), ijk );
1272 const size_t nbCol = myIndexer._xSize;
1273 const size_t nbRow = myIndexer._ySize;
1275 vector< double > len( nbRow );
1277 for ( size_t i = 0; i < nbCol; ++i )
1279 gp_Pnt pPrev = GetXYZ( i, 0 );
1280 for ( size_t j = 1; j < nbRow; ++j )
1282 gp_Pnt p = GetXYZ( i, j );
1283 len[ j ] = len[ j-1 ] + p.Distance( pPrev );
1286 for ( size_t j = 0; j < nbRow; ++j )
1287 GetIJK( i, j ).SetCoord( i2, len[ j ]/len.back() );
1290 len.resize( nbCol );
1291 for ( size_t j = 0; j < nbRow; ++j )
1293 gp_Pnt pPrev = GetXYZ( 0, j );
1294 for ( size_t i = 1; i < nbCol; ++i )
1296 gp_Pnt p = GetXYZ( i, j );
1297 len[ i ] = len[ i-1 ] + p.Distance( pPrev );
1300 for ( size_t i = 0; i < nbCol; ++i )
1301 GetIJK( i, j ).SetCoord( i1, len[ i ]/len.back() );
1305 //================================================================================
1307 * \brief Find out mutual location of children: find their right and up brothers
1309 //================================================================================
1311 bool _QuadFaceGrid::locateChildren()
1313 if ( myLeftBottomChild )
1316 TChildren::iterator child = myChildren.begin(), childEnd = myChildren.end();
1318 // find a child sharing it's first bottom vertex with no other brother
1319 myLeftBottomChild = 0;
1320 for ( ; !myLeftBottomChild && child != childEnd; ++child )
1322 TopoDS_Vertex leftVertex = child->GetSide( Q_BOTTOM ).FirstVertex();
1323 bool sharedVertex = false;
1324 TChildren::iterator otherChild = myChildren.begin();
1325 for ( ; otherChild != childEnd && !sharedVertex; ++otherChild )
1326 if ( otherChild != child )
1327 sharedVertex = otherChild->mySides.Contain( leftVertex );
1328 if ( !sharedVertex ) {
1329 myLeftBottomChild = & (*child);
1330 DUMP_VERT("0 left bottom Vertex: ",leftVertex );
1333 if (!myLeftBottomChild)
1334 return error(ERR_LI("Error in locateChildren()"));
1336 set< _QuadFaceGrid* > notLocatedChilren;
1337 for (child = myChildren.begin() ; child != childEnd; ++child )
1338 notLocatedChilren.insert( & (*child));
1340 // connect myLeftBottomChild to it's right and upper brothers
1341 notLocatedChilren.erase( myLeftBottomChild );
1342 myLeftBottomChild->setBrothers( notLocatedChilren );
1343 if ( !notLocatedChilren.empty() )
1344 return error(ERR_LI("Error in locateChildren()"));
1349 //================================================================================
1351 * \brief Fill myGrid with nodes of patches
1353 //================================================================================
1355 bool _QuadFaceGrid::loadCompositeGrid(SMESH_Mesh& mesh)
1357 // Find out mutual location of children: find their right and up brothers
1358 if ( !locateChildren() )
1361 // Load nodes according to mutual location of children
1364 myIndexer._xSize = 1 + myLeftBottomChild->GetNbHoriSegments(mesh, /*withBrothers=*/true);
1365 myIndexer._ySize = 1 + myLeftBottomChild->GetNbVertSegments(mesh, /*withBrothers=*/true);
1367 myGrid.resize( myIndexer.size() );
1369 int fromX = myReverse ? myIndexer._xSize : 0;
1370 if (!myLeftBottomChild->fillGrid( mesh, myGrid, myIndexer, fromX, 0 ))
1371 return error( myLeftBottomChild->GetError() );
1378 //================================================================================
1380 * \brief Find right an upper brothers among notLocatedBrothers
1382 //================================================================================
1384 void _QuadFaceGrid::setBrothers( set< _QuadFaceGrid* >& notLocatedBrothers )
1386 if ( !notLocatedBrothers.empty() )
1388 // find right brother
1389 TopoDS_Vertex rightVertex = GetSide( Q_BOTTOM ).LastVertex();
1390 DUMP_VERT("1 right bottom Vertex: ",rightVertex );
1391 set< _QuadFaceGrid* >::iterator brIt, brEnd = notLocatedBrothers.end();
1392 for ( brIt = notLocatedBrothers.begin(); brIt != brEnd; ++brIt )
1394 _QuadFaceGrid* brother = *brIt;
1395 TopoDS_Vertex brotherLeftVertex = brother->GetSide( Q_BOTTOM ).FirstVertex();
1396 DUMP_VERT( "brother left bottom: ", brotherLeftVertex );
1397 if ( rightVertex.IsSame( brotherLeftVertex )) {
1398 myRightBrother = brother;
1399 notLocatedBrothers.erase( brIt );
1403 // find upper brother
1404 TopoDS_Vertex upVertex = GetSide( Q_LEFT ).FirstVertex();
1405 DUMP_VERT("1 left up Vertex: ",upVertex);
1406 brIt = notLocatedBrothers.begin(), brEnd = notLocatedBrothers.end();
1407 for ( ; brIt != brEnd; ++brIt )
1409 _QuadFaceGrid* brother = *brIt;
1410 TopoDS_Vertex brotherLeftVertex = brother->GetSide( Q_BOTTOM ).FirstVertex();
1411 DUMP_VERT("brother left bottom: ", brotherLeftVertex);
1412 if ( upVertex.IsSame( brotherLeftVertex )) {
1413 myUpBrother = brother;
1414 notLocatedBrothers.erase( myUpBrother );
1419 if ( myRightBrother )
1420 myRightBrother->setBrothers( notLocatedBrothers );
1422 myUpBrother->setBrothers( notLocatedBrothers );
1426 //================================================================================
1428 * \brief Store nodes of a simple face into grid starting from (x,y) position
1430 //================================================================================
1432 bool _QuadFaceGrid::fillGrid(SMESH_Mesh& theMesh,
1433 vector<const SMDS_MeshNode*> & theGrid,
1434 const _Indexer& theIndexer,
1438 if ( myGrid.empty() && !LoadGrid( theMesh ))
1441 // store my own grid in the global grid
1443 int fromX = myReverse ? theX - myIndexer._xSize: theX;
1445 for ( int i = 0, x = fromX; i < myIndexer._xSize; ++i, ++x )
1446 for ( int j = 0, y = theY; j < myIndexer._ySize; ++j, ++y )
1447 theGrid[ theIndexer( x, y )] = myGrid[ myIndexer( i, j )];
1449 // store grids of my right and upper brothers
1451 if ( myRightBrother )
1456 fromX += myIndexer._xSize - 1;
1457 if ( !myRightBrother->fillGrid( theMesh, theGrid, theIndexer, fromX, theY ))
1458 return error( myRightBrother->GetError() );
1462 if ( !myUpBrother->fillGrid( theMesh, theGrid, theIndexer,
1463 theX, theY + myIndexer._ySize - 1))
1464 return error( myUpBrother->GetError() );
1469 //================================================================================
1471 * \brief Return number of segments on the hirizontal sides
1473 //================================================================================
1475 int _QuadFaceGrid::GetNbHoriSegments(SMESH_Mesh& mesh, bool withBrothers) const
1478 if ( myLeftBottomChild )
1480 nbSegs += myLeftBottomChild->GetNbHoriSegments( mesh, true );
1484 nbSegs = mySides.GetSide( Q_BOTTOM )->GetNbSegments(mesh);
1485 if ( withBrothers && myRightBrother )
1486 nbSegs += myRightBrother->GetNbHoriSegments( mesh, withBrothers );
1491 //================================================================================
1493 * \brief Return number of segments on the vertical sides
1495 //================================================================================
1497 int _QuadFaceGrid::GetNbVertSegments(SMESH_Mesh& mesh, bool withBrothers) const
1500 if ( myLeftBottomChild )
1502 nbSegs += myLeftBottomChild->GetNbVertSegments( mesh, true );
1506 nbSegs = mySides.GetSide( Q_LEFT )->GetNbSegments(mesh);
1507 if ( withBrothers && myUpBrother )
1508 nbSegs += myUpBrother->GetNbVertSegments( mesh, withBrothers );
1513 //================================================================================
1515 * \brief Return edge on the hirizontal bottom sides
1517 //================================================================================
1519 int _QuadFaceGrid::GetHoriEdges(vector<TopoDS_Edge> & edges) const
1521 if ( myLeftBottomChild )
1523 return myLeftBottomChild->GetHoriEdges( edges );
1527 const _FaceSide* bottom = mySides.GetSide( Q_BOTTOM );
1530 TopoDS_Edge e = bottom->Edge( i++ );
1534 edges.push_back( e );
1536 if ( myRightBrother )
1537 myRightBrother->GetHoriEdges( edges );
1539 return edges.size();
1542 //================================================================================
1544 * \brief Return a node by its position
1546 //================================================================================
1548 const SMDS_MeshNode* _QuadFaceGrid::GetNode(int iHori, int iVert) const
1550 return myGrid[ myIndexer( iHori, iVert )];
1553 //================================================================================
1555 * \brief Return node coordinates by its position
1557 //================================================================================
1559 gp_XYZ _QuadFaceGrid::GetXYZ(int iHori, int iVert) const
1561 SMESH_TNodeXYZ xyz = myGrid[ myIndexer( iHori, iVert )];
1565 //================================================================================
1567 * \brief Return normal to the face at vertex v
1569 //================================================================================
1571 bool _QuadFaceGrid::GetNormal( const TopoDS_Vertex& v, gp_Vec& n ) const
1573 if ( myChildren.empty() )
1575 if ( mySides.Contain( v )) {
1577 gp_Pnt2d uv = BRep_Tool::Parameters( v, myFace );
1578 BRepAdaptor_Surface surface( myFace );
1579 gp_Pnt p; gp_Vec d1u, d1v;
1580 surface.D1( uv.X(), uv.Y(), p, d1u, d1v );
1581 n = d1u.Crossed( d1v );
1584 catch (Standard_Failure) {
1591 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1592 for ( ; child != childEnd; ++child )
1593 if ( child->GetNormal( v, n ))
1599 //================================================================================
1601 * \brief Dumps coordinates of grid nodes
1603 //================================================================================
1605 void _QuadFaceGrid::DumpGrid() const
1608 const char* names[] = { "B_BOTTOM", "B_RIGHT", "B_TOP", "B_LEFT", "B_FRONT", "B_BACK" };
1609 cout << "****** Face " << names[ myID ] << endl;
1611 if ( myChildren.empty() || !myGrid.empty() )
1613 cout << "x size: " << myIndexer._xSize << "; y size: " << myIndexer._ySize << endl;
1614 for ( int y = 0; y < myIndexer._ySize; ++y ) {
1615 cout << "-- row " << y << endl;
1616 for ( int x = 0; x < myIndexer._xSize; ++x ) {
1617 const SMDS_MeshNode* n = myGrid[ myIndexer( x, y ) ];
1618 cout << x << " ( " << n->X() << ", " << n->Y() << ", " << n->Z() << " )" << endl;
1624 cout << "Nb children: " << myChildren.size() << endl;
1625 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1626 for ( int i=0; child != childEnd; ++child, ++i ) {
1627 cout << " *** SUBFACE " << i+1 << endl;
1628 ((_QuadFaceGrid&)(*child)).SetID( myID );
1635 //================================================================================
1637 * \brief Dump vertices
1639 //================================================================================
1641 void _QuadFaceGrid::DumpVertices() const
1644 cout << "****** Face ";
1645 const char* names[] = { "B_BOTTOM", "B_RIGHT", "B_TOP", "B_LEFT", "B_FRONT", "B_BACK" };
1646 if ( myID >= B_BOTTOM && myID < B_BACK )
1647 cout << names[ myID ] << endl;
1649 cout << "UNDEFINED" << endl;
1651 if ( myChildren.empty() )
1653 for ( int i = 0; i < 4; ++i )
1655 cout << " Side "; mySides.GetSide( i )->Dump();
1660 cout << "-- Nb children: " << myChildren.size() << endl;
1661 TChildren::const_iterator child = myChildren.begin(), childEnd = myChildren.end();
1662 for ( int i=0; child != childEnd; ++child, ++i ) {
1663 cout << " *** SUBFACE " << i+1 << endl;
1664 ((_QuadFaceGrid&)(*child)).SetID( myID );
1665 child->DumpVertices();
1671 //=======================================================================
1672 //function : _FaceSide
1673 //purpose : copy constructor
1674 //=======================================================================
1676 _FaceSide::_FaceSide(const _FaceSide& other)
1678 myEdge = other.myEdge;
1679 myChildren = other.myChildren;
1680 myNbChildren = other.myNbChildren;
1681 myVertices.Assign( other.myVertices );
1685 //================================================================================
1687 * \brief Construct a face side of one edge
1689 //================================================================================
1691 _FaceSide::_FaceSide(const TopoDS_Edge& edge):
1692 myEdge( edge ), myNbChildren(0)
1694 if ( !edge.IsNull() )
1695 for ( TopExp_Explorer exp( edge, TopAbs_VERTEX ); exp.More(); exp.Next() )
1696 //myVertices.insert( ptr ( exp.Current() ));
1697 myVertices.Add( exp.Current() );
1700 //================================================================================
1702 * \brief Construct a face side of several edges
1704 //================================================================================
1706 _FaceSide::_FaceSide(const list<TopoDS_Edge>& edges):
1709 list<TopoDS_Edge>::const_iterator edge = edges.begin(), eEnd = edges.end();
1710 for ( ; edge != eEnd; ++edge ) {
1711 myChildren.push_back( _FaceSide( *edge ));
1713 myVertices.Add( myChildren.back().FirstVertex() );
1714 myVertices.Add( myChildren.back().LastVertex() );
1715 myChildren.back().SetID( Q_CHILD ); // not to splice them
1719 //=======================================================================
1720 //function : GetSide
1722 //=======================================================================
1724 _FaceSide* _FaceSide::GetSide(const int i)
1726 if ( i >= myNbChildren )
1729 list< _FaceSide >::iterator side = myChildren.begin();
1731 std::advance( side, i );
1735 //=======================================================================
1736 //function : GetSide
1738 //=======================================================================
1740 const _FaceSide* _FaceSide::GetSide(const int i) const
1742 return const_cast< _FaceSide* >(this)->GetSide(i);
1745 //=======================================================================
1746 //function : NbVertices
1747 //purpose : return nb of vertices in the side
1748 //=======================================================================
1750 int _FaceSide::NbVertices() const
1752 if ( myChildren.empty() )
1753 return myVertices.Extent();
1755 return myNbChildren + 1;
1758 //=======================================================================
1759 //function : NbCommonVertices
1760 //purpose : Returns number of my vertices common with the given ones
1761 //=======================================================================
1763 int _FaceSide::NbCommonVertices( const TopTools_MapOfShape& VV ) const
1766 TopTools_MapIteratorOfMapOfShape vIt ( myVertices );
1767 for ( ; vIt.More(); vIt.Next() )
1768 nbCommon += ( VV.Contains( vIt.Key() ));
1773 //=======================================================================
1774 //function : FirstVertex
1776 //=======================================================================
1778 TopoDS_Vertex _FaceSide::FirstVertex() const
1780 if ( myChildren.empty() )
1781 return TopExp::FirstVertex( myEdge, Standard_True );
1783 return myChildren.front().FirstVertex();
1786 //=======================================================================
1787 //function : LastVertex
1789 //=======================================================================
1791 TopoDS_Vertex _FaceSide::LastVertex() const
1793 if ( myChildren.empty() )
1794 return TopExp::LastVertex( myEdge, Standard_True );
1796 return myChildren.back().LastVertex();
1799 //=======================================================================
1802 //=======================================================================
1804 TopoDS_Vertex _FaceSide::Vertex(int i) const
1806 if ( myChildren.empty() )
1807 return i ? LastVertex() : FirstVertex();
1809 if ( i >= myNbChildren )
1810 return myChildren.back().LastVertex();
1812 return GetSide(i)->FirstVertex();
1815 //================================================================================
1817 * \brief Return i-the zero-based edge of the side
1819 //================================================================================
1821 TopoDS_Edge _FaceSide::Edge(int i) const
1823 if ( i == 0 && !myEdge.IsNull() )
1826 if ( const _FaceSide* iSide = GetSide( i ))
1827 return iSide->myEdge;
1829 return TopoDS_Edge();
1832 //=======================================================================
1833 //function : Contain
1835 //=======================================================================
1837 bool _FaceSide::Contain( const _FaceSide& side, int* which ) const
1839 if ( !which || myChildren.empty() )
1844 TopTools_MapIteratorOfMapOfShape vIt ( side.myVertices );
1845 for ( ; vIt.More(); vIt.Next() )
1846 nbCommon += ( myVertices.Contains( vIt.Key() ));
1847 return (nbCommon > 1);
1849 list< _FaceSide >::const_iterator mySide = myChildren.begin(), sideEnd = myChildren.end();
1850 for ( int i = 0; mySide != sideEnd; ++mySide, ++i ) {
1851 if ( mySide->Contain( side )) {
1859 //=======================================================================
1860 //function : Contain
1862 //=======================================================================
1864 bool _FaceSide::Contain( const TopoDS_Vertex& vertex ) const
1866 return myVertices.Contains( vertex );
1869 //=======================================================================
1870 //function : AppendSide
1872 //=======================================================================
1874 void _FaceSide::AppendSide( const _FaceSide& side )
1876 if ( !myEdge.IsNull() )
1878 myChildren.push_back( *this );
1882 myChildren.push_back( side );
1884 TopTools_MapIteratorOfMapOfShape vIt ( side.myVertices );
1885 for ( ; vIt.More(); vIt.Next() )
1886 myVertices.Add( vIt.Key() );
1889 myChildren.back().SetID( EQuadSides( myNbChildren-1 ));
1892 //=======================================================================
1893 //function : SetBottomSide
1895 //=======================================================================
1897 void _FaceSide::SetBottomSide( int i )
1899 if ( i > 0 && myID == Q_PARENT ) {
1900 list< _FaceSide >::iterator sideEnd, side = myChildren.begin();
1901 std::advance( side, i );
1902 myChildren.splice( myChildren.begin(), myChildren, side, myChildren.end() );
1904 side = myChildren.begin(), sideEnd = myChildren.end();
1905 for ( int i = 0; side != sideEnd; ++side, ++i ) {
1906 side->SetID( EQuadSides(i) );
1907 side->SetBottomSide(i);
1912 //=======================================================================
1913 //function : GetNbSegments
1915 //=======================================================================
1917 int _FaceSide::GetNbSegments(SMESH_Mesh& mesh) const
1920 if ( myChildren.empty() )
1922 nb = mesh.GetSubMesh(myEdge)->GetSubMeshDS()->NbElements();
1926 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1927 for ( ; side != sideEnd; ++side )
1928 nb += side->GetNbSegments(mesh);
1933 //=======================================================================
1934 //function : StoreNodes
1936 //=======================================================================
1938 bool _FaceSide::StoreNodes(SMESH_Mesh& mesh,
1939 vector<const SMDS_MeshNode*>& myGrid,
1942 list< TopoDS_Edge > edges;
1943 if ( myChildren.empty() )
1945 edges.push_back( myEdge );
1949 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
1950 for ( ; side != sideEnd; ++side )
1952 edges.push_front( side->myEdge );
1954 edges.push_back ( side->myEdge );
1957 list< TopoDS_Edge >::iterator edge = edges.begin(), eEnd = edges.end();
1958 for ( ; edge != eEnd; ++edge )
1960 map< double, const SMDS_MeshNode* > nodes;
1961 bool ok = SMESH_Algo::GetSortedNodesOnEdge( mesh.GetMeshDS(),
1963 /*ignoreMediumNodes=*/true,
1965 if ( !ok ) return false;
1967 // skip nodes on VERTEXes not included in faces
1968 if ( !nodes.begin()->second->GetInverseElementIterator(SMDSAbs_Face)->more() )
1969 nodes.erase( nodes.begin() );
1970 if ( !nodes.empty() &&
1971 !nodes.rbegin()->second->GetInverseElementIterator(SMDSAbs_Face)->more() )
1972 nodes.erase( --nodes.end() );
1974 bool forward = ( edge->Orientation() == TopAbs_FORWARD );
1975 if ( reverse ) forward = !forward;
1978 map< double, const SMDS_MeshNode* >::iterator u_node, nEnd = nodes.end();
1979 for ( u_node = nodes.begin(); u_node != nEnd; ++u_node )
1980 myGrid[ nbNodes++ ] = u_node->second;
1984 map< double, const SMDS_MeshNode* >::reverse_iterator u_node, nEnd = nodes.rend();
1985 for ( u_node = nodes.rbegin(); u_node != nEnd; ++u_node )
1986 myGrid[ nbNodes++ ] = u_node->second;
1988 nbNodes--; // node on vertex present in two adjacent edges
1993 //=======================================================================
1995 //purpose : dump end vertices
1996 //=======================================================================
1998 void _FaceSide::Dump() const
2000 if ( myChildren.empty() )
2002 const char* sideNames[] = { "Q_BOTTOM", "Q_RIGHT", "Q_TOP", "Q_LEFT", "Q_CHILD", "Q_PARENT" };
2003 if ( myID >= Q_BOTTOM && myID < Q_PARENT )
2004 cout << sideNames[ myID ] << endl;
2006 cout << "<UNDEFINED ID>" << endl;
2007 TopoDS_Vertex f = FirstVertex();
2008 TopoDS_Vertex l = LastVertex();
2009 gp_Pnt pf = BRep_Tool::Pnt(f);
2010 gp_Pnt pl = BRep_Tool::Pnt(l);
2011 cout << "\t ( "<< ptr( f ) << " - " << ptr( l )<< " )"
2012 << "\t ( "<< pf.X()<<", "<<pf.Y()<<", "<<pf.Z()<<" ) - "
2013 << " ( "<< pl.X()<<", "<<pl.Y()<<", "<<pl.Z()<<" )"<<endl;
2017 list< _FaceSide >::const_iterator side = myChildren.begin(), sideEnd = myChildren.end();
2018 for ( ; side != sideEnd; ++side ) {