Skip to content

Commit

Permalink
Stub out WIP implementation of StationDefinedFrame baking (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Feb 13, 2025
1 parent 5597980 commit 64a438a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
48 changes: 48 additions & 0 deletions libopensimcreator/Documents/Model/UndoableModelActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include <OpenSim/Simulation/Model/PathPointSet.h>
#include <OpenSim/Simulation/Model/PhysicalFrame.h>
#include <OpenSim/Simulation/Model/PhysicalOffsetFrame.h>
#include <OpenSim/Simulation/Model/StationDefinedFrame.h>
#include <OpenSim/Simulation/SimbodyEngine/Body.h>
#include <OpenSim/Simulation/SimbodyEngine/Coordinate.h>
#include <OpenSim/Simulation/SimbodyEngine/Joint.h>
Expand All @@ -63,6 +64,7 @@
#include <algorithm>
#include <chrono>
#include <exception>
#include <iterator>
#include <memory>
#include <optional>
#include <sstream>
Expand Down Expand Up @@ -2330,3 +2332,49 @@ bool osc::ActionExportModelMultibodySystemAsDotviz(const OpenSim::Model& model)
set_clipboard_text(std::move(ss).str());
return true;
}

bool osc::ActionBakeStationDefinedFrames(IModelStatePair& model)
{
if (model.isReadonly()) {
return false;
}

// Ensure there is at least one `StationDefinedFrame`s in the model.
{
auto lst = model.getModel().getComponentList<OpenSim::StationDefinedFrame>();
if (std::distance(lst.begin(), lst.end()) == 0) {
return false;
}
}

// Mutate the model by adding equivalent `PhysicalOffsetFrame`s to the
// model, reattaching stuff to it, and then deleting the `StationDefinedFrame`.
//
// TODO:
// - Create `PhysicalOffsetFrame` with a transform equivalent to the `StationDefinedFrame`
// - Copy over anything that the `StationDefinedFrame` owns (e.g. component list, AttachedGeometry)
// - Delete the `StationDefinedFrame` from the model.
// - Add the `PhysicalOffsetFrame` into the model in the exact same location + name, so that
// all sockets, associations, etc. work as expected
OpenSim::Model& mutModel = model.updModel();
for (auto& sdf : mutModel.updComponentList<OpenSim::StationDefinedFrame>()) {
auto pof = std::make_unique<OpenSim::PhysicalOffsetFrame>();
//static_cast<OpenSim::Object&>(*pof) = sdf;
// TODO: copy component/frame stuff (attached geometry, wrap objects)
pof->setName(sdf.getName() + "_tmp");
const SimTK::Transform xform = sdf.findTransformInBaseFrame();
pof->set_translation(xform.p());
pof->set_orientation(xform.R().convertRotationToBodyFixedXYZ());
pof->updSocket("parent").setConnecteePath(sdf.findBaseFrame().getAbsolutePathString());
//pof->connectSocket_parent(sdf.findBaseFrame());

// Add it into the model
mutModel.updComponent(sdf.getAbsolutePath().getParentPath()).addComponent(pof.release());
// TODO: reassign anything that links to the SDF to instead link to the POF
}
InitializeModel(mutModel);
InitializeState(mutModel);
model.commit("Bake `StationDefinedFrame`s");

return true;
}
1 change: 1 addition & 0 deletions libopensimcreator/Documents/Model/UndoableModelActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,5 @@ namespace osc
bool ActionExportModelGraphToDotviz(const OpenSim::Model&);
bool ActionExportModelGraphToDotvizClipboard(const OpenSim::Model&);
bool ActionExportModelMultibodySystemAsDotviz(const OpenSim::Model&);
bool ActionBakeStationDefinedFrames(IModelStatePair&);
}
5 changes: 5 additions & 0 deletions libopensimcreator/UI/ModelEditor/ModelEditorMainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class osc::ModelEditorMainMenu::Impl final : public WidgetPrivate {
}
ui::draw_tooltip_if_item_hovered("Writes the model's multibody system (kinematic chain) in dotviz format, so that it can be visualized in external tooling such as Graphviz Online");

if (ui::draw_menu_item("WIP: Bake Station Defined Frames")) {
ActionBakeStationDefinedFrames(*m_Model);
}
ui::draw_tooltip_if_item_hovered("WORK IN PROGRESS (WIP): Converts any `StationDefinedFrame`s in the model into `PhysicalOffsetFrame`s. Effectively, \"baking\" the current (station-defined) frame transform.\n\nThe main reason to do this is backwards compatibility, OpenSim <= v4.5 doesn't have native support for `StationDefinedFrame`s (later versions should: see opensim-core/#3694)");

ui::end_menu();
}

Expand Down
1 change: 1 addition & 0 deletions opensimcreator/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 64a438a

Please sign in to comment.