+
+//================================================================================
+/*!
+ * \brief Check if all nodes of each layers have same logical Z
+ */
+//================================================================================
+
+bool StdMeshers_Sweeper::CheckSameZ()
+{
+ myZColumns.resize( myBndColumns.size() );
+ fillZColumn( myZColumns[0], *myBndColumns[0] );
+
+ bool sameZ = true;
+ const double tol = 0.1 * 1./ myBndColumns[0]->size();
+
+ // check columns based on VERTEXes
+
+ vector< int > vertexIndex;
+ vertexIndex.push_back( 0 );
+ for ( size_t iC = 1; iC < myBndColumns.size() && sameZ; ++iC )
+ {
+ if ( myBndColumns[iC]->front()->GetPosition()->GetDim() > 0 )
+ continue; // not on VERTEX
+
+ vertexIndex.push_back( iC );
+ fillZColumn( myZColumns[iC], *myBndColumns[iC] );
+
+ for ( size_t iZ = 0; iZ < myZColumns[0].size() && sameZ; ++iZ )
+ sameZ = ( Abs( myZColumns[0][iZ] - myZColumns[iC][iZ]) < tol );
+ }
+
+ // check columns based on EDGEs, one per EDGE
+
+ for ( size_t i = 1; i < vertexIndex.size() && sameZ; ++i )
+ {
+ if ( vertexIndex[i] - vertexIndex[i-1] < 2 )
+ continue;
+
+ int iC = ( vertexIndex[i] + vertexIndex[i-1] ) / 2;
+ fillZColumn( myZColumns[iC], *myBndColumns[iC] );
+
+ for ( size_t iZ = 0; iZ < myZColumns[0].size() && sameZ; ++iZ )
+ sameZ = ( Abs( myZColumns[0][iZ] - myZColumns[iC][iZ]) < tol );
+ }
+
+ if ( sameZ )
+ {
+ myZColumns.resize(1);
+ }
+ else
+ {
+ for ( size_t iC = 1; iC < myBndColumns.size(); ++iC )
+ fillZColumn( myZColumns[iC], *myBndColumns[iC] );
+ }
+
+ return sameZ;
+}
+
+//================================================================================
+/*!
+ * \brief Create internal nodes of the prism all located on straight lines with
+ * the same distribution along the lines.
+ */
+//================================================================================
+
+bool StdMeshers_Sweeper::ComputeNodesOnStraightSameZ()
+{
+ TZColumn& z = myZColumns[0];
+
+ for ( size_t i = 0; i < myIntColumns.size(); ++i )
+ {
+ TNodeColumn& nodes = *myIntColumns[i];
+ SMESH_NodeXYZ n0( nodes[0] ), n1( nodes.back() );
+
+ for ( size_t iZ = 0; iZ < z.size(); ++iZ )
+ {
+ gp_XYZ p = n0 * ( 1 - z[iZ] ) + n1 * z[iZ];
+ nodes[ iZ+1 ] = myHelper->AddNode( p.X(), p.Y(), p.Z() );
+ }
+ }
+
+ return true;
+}
+
+//================================================================================
+/*!
+ * \brief Create internal nodes of the prism all located on straight lines with
+ * different distributions along the lines.
+ */
+//================================================================================
+
+bool StdMeshers_Sweeper::ComputeNodesOnStraight()
+{
+ prepareTopBotDelaunay();
+
+ const SMDS_MeshNode *botNode, *topNode;
+ const BRepMesh_Triangle *topTria;
+ double botBC[3], topBC[3]; // barycentric coordinates
+ int botTriaNodes[3], topTriaNodes[3];
+ bool checkUV = true;
+
+ int nbInternalNodes = myIntColumns.size();
+ myBotDelaunay->InitTraversal( nbInternalNodes );
+
+ while (( botNode = myBotDelaunay->NextNode( botBC, botTriaNodes )))
+ {
+ TNodeColumn* column = myIntColumns[ myNodeID2ColID( botNode->GetID() )];
+
+ // find a Delaunay triangle containing the topNode
+ topNode = column->back();
+ gp_XY topUV = myHelper->GetNodeUV( myTopFace, topNode, NULL, &checkUV );
+ // get a starting triangle basing on that top and bot boundary nodes have same index
+ topTria = myTopDelaunay->GetTriangleNear( botTriaNodes[0] );
+ topTria = myTopDelaunay->FindTriangle( topUV, topTria, topBC, topTriaNodes );
+ if ( !topTria )
+ return false;
+
+ // create nodes along a line
+ SMESH_NodeXYZ botP( botNode ), topP( topNode );
+ for ( size_t iZ = 0; iZ < myZColumns[0].size(); ++iZ )
+ {
+ // use barycentric coordinates as weight of Z of boundary columns
+ double botZ = 0, topZ = 0;
+ for ( int i = 0; i < 3; ++i )
+ {
+ botZ += botBC[i] * myZColumns[ botTriaNodes[i] ][ iZ ];
+ topZ += topBC[i] * myZColumns[ topTriaNodes[i] ][ iZ ];
+ }
+ double rZ = double( iZ + 1 ) / ( myZColumns[0].size() + 1 );
+ double z = botZ * ( 1 - rZ ) + topZ * rZ;
+ gp_XYZ p = botP * ( 1 - z ) + topP * z;
+ (*column)[ iZ+1 ] = myHelper->AddNode( p.X(), p.Y(), p.Z() );
+ }
+ }
+
+ return myBotDelaunay->NbVisitedNodes() == nbInternalNodes;
+}
+
+//================================================================================
+/*!
+ * \brief Compute Z of nodes of a straight column
+ */
+//================================================================================
+
+void StdMeshers_Sweeper::fillZColumn( TZColumn& zColumn,
+ TNodeColumn& nodes )
+{
+ if ( zColumn.size() == nodes.size() - 2 )
+ return;
+
+ gp_Pnt p0 = SMESH_NodeXYZ( nodes[0] );
+ gp_Vec line( p0, SMESH_NodeXYZ( nodes.back() ));
+ double len2 = line.SquareMagnitude();
+
+ zColumn.resize( nodes.size() - 2 );
+ for ( size_t i = 0; i < zColumn.size(); ++i )
+ {
+ gp_Vec vec( p0, SMESH_NodeXYZ( nodes[ i+1] ));
+ zColumn[i] = ( line * vec ) / len2; // param [0,1] on the line
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Initialize *Delaunay members
+ */
+//================================================================================
+
+void StdMeshers_Sweeper::prepareTopBotDelaunay()
+{
+ UVPtStructVec botUV( myBndColumns.size() );
+ UVPtStructVec topUV( myBndColumns.size() );
+ for ( size_t i = 0; i < myBndColumns.size(); ++i )
+ {
+ TNodeColumn& nodes = *myBndColumns[i];
+ botUV[i].node = nodes[0];
+ botUV[i].SetUV( myHelper->GetNodeUV( myBotFace, nodes[0] ));
+ topUV[i].node = nodes.back();
+ topUV[i].SetUV( myHelper->GetNodeUV( myTopFace, nodes.back() ));
+ botUV[i].node->setIsMarked( true );
+ }
+ TopoDS_Edge dummyE;
+ SMESH_Mesh* mesh = myHelper->GetMesh();
+ TSideVector botWires( 1, StdMeshers_FaceSide::New( botUV, myBotFace, dummyE, mesh ));
+ TSideVector topWires( 1, StdMeshers_FaceSide::New( topUV, myTopFace, dummyE, mesh ));
+
+ // Delaunay mesh on the FACEs.
+ bool checkUV = false;
+ myBotDelaunay.reset( new NSProjUtils::Delaunay( botWires, checkUV ));
+ myTopDelaunay.reset( new NSProjUtils::Delaunay( topWires, checkUV ));
+
+ if ( myHelper->GetIsQuadratic() )
+ {
+ // mark all medium nodes of faces on botFace to avoid their treating
+ SMESHDS_SubMesh* smDS = myHelper->GetMeshDS()->MeshElements( myBotFace );
+ SMDS_ElemIteratorPtr eIt = smDS->GetElements();
+ while ( eIt->more() )
+ {
+ const SMDS_MeshElement* e = eIt->next();
+ for ( int i = e->NbCornerNodes(), nb = e->NbNodes(); i < nb; ++i )
+ e->GetNode( i )->setIsMarked( true );
+ }
+ }
+
+ // map to get a node column by a bottom node
+ myNodeID2ColID.Clear(/*doReleaseMemory=*/false);
+ myNodeID2ColID.ReSize( myIntColumns.size() );
+
+ // un-mark nodes to treat (internal bottom nodes) to be returned by myBotDelaunay
+ for ( size_t i = 0; i < myIntColumns.size(); ++i )
+ {
+ const SMDS_MeshNode* botNode = myIntColumns[i]->front();
+ botNode->setIsMarked( false );
+ myNodeID2ColID.Bind( botNode->GetID(), i );
+ }
+}
+
+//================================================================================
+/*!
+ * \brief For each internal node column, find Delaunay triangles including it
+ * and Barycentric Coordinates within the triangles. Fill in myTopBotTriangles
+ */
+//================================================================================
+
+bool StdMeshers_Sweeper::findDelaunayTriangles()
+{
+ const SMDS_MeshNode *botNode, *topNode;
+ const BRepMesh_Triangle *topTria;
+ TopBotTriangles tbTrias;
+ bool checkUV = true;
+
+ int nbInternalNodes = myIntColumns.size();
+ myTopBotTriangles.resize( nbInternalNodes );
+
+ myBotDelaunay->InitTraversal( nbInternalNodes );
+
+ while (( botNode = myBotDelaunay->NextNode( tbTrias.myBotBC, tbTrias.myBotTriaNodes )))
+ {
+ int colID = myNodeID2ColID( botNode->GetID() );
+ TNodeColumn* column = myIntColumns[ colID ];
+
+ // find a Delaunay triangle containing the topNode
+ topNode = column->back();
+ gp_XY topUV = myHelper->GetNodeUV( myTopFace, topNode, NULL, &checkUV );
+ // get a starting triangle basing on that top and bot boundary nodes have same index
+ topTria = myTopDelaunay->GetTriangleNear( tbTrias.myBotTriaNodes[0] );
+ topTria = myTopDelaunay->FindTriangle( topUV, topTria,
+ tbTrias.myTopBC, tbTrias.myTopTriaNodes );
+ if ( !topTria )
+ tbTrias.SetTopByBottom();
+
+ myTopBotTriangles[ colID ] = tbTrias;
+ }
+
+ if ( myBotDelaunay->NbVisitedNodes() < nbInternalNodes )
+ {
+ myTopBotTriangles.clear();
+ return false;
+ }
+
+ myBotDelaunay.reset();
+ myTopDelaunay.reset();
+ myNodeID2ColID.Clear();
+
+ return true;
+}
+
+//================================================================================
+/*!
+ * \brief Initialize fields
+ */
+//================================================================================
+
+StdMeshers_Sweeper::TopBotTriangles::TopBotTriangles()
+{
+ myBotBC[0] = myBotBC[1] = myBotBC[2] = myTopBC[0] = myTopBC[1] = myTopBC[2] = 0.;
+ myBotTriaNodes[0] = myBotTriaNodes[1] = myBotTriaNodes[2] = 0;
+ myTopTriaNodes[0] = myTopTriaNodes[1] = myTopTriaNodes[2] = 0;
+}
+
+//================================================================================
+/*!
+ * \brief Set top data equal to bottom data
+ */
+//================================================================================
+
+void StdMeshers_Sweeper::TopBotTriangles::SetTopByBottom()
+{
+ for ( int i = 0; i < 3; ++i )
+ {
+ myTopBC[i] = myBotBC[i];
+ myTopTriaNodes[i] = myBotTriaNodes[0];
+ }
+}