/* SPDX-FileCopyrightText: 2023 Blender Foundation
 *
 * SPDX-License-Identifier: GPL-2.0-or-later */

#include "BKE_curves.hh"
#include "BKE_geometry_fields.hh"

#include "BLI_task.hh"

#include "DNA_object_types.h"

#include "ED_curves.hh"
#include "ED_transverts.h"

namespace blender::ed::curves {

void transverts_from_curves_positions_create(bke::CurvesGeometry &curves, TransVertStore *tvs)
{
  IndexMaskMemory memory;
  IndexMask selection = retrieve_selected_points(curves, memory);
  MutableSpan<float3> positions = curves.positions_for_write();

  tvs->transverts = static_cast<TransVert *>(
      MEM_calloc_arrayN(selection.size(), sizeof(TransVert), __func__));
  tvs->transverts_tot = selection.size();

  selection.foreach_index(GrainSize(1024), [&](const int64_t i, const int64_t pos) {
    TransVert &tv = tvs->transverts[pos];
    tv.loc = positions[i];
    tv.flag = SELECT;
    copy_v3_v3(tv.oldloc, tv.loc);
  });
}

}  // namespace blender::ed::curves

float (*ED_curves_point_normals_array_create(const Curves *curves_id))[3]
{
  using namespace blender;
  const bke::CurvesGeometry &curves = curves_id->geometry.wrap();
  const int size = curves.points_num();

  float3 *data = static_cast<float3 *>(MEM_malloc_arrayN(size, sizeof(float3), __func__));

  const bke::CurvesFieldContext context(curves, ATTR_DOMAIN_POINT);
  fn::FieldEvaluator evaluator(context, size);
  fn::Field<float3> field(std::make_shared<bke::NormalFieldInput>());
  evaluator.add_with_destination(std::move(field), {data, size});
  evaluator.evaluate();

  return reinterpret_cast<float(*)[3]>(data);
}
