Skip to content

Commit

Permalink
Split MRFloatGrid.h on heavy and light headers (#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedr authored Jul 14, 2023
1 parent 90d98f4 commit 5bb227d
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 67 deletions.
21 changes: 0 additions & 21 deletions source/MRMesh/MRBoolean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,5 @@ Mesh MeshVoxelsConverter::operator() ( const FloatGrid & grid ) const
return *res;
}

FloatGrid operator += ( FloatGrid & a, const FloatGrid & b )
{
MR_TIMER
openvdb::tools::csgUnion( ovdb( *a ), ovdb( *b ) );
return a;
}

FloatGrid operator -= ( FloatGrid & a, const FloatGrid & b )
{
MR_TIMER
openvdb::tools::csgDifference( ovdb( *a ), ovdb( *b ) );
return a;
}

FloatGrid operator *= ( FloatGrid & a, const FloatGrid & b )
{
MR_TIMER
openvdb::tools::csgIntersection( ovdb( *a ), ovdb( *b ) );
return a;
}

} //namespace MR
#endif
10 changes: 1 addition & 9 deletions source/MRMesh/MRBoolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if !defined( __EMSCRIPTEN__) && !defined( MRMESH_NO_VOXEL )
#include "MRVDBConversions.h"
#include "MRAffineXf3.h"
#include "MRFloatGrid.h"

namespace MR
{
Expand All @@ -29,15 +30,6 @@ struct MeshVoxelsConverter
MRMESH_API Mesh operator() ( const FloatGrid & grid ) const;
};

// union operation on volumetric representation of two meshes
MRMESH_API FloatGrid operator += ( FloatGrid & a, const FloatGrid& b );

// difference operation on volumetric representation of two meshes
MRMESH_API FloatGrid operator -= ( FloatGrid & a, const FloatGrid& b );

// intersection operation on volumetric representation of two meshes
MRMESH_API FloatGrid operator *= ( FloatGrid & a, const FloatGrid& b );

} //namespace MR

#endif
5 changes: 1 addition & 4 deletions source/MRMesh/MRChangeVoxelsAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ class ChangeGridAction : public HistoryAction

[[nodiscard]] virtual size_t heapBytes() const override
{
size_t res = name_.capacity() + histogram_.heapBytes() + changeIsoAction_.heapBytes() + changeSurfaceAction_.heapBytes();
if ( vdbVolume_.data )
res += vdbVolume_.data->memUsage();
return res;
return name_.capacity() + histogram_.heapBytes() + changeIsoAction_.heapBytes() + changeSurfaceAction_.heapBytes() + MR::heapBytes( vdbVolume_.data );
}

private:
Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRFixUndercuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "MRRingIterator.h"
#include "MRDistanceMap.h"
#include "MRColor.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRMeshIntersect.h"
#include "MRLine3.h"
#include "MRPch/MRTBB.h"
Expand Down
39 changes: 35 additions & 4 deletions source/MRMesh/MRFloatGrid.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if !defined( __EMSCRIPTEN__) && !defined( MRMESH_NO_VOXEL )
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRVector3.h"
#include "MRBitSet.h"
#include "MRVolumeIndexer.h"
Expand All @@ -10,11 +11,9 @@
namespace MR
{

FloatGrid MakeFloatGrid( openvdb::FloatGrid::Ptr&& p )
size_t heapBytes( const FloatGrid& grid )
{
if ( !p )
return {};
return std::make_shared<OpenVdbFloatGrid>( std::move( *p ) );
return grid ? grid->memUsage() : 0;
}

FloatGrid resampled( const FloatGrid& grid, const Vector3f& voxelScale, ProgressCallback cb )
Expand Down Expand Up @@ -64,6 +63,11 @@ FloatGrid resampled( const FloatGrid& grid, float voxelScale, ProgressCallback c
return resampled( grid, Vector3f::diagonal( voxelScale ), cb );
}

float getValue( const FloatGrid & grid, const Vector3i & p )
{
return grid ? grid->getConstAccessor().getValue( openvdb::Coord{ p.x, p.y, p.z } ) : 0;
}

void setValue( FloatGrid & grid, const VoxelBitSet& region, float value )
{
if ( !grid )
Expand All @@ -82,5 +86,32 @@ void setValue( FloatGrid & grid, const VoxelBitSet& region, float value )
}
}

void setLevelSetType( FloatGrid & grid )
{
if ( grid )
grid->setGridClass( openvdb::GRID_LEVEL_SET );
}

FloatGrid operator += ( FloatGrid & a, const FloatGrid & b )
{
MR_TIMER
openvdb::tools::csgUnion( ovdb( *a ), ovdb( *b ) );
return a;
}

FloatGrid operator -= ( FloatGrid & a, const FloatGrid & b )
{
MR_TIMER
openvdb::tools::csgDifference( ovdb( *a ), ovdb( *b ) );
return a;
}

FloatGrid operator *= ( FloatGrid & a, const FloatGrid & b )
{
MR_TIMER
openvdb::tools::csgIntersection( ovdb( *a ), ovdb( *b ) );
return a;
}

}
#endif
33 changes: 18 additions & 15 deletions source/MRMesh/MRFloatGrid.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once
#if !defined( __EMSCRIPTEN__) && !defined( MRMESH_NO_VOXEL )
// this header includes the whole OpenVDB, so please include it from .cpp files only
// this is a lightweight header unlike MRVDBFloatGrid.h

#include "MRMeshFwd.h"
#include "MRPch/MROpenvdb.h"
#include "MRProgressCallback.h"

namespace MR
Expand All @@ -15,30 +14,34 @@ namespace MR
* \{
*/

/// this class just hides very complex type of typedef openvdb::FloatGrid
struct OpenVdbFloatGrid : openvdb::FloatGrid
{
OpenVdbFloatGrid() noexcept = default;
OpenVdbFloatGrid( openvdb::FloatGrid && in ) : openvdb::FloatGrid( std::move( in ) ) {}
[[nodiscard]] size_t heapBytes() const { return memUsage(); }
};

inline openvdb::FloatGrid & ovdb( OpenVdbFloatGrid & v ) { return v; }
inline const openvdb::FloatGrid & ovdb( const OpenVdbFloatGrid & v ) { return v; }

/// makes MR::FloatGrid shared pointer taking the contents of the input pointer
MRMESH_API FloatGrid MakeFloatGrid( openvdb::FloatGrid::Ptr&& );
/// returns the amount of heap memory occupied by grid
[[nodiscard]] MRMESH_API size_t heapBytes( const FloatGrid& grid );

/// resample this grid to fit voxelScale
MRMESH_API FloatGrid resampled( const FloatGrid& grid, float voxelScale, ProgressCallback cb = {} );

/// resample this grid to fit voxelScale
MRMESH_API FloatGrid resampled( const FloatGrid& grid, const Vector3f& voxelScale, ProgressCallback cb = {} );

/// returns the value at given voxel
[[nodiscard]] MRMESH_API float getValue( const FloatGrid & grid, const Vector3i & p );

/// sets given region voxels value
/// \note region is in grid space (0 voxel id is minimum active voxel in grid)
MRMESH_API void setValue( FloatGrid & grid, const VoxelBitSet& region, float value );

/// sets type of this grid as LEVEL SET (for normal flipping)
MRMESH_API void setLevelSetType( FloatGrid & grid );

// union operation on volumetric representation of two meshes
MRMESH_API FloatGrid operator += ( FloatGrid & a, const FloatGrid& b );

// difference operation on volumetric representation of two meshes
MRMESH_API FloatGrid operator -= ( FloatGrid & a, const FloatGrid& b );

// intersection operation on volumetric representation of two meshes
MRMESH_API FloatGrid operator *= ( FloatGrid & a, const FloatGrid& b );

/// \}

}
Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRFloatGridComponents.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if !defined( __EMSCRIPTEN__) && !defined( MRMESH_NO_VOXEL )
#include "MRFloatGridComponents.h"
#include "MRUnionFind.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRVolumeIndexer.h"
#include "MRBitSet.h"
#include "MRTimer.h"
Expand Down
1 change: 1 addition & 0 deletions source/MRMesh/MRMesh.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
<ClInclude Include="MRUnionFind.h" />
<ClInclude Include="MRUniquePtr.h" />
<ClInclude Include="MRUniteManyMeshes.h" />
<ClInclude Include="MRVDBFloatGrid.h" />
<ClInclude Include="MRVDBProgressInterrupter.h" />
<ClInclude Include="MRVector2.h" />
<ClInclude Include="MRVertexAttributeGradient.h" />
Expand Down
3 changes: 3 additions & 0 deletions source/MRMesh/MRMesh.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,9 @@
<ClInclude Include="MRVDBProgressInterrupter.h">
<Filter>Source Files\VDBConversions</Filter>
</ClInclude>
<ClInclude Include="MRVDBFloatGrid.h">
<Filter>Source Files\BaseStructures</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MRObject.cpp">
Expand Down
1 change: 1 addition & 0 deletions source/MRMesh/MRObjectVoxels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "MRObjectFactory.h"
#include "MRMesh.h"
#include "MRVDBConversions.h"
#include "MRVDBFloatGrid.h"
#include "MRFloatGrid.h"
#include "MRSimpleVolume.h"
#include "MRVoxelsSave.h"
Expand Down
3 changes: 1 addition & 2 deletions source/MRMesh/MROffset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ Expected<Mesh, std::string> offsetMesh( const MeshPart & mp, float offset, const
// Compute unsigned distance grid
grid = meshToDistanceField( mp, AffineXf3f(), voxelSizeVector, std::abs( offsetInVoxels ) + 2,
subprogress( params.callBack, 0.0f, signPostprocess ? 0.33f : 0.5f ) );
if ( grid ) // to flip mesh normals
grid->setGridClass( openvdb::GRID_LEVEL_SET );
setLevelSetType( grid ); // to flip mesh normals
}

if ( !grid )
Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRVDBConversions.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if !defined( __EMSCRIPTEN__) && !defined( MRMESH_NO_VOXEL )
#include "MRVDBConversions.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRMesh.h"
#include "MRMeshBuilder.h"
#include "MRTimer.h"
Expand Down
39 changes: 39 additions & 0 deletions source/MRMesh/MRVDBFloatGrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#if !defined( __EMSCRIPTEN__) && !defined( MRMESH_NO_VOXEL )
// this header includes the whole OpenVDB, so please include it from .cpp files only

#include "MRMeshFwd.h"
#include "MRPch/MROpenvdb.h"

namespace MR
{

/**
* \defgroup BasicStructuresGroup Basic Structures
* \brief This chapter represents documentation about basic structures elements
* \{
*/

/// this class just hides very complex type of typedef openvdb::FloatGrid
struct OpenVdbFloatGrid : openvdb::FloatGrid
{
OpenVdbFloatGrid() noexcept = default;
OpenVdbFloatGrid( openvdb::FloatGrid && in ) : openvdb::FloatGrid( std::move( in ) ) {}
[[nodiscard]] size_t heapBytes() const { return memUsage(); }
};

inline openvdb::FloatGrid & ovdb( OpenVdbFloatGrid & v ) { return v; }
inline const openvdb::FloatGrid & ovdb( const OpenVdbFloatGrid & v ) { return v; }

/// makes MR::FloatGrid shared pointer taking the contents of the input pointer
inline FloatGrid MakeFloatGrid( openvdb::FloatGrid::Ptr&& p )
{
if ( !p )
return {};
return std::make_shared<OpenVdbFloatGrid>( std::move( *p ) );
}

/// \}

}
#endif
2 changes: 1 addition & 1 deletion source/MRMesh/MRVolumeSegment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "MRBox.h"
#include "MRVoxelGraphCut.h"
#include "MRVDBConversions.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRMesh.h"
#include <filesystem>

Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRVoxelPath.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if !defined( __EMSCRIPTEN__) && !defined( MRMESH_NO_VOXEL )
#include "MRVoxelPath.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRSimpleVolume.h"
#include "MRVector3.h"
#include "MRTimer.h"
Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRVoxelsConversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "MRMeshIntersect.h"
#include "MRLine3.h"
#include "MRMeshBuilder.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRTimer.h"
#include "MRFastWindingNumber.h"
#include "MRPch/MRTBB.h"
Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRVoxelsConversionsByParts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "MRVoxelsConversions.h"

#include "MRGTest.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"

#include <fmt/format.h>

Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRVoxelsLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "MRObjectVoxels.h"
#include "MRVDBConversions.h"
#include "MRStringConvert.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRStringConvert.h"
#include "MRDirectory.h"
#include "MRPch/MRSpdlog.h"
Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRVoxelsSave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "MRVoxelsSave.h"
#include "MRMeshFwd.h"
#include "MRImageSave.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"
#include "MRStringConvert.h"
#include "MRProgressReadWrite.h"
#include "MRColor.h"
Expand Down
2 changes: 1 addition & 1 deletion source/MRMesh/MRVoxelsVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "MRMesh.h"
#include "MRVDBConversions.h"
#include "MRBoolean.h"
#include "MRFloatGrid.h"
#include "MRVDBFloatGrid.h"

namespace MR
{
Expand Down
2 changes: 1 addition & 1 deletion source/MRViewer/MRImGuiImage.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "MRImGuiImage.h"
#include "MRMesh/MRObjectVoxels.h"
#include "MRMesh/MRFloatGrid.h"
#include "MRMesh/MRVDBFloatGrid.h"
#include "MRGLMacro.h"
#include "MRViewer.h"
#include "MRGladGlfw.h"
Expand Down
2 changes: 1 addition & 1 deletion source/mrmeshpy/MRPythonVoxels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "MRMesh/MRSimpleVolume.h"
#include "MRMesh/MRVoxelsSave.h"
#include "MRMesh/MRVoxelsLoad.h"
#include "MRMesh/MRFloatGrid.h"
#include "MRMesh/MRVDBFloatGrid.h"
#include "MRMesh/MRVDBConversions.h"
#include "MRMesh/MRMesh.h"
#include "MRMesh/MRAffineXf3.h"
Expand Down

0 comments on commit 5bb227d

Please sign in to comment.