+
+//=======================================================================
+//function : LoadIJNodes
+//purpose : Load nodes bound to theFace into column (vectors) and rows
+// of theIJNodes.
+// The value of theIJNodes map is a vector of ordered nodes so
+// that the 0-the one lies on theBaseEdge.
+// The key of theIJNodes map is a normalized parameter of each
+// 0-the node on theBaseEdge.
+//=======================================================================
+
+bool StdMeshers_Penta_3D::LoadIJNodes(StdMeshers_IJNodeMap & theIJNodes,
+ const TopoDS_Face& theFace,
+ const TopoDS_Edge& theBaseEdge,
+ SMESHDS_Mesh* theMesh)
+{
+ // get vertices of theBaseEdge
+ TopoDS_Vertex vfb, vlb, vft; // first and last, bottom and top vertices
+ TopoDS_Edge eFrw = TopoDS::Edge( theBaseEdge.Oriented( TopAbs_FORWARD ));
+ TopExp::Vertices( eFrw, vfb, vlb );
+
+ // find the other edges of theFace and orientation of e1
+ TopoDS_Edge e1, e2, eTop;
+ bool rev1, CumOri = false;
+ TopExp_Explorer exp( theFace, TopAbs_EDGE );
+ int nbEdges = 0;
+ for ( ; exp.More(); exp.Next() ) {
+ if ( ++nbEdges > NB_WALL_FACES ) {
+ return false; // more than 4 edges in theFace
+ }
+ TopoDS_Edge e = TopoDS::Edge( exp.Current() );
+ if ( theBaseEdge.IsSame( e ))
+ continue;
+ TopoDS_Vertex vCommon;
+ if ( !TopExp::CommonVertex( theBaseEdge, e, vCommon ))
+ eTop = e;
+ else if ( vCommon.IsSame( vfb )) {
+ e1 = e;
+ vft = TopExp::LastVertex( e1, CumOri );
+ rev1 = vfb.IsSame( vft );
+ if ( rev1 )
+ vft = TopExp::FirstVertex( e1, CumOri );
+ }
+ else
+ e2 = e;
+ }
+ if ( nbEdges < NB_WALL_FACES ) {
+ return false; // less than 4 edges in theFace
+ }
+
+ // submeshes corresponding to shapes
+ SMESHDS_SubMesh* smFace = theMesh->MeshElements( theFace );
+ SMESHDS_SubMesh* smb = theMesh->MeshElements( theBaseEdge );
+ SMESHDS_SubMesh* smt = theMesh->MeshElements( eTop );
+ SMESHDS_SubMesh* sm1 = theMesh->MeshElements( e1 );
+ SMESHDS_SubMesh* sm2 = theMesh->MeshElements( e2 );
+ SMESHDS_SubMesh* smVfb = theMesh->MeshElements( vfb );
+ SMESHDS_SubMesh* smVlb = theMesh->MeshElements( vlb );
+ SMESHDS_SubMesh* smVft = theMesh->MeshElements( vft );
+ if (!smFace || !smb || !smt || !sm1 || !sm2 || !smVfb || !smVlb || !smVft ) {
+ MESSAGE( "NULL submesh " <<smFace<<" "<<smb<<" "<<smt<<" "<<
+ sm1<<" "<<sm2<<" "<<smVfb<<" "<<smVlb<<" "<<smVft);
+ return false;
+ }
+ if ( smb->NbNodes() != smt->NbNodes() || sm1->NbNodes() != sm2->NbNodes() ) {
+ MESSAGE(" Diff nb of nodes on opposite edges" );
+ return false;
+ }
+ if (smVfb->NbNodes() != 1 || smVlb->NbNodes() != 1 || smVft->NbNodes() != 1) {
+ MESSAGE("Empty submesh of vertex");
+ return false;
+ }
+ if ( sm1->NbNodes() * smb->NbNodes() != smFace->NbNodes() ) {
+ // check quadratic case
+ if ( myCreateQuadratic ) {
+ int n1 = sm1->NbNodes()/2;
+ int n2 = smb->NbNodes()/2;
+ int n3 = sm1->NbNodes() - n1;
+ int n4 = smb->NbNodes() - n2;
+ int nf = sm1->NbNodes()*smb->NbNodes() - n3*n4;
+ if( nf != smFace->NbNodes() ) {
+ MESSAGE( "Wrong nb face nodes: " <<
+ sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
+ return false;
+ }
+ }
+ else {
+ MESSAGE( "Wrong nb face nodes: " <<
+ sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
+ return false;
+ }
+ }
+ // IJ size
+ int vsize = sm1->NbNodes() + 2;
+ int hsize = smb->NbNodes() + 2;
+ if(myCreateQuadratic) {
+ vsize = vsize - sm1->NbNodes()/2 -1;
+ hsize = hsize - smb->NbNodes()/2 -1;
+ }
+
+ // load nodes from theBaseEdge
+
+ set<const SMDS_MeshNode*> loadedNodes;
+ const SMDS_MeshNode* nullNode = 0;
+
+ vector<const SMDS_MeshNode*> & nVecf = theIJNodes[ 0.];
+ nVecf.resize( vsize, nullNode );
+ loadedNodes.insert( nVecf[ 0 ] = smVfb->GetNodes()->next() );
+
+ vector<const SMDS_MeshNode*> & nVecl = theIJNodes[ 1.];
+ nVecl.resize( vsize, nullNode );
+ loadedNodes.insert( nVecl[ 0 ] = smVlb->GetNodes()->next() );
+
+ double f, l;
+ BRep_Tool::Range( eFrw, f, l );
+ double range = l - f;
+ SMDS_NodeIteratorPtr nIt = smb->GetNodes();
+ const SMDS_MeshNode* node;
+ while ( nIt->more() ) {
+ node = nIt->next();
+ if(myTool->IsMedium(node))
+ continue;
+ const SMDS_EdgePosition* pos =
+ dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
+ if ( !pos ) {
+ return false;
+ }
+ double u = ( pos->GetUParameter() - f ) / range;
+ vector<const SMDS_MeshNode*> & nVec = theIJNodes[ u ];
+ nVec.resize( vsize, nullNode );
+ loadedNodes.insert( nVec[ 0 ] = node );
+ }
+ if ( theIJNodes.size() != hsize ) {
+ MESSAGE( "Wrong node positions on theBaseEdge" );
+ return false;
+ }
+
+ // load nodes from e1
+
+ map< double, const SMDS_MeshNode*> sortedNodes; // sort by param on edge
+ nIt = sm1->GetNodes();
+ while ( nIt->more() ) {
+ node = nIt->next();
+ if(myTool->IsMedium(node))
+ continue;
+ const SMDS_EdgePosition* pos =
+ dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
+ if ( !pos ) {
+ return false;
+ }
+ sortedNodes.insert( make_pair( pos->GetUParameter(), node ));
+ }
+ loadedNodes.insert( nVecf[ vsize - 1 ] = smVft->GetNodes()->next() );
+ map< double, const SMDS_MeshNode*>::iterator u_n = sortedNodes.begin();
+ int row = rev1 ? vsize - 1 : 0;
+ for ( ; u_n != sortedNodes.end(); u_n++ ) {
+ if ( rev1 ) row--;
+ else row++;
+ loadedNodes.insert( nVecf[ row ] = u_n->second );
+ }
+
+ // try to load the rest nodes
+
+ // get all faces from theFace
+ TIDSortedElemSet allFaces, foundFaces;
+ SMDS_ElemIteratorPtr eIt = smFace->GetElements();
+ while ( eIt->more() ) {
+ const SMDS_MeshElement* e = eIt->next();
+ if ( e->GetType() == SMDSAbs_Face )
+ allFaces.insert( e );
+ }
+ // Starting from 2 neighbour nodes on theBaseEdge, look for a face
+ // the nodes belong to, and between the nodes of the found face,
+ // look for a not loaded node considering this node to be the next
+ // in a column of the starting second node. Repeat, starting
+ // from nodes next to the previous starting nodes in their columns,
+ // and so on while a face can be found. Then go the the next pair
+ // of nodes on theBaseEdge.
+ StdMeshers_IJNodeMap::iterator par_nVec_1 = theIJNodes.begin();
+ StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
+ // loop on columns
+ int col = 0;
+ for ( par_nVec_2++; par_nVec_2 != theIJNodes.end(); par_nVec_1++, par_nVec_2++ ) {
+ col++;
+ row = 0;
+ const SMDS_MeshNode* n1 = par_nVec_1->second[ row ];
+ const SMDS_MeshNode* n2 = par_nVec_2->second[ row ];
+ const SMDS_MeshElement* face = 0;
+ do {
+ // look for a face by 2 nodes
+ face = SMESH_MeshEditor::FindFaceInSet( n1, n2, allFaces, foundFaces );
+ if ( face ) {
+ int nbFaceNodes = face->NbNodes();
+ if ( (!myCreateQuadratic && nbFaceNodes>4) ||
+ (myCreateQuadratic && nbFaceNodes>8) ) {
+ MESSAGE(" Too many nodes in a face: " << nbFaceNodes );
+ return false;
+ }
+ // look for a not loaded node of the <face>
+ bool found = false;
+ const SMDS_MeshNode* n3 = 0; // a node defferent from n1 and n2
+ eIt = face->nodesIterator() ;
+ while ( !found && eIt->more() ) {
+ node = static_cast<const SMDS_MeshNode*>( eIt->next() );
+ if(myTool->IsMedium(node))
+ continue;
+ found = loadedNodes.insert( node ).second;
+ if ( !found && node != n1 && node != n2 )
+ n3 = node;
+ }
+ if ( found ) {
+ if ( ++row > vsize - 1 ) {
+ MESSAGE( "Too many nodes in column "<< col <<": "<< row+1);
+ return false;
+ }
+ par_nVec_2->second[ row ] = node;
+ foundFaces.insert( face );
+ n2 = node;
+ if ( nbFaceNodes==4 || (myCreateQuadratic && nbFaceNodes==8) ) {
+ n1 = par_nVec_1->second[ row ];
+ }
+ }
+ else if ( (nbFaceNodes==3 || (myCreateQuadratic && nbFaceNodes==6) ) &&
+ n3 == par_nVec_1->second[ row ] ) {
+ n1 = n3;
+ }
+ else {
+ MESSAGE( "Not quad mesh, column "<< col );
+ return false;
+ }
+ }
+ }
+ while ( face && n1 && n2 );
+
+ if ( row < vsize - 1 ) {
+ MESSAGE( "Too few nodes in column "<< col <<": "<< row+1);
+ MESSAGE( "Base node 1: "<< par_nVec_1->second[0]);
+ MESSAGE( "Base node 2: "<< par_nVec_2->second[0]);
+ MESSAGE( "Current node 1: "<< n1);
+ MESSAGE( "Current node 2: "<< n2);
+ MESSAGE( "first base node: "<< theIJNodes.begin()->second[0]);
+ MESSAGE( "last base node: "<< theIJNodes.rbegin()->second[0]);
+ return false;
+ }
+ } // loop on columns
+
+ return true;
+}
+