From 492a735ff72397ae3b81994e3b829fad6ecf8496 Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Sun, 27 Oct 2024 05:14:22 +0900 Subject: [PATCH] [Mass] Apply new factory registration mechanism (#5055) * apply new register mechanism to mass * adding missing registration in sceneutiliy * adding missing registration in engine.transform * fix unit tests by loading missing plugin * Apply suggestions from code review (update descriptions) Co-authored-by: Hugo --------- Co-authored-by: Hugo Co-authored-by: erik pernod --- .../transform/DisplacementMatrixEngine.cpp | 10 ++++---- .../sofa/component/engine/transform/init.cpp | 2 ++ .../src/sofa/component/mass/DiagonalMass.cpp | 10 ++++---- .../sofa/component/mass/MeshMatrixMass.cpp | 10 ++++---- .../src/sofa/component/mass/UniformMass.cpp | 24 ++++--------------- .../Mass/src/sofa/component/mass/init.cpp | 19 ++++++++++++++- .../Mass/tests/DiagonalMass_test.cpp | 1 + .../Mass/tests/MeshMatrixMass_test.cpp | 1 + .../Component/Mass/tests/UniformMass_test.cpp | 3 +++ .../tests/ConstantForceField_test.cpp | 1 + .../component/sceneutility/InfoComponent.cpp | 1 - .../sceneutility/MakeAliasComponent.cpp | 1 - .../sceneutility/MakeDataAliasComponent.cpp | 1 - .../sceneutility/MessageHandlerComponent.cpp | 11 +++++---- .../src/sofa/component/sceneutility/init.cpp | 4 +++- Sofa/Component/src/sofa/component/init.cpp | 1 + 16 files changed, 56 insertions(+), 44 deletions(-) diff --git a/Sofa/Component/Engine/Transform/src/sofa/component/engine/transform/DisplacementMatrixEngine.cpp b/Sofa/Component/Engine/Transform/src/sofa/component/engine/transform/DisplacementMatrixEngine.cpp index 8f7eab1f906..c87b2e0aa35 100644 --- a/Sofa/Component/Engine/Transform/src/sofa/component/engine/transform/DisplacementMatrixEngine.cpp +++ b/Sofa/Component/Engine/Transform/src/sofa/component/engine/transform/DisplacementMatrixEngine.cpp @@ -67,11 +67,11 @@ void DisplacementTransformEngine::mult( Mat4x4& out, const M out = out * inv; } -///////////////////////////////////////// - -int DisplacementMatrixEngineClass = core::RegisterObject("Converts a vector of Rigid to a vector of displacement matrices.") - .add< DisplacementMatrixEngine >() -; +void registerDisplacementMatrixEngine(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Converts a vector of Rigid to a vector of displacement matrices.") + .add< DisplacementMatrixEngine >()); +} template class SOFA_COMPONENT_ENGINE_TRANSFORM_API DisplacementMatrixEngine; diff --git a/Sofa/Component/Engine/Transform/src/sofa/component/engine/transform/init.cpp b/Sofa/Component/Engine/Transform/src/sofa/component/engine/transform/init.cpp index d85e6a7fae0..6cdc506f618 100644 --- a/Sofa/Component/Engine/Transform/src/sofa/component/engine/transform/init.cpp +++ b/Sofa/Component/Engine/Transform/src/sofa/component/engine/transform/init.cpp @@ -28,6 +28,7 @@ namespace sofa::component::engine::transform extern void registerDifferenceEngine(sofa::core::ObjectFactory* factory); extern void registerDilateEngine(sofa::core::ObjectFactory* factory); +extern void registerDisplacementMatrixEngine(sofa::core::ObjectFactory* factory); extern void registerDisplacementTransformEngine(sofa::core::ObjectFactory* factory); extern void registerIndexValueMapper(sofa::core::ObjectFactory* factory); extern void registerIndices2ValuesMapper(sofa::core::ObjectFactory* factory); @@ -73,6 +74,7 @@ void registerObjects(sofa::core::ObjectFactory* factory) { registerDifferenceEngine(factory); registerDilateEngine(factory); + registerDisplacementMatrixEngine(factory); registerDisplacementTransformEngine(factory); registerIndexValueMapper(factory); registerIndices2ValuesMapper(factory); diff --git a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp index 964d86f3e7e..1178d60105e 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/DiagonalMass.cpp @@ -332,8 +332,9 @@ type::Vec6 DiagonalMass::getMomentum ( const MechanicalParams* mpar return getMomentumRigid3Impl(mparams, vx, vv) ; } -// Register in the Factory -int DiagonalMassClass = core::RegisterObject("Define a specific mass for each particle") +void registerDiagonalMass(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Compute a lumped (diagonalized) mass matrix resulting from the space integration of a density over a domain.") .add< DiagonalMass >() .add< DiagonalMass >() .add< DiagonalMass >() @@ -341,9 +342,8 @@ int DiagonalMassClass = core::RegisterObject("Define a specific mass for each pa .add< DiagonalMass >() .add< DiagonalMass >() .add< DiagonalMass >() - .add< DiagonalMass >() - - ; + .add< DiagonalMass >()); +} template class SOFA_COMPONENT_MASS_API DiagonalMass; template class SOFA_COMPONENT_MASS_API DiagonalMass; diff --git a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.cpp b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.cpp index 739fc1147f0..3e085f2d49f 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/MeshMatrixMass.cpp @@ -71,16 +71,16 @@ Vec6 MeshMatrixMass::getMomentum ( const core::MechanicalParams*, con return momentum; } - -// Register in the Factory -int MeshMatrixMassClass = core::RegisterObject("Define a specific mass for each particle") +void registerMeshMatrixMass(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Compute a mass matrix resulting from the space integration of a density over a domain.") .add< MeshMatrixMass >() .add< MeshMatrixMass >() .add< MeshMatrixMass >() .add< MeshMatrixMass >() .add< MeshMatrixMass >() - .add< MeshMatrixMass >() -; + .add< MeshMatrixMass >()); +} template class SOFA_COMPONENT_MASS_API MeshMatrixMass; template class SOFA_COMPONENT_MASS_API MeshMatrixMass; diff --git a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp index ceabfd1e890..f2328c5231f 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/UniformMass.cpp @@ -503,30 +503,16 @@ Vec6 UniformMass::getMomentum ( const MechanicalParams* params, return getMomentumRigid3DImpl(params, d_x, d_v); } - - - -//////////////////////////////////////////// REGISTERING TO FACTORY ///////////////////////////////////////// -/// Registering the component -/// see: https://www.sofa-framework.org/community/doc/programming-with-sofa/components-api/the-objectfactory/ -/// 1-SOFA_DECL_CLASS(componentName) : Set the class name of the component -/// 2-RegisterObject("description") + .add<> : Register the component -/// 3-.add<>(true) : Set default template -// Register in the Factory -int UniformMassClass = core::RegisterObject("Define the same mass for all the particles") - +void registerUniformMass(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Compute a mass equally spread over the number of nodes.") .add< UniformMass >() .add< UniformMass >() .add< UniformMass >() .add< UniformMass >() .add< UniformMass >() - .add< UniformMass >() - - ; -//////////////////////////////////////////////////////////////////////////////////////////////////////// - - - + .add< UniformMass >()); +} ////////////////////////////// TEMPLATE INITIALIZATION ///////////////////////////////////////////////// /// Force template specialization for the most common sofa type. diff --git a/Sofa/Component/Mass/src/sofa/component/mass/init.cpp b/Sofa/Component/Mass/src/sofa/component/mass/init.cpp index de3b283820c..14eb994286d 100644 --- a/Sofa/Component/Mass/src/sofa/component/mass/init.cpp +++ b/Sofa/Component/Mass/src/sofa/component/mass/init.cpp @@ -21,13 +21,20 @@ ******************************************************************************/ #include #include +#include + namespace sofa::component::mass { - + +extern void registerDiagonalMass(sofa::core::ObjectFactory* factory); +extern void registerMeshMatrixMass(sofa::core::ObjectFactory* factory); +extern void registerUniformMass(sofa::core::ObjectFactory* factory); + extern "C" { SOFA_EXPORT_DYNAMIC_LIBRARY void initExternalModule(); SOFA_EXPORT_DYNAMIC_LIBRARY const char* getModuleName(); SOFA_EXPORT_DYNAMIC_LIBRARY const char* getModuleVersion(); + SOFA_EXPORT_DYNAMIC_LIBRARY void registerObjects(sofa::core::ObjectFactory* factory); } void initExternalModule() @@ -45,11 +52,21 @@ const char* getModuleVersion() return MODULE_VERSION; } +void registerObjects(sofa::core::ObjectFactory* factory) +{ + registerDiagonalMass(factory); + registerMeshMatrixMass(factory); + registerUniformMass(factory); +} + void init() { static bool first = true; if (first) { + // make sure that this plugin is registered into the PluginManager + sofa::helper::system::PluginManager::getInstance().registerPlugin(MODULE_NAME); + first = false; } } diff --git a/Sofa/Component/Mass/tests/DiagonalMass_test.cpp b/Sofa/Component/Mass/tests/DiagonalMass_test.cpp index 1c4d0c378db..2fa55643d95 100644 --- a/Sofa/Component/Mass/tests/DiagonalMass_test.cpp +++ b/Sofa/Component/Mass/tests/DiagonalMass_test.cpp @@ -101,6 +101,7 @@ class DiagonalMass_test : public BaseTest sofa::simpleapi::importPlugin("Sofa.Component.Topology.Container.Dynamic"); sofa::simpleapi::importPlugin("Sofa.Component.Topology.Container.Grid"); sofa::simpleapi::importPlugin("Sofa.Component.StateContainer"); + sofa::simpleapi::importPlugin("Sofa.Component.Mass"); simulation = simulation::getSimulation(); root = simulation::getSimulation()->createNewGraph("root"); diff --git a/Sofa/Component/Mass/tests/MeshMatrixMass_test.cpp b/Sofa/Component/Mass/tests/MeshMatrixMass_test.cpp index 3b4dcc8c920..024bf21dd13 100644 --- a/Sofa/Component/Mass/tests/MeshMatrixMass_test.cpp +++ b/Sofa/Component/Mass/tests/MeshMatrixMass_test.cpp @@ -104,6 +104,7 @@ class MeshMatrixMass_test : public BaseTest sofa::simpleapi::importPlugin("Sofa.Component.Topology.Container.Dynamic"); sofa::simpleapi::importPlugin("Sofa.Component.Topology.Container.Grid"); sofa::simpleapi::importPlugin("Sofa.Component.StateContainer"); + sofa::simpleapi::importPlugin("Sofa.Component.Mass"); simulation = simulation::getSimulation(); root = simulation::getSimulation()->createNewGraph("root"); diff --git a/Sofa/Component/Mass/tests/UniformMass_test.cpp b/Sofa/Component/Mass/tests/UniformMass_test.cpp index af48c7aba8e..ddfa5c0eabb 100644 --- a/Sofa/Component/Mass/tests/UniformMass_test.cpp +++ b/Sofa/Component/Mass/tests/UniformMass_test.cpp @@ -79,6 +79,9 @@ struct UniformMassTest : public BaseTest void SetUp() override { + sofa::simpleapi::importPlugin("Sofa.Component.StateContainer"); + sofa::simpleapi::importPlugin("Sofa.Component.Mass"); + todo = true ; m_simu = sofa::simulation::getSimulation(); m_root = m_simu->createNewGraph("root"); diff --git a/Sofa/Component/MechanicalLoad/tests/ConstantForceField_test.cpp b/Sofa/Component/MechanicalLoad/tests/ConstantForceField_test.cpp index 83f763d9c40..a901c1ec931 100644 --- a/Sofa/Component/MechanicalLoad/tests/ConstantForceField_test.cpp +++ b/Sofa/Component/MechanicalLoad/tests/ConstantForceField_test.cpp @@ -78,6 +78,7 @@ struct ConstantForceField_test : public BaseSimulationTest, NumericTest #include -using sofa::core::RegisterObject ; #include diff --git a/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MakeAliasComponent.cpp b/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MakeAliasComponent.cpp index fca69383c2d..0698496c34b 100644 --- a/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MakeAliasComponent.cpp +++ b/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MakeAliasComponent.cpp @@ -21,7 +21,6 @@ ******************************************************************************/ #include #include -using sofa::core::RegisterObject ; using sofa::core::ObjectFactory ; using sofa::core::objectmodel::ComponentState ; diff --git a/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MakeDataAliasComponent.cpp b/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MakeDataAliasComponent.cpp index 6b660d084d1..3f6ee73c225 100644 --- a/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MakeDataAliasComponent.cpp +++ b/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MakeDataAliasComponent.cpp @@ -21,7 +21,6 @@ ******************************************************************************/ #include #include -using sofa::core::RegisterObject ; using sofa::core::ObjectFactory ; using sofa::core::objectmodel::ComponentState ; diff --git a/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MessageHandlerComponent.cpp b/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MessageHandlerComponent.cpp index f3cab5870db..d800a95f905 100644 --- a/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MessageHandlerComponent.cpp +++ b/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/MessageHandlerComponent.cpp @@ -25,7 +25,6 @@ ******************************************************************************/ #include #include -using sofa::core::RegisterObject ; #include using sofa::helper::logging::ConsoleMessageHandler ; @@ -155,8 +154,10 @@ void FileMessageHandlerComponent::parse ( core::objectmodel::BaseObjectDescripti m_isValid = true ; } -int FileMessageHandlerComponentClass = RegisterObject("This component dump all the messages into" - "a file.") - .add< FileMessageHandlerComponent >() - ; +void registerFileMessageHandlerComponent(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("This component dumps all the messages into a file.") + .add< FileMessageHandlerComponent >()); +} + } // namespace sofa::component::sceneutility diff --git a/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/init.cpp b/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/init.cpp index 1daa9e7019f..1f1dead21ae 100644 --- a/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/init.cpp +++ b/Sofa/Component/SceneUtility/src/sofa/component/sceneutility/init.cpp @@ -29,7 +29,8 @@ namespace sofa::component::sceneutility extern void registerAddDataRepository(sofa::core::ObjectFactory* factory); extern void registerAddPluginRepository(sofa::core::ObjectFactory* factory); extern void registerAPIVersion(sofa::core::ObjectFactory* factory); -extern void registerInfoComponent(sofa::core::ObjectFactory* factory);; +extern void registerFileMessageHandlerComponent(sofa::core::ObjectFactory* factory); +extern void registerInfoComponent(sofa::core::ObjectFactory* factory); extern void registerMakeAliasComponent(sofa::core::ObjectFactory* factory); extern void registerMakeDataAliasComponent(sofa::core::ObjectFactory* factory); extern void registerMessageHandlerComponent(sofa::core::ObjectFactory* factory); @@ -62,6 +63,7 @@ void registerObjects(sofa::core::ObjectFactory* factory) registerAddDataRepository(factory); registerAddPluginRepository(factory); registerAPIVersion(factory); + registerFileMessageHandlerComponent(factory); registerInfoComponent(factory);; registerMakeAliasComponent(factory); registerMakeDataAliasComponent(factory); diff --git a/Sofa/Component/src/sofa/component/init.cpp b/Sofa/Component/src/sofa/component/init.cpp index 535063d0fa1..00f7034fde6 100644 --- a/Sofa/Component/src/sofa/component/init.cpp +++ b/Sofa/Component/src/sofa/component/init.cpp @@ -84,6 +84,7 @@ void registerObjects(sofa::core::ObjectFactory* factory) factory->registerObjectsFromPlugin("Sofa.Component.LinearSolver"); factory->registerObjectsFromPlugin("Sofa.Component.LinearSystem"); factory->registerObjectsFromPlugin("Sofa.Component.Mapping"); + factory->registerObjectsFromPlugin("Sofa.Component.Mass"); factory->registerObjectsFromPlugin("Sofa.Component.MechanicalLoad"); factory->registerObjectsFromPlugin("Sofa.Component.ODESolver"); factory->registerObjectsFromPlugin("Sofa.Component.Playback");