+ map< double, gp_XY >::const_iterator i1 = myUVmap.upper_bound( U );
+
+ if ( i1 == myUVmap.end() )
+ return myUVmap.empty() ? gp_XY(0,0) : myUVmap.rbegin()->second;
+
+ if ( i1 == myUVmap.begin() )
+ return (*i1).second;
+
+ map< double, gp_XY >::const_iterator i2 = i1--;
+
+ double r = ( U - i1->first ) / ( i2->first - i1->first );
+ return i1->second * ( 1 - r ) + i2->second * r;
+}
+
+//================================================================================
+/*!
+ * \brief Projects internal nodes using transformation found by boundary nodes
+ */
+//================================================================================
+
+bool StdMeshers_Sweeper::projectIntPoints(const vector< gp_XYZ >& fromBndPoints,
+ const vector< gp_XYZ >& toBndPoints,
+ const vector< gp_XYZ >& fromIntPoints,
+ vector< gp_XYZ >& toIntPoints,
+ NSProjUtils::TrsfFinder3D& trsf,
+ vector< gp_XYZ > * bndError)
+{
+ // find transformation
+ if ( trsf.IsIdentity() && !trsf.Solve( fromBndPoints, toBndPoints ))
+ return false;
+
+ // compute internal points using the found trsf
+ for ( size_t iP = 0; iP < fromIntPoints.size(); ++iP )
+ {
+ toIntPoints[ iP ] = trsf.Transform( fromIntPoints[ iP ]);
+ }
+
+ // compute boundary error
+ if ( bndError )
+ {
+ bndError->resize( fromBndPoints.size() );
+ gp_XYZ fromTrsf;
+ for ( size_t iP = 0; iP < fromBndPoints.size(); ++iP )
+ {
+ fromTrsf = trsf.Transform( fromBndPoints[ iP ] );
+ (*bndError)[ iP ] = toBndPoints[ iP ] - fromTrsf;
+ }
+ }
+ return true;
+}
+
+//================================================================================
+/*!
+ * \brief Add boundary error to ineternal points
+ */
+//================================================================================
+
+void StdMeshers_Sweeper::applyBoundaryError(const vector< gp_XYZ >& bndPoints,
+ const vector< gp_XYZ >& bndError1,
+ const vector< gp_XYZ >& bndError2,
+ const double r,
+ vector< gp_XYZ >& intPoints,
+ vector< double >& int2BndDist)
+{
+ // fix each internal point
+ const double eps = 1e-100;
+ for ( size_t iP = 0; iP < intPoints.size(); ++iP )
+ {
+ gp_XYZ & intPnt = intPoints[ iP ];
+
+ // compute distance from intPnt to each boundary node
+ double int2BndDistSum = 0;
+ for ( size_t iBnd = 0; iBnd < bndPoints.size(); ++iBnd )
+ {
+ int2BndDist[ iBnd ] = 1 / (( intPnt - bndPoints[ iBnd ]).SquareModulus() + eps );
+ int2BndDistSum += int2BndDist[ iBnd ];
+ }
+
+ // apply bndError
+ for ( size_t iBnd = 0; iBnd < bndPoints.size(); ++iBnd )
+ {
+ intPnt += bndError1[ iBnd ] * ( 1 - r ) * int2BndDist[ iBnd ] / int2BndDistSum;
+ intPnt += bndError2[ iBnd ] * r * int2BndDist[ iBnd ] / int2BndDistSum;
+ }
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Creates internal nodes of the prism
+ */
+//================================================================================
+
+bool StdMeshers_Sweeper::ComputeNodes( SMESH_MesherHelper& helper,
+ const double tol,
+ const bool allowHighBndError)
+{
+ const size_t zSize = myBndColumns[0]->size();
+ const size_t zSrc = 0, zTgt = zSize-1;
+ if ( zSize < 3 ) return true;
+
+ vector< vector< gp_XYZ > > intPntsOfLayer( zSize ); // node coodinates to compute
+ // set coordinates of src and tgt nodes
+ for ( size_t z = 0; z < intPntsOfLayer.size(); ++z )
+ intPntsOfLayer[ z ].resize( myIntColumns.size() );
+ for ( size_t iP = 0; iP < myIntColumns.size(); ++iP )
+ {
+ intPntsOfLayer[ zSrc ][ iP ] = intPoint( iP, zSrc );
+ intPntsOfLayer[ zTgt ][ iP ] = intPoint( iP, zTgt );
+ }
+
+ // compute coordinates of internal nodes by projecting (transfroming) src and tgt
+ // nodes towards the central layer
+
+ vector< NSProjUtils::TrsfFinder3D > trsfOfLayer( zSize );
+ vector< vector< gp_XYZ > > bndError( zSize );
+
+ // boundary points used to compute an affine transformation from a layer to a next one
+ vector< gp_XYZ > fromSrcBndPnts( myBndColumns.size() ), fromTgtBndPnts( myBndColumns.size() );
+ vector< gp_XYZ > toSrcBndPnts ( myBndColumns.size() ), toTgtBndPnts ( myBndColumns.size() );
+ for ( size_t iP = 0; iP < myBndColumns.size(); ++iP )
+ {
+ fromSrcBndPnts[ iP ] = bndPoint( iP, zSrc );
+ fromTgtBndPnts[ iP ] = bndPoint( iP, zTgt );
+ }
+
+ size_t zS = zSrc + 1;
+ size_t zT = zTgt - 1;
+ for ( ; zS < zT; ++zS, --zT ) // vertical loop on layers
+ {
+ for ( size_t iP = 0; iP < myBndColumns.size(); ++iP )
+ {
+ toSrcBndPnts[ iP ] = bndPoint( iP, zS );
+ toTgtBndPnts[ iP ] = bndPoint( iP, zT );
+ }
+ if (! projectIntPoints( fromSrcBndPnts, toSrcBndPnts,
+ intPntsOfLayer[ zS-1 ], intPntsOfLayer[ zS ],
+ trsfOfLayer [ zS-1 ], & bndError[ zS-1 ]))
+ return false;
+ if (! projectIntPoints( fromTgtBndPnts, toTgtBndPnts,
+ intPntsOfLayer[ zT+1 ], intPntsOfLayer[ zT ],
+ trsfOfLayer [ zT+1 ], & bndError[ zT+1 ]))
+ return false;
+
+ // if ( zT == zTgt - 1 )
+ // {
+ // for ( size_t iP = 0; iP < myBndColumns.size(); ++iP )
+ // {
+ // gp_XYZ fromTrsf = trsfOfLayer [ zT+1].Transform( fromTgtBndPnts[ iP ] );
+ // cout << "mesh.AddNode( "
+ // << fromTrsf.X() << ", "
+ // << fromTrsf.Y() << ", "
+ // << fromTrsf.Z() << ") " << endl;
+ // }
+ // for ( size_t iP = 0; iP < myIntColumns.size(); ++iP )
+ // cout << "mesh.AddNode( "
+ // << intPntsOfLayer[ zT ][ iP ].X() << ", "
+ // << intPntsOfLayer[ zT ][ iP ].Y() << ", "
+ // << intPntsOfLayer[ zT ][ iP ].Z() << ") " << endl;
+ // }
+
+ fromTgtBndPnts.swap( toTgtBndPnts );
+ fromSrcBndPnts.swap( toSrcBndPnts );
+ }
+
+ // Compute two projections of internal points to the central layer
+ // in order to evaluate an error of internal points
+
+ bool centerIntErrorIsSmall;
+ vector< gp_XYZ > centerSrcIntPnts( myIntColumns.size() );
+ vector< gp_XYZ > centerTgtIntPnts( myIntColumns.size() );
+
+ for ( size_t iP = 0; iP < myBndColumns.size(); ++iP )
+ {
+ toSrcBndPnts[ iP ] = bndPoint( iP, zS );
+ toTgtBndPnts[ iP ] = bndPoint( iP, zT );
+ }
+ if (! projectIntPoints( fromSrcBndPnts, toSrcBndPnts,
+ intPntsOfLayer[ zS-1 ], centerSrcIntPnts,
+ trsfOfLayer [ zS-1 ], & bndError[ zS-1 ]))
+ return false;
+ if (! projectIntPoints( fromTgtBndPnts, toTgtBndPnts,
+ intPntsOfLayer[ zT+1 ], centerTgtIntPnts,
+ trsfOfLayer [ zT+1 ], & bndError[ zT+1 ]))
+ return false;
+
+ // evaluate an error of internal points on the central layer
+ centerIntErrorIsSmall = true;
+ if ( zS == zT ) // odd zSize
+ {
+ for ( size_t iP = 0; ( iP < myIntColumns.size() && centerIntErrorIsSmall ); ++iP )
+ centerIntErrorIsSmall =
+ (centerSrcIntPnts[ iP ] - centerTgtIntPnts[ iP ]).SquareModulus() < tol*tol;
+ }
+ else // even zSize
+ {
+ for ( size_t iP = 0; ( iP < myIntColumns.size() && centerIntErrorIsSmall ); ++iP )
+ centerIntErrorIsSmall =
+ (intPntsOfLayer[ zS-1 ][ iP ] - centerTgtIntPnts[ iP ]).SquareModulus() < tol*tol;
+ }
+
+ // Evaluate an error of boundary points
+
+ bool bndErrorIsSmall = true;
+ for ( size_t iP = 0; ( iP < myBndColumns.size() && bndErrorIsSmall ); ++iP )
+ {
+ double sumError = 0;
+ for ( size_t z = 1; z < zS; ++z ) // loop on layers
+ sumError += ( bndError[ z-1 ][ iP ].Modulus() +
+ bndError[ zSize-z ][ iP ].Modulus() );
+
+ bndErrorIsSmall = ( sumError < tol );
+ }
+
+ if ( !bndErrorIsSmall && !allowHighBndError )
+ return false;
+
+ // compute final points on the central layer
+ std::vector< double > int2BndDist( myBndColumns.size() ); // work array of applyBoundaryError()
+ double r = zS / ( zSize - 1.);
+ if ( zS == zT )
+ {
+ for ( size_t iP = 0; iP < myIntColumns.size(); ++iP )
+ {
+ intPntsOfLayer[ zS ][ iP ] =
+ ( 1 - r ) * centerSrcIntPnts[ iP ] + r * centerTgtIntPnts[ iP ];
+ }
+ if ( !bndErrorIsSmall )
+ {
+ applyBoundaryError( toSrcBndPnts, bndError[ zS-1 ], bndError[ zS+1 ], r,
+ intPntsOfLayer[ zS ], int2BndDist );
+ }
+ }
+ else
+ {
+ for ( size_t iP = 0; iP < myIntColumns.size(); ++iP )
+ {
+ intPntsOfLayer[ zS ][ iP ] =
+ r * intPntsOfLayer[ zS ][ iP ] + ( 1 - r ) * centerSrcIntPnts[ iP ];
+ intPntsOfLayer[ zT ][ iP ] =
+ r * intPntsOfLayer[ zT ][ iP ] + ( 1 - r ) * centerTgtIntPnts[ iP ];
+ }
+ if ( !bndErrorIsSmall )
+ {
+ applyBoundaryError( toSrcBndPnts, bndError[ zS-1 ], bndError[ zS+1 ], r,
+ intPntsOfLayer[ zS ], int2BndDist );
+ applyBoundaryError( toTgtBndPnts, bndError[ zT+1 ], bndError[ zT-1 ], r,
+ intPntsOfLayer[ zT ], int2BndDist );
+ }
+ }
+
+ //centerIntErrorIsSmall = true;
+ //bndErrorIsSmall = true;
+ if ( !centerIntErrorIsSmall )
+ {
+ // Compensate the central error; continue adding projection
+ // by going from central layer to the source and target ones
+
+ vector< gp_XYZ >& fromSrcIntPnts = centerSrcIntPnts;
+ vector< gp_XYZ >& fromTgtIntPnts = centerTgtIntPnts;
+ vector< gp_XYZ > toSrcIntPnts( myIntColumns.size() );
+ vector< gp_XYZ > toTgtIntPnts( myIntColumns.size() );
+ vector< gp_XYZ > srcBndError( myBndColumns.size() );
+ vector< gp_XYZ > tgtBndError( myBndColumns.size() );
+
+ fromTgtBndPnts.swap( toTgtBndPnts );
+ fromSrcBndPnts.swap( toSrcBndPnts );
+
+ for ( ++zS, --zT; zS < zTgt; ++zS, --zT ) // vertical loop on layers
+ {
+ // invert transformation
+ if ( !trsfOfLayer[ zS+1 ].Invert() )
+ trsfOfLayer[ zS+1 ] = NSProjUtils::TrsfFinder3D(); // to recompute
+ if ( !trsfOfLayer[ zT-1 ].Invert() )
+ trsfOfLayer[ zT-1 ] = NSProjUtils::TrsfFinder3D();
+
+ // project internal nodes and compute bnd error
+ for ( size_t iP = 0; iP < myBndColumns.size(); ++iP )
+ {
+ toSrcBndPnts[ iP ] = bndPoint( iP, zS );
+ toTgtBndPnts[ iP ] = bndPoint( iP, zT );
+ }
+ projectIntPoints( fromSrcBndPnts, toSrcBndPnts,
+ fromSrcIntPnts, toSrcIntPnts,
+ trsfOfLayer[ zS+1 ], & srcBndError );
+ projectIntPoints( fromTgtBndPnts, toTgtBndPnts,
+ fromTgtIntPnts, toTgtIntPnts,
+ trsfOfLayer[ zT-1 ], & tgtBndError );
+
+ // if ( zS == zTgt - 1 )
+ // {
+ // cout << "mesh2 = smesh.Mesh()" << endl;
+ // for ( size_t iP = 0; iP < myBndColumns.size(); ++iP )
+ // {
+ // gp_XYZ fromTrsf = trsfOfLayer [ zS+1].Transform( fromSrcBndPnts[ iP ] );
+ // cout << "mesh2.AddNode( "
+ // << fromTrsf.X() << ", "
+ // << fromTrsf.Y() << ", "
+ // << fromTrsf.Z() << ") " << endl;
+ // }
+ // for ( size_t iP = 0; iP < myIntColumns.size(); ++iP )
+ // cout << "mesh2.AddNode( "
+ // << toSrcIntPnts[ iP ].X() << ", "
+ // << toSrcIntPnts[ iP ].Y() << ", "
+ // << toSrcIntPnts[ iP ].Z() << ") " << endl;
+ // }
+
+ // sum up 2 projections
+ r = zS / ( zSize - 1.);
+ vector< gp_XYZ >& zSIntPnts = intPntsOfLayer[ zS ];
+ vector< gp_XYZ >& zTIntPnts = intPntsOfLayer[ zT ];
+ for ( size_t iP = 0; iP < myIntColumns.size(); ++iP )
+ {
+ zSIntPnts[ iP ] = r * zSIntPnts[ iP ] + ( 1 - r ) * toSrcIntPnts[ iP ];
+ zTIntPnts[ iP ] = r * zTIntPnts[ iP ] + ( 1 - r ) * toTgtIntPnts[ iP ];
+ }
+
+ // compensate bnd error
+ if ( !bndErrorIsSmall )
+ {
+ applyBoundaryError( toSrcBndPnts, srcBndError, bndError[ zS+1 ], r,
+ intPntsOfLayer[ zS ], int2BndDist );
+ applyBoundaryError( toTgtBndPnts, tgtBndError, bndError[ zT-1 ], r,
+ intPntsOfLayer[ zT ], int2BndDist );
+ }
+
+ fromSrcBndPnts.swap( toSrcBndPnts );
+ fromSrcIntPnts.swap( toSrcIntPnts );
+ fromTgtBndPnts.swap( toTgtBndPnts );
+ fromTgtIntPnts.swap( toTgtIntPnts );
+ }
+ } // if ( !centerIntErrorIsSmall )
+
+ else if ( !bndErrorIsSmall )
+ {
+ zS = zSrc + 1;
+ zT = zTgt - 1;
+ for ( ; zS < zT; ++zS, --zT ) // vertical loop on layers
+ {
+ for ( size_t iP = 0; iP < myBndColumns.size(); ++iP )
+ {
+ toSrcBndPnts[ iP ] = bndPoint( iP, zS );
+ toTgtBndPnts[ iP ] = bndPoint( iP, zT );
+ }
+ // compensate bnd error
+ applyBoundaryError( toSrcBndPnts, bndError[ zS-1 ], bndError[ zS-1 ], 0.5,
+ intPntsOfLayer[ zS ], int2BndDist );
+ applyBoundaryError( toTgtBndPnts, bndError[ zT+1 ], bndError[ zT+1 ], 0.5,
+ intPntsOfLayer[ zT ], int2BndDist );
+ }
+ }
+
+ // cout << "centerIntErrorIsSmall = " << centerIntErrorIsSmall<< endl;
+ // cout << "bndErrorIsSmall = " << bndErrorIsSmall<< endl;
+
+ // Create nodes
+ for ( size_t iP = 0; iP < myIntColumns.size(); ++iP )
+ {
+ vector< const SMDS_MeshNode* > & nodeCol = *myIntColumns[ iP ];
+ for ( size_t z = zSrc + 1; z < zTgt; ++z ) // vertical loop on layers
+ {
+ const gp_XYZ & xyz = intPntsOfLayer[ z ][ iP ];
+ if ( !( nodeCol[ z ] = helper.AddNode( xyz.X(), xyz.Y(), xyz.Z() )))
+ return false;
+ }
+ }
+
+ return true;