Skip to content

Commit 104f87b

Browse files
authored
Merge branch 'master' into visualmesh
2 parents d8da54b + b2c158b commit 104f87b

File tree

11 files changed

+214
-85
lines changed

11 files changed

+214
-85
lines changed

Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleOctreeModel.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,41 @@ namespace sofa::component::collision::geometry
3737
void registerTriangleOctreeModel(sofa::core::ObjectFactory* factory)
3838
{
3939
factory->registerObjects(core::ObjectRegistrationData("Collision model using a triangular mesh mapped to an Octree.")
40-
.add < TriangleOctreeModel >());
40+
.add < TriangleOctreeCollisionModel >()
41+
.addAlias("TriangleOctreeModel")); // SOFA_ATTRIBUTE_DEPRECATED__RENAMED_TRIANGLEOCTREEMODEL()
4142
}
4243

43-
TriangleOctreeModel::TriangleOctreeModel ()
44+
TriangleOctreeCollisionModel::TriangleOctreeCollisionModel ()
45+
: Inherit1()
46+
, helper::TriangleOctreeRoot()
47+
, d_drawOctree(initData(&d_drawOctree, false, "drawOctree", "draw the octree structure"))
4448
{
4549
}
4650

47-
void TriangleOctreeModel::drawCollisionModel (const core::visual::VisualParams* vparams)
51+
void TriangleOctreeCollisionModel::drawCollisionModel (const core::visual::VisualParams* vparams)
4852
{
4953
TriangleCollisionModel<sofa::defaulttype::Vec3Types>::drawCollisionModel(vparams);
5054

51-
if (vparams->displayFlags().getShowWireFrame())
55+
if (d_drawOctree.getValue())
5256
{
53-
vparams->drawTool()->setPolygonMode(0, true);
54-
}
57+
if (vparams->displayFlags().getShowWireFrame())
58+
{
59+
vparams->drawTool()->setPolygonMode(0, true);
60+
}
5561

56-
vparams->drawTool()->enableLighting();
57-
const float* getCol = getColor4f();
58-
const auto color = sofa::type::RGBAColor(getCol[0], getCol[1], getCol[2], getCol[3]);
59-
vparams->drawTool()->setMaterial(color);
62+
vparams->drawTool()->enableLighting();
63+
const float* getCol = getColor4f();
64+
const auto color = sofa::type::RGBAColor(getCol[0], getCol[1], getCol[2], getCol[3]);
65+
vparams->drawTool()->setMaterial(color);
6066

61-
if (octreeRoot) octreeRoot->draw(vparams->drawTool());
67+
if (octreeRoot) octreeRoot->draw(vparams->drawTool());
6268

63-
vparams->drawTool()->disableLighting();
64-
if (vparams->displayFlags().getShowWireFrame()) vparams->drawTool()->setPolygonMode(0, false);
69+
vparams->drawTool()->disableLighting();
70+
if (vparams->displayFlags().getShowWireFrame()) vparams->drawTool()->setPolygonMode(0, false);
71+
}
6572
}
6673

67-
void TriangleOctreeModel::computeBoundingTree(int maxDepth)
74+
void TriangleOctreeCollisionModel::computeBoundingTree(int maxDepth)
6875
{
6976
const type::vector<topology::Triangle>& tri = *m_triangles;
7077
if(octreeRoot)
@@ -124,12 +131,12 @@ void TriangleOctreeModel::computeBoundingTree(int maxDepth)
124131
}
125132
}
126133

127-
void TriangleOctreeModel::computeContinuousBoundingTree(SReal/* dt*/, int maxDepth)
134+
void TriangleOctreeCollisionModel::computeContinuousBoundingTree(SReal/* dt*/, int maxDepth)
128135
{
129136
computeBoundingTree(maxDepth);
130137
}
131138

132-
void TriangleOctreeModel::buildOctree()
139+
void TriangleOctreeCollisionModel::buildOctree()
133140
{
134141
this->octreeTriangles = &this->getTriangles();
135142
this->octreePos = &this->getX();

Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/TriangleOctreeModel.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
namespace sofa::component::collision::geometry
3434
{
3535

36-
class SOFA_COMPONENT_COLLISION_GEOMETRY_API TriangleOctreeModel : public TriangleCollisionModel<sofa::defaulttype::Vec3Types>, public helper::TriangleOctreeRoot
36+
class SOFA_COMPONENT_COLLISION_GEOMETRY_API TriangleOctreeCollisionModel : public TriangleCollisionModel<sofa::defaulttype::Vec3Types>, public helper::TriangleOctreeRoot
3737
{
3838
public:
39-
SOFA_CLASS(TriangleOctreeModel, TriangleCollisionModel<sofa::defaulttype::Vec3Types>);
39+
SOFA_CLASS(TriangleOctreeCollisionModel, TriangleCollisionModel<sofa::defaulttype::Vec3Types>);
4040
protected:
41-
TriangleOctreeModel();
41+
TriangleOctreeCollisionModel();
4242
void drawCollisionModel(const core::visual::VisualParams* vparams) override;
4343
public:
44+
Data<bool> d_drawOctree; ///< draw the octree
4445

4546
/// the normals for each point
4647
type::vector<type::Vec3> pNorms;
@@ -50,4 +51,8 @@ class SOFA_COMPONENT_COLLISION_GEOMETRY_API TriangleOctreeModel : public Triang
5051
void buildOctree ();
5152
};
5253

54+
55+
using TriangleOctreeModel SOFA_ATTRIBUTE_DEPRECATED__RENAMED_TRIANGLEOCTREEMODEL() = TriangleOctreeCollisionModel;
56+
57+
5358
} // namespace sofa::component::collision::geometry

Sofa/Component/Collision/Geometry/src/sofa/component/collision/geometry/config.h.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,13 @@ namespace sofa::component::collision::geometry
3636
constexpr const char* MODULE_NAME = "@PROJECT_NAME@";
3737
constexpr const char* MODULE_VERSION = "@PROJECT_VERSION@";
3838
} // namespace sofa::component::collision::geometry
39+
40+
41+
#ifdef SOFA_BUILD_SOFA_COMPONENT_COLLISION_GEOMETRY
42+
#define SOFA_ATTRIBUTE_DEPRECATED__RENAMED_TRIANGLEOCTREEMODEL()
43+
#else
44+
#define SOFA_ATTRIBUTE_DEPRECATED__RENAMED_TRIANGLEOCTREEMODEL() \
45+
SOFA_ATTRIBUTE_DEPRECATED( \
46+
"v25.12", "v26.06", \
47+
"TriangleOctreeModel has been renamed to TriangleOctreeCollisionModel in PR5766")
48+
#endif

Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/BuiltConstraintSolver.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
#include <sofa/helper/ScopedAdvancedTimer.h>
2626
#include <sofa/simulation/MainTaskSchedulerFactory.h>
2727
#include <sofa/simulation/ParallelForEach.h>
28+
#include <Eigen/Eigenvalues>
2829

2930
namespace sofa::component::constraint::lagrangian::solver
3031
{
3132

3233
BuiltConstraintSolver::BuiltConstraintSolver()
3334
: d_multithreading(initData(&d_multithreading, false, "multithreading", "Build compliances concurrently"))
35+
, d_useSVDForRegularization(initData(&d_useSVDForRegularization, false, "useSVDForRegularization", "Use SVD decomposiiton of the compliance matrix to project singular values smaller than regularization to the regularization term. Only works with built"))
36+
, d_svdSingularValueNullSpaceCriteriaFactor(initData(&d_svdSingularValueNullSpaceCriteriaFactor, 0.01, "svdSingularValueNullSpaceCriteriaFactor", "Fraction of the highest singular value bellow which a singular value will be supposed to belong to the nullspace"))
37+
, d_svdSingularVectorNullSpaceCriteriaFactor(initData(&d_svdSingularVectorNullSpaceCriteriaFactor, 0.001, "svdSingularVectorNullSpaceCriteriaFactor", "Absolute value bellow which a component of a normalized base vector will be considered null"))
3438
{}
3539

3640
void BuiltConstraintSolver::init()
@@ -42,6 +46,86 @@ void BuiltConstraintSolver::init()
4246
}
4347
}
4448

49+
void BuiltConstraintSolver::addRegularization(linearalgebra::BaseMatrix& W, const SReal regularization)
50+
{
51+
52+
if (d_useSVDForRegularization.getValue())
53+
{
54+
if (regularization>std::numeric_limits<SReal>::epsilon())
55+
{
56+
auto * FullW = dynamic_cast<sofa::linearalgebra::LPtrFullMatrix<SReal> * >(&W);
57+
if (FullW == nullptr)
58+
{
59+
msg_error()<<"BuiltConstraintSolver expect a LPtrFullMatrix for W but didn't receive one. The constraint problem is wrong. Please fix the code or just deactivate SVD regularization.";
60+
return;
61+
}
62+
const size_t problemSize = FullW->rowSize();
63+
64+
Eigen::Map<Eigen::MatrixX<SReal>> EigenW(FullW->ptr(),problemSize, problemSize) ;
65+
Eigen::JacobiSVD<Eigen::MatrixXd> svd( EigenW, Eigen::ComputeFullV | Eigen::ComputeFullU );
66+
67+
68+
69+
//Given the SVD, loop over all singular values, and those that are smaller than 1% of the highest one are considered to be the null space
70+
std::vector<bool> nullSpaceIndicator(problemSize, false);
71+
int nullSpaceBegin = -1;
72+
for(size_t i=0; i<problemSize; i++)
73+
{
74+
if (fabs(svd.singularValues()(i)) < fabs(svd.singularValues()(0)) * d_svdSingularValueNullSpaceCriteriaFactor.getValue())
75+
{
76+
nullSpaceBegin = i;
77+
break;
78+
}
79+
}
80+
81+
//Now for all vector of the null space basis, we look at the indices where the coefficient
82+
//is greater than 1% of the norm of the vector, this is the constraints that
83+
//belong to the null space and thus have other one that are antagonists
84+
for(int i=nullSpaceBegin; (i != -1) && (i<problemSize); ++i)
85+
{
86+
for(size_t j=0; j<problemSize; j++)
87+
nullSpaceIndicator[j] = nullSpaceIndicator[j] || fabs(svd.matrixV().col(i)(j)) > d_svdSingularVectorNullSpaceCriteriaFactor.getValue();
88+
}
89+
90+
if (f_printLog.getValue())
91+
{
92+
std::stringstream msg ;
93+
msg <<"Unregularized diagonal : ";
94+
for(size_t i=0; i<problemSize; i++)
95+
msg<<EigenW(i,i) << " ";
96+
msg_info()<< msg.str();
97+
}
98+
99+
//Because the eigen matrix uses the buffer of W to store the matrix, this is sufficient to set the value.
100+
//Now using the indicator vector, set the regularization for the constraints that belongs
101+
//to the null space to the regularization term
102+
for(size_t i=0; i<problemSize; i++)
103+
EigenW(i,i) += nullSpaceIndicator[i]*d_regularizationTerm.getValue();
104+
105+
if (f_printLog.getValue())
106+
{
107+
std::stringstream msg ;
108+
msg <<"Null space : ";
109+
for(size_t i=0; i<problemSize; i++)
110+
msg<<nullSpaceIndicator[i] << " ";
111+
msg_info()<< msg.str();
112+
113+
msg.flush();
114+
msg <<"Regularized diagonal : ";
115+
for(size_t i=0; i<problemSize; i++)
116+
msg<<EigenW(i,i) << " ";
117+
msg_info()<< msg.str();
118+
}
119+
120+
}
121+
}
122+
else
123+
{
124+
Inherit1::addRegularization(W, regularization);
125+
}
126+
}
127+
128+
45129
void BuiltConstraintSolver::doBuildSystem( const core::ConstraintParams *cParams, GenericConstraintProblem * problem ,unsigned int numConstraints)
46130
{
47131
SOFA_UNUSED(numConstraints);

Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/BuiltConstraintSolver.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API BuiltConstraintSolver : pu
3838
public:
3939
SOFA_CLASS(BuiltConstraintSolver, GenericConstraintSolver);
4040
Data<bool> d_multithreading; ///< Build compliances concurrently
41+
Data<bool> d_useSVDForRegularization; ///< Use SVD decomposiiton of the compliance matrix to project singular values smaller than regularization to the regularization term. Only works with built
42+
Data<SReal> d_svdSingularValueNullSpaceCriteriaFactor; ///< Fraction of the highest singular value bellow which a singular value will be supposed to belong to the nullspace
43+
Data<SReal> d_svdSingularVectorNullSpaceCriteriaFactor; ///< Absolute value bellow which a component of a normalized base vector will be considered null
44+
4145

4246
BuiltConstraintSolver();
4347

4448
virtual void init() override;
4549

4650
protected:
4751
virtual void doBuildSystem( const core::ConstraintParams *cParams, GenericConstraintProblem * problem ,unsigned int numConstraints) override;
52+
virtual void addRegularization(linearalgebra::BaseMatrix& W, const SReal regularization) override;
53+
4854

4955
private:
5056

Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/GenericConstraintSolver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ class SOFA_COMPONENT_CONSTRAINT_LAGRANGIAN_SOLVER_API GenericConstraintSolver :
124124
*/
125125
virtual void doSolve( GenericConstraintProblem * problem, SReal timeout = 0.0) = 0;
126126

127+
virtual void addRegularization(linearalgebra::BaseMatrix& W, const SReal regularization);
127128

128-
static void addRegularization(linearalgebra::BaseMatrix& W, const SReal regularization);
129129

130130

131131
private:

Sofa/Component/Topology/Container/Constant/src/sofa/component/topology/container/constant/MeshTopology.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -802,61 +802,11 @@ void MeshTopology::setNbPoints(Size n)
802802
nbPoints = n;
803803
}
804804

805-
Size MeshTopology::getNbEdges()
806-
{
807-
return Size(getEdges().size());
808-
}
809-
810-
Size MeshTopology::getNbTriangles()
811-
{
812-
return Size(getTriangles().size());
813-
}
814-
815-
Size MeshTopology::getNbQuads()
816-
{
817-
return Size(getQuads().size());
818-
}
819-
820-
Size MeshTopology::getNbTetrahedra()
821-
{
822-
return Size(getTetrahedra().size());
823-
}
824-
825-
Size MeshTopology::getNbHexahedra()
826-
{
827-
return Size(getHexahedra().size());
828-
}
829-
830805
Size MeshTopology::getNbUVs()
831806
{
832807
return Size(getUVs().size());
833808
}
834809

835-
const MeshTopology::Edge MeshTopology::getEdge(Index i)
836-
{
837-
return getEdges()[i];
838-
}
839-
840-
const MeshTopology::Triangle MeshTopology::getTriangle(Index i)
841-
{
842-
return getTriangles()[i];
843-
}
844-
845-
const MeshTopology::Quad MeshTopology::getQuad(Index i)
846-
{
847-
return getQuads()[i];
848-
}
849-
850-
const MeshTopology::Tetra MeshTopology::getTetrahedron(Index i)
851-
{
852-
return getTetrahedra()[i];
853-
}
854-
855-
const MeshTopology::Hexa MeshTopology::getHexahedron(Index i)
856-
{
857-
return getHexahedra()[i];
858-
}
859-
860810
const MeshTopology::UV MeshTopology::getUV(Index i)
861811
{
862812
return getUVs()[i];

Sofa/Component/Topology/Container/Constant/src/sofa/component/topology/container/constant/MeshTopology.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,6 @@ class SOFA_COMPONENT_TOPOLOGY_CONTAINER_CONSTANT_API MeshTopology : public core:
9999
const SeqTetrahedra& getTetrahedra() override;
100100
const SeqHexahedra& getHexahedra() override;
101101

102-
// Random accessors
103-
104-
Size getNbEdges() override;
105-
Size getNbTriangles() override;
106-
Size getNbQuads() override;
107-
Size getNbTetrahedra() override;
108-
Size getNbHexahedra() override;
109-
110-
const Edge getEdge(EdgeID i) override;
111-
const Triangle getTriangle(TriangleID i) override;
112-
const Quad getQuad(QuadID i) override;
113-
const Tetra getTetrahedron(TetraID i) override;
114-
const Hexa getHexahedron(HexaID i) override;
115-
116102
// If using STEP loader, include also uv coordinates
117103
typedef Index UVID;
118104
typedef type::Vec2 UV;

Sofa/framework/Core/src/sofa/core/topology/TopologyChange.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ std::ostream& operator << (std::ostream& out, const ElemAncestorElem<NV>& d)
197197
template<int NV>
198198
std::istream& operator >> (std::istream& in, ElemAncestorElem<NV>& d)
199199
{
200+
SOFA_UNUSED(d);
201+
200202
return in;
201203
}
202204

Sofa/framework/Helper/src/sofa/helper/ComponentChange.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,8 @@ std::map< std::string, Renamed, std::less<> > renamedComponents = {
765765
{"StiffSpringForceField", Renamed("v24.06","v25.06","SpringForceField")},
766766
{"ParallelStiffSpringForceField", Renamed("v24.06","v25.06","ParallelSpringForceField")},
767767
{"ShewchukPCGLinearSolver", Renamed("v24.12","v25.12","PCGLinearSolver")},
768-
{"OglCylinderModel", Renamed("v24.12", "v25.06", "CylinderVisualModel")}
768+
{"OglCylinderModel", Renamed("v24.12", "v25.06", "CylinderVisualModel")},
769+
{"TriangleOctreeModel", Renamed("v25.12", "v26.06", "TriangleOctreeCollisionModel") }
769770
};
770771

771772

0 commit comments

Comments
 (0)