Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[All] Fix visualisation #5152

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,30 @@ VisualStyle::VisualStyle()
displayFlags.setOriginalData(&d_displayFlags);
}

void VisualStyle::fwdDraw(VisualParams* vparams)
void VisualStyle::updateVisualFlags(VisualParams* vparams)
{
backupFlags = vparams->displayFlags();
vparams->displayFlags() = sofa::core::visual::merge_displayFlags(backupFlags, d_displayFlags.getValue());
}

void VisualStyle::bwdDraw(VisualParams* vparams)
void VisualStyle::applyBackupFlags(VisualParams* vparams)
{
vparams->displayFlags() = backupFlags;
}


bool VisualStyle::insertInNode( sofa::core::objectmodel::BaseNode* node )
{
node->addVisualStyle(this);
return true;
}

bool VisualStyle::removeInNode( sofa::core::objectmodel::BaseNode* node )
{
node->removeVisualStyle(this);
return true;
}

helper::WriteAccessor<sofa::core::visual::DisplayFlags> addVisualStyle( simulation::Node::SPtr node )
{
const VisualStyle::SPtr visualStyle = New<sofa::component::visual::VisualStyle>();
Expand Down
13 changes: 9 additions & 4 deletions Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
******************************************************************************/
#pragma once
#include <sofa/component/visual/config.h>
#include <sofa/simulation/Node.h>

#include <sofa/core/visual/VisualModel.h>
#include <sofa/core/visual/BaseVisualStyle.h>
#include <sofa/core/visual/Data[DisplayFlags].h>
#include <sofa/simulation/fwd.h>

Expand Down Expand Up @@ -56,18 +58,21 @@ namespace sofa::component::visual
* showNormals hideNormals
* showWireframe hideWireframe
*/
class SOFA_COMPONENT_VISUAL_API VisualStyle : public sofa::core::visual::VisualModel
class SOFA_COMPONENT_VISUAL_API VisualStyle : public sofa::core::visual::BaseVisualStyle
{
public:
SOFA_CLASS(VisualStyle,sofa::core::visual::VisualModel);
SOFA_CLASS(VisualStyle,sofa::core::visual::BaseVisualStyle);

typedef sofa::core::visual::VisualParams VisualParams;
typedef sofa::core::visual::DisplayFlags DisplayFlags;
protected:
VisualStyle();
public:
void fwdDraw(VisualParams* ) override;
void bwdDraw(VisualParams* ) override;
void updateVisualFlags(VisualParams* ) override;
void applyBackupFlags(VisualParams* ) override;

bool insertInNode( sofa::core::objectmodel::BaseNode* node );
bool removeInNode( sofa::core::objectmodel::BaseNode* node );

SOFA_ATTRIBUTE_DEPRECATED__RENAME_DATA_IN_VISUAL()
sofa::core::objectmodel::RenamedData<DisplayFlags> displayFlags;
Expand Down
1 change: 1 addition & 0 deletions Sofa/framework/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ set(HEADER_FILES
${SRC_ROOT}/topology/TopologySubsetData.cpp
${SRC_ROOT}/topology/TopologySubsetIndices.h
${SRC_ROOT}/trait/DataTypes.h
${SRC_ROOT}/visual/BaseVisualStyle.h
${SRC_ROOT}/visual/Data[DisplayFlags].h
${SRC_ROOT}/visual/DisplayFlags.h
${SRC_ROOT}/visual/FlagTreeItem.h
Expand Down
2 changes: 2 additions & 0 deletions Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <sofa/core/objectmodel/BaseObject.h>
#include <sofa/core/objectmodel/TypeOfInsertion.h>
#include <sofa/core/visual/BaseVisualStyle.h>

namespace sofa::core::objectmodel
{
Expand Down Expand Up @@ -172,6 +173,7 @@ class SOFA_CORE_API BaseNode : public virtual Base
BASENODE_ADD_SPECIAL_COMPONENT( core::objectmodel::ConfigurationSetting, ConfigurationSetting, configurationSetting )
BASENODE_ADD_SPECIAL_COMPONENT( core::visual::Shader, Shader, shaders )
BASENODE_ADD_SPECIAL_COMPONENT( core::visual::VisualModel, VisualModel, visualModel )
BASENODE_ADD_SPECIAL_COMPONENT( core::visual::BaseVisualStyle, VisualStyle, visualStyle )
BASENODE_ADD_SPECIAL_COMPONENT( core::visual::VisualManager, VisualManager, visualManager )
BASENODE_ADD_SPECIAL_COMPONENT( core::CollisionModel, CollisionModel, collisionModel )
BASENODE_ADD_SPECIAL_COMPONENT( core::collision::Pipeline, CollisionPipeline, collisionPipeline )
Expand Down
51 changes: 51 additions & 0 deletions Sofa/framework/Core/src/sofa/core/visual/BaseVisualStyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/******************************************************************************
* 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/core/fwd.h>
#include <sofa/core/visual/VisualModel.h>
#include <string>
#include <iostream>


namespace sofa::core::visual
{

class SOFA_CORE_API BaseVisualStyle : public sofa::core::objectmodel::BaseObject
{
public:
SOFA_CLASS(BaseVisualStyle,sofa::core::objectmodel::BaseObject);

typedef sofa::core::visual::VisualParams VisualParams;
typedef sofa::core::visual::DisplayFlags DisplayFlags;

protected:
BaseVisualStyle() { }
~BaseVisualStyle() override { }

public:
virtual void updateVisualFlags(VisualParams* ) { };
virtual void applyBackupFlags(VisualParams* ) { };

};

} // namespace sofa::simulation::graph

7 changes: 7 additions & 0 deletions Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Node::Node(const std::string& name)
, configurationSetting(initLink("configurationSetting", "The ConfigurationSetting(s) attached to this node"))
, shaders(initLink("shaders", "The shaders attached to this node"))
, visualModel(initLink("visualModel", "The VisualModel(s) attached to this node"))
, visualStyle(initLink("visualStyle", "The VisualStyle(s) attached to this node"))
, visualManager(initLink("visualManager", "The VisualManager(s) attached to this node"))
, collisionModel(initLink("collisionModel", "The CollisionModel(s) attached to this node"))
, unsorted(initLink("unsorted", "The remaining objects attached to this node"))
Expand Down Expand Up @@ -930,6 +931,7 @@ void Node::printComponents()
using core::objectmodel::ContextObject;
using core::collision::Pipeline;
using core::BaseState;
using core::visual::BaseVisualStyle;

std::stringstream sstream;

Expand Down Expand Up @@ -987,6 +989,9 @@ void Node::printComponents()
sstream << "\n" << "VisualModel: ";
for (NodeSequence<VisualModel>::iterator i = visualModel.begin(), iend = visualModel.end(); i != iend; ++i)
sstream << (*i)->getName() << " ";
sstream << "\n" << "BaseVisualStyle: ";
for (NodeSingle<BaseVisualStyle>::iterator i = visualStyle.begin(), iend = visualStyle.end(); i != iend; ++i)
sstream << (*i)->getName() << " ";
sstream << "\n" << "CollisionModel: ";
for (NodeSequence<CollisionModel>::iterator i = collisionModel.begin(), iend = collisionModel.end(); i != iend; ++i)
sstream << (*i)->getName() << " ";
Expand Down Expand Up @@ -1045,6 +1050,7 @@ NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::objectmodel::ContextObject, ContextOb
NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::objectmodel::ConfigurationSetting, ConfigurationSetting, configurationSetting )
NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::visual::Shader, Shader, shaders )
NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::visual::VisualModel, VisualModel, visualModel )
NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::visual::BaseVisualStyle, VisualStyle, visualStyle )
NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::visual::VisualManager, VisualManager, visualManager )
NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::CollisionModel, CollisionModel, collisionModel )
NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::collision::Pipeline, CollisionPipeline, collisionPipeline )
Expand All @@ -1071,6 +1077,7 @@ template class NodeSequence<sofa::core::objectmodel::BaseObject>;

template class NodeSingle<sofa::core::behavior::BaseAnimationLoop>;
template class NodeSingle<sofa::core::visual::VisualLoop>;
template class NodeSingle<sofa::core::visual::BaseVisualStyle>;
template class NodeSingle<sofa::core::topology::Topology>;
template class NodeSingle<sofa::core::topology::BaseMeshTopology>;
template class NodeSingle<sofa::core::BaseState>;
Expand Down
3 changes: 3 additions & 0 deletions Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ extern template class NodeSequence<sofa::core::objectmodel::BaseObject>;

extern template class NodeSingle<sofa::core::behavior::BaseAnimationLoop>;
extern template class NodeSingle<sofa::core::visual::VisualLoop>;
extern template class NodeSingle<sofa::core::visual::BaseVisualStyle>;
extern template class NodeSingle<sofa::core::topology::Topology>;
extern template class NodeSingle<sofa::core::topology::BaseMeshTopology>;
extern template class NodeSingle<sofa::core::BaseState>;
Expand Down Expand Up @@ -255,6 +256,7 @@ class SOFA_SIMULATION_CORE_API Node : public sofa::core::objectmodel::BaseNode,

NodeSingle<sofa::core::behavior::BaseAnimationLoop> animationManager;
NodeSingle<sofa::core::visual::VisualLoop> visualLoop;
NodeSingle<sofa::core::visual::BaseVisualStyle> visualStyle;
NodeSingle<sofa::core::topology::Topology> topology;
NodeSingle<sofa::core::topology::BaseMeshTopology> meshTopology;
NodeSingle<sofa::core::BaseState> state;
Expand Down Expand Up @@ -599,6 +601,7 @@ class SOFA_SIMULATION_CORE_API Node : public sofa::core::objectmodel::BaseNode,
NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::objectmodel::ConfigurationSetting, ConfigurationSetting, configurationSetting )
NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::visual::Shader, Shader, shaders )
NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::visual::VisualModel, VisualModel, visualModel )
NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::visual::BaseVisualStyle, VisualStyle, visualStyle )
NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::visual::VisualManager, VisualManager, visualManager )
NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::CollisionModel, CollisionModel, collisionModel )
NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::collision::Pipeline, CollisionPipeline, collisionPipeline )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ void animate(Node* root, SReal dt)
void updateVisual(Node* root)
{
SCOPED_TIMER("Simulation::updateVisual");



sofa::core::visual::VisualParams* vparams = sofa::core::visual::visualparams::defaultInstance();

if (sofa::core::visual::VisualLoop* vloop = root->getVisualLoop())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,32 @@ Visitor::Result VisualVisitor::processNodeTopDown(simulation::Node* node)
}


Visitor::Result VisualDrawVisitor::processNodeTopDown(simulation::Node* node)
void VisualVisitor::fwdProcessVisualStyle(simulation::Node* node, core::visual::BaseVisualStyle* vm)
{
vm-> updateVisualFlags(vparams);
}


void VisualVisitor::bwdProcessVisualStyle(simulation::Node* node, core::visual::BaseVisualStyle* vm)
{
vm-> applyBackupFlags(vparams);
}


Visitor::Result VisualDrawVisitor::processNodeTopDown(simulation::Node* node)
{
// NB: hasShader is only used when there are visual models and getShader does a graph search when there is no shader,
// which will most probably be the case when there are no visual models, so we skip the search unless we have visual models.
hasShader = !node->visualModel.empty() && (node->getShader()!=nullptr);

if(node->visualStyle.get())
{
fwdProcessVisualStyle(node,node->visualStyle.get());
}


for_each(this, node, node->visualModel, &VisualDrawVisitor::fwdVisualModel);

this->VisualVisitor::processNodeTopDown(node);

return RESULT_CONTINUE;
Expand All @@ -65,6 +83,12 @@ void VisualDrawVisitor::processNodeBottomUp(simulation::Node* node)
if (!vparams->displayFlags().getShowVisualModels())
return;

if(node->visualStyle.get())
{
bwdProcessVisualStyle(node,node->visualStyle.get());
}


for_each(this, node, node->visualModel, &VisualDrawVisitor::bwdVisualModel);
}

Expand Down Expand Up @@ -149,11 +173,27 @@ void VisualDrawVisitor::processVisualModel(simulation::Node* node, core::visual:

Visitor::Result VisualUpdateVisitor::processNodeTopDown(simulation::Node* node)
{
for_each(this, node, node->visualModel, &VisualUpdateVisitor::processVisualModel);
//Necessary check for first draw
if(node->visualStyle.get())
{
fwdProcessVisualStyle(node,node->visualStyle.get());
}

for_each(this, node, node->visualModel, &VisualUpdateVisitor::processVisualModel);

return RESULT_CONTINUE;
}

void VisualUpdateVisitor::processNodeBottomUp(simulation::Node* node)
{
//Necessary check for first draw
if(node->visualStyle.get())
{
bwdProcessVisualStyle(node,node->visualStyle.get());
}
}


void VisualUpdateVisitor::processVisualModel(simulation::Node*, core::visual::VisualModel* vm)
{
helper::ScopedAdvancedTimer timer("VisualUpdateVisitor process: " + vm->getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <sofa/simulation/Visitor.h>
#include <sofa/core/visual/VisualModel.h>
#include <sofa/core/visual/BaseVisualStyle.h>


namespace sofa::simulation
Expand All @@ -38,6 +39,8 @@ class SOFA_SIMULATION_CORE_API VisualVisitor : public Visitor
{}

virtual void processVisualModel(simulation::Node* node, core::visual::VisualModel* vm) = 0;
virtual void fwdProcessVisualStyle(simulation::Node* node, core::visual::BaseVisualStyle* vm);
virtual void bwdProcessVisualStyle(simulation::Node* node, core::visual::BaseVisualStyle* vm);
virtual void processObject(simulation::Node* /*node*/, core::objectmodel::BaseObject* /*o*/) {}

Result processNodeTopDown(simulation::Node* node) override;
Expand All @@ -54,14 +57,14 @@ class SOFA_SIMULATION_CORE_API VisualVisitor : public Visitor
core::visual::VisualParams* vparams;
};


class SOFA_SIMULATION_CORE_API VisualDrawVisitor : public VisualVisitor
{
public:
bool hasShader;
VisualDrawVisitor(core::visual::VisualParams* params)
: VisualVisitor(params)
{
}
: VisualVisitor(params)
{};
Result processNodeTopDown(simulation::Node* node) override;
void processNodeBottomUp(simulation::Node* node) override;
virtual void fwdVisualModel(simulation::Node* node, core::visual::VisualModel* vm);
Expand All @@ -83,11 +86,10 @@ class SOFA_SIMULATION_CORE_API VisualUpdateVisitor : public VisualVisitor

virtual void processVisualModel(simulation::Node*, core::visual::VisualModel* vm) override;
Result processNodeTopDown(simulation::Node* node) override;
void processNodeBottomUp(simulation::Node* node) override;

const char* getClassName() const override { return "VisualUpdateVisitor"; }

protected:
core::visual::VisualParams* m_vparams;
};

class SOFA_SIMULATION_CORE_API VisualInitVisitor : public VisualVisitor
Expand Down
Loading