Skip to content

Commit

Permalink
Expected<void> FastWindingNumber::calcFromVector
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedr committed Jan 2, 2025
1 parent b5b26bb commit 27b5238
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
6 changes: 3 additions & 3 deletions source/MRCuda/MRCudaBasic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ namespace Cuda
MRCUDA_API cudaError_t logError( cudaError_t code, const char * file = nullptr, int line = 0 );

/// executes given CUDA function and logs the error if any
#define CUDA_EXEC( func ) logError( func, __FILE__ , __LINE__ )
#define CUDA_EXEC( func ) MR::Cuda::logError( func, __FILE__ , __LINE__ )

/// executes given CUDA function, logs if it fails and returns error code
#define CUDA_EXEC_RETURN( func ) if ( auto code = CUDA_EXEC( func ); code != cudaError::cudaSuccess ) return code

/// if func evaluates not to cudaError::cudaSuccess, then returns MR::unexpected with the error string without logging
#define CUDA_RETURN_UNEXPECTED( func ) if ( auto code = func; code != cudaError::cudaSuccess ) return MR::unexpected( getError( code ) )
#define CUDA_RETURN_UNEXPECTED( func ) if ( auto code = ( func ); code != cudaError::cudaSuccess ) return MR::unexpected( MR::Cuda::getError( code ) )

/// executes given CUDA function, logs if it fails and returns MR::unexpected with the error string
#define CUDA_EXEC_RETURN_UNEXPECTED( func ) if ( auto code = CUDA_EXEC( func ); code != cudaError::cudaSuccess ) return MR::unexpected( getError( code ) )
#define CUDA_EXEC_RETURN_UNEXPECTED( func ) if ( auto code = CUDA_EXEC( func ); code != cudaError::cudaSuccess ) return MR::unexpected( MR::Cuda::getError( code ) )

template<typename T>
DynamicArray<T>::DynamicArray( size_t size )
Expand Down
28 changes: 18 additions & 10 deletions source/MRCuda/MRCudaFastWindingNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,28 @@ Expected<void> FastWindingNumber::prepareData_( ProgressCallback cb )
return {};
}

void FastWindingNumber::calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace )
Expected<void> FastWindingNumber::calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace, const ProgressCallback& cb )
{
MR_TIMER
prepareData_( {} ); //TODO: check error

const size_t size = points.size();
res.resize( size );
data_->cudaPoints.fromVector( points );
DynamicArrayF cudaResult( size );
return prepareData_( subprogress( cb, 0.0, 0.5f ) ).and_then( [&]() -> Expected<void>
{
const size_t size = points.size();
res.resize( size );
CUDA_RETURN_UNEXPECTED( data_->cudaPoints.fromVector( points ) );
if ( !reportProgress( cb, 0.6f ) )
return unexpectedOperationCanceled();

fastWindingNumberFromVector( data_->cudaPoints.data(), data_->dipoles.data(), data_->cudaNodes.data(), data_->cudaMeshPoints.data(), data_->cudaFaces.data(), cudaResult.data(), beta, int( skipFace ), size );
CUDA_EXEC( cudaGetLastError() );
DynamicArrayF cudaResult( size );
fastWindingNumberFromVector( data_->cudaPoints.data(), data_->dipoles.data(), data_->cudaNodes.data(), data_->cudaMeshPoints.data(), data_->cudaFaces.data(), cudaResult.data(), beta, int( skipFace ), size );
CUDA_EXEC_RETURN_UNEXPECTED( cudaGetLastError() );
if ( !reportProgress( cb, 0.7f ) )
return unexpectedOperationCanceled();

CUDA_EXEC( cudaResult.toVector( res ) );
CUDA_RETURN_UNEXPECTED( cudaResult.toVector( res ) );
if ( !reportProgress( cb, 1.0f ) )
return unexpectedOperationCanceled();
return {};
} );
}

bool FastWindingNumber::calcSelfIntersections( FaceBitSet& res, float beta, ProgressCallback cb )
Expand Down
2 changes: 1 addition & 1 deletion source/MRCuda/MRCudaFastWindingNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MRCUDA_CLASS FastWindingNumber : public IFastWindingNumber
MRCUDA_API FastWindingNumber( const Mesh& mesh );

// see methods' descriptions in IFastWindingNumber
MRCUDA_API void calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace = {} ) override;
MRCUDA_API Expected<void> calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace, const ProgressCallback& cb ) override;
MRCUDA_API bool calcSelfIntersections( FaceBitSet& res, float beta, ProgressCallback cb ) override;
MRCUDA_API Expected<void> calcFromGrid( std::vector<float>& res, const Vector3i& dims, const AffineXf3f& gridToMeshXf, float beta, ProgressCallback cb ) override;
MRCUDA_API Expected<void> calcFromGridWithDistances( std::vector<float>& res, const Vector3i& dims, const AffineXf3f& gridToMeshXf, const DistanceToMeshOptions& options, const ProgressCallback& cb ) override;
Expand Down
9 changes: 6 additions & 3 deletions source/MRMesh/MRFastWindingNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ inline float FastWindingNumber::calc_( const Vector3f & q, float beta, FaceId sk
return calcFastWindingNumber( dipoles_, tree_, mesh_, q, beta, skipFace );
}

void FastWindingNumber::calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace )
Expected<void> FastWindingNumber::calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace, const ProgressCallback& cb )
{
MR_TIMER
res.resize( points.size() );
ParallelFor( points, [&]( size_t i )
if ( !ParallelFor( points, [&]( size_t i )
{
res[i] = calc_( points[i], beta, skipFace );
} );
}, cb ) )
return unexpectedOperationCanceled();
return {};
}

bool FastWindingNumber::calcSelfIntersections( FaceBitSet& res, float beta, ProgressCallback cb )
Expand Down
4 changes: 2 additions & 2 deletions source/MRMesh/MRFastWindingNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IFastWindingNumber
/// <param name="points">incoming points</param>
/// <param name="beta">determines the precision of the approximation: the more the better, recommended value 2 or more</param>
/// <param name="skipFace">this triangle (if it is close to `q`) will be skipped from summation</param>
virtual void calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace = {} ) = 0;
virtual Expected<void> calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace = {}, const ProgressCallback& cb = {} ) = 0;

/// <summary>
/// calculates winding numbers for all centers of mesh's triangles. if winding number is less than 0 or greater then 1, that face is marked as self-intersected
Expand Down Expand Up @@ -60,7 +60,7 @@ class MRMESH_CLASS FastWindingNumber : public IFastWindingNumber
[[nodiscard]] MRMESH_API FastWindingNumber( const Mesh & mesh );

// see methods' descriptions in IFastWindingNumber
MRMESH_API void calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace = {} ) override;
MRMESH_API Expected<void> calcFromVector( std::vector<float>& res, const std::vector<Vector3f>& points, float beta, FaceId skipFace, const ProgressCallback& cb ) override;
MRMESH_API bool calcSelfIntersections( FaceBitSet& res, float beta, ProgressCallback cb ) override;
MRMESH_API Expected<void> calcFromGrid( std::vector<float>& res, const Vector3i& dims, const AffineXf3f& gridToMeshXf, float beta, ProgressCallback cb ) override;
MRMESH_API float calcWithDistances( const Vector3f& p, const DistanceToMeshOptions& options );
Expand Down

0 comments on commit 27b5238

Please sign in to comment.