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

[visual] Adds features to LineAxis and DrawTools #5258

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
72 changes: 57 additions & 15 deletions Sofa/Component/Visual/src/sofa/component/visual/LineAxis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
namespace sofa::component::visual
{

using helper::visual::DrawTool;

void registerLineAxis(sofa::core::ObjectFactory* factory)
{
factory->registerObjects(core::ObjectRegistrationData("Display scene axis")
Expand All @@ -38,9 +40,10 @@ void registerLineAxis(sofa::core::ObjectFactory* factory)
using namespace sofa::defaulttype;

LineAxis::LineAxis()
: d_axis(initData(&d_axis, std::string("xyz"), "axis", "Axis to draw"))
, d_size(initData(&d_size, 10.f, "size", "Size of the squared grid"))
, d_thickness(initData(&d_thickness, 1.f, "thickness", "Thickness of the lines in the grid"))
: d_axis(initData(&d_axis, std::string("xyz"), "axis", "Axis to draw."))
, d_size(initData(&d_size, 10.f, "size", "Size of the lines. Give a negative value for infinite lines."))
bakpaul marked this conversation as resolved.
Show resolved Hide resolved
, d_thickness(initData(&d_thickness, 1.f, "thickness", "Thickness of the lines."))
, d_vanishing(initData(&d_vanishing, false, "vanishing", "In case of infinite lines, should the lines vanish."))
, m_drawX(true), m_drawY(true), m_drawZ(true)
{}

Expand Down Expand Up @@ -75,28 +78,67 @@ void LineAxis::doDrawVisual(const core::visual::VisualParams* vparams)

vparams->drawTool()->disableLighting();

std::vector<type::Vec3> points;
points.resize(2);

std::vector<type::Vec2i> indices = {type::Vec2i(0,1)};

auto box = getContext()->f_bbox.getValue().maxBBox() - getContext()->f_bbox.getValue().minBBox();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would factorize the getContext()->f_box.getValue() /

const auto& thickness = helper::getReadAccessor(d_thickness);
const auto& vanishing = helper::getReadAccessor(d_vanishing);

if(m_drawX)
{
vparams->drawTool()->drawLine(
helper::visual::DrawTool::Vec3(-s*0.5, 0.0, 0.0),
helper::visual::DrawTool::Vec3(s*0.5, 0.0, 0.0),
helper::visual::DrawTool::RGBAColor(1.0f, 0.0f, 0.0f, 1.0f));
points[0] = DrawTool::Vec3(-s*0.5, 0.0, 0.0);
points[1] = DrawTool::Vec3(s*0.5, 0.0, 0.0);

if (s>0)
{
vparams->drawTool()->drawLines(points, indices, thickness, DrawTool::RGBAColor(1.0f, 0.0f, 0.0f, 1.0f));
bakpaul marked this conversation as resolved.
Show resolved Hide resolved
}
else // infinite line
{
vparams->drawTool()->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(box.x(), 0, 0), thickness,
DrawTool::RGBAColor(1.0f, 0.0f, 0.0f, 1.0f), vanishing);
vparams->drawTool()->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(-box.x(), 0, 0), thickness,
DrawTool::RGBAColor(1.0f, 0.0f, 0.0f, 1.0f), vanishing);
}
}

if(m_drawY)
{
vparams->drawTool()->drawLine(
helper::visual::DrawTool::Vec3(0.0, -s*0.5, 0.0),
helper::visual::DrawTool::Vec3(0.0, s*0.5, 0.0),
helper::visual::DrawTool::RGBAColor(0.0f, 1.0f, 0.0f, 1.0f));
points[0] = DrawTool::Vec3(0.0, -s*0.5, 0.0);
points[1] = DrawTool::Vec3(0.0, s*0.5, 0.0);

if (s>0)
{
vparams->drawTool()->drawLines(points, indices, thickness, DrawTool::RGBAColor(0.0f, 1.0f, 0.0f, 1.0f));
}
else // infinite line
{
vparams->drawTool()->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(0, box.y(), 0), thickness,
DrawTool::RGBAColor(0.0f, 1.0f, 0.0f, 1.0f), vanishing);
vparams->drawTool()->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(0, -box.y(), 0), thickness,
DrawTool::RGBAColor(0.0f, 1.0f, 0.0f, 1.0f), vanishing);
}
}

if(m_drawZ)
{
vparams->drawTool()->drawLine(
helper::visual::DrawTool::Vec3(0.0, 0.0, -s*0.5),
helper::visual::DrawTool::Vec3(0.0, 0.0, s*0.5),
helper::visual::DrawTool::RGBAColor(0.0f, 0.0f, 1.0f, 1.0f));
points[0] = DrawTool::Vec3(0.0, 0.0, -s*0.5);
points[1] = DrawTool::Vec3(0.0, 0.0, s*0.5);

if (s>0)
{
vparams->drawTool()->drawLines(points, indices, thickness, DrawTool::RGBAColor(0.0f, 0.0f, 1.0f, 1.0f));
}
else // infinite line
{
vparams->drawTool()->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(0, 0, box.z()), thickness,
DrawTool::RGBAColor(0.0f, 0.0f, 1.0f, 1.0f), vanishing);
vparams->drawTool()->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(0, 0, -box.z()), thickness,
DrawTool::RGBAColor(0.0f, 0.0f, 1.0f, 1.0f), vanishing);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions Sofa/Component/Visual/src/sofa/component/visual/LineAxis.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SOFA_COMPONENT_VISUAL_API LineAxis : public core::visual::VisualModel
Data<std::string> d_axis; ///< Axis to draw
Data<float> d_size; ///< Size of the squared grid
Data<float> d_thickness; ///< Thickness of the lines in the grid
Data<bool> d_vanishing; ///< In case of infinite lines, should the lines vanish.
core::objectmodel::lifecycle::RemovedData d_draw {this, "v23.06", "23.12", "draw", "Use the 'enable' data field instead of 'draw'"};

LineAxis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ void VisualGrid::doDrawVisual(const core::visual::VisualParams* vparams)
{
vparams->drawTool()->disableLighting();
vparams->drawTool()->drawLines(m_drawnPoints, d_thickness.getValue(), d_color.getValue());

}

} // namespace sofa::component::visual
15 changes: 13 additions & 2 deletions Sofa/GL/src/sofa/gl/DrawToolGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,26 @@ void DrawToolGL::drawLine(const Vec3 &p1, const Vec3 &p2, const type::RGBAColor&
glEnd();
}

void DrawToolGL::drawInfiniteLine(const Vec3 &point, const Vec3 &direction, const type::RGBAColor& color)
void DrawToolGL::drawInfiniteLine(const Vec3 &point, const Vec3 &direction, const type::RGBAColor& color, const bool& vanishing)
{
glBegin(GL_LINES);
glColor4f(color[0],color[1],color[2],color[3]);

glColor4f(color[0], color[1], color[2], color[3]);
glVertex4d(point[0], point[1], point[2], 1.0);
if(vanishing)
glColor4f(color[0], color[1], color[2], 0.0f);
glVertex4d(direction[0], direction[1], direction[2], 0.0);

glEnd();
}

void DrawToolGL::drawInfiniteLine(const Vec3 &point, const Vec3 &direction, const float& size, const type::RGBAColor& color, const bool &vanishing)
{
glLineWidth(size);
drawInfiniteLine(point, direction, color, vanishing);
glLineWidth(1);
}

void DrawToolGL::drawLines(const std::vector<Vec3> &points, float size, const type::RGBAColor& color)
{
setMaterial(color);
Expand Down
4 changes: 3 additions & 1 deletion Sofa/GL/src/sofa/gl/DrawToolGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class SOFA_GL_API DrawToolGL : public helper::visual::DrawTool
virtual void drawPoints(const std::vector<type::Vec3> &points, float size, const std::vector<type::RGBAColor>& color) override;

void drawLine(const type::Vec3 &p1, const type::Vec3 &p2, const type::RGBAColor& color) override;
void drawInfiniteLine(const type::Vec3 &point, const type::Vec3 &direction, const type::RGBAColor& color) override;
void drawInfiniteLine(const type::Vec3 &point, const type::Vec3 &direction, const type::RGBAColor& color, const bool& vanishing=false) override;
void drawInfiniteLine(const Vec3 &point, const Vec3 &direction, const float& size, const type::RGBAColor& color, const bool& vanishing=false) override;

virtual void drawLines(const std::vector<type::Vec3> &points, float size, const type::RGBAColor& color) override;
virtual void drawLines(const std::vector<type::Vec3> &points, float size, const std::vector<type::RGBAColor>& colors) override;
virtual void drawLines(const std::vector<type::Vec3> &points, const std::vector< type::Vec2i > &index, float size, const type::RGBAColor& color) override;
Expand Down
3 changes: 2 additions & 1 deletion Sofa/framework/Helper/src/sofa/helper/visual/DrawTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class SOFA_HELPER_API DrawTool
virtual void drawPoints(const std::vector<Vec3> &points, float size, const std::vector<RGBAColor>& color) = 0;

virtual void drawLine(const Vec3 &p1, const Vec3 &p2, const RGBAColor& color) = 0;
virtual void drawInfiniteLine(const Vec3 &point, const Vec3 &direction, const RGBAColor& color) = 0;
virtual void drawInfiniteLine(const Vec3 &point, const Vec3 &direction, const RGBAColor& color, const bool& vanishing=false) = 0;
virtual void drawInfiniteLine(const Vec3 &point, const Vec3 &direction, const float& size, const RGBAColor& color, const bool& vanishing=false) = 0;
virtual void drawLines(const std::vector<Vec3> &points, float size, const RGBAColor& color) = 0 ;
virtual void drawLines(const std::vector<Vec3> &points, float size, const std::vector<RGBAColor>& colors) = 0 ;
virtual void drawLines(const std::vector<Vec3> &points, const std::vector< Vec2i > &index , float size, const RGBAColor& color) = 0 ;
Expand Down
Loading