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 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
78 changes: 62 additions & 16 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,11 @@ 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."))
, d_infinite(initData(&d_infinite, false, "infinite", "If true, ignore the 'size' and draw infinite lines."))
, 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 gradually vanish."))
, m_drawX(true), m_drawY(true), m_drawZ(true)
{}

Expand Down Expand Up @@ -73,30 +77,72 @@ void LineAxis::doDrawVisual(const core::visual::VisualParams* vparams)
{
const double s = sofa::helper::narrow_cast<double>(d_size.getValue());

vparams->drawTool()->disableLighting();
auto drawtool = vparams->drawTool();
drawtool->disableLighting();

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

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

const auto& bbox = helper::getReadAccessor(getContext()->f_bbox);
auto v = bbox->maxBBox() - bbox->minBBox();
const auto& thickness = helper::getReadAccessor(d_thickness);
const auto& vanishing = helper::getReadAccessor(d_vanishing);
const auto& infinite = helper::getReadAccessor(d_infinite);

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 (!infinite)
{
drawtool->drawLines(points, indices, thickness, DrawTool::RGBAColor::red());
}
else // infinite line
{
drawtool->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(v.x(), 0, 0), thickness,
DrawTool::RGBAColor::red(), vanishing);
drawtool->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(-v.x(), 0, 0), thickness,
DrawTool::RGBAColor::red(), 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 (!infinite)
{
drawtool->drawLines(points, indices, thickness, DrawTool::RGBAColor::green());
}
else // infinite line
{
drawtool->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(0, v.y(), 0), thickness,
DrawTool::RGBAColor::green(), vanishing);
drawtool->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(0, -v.y(), 0), thickness,
DrawTool::RGBAColor::green(), 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 (!infinite)
{
drawtool->drawLines(points, indices, thickness, DrawTool::RGBAColor::blue());
}
else // infinite line
{
drawtool->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(0, 0, v.z()), thickness,
DrawTool::RGBAColor::blue(), vanishing);
drawtool->drawInfiniteLine(DrawTool::Vec3(0, 0, 0), DrawTool::Vec3(0, 0, -v.z()), thickness,
DrawTool::RGBAColor::blue(), vanishing);
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions Sofa/Component/Visual/src/sofa/component/visual/LineAxis.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class SOFA_COMPONENT_VISUAL_API LineAxis : public core::visual::VisualModel
SOFA_CLASS(LineAxis, 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<float> d_size; ///< Size of the lines
Data<bool> d_infinite; ///< If true, ignore the "size" and draw infinite lines
Data<float> d_thickness; ///< Thickness of the lines
Data<bool> d_vanishing; ///< In case of infinite lines, should the lines gradually 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