Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions source/MRVoxels/MRFillingSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,52 @@ FunctionVolume buildVolume( const Vector3f& size, const VolumeParams& params )
Expected<Mesh> build( const Vector3f& size, const MeshParams& params, ProgressCallback cb )
{
MR_TIMER;
ProgressCallback mcProgress, decProgress;
ProgressCallback mcProgress, repProgress, decProgress;
if ( params.decimate )
{
mcProgress = subprogress( cb, 0.f, 0.8f );
mcProgress = subprogress( cb, 0.f, 0.2f );
repProgress = subprogress( cb, 0.2f, 0.8f );
decProgress = subprogress( cb, 0.8f, 1.f );
}
else
{
mcProgress = cb;
mcProgress = subprogress( cb, 0.f, 0.3f );
repProgress = subprogress( cb, 0.3f, 1.f );
}

auto res = marchingCubes( buildVolume( size, params ), { .cb = mcProgress, .iso = params.iso } );
if ( !res )
return res;
const float period = 1.f / params.frequency;
const auto voxelSize = getDimsAndSize( size, params.frequency, params.resolution ).size;
const float minVoxelSize = std::min( { voxelSize.x, voxelSize.y, voxelSize.z } );
const float maxVoxelSize = std::max( { voxelSize.x, voxelSize.y, voxelSize.z } );

Mesh baseElement;
QME( baseElement, marchingCubes( buildVolume( Vector3f::diagonal( period ) + voxelSize / 2.f, params ), { .cb = mcProgress, .iso = params.iso } ) )
if ( isThick( params.type ) )
res->topology.flipOrientation();
baseElement.topology.flipOrientation();

if ( params.decimate )
decimateMesh( baseElement, { .maxError = minVoxelSize, .stabilizer = minVoxelSize / 100.f, .touchNearBdEdges = false, .touchBdVerts = false } );

Mesh result;
for ( int x = 0; x < size.x * params.frequency; ++x )
{
const auto voxelSize = getDimsAndSize( size, params.frequency, params.resolution ).size;
decimateMesh( *res, DecimateSettings{ .maxError = std::min( voxelSize.x, std::min( voxelSize.y, voxelSize.z ) ), .progressCallback = decProgress } );
for ( int y = 0; y < size.y * params.frequency; ++y )
{
for ( int z = 0; z < size.z * params.frequency; ++z )
{
auto mesh = baseElement;
mesh.transform( AffineXf3f::translation( Vector3f( (float)x, (float)y, (float)z ) * period ) );
result.addMesh( mesh, {}, true );
}
}
if ( !reportProgress( repProgress, (float)x * period / float( size.x ) ) )
return unexpectedOperationCanceled();
}
return res;

MeshBuilder::uniteCloseVertices( result, maxVoxelSize );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is better to call uniteCloseVertices( result, 0 ), or very small value if rounding errors are inevitable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic element is created from voxels, thus we cannot guarantee the precision smaller than the voxel size.

if ( params.decimate )
decimateMesh( result, DecimateSettings{ .maxError = minVoxelSize, .progressCallback = decProgress } );
return result;
}

Expected<Mesh> fill( const Mesh& mesh, const MeshParams& params, ProgressCallback cb )
Expand Down
Loading