// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
// SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov

#include <vtkArrayData.h>
#include <vtkArrayNorm.h>
#include <vtkArrayPrint.h>
#include <vtkDenseArray.h>
#include <vtkDiagonalMatrixSource.h>
#include <vtkSmartPointer.h>
#include <vtkSparseArray.h>

#include <cmath>
#include <iostream>
#include <stdexcept>

#define test_expression(expression)                                                                \
  do                                                                                               \
  {                                                                                                \
    if (!(expression))                                                                             \
      throw std::runtime_error("Expression failed: " #expression);                                 \
  } while (false)

static bool close_enough(const double lhs, const double rhs)
{
  return fabs(lhs - rhs) < 1.0e-12;
}

int TestArrayNorm(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
  cout << setprecision(17);

  try
  {
    vtkSmartPointer<vtkDiagonalMatrixSource> source =
      vtkSmartPointer<vtkDiagonalMatrixSource>::New();
    source->SetExtents(3);
    source->SetArrayType(vtkDiagonalMatrixSource::SPARSE);
    source->SetDiagonal(1.0);
    source->SetSuperDiagonal(0.5);
    source->SetSubDiagonal(-0.5);
    source->Update();

    cout << "diagonal source:\n";
    vtkPrintMatrixFormat(cout,
      vtkSparseArray<double>::SafeDownCast(
        source->GetOutput()->GetArray(static_cast<vtkIdType>(0))));

    vtkSmartPointer<vtkArrayNorm> vector_norm = vtkSmartPointer<vtkArrayNorm>::New();
    vector_norm->AddInputConnection(source->GetOutputPort());
    vector_norm->SetDimension(1); // Column-vectors
    vector_norm->SetL(2);
    vector_norm->Update();

    vtkDenseArray<double>* const l2_norm = vtkDenseArray<double>::SafeDownCast(
      vector_norm->GetOutput()->GetArray(static_cast<vtkIdType>(0)));

    cout << "L2-norm:\n";
    vtkPrintVectorFormat(cout, l2_norm);

    test_expression(l2_norm);
    test_expression(close_enough(l2_norm->GetValueN(0), 1.1180339887498949));
    test_expression(close_enough(l2_norm->GetValueN(1), 1.2247448713915889));
    test_expression(close_enough(l2_norm->GetValueN(2), 1.1180339887498949));

    vector_norm->SetL(1);
    vector_norm->Update();

    vtkDenseArray<double>* const l1_norm = vtkDenseArray<double>::SafeDownCast(
      vector_norm->GetOutput()->GetArray(static_cast<vtkIdType>(0)));

    cout << "L1-norm:\n";
    vtkPrintVectorFormat(cout, l1_norm);

    test_expression(l1_norm);
    test_expression(close_enough(l1_norm->GetValueN(0), 0.5));
    test_expression(close_enough(l1_norm->GetValueN(1), 1.0));
    test_expression(close_enough(l1_norm->GetValueN(2), 1.5));

    vector_norm->SetInvert(true);
    vector_norm->Update();

    vtkDenseArray<double>* const inverse_l1_norm = vtkDenseArray<double>::SafeDownCast(
      vector_norm->GetOutput()->GetArray(static_cast<vtkIdType>(0)));

    cout << "Inverse L1-norm:\n";
    vtkPrintVectorFormat(cout, inverse_l1_norm);

    test_expression(inverse_l1_norm);
    test_expression(close_enough(inverse_l1_norm->GetValueN(0), 2.0));
    test_expression(close_enough(inverse_l1_norm->GetValueN(1), 1.0));
    test_expression(close_enough(inverse_l1_norm->GetValueN(2), 0.666666666666666));

    vector_norm->SetInvert(false);
    vector_norm->SetWindow(vtkArrayRange(0, 2));
    vector_norm->Update();

    vtkDenseArray<double>* const window_l1_norm = vtkDenseArray<double>::SafeDownCast(
      vector_norm->GetOutput()->GetArray(static_cast<vtkIdType>(0)));

    cout << "Windowed L1-norm:\n";
    vtkPrintVectorFormat(cout, window_l1_norm);

    test_expression(window_l1_norm);
    test_expression(close_enough(window_l1_norm->GetValueN(0), 0.5));
    test_expression(close_enough(window_l1_norm->GetValueN(1), 1.5));
    test_expression(close_enough(window_l1_norm->GetValueN(2), 0.5));
    return 0;
  }
  catch (std::exception& e)
  {
    cerr << e.what() << endl;
    return 1;
  }
}
