Skip to content

Commit

Permalink
IFastWindingNumber::calcFromGridWithDistances returns NaN outside min…
Browse files Browse the repository at this point in the history
…-max range (#3917)
  • Loading branch information
Fedr authored Dec 24, 2024
1 parent 9339fed commit 6e4b7b2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
17 changes: 13 additions & 4 deletions source/MRCuda/MRCudaFastWindingNumber.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "MRMesh/MRAABBTree.h"
#include "MRMesh/MRConstants.h"
#include "device_launch_parameters.h"
#include <limits>

namespace MR
{
Expand Down Expand Up @@ -59,7 +60,7 @@ __device__ void processPoint( const float3& q, float& res, const Dipole* dipoles
res *= INV_4PI;
}

__device__ float calcDistance( const float3& pt,
__device__ float calcDistanceSq( const float3& pt,
const Node3* __restrict__ nodes, const float3* __restrict__ meshPoints, const FaceToThreeVerts* __restrict__ faces,
float maxDistSq, float minDistSq )
{
Expand Down Expand Up @@ -130,7 +131,7 @@ __device__ float calcDistance( const float3& pt,
addSubTask( s1 ); // larger distance to look later
addSubTask( s2 ); // smaller distance to look first
}
return sqrt( resSq );
return resSq;
}

__global__ void fastWindingNumberFromVectorKernel( const float3* points,
Expand Down Expand Up @@ -195,6 +196,8 @@ __global__ void fastWindingNumberFromGridKernel( int3 dims, Matrix4 gridToMeshXf
processPoint( transformedPoint, resVec[index], dipoles, nodes, meshPoints, faces, beta, index );
}

static constexpr float cQuietNan = std::numeric_limits<float>::quiet_NaN();

__global__ void signedDistanceKernel( int3 dims, Matrix4 gridToMeshXf,
const Dipole* __restrict__ dipoles, const Node3* __restrict__ nodes, const float3* __restrict__ meshPoints, const FaceToThreeVerts* __restrict__ faces,
float* resVec, float windingNumberThreshold, float beta, float maxDistSq, float minDistSq, size_t size )
Expand All @@ -215,13 +218,19 @@ __global__ void signedDistanceKernel( int3 dims, Matrix4 gridToMeshXf,
const float3 point{ float( voxel.x ), float( voxel.y ), float( voxel.z ) };
const float3 transformedPoint = gridToMeshXf.isIdentity ? point : gridToMeshXf.transform( point );

float& res = resVec[index];
res = calcDistance( transformedPoint, nodes, meshPoints, faces, maxDistSq, minDistSq );
float resSq = calcDistanceSq( transformedPoint, nodes, meshPoints, faces, maxDistSq, minDistSq );
if ( resSq < minDistSq || resSq >= maxDistSq ) // note that resSq == minDistSq (e.g. == 0) is a valid situation
{
resVec[index] = cQuietNan;
return;
}

float fwn{ 0 };
processPoint( transformedPoint, fwn, dipoles, nodes, meshPoints, faces, beta, index );
float res = sqrt( resSq );
if ( fwn > windingNumberThreshold )
res = -res;
resVec[index] = res;
}

void fastWindingNumberFromVector( const float3* points, const Dipole* dipoles,
Expand Down
6 changes: 5 additions & 1 deletion source/MRMesh/MRFastWindingNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "MRMeshProject.h"
#include "MRAABBTree.h"
#include "MRDipole.h"
#include "MRIsNaN.h"

namespace MR
{
Expand Down Expand Up @@ -60,8 +61,11 @@ Expected<void> FastWindingNumber::calcFromGrid( std::vector<float>& res, const V

float FastWindingNumber::calcWithDistances( const Vector3f& p, float windingNumberThreshold, float beta, float maxDistSq, float minDistSq )
{
auto resSq = findProjection( p, mesh_, maxDistSq, nullptr, minDistSq ).distSq;
if ( resSq < minDistSq || resSq >= maxDistSq ) // note that resSq == minDistSq (e.g. == 0) is a valid situation
return cQuietNan;
const auto sign = calc_( p, beta ) > windingNumberThreshold ? -1.f : +1.f;
return sign * std::sqrt( findProjection( p, mesh_, maxDistSq, nullptr, minDistSq ).distSq );
return sign * std::sqrt( resSq );
}

Expected<void> FastWindingNumber::calcFromGridWithDistances( std::vector<float>& res, const Vector3i& dims, const AffineXf3f& gridToMeshXf, float windingNumberThreshold, float beta, float maxDistSq, float minDistSq, ProgressCallback cb )
Expand Down
3 changes: 2 additions & 1 deletion source/MRMesh/MRFastWindingNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class IFastWindingNumber
virtual Expected<void> calcFromGrid( std::vector<float>& res, const Vector3i& dims, const AffineXf3f& gridToMeshXf, float beta, ProgressCallback cb = {} ) = 0;

/// <summary>
/// calculates distances with the sign obtained from winding number in each point from a three-dimensional grid
/// calculates distances with the sign obtained from generalized winding number in each point from a three-dimensional grid;
/// if sqr(res) < minDistSq or sqr(res) >= maxDistSq, then NaN is returned for such point
/// </summary>
/// <param name="res">resulting signed distances, will be resized automatically</param>
/// <param name="dims">dimensions of the grid</param>
Expand Down

0 comments on commit 6e4b7b2

Please sign in to comment.