Skip to content

Commit

Permalink
[Type] Refactor Vec (#4296)
Browse files Browse the repository at this point in the history
* WIP

* fix vecnoinit/vec, and constexpr init of array

* add an other conversion for rgbacolor

* correct aliases

* fix tests compil

* use correct type for getClosest

* add compat for picking

* polish

* change type in image

* remove superfluous typenames

* no need to clear as it is already zero-init
  • Loading branch information
fredroy authored Dec 12, 2023
1 parent 3249be8 commit 4a40054
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void BarycentricMapperHexahedronSetTopology<In,Out>::handleTopologyChange(core::
const auto j = *iter;
if ( mapData[j].in_index == sofa::InvalidID ) // compute new mapping
{
sofa::type::fixed_array<SReal, 3> coefs;
type::Vec3 coefs;
typename In::Coord pos;
pos[0] = mapData[j].baryCoords[0];
pos[1] = mapData[j].baryCoords[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void BarycentricMapperHexahedronSetTopology<defaulttype::Vec3Types, defaulttype:
const int j = *iter;
if ( mapData[j].in_index == sofa::InvalidID ) // compute new mapping
{
sofa::type::fixed_array<SReal, 3> coefs;
sofa::type::Vec3 coefs;
defaulttype::Vec3Types::Coord pos;
pos[0] = mapData[j].baryCoords[0];
pos[1] = mapData[j].baryCoords[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class HexahedronFEMForceField : virtual public core::behavior::ForceField<DataTy
inline const VecElement *getIndexedElements(){ return & (m_topology->getHexahedra()); }

virtual void computeElementStiffness( ElementStiffness &K, const MaterialStiffness &M,
const type::fixed_array<Coord,8> &nodes, const sofa::Index elementIndice,
const type::Vec<8, Coord> &nodes, const sofa::Index elementIndice,
double stiffnessFactor=1.0) const;
static Mat33 integrateStiffness( int signx0, int signy0, int signz0, int signx1, int signy1, int signz1,
const Real u, const Real v, const Real w, const Mat33& J_1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ const typename HexahedronFEMForceField<DataTypes>::Transformation& HexahedronFEM


template<class DataTypes>
void HexahedronFEMForceField<DataTypes>::computeElementStiffness( ElementStiffness &K, const MaterialStiffness &M, const type::fixed_array<Coord,8> &nodes, const sofa::Index elementIndice, double stiffnessFactor) const
void HexahedronFEMForceField<DataTypes>::computeElementStiffness( ElementStiffness &K, const MaterialStiffness &M, const type::Vec<8, Coord> &nodes, const sofa::Index elementIndice, double stiffnessFactor) const
{
const bool verbose = elementIndice==0;
// X = n0 (1-x1)(1-x2)(1-x3)/8 + n1 (1+x1)(1-x2)(1-x3)/8 + n2 (1+x1)(1+x2)(1-x3)/8 + n3 (1-x1)(1+x2)(1-x3)/8 + n4 (1-x1)(1-x2)(1+x3)/8 + n5 (1+x1)(1-x2)(1+x3)/8 + n6 (1+x1)(1+x2)(1+x3)/8 + n7 (1-x1)(1+x2)(1+x3)/8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,22 +384,22 @@ void JointSpringForceField<DataTypes>::draw(const core::visual::VisualParams* vp

for (sofa::Index i=0; i<springs.size(); i++)
{
Vec4f color;
sofa::type::RGBAColor color;

Real d = (p2[springs[i].m2]-p1[springs[i].m1]).getCenter().norm();
if (external)
{
if (d<springs[i].initTrans.norm()*0.9999)
color = Vec4f(1,0,0,1);
color = sofa::type::RGBAColor::red();
else
color = Vec4f(0,1,0,1);
color = sofa::type::RGBAColor::green();
}
else
{
if (d<springs[i].initTrans.norm()*0.9999)
color = Vec4f(1,0.5f,0,1);
color = sofa::type::RGBAColor(1,0.5f,0,1);
else
color = Vec4f(0,1,0.5f,1);
color = sofa::type::RGBAColor(0,1,0.5f,1);
}

Vec3 v0(p1[springs[i].m1].getCenter()[0], p1[springs[i].m1].getCenter()[1], p1[springs[i].m1].getCenter()[2]);
Expand Down
37 changes: 28 additions & 9 deletions Sofa/GUI/Common/src/sofa/gui/common/ColourPickingVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace
constexpr float threshold = std::numeric_limits<float>::min();
}

void decodeCollisionElement(const sofa::type::Vec4f colour, BodyPicked& body)
void decodeCollisionElement(const RGBAColor& colour, BodyPicked& body)
{

if( colour[0] > threshold || colour[1] > threshold || colour[2] > threshold ) // make sure we are not picking the background...
Expand All @@ -70,7 +70,7 @@ void decodeCollisionElement(const sofa::type::Vec4f colour, BodyPicked& body)

}

void decodePosition(BodyPicked& body, const sofa::type::Vec4f colour, const TriangleCollisionModel<sofa::defaulttype::Vec3Types>* model,
void decodePosition(BodyPicked& body, const RGBAColor& colour, const TriangleCollisionModel<sofa::defaulttype::Vec3Types>* model,
const unsigned int index)
{

Expand All @@ -83,9 +83,11 @@ void decodePosition(BodyPicked& body, const sofa::type::Vec4f colour, const Tria

}

void decodePosition(BodyPicked& body, const sofa::type::Vec4f /*colour*/, const SphereCollisionModel<sofa::defaulttype::Vec3Types> *model,
void decodePosition(BodyPicked& body, const RGBAColor& colour, const SphereCollisionModel<sofa::defaulttype::Vec3Types> *model,
const unsigned int index)
{
SOFA_UNUSED(colour);

const Sphere s(const_cast<SphereCollisionModel<sofa::defaulttype::Vec3Types>*>(model),index);
body.point = s.center();
}
Expand All @@ -97,6 +99,23 @@ simulation::Visitor::Result ColourPickingVisitor::processNodeTopDown(simulation:
return RESULT_CONTINUE;
}

void decodeCollisionElement(const sofa::type::Vec4f& colour, BodyPicked& body)
{
decodeCollisionElement(sofa::type::RGBAColor::fromVec4(colour), body);
}

void decodePosition(BodyPicked& body, const sofa::type::Vec4f& colour, const TriangleCollisionModel<sofa::defaulttype::Vec3Types>* model,
const unsigned int index)
{
decodePosition(body, sofa::type::RGBAColor::fromVec4(colour), model, index);
}

void decodePosition(BodyPicked& body, const sofa::type::Vec4f& colour, const SphereCollisionModel<sofa::defaulttype::Vec3Types> *model,
const unsigned int index)
{
decodePosition(body, sofa::type::RGBAColor::fromVec4(colour), model, index);
}

void ColourPickingVisitor::processCollisionModel(simulation::Node* node , core::CollisionModel* o)
{
using namespace core::objectmodel;
Expand Down Expand Up @@ -145,9 +164,9 @@ void ColourPickingVisitor::processTriangleModel(simulation::Node * node, sofa::c
points.push_back( t.p1() );
points.push_back( t.p2() );
points.push_back( t.p3() );
colours.push_back( Vec<4,float>(r,g,0,1) );
colours.push_back( Vec<4,float>(r,g,0,1) );
colours.push_back( Vec<4,float>(r,g,0,1) );
colours.emplace_back( r,g,0,1 );
colours.emplace_back( r,g,0,1 );
colours.emplace_back( r,g,0,1 );
}
break;
case ENCODE_RELATIVEPOSITION:
Expand All @@ -158,9 +177,9 @@ void ColourPickingVisitor::processTriangleModel(simulation::Node * node, sofa::c
points.push_back( t.p1() );
points.push_back( t.p2() );
points.push_back( t.p3() );
colours.push_back( Vec<4,float>(1,0,0,1) );
colours.push_back( Vec<4,float>(0,1,0,1) );
colours.push_back( Vec<4,float>(0,0,1,1) );
colours.emplace_back( 1,0,0,1 );
colours.emplace_back( 0,1,0,1 );
colours.emplace_back( 0,0,1,1 );
}
break;
default: assert(false);
Expand Down
16 changes: 13 additions & 3 deletions Sofa/GUI/Common/src/sofa/gui/common/ColourPickingVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@
namespace sofa::gui::common
{

void SOFA_GUI_COMMON_API decodeCollisionElement( const sofa::type::Vec4f colour, sofa::gui::component::performer::BodyPicked& body );
void SOFA_GUI_COMMON_API decodePosition( sofa::gui::component::performer::BodyPicked& body, const sofa::type::Vec4f colour, const sofa::component::collision::geometry::TriangleCollisionModel<sofa::defaulttype::Vec3Types>* model,
void SOFA_GUI_COMMON_API decodeCollisionElement( const type::RGBAColor& colour, sofa::gui::component::performer::BodyPicked& body );
void SOFA_GUI_COMMON_API decodePosition( sofa::gui::component::performer::BodyPicked& body, const type::RGBAColor& colour, const sofa::component::collision::geometry::TriangleCollisionModel<sofa::defaulttype::Vec3Types>* model,
const unsigned int index);
void SOFA_GUI_COMMON_API decodePosition( sofa::gui::component::performer::BodyPicked& body, const sofa::type::Vec4f colour, const sofa::component::collision::geometry::SphereCollisionModel<sofa::defaulttype::Vec3Types>* model,
void SOFA_GUI_COMMON_API decodePosition( sofa::gui::component::performer::BodyPicked& body, const type::RGBAColor& colour, const sofa::component::collision::geometry::SphereCollisionModel<sofa::defaulttype::Vec3Types>* model,
const unsigned int index);

// compat
SOFA_ATTRIBUTE_DEPRECATED__RGBACOLOR_AS_FIXEDARRAY()
void SOFA_GUI_COMMON_API decodeCollisionElement( const sofa::type::Vec4f& colour, sofa::gui::component::performer::BodyPicked& body );
SOFA_ATTRIBUTE_DEPRECATED__RGBACOLOR_AS_FIXEDARRAY()
void SOFA_GUI_COMMON_API decodePosition( sofa::gui::component::performer::BodyPicked& body, const sofa::type::Vec4f& colour, const sofa::component::collision::geometry::TriangleCollisionModel<sofa::defaulttype::Vec3Types>* model,
const unsigned int index);
SOFA_ATTRIBUTE_DEPRECATED__RGBACOLOR_AS_FIXEDARRAY()
void SOFA_GUI_COMMON_API decodePosition( sofa::gui::component::performer::BodyPicked& body, const sofa::type::Vec4f& colour, const sofa::component::collision::geometry::SphereCollisionModel<sofa::defaulttype::Vec3Types>* model,
const unsigned int index);


Expand Down
6 changes: 3 additions & 3 deletions Sofa/GUI/Qt/src/sofa/gui/qt/GLPickHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ BodyPicked GLPickHandler::findCollisionUsingColourCoding(const type::Vec3& origi
BodyPicked result;

result.dist = 0;
sofa::type::Vec4f color;
type::RGBAColor color;
const int x = mousePosition.x;
const int y = mousePosition.screenHeight - mousePosition.y;
TriangleCollisionModel<defaulttype::Vec3Types>* tmodel;
Expand All @@ -116,10 +116,10 @@ BodyPicked GLPickHandler::findCollisionUsingColourCoding(const type::Vec3& origi
if(renderCallback)
{
renderCallback->render(ColourPickingVisitor::ENCODE_COLLISIONELEMENT );
glReadPixels(x,y,1,1,_fboParams.colorFormat,_fboParams.colorType,color.elems);
glReadPixels(x,y,1,1,_fboParams.colorFormat,_fboParams.colorType, &color[0]);
decodeCollisionElement(color,result);
renderCallback->render(ColourPickingVisitor::ENCODE_RELATIVEPOSITION );
glReadPixels(x,y,1,1,_fboParams.colorFormat,_fboParams.colorType,color.elems);
glReadPixels(x,y,1,1,_fboParams.colorFormat,_fboParams.colorType, &color[0]);
if( ( tmodel = dynamic_cast<TriangleCollisionModel<defaulttype::Vec3Types>*>(result.body) ) != nullptr )
{
decodePosition(result,color,tmodel,result.indexCollisionElement);
Expand Down
4 changes: 2 additions & 2 deletions Sofa/GUI/Qt/src/sofa/gui/qt/QRGBAColorPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ QRGBAColorPicker::QRGBAColorPicker(QWidget* parent) : QWidget(parent)
connect( _colorButton, SIGNAL( clicked() ), this, SLOT( raiseQColorDialog() ) );
}

Vec4f QRGBAColorPicker::getColor() const
type::RGBAColor QRGBAColorPicker::getColor() const
{
typedef unsigned char uchar;
constexpr uchar max = std::numeric_limits<uchar>::max();
Vec4f color;
type::RGBAColor color;
float r = _r->text().toFloat();
float g = _g->text().toFloat();
float b = _b->text().toFloat();
Expand Down
2 changes: 1 addition & 1 deletion Sofa/GUI/Qt/src/sofa/gui/qt/QRGBAColorPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class QRGBAColorPicker : public QWidget
void setColor(const type::RGBAColor& color);

void setColor( const Vec4f& color );
Vec4f getColor() const;
type::RGBAColor getColor() const;

protected:
QRgb _rgba;
Expand Down
2 changes: 1 addition & 1 deletion Sofa/framework/Topology/src/sofa/topology/Hexahedron.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace sofa::topology

template<typename Coordinates, typename VectorCoordinates>
static constexpr sofa::Index getClosestHexahedronIndex(const VectorCoordinates& hexahedronPositions, const sofa::type::vector<Hexahedron>& hexahedra,
const Coordinates& pos, sofa::type::fixed_array<SReal,3>& barycentricCoefficients, SReal& distance)
const Coordinates& pos, type::Vec3& barycentricCoefficients, SReal& distance)
{
sofa::Index index = sofa::InvalidID;
distance = std::numeric_limits<SReal>::max();
Expand Down
2 changes: 1 addition & 1 deletion Sofa/framework/Topology/test/Hexahedron_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace sofa

TEST(TopologyHexahedron_test, getClosestHexahedronIndex)
{
sofa::type::fixed_array<SReal, 3> coeffs{};
type::Vec3 coeffs{};
SReal distance{};

const sofa::type::Vec3d pos0{0.001, 0., 0.};
Expand Down
16 changes: 8 additions & 8 deletions Sofa/framework/Type/src/sofa/type/BoundingBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ BoundingBox::operator bbox_t() const

SReal* BoundingBox::minBBoxPtr()
{
return bbox.first.elems;
return bbox.first.ptr();
}

SReal* BoundingBox::maxBBoxPtr()
{
return bbox.second.elems;
return bbox.second.ptr();
}

const SReal* BoundingBox::minBBoxPtr() const
{
return bbox.first.elems;
return bbox.first.ptr();
}

const SReal* BoundingBox::maxBBoxPtr() const
{
return bbox.second.elems;
return bbox.second.ptr();
}

const sofa::type::Vec3& BoundingBox::minBBox() const
Expand Down Expand Up @@ -360,22 +360,22 @@ BoundingBox2D::operator bbox_t() const

SReal* BoundingBox2D::minBBoxPtr()
{
return bbox.first.elems;
return bbox.first.ptr();
}

SReal* BoundingBox2D::maxBBoxPtr()
{
return bbox.second.elems;
return bbox.second.ptr();
}

const SReal* BoundingBox2D::minBBoxPtr() const
{
return bbox.first.elems;
return bbox.first.ptr();
}

const SReal* BoundingBox2D::maxBBoxPtr() const
{
return bbox.second.elems;
return bbox.second.ptr();
}

const sofa::type::Vec<2, SReal>& BoundingBox2D::minBBox() const
Expand Down
5 changes: 5 additions & 0 deletions Sofa/framework/Type/src/sofa/type/RGBAColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ RGBAColor::RGBAColor(const type::fixed_array<float, NumberOfComponents>& c)
{
}

RGBAColor::RGBAColor(const type::Vec4f& c)
: m_components{ c[0], c[1], c[2], c[3] }
{
}

bool RGBAColor::read(const std::string& str, RGBAColor& color)
{
std::stringstream s(str);
Expand Down
2 changes: 2 additions & 0 deletions Sofa/framework/Type/src/sofa/type/RGBAColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class SOFA_TYPE_API RGBAColor
// compat
SOFA_ATTRIBUTE_DEPRECATED__RGBACOLOR_AS_FIXEDARRAY()
RGBAColor(const type::fixed_array<float, NumberOfComponents>& c);
SOFA_ATTRIBUTE_DEPRECATED__RGBACOLOR_AS_FIXEDARRAY()
RGBAColor(const type::Vec4f& c);

SOFA_ATTRIBUTE_DEPRECATED__RGBACOLOR_AS_FIXEDARRAY()
static RGBAColor fromVec4(const type::fixed_array<float, 4>& color);
Expand Down
Loading

0 comments on commit 4a40054

Please sign in to comment.