Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e2fcf45
[Visual] Introction of VisualMesh
alxbilger Nov 17, 2025
fcd91a9
draw hexahedra
alxbilger Nov 17, 2025
e8f7946
factorize element center
alxbilger Nov 17, 2025
b4e6fed
factorize definition of points and colors
alxbilger Nov 17, 2025
f547805
draw triangles
alxbilger Nov 17, 2025
57ddb35
modify color vector only if elements vector change
alxbilger Nov 20, 2025
2530457
make drawSelection const
alxbilger Nov 20, 2025
532fee6
move drawing methods into functions that can be called elsewhere
alxbilger Nov 20, 2025
445de0a
draw mesh from the viewer
alxbilger Nov 20, 2025
8ddd723
missing position
alxbilger Nov 20, 2025
ca45b15
implement CRTP
alxbilger Nov 21, 2025
78f9e40
fix
alxbilger Nov 21, 2025
32b5cfa
fix
alxbilger Nov 21, 2025
f541a31
use DrawElementMesh in TetrahedronHyperelasticityFEMDrawing
alxbilger Nov 21, 2025
bbb7f7a
use DrawElementMesh in volume force fields
alxbilger Nov 21, 2025
48daffb
use DrawElementMesh in surface force fields
alxbilger Nov 21, 2025
91db5dc
missing typename
alxbilger Dec 10, 2025
413df76
TEMPLATE EVERYWHERE!!!
alxbilger Dec 11, 2025
bd851e5
missing typename keyword
alxbilger Dec 11, 2025
3708447
support drawing of some elements given the indices
alxbilger Dec 11, 2025
ceff6fb
add documentation
alxbilger Dec 11, 2025
d468b84
fix template deduction
alxbilger Dec 11, 2025
cf6d5ef
apply renaming
alxbilger Dec 11, 2025
41d53e1
unused variable
alxbilger Dec 12, 2025
c8a6fcf
fix link issues
alxbilger Dec 12, 2025
1e38be2
replace std::ranges::iota_view by a custom equivalent
alxbilger Dec 12, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
#include <sofa/component/solidmechanics/fem/elastic/config.h>
#include <sofa/core/behavior/ForceField.h>
#include <sofa/core/topology/TopologyData.h>
#include <sofa/type/fixed_array.h>
#include <sofa/type/vector.h>
#include <sofa/type/Vec.h>
#include <sofa/core/visual/DrawMesh.h>
#include <sofa/type/Mat.h>
#include <sofa/type/Vec.h>
#include <sofa/type/fixed_array.h>
#include <sofa/type/trait/Rebind.h>
#include <sofa/type/vector.h>

namespace sofa::component::solidmechanics::fem::elastic
{
Expand Down Expand Up @@ -177,6 +178,8 @@ protected :

typedef FastTetrahedralCorotationalForceFieldData<DataTypes> ExtraData;
ExtraData m_data;

core::visual::DrawElementMesh<sofa::geometry::Tetrahedron> m_drawMesh;
};

#if !defined(SOFA_COMPONENT_INTERACTIONFORCEFIELD_FASTTETRAHEDRALCOROTATIONALFORCEFIELD_CPP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,50 +672,7 @@ void FastTetrahedralCorotationalForceField<DataTypes>::draw(const core::visual::
if (vparams->displayFlags().getShowWireFrame())
vparams->drawTool()->setPolygonMode(0, true);


std::vector< type::Vec3 > points[4];
for (size_t i = 0; i<this->l_topology->getNbTetrahedra(); ++i)
{
const core::topology::BaseMeshTopology::Tetrahedron t = this->l_topology->getTetrahedron(i);

const auto& [a, b, c, d] = t.array();
Coord center = (x[a] + x[b] + x[c] + x[d])*0.125;
Coord pa = (x[a] + center)*(Real)0.666667;
Coord pb = (x[b] + center)*(Real)0.666667;
Coord pc = (x[c] + center)*(Real)0.666667;
Coord pd = (x[d] + center)*(Real)0.666667;

// glColor4f(0,0,1,1);
points[0].push_back(pa);
points[0].push_back(pb);
points[0].push_back(pc);

// glColor4f(0,0.5,1,1);
points[1].push_back(pb);
points[1].push_back(pc);
points[1].push_back(pd);

// glColor4f(0,1,1,1);
points[2].push_back(pc);
points[2].push_back(pd);
points[2].push_back(pa);

// glColor4f(0.5,1,1,1);
points[3].push_back(pd);
points[3].push_back(pa);
points[3].push_back(pb);
}

vparams->drawTool()->drawTriangles(points[0], d_drawColor1.getValue());
vparams->drawTool()->drawTriangles(points[1], d_drawColor2.getValue());
vparams->drawTool()->drawTriangles(points[2], d_drawColor3.getValue());
vparams->drawTool()->drawTriangles(points[3], d_drawColor4.getValue());

if (vparams->displayFlags().getShowWireFrame())
vparams->drawTool()->setPolygonMode(0, false);



m_drawMesh.drawAllElements(vparams->drawTool(), x, this->l_topology.get());
}

} // namespace sofa::component::solidmechanics::fem::elastic
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <sofa/component/solidmechanics/fem/elastic/config.h>

#include <sofa/component/solidmechanics/fem/elastic/BaseLinearElasticityFEMForceField.h>

#include <sofa/type/vector.h>
#include <sofa/type/Vec.h>
#include <sofa/type/Mat.h>

#include <sofa/component/solidmechanics/fem/elastic/config.h>
#include <sofa/core/topology/TopologyData.h>
#include <sofa/core/visual/DrawMesh.h>
#include <sofa/type/Mat.h>
#include <sofa/type/Vec.h>
#include <sofa/type/vector.h>

namespace sofa::component::solidmechanics::fem::elastic
{
Expand Down Expand Up @@ -198,6 +196,8 @@ class HexahedralFEMForceField : virtual public BaseLinearElasticityFEMForceField
protected:

type::Mat<8,3,int> _coef; ///< coef of each vertices to compute the strain stress matrix

core::visual::DrawElementMesh<sofa::geometry::Hexahedron> m_drawMesh;
};

#if !defined(SOFA_COMPONENT_FORCEFIELD_HEXAHEDRALFEMFORCEFIELD_CPP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,6 @@ void HexahedralFEMForceField<DataTypes>::draw(const core::visual::VisualParams*
if (!this->mstate) return;

const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle();
vparams->drawTool()->disableLighting();
std::vector<sofa::type::RGBAColor> colorVector;
std::vector<sofa::type::Vec3> vertices;

const VecCoord& x = this->mstate->read(core::vec_id::read_access::position)->getValue();

Expand All @@ -670,92 +667,7 @@ void HexahedralFEMForceField<DataTypes>::draw(const core::visual::VisualParams*
vparams->drawTool()->setPolygonMode(0, true);
}

for(size_t i = 0 ; i<this->l_topology->getNbHexahedra(); ++i)
{
const core::topology::BaseMeshTopology::Hexahedron &t=this->l_topology->getHexahedron(i);

Index a = t[0];
Index b = t[1];
Index d = t[2];
Index c = t[3];
Index e = t[4];
Index f = t[5];
Index h = t[6];
Index g = t[7];

Coord center = (x[a]+x[b]+x[c]+x[d]+x[e]+x[g]+x[f]+x[h])*0.125;
Real percentage = (Real) 0.15;
Coord p0 = x[a]-(x[a]-center)*percentage;
Coord p1 = x[b]-(x[b]-center)*percentage;
Coord p2 = x[c]-(x[c]-center)*percentage;
Coord p3 = x[d]-(x[d]-center)*percentage;
Coord p4 = x[e]-(x[e]-center)*percentage;
Coord p5 = x[f]-(x[f]-center)*percentage;
Coord p6 = x[g]-(x[g]-center)*percentage;
Coord p7 = x[h]-(x[h]-center)*percentage;

constexpr sofa::type::RGBAColor color1(0.7f, 0.7f, 0.1f, 1.0f);
colorVector.push_back(color1);
colorVector.push_back(color1);
colorVector.push_back(color1);
colorVector.push_back(color1);
vertices.push_back(DataTypes::getCPos(p5));
vertices.push_back(DataTypes::getCPos(p1));
vertices.push_back(DataTypes::getCPos(p3));
vertices.push_back(DataTypes::getCPos(p7));

constexpr sofa::type::RGBAColor color2(0.7f, 0.0f, 0.0f, 1.0f);
colorVector.push_back(color2);
colorVector.push_back(color2);
colorVector.push_back(color2);
colorVector.push_back(color2);
vertices.push_back(DataTypes::getCPos(p1));
vertices.push_back(DataTypes::getCPos(p0));
vertices.push_back(DataTypes::getCPos(p2));
vertices.push_back(DataTypes::getCPos(p3));

constexpr sofa::type::RGBAColor color3(0.0f, 0.7f, 0.0f, 1.0f);
colorVector.push_back(color3);
colorVector.push_back(color3);
colorVector.push_back(color3);
colorVector.push_back(color3);
vertices.push_back(DataTypes::getCPos(p0));
vertices.push_back(DataTypes::getCPos(p4));
vertices.push_back(DataTypes::getCPos(p6));
vertices.push_back(DataTypes::getCPos(p2));

constexpr sofa::type::RGBAColor color4(0.0f, 0.0f, 0.7f, 1.0f);
colorVector.push_back(color4);
colorVector.push_back(color4);
colorVector.push_back(color4);
colorVector.push_back(color4);
vertices.push_back(DataTypes::getCPos(p4));
vertices.push_back(DataTypes::getCPos(p5));
vertices.push_back(DataTypes::getCPos(p7));
vertices.push_back(DataTypes::getCPos(p6));

constexpr sofa::type::RGBAColor color5(0.1f, 0.7f, 0.7f, 1.0f);
colorVector.push_back(color5);
colorVector.push_back(color5);
colorVector.push_back(color5);
colorVector.push_back(color5);
vertices.push_back(DataTypes::getCPos(p7));
vertices.push_back(DataTypes::getCPos(p3));
vertices.push_back(DataTypes::getCPos(p2));
vertices.push_back(DataTypes::getCPos(p6));

constexpr sofa::type::RGBAColor color6(0.1f, 0.7f, 0.7f, 1.0f);
colorVector.push_back(color6);
colorVector.push_back(color6);
colorVector.push_back(color6);
colorVector.push_back(color6);
vertices.push_back(DataTypes::getCPos(p1));
vertices.push_back(DataTypes::getCPos(p5));
vertices.push_back(DataTypes::getCPos(p4));
vertices.push_back(DataTypes::getCPos(p0));
}
vparams->drawTool()->drawQuads(vertices,colorVector);

m_drawMesh.drawAllElements(vparams->drawTool(), x, this->l_topology.get());
}

} // namespace sofa::component::solidmechanics::fem::elastic
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <sofa/component/solidmechanics/fem/elastic/fwd.h>

#include <sofa/component/solidmechanics/fem/elastic/BaseLinearElasticityFEMForceField.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/component/solidmechanics/fem/elastic/fwd.h>
#include <sofa/component/topology/container/grid/SparseGridTopology.h>
#include <sofa/type/vector.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/type/Mat.h>
#include <sofa/core/behavior/BaseRotationFinder.h>
#include <sofa/helper/decompose.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/core/visual/DrawMesh.h>
#include <sofa/defaulttype/VecTypes.h>
#include <sofa/helper/OptionsGroup.h>
#include <sofa/helper/decompose.h>
#include <sofa/type/Mat.h>
#include <sofa/type/vector.h>

namespace sofa::component::solidmechanics::fem::elastic
{
Expand Down Expand Up @@ -222,6 +222,8 @@ class HexahedronFEMForceField :
virtual void accumulateForceSmall( WDataRefVecDeriv &f, RDataRefVecCoord &p, sofa::Index i, const Element&elem );

bool _alreadyInit;

core::visual::DrawElementMesh<sofa::geometry::Hexahedron> m_drawMesh;
};

#if !defined(SOFA_COMPONENT_FORCEFIELD_HEXAHEDRONFEMFORCEFIELD_CPP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1212,8 +1212,6 @@ void HexahedronFEMForceField<DataTypes>::draw(const core::visual::VisualParams*

const VecCoord& x = this->mstate->read(core::vec_id::read_access::position)->getValue();

vparams->drawTool()->setLightingEnabled(false);

if(_sparseGrid )
{
vparams->drawTool()->enableBlending();
Expand All @@ -1222,104 +1220,8 @@ void HexahedronFEMForceField<DataTypes>::draw(const core::visual::VisualParams*
if (vparams->displayFlags().getShowWireFrame())
vparams->drawTool()->setPolygonMode(0,true);

const Real percentage = d_drawPercentageOffset.getValue();
const Real oneMinusPercentage = static_cast<Real>(1) - percentage;

const auto* indexedElements = this->getIndexedElements();

sofa::type::fixed_array<std::vector<sofa::type::Vec3>, 6 > quads; //one list of quads per hexahedron face
sofa::type::fixed_array<std::vector<RGBAColor>, 6> colors; //one list of quads per hexahedron face

for (auto& q : quads)
{
q.reserve(indexedElements->size() * 4);
}
for (auto& c : colors)
{
c.reserve(indexedElements->size() * 4);
}

sofa::Index i {};
for (const auto& element : *indexedElements)
{
const Coord& a = x[element[0]];
const Coord& b = x[element[1]];
const Coord& c = x[element[2]];
const Coord& d = x[element[3]];
const Coord& e = x[element[4]];
const Coord& f = x[element[5]];
const Coord& g = x[element[6]];
const Coord& h = x[element[7]];

const Coord center = (a + b + c + d + e + f + g + h ) * static_cast<Real>(0.125);
const Coord centerPercent = center * percentage;

const Coord pa = a * oneMinusPercentage + centerPercent;
const Coord pb = b * oneMinusPercentage + centerPercent;
const Coord pc = c * oneMinusPercentage + centerPercent;
const Coord pd = d * oneMinusPercentage + centerPercent;
const Coord pe = e * oneMinusPercentage + centerPercent;
const Coord pf = f * oneMinusPercentage + centerPercent;
const Coord pg = g * oneMinusPercentage + centerPercent;
const Coord ph = h * oneMinusPercentage + centerPercent;

quads[0].emplace_back(pa);
quads[0].emplace_back(pb);
quads[0].emplace_back(pc);
quads[0].emplace_back(pd);

quads[1].emplace_back(pe);
quads[1].emplace_back(pf);
quads[1].emplace_back(pg);
quads[1].emplace_back(ph);

quads[2].emplace_back(pc);
quads[2].emplace_back(pd);
quads[2].emplace_back(ph);
quads[2].emplace_back(pg);

quads[3].emplace_back(pa);
quads[3].emplace_back(pb);
quads[3].emplace_back(pf);
quads[3].emplace_back(pe);

quads[4].emplace_back(pa);
quads[4].emplace_back(pd);
quads[4].emplace_back(ph);
quads[4].emplace_back(pe);

quads[5].emplace_back(pb);
quads[5].emplace_back(pc);
quads[5].emplace_back(pg);
quads[5].emplace_back(pf);

const float stiffnessCoef = _sparseGrid ? _sparseGrid->getStiffnessCoef(i) : 1.0f;
sofa::type::fixed_array<sofa::type::RGBAColor, 6> quadColors {
sofa::type::RGBAColor(0.7f,0.7f,0.1f,stiffnessCoef),
sofa::type::RGBAColor(0.7f,0.0f,0.0f,stiffnessCoef),
sofa::type::RGBAColor(0.0f,0.7f,0.0f,stiffnessCoef),
sofa::type::RGBAColor(0.0f,0.0f,0.7f,stiffnessCoef),
sofa::type::RGBAColor(0.1f,0.7f,0.7f,stiffnessCoef),
sofa::type::RGBAColor(0.7f,0.1f,0.7f,stiffnessCoef)
};

for (unsigned int j = 0; j < 6; ++j)
{
auto& faceColors = colors[j];
const auto& color = quadColors[j];
for (unsigned int k = 0; k < 4; ++k)
{
faceColors.emplace_back(color);
}
}

++i;
}

for (unsigned int j = 0; j < 6; ++j)
{
vparams->drawTool()->drawQuads(quads[j], colors[j]);
}
m_drawMesh.elementSpace = d_drawPercentageOffset.getValue();
m_drawMesh.drawAllElements(vparams->drawTool(), x, this->l_topology.get());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
#pragma once
#include <sofa/component/solidmechanics/fem/elastic/BaseLinearElasticityFEMForceField.h>
#include <sofa/component/solidmechanics/fem/elastic/config.h>

#include <sofa/core/behavior/ForceField.h>
#include <sofa/core/topology/TopologyData.h>
#include <sofa/type/vector.h>
#include <sofa/type/Vec.h>
#include <sofa/type/Mat.h>
#include <sofa/helper/map.h>
#include <sofa/core/visual/DrawMesh.h>
#include <sofa/helper/ColorMap.h>
#include <sofa/helper/map.h>
#include <sofa/type/Mat.h>
#include <sofa/type/Vec.h>
#include <sofa/type/vector.h>

// corotational tetrahedron from
// @InProceedings{NPF05,
Expand Down Expand Up @@ -250,6 +250,8 @@ class TetrahedralCorotationalFEMForceField : public BaseLinearElasticityFEMForce

void printStiffnessMatrix(int idTetra);

core::visual::DrawElementMesh<sofa::geometry::Tetrahedron> m_drawMesh;

};

#if !defined(SOFA_COMPONENT_FORCEFIELD_TETRAHEDRALCOROTATIONALFEMFORCEFIELD_CPP)
Expand Down
Loading