Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions source/MRMesh/MRChangeMeshDataAction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "MRChangeMeshDataAction.h"
#include "MRMesh.h"
#include "MRPartialChangeMeshAction.h"
#include "MRChangeMeshAction.h"
#include "MRChangeVertsColorMapAction.h"
#include "MRChangeColoringActions.h"
#include "MRChangeSelectionAction.h"

namespace MR
{

PartialChangeMeshDataAction::PartialChangeMeshDataAction( std::string name, const std::shared_ptr<ObjectMesh>& obj, ObjectMeshData&& newData )
{
std::vector<std::shared_ptr<HistoryAction>> actions;
actions.push_back( std::make_shared<PartialChangeMeshAction>( "mesh", obj, setNew, std::move( newData.mesh ) ) );

actions.push_back( std::make_shared<ChangeMeshUVCoordsAction>( "setUVCoords", obj, std::move( newData.uvCoordinates ) ) );
actions.push_back( std::make_shared<ChangeMeshTexturePerFaceAction>( "setTexturePerFace", obj, std::move( newData.texturePerFace ) ) );
actions.push_back( std::make_shared<ChangeVertsColorMapAction<ObjectMesh>>( "setVertsColorMap", obj, std::move( newData.vertColors ) ) );
actions.push_back( std::make_shared<ChangeFacesColorMapAction>( "setFacesColorMap", obj, std::move( newData.faceColors ) ) );
actions.push_back( std::make_shared<ChangeMeshFaceSelectionAction>( "faceSelection", obj, std::move( newData.selectedFaces ) ) );
actions.push_back( std::make_shared<ChangeMeshEdgeSelectionAction>( "edgeSelection", obj, std::move( newData.selectedEdges ) ) );
actions.push_back( std::make_shared<ChangeMeshCreasesAction>( "creases", obj, std::move( newData.creases ) ) );

combinedAction_ = std::make_unique<CombinedHistoryAction>( std::move( name ), actions );
}

}
35 changes: 34 additions & 1 deletion source/MRMesh/MRChangeMeshDataAction.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "MRHistoryAction.h"
#include "MRCombinedHistoryAction.h"
#include "MRObjectMesh.h"

namespace MR
Expand Down Expand Up @@ -75,6 +75,39 @@ class ChangeMeshDataAction : public HistoryAction
std::string name_;
};

/// Undo action for ObjectMeshData change partially
class MRMESH_CLASS PartialChangeMeshDataAction : public HistoryAction
{
public:
/// use this constructor to remember object's data and immediately set new data
MRMESH_API PartialChangeMeshDataAction( std::string name, const std::shared_ptr<ObjectMesh>& obj, ObjectMeshData&& newData );

virtual std::string name() const override
{
if ( combinedAction_ )
return combinedAction_->name();
assert( false );
return "##empty_PartialChangeMeshDataAction";
}

virtual void action( HistoryAction::Type type) override
{
if ( combinedAction_ )
combinedAction_->action( type );
}

[[nodiscard]] virtual size_t heapBytes() const override
{
if ( combinedAction_ )
return combinedAction_->heapBytes() + sizeof( CombinedHistoryAction );
else
return 0;
}

private:
std::unique_ptr<CombinedHistoryAction> combinedAction_;
};

/// \}

} // namespace MR
2 changes: 2 additions & 0 deletions source/MRMesh/MRMesh.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="MRAlignContoursToMesh.cpp" />
<ClCompile Include="MRChangeMeshDataAction.cpp" />
<ClCompile Include="MRDirMax.cpp" />
<ClCompile Include="MRDirMaxBruteForce.cpp" />
<ClCompile Include="MREdgeLengthMesh.cpp" />
Expand Down Expand Up @@ -620,6 +621,7 @@
<ClCompile Include="MRPrism.cpp" />
<ClCompile Include="MRProcessSelfTreeSubtasks.cpp" />
<ClCompile Include="MRProgressReadWrite.cpp" />
<ClCompile Include="MRProjectionMeshAttribute.cpp" />
<ClCompile Include="MRRadiusCompensation.cpp" />
<ClCompile Include="MRSaveSettings.cpp" />
<ClCompile Include="MRSceneLoad.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions source/MRMesh/MRMesh.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,12 @@
<ClCompile Include="MRTelemetry.cpp">
<Filter>Source Files\System</Filter>
</ClCompile>
<ClCompile Include="MRProjectionMeshAttribute.cpp">
<Filter>Source Files\MeshAlgorithm</Filter>
</ClCompile>
<ClCompile Include="MRChangeMeshDataAction.cpp">
<Filter>Source Files\History</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\.editorconfig" />
Expand Down
98 changes: 98 additions & 0 deletions source/MRMesh/MRProjectionMeshAttribute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "MRProjectionMeshAttribute.h"
#include "MRObjectMeshData.h"
#include "MRRegionBoundary.h"
#include "MRBitSet.h"

namespace MR
{

Expected<void> projectObjectMeshData( const ObjectMeshData& oldMeshData, ObjectMeshData& newMeshData,
const FaceBitSet* region /*= nullptr*/, const ProjectAttributeParams& params /*= {} */ )
{
if ( !newMeshData.mesh || !oldMeshData.mesh )
{
assert( false );
return unexpected( "No mesh for projecting attributes" );
}
const auto& newMesh = *newMeshData.mesh;
const auto& oldVertColors = oldMeshData.vertColors;
const auto& oldUVCoords = oldMeshData.uvCoordinates;
const auto& oldFaceColorMap = oldMeshData.faceColors;
const auto& oldTexturePerFace = oldMeshData.texturePerFace;
const auto& oldFaceSelection = oldMeshData.selectedFaces;

if ( !oldUVCoords.empty() )
{
newMeshData.uvCoordinates = oldUVCoords;
newMeshData.uvCoordinates.resize( size_t( newMesh.topology.lastValidVert() + 1 ) );
}
if ( !oldVertColors.empty() )
{
newMeshData.vertColors = oldVertColors;
newMeshData.vertColors.resize( size_t( newMesh.topology.lastValidVert() + 1 ) );
}
if ( !oldFaceColorMap.empty() )
{
newMeshData.faceColors = oldFaceColorMap;
newMeshData.faceColors.resize( size_t( newMesh.topology.lastValidFace() + 1 ) );
}
if ( !oldTexturePerFace.empty() )
{
newMeshData.texturePerFace = oldTexturePerFace;
newMeshData.texturePerFace.resize( size_t( newMesh.topology.lastValidFace() + 1 ) );
}
bool haveFaceSelection = oldFaceSelection.any();
if ( haveFaceSelection )
{
newMeshData.selectedFaces = oldFaceSelection;
newMeshData.selectedFaces.resize( size_t( newMesh.topology.lastValidFace() + 1 ) );
}

const bool hasFaceAttribs = !oldFaceColorMap.empty() || !oldTexturePerFace.empty() || haveFaceSelection;
const bool hasVertAttribs = !oldVertColors.empty() || !oldUVCoords.empty();

auto faceFunc = [&] ( FaceId id, const MeshProjectionResult& res )
{
if ( !oldFaceColorMap.empty() )
newMeshData.faceColors[id] = oldFaceColorMap[res.proj.face];
if ( !oldTexturePerFace.empty() )
newMeshData.texturePerFace[id] = oldTexturePerFace[res.proj.face];
if ( haveFaceSelection )
newMeshData.selectedFaces.set( id, oldFaceSelection.test( res.proj.face ) );
};

ProjectAttributeParams localParams = params;

if ( hasVertAttribs )
{
auto vertFunc = [&] ( VertId id, const MeshProjectionResult& res, VertId v1, VertId v2, VertId v3 )
{
if ( !oldVertColors.empty() )
newMeshData.vertColors[id] = res.mtp.bary.interpolate( oldVertColors[v1], oldVertColors[v2], oldVertColors[v3] );
if ( !oldUVCoords.empty() )
newMeshData.uvCoordinates[id] = res.mtp.bary.interpolate( oldUVCoords[v1], oldUVCoords[v2], oldUVCoords[v3] );
};

VertBitSet vertRegion;
MeshVertPart mvp( newMesh );
if ( region )
{
vertRegion = getIncidentVerts( newMesh.topology, *region );
mvp.region = &vertRegion;
}


localParams.progressCb = subprogress( params.progressCb, 0.0f, hasFaceAttribs ? 0.5f : 1.0f );
if ( !projectVertAttribute( mvp, *oldMeshData.mesh, vertFunc, localParams ) )
return unexpectedOperationCanceled();

}

localParams.progressCb = subprogress( params.progressCb, hasVertAttribs ? 0.5f : 0.0f, 1.0f );
if ( hasFaceAttribs && !projectFaceAttribute( MeshPart( newMesh, region ), *oldMeshData.mesh, faceFunc, localParams ) )
return unexpectedOperationCanceled();

return {};
}

}
22 changes: 18 additions & 4 deletions source/MRMesh/MRProjectionMeshAttribute.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include "MRMeshFwd.h"
#include "MRAffineXf.h"
#include "MRMatrix3.h"
#include "MRMesh.h"
#include "MRBitSetParallelFor.h"
#include "MRMeshProject.h"
Expand All @@ -9,22 +11,34 @@
namespace MR
{

/// this structure contains transformation for projection from one mesh to anther and progress callback
Copy link
Contributor

Choose a reason for hiding this comment

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

anOther

struct ProjectAttributeParams
{
MeshProjectionTransforms xfs;
ProgressCallback progressCb;
};

// projecting the vertex attributes of the old onto the new one
// returns false if canceled by progress bar
/// projecting the vertex attributes of the old onto the new one
/// returns false if canceled by progress bar
template<typename F>
bool projectVertAttribute( const MeshVertPart& mp, const Mesh& oldMesh, F&& func, const ProjectAttributeParams& params = {} );

// projecting the face attributes of the old onto the new one
// returns false if canceled by progress bar
/// projecting the face attributes of the old onto the new one
/// returns false if canceled by progress bar
template<typename F>
bool projectFaceAttribute( const MeshPart& mp, const Mesh& oldMesh, F&& func, const ProjectAttributeParams& params = {} );

/// <summary>
/// finds attributes of new mesh by projecting faces/vertices on old mesh
/// \note for now clears edges attributes
/// </summary>
/// <param name="oldMeshData">old mesh along with input attributes</param>
/// <param name="newMeshData">new mesh along with outpuyt attributes</param>
/// <param name="region">optional input region for projecting (usefull if newMesh is changed part of old mesh)</param>
/// <param name="params">parameters of prohecting</param>
[[nodiscard]] MRMESH_API Expected<void> projectObjectMeshData(
const ObjectMeshData& oldMeshData, ObjectMeshData& newMeshData, const FaceBitSet* region = nullptr,
const ProjectAttributeParams& params = {} );

template<typename F>
bool projectVertAttribute( const MeshVertPart& mp, const Mesh& oldMesh, F&& func, const ProjectAttributeParams& params )
Expand Down
123 changes: 0 additions & 123 deletions source/MRViewer/MRProjectMeshAttributes.cpp

This file was deleted.

Loading
Loading