Skip to content

Commit

Permalink
Add recalculate_normals toggle to mesh warper (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jul 29, 2024
1 parent 4dfd81a commit 630848f
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Upcoming Release]

- The mesh warper UI now recalculates the result mesh's normals after applying the warp
to the source mesh. This behavior can be disabled using a checkbox that was added to
the UI (#743)
- The socket reassignment popup now shows the name of the component that the socket is
currently assigned to (#770)
- The model editor toolbar now contains a toggle for toggling a model's `show_forces`
Expand Down
1 change: 1 addition & 0 deletions src/OpenSimCreator/Documents/MeshWarper/TPSDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ namespace osc
std::vector<TPSDocumentLandmarkPair> landmarkPairs;
std::vector<TPSDocumentNonParticipatingLandmark> nonParticipatingLandmarks;
float blendingFactor;
bool recalculateNormals;
};
}
20 changes: 18 additions & 2 deletions src/OpenSimCreator/Documents/MeshWarper/TPSWarpResultCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ namespace osc
const bool updatedNonParticipatingLandmarks = updateSourceNonParticipatingLandmarks(doc);
const bool updatedMesh = updateInputMesh(doc);
const bool updatedBlendingFactor = updateBlendingFactor(doc);
const bool updatedRecalculateNormalsState = updateRecalculateNormalsState(doc);

if (updatedCoefficients || updatedNonParticipatingLandmarks || updatedMesh || updatedBlendingFactor)
if (updatedCoefficients || updatedNonParticipatingLandmarks || updatedMesh || updatedBlendingFactor || updatedRecalculateNormalsState)
{
m_CachedResultMesh = ApplyThinPlateWarpToMesh(m_CachedCoefficients, m_CachedSourceMesh, m_CachedBlendingFactor);
m_CachedResultMesh = ApplyThinPlateWarpToMeshVertices(m_CachedCoefficients, m_CachedSourceMesh, m_CachedBlendingFactor);
if (m_CachedRecalculateNormalsState) {
m_CachedResultMesh.recalculate_normals();
}
m_CachedResultNonParticipatingLandmarks = ApplyThinPlateWarpToPoints(m_CachedCoefficients, m_CachedSourceNonParticipatingLandmarks, m_CachedBlendingFactor);
}
}
Expand Down Expand Up @@ -146,10 +150,22 @@ namespace osc
}
}

bool updateRecalculateNormalsState(const TPSDocument& doc)
{
if (m_CachedRecalculateNormalsState != doc.recalculateNormals) {
m_CachedRecalculateNormalsState = doc.recalculateNormals;
return true;
}
else {
return false;
}
}

TPSCoefficientSolverInputs3D m_CachedInputs;
TPSCoefficients3D m_CachedCoefficients;
Mesh m_CachedSourceMesh;
float m_CachedBlendingFactor = 1.0f;
bool m_CachedRecalculateNormalsState = false;
Mesh m_CachedResultMesh;
std::vector<Vec3> m_CachedSourceNonParticipatingLandmarks;
std::vector<Vec3> m_CachedResultNonParticipatingLandmarks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ void osc::ActionSetBlendFactor(UndoableTPSDocument& doc, float factor)
doc.commit_scratch("changed blend factor");
}

void osc::ActionSetRecalculatingNormals(UndoableTPSDocument& doc, bool newState)
{
doc.upd_scratch().recalculateNormals = newState;
const std::string_view msg = newState ?
"enabled recalculating normals" :
"disabled recalculating normals";
doc.commit_scratch(msg);
}

void osc::ActionCreateNewDocument(UndoableTPSDocument& doc)
{
doc.upd_scratch() = TPSDocument{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ namespace osc
// sets the TPS blending factor for the result and saves the change to undo/redo storage
void ActionSetBlendFactor(UndoableTPSDocument&, float factor);

// sets whether the engine should recalculate the mesh's normal after applying the warp
void ActionSetRecalculatingNormals(UndoableTPSDocument&, bool newState);

// creates a "fresh" (default) TPS document
void ActionCreateNewDocument(UndoableTPSDocument&);

Expand Down
4 changes: 2 additions & 2 deletions src/OpenSimCreator/UI/Experimental/TPS2DTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ namespace

// returns a mesh that is the equivalent of applying the 2D TPS warp to all
// vertices of the input mesh
Mesh ApplyThinPlateWarpToMesh(const ThinPlateWarper2D& t, const Mesh& mesh)
Mesh ApplyThinPlateWarpToMeshVertices(const ThinPlateWarper2D& t, const Mesh& mesh)
{
Mesh rv = mesh;
rv.transform_vertices([&t](Vec3 v) { return Vec3{t.transform(Vec2{v}), v.z}; });
Expand Down Expand Up @@ -377,7 +377,7 @@ class osc::TPS2DTab::Impl final {
p.dest = lerp(p.src, p.dest, m_BlendingFactor);
}
ThinPlateWarper2D warper{pairs};
m_OutputGrid = ApplyThinPlateWarpToMesh(warper, m_InputGrid);
m_OutputGrid = ApplyThinPlateWarpToMeshVertices(warper, m_InputGrid);
}

renderMesh(m_OutputGrid, texDims, m_OutputRender);
Expand Down
12 changes: 10 additions & 2 deletions src/OpenSimCreator/UI/MeshWarper/MeshWarpingTabResultMeshPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ namespace osc
ui::same_line();
drawAutoFitCameraButton();
ui::same_line();
ui::draw_checkbox("show destination", &m_ShowDestinationMesh);
ui::same_line();
drawLandmarkRadiusSlider();
drawBlendingFactorSlider();

ui::set_cursor_pos_x(m_CursorXAtExportButton); // align with "export" button in row above
ui::draw_checkbox("overlay destination mesh", &m_ShowDestinationMesh);
ui::same_line();
{
bool recalculatingNormals = m_State->getScratch().recalculateNormals;
if (ui::draw_checkbox("recalculate mesh's normals", &recalculatingNormals)) {
ActionSetRecalculatingNormals(m_State->updUndoable(), recalculatingNormals);
}
}
}

// draws a information icon that shows basic mesh info when hovered
Expand Down
4 changes: 2 additions & 2 deletions src/OpenSimCreator/Utils/TPS3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ Vec3 osc::EvaluateTPSEquation(const TPSCoefficients3D& coefs, Vec3 p)
}

// returns a mesh that is the equivalent of applying the 3D TPS warp to each vertex of the mesh
Mesh osc::ApplyThinPlateWarpToMesh(const TPSCoefficients3D& coefs, const Mesh& mesh, float blendingFactor)
Mesh osc::ApplyThinPlateWarpToMeshVertices(const TPSCoefficients3D& coefs, const Mesh& mesh, float blendingFactor)
{
OSC_PERF("ApplyThinPlateWarpToMesh");
OSC_PERF("ApplyThinPlateWarpToMeshVertices");

Mesh rv = mesh; // make a local copy of the input mesh

Expand Down
2 changes: 1 addition & 1 deletion src/OpenSimCreator/Utils/TPS3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace osc
Vec3 EvaluateTPSEquation(const TPSCoefficients3D&, Vec3);

// returns a mesh that is the equivalent of applying the 3D TPS warp to the mesh
Mesh ApplyThinPlateWarpToMesh(const TPSCoefficients3D&, const Mesh&, float blendingFactor);
Mesh ApplyThinPlateWarpToMeshVertices(const TPSCoefficients3D&, const Mesh&, float blendingFactor);

// returns points that are the equivalent of applying the 3D TPS warp to each input point
std::vector<Vec3> ApplyThinPlateWarpToPoints(const TPSCoefficients3D&, std::span<const Vec3>, float blendingFactor);
Expand Down

0 comments on commit 630848f

Please sign in to comment.