+ /*!
+ * \brief Find existing triangulation of a polygon
+ */
+ int findExistingTriangulation( const SMDS_MeshElement* polygon,
+ //const SMDS_Mesh* mesh,
+ std::vector< const SMDS_MeshNode* >& nodes )
+ {
+ int nbSplits = 0;
+ nodes.clear();
+ std::vector<const SMDS_MeshNode *> twoNodes(2);
+ std::vector<const SMDS_MeshElement *> foundFaces; foundFaces.reserve(10);
+ std::set< const SMDS_MeshElement * > avoidFaces; avoidFaces.insert( polygon );
+
+ const int nbPolyNodes = polygon->NbCornerNodes();
+ twoNodes[1] = polygon->GetNode( nbPolyNodes - 1 );
+ for ( int iN = 0; iN < nbPolyNodes; ++iN ) // loop on border links of polygon
+ {
+ twoNodes[0] = polygon->GetNode( iN );
+
+ int nbFaces = SMDS_Mesh::GetElementsByNodes( twoNodes, foundFaces, SMDSAbs_Face );
+ int nbOkFaces = 0;
+ for ( int iF = 0; iF < nbFaces; ++iF ) // keep faces lying over polygon
+ {
+ if ( avoidFaces.count( foundFaces[ iF ]))
+ continue;
+ int i, nbFaceNodes = foundFaces[ iF ]->NbCornerNodes();
+ for ( i = 0; i < nbFaceNodes; ++i )
+ {
+ const SMDS_MeshNode* n = foundFaces[ iF ]->GetNode( i );
+ bool isCommonNode = ( n == twoNodes[0] ||
+ n == twoNodes[1] ||
+ polygon->GetNodeIndex( n ) >= 0 );
+ if ( !isCommonNode )
+ break;
+ }
+ if ( i == nbFaceNodes ) // all nodes of foundFaces[iF] are shared with polygon
+ if ( nbOkFaces++ != iF )
+ foundFaces[ nbOkFaces-1 ] = foundFaces[ iF ];
+ }
+ if ( nbOkFaces > 0 )
+ {
+ int iFaceSelected = 0;
+ if ( nbOkFaces > 1 ) // select a face with minimal distance from polygon
+ {
+ double minDist = Precision::Infinite();
+ for ( int iF = 0; iF < nbOkFaces; ++iF )
+ {
+ int i, nbFaceNodes = foundFaces[ iF ]->NbCornerNodes();
+ gp_XYZ gc = SMESH_NodeXYZ( foundFaces[ iF ]->GetNode( 0 ));
+ for ( i = 1; i < nbFaceNodes; ++i )
+ gc += SMESH_NodeXYZ( foundFaces[ iF ]->GetNode( i ));
+ gc /= nbFaceNodes;
+
+ double dist = SMESH_MeshAlgos::GetDistance( polygon, gc );
+ if ( dist < minDist )
+ {
+ minDist = dist;
+ iFaceSelected = iF;
+ }
+ }
+ }
+ if ( foundFaces[ iFaceSelected ]->NbCornerNodes() != 3 )
+ return 0;
+ nodes.insert( nodes.end(),
+ foundFaces[ iFaceSelected ]->begin_nodes(),
+ foundFaces[ iFaceSelected ]->end_nodes());
+ if ( !SMESH_MeshAlgos::IsRightOrder( foundFaces[ iFaceSelected ],
+ twoNodes[0], twoNodes[1] ))
+ {
+ // reverse just added nodes
+ std::reverse( nodes.end() - 3, nodes.end() );
+ }
+ avoidFaces.insert( foundFaces[ iFaceSelected ]);
+ nbSplits++;
+ }
+
+ twoNodes[1] = twoNodes[0];
+
+ } // loop on polygon nodes
+
+ return nbSplits;
+ }
+ //================================================================================