/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkCompositeInterpolatedVelocityField.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkCompositeInterpolatedVelocityField.h"

#include "vtkDataArray.h"
#include "vtkDataSet.h"
#include "vtkGenericCell.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"

//------------------------------------------------------------------------------
vtkCompositeInterpolatedVelocityField::vtkCompositeInterpolatedVelocityField()
{
  this->LastDataSetIndex = 0;
}

//------------------------------------------------------------------------------
vtkCompositeInterpolatedVelocityField::~vtkCompositeInterpolatedVelocityField() = default;

//------------------------------------------------------------------------------
// Copy the list of datasets to copy from.
void vtkCompositeInterpolatedVelocityField::CopyParameters(
  vtkAbstractInterpolatedVelocityField* from)
{
  this->Superclass::CopyParameters(from);

  // See if we need to copy our parameters
  vtkCompositeInterpolatedVelocityField* obj =
    vtkCompositeInterpolatedVelocityField::SafeDownCast(from);
  if (!obj)
  {
    return;
  }
  this->DataSets = obj->DataSets;

  // The weights must be copied as well
  this->Weights.resize(obj->Weights.size());
}

//------------------------------------------------------------------------------
void vtkCompositeInterpolatedVelocityField::AddDataSet(vtkDataSet* dataset)
{
  if (!dataset)
  {
    vtkErrorMacro(<< "Dataset nullptr!");
    return;
  }

  // insert the dataset (do NOT register the dataset to 'this')
  this->DataSets.push_back(dataset);

  size_t size = dataset->GetMaxCellSize();
  if (size > this->Weights.size())
  {
    this->Weights.resize(size);
  }
}

//------------------------------------------------------------------------------
void vtkCompositeInterpolatedVelocityField::SetLastCellId(vtkIdType c, int dataindex)
{
  this->LastCellId = c;
  this->LastDataSet = this->DataSets[dataindex];

  // If the dataset changes, then the cached cell is invalidated. We might as
  // well prefetch the cached cell either way.
  if (this->LastCellId != -1)
  {
    this->LastDataSet->GetCell(this->LastCellId, this->CurrentCell);
  }

  this->LastDataSetIndex = dataindex;
}

//------------------------------------------------------------------------------
int vtkCompositeInterpolatedVelocityField::FunctionValues(double* x, double* f)
{
  vtkDataSet* ds;
  if (!this->LastDataSet && !this->DataSets.empty())
  {
    ds = this->DataSets[0];
    this->LastDataSet = ds;
    this->LastDataSetIndex = 0;
  }
  else
  {
    ds = this->LastDataSet;
  }

  // Use the superclass's method first as it is faster.
  int retVal = this->FunctionValues(ds, x, f);

  if (!retVal)
  {
    // Okay need to check other datasets since we are outside of the current dataset.
    for (this->LastDataSetIndex = 0;
         this->LastDataSetIndex < static_cast<int>(this->DataSets.size()); this->LastDataSetIndex++)
    {
      ds = this->DataSets[this->LastDataSetIndex];
      if (ds && ds->GetNumberOfPoints() > 0 && ds != this->LastDataSet)
      {
        this->ClearLastCellId();
        retVal = this->FunctionValues(ds, x, f);
        if (retVal)
        {
          this->LastDataSet = ds;
          return retVal;
        }
      }
    }
    this->LastCellId = -1;
    this->LastDataSetIndex = 0;
    this->LastDataSet = this->DataSets[0];
    return 0;
  }

  return retVal;
}

//------------------------------------------------------------------------------
int vtkCompositeInterpolatedVelocityField::SnapPointOnCell(double* pOrigin, double* pSnap)
{
  if (this->LastDataSet == nullptr)
  {
    return 0;
  }
  auto datasetFunctionCacheIter = this->FunctionCacheMap.find(this->LastDataSet);
  // Find the closest cell
  if (!this->FindAndUpdateCell(
        this->LastDataSet, datasetFunctionCacheIter->second.Strategy, pOrigin))
  {
    return 0;
  }
  pSnap[0] = this->LastClosestPoint[0];
  pSnap[1] = this->LastClosestPoint[1];
  pSnap[2] = this->LastClosestPoint[2];
  return 1;
}

//------------------------------------------------------------------------------
void vtkCompositeInterpolatedVelocityField::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);

  os << indent << "Number of DataSets: " << this->DataSets.size() << endl;
  os << indent << "Last Dataset Index: " << this->LastDataSetIndex << endl;
}
