Skip to content

Commit

Permalink
[Visual] Move, rename and clean OglCylinderModel (sofa-framework#5124)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alxbilger authored Dec 17, 2024
1 parent 02b3366 commit 99df0a2
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 240 deletions.
2 changes: 2 additions & 0 deletions Sofa/Component/Visual/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#include <sofa/component/visual/CylinderVisualModel.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/ObjectFactory.h>

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 "<<name<<"\n";

for( int i=0 ; i<nbv; i++ )
*out << "v "<< std::fixed << x[i][0]<<' '<< std::fixed <<x[i][1]<<' '<< std::fixed <<x[i][2]<<'\n';

for( size_t i = 0 ; i < edges.size() ; i++ )
*out << "f " << edges[i][0]+vindex+1 << " " << edges[i][1]+vindex+1 << '\n';

*out << std::endl;

vindex += nbv;
}

} // namespace sofa::component::visual
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/******************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <sofa/component/visual/config.h>

#include <sofa/core/visual/VisualModel.h>
#include <sofa/core/visual/VisualState.h>
#include <sofa/type/RGBAColor.h>

namespace sofa::component::visual
{

class SOFA_COMPONENT_VISUAL_API CylinderVisualModel :
public core::visual::VisualModel, public sofa::core::visual::VisualState<defaulttype::Vec3Types>
{
public:
using Vec3State = sofa::core::visual::VisualState<defaulttype::Vec3Types>;
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<float> radius; ///< Radius of the cylinder.
Data<sofa::type::RGBAColor> color; ///< Color of the cylinders.

typedef sofa::type::vector<core::topology::Edge> SeqEdges;
Data<SeqEdges> 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
2 changes: 2 additions & 0 deletions Sofa/Component/Visual/src/sofa/component/visual/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -63,6 +64,7 @@ void registerObjects(sofa::core::ObjectFactory* factory)
{
registerCamera(factory);
registerInteractiveCamera(factory);
registerCylinderVisualModel(factory);
registerLineAxis(factory);
registerRecordedCamera(factory);
registerTrailRenderer(factory);
Expand Down
1 change: 0 additions & 1 deletion Sofa/GL/Component/Rendering3D/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

Loading

0 comments on commit 99df0a2

Please sign in to comment.