/* +------------------------------------------------------------------------+
   |                     Mobile Robot Programming Toolkit (MRPT)            |
   |                          https://www.mrpt.org/                         |
   |                                                                        |
   | Copyright (c) 2005-2024, Individual contributors, see AUTHORS file     |
   | See: https://www.mrpt.org/Authors - All rights reserved.               |
   | Released under BSD License. See: https://www.mrpt.org/License          |
   +------------------------------------------------------------------------+ */

#include "opengl-precomp.h"  // Precompiled header
//
#include <mrpt/opengl/CGridPlaneXY.h>
#include <mrpt/serialization/CArchive.h>

using namespace mrpt;
using namespace mrpt::opengl;
using namespace std;

IMPLEMENTS_SERIALIZABLE(CGridPlaneXY, CRenderizableShaderWireFrame, mrpt::opengl)

/** Constructor  */
CGridPlaneXY::CGridPlaneXY(
    float xMin,
    float xMax,
    float yMin,
    float yMax,
    float z,
    float frequency,
    float lineWidth,
    bool antiAliasing) :
    m_xMin(xMin), m_xMax(xMax), m_yMin(yMin), m_yMax(yMax), m_plane_z(z), m_frequency(frequency)
{
  m_lineWidth = lineWidth;
  m_antiAliasing = antiAliasing;
}

void CGridPlaneXY::onUpdateBuffers_Wireframe()
{
  ASSERT_GT_(m_frequency, 0);

  m_vertex_buffer_data.clear();
  m_color_buffer_data.clear();
  std::unique_lock<std::shared_mutex> wfWriteLock(
      CRenderizableShaderWireFrame::m_wireframeMtx.data);

  for (float y = m_yMin; y <= m_yMax; y += m_frequency)
  {
    m_vertex_buffer_data.emplace_back(m_xMin, y, m_plane_z);
    m_vertex_buffer_data.emplace_back(m_xMax, y, m_plane_z);
  }

  for (float x = m_xMin; x <= m_xMax; x += m_frequency)
  {
    m_vertex_buffer_data.emplace_back(x, m_yMin, m_plane_z);
    m_vertex_buffer_data.emplace_back(x, m_yMax, m_plane_z);
  }
  // The same color to all vertices:
  m_color_buffer_data.assign(m_vertex_buffer_data.size(), getColor_u8());
}

uint8_t CGridPlaneXY::serializeGetVersion() const { return 1; }
void CGridPlaneXY::serializeTo(mrpt::serialization::CArchive& out) const
{
  writeToStreamRender(out);
  out << m_xMin << m_xMax;
  out << m_yMin << m_yMax << m_plane_z;
  out << m_frequency;
  out << m_lineWidth << m_antiAliasing;  // v1
}

void CGridPlaneXY::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version)
{
  switch (version)
  {
    case 0:
    case 1:
    {
      readFromStreamRender(in);
      in >> m_xMin >> m_xMax;
      in >> m_yMin >> m_yMax >> m_plane_z;
      in >> m_frequency;
      if (version >= 1)
        in >> m_lineWidth >> m_antiAliasing;
      else
      {
        m_lineWidth = 1.0f;
        m_antiAliasing = true;
      }
    }
    break;
    default:
      MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
  };
  CRenderizable::notifyChange();
}

auto CGridPlaneXY::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf
{
  return mrpt::math::TBoundingBoxf::FromUnsortedPoints({m_xMin, m_yMin, 0}, {m_xMax, m_yMax, 0});
}
