+//=======================================================================
+//function : Apply
+//purpose : Compute nodes coordinates applying
+// the loaded pattern to <theFace>. The first key-point
+// will be mapped into <theNodeIndexOnKeyPoint1>-th node
+//=======================================================================
+
+bool SMESH_Pattern::Apply (const SMDS_MeshFace* theFace,
+ const int theNodeIndexOnKeyPoint1,
+ const bool theReverse)
+{
+ MESSAGE(" ::Apply(MeshFace) " );
+
+ 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 = static_cast<const SMDS_MeshNode*>( 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 );
+ }
+ list< gp_XYZ > xyzList;
+ myOrderedNodes.resize( theFace->NbNodes() );
+ for ( iSub = 0, n = nodes.begin(); n != nodes.end(); ++n ) {
+ xyzList.push_back( gp_XYZ( (*n)->X(), (*n)->Y(), (*n)->Z() ));
+ myOrderedNodes[ iSub++] = *n;
+ }
+
+ // Define a face plane
+
+ list< gp_XYZ >::iterator xyzIt = xyzList.begin();
+ gp_Pnt P ( *xyzIt++ );
+ gp_Vec Vx( P, *xyzIt++ ), N;
+ do {
+ N = Vx ^ gp_Vec( P, *xyzIt++ );
+ } while ( N.SquareMagnitude() <= DBL_MIN && xyzIt != xyzList.end() );
+ if ( N.SquareMagnitude() <= DBL_MIN )
+ return setErrorCode( ERR_APPLF_BAD_FACE_GEOM );
+ gp_Ax2 pos( P, N, Vx );
+
+ // Compute UV of key-points on a plane
+ for ( xyzIt = xyzList.begin(), iSub = 1; xyzIt != xyzList.end(); xyzIt++, iSub++ )
+ {
+ gp_Vec vec ( pos.Location(), *xyzIt );
+ TPoint* p = getShapePoints( iSub ).front();
+ p->myUV.SetX( vec * pos.XDirection() );
+ p->myUV.SetY( vec * pos.YDirection() );
+ p->myXYZ = *xyzIt;
+ }
+
+ // 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 ( xyzIt = xyzList.begin(); xyzIt != xyzList.end(); iSub++ )
+ {
+ gp_XYZ& xyz1 = *xyzIt++;
+ gp_XYZ& xyz2 = ( xyzIt != xyzList.end() ) ? *xyzIt : xyzList.front();
+
+ 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->myXYZ = xyz1 * ( 1 - p->myInitU ) + xyz2 * p->myInitU;
+ gp_Vec vec ( pos.Location(), p->myXYZ );
+ p->myUV.SetX( vec * pos.XDirection() );
+ p->myUV.SetY( vec * pos.YDirection() );
+ }
+ // 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++ )
+ {
+ (*pIt)->myXYZ = ElSLib::PlaneValue( (*pIt)->myUV.X(), (*pIt)->myUV.Y(), pos );
+ }
+
+ 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 : mergePoints
+//purpose : Look for coincident points between myXYZs indexed with
+// list<int> of each element of xyzIndGroups. Coincident indices
+// are merged in myElemXYZIDs.
+//=======================================================================
+
+void SMESH_Pattern::mergePoints (map<TNodeSet, list<list<int> > >& indGroups,
+ map< int, list< list< int >* > > & reverseConnectivity)
+{
+ map< TNodeSet, list< list< int > > >::iterator indListIt;
+ for ( indListIt = indGroups.begin(); indListIt != indGroups.end(); indListIt++ )
+ {
+ list<list< int > > groups = indListIt->second;
+ if ( groups.size() < 2 )
+ continue;
+
+// const TNodeSet & nodes = indListIt->first;
+// TNodeSet::const_iterator n = nodes.begin();
+// for ( ; n != nodes.end(); n++ )
+// cout << *n ;
+
+ // find tolerance
+ Bnd_Box box;
+ list< int >& indices = groups.front();
+ list< int >::iterator ind, ind1, ind2;
+ for ( ind = indices.begin(); ind != indices.end(); ind++ )
+ box.Add( gp_Pnt( myXYZ[ *ind ]));
+ 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 );
+ double tol2 = 1.e-4 * p.SquareDistance( P );
+
+ // compare points, replace indices
+
+ 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( reverseConnectivity.find( *ind2 ) != reverseConnectivity.end() );
+ list< list< int >* > & elemXYZIDsList = reverseConnectivity[ *ind2 ];
+ list< list< int >* >::iterator elemXYZIDs = elemXYZIDsList.begin();
+ for ( ; elemXYZIDs != elemXYZIDsList.end(); elemXYZIDs++ )
+ {
+ ind = find( (*elemXYZIDs)->begin(), (*elemXYZIDs)->end(), *ind2 );
+ //MESSAGE( " Replace " << *ind << " with " << *ind1 );
+ myXYZ[ *ind ] = undefinedXYZ();
+ *ind = *ind1;
+ }
+ ind2 = indices2.erase( ind2 );
+ }
+ else
+ ind2++;
+ }
+ }
+ }
+ }
+ }
+}
+
+//=======================================================================
+//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 (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 );
+ }
+
+ myXYZ.clear();
+ myElemXYZIDs.clear();
+ myXYZIdToNodeMap.clear();
+ myElements.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 ));
+
+ // to merge nodes on edges of the elements being refined
+ typedef set<const SMDS_MeshNode*> TLink;
+ map< TLink, list< list< int > > > linkPointIndListMap;
+ map< int, list< list< int >* > > reverseConnectivity;
+
+ int ind1 = 0; // lowest point index for a face
+
+ // apply to each face in theFaces set
+ set<const SMDS_MeshFace*>::iterator face = theFaces.begin();
+ for ( ; face != theFaces.end(); ++face )
+ {
+ if ( !Apply( *face, theNodeIndexOnKeyPoint1, theReverse )) {
+ MESSAGE( "Failed on " << *face );
+ continue;
+ }
+ myElements.push_back( *face );
+
+ // store computed points belonging to elements
+ list< list< int > >::iterator ll = myElemPointIDs.begin();
+ for ( ; ll != myElemPointIDs.end(); ++ll )
+ {
+ myElemXYZIDs.push_back();
+ list< int >& xyzIds = myElemXYZIDs.back();
+ list< int >& pIds = *ll;
+ for ( list<int>::iterator id = pIds.begin(); id != pIds.end(); id++ ) {
+ int pIndex = *id + ind1;
+ xyzIds.push_back( pIndex );
+ myXYZ[ pIndex ] = myPoints[ *id ].myXYZ.XYZ();
+ reverseConnectivity[ pIndex ].push_back( & xyzIds );
+ }
+ }
+ // put points on links to linkPointIndListMap
+ int nbNodes = (*face)->NbNodes(), eID = nbNodes + 1;
+ for ( int i = 0; i < nbNodes; i++ )
+ {
+ const SMDS_MeshNode* n1 = myOrderedNodes[ i ];
+ const SMDS_MeshNode* n2 = myOrderedNodes[ i + 1 == nbNodes ? 0 : i + 1 ];
+ // make a link of node pointers
+ TLink link;
+ link.insert( n1 );
+ link.insert( n2 );
+ // add the link to the map
+ list< list< int > >& groups = linkPointIndListMap[ link ];
+ groups.push_back();
+ list< int >& indList = groups.back();
+ list< TPoint* > & linkPoints = getShapePoints( eID++ );
+ list< TPoint* >::iterator p = linkPoints.begin();
+ // map the first link point to n1
+ myXYZIdToNodeMap[ pointIndex[ *p ] + ind1 ] = n1;
+ // add points to the map excluding the end points
+ for ( p++; *p != linkPoints.back(); p++ )
+ indList.push_back( pointIndex[ *p ] + ind1 );
+ }
+ ind1 += myPoints.size();
+ }
+
+ mergePoints( linkPointIndListMap, reverseConnectivity );
+
+ 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 );
+ }
+
+ myXYZ.clear();
+ myElemXYZIDs.clear();
+ myXYZIdToNodeMap.clear();
+ myElements.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 ));
+
+ // to merge nodes on edges and faces of the elements being refined
+ map< TNodeSet, list< list< int > > > subPointIndListMap;
+ map< int, list< list< int >* > > reverseConnectivity;
+
+ 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< list< int > >::iterator ll = myElemPointIDs.begin();
+ for ( ; ll != myElemPointIDs.end(); ++ll )
+ {
+ myElemXYZIDs.push_back();
+ list< int >& xyzIds = myElemXYZIDs.back();
+ list< int >& pIds = *ll;
+ for ( list<int>::iterator id = pIds.begin(); id != pIds.end(); id++ ) {
+ int pIndex = *id + ind1;
+ xyzIds.push_back( pIndex );
+ myXYZ[ pIndex ] = myPoints[ *id ].myXYZ.XYZ();
+ reverseConnectivity[ pIndex ].push_back( & xyzIds );
+ }
+ }
+ // put points on edges and faces to subPointIndListMap
+ 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 )) {
+ // use nodes of refined volumes for merge
+ }
+ 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 ]);
+ }
+ list< list< int > >& groups = subPointIndListMap[ subNodes ];
+ groups.push_back();
+ list< int >& indList = groups.back();
+ // add points
+ list< TPoint* > & points = getShapePoints( Id );
+ list< TPoint* >::iterator p = points.begin();
+ if ( subNodes.empty() ) // vertex case
+ myXYZIdToNodeMap[ pointIndex[ *p ] + ind1 ] = myOrderedNodes[ Id - 1 ];
+ else
+ for ( ; p != points.end(); p++ )
+ indList.push_back( pointIndex[ *p ] + ind1 );
+ }
+ ind1 += myPoints.size();
+ }
+
+ mergePoints( subPointIndListMap, reverseConnectivity );
+
+ return !myElemXYZIDs.empty();
+}
+