<commit_before>#include "./label_node.h"
#include <QPainter>
#include <QImage>
#include <QPoint>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include "./graphics/texture_address.h"
#include "./graphics/shader_program.h"
#include "./importer.h"
#include "./math/eigen.h"
#include "./labelling/labeller_frame_data.h"

LabelNode::LabelNode(Label label) : label(label)
{
  Importer importer;

  anchorMesh = importer.import("assets/anchor.dae", 0);
  quad = std::make_shared<Graphics::Quad>(":/shader/label.vert",
                                          ":/shader/label.frag");

  connector = std::make_shared<Graphics::Connector>(
      ":/shader/pass.vert", ":/shader/connector.frag", Eigen::Vector3f(0, 0, 0),
      Eigen::Vector3f(1, 0, 0));
  connector->color = Eigen::Vector4f(0.75f, 0.75f, 0.75f, 1);
}

LabelNode::~LabelNode()
{
}

void LabelNode::render(Graphics::Gl *gl,
                       std::shared_ptr<Graphics::Managers> managers,
                       RenderData renderData)
{
  if (!label.isAnchorInsideFieldOfView(LabellerFrameData(
          0, renderData.projectionMatrix, renderData.viewMatrix)))
    return;

  if (textureId == -1 || textureText != label.text || labelSize != label.size)
  {
    initialize(gl, managers);
  }

  if (isVisible)
    renderAnchor(gl, managers, renderData);
}

void
LabelNode::renderLabelAndConnector(Graphics::Gl *gl,
                                   std::shared_ptr<Graphics::Managers> managers,
                                   RenderData renderData)
{
  if (!label.isAnchorInsideFieldOfView(LabellerFrameData(
          0, renderData.projectionMatrix, renderData.viewMatrix)))
    return;

  if (textureId == -1 || textureText != label.text)
  {
    initialize(gl, managers);
  }

  if (isVisible)
  {
    renderConnector(gl, managers, renderData);
    renderLabel(gl, managers, renderData);
  }
}

void LabelNode::setIsVisible(bool isVisible)
{
  this->isVisible = isVisible;
}

void LabelNode::initialize(Graphics::Gl *gl,
                           std::shared_ptr<Graphics::Managers> managers)
{
  quad->initialize(gl, managers);
  connector->initialize(gl, managers);

  auto image = renderLabelTextToQImage();
  auto textureManager = managers->getTextureManager();
  textureId = textureManager->addTexture(image);
  delete image;

  if (!labelQuad.isInitialized())
  {
    labelQuad = quad->getObjectData();
    labelQuad.setCustomBuffer(sizeof(Graphics::TextureAddress),
                              [textureManager, this](void *insertionPoint)
                              {
      auto textureAddress = textureManager->getAddressFor(textureId);
      textureAddress.reserved = this->layerIndex;
      std::memcpy(insertionPoint, &textureAddress,
                  sizeof(Graphics::TextureAddress));
    });
  }

  if (!labelConnector.isInitialized())
  {
    labelConnector = connector->getObjectData();
    labelConnector.setCustomBuffer(sizeof(int), [this](void *insertionPoint)
                                   {
      std::memcpy(insertionPoint, &this->layerIndex, sizeof(int));
    });
  }
}

void LabelNode::renderConnector(Graphics::Gl *gl,
                                std::shared_ptr<Graphics::Managers> managers,
                                RenderData renderData)<commit_msg>Delete old texture on LabelNode reinitialization.<commit_after>#include "./label_node.h"
#include <QPainter>
#include <QImage>
#include <QPoint>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include "./graphics/texture_address.h"
#include "./graphics/shader_program.h"
#include "./importer.h"
#include "./math/eigen.h"
#include "./labelling/labeller_frame_data.h"

LabelNode::LabelNode(Label label) : label(label)
{
  Importer importer;

  anchorMesh = importer.import("assets/anchor.dae", 0);
  quad = std::make_shared<Graphics::Quad>(":/shader/label.vert",
                                          ":/shader/label.frag");

  connector = std::make_shared<Graphics::Connector>(
      ":/shader/pass.vert", ":/shader/connector.frag", Eigen::Vector3f(0, 0, 0),
      Eigen::Vector3f(1, 0, 0));
  connector->color = Eigen::Vector4f(0.75f, 0.75f, 0.75f, 1);
}

LabelNode::~LabelNode()
{
}

void LabelNode::render(Graphics::Gl *gl,
                       std::shared_ptr<Graphics::Managers> managers,
                       RenderData renderData)
{
  if (!label.isAnchorInsideFieldOfView(LabellerFrameData(
          0, renderData.projectionMatrix, renderData.viewMatrix)))
    return;

  if (textureId == -1 || textureText != label.text || labelSize != label.size)
  {
    initialize(gl, managers);
  }

  if (isVisible)
    renderAnchor(gl, managers, renderData);
}

void
LabelNode::renderLabelAndConnector(Graphics::Gl *gl,
                                   std::shared_ptr<Graphics::Managers> managers,
                                   RenderData renderData)
{
  if (!label.isAnchorInsideFieldOfView(LabellerFrameData(
          0, renderData.projectionMatrix, renderData.viewMatrix)))
    return;

  if (textureId == -1 || textureText != label.text)
  {
    initialize(gl, managers);
  }

  if (isVisible)
  {
    renderConnector(gl, managers, renderData);
    renderLabel(gl, managers, renderData);
  }
}

void LabelNode::setIsVisible(bool isVisible)
{
  this->isVisible = isVisible;
}

void LabelNode::initialize(Graphics::Gl *gl,
                           std::shared_ptr<Graphics::Managers> managers)
{
  quad->initialize(gl, managers);
  connector->initialize(gl, managers);

  auto image = renderLabelTextToQImage();
  auto textureManager = managers->getTextureManager();
  if (textureId >= 0)
    textureManager->free(textureId);

  textureId = textureManager->addTexture(image);
  delete image;

  if (!labelQuad.isInitialized())
  {
    labelQuad = quad->getObjectData();
    labelQuad.setCustomBuffer(sizeof(Graphics::TextureAddress),
                              [textureManager, this](void *insertionPoint)
                              {
      auto textureAddress = textureManager->getAddressFor(textureId);
      textureAddress.reserved = this->layerIndex;
      std::memcpy(insertionPoint, &textureAddress,
                  sizeof(Graphics::TextureAddress));
    });
  }

  if (!labelConnector.isInitialized())
  {
    labelConnector = connector->getObjectData();
    labelConnector.setCustomBuffer(sizeof(int), [this](void *insertionPoint)
                                   {
      std::memcpy(insertionPoint, &this->layerIndex, sizeof(int));
    });
  }
}

void LabelNode::renderConnector(Graphics::Gl *gl,
                                std::shared_ptr<Graphics::Managers> managers,
                                RenderData renderData)
{'fraction_non_alphanumeric': 0.13498041578788791, 'fraction_numerical': 0.0073817414884001205, 'mean_word_length': 2.9897836538461537, 'pattern_counts': {'":': 8, '<': 25, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '67870', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1940, 'n_words': 330}