+//=======================================================================
+//function : Apply
+//purpose : Compute nodes coordinates applying
+// the loaded pattern to <theVolume>. The (0,0,0) key-point
+// will be mapped into <theNode000Index>-th node. The
+// (0,0,1) key-point will be mapped into <theNode000Index>-th
+// node.
+//=======================================================================
+
+bool SMESH_Pattern::Apply (const SMDS_MeshVolume* theVolume,
+ const int theNode000Index,
+ const int theNode001Index)
+{
+ //MESSAGE(" ::Apply(MeshVolume) " );
+
+ if (!findBoundaryPoints()) // bind ID to points
+ return false;
+
+ SMESH_Block block; // bind ID to shape
+ if (!block.LoadMeshBlock( theVolume, theNode000Index, theNode001Index, myOrderedNodes ))
+ return setErrorCode( ERR_APPLV_BAD_SHAPE );
+ // compute XYZ of points on shapes
+
+ for ( int ID = SMESH_Block::ID_V000; ID <= SMESH_Block::ID_Shell; ID++ )
+ {
+ list< TPoint* > & shapePoints = getShapePoints( ID );
+ list< TPoint* >::iterator pIt = shapePoints.begin();
+
+ if ( block.IsVertexID( ID ))
+ for ( ; pIt != shapePoints.end(); pIt++ ) {
+ block.VertexPoint( ID, (*pIt)->myXYZ.ChangeCoord() );
+ }
+ else if ( block.IsEdgeID( ID ))
+ for ( ; pIt != shapePoints.end(); pIt++ ) {
+ block.EdgePoint( ID, (*pIt)->myInitXYZ, (*pIt)->myXYZ.ChangeCoord() );
+ }
+ else if ( block.IsFaceID( ID ))
+ for ( ; pIt != shapePoints.end(); pIt++ ) {
+ block.FacePoint( ID, (*pIt)->myInitXYZ, (*pIt)->myXYZ.ChangeCoord() );
+ }
+ else
+ for ( ; pIt != shapePoints.end(); pIt++ )
+ block.ShellPoint( (*pIt)->myInitXYZ, (*pIt)->myXYZ.ChangeCoord() );
+ } // loop on block sub-shapes
+
+ myIsComputed = true;
+
+ return setErrorCode( ERR_OK );
+}
+
+//=======================================================================
+//function : mergePoints
+//purpose : Merge XYZ on edges and/or faces.
+//=======================================================================
+
+void SMESH_Pattern::mergePoints (const bool uniteGroups)
+{
+ map< TNodeSet, list< list< int > > >::iterator idListIt = myIdsOnBoundary.begin();
+ for ( ; idListIt != myIdsOnBoundary.end(); idListIt++ )
+ {
+ list<list< int > >& groups = idListIt->second;
+ if ( groups.size() < 2 )
+ continue;
+
+ // find tolerance
+ const TNodeSet& nodes = idListIt->first;
+ double tol2 = 1.e-10;
+ if ( nodes.size() > 1 ) {
+ Bnd_Box box;
+ TNodeSet::const_iterator n = nodes.begin();
+ for ( ; n != nodes.end(); ++n )
+ box.Add( gp_Pnt( (*n)->X(), (*n)->Y(), (*n)->Z() ));
+ double x, y, z, X, Y, Z;
+ box.Get( x, y, z, X, Y, Z );
+ gp_Pnt p( x, y, z ), P( X, Y, Z );
+ tol2 = 1.e-4 * p.SquareDistance( P );
+ }
+
+ // to unite groups on link
+ bool unite = ( uniteGroups && nodes.size() == 2 );
+ map< double, int > distIndMap;
+ const SMDS_MeshNode* node = *nodes.begin();
+ gp_Pnt P( node->X(), node->Y(), node->Z() );
+
+ // compare points, replace indices
+
+ list< int >::iterator ind1, ind2;
+ list< list< int > >::iterator grpIt1, grpIt2;
+ for ( grpIt1 = groups.begin(); grpIt1 != groups.end(); grpIt1++ )
+ {
+ list< int >& indices1 = *grpIt1;
+ grpIt2 = grpIt1;
+ for ( grpIt2++; grpIt2 != groups.end(); grpIt2++ )
+ {
+ list< int >& indices2 = *grpIt2;
+ for ( ind1 = indices1.begin(); ind1 != indices1.end(); ind1++ )
+ {
+ gp_XYZ& p1 = myXYZ[ *ind1 ];
+ ind2 = indices2.begin();
+ while ( ind2 != indices2.end() )
+ {
+ gp_XYZ& p2 = myXYZ[ *ind2 ];
+ //MESSAGE("COMP: " << *ind1 << " " << *ind2 << " X: " << p2.X() << " tol2: " << tol2);
+ if ( ( p1 - p2 ).SquareModulus() <= tol2 )
+ {
+ ASSERT( myReverseConnectivity.find( *ind2 ) != myReverseConnectivity.end() );
+ list< TElemDef* > & elemXYZIDsList = myReverseConnectivity[ *ind2 ];
+ list< TElemDef* >::iterator elemXYZIDs = elemXYZIDsList.begin();
+ for ( ; elemXYZIDs != elemXYZIDsList.end(); elemXYZIDs++ )
+ {
+ //MESSAGE( " Replace " << *ind2 << " with " << *ind1 );
+ myXYZ[ *ind2 ] = undefinedXYZ();
+ replace( (*elemXYZIDs)->begin(), (*elemXYZIDs)->end(), *ind2, *ind1 );
+ }
+ ind2 = indices2.erase( ind2 );
+ }
+ else
+ ind2++;
+ }
+ }
+ }
+ if ( unite ) { // sort indices using distIndMap
+ for ( ind1 = indices1.begin(); ind1 != indices1.end(); ind1++ )
+ {
+ ASSERT( isDefined( myXYZ[ *ind1 ] ));
+ double dist = P.SquareDistance( myXYZ[ *ind1 ]);
+ distIndMap.insert( make_pair( dist, *ind1 ));
+ }
+ }
+ }
+ if ( unite ) { // put all sorted indices into the first group
+ list< int >& g = groups.front();
+ g.clear();
+ map< double, int >::iterator dist_ind = distIndMap.begin();
+ for ( ; dist_ind != distIndMap.end(); dist_ind++ )
+ g.push_back( dist_ind->second );
+ }
+ } // loop on myIdsOnBoundary
+}
+
+//=======================================================================
+//function : makePolyElements
+//purpose : prepare intermediate data to create Polygons and Polyhedrons
+//=======================================================================
+
+void SMESH_Pattern::
+ makePolyElements(const vector< const SMDS_MeshNode* >& theNodes,
+ const bool toCreatePolygons,
+ const bool toCreatePolyedrs)
+{
+ myPolyElemXYZIDs.clear();
+ myPolyElems.clear();
+ myPolyElems.reserve( myIdsOnBoundary.size() );
+
+ // make a set of refined elements
+ set< const SMDS_MeshElement* > avoidSet, elemSet;
+ avoidSet.insert( myElements.begin(), myElements.end() );
+
+ map< TNodeSet, list< list< int > > >::iterator indListIt, nn_IdList;
+
+ if ( toCreatePolygons )
+ {
+ int lastFreeId = myXYZ.size();
+
+ // loop on links of refined elements
+ indListIt = myIdsOnBoundary.begin();
+ for ( ; indListIt != myIdsOnBoundary.end(); indListIt++ )
+ {
+ const TNodeSet & linkNodes = indListIt->first;
+ if ( linkNodes.size() != 2 )
+ continue; // skip face
+ const SMDS_MeshNode* n1 = * linkNodes.begin();
+ const SMDS_MeshNode* n2 = * linkNodes.rbegin();
+
+ list<list< int > >& idGroups = indListIt->second; // ids of nodes to build
+ if ( idGroups.empty() || idGroups.front().empty() )
+ continue;
+
+ // find not refined face having n1-n2 link
+
+ while (true)
+ {
+ const SMDS_MeshElement* face =
+ SMESH_MeshEditor::FindFaceInSet( n1, n2, elemSet, avoidSet );
+ if ( face )
+ {
+ avoidSet.insert ( face );
+ myPolyElems.push_back( face );
+
+ // some links of <face> are split;
+ // make list of xyz for <face>
+ myPolyElemXYZIDs.push_back(TElemDef());
+ TElemDef & faceNodeIds = myPolyElemXYZIDs.back();
+ // loop on links of a <face>
+ SMDS_ElemIteratorPtr nIt = face->nodesIterator();
+ int i = 0, nbNodes = face->NbNodes();
+ vector<const SMDS_MeshNode*> nodes( nbNodes + 1 );
+ while ( nIt->more() )
+ nodes[ i++ ] = static_cast<const SMDS_MeshNode*>( nIt->next() );
+ nodes[ i ] = nodes[ 0 ];
+ for ( i = 0; i < nbNodes; ++i )
+ {
+ // look for point mapped on a link
+ TNodeSet faceLinkNodes;
+ faceLinkNodes.insert( nodes[ i ] );
+ faceLinkNodes.insert( nodes[ i + 1 ] );
+ if ( faceLinkNodes == linkNodes )
+ nn_IdList = indListIt;
+ else
+ nn_IdList = myIdsOnBoundary.find( faceLinkNodes );
+ // add face point ids
+ faceNodeIds.push_back( ++lastFreeId );
+ myXYZIdToNodeMap.insert( make_pair( lastFreeId, nodes[ i ]));
+ if ( nn_IdList != myIdsOnBoundary.end() )
+ {
+ // there are points mapped on a link
+ list< int >& mappedIds = nn_IdList->second.front();
+ if ( isReversed( nodes[ i ], mappedIds ))
+ faceNodeIds.insert (faceNodeIds.end(),mappedIds.rbegin(), mappedIds.rend() );
+ else
+ faceNodeIds.insert (faceNodeIds.end(),mappedIds.begin(), mappedIds.end() );
+ }
+ } // loop on links of a <face>
+ } // if ( face )
+ else
+ break;
+ } // while (true)
+
+ if ( myIs2D && idGroups.size() > 1 ) {
+
+ // sew new elements on 2 refined elements sharing n1-n2 link
+
+ list< int >& idsOnLink = idGroups.front();
+ // temporarily add ids of link nodes to idsOnLink
+ bool rev = isReversed( n1, idsOnLink );
+ for ( int i = 0; i < 2; ++i )
+ {
+ TNodeSet nodeSet;
+ nodeSet.insert( i ? n2 : n1 );
+ ASSERT( myIdsOnBoundary.find( nodeSet ) != myIdsOnBoundary.end() );
+ list<list< int > >& groups = myIdsOnBoundary[ nodeSet ];
+ int nodeId = groups.front().front();
+ bool append = i;
+ if ( rev ) append = !append;
+ if ( append )
+ idsOnLink.push_back( nodeId );
+ else
+ idsOnLink.push_front( nodeId );
+ }
+ list< int >::iterator id = idsOnLink.begin();
+ for ( ; id != idsOnLink.end(); ++id ) // loop on XYZ ids on a link
+ {
+ list< TElemDef* >& elemDefs = myReverseConnectivity[ *id ]; // elems sharing id
+ list< TElemDef* >::iterator pElemDef = elemDefs.begin();
+ for ( ; pElemDef != elemDefs.end(); pElemDef++ ) // loop on elements sharing id
+ {
+ TElemDef* pIdList = *pElemDef; // ptr on list of ids making element up
+ // look for <id> in element definition
+ TElemDef::iterator idDef = find( pIdList->begin(), pIdList->end(), *id );
+ ASSERT ( idDef != pIdList->end() );
+ // look for 2 neighbour ids of <id> in element definition
+ for ( int prev = 0; prev < 2; ++prev ) {
+ TElemDef::iterator idDef2 = idDef;
+ if ( prev )
+ idDef2 = ( idDef2 == pIdList->begin() ) ? --pIdList->end() : --idDef2;
+ else
+ idDef2 = ( ++idDef2 == pIdList->end() ) ? pIdList->begin() : idDef2;
+ // look for idDef2 on a link starting from id
+ list< int >::iterator id2 = find( id, idsOnLink.end(), *idDef2 );
+ if ( id2 != idsOnLink.end() && id != --id2 ) { // found not next to id
+ // insert ids located on link between <id> and <id2>
+ // into the element definition between idDef and idDef2
+ if ( prev )
+ for ( ; id2 != id; --id2 )
+ pIdList->insert( idDef, *id2 );
+ else {
+ list< int >::iterator id1 = id;
+ for ( ++id1, ++id2; id1 != id2; ++id1 )
+ pIdList->insert( idDef2, *id1 );
+ }
+ }
+ }
+ }
+ }
+ // remove ids of link nodes
+ idsOnLink.pop_front();
+ idsOnLink.pop_back();
+ }
+ } // loop on myIdsOnBoundary
+ } // if ( toCreatePolygons )
+
+ if ( toCreatePolyedrs )
+ {
+ // check volumes adjacent to the refined elements
+ SMDS_VolumeTool volTool;
+ vector<const SMDS_MeshElement*>::iterator refinedElem = myElements.begin();
+ for ( ; refinedElem != myElements.end(); ++refinedElem )
+ {
+ // loop on nodes of refinedElem
+ SMDS_ElemIteratorPtr nIt = (*refinedElem)->nodesIterator();
+ while ( nIt->more() ) {
+ const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nIt->next() );
+ // loop on inverse elements of node
+ SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator();
+ while ( eIt->more() )
+ {
+ const SMDS_MeshElement* elem = eIt->next();
+ if ( !volTool.Set( elem ) || !avoidSet.insert( elem ).second )
+ continue; // skip faces or refined elements
+ // add polyhedron definition
+ myPolyhedronQuantities.push_back(vector<int> ());
+ myPolyElemXYZIDs.push_back(TElemDef());
+ vector<int>& quantity = myPolyhedronQuantities.back();
+ TElemDef & elemDef = myPolyElemXYZIDs.back();
+ // get definitions of new elements on volume faces
+ bool makePoly = false;
+ for ( int iF = 0; iF < volTool.NbFaces(); ++iF )
+ {
+ if ( getFacesDefinition(volTool.GetFaceNodes( iF ),
+ volTool.NbFaceNodes( iF ),
+ theNodes, elemDef, quantity))
+ makePoly = true;
+ }
+ if ( makePoly )
+ myPolyElems.push_back( elem );
+ else {
+ myPolyhedronQuantities.pop_back();
+ myPolyElemXYZIDs.pop_back();
+ }
+ }
+ }
+ }
+ }
+}
+
+//=======================================================================
+//function : getFacesDefinition
+//purpose : return faces definition for a volume face defined by theBndNodes
+//=======================================================================
+
+bool SMESH_Pattern::
+ getFacesDefinition(const SMDS_MeshNode** theBndNodes,
+ const int theNbBndNodes,
+ const vector< const SMDS_MeshNode* >& theNodes,
+ list< int >& theFaceDefs,
+ vector<int>& theQuantity)
+{
+ bool makePoly = false;
+// cout << "FROM FACE NODES: " <<endl;
+// for ( int i = 0; i < theNbBndNodes; ++i )
+// cout << theBndNodes[ i ];
+
+ set< const SMDS_MeshNode* > bndNodeSet;
+ for ( int i = 0; i < theNbBndNodes; ++i )
+ bndNodeSet.insert( theBndNodes[ i ]);
+
+ map< TNodeSet, list< list< int > > >::iterator nn_IdList;
+
+ // make a set of all nodes on a face
+ set< int > ids;
+ if ( !myIs2D ) { // for 2D, merge only edges
+ nn_IdList = myIdsOnBoundary.find( bndNodeSet );
+ if ( nn_IdList != myIdsOnBoundary.end() ) {
+ makePoly = true;
+ list< int > & faceIds = nn_IdList->second.front();
+ ids.insert( faceIds.begin(), faceIds.end() );
+ }
+ }
+ //bool hasIdsInFace = !ids.empty();
+
+ // add ids on links and bnd nodes
+ int lastFreeId = Max( myXYZIdToNodeMap.rbegin()->first, theNodes.size() );
+ TElemDef faceDef; // definition for the case if there is no new adjacent volumes
+ for ( int iN = 0; iN < theNbBndNodes; ++iN )
+ {
+ // add id of iN-th bnd node
+ TNodeSet nSet;
+ nSet.insert( theBndNodes[ iN ] );
+ nn_IdList = myIdsOnBoundary.find( nSet );
+ int bndId = ++lastFreeId;
+ if ( nn_IdList != myIdsOnBoundary.end() ) {
+ bndId = nn_IdList->second.front().front();
+ ids.insert( bndId );
+ }
+ else
+ myXYZIdToNodeMap.insert( make_pair( bndId, theBndNodes[ iN ] ));
+ faceDef.push_back( bndId );
+ // add ids on a link
+ TNodeSet linkNodes;
+ linkNodes.insert( theBndNodes[ iN ]);
+ linkNodes.insert( theBndNodes[ iN + 1 == theNbBndNodes ? 0 : iN + 1 ]);
+ nn_IdList = myIdsOnBoundary.find( linkNodes );
+ if ( nn_IdList != myIdsOnBoundary.end() ) {
+ makePoly = true;
+ list< int > & linkIds = nn_IdList->second.front();
+ ids.insert( linkIds.begin(), linkIds.end() );
+ if ( isReversed( theBndNodes[ iN ], linkIds ))
+ faceDef.insert( faceDef.end(), linkIds.begin(), linkIds.end() );
+ else
+ faceDef.insert( faceDef.end(), linkIds.rbegin(), linkIds.rend() );
+ }
+ }
+
+ // find faces definition of new volumes
+
+ bool defsAdded = false;
+ if ( !myIs2D ) { // for 2D, merge only edges
+ SMDS_VolumeTool vol;
+ set< TElemDef* > checkedVolDefs;
+ set< int >::iterator id = ids.begin();
+ for ( ; id != ids.end(); ++id )
+ {
+ // definitions of volumes sharing id
+ list< TElemDef* >& defList = myReverseConnectivity[ *id ];
+ ASSERT( !defList.empty() );
+ // loop on volume definitions
+ list< TElemDef* >::iterator pIdList = defList.begin();
+ for ( ; pIdList != defList.end(); ++pIdList)
+ {
+ if ( !checkedVolDefs.insert( *pIdList ).second )
+ continue; // skip already checked volume definition
+ vector< int > idVec;
+ idVec.reserve( (*pIdList)->size() );
+ idVec.insert( idVec.begin(), (*pIdList)->begin(), (*pIdList)->end() );
+ // loop on face defs of a volume
+ SMDS_VolumeTool::VolumeType volType = vol.GetType( idVec.size() );
+ if ( volType == SMDS_VolumeTool::UNKNOWN )
+ continue;
+ int nbFaces = vol.NbFaces( volType );
+ for ( int iF = 0; iF < nbFaces; ++iF )
+ {
+ const int* nodeInds = vol.GetFaceNodesIndices( volType, iF, true );
+ int iN, nbN = vol.NbFaceNodes( volType, iF );
+ // check if all nodes of a faces are in <ids>
+ bool all = true;
+ for ( iN = 0; iN < nbN && all; ++iN ) {
+ int nodeId = idVec[ nodeInds[ iN ]];
+ all = ( ids.find( nodeId ) != ids.end() );
+ }
+ if ( all ) {
+ // store a face definition
+ for ( iN = 0; iN < nbN; ++iN ) {
+ theFaceDefs.push_back( idVec[ nodeInds[ iN ]]);
+ }
+ theQuantity.push_back( nbN );
+ defsAdded = true;
+ }
+ }
+ }
+ }
+ }
+ if ( !defsAdded ) {
+ theQuantity.push_back( faceDef.size() );
+ theFaceDefs.splice( theFaceDefs.end(), faceDef, faceDef.begin(), faceDef.end() );
+ }
+
+ return makePoly;
+}
+