+// the loaded pattern to <theFace>. The first key-point
+// will be mapped into <theNodeIndexOnKeyPoint1>-th node
+//=======================================================================
+
+bool SMESH_Pattern::Apply (SMESH_Mesh* theMesh,
+ const SMDS_MeshFace* theFace,
+ const TopoDS_Shape& theSurface,
+ const int theNodeIndexOnKeyPoint1,
+ const bool theReverse)
+{
+// MESSAGE(" ::Apply(MeshFace) " );
+ if ( theSurface.IsNull() || theSurface.ShapeType() != TopAbs_FACE ) {
+ return Apply( theFace, theNodeIndexOnKeyPoint1, theReverse);
+ }
+ const TopoDS_Face& face = TopoDS::Face( theSurface );
+ TopLoc_Location loc;
+ Handle(Geom_Surface) surface = BRep_Tool::Surface( face, loc );
+ const gp_Trsf & aTrsf = loc.Transformation();
+
+ if ( !IsLoaded() ) {
+ MESSAGE( "Pattern not loaded" );
+ return setErrorCode( ERR_APPL_NOT_LOADED );
+ }
+
+ // check nb of nodes
+ if (theFace->NbNodes() != myNbKeyPntInBoundary.front() ) {
+ MESSAGE( myKeyPointIDs.size() << " != " << theFace->NbNodes() );
+ return setErrorCode( ERR_APPL_BAD_NB_VERTICES );
+ }
+
+ // find points on edges, it fills myNbKeyPntInBoundary
+ if ( !findBoundaryPoints() )
+ return false;
+
+ // check that there are no holes in a pattern
+ if (myNbKeyPntInBoundary.size() > 1 ) {
+ return setErrorCode( ERR_APPL_BAD_NB_VERTICES );
+ }
+
+ // Define the nodes order
+
+ list< const SMDS_MeshNode* > nodes;
+ list< const SMDS_MeshNode* >::iterator n = nodes.end();
+ SMDS_ElemIteratorPtr noIt = theFace->nodesIterator();
+ int iSub = 0;
+ while ( noIt->more() ) {
+ const SMDS_MeshNode* node = smdsNode( noIt->next() );
+ nodes.push_back( node );
+ if ( iSub++ == theNodeIndexOnKeyPoint1 )
+ n = --nodes.end();
+ }
+ if ( n != nodes.end() ) {
+ if ( theReverse ) {
+ if ( n != --nodes.end() )
+ nodes.splice( nodes.begin(), nodes, ++n, nodes.end() );
+ nodes.reverse();
+ }
+ else if ( n != nodes.begin() )
+ nodes.splice( nodes.end(), nodes, nodes.begin(), n );
+ }
+
+ // find a node not on a seam edge, if necessary
+ SMESH_MesherHelper helper( *theMesh );
+ helper.SetSubShape( theSurface );
+ const SMDS_MeshNode* inFaceNode = 0;
+ if ( helper.GetNodeUVneedInFaceNode() )
+ {
+ SMESH_MeshEditor editor( theMesh );
+ for ( n = nodes.begin(); ( !inFaceNode && n != nodes.end()); ++n ) {
+ int shapeID = editor.FindShape( *n );
+ if ( !shapeID )
+ return Apply( theFace, theNodeIndexOnKeyPoint1, theReverse);
+ if ( !helper.IsSeamShape( shapeID ))
+ inFaceNode = *n;
+ }
+ }
+
+ // Set UV of key-points (i.e. of nodes of theFace )
+ vector< gp_XY > keyUV( theFace->NbNodes() );
+ myOrderedNodes.resize( theFace->NbNodes() );
+ for ( iSub = 1, n = nodes.begin(); n != nodes.end(); ++n, ++iSub )
+ {
+ TPoint* p = getShapePoints( iSub ).front();
+ p->myUV = helper.GetNodeUV( face, *n, inFaceNode );
+ p->myXYZ = gp_XYZ( (*n)->X(), (*n)->Y(), (*n)->Z() );
+
+ keyUV[ iSub-1 ] = p->myUV;
+ myOrderedNodes[ iSub-1 ] = *n;
+ }
+
+ // points on edges to be used for UV computation of in-face points
+ list< list< TPoint* > > edgesPointsList;
+ edgesPointsList.push_back( list< TPoint* >() );
+ list< TPoint* > * edgesPoints = & edgesPointsList.back();
+ list< TPoint* >::iterator pIt;
+
+ // compute UV and XYZ of points on edges
+
+ for ( int i = 0; i < myOrderedNodes.size(); ++i, ++iSub )
+ {
+ gp_XY& uv1 = keyUV[ i ];
+ gp_XY& uv2 = ( i+1 < keyUV.size() ) ? keyUV[ i+1 ] : keyUV[ 0 ];
+
+ list< TPoint* > & ePoints = getShapePoints( iSub );
+ ePoints.back()->myInitU = 1.0;
+ list< TPoint* >::const_iterator pIt = ++ePoints.begin();
+ while ( *pIt != ePoints.back() )
+ {
+ TPoint* p = *pIt++;
+ p->myUV = uv1 * ( 1 - p->myInitU ) + uv2 * p->myInitU;
+ p->myXYZ = surface->Value( p->myUV.X(), p->myUV.Y() );
+ if ( !loc.IsIdentity() )
+ aTrsf.Transforms( p->myXYZ.ChangeCoord() );
+ }
+ // collect on-edge points (excluding the last one)
+ edgesPoints->insert( edgesPoints->end(), ePoints.begin(), --ePoints.end());
+ }
+
+ // Compute UV and XYZ of in-face points
+
+ // try to use a simple algo to compute UV
+ list< TPoint* > & fPoints = getShapePoints( iSub );
+ bool isDeformed = false;
+ for ( pIt = fPoints.begin(); !isDeformed && pIt != fPoints.end(); pIt++ )
+ if ( !compUVByIsoIntersection( edgesPointsList, (*pIt)->myInitUV,
+ (*pIt)->myUV, isDeformed )) {
+ MESSAGE("cant Apply(face)");
+ return false;
+ }
+ // try to use a complex algo if it is a difficult case
+ if ( isDeformed && !compUVByElasticIsolines( edgesPointsList, fPoints ))
+ {
+ for ( ; pIt != fPoints.end(); pIt++ ) // continue with the simple algo
+ if ( !compUVByIsoIntersection( edgesPointsList, (*pIt)->myInitUV,
+ (*pIt)->myUV, isDeformed )) {
+ MESSAGE("cant Apply(face)");
+ return false;
+ }
+ }
+
+ for ( pIt = fPoints.begin(); pIt != fPoints.end(); pIt++ )
+ {
+ TPoint * point = *pIt;
+ point->myXYZ = surface->Value( point->myUV.X(), point->myUV.Y() );
+ if ( !loc.IsIdentity() )
+ aTrsf.Transforms( point->myXYZ.ChangeCoord() );
+ }
+
+ myIsComputed = true;
+
+ return setErrorCode( ERR_OK );
+}
+
+//=======================================================================
+//function : undefinedXYZ
+//purpose :
+//=======================================================================
+
+static const gp_XYZ& undefinedXYZ()
+{
+ static gp_XYZ xyz( 1.e100, 0., 0. );
+ return xyz;
+}
+
+//=======================================================================
+//function : isDefined
+//purpose :
+//=======================================================================
+
+inline static bool isDefined(const gp_XYZ& theXYZ)
+{
+ return theXYZ.X() < 1.e100;
+}
+
+//=======================================================================
+//function : Apply
+//purpose : Compute nodes coordinates applying
+// the loaded pattern to <theFaces>. The first key-point
+// will be mapped into <theNodeIndexOnKeyPoint1>-th node
+//=======================================================================
+
+bool SMESH_Pattern::Apply (SMESH_Mesh* theMesh,
+ std::set<const SMDS_MeshFace*>& theFaces,
+ const int theNodeIndexOnKeyPoint1,
+ const bool theReverse)
+{
+ MESSAGE(" ::Apply(set<MeshFace>) " );
+
+ if ( !IsLoaded() ) {
+ MESSAGE( "Pattern not loaded" );
+ return setErrorCode( ERR_APPL_NOT_LOADED );
+ }
+
+ // find points on edges, it fills myNbKeyPntInBoundary
+ if ( !findBoundaryPoints() )
+ return false;
+
+ // check that there are no holes in a pattern
+ if (myNbKeyPntInBoundary.size() > 1 ) {
+ return setErrorCode( ERR_APPL_BAD_NB_VERTICES );
+ }
+
+ myShape.Nullify();
+ myXYZ.clear();
+ myElemXYZIDs.clear();
+ myXYZIdToNodeMap.clear();
+ myElements.clear();
+ myIdsOnBoundary.clear();
+ myReverseConnectivity.clear();
+
+ myXYZ.resize( myPoints.size() * theFaces.size(), undefinedXYZ() );
+ myElements.reserve( theFaces.size() );
+
+ // to find point index
+ map< TPoint*, int > pointIndex;
+ for ( int i = 0; i < myPoints.size(); i++ )
+ pointIndex.insert( make_pair( & myPoints[ i ], i ));
+
+ int ind1 = 0; // lowest point index for a face
+
+ // meshed geometry
+ TopoDS_Shape shape;
+// int shapeID = 0;
+// SMESH_MeshEditor editor( theMesh );
+
+ // apply to each face in theFaces set
+ set<const SMDS_MeshFace*>::iterator face = theFaces.begin();
+ for ( ; face != theFaces.end(); ++face )
+ {
+// int curShapeId = editor.FindShape( *face );
+// if ( curShapeId != shapeID ) {
+// if ( curShapeId )
+// shape = theMesh->GetMeshDS()->IndexToShape( curShapeId );
+// else
+// shape.Nullify();
+// shapeID = curShapeId;
+// }
+ bool ok;
+ if ( shape.IsNull() )
+ ok = Apply( *face, theNodeIndexOnKeyPoint1, theReverse );
+ else
+ ok = Apply( theMesh, *face, shape, theNodeIndexOnKeyPoint1, theReverse );
+ if ( !ok ) {
+ MESSAGE( "Failed on " << *face );
+ continue;
+ }
+ myElements.push_back( *face );
+
+ // store computed points belonging to elements
+ list< TElemDef >::iterator ll = myElemPointIDs.begin();
+ for ( ; ll != myElemPointIDs.end(); ++ll )
+ {
+ myElemXYZIDs.push_back(TElemDef());
+ TElemDef& xyzIds = myElemXYZIDs.back();
+ TElemDef& pIds = *ll;
+ for ( TElemDef::iterator id = pIds.begin(); id != pIds.end(); id++ ) {
+ int pIndex = *id + ind1;
+ xyzIds.push_back( pIndex );
+ myXYZ[ pIndex ] = myPoints[ *id ].myXYZ.XYZ();
+ myReverseConnectivity[ pIndex ].push_back( & xyzIds );
+ }
+ }
+ // put points on links to myIdsOnBoundary,
+ // they will be used to sew new elements on adjacent refined elements
+ int nbNodes = (*face)->NbNodes(), eID = nbNodes + 1;
+ for ( int i = 0; i < nbNodes; i++ )
+ {
+ list< TPoint* > & linkPoints = getShapePoints( eID++ );
+ const SMDS_MeshNode* n1 = myOrderedNodes[ i ];
+ const SMDS_MeshNode* n2 = myOrderedNodes[ i + 1 == nbNodes ? 0 : i + 1 ];
+ // make a link and a node set
+ TNodeSet linkSet, node1Set;
+ linkSet.insert( n1 );
+ linkSet.insert( n2 );
+ node1Set.insert( n1 );
+ list< TPoint* >::iterator p = linkPoints.begin();
+ {
+ // map the first link point to n1
+ int nId = pointIndex[ *p ] + ind1;
+ myXYZIdToNodeMap[ nId ] = n1;
+ list< list< int > >& groups = myIdsOnBoundary[ node1Set ];
+ groups.push_back(list< int > ());
+ groups.back().push_back( nId );
+ }
+ // add the linkSet to the map
+ list< list< int > >& groups = myIdsOnBoundary[ linkSet ];
+ groups.push_back(list< int > ());
+ list< int >& indList = groups.back();
+ // add points to the map excluding the end points
+ for ( p++; *p != linkPoints.back(); p++ )
+ indList.push_back( pointIndex[ *p ] + ind1 );
+ }
+ ind1 += myPoints.size();
+ }
+
+ return !myElemXYZIDs.empty();
+}
+
+//=======================================================================
+//function : Apply
+//purpose : Compute nodes coordinates applying
+// the loaded pattern to <theVolumes>. 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 (std::set<const SMDS_MeshVolume*> & theVolumes,
+ const int theNode000Index,
+ const int theNode001Index)
+{
+ MESSAGE(" ::Apply(set<MeshVolumes>) " );
+
+ if ( !IsLoaded() ) {
+ MESSAGE( "Pattern not loaded" );
+ return setErrorCode( ERR_APPL_NOT_LOADED );
+ }
+
+ // bind ID to points
+ if ( !findBoundaryPoints() )
+ return false;
+
+ // check that there are no holes in a pattern
+ if (myNbKeyPntInBoundary.size() > 1 ) {
+ return setErrorCode( ERR_APPL_BAD_NB_VERTICES );
+ }
+
+ myShape.Nullify();
+ myXYZ.clear();
+ myElemXYZIDs.clear();
+ myXYZIdToNodeMap.clear();
+ myElements.clear();
+ myIdsOnBoundary.clear();
+ myReverseConnectivity.clear();
+
+ myXYZ.resize( myPoints.size() * theVolumes.size(), undefinedXYZ() );
+ myElements.reserve( theVolumes.size() );
+
+ // to find point index
+ map< TPoint*, int > pointIndex;
+ for ( int i = 0; i < myPoints.size(); i++ )
+ pointIndex.insert( make_pair( & myPoints[ i ], i ));
+
+ int ind1 = 0; // lowest point index for an element
+
+ // apply to each element in theVolumes set
+ set<const SMDS_MeshVolume*>::iterator vol = theVolumes.begin();
+ for ( ; vol != theVolumes.end(); ++vol )
+ {
+ if ( !Apply( *vol, theNode000Index, theNode001Index )) {
+ MESSAGE( "Failed on " << *vol );
+ continue;
+ }
+ myElements.push_back( *vol );
+
+ // store computed points belonging to elements
+ list< TElemDef >::iterator ll = myElemPointIDs.begin();
+ for ( ; ll != myElemPointIDs.end(); ++ll )
+ {
+ myElemXYZIDs.push_back(TElemDef());
+ TElemDef& xyzIds = myElemXYZIDs.back();
+ TElemDef& pIds = *ll;
+ for ( TElemDef::iterator id = pIds.begin(); id != pIds.end(); id++ ) {
+ int pIndex = *id + ind1;
+ xyzIds.push_back( pIndex );
+ myXYZ[ pIndex ] = myPoints[ *id ].myXYZ.XYZ();
+ myReverseConnectivity[ pIndex ].push_back( & xyzIds );
+ }
+ }
+ // put points on edges and faces to myIdsOnBoundary,
+ // they will be used to sew new elements on adjacent refined elements
+ for ( int Id = SMESH_Block::ID_V000; Id <= SMESH_Block::ID_F1yz; Id++ )
+ {
+ // make a set of sub-points
+ TNodeSet subNodes;
+ vector< int > subIDs;
+ if ( SMESH_Block::IsVertexID( Id )) {
+ subNodes.insert( myOrderedNodes[ Id - 1 ]);
+ }
+ else if ( SMESH_Block::IsEdgeID( Id )) {
+ SMESH_Block::GetEdgeVertexIDs( Id, subIDs );
+ subNodes.insert( myOrderedNodes[ subIDs.front() - 1 ]);
+ subNodes.insert( myOrderedNodes[ subIDs.back() - 1 ]);
+ }
+ else {
+ SMESH_Block::GetFaceEdgesIDs( Id, subIDs );
+ int e1 = subIDs[ 0 ], e2 = subIDs[ 1 ];
+ SMESH_Block::GetEdgeVertexIDs( e1, subIDs );
+ subNodes.insert( myOrderedNodes[ subIDs.front() - 1 ]);
+ subNodes.insert( myOrderedNodes[ subIDs.back() - 1 ]);
+ SMESH_Block::GetEdgeVertexIDs( e2, subIDs );
+ subNodes.insert( myOrderedNodes[ subIDs.front() - 1 ]);
+ subNodes.insert( myOrderedNodes[ subIDs.back() - 1 ]);
+ }
+ // add points
+ list< TPoint* > & points = getShapePoints( Id );
+ list< TPoint* >::iterator p = points.begin();
+ list< list< int > >& groups = myIdsOnBoundary[ subNodes ];
+ groups.push_back(list< int > ());
+ list< int >& indList = groups.back();
+ for ( ; p != points.end(); p++ )
+ indList.push_back( pointIndex[ *p ] + ind1 );
+ if ( subNodes.size() == 1 ) // vertex case
+ myXYZIdToNodeMap[ indList.back() ] = myOrderedNodes[ Id - 1 ];
+ }
+ ind1 += myPoints.size();
+ }
+
+ return !myElemXYZIDs.empty();
+}
+
+//=======================================================================
+//function : Load
+//purpose : Create a pattern from the mesh built on <theBlock>
+//=======================================================================
+
+bool SMESH_Pattern::Load (SMESH_Mesh* theMesh,
+ const TopoDS_Shell& theBlock)
+{
+ MESSAGE(" ::Load(volume) " );
+ Clear();
+ myIs2D = false;
+ SMESHDS_SubMesh * aSubMesh;
+
+ // load shapes in myShapeIDMap
+ SMESH_Block block;
+ TopoDS_Vertex v1, v2;
+ if ( !block.LoadBlockShapes( theBlock, v1, v2, myShapeIDMap ))
+ return setErrorCode( ERR_LOADV_BAD_SHAPE );
+
+ // count nodes
+ int nbNodes = 0, shapeID;
+ for ( shapeID = 1; shapeID <= myShapeIDMap.Extent(); shapeID++ )
+ {
+ const TopoDS_Shape& S = myShapeIDMap( shapeID );
+ aSubMesh = getSubmeshWithElements( theMesh, S );
+ if ( aSubMesh )
+ nbNodes += aSubMesh->NbNodes();
+ }
+ myPoints.resize( nbNodes );
+
+ // load U of points on edges
+ TNodePointIDMap nodePointIDMap;
+ int iPoint = 0;
+ for ( shapeID = 1; shapeID <= myShapeIDMap.Extent(); shapeID++ )
+ {
+ const TopoDS_Shape& S = myShapeIDMap( shapeID );
+ list< TPoint* > & shapePoints = getShapePoints( shapeID );
+ aSubMesh = getSubmeshWithElements( theMesh, S );
+ if ( ! aSubMesh ) continue;
+ SMDS_NodeIteratorPtr nIt = aSubMesh->GetNodes();
+ if ( !nIt->more() ) continue;
+
+ // store a node and a point
+ while ( nIt->more() ) {
+ const SMDS_MeshNode* node = smdsNode( nIt->next() );
+ nodePointIDMap.insert( make_pair( node, iPoint ));
+ if ( block.IsVertexID( shapeID ))
+ myKeyPointIDs.push_back( iPoint );
+ TPoint* p = & myPoints[ iPoint++ ];
+ shapePoints.push_back( p );
+ p->myXYZ.SetCoord( node->X(), node->Y(), node->Z() );
+ p->myInitXYZ.SetCoord( 0,0,0 );
+ }
+ list< TPoint* >::iterator pIt = shapePoints.begin();
+
+ // compute init XYZ
+ switch ( S.ShapeType() )
+ {
+ case TopAbs_VERTEX:
+ case TopAbs_EDGE: {
+
+ for ( ; pIt != shapePoints.end(); pIt++ ) {
+ double * coef = block.GetShapeCoef( shapeID );
+ for ( int iCoord = 1; iCoord <= 3; iCoord++ )
+ if ( coef[ iCoord - 1] > 0 )
+ (*pIt)->myInitXYZ.SetCoord( iCoord, 1. );
+ }
+ if ( S.ShapeType() == TopAbs_VERTEX )
+ break;
+
+ const TopoDS_Edge& edge = TopoDS::Edge( S );
+ double f,l;
+ BRep_Tool::Range( edge, f, l );
+ int iCoord = SMESH_Block::GetCoordIndOnEdge( shapeID );
+ bool isForward = SMESH_Block::IsForwardEdge( edge, myShapeIDMap );
+ pIt = shapePoints.begin();
+ nIt = aSubMesh->GetNodes();
+ for ( ; nIt->more(); pIt++ )
+ {
+ const SMDS_MeshNode* node = smdsNode( nIt->next() );
+ const SMDS_EdgePosition* epos =
+ static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
+ double u = ( epos->GetUParameter() - f ) / ( l - f );
+ (*pIt)->myInitXYZ.SetCoord( iCoord, isForward ? u : 1 - u );
+ }
+ break;
+ }
+ default:
+ for ( ; pIt != shapePoints.end(); pIt++ )
+ {
+ if ( !block.ComputeParameters( (*pIt)->myXYZ, (*pIt)->myInitXYZ, shapeID )) {
+ MESSAGE( "!block.ComputeParameters()" );
+ return setErrorCode( ERR_LOADV_COMPUTE_PARAMS );
+ }
+ }
+ }
+ } // loop on block sub-shapes
+
+ // load elements
+
+ aSubMesh = getSubmeshWithElements( theMesh, theBlock );
+ if ( aSubMesh )
+ {
+ SMDS_ElemIteratorPtr elemIt = aSubMesh->GetElements();
+ while ( elemIt->more() ) {
+ SMDS_ElemIteratorPtr nIt = elemIt->next()->nodesIterator();
+ myElemPointIDs.push_back( TElemDef() );
+ TElemDef& elemPoints = myElemPointIDs.back();
+ while ( nIt->more() )
+ elemPoints.push_back( nodePointIDMap[ nIt->next() ]);
+ }
+ }
+
+ myIsBoundaryPointsFound = true;
+
+ return setErrorCode( ERR_OK );
+}
+
+//=======================================================================
+//function : getSubmeshWithElements
+//purpose : return submesh containing elements bound to theBlock in theMesh
+//=======================================================================
+
+SMESHDS_SubMesh * SMESH_Pattern::getSubmeshWithElements(SMESH_Mesh* theMesh,
+ const TopoDS_Shape& theShape)
+{
+ SMESHDS_SubMesh * aSubMesh = theMesh->GetMeshDS()->MeshElements( theShape );
+ if ( aSubMesh && ( aSubMesh->GetElements()->more() || aSubMesh->GetNodes()->more() ))
+ return aSubMesh;
+
+ if ( theShape.ShapeType() == TopAbs_SHELL )
+ {
+ // look for submesh of VOLUME
+ TopTools_ListIteratorOfListOfShape it( theMesh->GetAncestors( theShape ));
+ for (; it.More(); it.Next()) {
+ aSubMesh = theMesh->GetMeshDS()->MeshElements( it.Value() );
+ if ( aSubMesh && ( aSubMesh->GetElements()->more() || aSubMesh->GetNodes()->more() ))
+ return aSubMesh;
+ }
+ }
+ return 0;
+}
+
+
+//=======================================================================
+//function : Apply
+//purpose : Compute nodes coordinates applying
+// the loaded pattern to <theBlock>. The (0,0,0) key-point
+// will be mapped into <theVertex000>. The (0,0,1)
+// fifth key-point will be mapped into <theVertex001>.
+//=======================================================================
+
+bool SMESH_Pattern::Apply (const TopoDS_Shell& theBlock,
+ const TopoDS_Vertex& theVertex000,
+ const TopoDS_Vertex& theVertex001)
+{
+ MESSAGE(" ::Apply(volume) " );
+
+ if (!findBoundaryPoints() || // bind ID to points
+ !setShapeToMesh( theBlock )) // check theBlock is a suitable shape
+ return false;
+
+ SMESH_Block block; // bind ID to shape
+ if (!block.LoadBlockShapes( theBlock, theVertex000, theVertex001, myShapeIDMap ))
+ return setErrorCode( ERR_APPLV_BAD_SHAPE );
+
+ // compute XYZ of points on shapes
+
+ for ( int shapeID = 1; shapeID <= myShapeIDMap.Extent(); shapeID++ )
+ {
+ list< TPoint* > & shapePoints = getShapePoints( shapeID );
+ list< TPoint* >::iterator pIt = shapePoints.begin();
+ const TopoDS_Shape& S = myShapeIDMap( shapeID );
+ switch ( S.ShapeType() )
+ {
+ case TopAbs_VERTEX: {
+
+ for ( ; pIt != shapePoints.end(); pIt++ )
+ block.VertexPoint( shapeID, (*pIt)->myXYZ.ChangeCoord() );
+ break;
+ }
+ case TopAbs_EDGE: {
+
+ for ( ; pIt != shapePoints.end(); pIt++ )
+ block.EdgePoint( shapeID, (*pIt)->myInitXYZ, (*pIt)->myXYZ.ChangeCoord() );
+ break;
+ }
+ case TopAbs_FACE: {
+
+ for ( ; pIt != shapePoints.end(); pIt++ )
+ block.FacePoint( shapeID, (*pIt)->myInitXYZ, (*pIt)->myXYZ.ChangeCoord() );
+ break;
+ }
+ default:
+ for ( ; pIt != shapePoints.end(); pIt++ )
+ block.ShellPoint( (*pIt)->myInitXYZ, (*pIt)->myXYZ.ChangeCoord() );
+ }
+ } // loop on block sub-shapes
+
+ myIsComputed = true;
+
+ return setErrorCode( ERR_OK );
+}
+
+//=======================================================================
+//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