1 // Copyright (C) 2009-2011 CEA/DEN, EDF R&D
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.
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 // File : SMESH_HexaFromSkin_3D.cxx
21 // Created : Wed Jan 27 12:28:07 2010
22 // Author : Edward AGAPOV (eap)
24 #include "HEXABLOCKPlugin_FromSkin_3D.hxx"
26 #include "SMDS_VolumeOfNodes.hxx"
27 #include "SMDS_VolumeTool.hxx"
28 #include "SMESH_Block.hxx"
29 #include "SMESH_MesherHelper.hxx"
33 //#include "utilities.h"
36 // Define error message and _MYDEBUG_ if needed
38 #define BAD_MESH_ERR \
39 error(SMESH_Comment("Can't detect block-wise structure of the input 2D mesh.\n" \
40 __FILE__ ":" )<<__LINE__)
43 #define BAD_MESH_ERR \
44 error(SMESH_Comment("Can't detect block-wise structure of the input 2D mesh"))
49 #define _DUMP_(msg) cout << msg << endl
56 static int MYDEBUG = HEXA_NS::on_debug ();
58 static int MYDEBUG = 0;
64 enum EBoxSides //!< sides of the block
66 B_BOTTOM=0, B_RIGHT, B_TOP, B_LEFT, B_FRONT, B_BACK, NB_BLOCK_SIDES
69 const char* SBoxSides[] = //!< names of block sides
71 "BOTTOM", "RIGHT", "TOP", "LEFT", "FRONT", "BACK", "UNDEFINED"
74 enum EQuadEdge //!< edges of quadrangle side
76 Q_BOTTOM = 0, Q_RIGHT, Q_TOP, Q_LEFT, NB_QUAD_SIDES
80 //================================================================================
82 * \brief return logical coordinates (i.e. min or max) of ends of edge
84 //================================================================================
86 bool getEdgeEnds(EQuadEdge edge, bool& xMax1, bool& yMax1, bool& xMax2, bool& yMax2 )
88 xMax1=0, yMax1=0, xMax2=1, yMax2=1;
91 case Q_BOTTOM: yMax2 = 0; break;
92 case Q_RIGHT: xMax1 = 1; break;
93 case Q_TOP: yMax1 = 1; break;
94 case Q_LEFT: xMax2 = 0; break;
101 //================================================================================
103 * \brief return true if a node is at block corner
105 * This check is valid for simple cases only
107 //================================================================================
109 bool isCornerNode( const SMDS_MeshNode* n )
111 int nbF = n ? n->NbInverseElements( SMDSAbs_Face ) : 1;
115 set<const SMDS_MeshNode*> nodesInInverseFaces;
116 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face );
117 while ( fIt->more() )
119 const SMDS_MeshElement* face = fIt->next();
120 nodesInInverseFaces.insert( face->begin_nodes(), face->end_nodes() );
123 return nodesInInverseFaces.size() != ( 6 + (nbF/2-1)*3 );
126 //================================================================================
128 * \brief check element type
130 //================================================================================
132 bool isQuadrangle(const SMDS_MeshElement* e)
134 return ( e && e->NbCornerNodes() == 4 );
137 //================================================================================
139 * \brief return opposite node of a quadrangle face
141 //================================================================================
143 const SMDS_MeshNode* oppositeNode(const SMDS_MeshElement* quad, int iNode)
145 return quad->GetNode( (iNode+2) % 4 );
148 //================================================================================
150 * \brief Convertor of a pair of integers to a sole index
155 _Indexer( int xSize=0, int ySize=0 ): _xSize(xSize), _ySize(ySize) {}
156 int size() const { return _xSize * _ySize; }
157 int operator()(int x, int y) const { return y * _xSize + x; }
159 //================================================================================
161 * \brief Oriented convertor of a pair of integers to a sole index
163 class _OrientedIndexer : public _Indexer
166 enum OriFlags //!< types of block side orientation
168 REV_X = 1, REV_Y = 2, SWAP_XY = 4, MAX_ORI = REV_X|REV_Y|SWAP_XY
170 _OrientedIndexer( const _Indexer& indexer, const int oriFlags ):
171 _Indexer( indexer._xSize, indexer._ySize ),
172 _xSize (indexer._xSize), _ySize(indexer._ySize),
173 _xRevFun((oriFlags & REV_X) ? & reverse : & lazy),
174 _yRevFun((oriFlags & REV_Y) ? & reverse : & lazy),
175 _swapFun((oriFlags & SWAP_XY ) ? & swap : & lazy)
177 (*_swapFun)( _xSize, _ySize );
179 //!< Return index by XY
180 int operator()(int x, int y) const
182 (*_xRevFun)( x, const_cast<int&>( _xSize ));
183 (*_yRevFun)( y, const_cast<int&>( _ySize ));
185 return _Indexer::operator()(x,y);
187 //!< Return index for a corner
188 int corner(bool xMax, bool yMax) const
190 int x = xMax, y = yMax, size = 2;
191 (*_xRevFun)( x, size );
192 (*_yRevFun)( y, size );
194 return _Indexer::operator()(x ? _Indexer::_xSize-1 : 0 , y ? _Indexer::_ySize-1 : 0);
196 int xSize() const { return _xSize; }
197 int ySize() const { return _ySize; }
202 typedef void (*TFun)(int& x, int& y);
203 TFun _xRevFun, _yRevFun, _swapFun;
205 static void lazy (int&, int&) {}
206 static void reverse(int& x, int& size) { x = size - x - 1; }
207 static void swap (int& x, int& y) { std::swap(x,y); }
209 //================================================================================
211 * \brief Structure corresponding to the meshed side of block
215 vector<const SMDS_MeshNode*> _grid;
217 int _nbBlocksExpected;
220 #ifdef _DEBUG_ // want to get SIGSEGV in case of invalid index
221 #define _grid_access_(pobj, i) pobj->_grid[ ((i) < pobj->_grid.size()) ? i : int(1e100)]
223 #define _grid_access_(pobj, i) pobj->_grid[ i ]
225 //!< Return node at XY
226 const SMDS_MeshNode* getNode(int x, int y) const { return _grid_access_(this, _index( x,y ));}
228 void setNode(int x, int y, const SMDS_MeshNode* n) { _grid_access_(this, _index( x,y )) = n; }
230 SMESH_OrientedLink getEdge(EQuadEdge edge) const
232 bool x1, y1, x2, y2; getEdgeEnds( edge, x1, y1, x2, y2 );
233 return SMESH_OrientedLink( getCornerNode ( x1, y1 ), getCornerNode( x2, y2 ));
235 //!< Return a corner node
236 const SMDS_MeshNode* getCornerNode(bool isXMax, bool isYMax) const
238 return getNode( isXMax ? _index._xSize-1 : 0 , isYMax ? _index._ySize-1 : 0 );
240 const SMDS_MeshElement* getCornerFace(const SMDS_MeshNode* cornerNode) const;
241 //!< True if all blocks this side belongs to have been found
242 bool isBound() const { return _nbBlocksExpected <= _nbBlocksFound; }
243 //!< Return coordinates of node at XY
244 gp_XYZ getXYZ(int x, int y) const { return SMESH_TNodeXYZ( getNode( x, y )); }
245 //!< Return gravity center of the four corners and the middle node
250 getXYZ( _index._xSize-1, 0 ) +
251 getXYZ( 0, _index._ySize-1 ) +
252 getXYZ( _index._xSize-1, _index._ySize-1 ) +
253 getXYZ( _index._xSize/2, _index._ySize/2 );
256 //!< Return number of mesh faces
257 int getNbFaces() const { return (_index._xSize-1) * (_index._ySize-1); }
259 //================================================================================
261 * \brief _BlockSide with changed orientation
263 struct _OrientedBlockSide
266 _OrientedIndexer _index;
268 _OrientedBlockSide( _BlockSide* side=0, const int oriFlags=0 ):
269 _side(side), _index(side ? side->_index : _Indexer(), oriFlags ) {}
270 //!< return coordinates by XY
271 gp_XYZ xyz(int x, int y) const
273 return SMESH_TNodeXYZ( _grid_access_(_side, _index( x, y )) );
275 //!< safely return a node by XY
276 const SMDS_MeshNode* node(int x, int y) const
278 int i = _index( x, y );
279 return ( i < 0 || i >= _side->_grid.size()) ? 0 : _side->_grid[i];
282 SMESH_OrientedLink edge(EQuadEdge edge) const
284 bool x1, y1, x2, y2; getEdgeEnds( edge, x1, y1, x2, y2 );
285 return SMESH_OrientedLink( cornerNode ( x1, y1 ), cornerNode( x2, y2 ));
287 //!< Return a corner node
288 const SMDS_MeshNode* cornerNode(bool isXMax, bool isYMax) const
290 return _grid_access_(_side, _index.corner( isXMax, isYMax ));
292 //!< return its size in nodes
293 int getHoriSize() const { return _index.xSize(); }
294 int getVertSize() const { return _index.ySize(); }
295 //!< True if _side has been initialized
296 operator bool() const { return _side; }
297 //! Direct access to _side
298 const _BlockSide* operator->() const { return _side; }
299 _BlockSide* operator->() { return _side; }
301 //================================================================================
303 * \brief Meshed skin of block
307 _OrientedBlockSide _side[6]; // 6 sides of a sub-block
308 set<const SMDS_MeshNode*> _corners;
310 const _OrientedBlockSide& getSide(int i) const { return _side[i]; }
311 bool setSide( int i, const _OrientedBlockSide& s)
313 if (( _side[i] = s ))
315 _corners.insert( s.cornerNode(0,0));
316 _corners.insert( s.cornerNode(1,0));
317 _corners.insert( s.cornerNode(0,1));
318 _corners.insert( s.cornerNode(1,1));
322 void clear() { for (int i=0;i<6;++i) _side[i]=0; _corners.clear(); }
323 bool hasSide( const _OrientedBlockSide& s) const
325 if ( s ) for (int i=0;i<6;++i) if ( _side[i] && _side[i]._side == s._side ) return true;
328 int nbSides() const { int n=0; for (int i=0;i<6;++i) if ( _side[i] ) ++n; return n; }
329 bool isValid() const;
331 //================================================================================
333 * \brief Skin mesh possibly containing several meshed blocks
339 int findBlocks(SMESH_Mesh& mesh);
340 //!< return i-th block
341 const _Block& getBlock(int i) const { return _blocks[i]; }
342 //!< return error description
343 const SMESH_Comment& error() const { return _error; }
346 bool fillSide( _BlockSide& side,
347 const SMDS_MeshElement* cornerQuad,
348 const SMDS_MeshNode* cornerNode);
349 bool fillRowsUntilCorner(const SMDS_MeshElement* quad,
350 const SMDS_MeshNode* n1,
351 const SMDS_MeshNode* n2,
352 vector<const SMDS_MeshNode*>& verRow1,
353 vector<const SMDS_MeshNode*>& verRow2,
355 _OrientedBlockSide findBlockSide( EBoxSides startBlockSide,
356 EQuadEdge sharedSideEdge1,
357 EQuadEdge sharedSideEdge2,
358 bool withGeometricAnalysis,
359 set< _BlockSide* >& sidesAround);
360 //!< update own data and data of the side bound to block
361 void setSideBoundToBlock( _BlockSide& side )
363 if ( side._nbBlocksFound++, side.isBound() )
364 for ( int e = 0; e < int(NB_QUAD_SIDES); ++e )
365 _edge2sides[ side.getEdge( (EQuadEdge) e ) ].erase( &side );
367 //!< store reason of error
368 int error(const SMESH_Comment& reason) { _error = reason; return 0; }
370 SMESH_Comment _error;
372 list< _BlockSide > _allSides;
373 vector< _Block > _blocks;
375 //map< const SMDS_MeshNode*, set< _BlockSide* > > _corner2sides;
376 map< SMESH_OrientedLink, set< _BlockSide* > > _edge2sides;
379 //================================================================================
381 * \brief Find and return number of submeshes corresponding to blocks
383 //================================================================================
385 int _Skin::findBlocks(SMESH_Mesh& mesh)
387 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
389 // Find a node at any block corner
391 SMDS_NodeIteratorPtr nIt = meshDS->nodesIterator(/*idInceasingOrder=*/true);
392 if ( !nIt->more() ) return error("Empty mesh");
394 const SMDS_MeshNode* nCorner = 0;
395 while ( nIt->more() )
397 nCorner = nIt->next();
398 if ( isCornerNode( nCorner ))
406 // --------------------------------------------------------------------
407 // Find all block sides starting from mesh faces sharing the corner node
408 // --------------------------------------------------------------------
410 int nbFacesOnSides = 0;
411 TIDSortedElemSet cornerFaces; // corner faces of found _BlockSide's
412 list< const SMDS_MeshNode* > corners( 1, nCorner );
413 list< const SMDS_MeshNode* >::iterator corner = corners.begin();
414 while ( corner != corners.end() )
416 SMDS_ElemIteratorPtr faceIt = (*corner)->GetInverseElementIterator( SMDSAbs_Face );
417 while ( faceIt->more() )
419 const SMDS_MeshElement* face = faceIt->next();
420 if ( !cornerFaces.insert( face ).second )
421 continue; // already loaded block side
423 if ( !isQuadrangle( face ))
424 return error("Non-quadrangle elements in the input mesh");
426 if ( _allSides.empty() || !_allSides.back()._grid.empty() )
427 _allSides.push_back( _BlockSide() );
429 _BlockSide& side = _allSides.back();
430 if ( !fillSide( side, face, *corner ) )
432 if ( !_error.empty() )
437 for ( int isXMax = 0; isXMax < 2; ++isXMax )
438 for ( int isYMax = 0; isYMax < 2; ++isYMax )
440 const SMDS_MeshNode* nCorner = side.getCornerNode(isXMax,isYMax );
441 corners.push_back( nCorner );
442 cornerFaces.insert( side.getCornerFace( nCorner ));
444 for ( int e = 0; e < int(NB_QUAD_SIDES); ++e )
445 _edge2sides[ side.getEdge( (EQuadEdge) e ) ].insert( &side );
447 nbFacesOnSides += side.getNbFaces();
452 // find block sides of other domains if any
453 if ( corner == corners.end() && nbFacesOnSides < mesh.NbQuadrangles() )
455 while ( nIt->more() )
457 nCorner = nIt->next();
458 if ( isCornerNode( nCorner ))
459 corner = corners.insert( corner, nCorner );
461 nbFacesOnSides = mesh.NbQuadrangles();
465 if ( _allSides.empty() )
467 if ( _allSides.back()._grid.empty() )
468 _allSides.pop_back();
469 _DUMP_("Nb detected sides "<< _allSides.size());
471 // ---------------------------
472 // Organize sides into blocks
473 // ---------------------------
475 // analyse sharing of sides by blocks and sort sides by nb of adjacent sides
476 int nbBlockSides = 0; // total nb of block sides taking into account their sharing
477 multimap<int, _BlockSide* > sortedSides;
479 list < _BlockSide >::iterator sideIt = _allSides.begin();
480 for ( ; sideIt != _allSides.end(); ++sideIt )
482 _BlockSide& side = *sideIt;
483 bool isSharedSide = true;
485 for ( int e = 0; e < int(NB_QUAD_SIDES) && isSharedSide; ++e )
487 int nbAdj = _edge2sides[ side.getEdge( (EQuadEdge) e ) ].size();
489 isSharedSide = ( nbAdj > 2 );
491 side._nbBlocksFound = 0;
492 side._nbBlocksExpected = isSharedSide ? 2 : 1;
493 nbBlockSides += side._nbBlocksExpected;
494 sortedSides.insert( make_pair( nbAdjacent, & side ));
498 // find sides of each block
500 while ( nbBlockSides >= 6 )
502 // get any side not bound to all blocks it belongs to
503 multimap<int, _BlockSide*>::iterator i_side = sortedSides.begin();
504 while ( i_side != sortedSides.end() && i_side->second->isBound())
507 // start searching for block sides from the got side
509 if ( _blocks.empty() || _blocks.back()._side[B_FRONT] )
510 _blocks.resize( _blocks.size() + 1 );
512 _Block& block = _blocks.back();
513 block.setSide( B_FRONT, i_side->second );
514 setSideBoundToBlock( *i_side->second );
517 // edges of adjacent sides of B_FRONT corresponding to front's edges
518 EQuadEdge edgeOfFront[4] = { Q_BOTTOM, Q_RIGHT, Q_TOP, Q_LEFT };
519 EQuadEdge edgeOfAdj [4] = { Q_BOTTOM, Q_LEFT, Q_BOTTOM, Q_LEFT };
520 // first find all sides detectable w/o advanced analysis,
521 // then repeat the search, which then may pass without advanced analysis
522 set< _BlockSide* > sidesAround;
523 for ( int advAnalys = 0; advAnalys < 2; ++advAnalys )
525 // try to find 4 sides adjacent to a FRONT side
526 for ( int i = 0; (ok || !advAnalys) && i < NB_QUAD_SIDES; ++i )
527 if ( !block._side[i] )
528 ok = block.setSide( i, findBlockSide( B_FRONT, edgeOfFront[i], edgeOfAdj[i],
529 advAnalys, sidesAround));
530 // try to find a BACK side by a TOP one
531 if ( ok || !advAnalys)
532 if ( !block._side[B_BACK] && block._side[B_TOP] )
533 ok = block.setSide( B_BACK, findBlockSide( B_TOP, Q_TOP, Q_TOP,
534 advAnalys, sidesAround ));
535 if ( !advAnalys ) ok = true;
537 ok = block.isValid();
540 // check if just found block is same as one of previously found blocks
542 for ( int i = 1; i < _blocks.size() && !isSame; ++i )
543 isSame = ( block._corners == _blocks[i-1]._corners );
547 // count the found sides
548 _DUMP_(endl << "** Block " << _blocks.size() << " valid: " << block.isValid());
549 for (int i = 0; i < NB_BLOCK_SIDES; ++i )
551 _DUMP_("\tSide "<< SBoxSides[i] <<" "<< block._side[ i ]._side);
552 if ( block._side[ i ] )
554 if ( ok && i != B_FRONT)
556 setSideBoundToBlock( *block._side[ i ]._side );
559 _DUMP_("\t corners "<<
560 block._side[ i ].cornerNode(0,0)->GetID() << ", " <<
561 block._side[ i ].cornerNode(1,0)->GetID() << ", " <<
562 block._side[ i ].cornerNode(1,1)->GetID() << ", " <<
563 block._side[ i ].cornerNode(0,1)->GetID() << ", "<<endl);
567 _DUMP_("\t not found"<<endl);
575 _DUMP_("Nb found blocks "<< nbBlocks <<endl);
577 if ( nbBlocks == 0 && _error.empty() )
583 //================================================================================
585 * \brief Fill block side data starting from its corner quadrangle
587 //================================================================================
589 bool _Skin::fillSide( _BlockSide& side,
590 const SMDS_MeshElement* cornerQuad,
591 const SMDS_MeshNode* nCorner)
593 // Find out size of block side mesured in nodes and by the way find two rows
594 // of nodes in two directions.
597 const SMDS_MeshElement* firstQuad = cornerQuad;
599 // get a node on block edge
600 int iCorner = firstQuad->GetNodeIndex( nCorner );
601 const SMDS_MeshNode* nOnEdge = firstQuad->GetNode( (iCorner+1) % 4);
603 // find out size of block side
604 vector<const SMDS_MeshNode*> horRow1, horRow2, verRow1, verRow2;
605 if ( !fillRowsUntilCorner( firstQuad, nCorner, nOnEdge, horRow1, horRow2, true ) ||
606 !fillRowsUntilCorner( firstQuad, nCorner, nOnEdge, verRow1, verRow2, false ))
608 nbX = horRow1.size(), nbY = verRow1.size();
611 side._index._xSize = horRow1.size();
612 side._index._ySize = verRow1.size();
613 side._grid.resize( side._index.size(), NULL );
615 for ( x = 0; x < horRow1.size(); ++x )
617 side.setNode( x, 0, horRow1[x] );
618 side.setNode( x, 1, horRow2[x] );
620 for ( y = 0; y < verRow1.size(); ++y )
622 side.setNode( 0, y, verRow1[y] );
623 side.setNode( 1, y, verRow2[y] );
626 // Find the rest nodes
628 y = 1; // y of the row to fill
629 TIDSortedElemSet emptySet, avoidSet;
632 // get next firstQuad in the next row of quadrangles
637 // o---o o o o o <- found nodes
640 int i1down, i2down, i2up;
641 const SMDS_MeshNode* n1down = side.getNode( 0, y-1 );
642 const SMDS_MeshNode* n2down = side.getNode( 1, y-1 );
643 avoidSet.clear(); avoidSet.insert( firstQuad );
644 firstQuad = SMESH_MeshEditor::FindFaceInSet( n1down, n2down, emptySet, avoidSet,
646 if ( !isQuadrangle( firstQuad ))
649 const SMDS_MeshNode* n2up = oppositeNode( firstQuad, i1down );
650 avoidSet.clear(); avoidSet.insert( firstQuad );
652 // find the rest nodes in the y-th row by faces in the row
657 const SMDS_MeshElement* quad = SMESH_MeshEditor::FindFaceInSet( n2up, n2down, emptySet,
658 avoidSet, &i2up, &i2down);
659 if ( !isQuadrangle( quad ))
662 n2up = oppositeNode( quad, i2down );
663 n2down = oppositeNode( quad, i2up );
664 avoidSet.clear(); avoidSet.insert( quad );
666 side.setNode( x, y, n2up );
670 // check side validity
672 side.getCornerFace( side.getCornerNode( 0, 0 )) &&
673 side.getCornerFace( side.getCornerNode( 1, 0 )) &&
674 side.getCornerFace( side.getCornerNode( 0, 1 )) &&
675 side.getCornerFace( side.getCornerNode( 1, 1 ));
680 //================================================================================
682 * \brief Return true if it's possible to make a loop over corner2Sides starting
685 //================================================================================
687 bool isClosedChainOfSides( _BlockSide* startSide,
688 map< const SMDS_MeshNode*, list< _BlockSide* > > & corner2Sides )
690 // get start and end nodes
691 const SMDS_MeshNode *n1 = 0, *n2 = 0, *n;
692 for ( int y = 0; y < 2; ++y )
693 for ( int x = 0; x < 2; ++x )
695 n = startSide->getCornerNode(x,y);
696 if ( !corner2Sides.count( n )) continue;
702 if ( !n2 ) return false;
704 map< const SMDS_MeshNode*, list< _BlockSide* > >::iterator
705 c2sides = corner2Sides.find( n1 );
706 if ( c2sides == corner2Sides.end() ) return false;
708 int nbChainLinks = 1;
710 _BlockSide* prevSide = startSide;
713 // get the next side sharing n
714 list< _BlockSide* > & sides = c2sides->second;
715 _BlockSide* nextSide = ( sides.back() == prevSide ? sides.front() : sides.back() );
716 if ( nextSide == prevSide ) return false;
718 // find the next corner of the nextSide being in corner2Sides
721 for ( int y = 0; y < 2 && !n; ++y )
722 for ( int x = 0; x < 2; ++x )
724 n = nextSide->getCornerNode(x,y);
725 c2sides = corner2Sides.find( n );
726 if ( n == n1 || c2sides == corner2Sides.end() )
731 if ( !n ) return false;
737 return ( n == n2 && nbChainLinks == NB_QUAD_SIDES );
740 //================================================================================
742 * \brief Try to find a block side adjacent to the given side by given edge
744 //================================================================================
746 _OrientedBlockSide _Skin::findBlockSide( EBoxSides startBlockSide,
747 EQuadEdge sharedSideEdge1,
748 EQuadEdge sharedSideEdge2,
749 bool withGeometricAnalysis,
750 set< _BlockSide* >& sidesAround)
752 _Block& block = _blocks.back();
753 _OrientedBlockSide& side1 = block._side[ startBlockSide ];
755 // get corner nodes of the given block edge
756 SMESH_OrientedLink edge = side1.edge( sharedSideEdge1 );
757 const SMDS_MeshNode* n1 = edge.node1();
758 const SMDS_MeshNode* n2 = edge.node2();
759 if ( edge._reversed ) swap( n1, n2 );
761 // find all sides sharing both nodes n1 and n2
762 set< _BlockSide* > sidesOnEdge = _edge2sides[ edge ]; // copy a set
764 // exclude loaded sides of block from sidesOnEdge
765 for (int i = 0; i < NB_BLOCK_SIDES; ++i )
766 if ( block._side[ i ] )
767 sidesOnEdge.erase( block._side[ i ]._side );
769 int nbSidesOnEdge = sidesOnEdge.size();
770 _DUMP_("nbSidesOnEdge "<< nbSidesOnEdge << " " << n1->GetID() << "-" << n2->GetID() );
771 if ( nbSidesOnEdge == 0 )
774 _BlockSide* foundSide = 0;
775 if ( nbSidesOnEdge == 1 )
777 foundSide = *sidesOnEdge.begin();
781 set< _BlockSide* >::iterator sideIt = sidesOnEdge.begin();
782 int nbLoadedSides = block.nbSides();
783 if ( nbLoadedSides > 1 )
785 // Find the side having more than 2 corners common with already loaded sides
786 for (; !foundSide && sideIt != sidesOnEdge.end(); ++sideIt )
788 _BlockSide* sideI = *sideIt;
789 int nbCommonCorners =
790 block._corners.count( sideI->getCornerNode(0,0)) +
791 block._corners.count( sideI->getCornerNode(1,0)) +
792 block._corners.count( sideI->getCornerNode(0,1)) +
793 block._corners.count( sideI->getCornerNode(1,1));
794 if ( nbCommonCorners > 2 )
801 if ( !withGeometricAnalysis )
803 sidesAround.insert( sidesOnEdge.begin(), sidesOnEdge.end() );
806 if ( nbLoadedSides == 1 )
808 // Issue 0021529. There are at least 2 sides by each edge and
809 // position of block gravity center is undefined.
810 // Find a side starting from which we can walk around the startBlockSide
812 // fill in corner2Sides
813 map< const SMDS_MeshNode*, list< _BlockSide* > > corner2Sides;
814 for ( sideIt = sidesAround.begin(); sideIt != sidesAround.end(); ++sideIt )
816 _BlockSide* sideI = *sideIt;
817 corner2Sides[ sideI->getCornerNode(0,0) ].push_back( sideI );
818 corner2Sides[ sideI->getCornerNode(1,0) ].push_back( sideI );
819 corner2Sides[ sideI->getCornerNode(0,1) ].push_back( sideI );
820 corner2Sides[ sideI->getCornerNode(1,1) ].push_back( sideI );
822 // remove corners of startBlockSide from corner2Sides
823 set<const SMDS_MeshNode*>::iterator nIt = block._corners.begin();
824 for ( ; nIt != block._corners.end(); ++nIt )
825 corner2Sides.erase( *nIt );
828 for ( sideIt = sidesOnEdge.begin(); sideIt != sidesOnEdge.end(); ++sideIt )
830 if ( isClosedChainOfSides( *sideIt, corner2Sides ))
841 // Select one of found sides most close to startBlockSide
843 gp_XYZ p1 ( n1->X(),n1->Y(),n1->Z()), p2 (n2->X(),n2->Y(),n2->Z());
844 gp_Vec p1p2( p1, p2 );
846 const SMDS_MeshElement* face1 = side1->getCornerFace( n1 );
847 gp_XYZ p1Op = SMESH_TNodeXYZ( oppositeNode( face1, face1->GetNodeIndex(n1)));
848 gp_Vec side1Dir( p1, p1Op );
849 gp_Ax2 pln( p1, p1p2, side1Dir ); // plane with normal p1p2 and X dir side1Dir
850 _DUMP_(" Select adjacent for "<< side1._side << " - side dir ("
851 << side1Dir.X() << ", " << side1Dir.Y() << ", " << side1Dir.Z() << ")" );
853 map < double , _BlockSide* > angleOfSide;
854 for (sideIt = sidesOnEdge.begin(); sideIt != sidesOnEdge.end(); ++sideIt )
856 _BlockSide* sideI = *sideIt;
857 const SMDS_MeshElement* faceI = sideI->getCornerFace( n1 );
858 gp_XYZ p1Op = SMESH_TNodeXYZ( oppositeNode( faceI, faceI->GetNodeIndex(n1)));
859 gp_Vec sideIDir( p1, p1Op );
860 // compute angle of (sideIDir projection to pln) and (X dir of pln)
861 gp_Vec2d sideIDirProj( sideIDir * pln.XDirection(), sideIDir * pln.YDirection());
862 double angle = sideIDirProj.Angle( gp::DX2d() );
863 if ( angle < 0 ) angle += 2. * M_PI; // angle [0-2*PI]
864 angleOfSide.insert( make_pair( angle, sideI ));
865 _DUMP_(" "<< sideI << " - side dir ("
866 << sideIDir.X() << ", " << sideIDir.Y() << ", " << sideIDir.Z() << ")"
867 << " angle " << angle);
870 gp_XYZ gc(0,0,0); // gravity center of already loaded block sides
871 for (int i = 0; i < NB_BLOCK_SIDES; ++i )
872 if ( block._side[ i ] )
873 gc += block._side[ i ]._side->getGC();
876 gp_Vec gcDir( p1, gc );
877 gp_Vec2d gcDirProj( gcDir * pln.XDirection(), gcDir * pln.YDirection());
878 double gcAngle = gcDirProj.Angle( gp::DX2d() );
879 foundSide = gcAngle < 0 ? angleOfSide.rbegin()->second : angleOfSide.begin()->second;
882 _DUMP_(" selected "<< foundSide );
885 // Orient the found side correctly
887 // corners of found side corresponding to nodes n1 and n2
888 bool xMax1, yMax1, xMax2, yMax2;
889 if ( !getEdgeEnds( sharedSideEdge2, xMax1, yMax1, xMax2, yMax2 ))
890 return error(SMESH_Comment("Internal error at ")<<__FILE__<<":"<<__LINE__),
891 _OrientedBlockSide(0);
893 for ( int ori = 0; ori < _OrientedIndexer::MAX_ORI+1; ++ori )
895 _OrientedBlockSide orientedSide( foundSide, ori );
896 const SMDS_MeshNode* n12 = orientedSide.cornerNode( xMax1, yMax1);
897 const SMDS_MeshNode* n22 = orientedSide.cornerNode( xMax2, yMax2);
898 if ( n1 == n12 && n2 == n22 )
901 error(SMESH_Comment("Failed to orient a block side found by edge ")<<sharedSideEdge1
902 << " of side " << startBlockSide
903 << " of block " << _blocks.size());
907 //================================================================================
909 * \brief: Fill rows (which are actually columns,if !alongN1N2) of nodes starting
910 * from the given quadrangle until another block corner encounters.
911 * n1 and n2 are at bottom of quad, n1 is at block corner.
913 //================================================================================
915 bool _Skin::fillRowsUntilCorner(const SMDS_MeshElement* quad,
916 const SMDS_MeshNode* n1,
917 const SMDS_MeshNode* n2,
918 vector<const SMDS_MeshNode*>& row1,
919 vector<const SMDS_MeshNode*>& row2,
920 const bool alongN1N2 )
922 const SMDS_MeshNode* corner1 = n1;
924 // Store nodes of quad in the rows and find new n1 and n2 to get
925 // the next face so that new n2 is on block edge
926 int i1 = quad->GetNodeIndex( n1 );
927 int i2 = quad->GetNodeIndex( n2 );
928 row1.clear(); row2.clear();
929 row1.push_back( n1 );
932 row1.push_back( n2 );
933 row2.push_back( oppositeNode( quad, i2 ));
934 row2.push_back( n1 = oppositeNode( quad, i1 ));
938 row2.push_back( n2 );
939 row1.push_back( n2 = oppositeNode( quad, i2 ));
940 row2.push_back( n1 = oppositeNode( quad, i1 ));
943 if ( isCornerNode( row1[1] ))
946 // Find the rest nodes
947 TIDSortedElemSet emptySet, avoidSet;
948 while ( !isCornerNode( n2 ) )
950 avoidSet.clear(); avoidSet.insert( quad );
951 quad = SMESH_MeshEditor::FindFaceInSet( n1, n2, emptySet, avoidSet, &i1, &i2 );
952 if ( !isQuadrangle( quad ))
955 row1.push_back( n2 = oppositeNode( quad, i1 ));
956 row2.push_back( n1 = oppositeNode( quad, i2 ));
958 return n1 != corner1;
961 //================================================================================
963 * \brief Return a corner face by a corner node
965 //================================================================================
967 const SMDS_MeshElement* _BlockSide::getCornerFace(const SMDS_MeshNode* cornerNode) const
969 int x, y, isXMax, isYMax, found = 0;
970 for ( isXMax = 0; isXMax < 2; ++isXMax )
972 for ( isYMax = 0; isYMax < 2; ++isYMax )
974 x = isXMax ? _index._xSize-1 : 0;
975 y = isYMax ? _index._ySize-1 : 0;
976 found = ( getNode(x,y) == cornerNode );
981 if ( !found ) return 0;
982 int dx = isXMax ? -1 : +1;
983 int dy = isYMax ? -1 : +1;
984 const SMDS_MeshNode* n1 = getNode(x,y);
985 const SMDS_MeshNode* n2 = getNode(x+dx,y);
986 const SMDS_MeshNode* n3 = getNode(x,y+dy);
987 const SMDS_MeshNode* n4 = getNode(x+dx,y+dy);
988 return SMDS_Mesh::FindFace(n1, n2, n3, n4 );
991 //================================================================================
993 * \brief Checks own validity
995 //================================================================================
997 bool _Block::isValid() const
999 bool ok = ( nbSides() == 6 );
1001 // check only corners depending on side selection
1002 EBoxSides adjacent[4] = { B_BOTTOM, B_RIGHT, B_TOP, B_LEFT };
1003 EQuadEdge edgeAdj [4] = { Q_TOP, Q_RIGHT, Q_TOP, Q_RIGHT };
1004 EQuadEdge edgeBack[4] = { Q_BOTTOM, Q_RIGHT, Q_TOP, Q_LEFT };
1006 for ( int i=0; ok && i < NB_QUAD_SIDES; ++i )
1008 SMESH_OrientedLink eBack = _side[ B_BACK ].edge( edgeBack[i] );
1009 SMESH_OrientedLink eAdja = _side[ adjacent[i] ].edge( edgeAdj[i] );
1010 ok = ( eBack == eAdja );
1021 HEXA_NS::Hexa* _block2Hexa( const _Block& block,
1022 HEXA_NS::Document* doc,
1023 std::map<HEXA_NS::Vertex*, SMDS_MeshNode*> vertexNode )
1025 const SMDS_MeshNode* n000 = block.getSide(B_BOTTOM).cornerNode( 0, 0 );
1026 const SMDS_MeshNode* n100 = block.getSide(B_BOTTOM).cornerNode( 1, 0 );
1027 const SMDS_MeshNode* n010 = block.getSide(B_BOTTOM).cornerNode( 0, 1 );
1028 const SMDS_MeshNode* n110 = block.getSide(B_BOTTOM).cornerNode( 1, 1 );
1029 const SMDS_MeshNode* n001 = block.getSide(B_TOP).cornerNode( 0, 0 );
1030 const SMDS_MeshNode* n101 = block.getSide(B_TOP).cornerNode( 1, 0 );
1031 const SMDS_MeshNode* n011 = block.getSide(B_TOP).cornerNode( 0, 1 );
1032 const SMDS_MeshNode* n111 = block.getSide(B_TOP).cornerNode( 1, 1 );
1034 list<const SMDS_MeshNode* > nodeFromBlock;
1035 nodeFromBlock.push_back(n000);
1036 nodeFromBlock.push_back(n100);
1037 nodeFromBlock.push_back(n010);
1038 nodeFromBlock.push_back(n110);
1039 nodeFromBlock.push_back(n001);
1040 nodeFromBlock.push_back(n101);
1041 nodeFromBlock.push_back(n011);
1042 nodeFromBlock.push_back(n111);
1043 nodeFromBlock.sort();
1046 HEXA_NS::Hexa* hexa = NULL;
1047 int nHexa = doc->countUsedHexa();
1048 for (int j=0; j <nHexa; ++j ){
1049 hexa = doc->getUsedHexa(j);
1050 list<const SMDS_MeshNode* > nodeFromHexa;
1051 int nVx = hexa->countVertex();
1052 for ( int i=0; i <nVx; ++i ){
1053 HEXA_NS::Vertex* v = hexa->getVertex(i);
1054 const SMDS_MeshNode* n = vertexNode[v];
1055 nodeFromHexa.push_back(n);
1057 nodeFromHexa.sort();
1059 if ( nodeFromBlock == nodeFromHexa ){
1060 // std::cout << "OK block match hexa "<< hexa <<" id = "<<hexa->getId()<<std::endl;
1063 std::cout << "KO block does not match hexa "<< hexa <<" id = "<<hexa->getId()<<std::endl;
1076 //=======================================================================
1077 //function : SMESH_HexaFromSkin_3D
1079 //=======================================================================
1081 // SMESH_HexaFromSkin_3D::SMESH_HexaFromSkin_3D(int hypId, int studyId, SMESH_Gen* gen)
1082 // :SMESH_3D_Algo(hypId, studyId, gen)
1084 // MESSAGE("SMESH_HexaFromSkin_3D::SMESH_HexaFromSkin_3D");
1085 // _name = "HexaFromSkin_3D";
1088 SMESH_HexaFromSkin_3D::SMESH_HexaFromSkin_3D(int hypId, int studyId, SMESH_Gen* gen, HEXA_NS::Document* doc)
1089 :SMESH_3D_Algo(hypId, studyId, gen),
1092 if(MYDEBUG) MESSAGE("SMESH_HexaFromSkin_3D::SMESH_HexaFromSkin_3D");
1093 _name = "HexaFromSkin_3D";
1097 SMESH_HexaFromSkin_3D::~SMESH_HexaFromSkin_3D()
1099 if(MYDEBUG) MESSAGE("SMESH_HexaFromSkin_3D::~SMESH_HexaFromSkin_3D");
1102 //================================================================================
1104 * \brief Main method, which generates hexaheda
1106 //================================================================================
1108 bool SMESH_HexaFromSkin_3D::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
1111 int nbBlocks = skin.findBlocks(aMesh);
1112 if ( nbBlocks == 0 )
1113 return error( skin.error());
1115 vector< vector< const SMDS_MeshNode* > > columns;
1116 int x, xSize, y, ySize, z, zSize;
1119 for ( int i = 0; i < nbBlocks; ++i )
1121 const _Block& block = skin.getBlock( i );
1123 // ------------------------------------------
1124 // Fill columns of nodes with existing nodes
1125 // ------------------------------------------
1127 xSize = block.getSide(B_BOTTOM).getHoriSize();
1128 ySize = block.getSide(B_BOTTOM).getVertSize();
1129 zSize = block.getSide(B_FRONT ).getVertSize();
1130 int X = xSize - 1, Y = ySize - 1, Z = zSize - 1;
1131 colIndex = _Indexer( xSize, ySize );
1132 columns.resize( colIndex.size() );
1134 // fill node columns by front and back box sides
1135 for ( x = 0; x < xSize; ++x ) {
1136 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( x, 0 )];
1137 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( x, Y )];
1138 column0.resize( zSize );
1139 column1.resize( zSize );
1140 for ( z = 0; z < zSize; ++z ) {
1141 column0[ z ] = block.getSide(B_FRONT).node( x, z );
1142 column1[ z ] = block.getSide(B_BACK) .node( x, z );
1145 // fill node columns by left and right box sides
1146 for ( y = 1; y < ySize-1; ++y ) {
1147 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( 0, y )];
1148 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( X, y )];
1149 column0.resize( zSize );
1150 column1.resize( zSize );
1151 for ( z = 0; z < zSize; ++z ) {
1152 column0[ z ] = block.getSide(B_LEFT) .node( y, z );
1153 column1[ z ] = block.getSide(B_RIGHT).node( y, z );
1156 // get nodes from top and bottom box sides
1157 for ( x = 1; x < xSize-1; ++x ) {
1158 for ( y = 1; y < ySize-1; ++y ) {
1159 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
1160 column.resize( zSize );
1161 column.front() = block.getSide(B_BOTTOM).node( x, y );
1162 column.back() = block.getSide(B_TOP) .node( x, y );
1166 // ----------------------------
1167 // Add internal nodes of a box
1168 // ----------------------------
1169 // projection points of internal nodes on box sub-shapes by which
1170 // coordinates of internal nodes are computed
1171 vector<gp_XYZ> pointOnShape( SMESH_Block::ID_Shell );
1173 // projections on vertices are constant
1174 pointOnShape[ SMESH_Block::ID_V000 ] = block.getSide(B_BOTTOM).xyz( 0, 0 );
1175 pointOnShape[ SMESH_Block::ID_V100 ] = block.getSide(B_BOTTOM).xyz( X, 0 );
1176 pointOnShape[ SMESH_Block::ID_V010 ] = block.getSide(B_BOTTOM).xyz( 0, Y );
1177 pointOnShape[ SMESH_Block::ID_V110 ] = block.getSide(B_BOTTOM).xyz( X, Y );
1178 pointOnShape[ SMESH_Block::ID_V001 ] = block.getSide(B_TOP).xyz( 0, 0 );
1179 pointOnShape[ SMESH_Block::ID_V101 ] = block.getSide(B_TOP).xyz( X, 0 );
1180 pointOnShape[ SMESH_Block::ID_V011 ] = block.getSide(B_TOP).xyz( 0, Y );
1181 pointOnShape[ SMESH_Block::ID_V111 ] = block.getSide(B_TOP).xyz( X, Y );
1183 for ( x = 1; x < xSize-1; ++x )
1185 gp_XYZ params; // normalized parameters of internal node within a unit box
1186 params.SetCoord( 1, x / double(X) );
1187 for ( y = 1; y < ySize-1; ++y )
1189 params.SetCoord( 2, y / double(Y) );
1190 // column to fill during z loop
1191 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
1192 // projections on horizontal edges
1193 pointOnShape[ SMESH_Block::ID_Ex00 ] = block.getSide(B_BOTTOM).xyz( x, 0 );
1194 pointOnShape[ SMESH_Block::ID_Ex10 ] = block.getSide(B_BOTTOM).xyz( x, Y );
1195 pointOnShape[ SMESH_Block::ID_E0y0 ] = block.getSide(B_BOTTOM).xyz( 0, y );
1196 pointOnShape[ SMESH_Block::ID_E1y0 ] = block.getSide(B_BOTTOM).xyz( X, y );
1197 pointOnShape[ SMESH_Block::ID_Ex01 ] = block.getSide(B_TOP).xyz( x, 0 );
1198 pointOnShape[ SMESH_Block::ID_Ex11 ] = block.getSide(B_TOP).xyz( x, Y );
1199 pointOnShape[ SMESH_Block::ID_E0y1 ] = block.getSide(B_TOP).xyz( 0, y );
1200 pointOnShape[ SMESH_Block::ID_E1y1 ] = block.getSide(B_TOP).xyz( X, y );
1201 // projections on horizontal sides
1202 pointOnShape[ SMESH_Block::ID_Fxy0 ] = block.getSide(B_BOTTOM).xyz( x, y );
1203 pointOnShape[ SMESH_Block::ID_Fxy1 ] = block.getSide(B_TOP) .xyz( x, y );
1204 for ( z = 1; z < zSize-1; ++z ) // z loop
1206 params.SetCoord( 3, z / double(Z) );
1207 // projections on vertical edges
1208 pointOnShape[ SMESH_Block::ID_E00z ] = block.getSide(B_FRONT).xyz( 0, z );
1209 pointOnShape[ SMESH_Block::ID_E10z ] = block.getSide(B_FRONT).xyz( X, z );
1210 pointOnShape[ SMESH_Block::ID_E01z ] = block.getSide(B_BACK).xyz( 0, z );
1211 pointOnShape[ SMESH_Block::ID_E11z ] = block.getSide(B_BACK).xyz( X, z );
1212 // projections on vertical sides
1213 pointOnShape[ SMESH_Block::ID_Fx0z ] = block.getSide(B_FRONT).xyz( x, z );
1214 pointOnShape[ SMESH_Block::ID_Fx1z ] = block.getSide(B_BACK) .xyz( x, z );
1215 pointOnShape[ SMESH_Block::ID_F0yz ] = block.getSide(B_LEFT) .xyz( y, z );
1216 pointOnShape[ SMESH_Block::ID_F1yz ] = block.getSide(B_RIGHT).xyz( y, z );
1218 // compute internal node coordinates
1220 SMESH_Block::ShellPoint( params, pointOnShape, coords );
1221 column[ z ] = aHelper->AddNode( coords.X(), coords.Y(), coords.Z() );
1225 //cout << "----------------------------------------------------------------------"<<endl;
1226 //for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id)
1228 // gp_XYZ p = pointOnShape[ id ];
1229 // SMESH_Block::DumpShapeID( id,cout)<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;
1231 //cout << "Params: ( "<< params.X()<<", "<<params.Y()<<", "<<params.Z()<<" )"<<endl;
1232 //cout << "coords: ( "<< coords.X()<<", "<<coords.Y()<<", "<<coords.Z()<<" )"<<endl;
1241 // find out orientation by a least distorted hexahedron (issue 0020855);
1242 // the last is defined by evaluating sum of face normals of 8 corner hexahedrons
1243 double badness = numeric_limits<double>::max();
1245 for ( int xMax = 0; xMax < 2; ++xMax )
1246 for ( int yMax = 0; yMax < 2; ++yMax )
1247 for ( int zMax = 0; zMax < 2; ++zMax )
1249 x = xMax ? xSize-1 : 1;
1250 y = yMax ? ySize-1 : 1;
1251 z = zMax ? zSize-1 : 1;
1252 vector< const SMDS_MeshNode* >& col00 = columns[ colIndex( x-1, y-1 )];
1253 vector< const SMDS_MeshNode* >& col10 = columns[ colIndex( x , y-1 )];
1254 vector< const SMDS_MeshNode* >& col01 = columns[ colIndex( x-1, y )];
1255 vector< const SMDS_MeshNode* >& col11 = columns[ colIndex( x , y )];
1257 const SMDS_MeshNode* n000 = col00[z-1];
1258 const SMDS_MeshNode* n100 = col10[z-1];
1259 const SMDS_MeshNode* n010 = col01[z-1];
1260 const SMDS_MeshNode* n110 = col11[z-1];
1261 const SMDS_MeshNode* n001 = col00[z];
1262 const SMDS_MeshNode* n101 = col10[z];
1263 const SMDS_MeshNode* n011 = col01[z];
1264 const SMDS_MeshNode* n111 = col11[z];
1265 SMDS_VolumeOfNodes probeVolume (n000,n010,n110,n100,
1266 n001,n011,n111,n101);
1267 SMDS_VolumeTool volTool( &probeVolume );
1268 double Nx=0.,Ny=0.,Nz=0.;
1269 for ( int iFace = 0; iFace < volTool.NbFaces(); ++iFace )
1272 volTool.GetFaceNormal( iFace, nx,ny,nz );
1277 double quality = Nx*Nx + Ny*Ny + Nz*Nz;
1278 if ( quality < badness )
1281 isForw = volTool.IsForward();
1286 for ( x = 0; x < xSize-1; ++x ) {
1287 for ( y = 0; y < ySize-1; ++y ) {
1288 vector< const SMDS_MeshNode* >& col00 = columns[ colIndex( x, y )];
1289 vector< const SMDS_MeshNode* >& col10 = columns[ colIndex( x+1, y )];
1290 vector< const SMDS_MeshNode* >& col01 = columns[ colIndex( x, y+1 )];
1291 vector< const SMDS_MeshNode* >& col11 = columns[ colIndex( x+1, y+1 )];
1292 // bottom face normal of a hexa mush point outside the volume
1294 for ( z = 0; z < zSize-1; ++z )
1295 aHelper->AddVolume(col00[z], col01[z], col11[z], col10[z],
1296 col00[z+1], col01[z+1], col11[z+1], col10[z+1]);
1298 for ( z = 0; z < zSize-1; ++z )
1299 aHelper->AddVolume(col00[z], col10[z], col11[z], col01[z],
1300 col00[z+1], col10[z+1], col11[z+1], col01[z+1]);
1309 //================================================================================
1311 * \brief Main method, which generates hexaheda
1313 //================================================================================
1314 bool SMESH_HexaFromSkin_3D::Compute( SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper,
1315 std::map<HEXA_NS::Hexa*, SMESH_HexaBlocks::SMESHVolumes>& volumesOnHexa,
1316 std::map<HEXA_NS::Vertex*, SMDS_MeshNode*> vertexNode )
1318 if(MYDEBUG) MESSAGE("SMESH_HexaFromSkin_3D::Compute BEGIN");
1320 int nbBlocks = skin.findBlocks(aMesh);
1321 if ( nbBlocks == 0 )
1322 return error( skin.error());
1324 vector< vector< const SMDS_MeshNode* > > columns;
1325 int x, xSize, y, ySize, z, zSize;
1328 for ( int i = 0; i < nbBlocks; ++i )
1330 const _Block& block = skin.getBlock( i );
1332 // ------------------------------------------
1333 // Fill columns of nodes with existing nodes
1334 // ------------------------------------------
1336 xSize = block.getSide(B_BOTTOM).getHoriSize();
1337 ySize = block.getSide(B_BOTTOM).getVertSize();
1338 zSize = block.getSide(B_FRONT ).getVertSize();
1339 int X = xSize - 1, Y = ySize - 1, Z = zSize - 1;
1340 colIndex = _Indexer( xSize, ySize );
1341 columns.resize( colIndex.size() );
1343 // fill node columns by front and back box sides
1344 for ( x = 0; x < xSize; ++x ) {
1345 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( x, 0 )];
1346 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( x, Y )];
1347 column0.resize( zSize );
1348 column1.resize( zSize );
1349 for ( z = 0; z < zSize; ++z ) {
1350 column0[ z ] = block.getSide(B_FRONT).node( x, z );
1351 column1[ z ] = block.getSide(B_BACK) .node( x, z );
1354 // fill node columns by left and right box sides
1355 for ( y = 1; y < ySize-1; ++y ) {
1356 vector< const SMDS_MeshNode* >& column0 = columns[ colIndex( 0, y )];
1357 vector< const SMDS_MeshNode* >& column1 = columns[ colIndex( X, y )];
1358 column0.resize( zSize );
1359 column1.resize( zSize );
1360 for ( z = 0; z < zSize; ++z ) {
1361 column0[ z ] = block.getSide(B_LEFT) .node( y, z );
1362 column1[ z ] = block.getSide(B_RIGHT).node( y, z );
1365 // get nodes from top and bottom box sides
1366 for ( x = 1; x < xSize-1; ++x ) {
1367 for ( y = 1; y < ySize-1; ++y ) {
1368 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
1369 column.resize( zSize );
1370 column.front() = block.getSide(B_BOTTOM).node( x, y );
1371 column.back() = block.getSide(B_TOP) .node( x, y );
1375 // ----------------------------
1376 // Add internal nodes of a box
1377 // ----------------------------
1378 // projection points of internal nodes on box subshapes by which
1379 // coordinates of internal nodes are computed
1380 vector<gp_XYZ> pointOnShape( SMESH_Block::ID_Shell );
1382 // projections on vertices are constant
1383 pointOnShape[ SMESH_Block::ID_V000 ] = block.getSide(B_BOTTOM).xyz( 0, 0 );
1384 pointOnShape[ SMESH_Block::ID_V100 ] = block.getSide(B_BOTTOM).xyz( X, 0 );
1385 pointOnShape[ SMESH_Block::ID_V010 ] = block.getSide(B_BOTTOM).xyz( 0, Y );
1386 pointOnShape[ SMESH_Block::ID_V110 ] = block.getSide(B_BOTTOM).xyz( X, Y );
1387 pointOnShape[ SMESH_Block::ID_V001 ] = block.getSide(B_TOP).xyz( 0, 0 );
1388 pointOnShape[ SMESH_Block::ID_V101 ] = block.getSide(B_TOP).xyz( X, 0 );
1389 pointOnShape[ SMESH_Block::ID_V011 ] = block.getSide(B_TOP).xyz( 0, Y );
1390 pointOnShape[ SMESH_Block::ID_V111 ] = block.getSide(B_TOP).xyz( X, Y );
1392 for ( x = 1; x < xSize-1; ++x )
1394 gp_XYZ params; // normalized parameters of internal node within a unit box
1395 params.SetCoord( 1, x / double(X) );
1396 for ( y = 1; y < ySize-1; ++y )
1398 params.SetCoord( 2, y / double(Y) );
1399 // column to fill during z loop
1400 vector< const SMDS_MeshNode* >& column = columns[ colIndex( x, y )];
1401 // projections on horizontal edges
1402 pointOnShape[ SMESH_Block::ID_Ex00 ] = block.getSide(B_BOTTOM).xyz( x, 0 );
1403 pointOnShape[ SMESH_Block::ID_Ex10 ] = block.getSide(B_BOTTOM).xyz( x, Y );
1404 pointOnShape[ SMESH_Block::ID_E0y0 ] = block.getSide(B_BOTTOM).xyz( 0, y );
1405 pointOnShape[ SMESH_Block::ID_E1y0 ] = block.getSide(B_BOTTOM).xyz( X, y );
1406 pointOnShape[ SMESH_Block::ID_Ex01 ] = block.getSide(B_TOP).xyz( x, 0 );
1407 pointOnShape[ SMESH_Block::ID_Ex11 ] = block.getSide(B_TOP).xyz( x, Y );
1408 pointOnShape[ SMESH_Block::ID_E0y1 ] = block.getSide(B_TOP).xyz( 0, y );
1409 pointOnShape[ SMESH_Block::ID_E1y1 ] = block.getSide(B_TOP).xyz( X, y );
1410 // projections on horizontal sides
1411 pointOnShape[ SMESH_Block::ID_Fxy0 ] = block.getSide(B_BOTTOM).xyz( x, y );
1412 pointOnShape[ SMESH_Block::ID_Fxy1 ] = block.getSide(B_TOP) .xyz( x, y );
1413 for ( z = 1; z < zSize-1; ++z ) // z loop
1415 params.SetCoord( 3, z / double(Z) );
1416 // projections on vertical edges
1417 pointOnShape[ SMESH_Block::ID_E00z ] = block.getSide(B_FRONT).xyz( 0, z );
1418 pointOnShape[ SMESH_Block::ID_E10z ] = block.getSide(B_FRONT).xyz( X, z );
1419 pointOnShape[ SMESH_Block::ID_E01z ] = block.getSide(B_BACK).xyz( 0, z );
1420 pointOnShape[ SMESH_Block::ID_E11z ] = block.getSide(B_BACK).xyz( X, z );
1421 // projections on vertical sides
1422 pointOnShape[ SMESH_Block::ID_Fx0z ] = block.getSide(B_FRONT).xyz( x, z );
1423 pointOnShape[ SMESH_Block::ID_Fx1z ] = block.getSide(B_BACK) .xyz( x, z );
1424 pointOnShape[ SMESH_Block::ID_F0yz ] = block.getSide(B_LEFT) .xyz( y, z );
1425 pointOnShape[ SMESH_Block::ID_F1yz ] = block.getSide(B_RIGHT).xyz( y, z );
1427 // compute internal node coordinates
1429 SMESH_Block::ShellPoint( params, pointOnShape, coords );
1430 column[ z ] = aHelper->AddNode( coords.X(), coords.Y(), coords.Z() );
1434 //cout << "----------------------------------------------------------------------"<<endl;
1435 //for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id)
1437 // gp_XYZ p = pointOnShape[ id ];
1438 // SMESH_Block::DumpShapeID( id,cout)<<" ( "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<" )"<<endl;
1440 //cout << "Params: ( "<< params.X()<<", "<<params.Y()<<", "<<params.Z()<<" )"<<endl;
1441 //cout << "coords: ( "<< coords.X()<<", "<<coords.Y()<<", "<<coords.Z()<<" )"<<endl;
1450 // find out orientation
1451 const SMDS_MeshNode* n000 = block.getSide(B_BOTTOM).cornerNode( 0, 0 );
1452 const SMDS_MeshNode* n100 = block.getSide(B_BOTTOM).cornerNode( 1, 0 );
1453 const SMDS_MeshNode* n010 = block.getSide(B_BOTTOM).cornerNode( 0, 1 );
1454 const SMDS_MeshNode* n110 = block.getSide(B_BOTTOM).cornerNode( 1, 1 );
1455 const SMDS_MeshNode* n001 = block.getSide(B_TOP).cornerNode( 0, 0 );
1456 const SMDS_MeshNode* n101 = block.getSide(B_TOP).cornerNode( 1, 0 );
1457 const SMDS_MeshNode* n011 = block.getSide(B_TOP).cornerNode( 0, 1 );
1458 const SMDS_MeshNode* n111 = block.getSide(B_TOP).cornerNode( 1, 1 );
1459 SMDS_VolumeOfNodes probeVolume (n000,n010,n110,n100,
1460 n001,n011,n111,n101);
1461 bool isForw = SMDS_VolumeTool( &probeVolume ).IsForward();
1464 SMESH_HexaBlocks::SMESHVolumes volumesOnBlock; //Groups creation
1466 for ( x = 0; x < xSize-1; ++x ) {
1467 for ( y = 0; y < ySize-1; ++y ) {
1468 vector< const SMDS_MeshNode* >& col00 = columns[ colIndex( x, y )];
1469 vector< const SMDS_MeshNode* >& col10 = columns[ colIndex( x+1, y )];
1470 vector< const SMDS_MeshNode* >& col01 = columns[ colIndex( x, y+1 )];
1471 vector< const SMDS_MeshNode* >& col11 = columns[ colIndex( x+1, y+1 )];
1472 // bottom face normal of a hexa mush point outside the volume
1474 for ( z = 0; z < zSize-1; ++z ){
1475 SMDS_MeshVolume* newVolume =
1476 aHelper->AddVolume(col00[z], col01[z], col11[z], col10[z],
1477 col00[z+1], col01[z+1], col11[z+1], col10[z+1]);
1478 volumesOnBlock.push_back( newVolume );
1481 for ( z = 0; z < zSize-1; ++z ){
1482 SMDS_MeshVolume* newVolume =
1483 aHelper->AddVolume(col00[z], col10[z], col11[z], col01[z],
1484 col00[z+1], col10[z+1], col11[z+1], col01[z+1]);
1485 volumesOnBlock.push_back( newVolume );
1489 // std::cout << "block i = " << i << std::endl;
1490 HEXA_NS::Hexa* currentHexa = _block2Hexa( block, _doc, vertexNode );
1491 if ( currentHexa != NULL ){
1492 // std::cout<<"===== found ->"<<currentHexa<<" for block "<<i<<std::endl;
1493 if ( volumesOnHexa.count(currentHexa)==0 ) {
1494 // std::cout<<"added!! "<<std::endl;
1495 volumesOnHexa[currentHexa] = volumesOnBlock;
1497 // std::cout<<"already !! "<<std::endl;
1500 // std::cout<<"===== not found ->"<<currentHexa<<" for block "<<i<<std::endl;
1505 if(MYDEBUG) MESSAGE("SMESH_HexaFromSkin_3D::Compute END");
1509 //================================================================================
1511 * \brief Evaluate nb of hexa
1513 //================================================================================
1515 bool SMESH_HexaFromSkin_3D::Evaluate(SMESH_Mesh & aMesh,
1516 const TopoDS_Shape & aShape,
1517 MapShapeNbElems& aResMap)
1520 int nbBlocks = skin.findBlocks(aMesh);
1521 if ( nbBlocks == 0 )
1522 return error( skin.error());
1524 bool secondOrder = aMesh.NbFaces( ORDER_QUADRATIC );
1526 int entity = secondOrder ? SMDSEntity_Quad_Hexa : SMDSEntity_Hexa;
1527 vector<int>& nbByType = aResMap[ aMesh.GetSubMesh( aShape )];
1528 if ( entity >= nbByType.size() )
1529 nbByType.resize( SMDSEntity_Last, 0 );
1531 for ( int i = 0; i < nbBlocks; ++i )
1533 const _Block& block = skin.getBlock( i );
1535 int nbX = block.getSide(B_BOTTOM).getHoriSize();
1536 int nbY = block.getSide(B_BOTTOM).getVertSize();
1537 int nbZ = block.getSide(B_FRONT ).getVertSize();
1539 int nbHexa = (nbX-1) * (nbY-1) * (nbZ-1);
1540 int nbNodes = (nbX-2) * (nbY-2) * (nbZ-2);
1543 (nbX-2) * (nbY-2) * (nbZ-1) +
1544 (nbX-2) * (nbY-1) * (nbZ-2) +
1545 (nbX-1) * (nbY-2) * (nbZ-2);
1548 nbByType[ entity ] += nbHexa;
1549 nbByType[ SMDSEntity_Node ] += nbNodes;
1555 //================================================================================
1557 * \brief Abstract method must be defined but does nothing
1559 //================================================================================
1561 bool SMESH_HexaFromSkin_3D::CheckHypothesis(SMESH_Mesh&, const TopoDS_Shape&,
1562 Hypothesis_Status& aStatus)
1564 aStatus = SMESH_Hypothesis::HYP_OK;
1568 //================================================================================
1570 * \brief Abstract method must be defined but just reports an error as this
1571 * algo is not intended to work with shapes
1573 //================================================================================
1575 bool SMESH_HexaFromSkin_3D::Compute(SMESH_Mesh&, const TopoDS_Shape&)
1577 return error("Algorithm can't work with geometrical shapes");