From 99df0a21e3764151270fe9f698387b599cb5618a Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Tue, 17 Dec 2024 15:26:53 +0100 Subject: [PATCH] [Visual] Move, rename and clean OglCylinderModel (#5124) * move files * depreciation * rename * fix color management * remove string to color conversion and use the one from RGBAColor * remove unused private methods * cleaning * component change * remove Index alias * change the description * cache drawTool * remove calls to removed functions --- Sofa/Component/Visual/CMakeLists.txt | 2 + .../component/visual/CylinderVisualModel.cpp | 94 ++++++++++ .../component/visual/CylinderVisualModel.h | 61 +++++++ .../Visual/src/sofa/component/visual/init.cpp | 2 + Sofa/GL/Component/Rendering3D/CMakeLists.txt | 1 - .../rendering3d/OglCylinderModel.cpp | 163 ------------------ .../component/rendering3d/OglCylinderModel.h | 68 +------- .../sofa/gl/component/rendering3d/init.cpp | 2 - .../src/sofa/helper/ComponentChange.cpp | 8 +- .../Component/Visual/CylinderVisualModel.scn | 8 + .../Component/Visual/OglCylinderModel.scn | 9 - 11 files changed, 178 insertions(+), 240 deletions(-) create mode 100644 Sofa/Component/Visual/src/sofa/component/visual/CylinderVisualModel.cpp create mode 100644 Sofa/Component/Visual/src/sofa/component/visual/CylinderVisualModel.h delete mode 100644 Sofa/GL/Component/Rendering3D/src/sofa/gl/component/rendering3d/OglCylinderModel.cpp create mode 100644 examples/Component/Visual/CylinderVisualModel.scn delete mode 100644 examples/Component/Visual/OglCylinderModel.scn diff --git a/Sofa/Component/Visual/CMakeLists.txt b/Sofa/Component/Visual/CMakeLists.txt index 8dd7b074047..d1a525f0c29 100644 --- a/Sofa/Component/Visual/CMakeLists.txt +++ b/Sofa/Component/Visual/CMakeLists.txt @@ -8,6 +8,7 @@ set(HEADER_FILES ${SOFACOMPONENTVISUAL_SOURCE_DIR}/init.h ${SOFACOMPONENTVISUAL_SOURCE_DIR}/BaseCamera.h ${SOFACOMPONENTVISUAL_SOURCE_DIR}/Camera.h + ${SOFACOMPONENTVISUAL_SOURCE_DIR}/CylinderVisualModel.h ${SOFACOMPONENTVISUAL_SOURCE_DIR}/InteractiveCamera.h ${SOFACOMPONENTVISUAL_SOURCE_DIR}/LineAxis.h ${SOFACOMPONENTVISUAL_SOURCE_DIR}/RecordedCamera.h @@ -24,6 +25,7 @@ set(SOURCE_FILES ${SOFACOMPONENTVISUAL_SOURCE_DIR}/init.cpp ${SOFACOMPONENTVISUAL_SOURCE_DIR}/BaseCamera.cpp ${SOFACOMPONENTVISUAL_SOURCE_DIR}/Camera.cpp + ${SOFACOMPONENTVISUAL_SOURCE_DIR}/CylinderVisualModel.cpp ${SOFACOMPONENTVISUAL_SOURCE_DIR}/InteractiveCamera.cpp ${SOFACOMPONENTVISUAL_SOURCE_DIR}/LineAxis.cpp ${SOFACOMPONENTVISUAL_SOURCE_DIR}/RecordedCamera.cpp diff --git a/Sofa/Component/Visual/src/sofa/component/visual/CylinderVisualModel.cpp b/Sofa/Component/Visual/src/sofa/component/visual/CylinderVisualModel.cpp new file mode 100644 index 00000000000..98d778731bd --- /dev/null +++ b/Sofa/Component/Visual/src/sofa/component/visual/CylinderVisualModel.cpp @@ -0,0 +1,94 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#include +#include +#include +#include + +namespace sofa::component::visual +{ + +void registerCylinderVisualModel(sofa::core::ObjectFactory* factory) +{ + factory->registerObjects(core::ObjectRegistrationData("Visualize a set of cylinders.") + .add< CylinderVisualModel >()); +} + +CylinderVisualModel::CylinderVisualModel() + : radius(initData(&radius, 1.0f, "radius", "Radius of the cylinder.")), + color(initData(&color, sofa::type::RGBAColor::white(), "color", "Color of the cylinders.")) + , d_edges(initData(&d_edges,"edges","List of edge indices")) +{ +} + +CylinderVisualModel::~CylinderVisualModel() = default; + +void CylinderVisualModel::init() +{ + VisualModel::init(); + + reinit(); +} + +void CylinderVisualModel::doDrawVisual(const core::visual::VisualParams* vparams) +{ + const VecCoord& pos = this->read( core::vec_id::read_access::position )->getValue(); + + auto* drawTool = vparams->drawTool(); + + drawTool->setLightingEnabled(true); + + const float _radius = radius.getValue(); + const sofa::type::RGBAColor& col = color.getValue(); + + const SeqEdges& edges = d_edges.getValue(); + + for(const auto& edge : edges) + { + const Coord& p1 = pos[edge[0]]; + const Coord& p2 = pos[edge[1]]; + + drawTool->drawCylinder(p1, p2, _radius, col); + } +} + +void CylinderVisualModel::exportOBJ(std::string name, std::ostream* out, std::ostream* /*mtl*/, Index& vindex, Index& /*nindex*/, Index& /*tindex*/, int& /*count*/) +{ + const VecCoord& x = this->read( core::vec_id::read_access::position )->getValue(); + const SeqEdges& edges = d_edges.getValue(); + + const int nbv = int(x.size()); + + *out << "g "<. * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once +#include + +#include +#include +#include + +namespace sofa::component::visual +{ + +class SOFA_COMPONENT_VISUAL_API CylinderVisualModel : + public core::visual::VisualModel, public sofa::core::visual::VisualState +{ +public: + using Vec3State = sofa::core::visual::VisualState; + SOFA_CLASS2(CylinderVisualModel,core::visual::VisualModel, Vec3State); + +protected: + CylinderVisualModel(); + ~CylinderVisualModel() override; +public: + void init() override; + + void doDrawVisual(const core::visual::VisualParams* vparams) override; + + void exportOBJ(std::string /*name*/, std::ostream* /*out*/, std::ostream* /*mtl*/, Index& /*vindex*/, Index& /*nindex*/, Index& /*tindex*/, int& /*count*/) override; + +private: + Data radius; ///< Radius of the cylinder. + Data color; ///< Color of the cylinders. + + typedef sofa::type::vector SeqEdges; + Data d_edges; ///< List of edge indices + +public: + bool insertInNode( core::objectmodel::BaseNode* node ) override { Inherit1::insertInNode(node); Inherit2::insertInNode(node); return true; } + bool removeInNode( core::objectmodel::BaseNode* node ) override { Inherit1::removeInNode(node); Inherit2::removeInNode(node); return true; } +}; + +} // namespace sofa::component::visual diff --git a/Sofa/Component/Visual/src/sofa/component/visual/init.cpp b/Sofa/Component/Visual/src/sofa/component/visual/init.cpp index b2f8a2c08eb..c4060d561ac 100644 --- a/Sofa/Component/Visual/src/sofa/component/visual/init.cpp +++ b/Sofa/Component/Visual/src/sofa/component/visual/init.cpp @@ -27,6 +27,7 @@ namespace sofa::component::visual { extern void registerCamera(sofa::core::ObjectFactory* factory); +extern void registerCylinderVisualModel(sofa::core::ObjectFactory* factory); extern void registerInteractiveCamera(sofa::core::ObjectFactory* factory); extern void registerLineAxis(sofa::core::ObjectFactory* factory); extern void registerRecordedCamera(sofa::core::ObjectFactory* factory); @@ -63,6 +64,7 @@ void registerObjects(sofa::core::ObjectFactory* factory) { registerCamera(factory); registerInteractiveCamera(factory); + registerCylinderVisualModel(factory); registerLineAxis(factory); registerRecordedCamera(factory); registerTrailRenderer(factory); diff --git a/Sofa/GL/Component/Rendering3D/CMakeLists.txt b/Sofa/GL/Component/Rendering3D/CMakeLists.txt index 112674f2623..948bc3ac2b8 100644 --- a/Sofa/GL/Component/Rendering3D/CMakeLists.txt +++ b/Sofa/GL/Component/Rendering3D/CMakeLists.txt @@ -21,7 +21,6 @@ set(SOURCE_FILES ${SOFAGLCOMPONENTRENDERING3D_SOURCE_DIR}/ClipPlane.cpp ${SOFAGLCOMPONENTRENDERING3D_SOURCE_DIR}/DataDisplay.cpp ${SOFAGLCOMPONENTRENDERING3D_SOURCE_DIR}/MergeVisualModels.cpp - ${SOFAGLCOMPONENTRENDERING3D_SOURCE_DIR}/OglCylinderModel.cpp ${SOFAGLCOMPONENTRENDERING3D_SOURCE_DIR}/OglModel.cpp ${SOFAGLCOMPONENTRENDERING3D_SOURCE_DIR}/OglSceneFrame.cpp ${SOFAGLCOMPONENTRENDERING3D_SOURCE_DIR}/PointSplatModel.cpp diff --git a/Sofa/GL/Component/Rendering3D/src/sofa/gl/component/rendering3d/OglCylinderModel.cpp b/Sofa/GL/Component/Rendering3D/src/sofa/gl/component/rendering3d/OglCylinderModel.cpp deleted file mode 100644 index 9067ba2522b..00000000000 --- a/Sofa/GL/Component/Rendering3D/src/sofa/gl/component/rendering3d/OglCylinderModel.cpp +++ /dev/null @@ -1,163 +0,0 @@ -/****************************************************************************** -* SOFA, Simulation Open-Framework Architecture * -* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * -* * -* This program is free software; you can redistribute it and/or modify it * -* under the terms of the GNU Lesser General Public License as published by * -* the Free Software Foundation; either version 2.1 of the License, or (at * -* your option) any later version. * -* * -* This program is distributed in the hope that it will be useful, but WITHOUT * -* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * -* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * -* for more details. * -* * -* You should have received a copy of the GNU Lesser General Public License * -* along with this program. If not, see . * -******************************************************************************* -* Authors: The SOFA Team and external contributors (see Authors.txt) * -* * -* Contact information: contact@sofa-framework.org * -******************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include - -namespace sofa::gl::component::rendering3d -{ - -void registerOglCylinderModel(sofa::core::ObjectFactory* factory) -{ - factory->registerObjects(core::ObjectRegistrationData("OpenGL-based visualization for a cylinders over edges.") - .add< OglCylinderModel >()); -} - -using namespace sofa::defaulttype; -using namespace sofa::core::topology; - -OglCylinderModel::OglCylinderModel() - : radius(initData(&radius, 1.0f, "radius", "Radius of the cylinder.")), - color(initData(&color, sofa::type::RGBAColor(1.0,1.0,1.0,1.0), "color", "Color of the cylinders.")) - , d_edges(initData(&d_edges,"edges","List of edge indices")) - // , pointData(initData(&pointData, "pointData", "scalar field modulating point colors")) -{ -} - -OglCylinderModel::~OglCylinderModel() -{ -} - -void OglCylinderModel::init() -{ - VisualModel::init(); - - reinit(); -} - -void OglCylinderModel::doDrawVisual(const core::visual::VisualParams* vparams) -{ - const VecCoord& pos = this->read( core::vec_id::read_access::position )->getValue(); - - vparams->drawTool()->setLightingEnabled(true); - const float _radius = radius.getValue(); - - const sofa::type::RGBAColor col( r, g, b, a ); - - const SeqEdges& edges = d_edges.getValue(); - - for(auto edge : edges) - { - const Coord& p1 = pos[edge[0]]; - const Coord& p2 = pos[edge[1]]; - - vparams->drawTool()->drawCylinder(p1,p2,_radius,col); - } -} - - -void OglCylinderModel::setColor(float r, float g, float b, float a) -{ - this->r = r; - this->g = g; - this->b = b; - this->a = a; -} - -static int hexval(char c) -{ - if (c>='0' && c<='9') return c-'0'; - else if (c>='a' && c<='f') return (c-'a')+10; - else if (c>='A' && c<='F') return (c-'A')+10; - else return 0; -} - -void OglCylinderModel::setColor(std::string color) -{ - if (color.empty()) return; - float r = 1.0f; - float g = 1.0f; - float b = 1.0f; - float a = 1.0f; - if (color[0]>='0' && color[0]<='9') - { - sscanf(color.c_str(),"%f %f %f %f", &r, &g, &b, &a); - } - else if (color[0]=='#' && color.length()>=7) - { - r = (hexval(color[1])*16+hexval(color[2]))/255.0f; - g = (hexval(color[3])*16+hexval(color[4]))/255.0f; - b = (hexval(color[5])*16+hexval(color[6]))/255.0f; - if (color.length()>=9) - a = (hexval(color[7])*16+hexval(color[8]))/255.0f; - } - else if (color[0]=='#' && color.length()>=4) - { - r = (hexval(color[1])*17)/255.0f; - g = (hexval(color[2])*17)/255.0f; - b = (hexval(color[3])*17)/255.0f; - if (color.length()>=5) - a = (hexval(color[4])*17)/255.0f; - } - else if (color == "white") { r = 1.0f; g = 1.0f; b = 1.0f; } - else if (color == "black") { r = 0.0f; g = 0.0f; b = 0.0f; } - else if (color == "red") { r = 1.0f; g = 0.0f; b = 0.0f; } - else if (color == "green") { r = 0.0f; g = 1.0f; b = 0.0f; } - else if (color == "blue") { r = 0.0f; g = 0.0f; b = 1.0f; } - else if (color == "cyan") { r = 0.0f; g = 1.0f; b = 1.0f; } - else if (color == "magenta") { r = 1.0f; g = 0.0f; b = 1.0f; } - else if (color == "yellow") { r = 1.0f; g = 1.0f; b = 0.0f; } - else if (color == "gray") { r = 0.5f; g = 0.5f; b = 0.5f; } - else - { - msg_error() << "Unknown color " << color; - return; - } - setColor(r,g,b,a); -} - -void OglCylinderModel::exportOBJ(std::string name, std::ostream* out, std::ostream* /*mtl*/, Index& vindex, Index& /*nindex*/, Index& /*tindex*/, int& /*count*/) -{ - const VecCoord& x = this->read( core::vec_id::read_access::position )->getValue(); - const SeqEdges& edges = d_edges.getValue(); - - const int nbv = int(x.size()); - - *out << "g "< +#include -#include -#include -#include -#include -#include -#include +SOFA_HEADER_DEPRECATED("v24.12", "v25.06", "sofa/component/visual/CylinderVisualModel.h") -namespace sofa::core::topology -{ - class BaseMeshTopology; -} // namespace sofa::core::topology - -namespace sofa::core::behavior -{ - class BaseMechanicalState; -} // namespace sofa::core::behavior +#include namespace sofa::gl::component::rendering3d { - -// I have no idea what is Ogl in this component ?... -class SOFA_GL_COMPONENT_RENDERING3D_API OglCylinderModel : public core::visual::VisualModel, public sofa::core::visual::VisualState -{ -public: - using Vec3State = sofa::core::visual::VisualState; - SOFA_CLASS2(OglCylinderModel,core::visual::VisualModel, Vec3State); - - using Index = sofa::Index; -protected: - OglCylinderModel(); - ~OglCylinderModel() override; -public: - void init() override; - - void doDrawVisual(const core::visual::VisualParams* vparams) override; - - void exportOBJ(std::string /*name*/, std::ostream* /*out*/, std::ostream* /*mtl*/, Index& /*vindex*/, Index& /*nindex*/, Index& /*tindex*/, int& /*count*/) override; - -private: - void setColor(float r, float g, float b, float a); - void setColor(std::string color); - -private: - Data radius; ///< Radius of the cylinder. - // Data alpha; - Data color; ///< Color of the cylinders. - - typedef sofa::type::vector SeqEdges; - Data d_edges; ///< List of edge indices - - - float r,g,b,a; - // sofa::core::topology::PointData > pointData; - - typedef Vec3State::Coord Coord; - typedef Vec3State::VecCoord VecCoord; - typedef Vec3State::Real Real; - -public: - bool insertInNode( core::objectmodel::BaseNode* node ) override { Inherit1::insertInNode(node); Inherit2::insertInNode(node); return true; } - bool removeInNode( core::objectmodel::BaseNode* node ) override { Inherit1::removeInNode(node); Inherit2::removeInNode(node); return true; } -}; - -} // namespace sofa::gl::component::rendering3d +using OglCylinderModel = sofa::component::visual::CylinderVisualModel; +} diff --git a/Sofa/GL/Component/Rendering3D/src/sofa/gl/component/rendering3d/init.cpp b/Sofa/GL/Component/Rendering3D/src/sofa/gl/component/rendering3d/init.cpp index a4096afa836..61a827c7dea 100644 --- a/Sofa/GL/Component/Rendering3D/src/sofa/gl/component/rendering3d/init.cpp +++ b/Sofa/GL/Component/Rendering3D/src/sofa/gl/component/rendering3d/init.cpp @@ -29,7 +29,6 @@ namespace sofa::gl::component::rendering3d extern void registerClipPlane(sofa::core::ObjectFactory* factory); extern void registerDataDisplay(sofa::core::ObjectFactory* factory); extern void registerMergeVisualModels(sofa::core::ObjectFactory* factory); -extern void registerOglCylinderModel(sofa::core::ObjectFactory* factory); extern void registerOglModel(sofa::core::ObjectFactory* factory); extern void registerOglSceneFrame(sofa::core::ObjectFactory* factory); extern void registerPointSplatModel(sofa::core::ObjectFactory* factory); @@ -62,7 +61,6 @@ void registerObjects(sofa::core::ObjectFactory* factory) registerClipPlane(factory); registerDataDisplay(factory); registerMergeVisualModels(factory); - registerOglCylinderModel(factory); registerOglModel(factory); registerOglSceneFrame(factory); registerPointSplatModel(factory); diff --git a/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp b/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp index fa6c8375885..fcfb5e13734 100644 --- a/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp @@ -622,7 +622,9 @@ const std::map > movedComponents = { { "SparseLUSolver", Moved("v23.12", "Sofa.Component.LinearSolver.Direct", "CSparseSolvers") }, // Moved to Sofa.Component.MechanicalLoad - { "Gravity", Moved("v24.12", "SofaGraphComponent", "Sofa.Component.Mechanicalload") } + { "Gravity", Moved("v24.12", "SofaGraphComponent", "Sofa.Component.Mechanicalload") }, + + { "OglCylinderModel", Moved("v24.12", "Sofa.GL.Component.Rendering3D", "Sofa.Component.Visual")} }; const std::map > uncreatableComponents = { @@ -760,8 +762,8 @@ const std::map< std::string, Renamed, std::less<> > renamedComponents = { {"UnilateralInteractionConstraint", Renamed("v24.06","v25.06","UnilateralLagrangianConstraint")}, {"StiffSpringForceField", Renamed("v24.06","v25.06","SpringForceField")}, {"ParallelStiffSpringForceField", Renamed("v24.06","v25.06","ParallelSpringForceField")}, - {"ShewchukPCGLinearSolver", Renamed("v24.12","v25.12","PCGLinearSolver")} - + {"ShewchukPCGLinearSolver", Renamed("v24.12","v25.12","PCGLinearSolver")}, + {"OglCylinderModel", Renamed("v24.12", "v25.06", "CylinderVisualModel")} }; diff --git a/examples/Component/Visual/CylinderVisualModel.scn b/examples/Component/Visual/CylinderVisualModel.scn new file mode 100644 index 00000000000..65191fc0e44 --- /dev/null +++ b/examples/Component/Visual/CylinderVisualModel.scn @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/examples/Component/Visual/OglCylinderModel.scn b/examples/Component/Visual/OglCylinderModel.scn deleted file mode 100644 index 0db4bc72a6c..00000000000 --- a/examples/Component/Visual/OglCylinderModel.scn +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file