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

#include "vtkWedge.h"

#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDoubleArray.h"
#include "vtkIncrementalPointLocator.h"
#include "vtkLine.h"
#include "vtkMarchingCellsClipCases.h"
#include "vtkMarchingCellsContourCases.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkQuad.h"
#include "vtkTriangle.h"
#include "vtkUnstructuredGrid.h"

#include <algorithm> //std::copy
#include <cassert>

namespace
{
//------------------------------------------------------------------------------
[[maybe_unused]] constexpr const char* Topology = R"(
   Wedge topology:

           2
          /|\
         / | \
        /  |  \
       /   |   \
      0----|----1    ← back triangle
      |    5    |
      |   / \   |
      |  /   \  |
      | /     \ |
      |/       \|
      3---------4    ← front triangle
)";

//------------------------------------------------------------------------------
double ParametricCoords[18] = {
  0.0, 0.0, 0.0, //
  1.0, 0.0, 0.0, //
  0.0, 1.0, 0.0, //
  0.0, 0.0, 1.0, //
  1.0, 0.0, 1.0, //
  0.0, 1.0, 1.0  //
};

//------------------------------------------------------------------------------
constexpr vtkIdType Edges[vtkWedge::NumberOfEdges][2] = {
  { 0, 1 }, // 0
  { 1, 2 }, // 1
  { 2, 0 }, // 2
  { 3, 4 }, // 3
  { 4, 5 }, // 4
  { 5, 3 }, // 5
  { 0, 3 }, // 6
  { 1, 4 }, // 7
  { 2, 5 }, // 8
};

//------------------------------------------------------------------------------
constexpr vtkIdType Faces[vtkWedge::NumberOfFaces][vtkWedge::MaximumFaceSize + 1] = {
  { 0, 2, 1, -1, -1 }, // 0
  { 3, 4, 5, -1, -1 }, // 1
  { 0, 1, 4, 3, -1 },  // 2
  { 1, 2, 5, 4, -1 },  // 3
  { 2, 0, 3, 5, -1 },  // 4
};

//------------------------------------------------------------------------------
constexpr vtkIdType EdgeToAdjacentFaces[vtkWedge::NumberOfEdges][2] = {
  { 0, 2 }, // 0
  { 0, 3 }, // 1
  { 0, 3 }, // 2
  { 1, 2 }, // 3
  { 1, 3 }, // 4
  { 1, 4 }, // 5
  { 2, 4 }, // 6
  { 2, 3 }, // 7
  { 3, 4 }, // 8
};

//------------------------------------------------------------------------------
constexpr vtkIdType FaceToAdjacentFaces[vtkWedge::NumberOfFaces][vtkWedge::MaximumFaceSize] = {
  { 4, 3, 2, -1 }, // 0
  { 2, 3, 4, -1 }, // 1
  { 0, 3, 1, 4 },  // 2
  { 0, 4, 1, 2 },  // 3
  { 0, 2, 1, 3 },  // 4
};

//------------------------------------------------------------------------------
constexpr vtkIdType PointToIncidentEdges[vtkWedge::NumberOfPoints][vtkWedge::MaximumValence] = {
  { 0, 6, 2 }, // 0
  { 0, 1, 7 }, // 1
  { 1, 2, 8 }, // 2
  { 3, 5, 6 }, // 3
  { 3, 7, 4 }, // 4
  { 4, 8, 5 }, // 5
};

//------------------------------------------------------------------------------
constexpr vtkIdType PointToIncidentFaces[vtkWedge::NumberOfPoints][vtkWedge::MaximumValence] = {
  { 2, 4, 0 }, // 0
  { 0, 3, 2 }, // 1
  { 0, 4, 3 }, // 2
  { 1, 4, 2 }, // 3
  { 2, 3, 1 }, // 4
  { 3, 4, 1 }, // 5
};

//------------------------------------------------------------------------------
constexpr vtkIdType PointToOneRingPoints[vtkWedge::NumberOfPoints][vtkWedge::MaximumValence] = {
  { 1, 3, 2 }, // 0
  { 0, 2, 4 }, // 1
  { 1, 0, 5 }, // 2
  { 4, 5, 0 }, // 3
  { 3, 1, 5 }, // 4
  { 4, 2, 3 }, // 5
};

//------------------------------------------------------------------------------
constexpr vtkIdType NumberOfPointsInFace[vtkWedge::NumberOfFaces] = {
  3, // 0
  3, // 1
  4, // 2
  4, // 3
  4, // 4
};

constexpr double VTK_DIVERGED = 1.e6;
constexpr int VTK_MAX_ITERATIONS = 20;
constexpr double VTK_CONVERGED = 1.e-04;
}

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkWedge);

//------------------------------------------------------------------------------
// Construct the wedge with six points.
vtkWedge::vtkWedge()
{
  this->Points->SetNumberOfPoints(6);
  this->PointIds->SetNumberOfIds(6);

  for (int i = 0; i < 6; i++)
  {
    this->Points->SetPoint(i, 0.0, 0.0, 0.0);
    this->PointIds->SetId(i, 0);
  }
  this->Line = vtkSmartPointer<vtkLine>::New();
  this->Triangle = vtkSmartPointer<vtkTriangle>::New();
  this->Quad = vtkSmartPointer<vtkQuad>::New();
}

//------------------------------------------------------------------------------
bool vtkWedge::GetCentroid(double centroid[3]) const
{
  return vtkWedge::ComputeCentroid(this->Points, nullptr, centroid);
}

//------------------------------------------------------------------------------
bool vtkWedge::ComputeCentroid(vtkPoints* points, const vtkIdType* pointIds, double centroid[3])
{
  double p[3];
  centroid[0] = centroid[1] = centroid[2] = 0.0;
  if (!pointIds)
  {
    vtkTriangle::ComputeCentroid(points, Faces[0], centroid);
    vtkTriangle::ComputeCentroid(points, Faces[1], p);
  }
  else
  {
    vtkIdType facePointsIds[3] = { pointIds[Faces[0][0]], pointIds[Faces[0][1]],
      pointIds[Faces[0][2]] };
    vtkTriangle::ComputeCentroid(points, facePointsIds, centroid);
    facePointsIds[0] = pointIds[Faces[1][0]];
    facePointsIds[1] = pointIds[Faces[1][1]];
    facePointsIds[2] = pointIds[Faces[1][2]];
    vtkTriangle::ComputeCentroid(points, facePointsIds, p);
  }
  centroid[0] += p[0];
  centroid[1] += p[1];
  centroid[2] += p[2];
  centroid[0] *= 0.5;
  centroid[1] *= 0.5;
  centroid[2] *= 0.5;
  return true;
}

//------------------------------------------------------------------------------
bool vtkWedge::IsInsideOut()
{
  double n0[3], n1[3], a[3], b[3], c[3];
  this->Points->GetPoint(0, a);
  this->Points->GetPoint(1, b);
  this->Points->GetPoint(2, c);
  b[0] -= a[0];
  b[1] -= a[1];
  b[2] -= a[2];
  a[0] -= c[0];
  a[1] -= c[1];
  a[2] -= c[2];
  vtkMath::Cross(b, a, n0);
  this->Points->GetPoint(3, a);
  this->Points->GetPoint(4, b);
  this->Points->GetPoint(5, c);
  b[0] -= a[0];
  b[1] -= a[1];
  b[2] -= a[2];
  a[0] -= c[0];
  a[1] -= c[1];
  a[2] -= c[2];
  vtkMath::Cross(b, a, n1);
  return vtkMath::Dot(n0, n1) > 0.0;
}

//------------------------------------------------------------------------------
int vtkWedge::EvaluatePosition(const double x[3], double closestPoint[3], int& subId,
  double pcoords[3], double& dist2, double weights[])
{
  double params[3] = { 0.5, 0.5, 0.5 };
  double derivs[18];

  // Efficient point access
  const auto pointsArray = vtkDoubleArray::FastDownCast(this->Points->GetData());
  if (!pointsArray)
  {
    vtkErrorMacro(<< "Points should be double type");
    return 0;
  }
  const double* pts = pointsArray->GetPointer(0);
  const double *pt0, *pt1;

  // compute a bound on the volume to get a scale for an acceptable determinant
  double longestEdge = 0;
  for (int i = 0; i < 9; i++)
  {
    pt0 = pts + 3 * Edges[i][0];
    pt1 = pts + 3 * Edges[i][1];
    double d2 = vtkMath::Distance2BetweenPoints(pt0, pt1);
    longestEdge = std::max(longestEdge, d2);
  }
  // longestEdge value is already squared
  double volumeBound = longestEdge * std::sqrt(longestEdge);
  double determinantTolerance = 1e-20 < .00001 * volumeBound ? 1e-20 : .00001 * volumeBound;

  //  set initial position for Newton's method
  subId = 0;
  pcoords[0] = pcoords[1] = pcoords[2] = 0.5;

  //  enter iteration loop
  int converged = 0;
  for (int iteration = 0; !converged && (iteration < VTK_MAX_ITERATIONS); iteration++)
  {
    //  calculate element interpolation functions and derivatives
    vtkWedge::InterpolationFunctions(pcoords, weights);
    vtkWedge::InterpolationDerivs(pcoords, derivs);

    //  calculate newton functions
    double fcol[3] = { 0, 0, 0 }, rcol[3] = { 0, 0, 0 }, scol[3] = { 0, 0, 0 },
           tcol[3] = { 0, 0, 0 };
    for (int i = 0; i < 6; i++)
    {
      for (int j = 0; j < 3; j++)
      {
        const double coord = pts[3 * i + j];
        fcol[j] += coord * weights[i];
        rcol[j] += coord * derivs[i];
        scol[j] += coord * derivs[i + 6];
        tcol[j] += coord * derivs[i + 12];
      }
    }

    for (int i = 0; i < 3; i++)
    {
      fcol[i] -= x[i];
    }

    //  compute determinants and generate improvements
    double d = vtkMath::Determinant3x3(rcol, scol, tcol);
    if (std::abs(d) < determinantTolerance)
    {
      vtkDebugMacro(<< "Determinant incorrect, iteration " << iteration);
      return -1;
    }

    pcoords[0] = params[0] - vtkMath::Determinant3x3(fcol, scol, tcol) / d;
    pcoords[1] = params[1] - vtkMath::Determinant3x3(rcol, fcol, tcol) / d;
    pcoords[2] = params[2] - vtkMath::Determinant3x3(rcol, scol, fcol) / d;

    //  check for convergence
    if (std::abs(pcoords[0] - params[0]) < VTK_CONVERGED &&
      std::abs(pcoords[1] - params[1]) < VTK_CONVERGED &&
      std::abs(pcoords[2] - params[2]) < VTK_CONVERGED)
    {
      converged = 1;
    }
    // Test for bad divergence (S.Hirschberg 11.12.2001)
    else if (std::abs(pcoords[0]) > VTK_DIVERGED || std::abs(pcoords[1]) > VTK_DIVERGED ||
      std::abs(pcoords[2]) > VTK_DIVERGED)
    {
      return -1;
    }
    //  if not converged, repeat
    else
    {
      params[0] = pcoords[0];
      params[1] = pcoords[1];
      params[2] = pcoords[2];
    }
  }

  //  if not converged, set the parametric coordinates to arbitrary values
  //  outside of element
  if (!converged)
  {
    return -1;
  }

  vtkWedge::InterpolationFunctions(pcoords, weights);

  if (pcoords[0] >= -0.001 && pcoords[0] <= 1.001 && pcoords[1] >= -0.001 && pcoords[1] <= 1.001 &&
    pcoords[2] >= -0.001 && pcoords[2] <= 1.001 && pcoords[0] + pcoords[1] <= 1.001)
  {
    if (closestPoint)
    {
      closestPoint[0] = x[0];
      closestPoint[1] = x[1];
      closestPoint[2] = x[2];
      dist2 = 0.0; // inside wedge
    }
    return 1;
  }
  else
  {
    double pc[3], w[6];
    if (closestPoint)
    {
      for (int i = 0; i < 3; i++) // only approximate, not really true for warped hexa
      {
        if (pcoords[i] < 0.0)
        {
          pc[i] = 0.0;
        }
        else if (pcoords[i] > 1.0)
        {
          pc[i] = 1.0;
        }
        else
        {
          pc[i] = pcoords[i];
        }
      }
      this->EvaluateLocation(subId, pc, closestPoint, static_cast<double*>(w));
      dist2 = vtkMath::Distance2BetweenPoints(closestPoint, x);
    }
    return 0;
  }
}

//------------------------------------------------------------------------------
void vtkWedge::EvaluateLocation(
  int& vtkNotUsed(subId), const double pcoords[3], double x[3], double* weights)
{
  vtkWedge::InterpolationFunctions(pcoords, weights);

  // Efficient point access
  const auto pointsArray = vtkDoubleArray::FastDownCast(this->Points->GetData());
  if (!pointsArray)
  {
    vtkErrorMacro(<< "Points should be double type");
    return;
  }
  const double* pts = pointsArray->GetPointer(0);

  x[0] = x[1] = x[2] = 0.0;
  for (int i = 0; i < 6; i++)
  {
    const double* pt = pts + 3 * i;
    for (int j = 0; j < 3; j++)
    {
      x[j] += pt[j] * weights[i];
    }
  }
}

//------------------------------------------------------------------------------
// Returns the closest face to the point specified. Closeness is measured
// parametrically.
int vtkWedge::CellBoundary(int vtkNotUsed(subId), const double pcoords[3], vtkIdList* pts)
{

  // define 9 planes that separate regions
  static double normals[9][3] = { { 0.0, 0.83205, -0.5547 }, { -0.639602, -0.639602, -0.426401 },
    { 0.83205, 0.0, -0.5547 }, { 0.0, 0.83205, 0.5547 }, { -0.639602, -0.639602, 0.426401 },
    { 0.83205, 0.0, 0.5547 }, { -0.707107, 0.707107, 0.0 }, { 0.447214, 0.894427, 0.0 },
    { 0.894427, 0.447214, 0.0 } };
  static double point[3] = { 0.333333, 0.333333, 0.5 };
  double vals[9];

  // evaluate 9 plane equations
  for (int i = 0; i < 9; i++)
  {
    vals[i] = normals[i][0] * (pcoords[0] - point[0]) + normals[i][1] * (pcoords[1] - point[1]) +
      normals[i][2] * (pcoords[2] - point[2]);
  }

  // compare against nine planes in parametric space that divide element
  // into five pieces (each corresponding to a face).
  if (vals[0] >= 0.0 && vals[1] >= 0.0 && vals[2] >= 0.0)
  {
    pts->SetNumberOfIds(3); // triangle face
    pts->SetId(0, this->PointIds->GetId(0));
    pts->SetId(1, this->PointIds->GetId(1));
    pts->SetId(2, this->PointIds->GetId(2));
  }

  else if (vals[3] >= 0.0 && vals[4] >= 0.0 && vals[5] >= 0.0)
  {
    pts->SetNumberOfIds(3); // triangle face
    pts->SetId(0, this->PointIds->GetId(3));
    pts->SetId(1, this->PointIds->GetId(4));
    pts->SetId(2, this->PointIds->GetId(5));
  }

  else if (vals[0] <= 0.0 && vals[3] <= 0.0 && vals[6] <= 0.0 && vals[7] <= 0.0)
  {
    pts->SetNumberOfIds(4); // quad face
    pts->SetId(0, this->PointIds->GetId(0));
    pts->SetId(1, this->PointIds->GetId(1));
    pts->SetId(2, this->PointIds->GetId(4));
    pts->SetId(3, this->PointIds->GetId(3));
  }

  else if (vals[1] <= 0.0 && vals[4] <= 0.0 && vals[7] >= 0.0 && vals[8] >= 0.0)
  {
    pts->SetNumberOfIds(4); // quad face
    pts->SetId(0, this->PointIds->GetId(1));
    pts->SetId(1, this->PointIds->GetId(2));
    pts->SetId(2, this->PointIds->GetId(5));
    pts->SetId(3, this->PointIds->GetId(4));
  }

  else // vals[2] <= 0.0 && vals[5] <= 0.0 && vals[8] <= 0.0 && vals[6] >= 0.0
  {
    pts->SetNumberOfIds(4); // quad face
    pts->SetId(0, this->PointIds->GetId(2));
    pts->SetId(1, this->PointIds->GetId(0));
    pts->SetId(2, this->PointIds->GetId(3));
    pts->SetId(3, this->PointIds->GetId(5));
  }

  if (pcoords[0] < 0.0 || pcoords[0] > 1.0 || pcoords[1] < 0.0 || pcoords[1] > 1.0 ||
    pcoords[2] < 0.0 || pcoords[2] > 1.0)
  {
    return 0;
  }
  else
  {
    return 1;
  }
}

//------------------------------------------------------------------------------
void vtkWedge::Contour(double value, vtkDataArray* cellScalars, vtkIncrementalPointLocator* locator,
  vtkCellArray* verts, vtkCellArray* lines, vtkCellArray* polys, vtkPointData* inPd,
  vtkPointData* outPd, vtkCellData* inCd, vtkIdType cellId, vtkCellData* outCd)
{
  static const int CASE_MASK[6] = { 1, 2, 4, 8, 16, 32 };
  int v1, v2;
  vtkIdType pts[3];
  double x1[3], x2[3], x[3];
  vtkIdType offset = verts->GetNumberOfCells() + lines->GetNumberOfCells();

  // Build the case table
  int caseIndex = 0;
  for (int i = 0; i < 6; i++)
  {
    if (cellScalars->GetComponent(i, 0) >= value)
    {
      caseIndex |= CASE_MASK[i];
    }
  }

  const int* edge = vtkMarchingCellsContourCases::GetWedgeCase(caseIndex);

  for (; edge[0] > -1; edge += 3)
  {
    for (int i = 0; i < 3; i++) // insert triangle
    {
      const vtkIdType* vert = Edges[edge[i]];

      // calculate a preferred interpolation direction
      double deltaScalar =
        (cellScalars->GetComponent(vert[1], 0) - cellScalars->GetComponent(vert[0], 0));
      if (deltaScalar > 0)
      {
        v1 = vert[0];
        v2 = vert[1];
      }
      else
      {
        v1 = vert[1];
        v2 = vert[0];
        deltaScalar = -deltaScalar;
      }

      // linear interpolation
      double t =
        (deltaScalar == 0.0 ? 0.0 : (value - cellScalars->GetComponent(v1, 0)) / deltaScalar);

      this->Points->GetPoint(v1, x1);
      this->Points->GetPoint(v2, x2);

      for (int j = 0; j < 3; j++)
      {
        x[j] = x1[j] + t * (x2[j] - x1[j]);
      }
      if (locator->InsertUniquePoint(x, pts[i]))
      {
        if (outPd)
        {
          vtkIdType p1 = this->PointIds->GetId(v1);
          vtkIdType p2 = this->PointIds->GetId(v2);
          outPd->InterpolateEdge(inPd, pts[i], p1, p2, t);
        }
      }
    }
    // check for degenerate triangle
    if (pts[0] != pts[1] && pts[0] != pts[2] && pts[1] != pts[2])
    {
      const vtkIdType newCellId = offset + polys->InsertNextCell(3, pts);
      if (outCd)
      {
        outCd->CopyData(inCd, cellId, newCellId);
      }
    }
  }
}

//------------------------------------------------------------------------------
// Return the case table for table-based isocontouring (aka marching cubes
// style implementations). A linear 3D cell with N vertices will have 2**N
// cases. The cases list three Edges in order to produce one output triangle.
int* vtkWedge::GetTriangleCases(int caseId)
{
  return const_cast<int*>(vtkMarchingCellsContourCases::GetWedgeCase(caseId));
}

//------------------------------------------------------------------------------
void vtkWedge::Clip(double value, vtkDataArray* cellScalars, vtkIncrementalPointLocator* locator,
  vtkCellArray* connectivity, vtkPointData* inPd, vtkPointData* outPd, vtkCellData* inCd,
  vtkIdType cellId, vtkCellData* outCd, int insideOut)
{
  vtkIdType pts[8];
  double coords[8][3];
  double grdDiffs[6];
  double x[3], x1[3], x2[3];
  vtkIdType centroidIndex = 0;

  // Build the case table
  uint8_t caseIndex = 0;
  for (int pointId = 0; pointId < 6; ++pointId)
  {
    grdDiffs[pointId] = cellScalars->GetComponent(pointId, 0) - value;
    caseIndex |= (grdDiffs[pointId] >= 0.0) << pointId;
  }
  const uint8_t* thisCase = insideOut
    ? vtkMarchingCellsClipCases<true>::GetCellCase(VTK_WEDGE, caseIndex)
    : vtkMarchingCellsClipCases<false>::GetCellCase(VTK_WEDGE, caseIndex);
  using MCCases = vtkMarchingCellsClipCasesBase;
  const MCCases::EDGEIDXS* edgeVertices = MCCases::GetCellEdges(VTK_WEDGE);
  const uint8_t numberOfOutputCells = *thisCase++;

  for (uint8_t outputCellId = 0; outputCellId < numberOfOutputCells; ++outputCellId)
  {
    const uint8_t shape = *thisCase++;
    const uint8_t numberOfCellPoints = *thisCase++;
    for (int i = 0; i < numberOfCellPoints; ++i)
    {
      const uint8_t pointIndex = *thisCase++;
      if (pointIndex <= MCCases::P7) // Input Point
      {
        this->Points->GetPoint(pointIndex, coords[i]);
        if (locator->InsertUniquePoint(coords[i], pts[i]))
        {
          if (outPd)
          {
            outPd->CopyData(inPd, this->PointIds->GetId(pointIndex), pts[i]);
          }
        }
      }
      else if (pointIndex <= MCCases::EL) // Edge point
      {
        const auto& edgePoints = edgeVertices[pointIndex - MCCases::EA];
        uint8_t point1Index = edgePoints[0];
        uint8_t point2Index = edgePoints[1];
        double point1ToPoint2 = grdDiffs[point2Index] - grdDiffs[point1Index];
        if (point1ToPoint2 < 0)
        {
          std::swap(point1Index, point2Index);
          point1ToPoint2 = -point1ToPoint2;
        }
        const double point1ToIso = 0.0 - grdDiffs[point1Index];
        const double t = point1ToPoint2 != 0 ? point1ToIso / point1ToPoint2 : 0;
        this->Points->GetPoint(point1Index, x1);
        this->Points->GetPoint(point2Index, x2);
        for (int j = 0; j < 3; j++)
        {
          coords[i][j] = x1[j] + t * (x2[j] - x1[j]);
        }

        if (locator->InsertUniquePoint(coords[i], pts[i]))
        {
          if (outPd)
          {
            vtkIdType pointIndex1 = this->PointIds->GetId(point1Index);
            vtkIdType pointIndex2 = this->PointIds->GetId(point2Index);
            outPd->InterpolateEdge(inPd, pts[i], pointIndex1, pointIndex2, t);
          }
        }
      }
      else // centroid point
      {
        pts[i] = centroidIndex;
      }
    }
    if (shape != VTK_EMPTY_CELL) // normal cell
    {
      const vtkIdType newCellId = connectivity->InsertNextCell(numberOfCellPoints, pts);
      if (outCd)
      {
        outCd->CopyData(inCd, cellId, newCellId);
      }
    }
    else // centroid
    {
      x[0] = x[1] = x[2] = 0.0;
      double weightFactor = 1.0 / numberOfCellPoints;
      double weights[8];
      for (int i = 0; i < numberOfCellPoints; ++i)
      {
        x[0] += coords[i][0];
        x[1] += coords[i][1];
        x[2] += coords[i][2];
        weights[i] = weightFactor;
      }
      x[0] *= weightFactor;
      x[1] *= weightFactor;
      x[2] *= weightFactor;
      if (locator->InsertUniquePoint(x, centroidIndex))
      {
        if (outPd)
        {
          vtkNew<vtkIdList> idList;
          idList->SetList(pts, numberOfCellPoints, /*save*/ true);
          outPd->InterpolatePoint(outPd, centroidIndex, idList, weights);
        }
      }
    }
  }
}

//------------------------------------------------------------------------------
vtkCell* vtkWedge::GetEdge(int edgeId)
{
  const vtkIdType* verts = Edges[edgeId];

  // load point id's
  this->Line->PointIds->SetId(0, this->PointIds->GetId(verts[0]));
  this->Line->PointIds->SetId(1, this->PointIds->GetId(verts[1]));

  // load coordinates
  this->Line->Points->SetPoint(0, this->Points->GetPoint(verts[0]));
  this->Line->Points->SetPoint(1, this->Points->GetPoint(verts[1]));

  return this->Line;
}

//------------------------------------------------------------------------------
vtkCell* vtkWedge::GetFace(int faceId)
{
  const vtkIdType* verts = Faces[faceId];

  if (verts[3] != -1) // quad cell
  {
    // load point id's
    this->Quad->PointIds->SetId(0, this->PointIds->GetId(verts[0]));
    this->Quad->PointIds->SetId(1, this->PointIds->GetId(verts[1]));
    this->Quad->PointIds->SetId(2, this->PointIds->GetId(verts[2]));
    this->Quad->PointIds->SetId(3, this->PointIds->GetId(verts[3]));

    // load coordinates
    this->Quad->Points->SetPoint(0, this->Points->GetPoint(verts[0]));
    this->Quad->Points->SetPoint(1, this->Points->GetPoint(verts[1]));
    this->Quad->Points->SetPoint(2, this->Points->GetPoint(verts[2]));
    this->Quad->Points->SetPoint(3, this->Points->GetPoint(verts[3]));

    return this->Quad;
  }
  else
  {
    // load point id's
    this->Triangle->PointIds->SetId(0, this->PointIds->GetId(verts[0]));
    this->Triangle->PointIds->SetId(1, this->PointIds->GetId(verts[1]));
    this->Triangle->PointIds->SetId(2, this->PointIds->GetId(verts[2]));

    // load coordinates
    this->Triangle->Points->SetPoint(0, this->Points->GetPoint(verts[0]));
    this->Triangle->Points->SetPoint(1, this->Points->GetPoint(verts[1]));
    this->Triangle->Points->SetPoint(2, this->Points->GetPoint(verts[2]));

    return this->Triangle;
  }
}

//------------------------------------------------------------------------------
const vtkIdType* vtkWedge::GetEdgeArray(vtkIdType edgeId)
{
  assert(edgeId < vtkWedge::NumberOfEdges && "edgeId too large");
  return Edges[edgeId];
}

//------------------------------------------------------------------------------
const vtkIdType* vtkWedge::GetFaceArray(vtkIdType faceId)
{
  assert(faceId < vtkWedge::NumberOfFaces && "faceId too large");
  return Faces[faceId];
}

//------------------------------------------------------------------------------
const vtkIdType* vtkWedge::GetEdgeToAdjacentFacesArray(vtkIdType edgeId)
{
  assert(edgeId < vtkWedge::NumberOfEdges && "edgeId too large");
  return EdgeToAdjacentFaces[edgeId];
}

//------------------------------------------------------------------------------
const vtkIdType* vtkWedge::GetFaceToAdjacentFacesArray(vtkIdType faceId)
{
  assert(faceId < vtkWedge::NumberOfFaces && "faceId too large");
  return FaceToAdjacentFaces[faceId];
}

//------------------------------------------------------------------------------
const vtkIdType* vtkWedge::GetPointToIncidentEdgesArray(vtkIdType pointId)
{
  assert(pointId < vtkWedge::NumberOfPoints && "pointId too large");
  return PointToIncidentEdges[pointId];
}

//------------------------------------------------------------------------------
const vtkIdType* vtkWedge::GetPointToIncidentFacesArray(vtkIdType pointId)
{
  assert(pointId < vtkWedge::NumberOfPoints && "pointId too large");
  return PointToIncidentFaces[pointId];
}

//------------------------------------------------------------------------------
const vtkIdType* vtkWedge::GetPointToOneRingPointsArray(vtkIdType pointId)
{
  assert(pointId < vtkWedge::NumberOfPoints && "pointId too large");
  return PointToOneRingPoints[pointId];
}

//------------------------------------------------------------------------------
void vtkWedge::GetEdgePoints(vtkIdType edgeId, const vtkIdType*& pts)
{
  assert(edgeId < vtkWedge::NumberOfEdges && "edgeId too large");
  pts = this->GetEdgeArray(edgeId);
}

//------------------------------------------------------------------------------
vtkIdType vtkWedge::GetFacePoints(vtkIdType faceId, const vtkIdType*& pts)
{
  assert(faceId < vtkWedge::NumberOfFaces && "faceId too large");
  pts = this->GetFaceArray(faceId);
  return NumberOfPointsInFace[faceId];
}

//------------------------------------------------------------------------------
void vtkWedge::GetEdgeToAdjacentFaces(vtkIdType edgeId, const vtkIdType*& pts)
{
  assert(edgeId < vtkWedge::NumberOfEdges && "edgeId too large");
  pts = EdgeToAdjacentFaces[edgeId];
}

//------------------------------------------------------------------------------
vtkIdType vtkWedge::GetFaceToAdjacentFaces(vtkIdType faceId, const vtkIdType*& faceIds)
{
  assert(faceId < vtkWedge::NumberOfFaces && "faceId too large");
  faceIds = FaceToAdjacentFaces[faceId];
  return NumberOfPointsInFace[faceId];
}

//------------------------------------------------------------------------------
vtkIdType vtkWedge::GetPointToIncidentEdges(vtkIdType pointId, const vtkIdType*& edgeIds)
{
  assert(pointId < vtkWedge::NumberOfPoints && "pointId too large");
  edgeIds = PointToIncidentEdges[pointId];
  return vtkWedge::MaximumValence;
}

//------------------------------------------------------------------------------
vtkIdType vtkWedge::GetPointToIncidentFaces(vtkIdType pointId, const vtkIdType*& faceIds)
{
  assert(pointId < vtkWedge::NumberOfPoints && "pointId too large");
  faceIds = PointToIncidentFaces[pointId];
  return vtkWedge::MaximumValence;
}

//------------------------------------------------------------------------------
vtkIdType vtkWedge::GetPointToOneRingPoints(vtkIdType pointId, const vtkIdType*& pts)
{
  assert(pointId < vtkWedge::NumberOfPoints && "pointId too large");
  pts = PointToOneRingPoints[pointId];
  return vtkWedge::MaximumValence;
}

//------------------------------------------------------------------------------
// Intersect Faces against line.
//
int vtkWedge::IntersectWithLine(const double p1[3], const double p2[3], double tol, double& t,
  double x[3], double pcoords[3], int& subId)
{
  int intersection = 0;
  double pt1[3], pt2[3], pt3[3], pt4[3];
  double tTemp;
  double pc[3], xTemp[3];
  int faceNum;

  t = VTK_DOUBLE_MAX;

  // first intersect the triangle Faces
  for (faceNum = 0; faceNum < 2; faceNum++)
  {
    this->Points->GetPoint(Faces[faceNum][0], pt1);
    this->Points->GetPoint(Faces[faceNum][1], pt2);
    this->Points->GetPoint(Faces[faceNum][2], pt3);

    this->Triangle->Points->SetPoint(0, pt1);
    this->Triangle->Points->SetPoint(1, pt2);
    this->Triangle->Points->SetPoint(2, pt3);

    if (this->Triangle->IntersectWithLine(p1, p2, tol, tTemp, xTemp, pc, subId))
    {
      intersection = 1;
      if (tTemp < t)
      {
        t = tTemp;
        x[0] = xTemp[0];
        x[1] = xTemp[1];
        x[2] = xTemp[2];
        switch (faceNum)
        {
          case 0:
            pcoords[0] = pc[0];
            pcoords[1] = pc[1];
            pcoords[2] = 0.0;
            break;

          case 1:
            pcoords[0] = pc[0];
            pcoords[1] = pc[1];
            pcoords[2] = 1.0;
            break;
        }
      }
    }
  }

  // now intersect the quad Faces
  for (faceNum = 2; faceNum < 5; faceNum++)
  {
    this->Points->GetPoint(Faces[faceNum][0], pt1);
    this->Points->GetPoint(Faces[faceNum][1], pt2);
    this->Points->GetPoint(Faces[faceNum][2], pt3);
    this->Points->GetPoint(Faces[faceNum][3], pt4);

    this->Quad->Points->SetPoint(0, pt1);
    this->Quad->Points->SetPoint(1, pt2);
    this->Quad->Points->SetPoint(2, pt3);
    this->Quad->Points->SetPoint(3, pt4);

    if (this->Quad->IntersectWithLine(p1, p2, tol, tTemp, xTemp, pc, subId))
    {
      intersection = 1;
      if (tTemp < t)
      {
        t = tTemp;
        x[0] = xTemp[0];
        x[1] = xTemp[1];
        x[2] = xTemp[2];
        switch (faceNum)
        {
          case 2:
            pcoords[0] = pc[1];
            pcoords[1] = 0.0;
            pcoords[2] = pc[0];
            break;

          case 3:
            pcoords[0] = 1.0 - pc[1];
            pcoords[1] = pc[1];
            pcoords[2] = pc[0];
            break;

          case 4:
            pcoords[0] = 0.0;
            pcoords[1] = pc[1];
            pcoords[2] = pc[0];
            break;
        }
      }
    }
  }

  return intersection;
}

//------------------------------------------------------------------------------
int vtkWedge::TriangulateLocalIds(int vtkNotUsed(index), vtkIdList* ptIds)
{
  // one wedge (or prism) is decomposed into 3 tetrahedrons
  // and four pairs of (pointId, pointCoordinates) are provided for each tetrahedron
  ptIds->SetNumberOfIds(12);
  constexpr vtkIdType ids[3][4] = { { 0, 1, 2, 3 }, { 1, 4, 5, 3 }, { 1, 3, 5, 2 } };
  std::copy_n(&ids[0][0], 12, ptIds->begin());
  return 1;
}

//------------------------------------------------------------------------------
void vtkWedge::Derivatives(
  int vtkNotUsed(subId), const double pcoords[3], const double* values, int dim, double* derivs)
{
  double *jI[3], j0[3], j1[3], j2[3];
  double functionDerivs[18], sum[3];

  // compute inverse Jacobian and interpolation function derivatives
  jI[0] = j0;
  jI[1] = j1;
  jI[2] = j2;
  this->JacobianInverse(pcoords, jI, functionDerivs);

  // now compute derivates of values provided
  for (int k = 0; k < dim; k++) // loop over values per point
  {
    sum[0] = sum[1] = sum[2] = 0.0;
    for (int i = 0; i < 6; i++) // loop over interp. function derivatives
    {
      double value = values[dim * i + k];
      sum[0] += functionDerivs[i] * value;
      sum[1] += functionDerivs[6 + i] * value;
      sum[2] += functionDerivs[12 + i] * value;
    }

    for (int j = 0; j < 3; j++) // loop over derivative directions
    {
      derivs[3 * k + j] = sum[0] * jI[j][0] + sum[1] * jI[j][1] + sum[2] * jI[j][2];
    }
  }
}

//------------------------------------------------------------------------------
// Compute iso-parametric interpolation functions
//
void vtkWedge::InterpolationFunctions(const double pcoords[3], double sf[6])
{
  sf[0] = (1.0 - pcoords[0] - pcoords[1]) * (1.0 - pcoords[2]);
  sf[1] = pcoords[0] * (1.0 - pcoords[2]);
  sf[2] = pcoords[1] * (1.0 - pcoords[2]);
  sf[3] = (1.0 - pcoords[0] - pcoords[1]) * pcoords[2];
  sf[4] = pcoords[0] * pcoords[2];
  sf[5] = pcoords[1] * pcoords[2];
}

//------------------------------------------------------------------------------
void vtkWedge::InterpolationDerivs(const double pcoords[3], double derivs[18])
{
  // r-derivatives
  derivs[0] = -1.0 + pcoords[2];
  derivs[1] = 1.0 - pcoords[2];
  derivs[2] = 0.0;
  derivs[3] = -pcoords[2];
  derivs[4] = pcoords[2];
  derivs[5] = 0.0;

  // s-derivatives
  derivs[6] = -1.0 + pcoords[2];
  derivs[7] = 0.0;
  derivs[8] = 1.0 - pcoords[2];
  derivs[9] = -pcoords[2];
  derivs[10] = 0.0;
  derivs[11] = pcoords[2];

  // t-derivatives
  derivs[12] = -1.0 + pcoords[0] + pcoords[1];
  derivs[13] = -pcoords[0];
  derivs[14] = -pcoords[1];
  derivs[15] = 1.0 - pcoords[0] - pcoords[1];
  derivs[16] = pcoords[0];
  derivs[17] = pcoords[1];
}

//------------------------------------------------------------------------------
// Given parametric coordinates compute inverse Jacobian transformation
// matrix. Returns 9 elements of 3x3 inverse Jacobian plus interpolation
// function derivatives. Returns 0 if no inverse exists.
int vtkWedge::JacobianInverse(const double pcoords[3], double** inverse, double derivs[18])
{
  double *m[3], m0[3], m1[3], m2[3];
  double x[3];

  // compute interpolation function derivatives
  this->InterpolationDerivs(pcoords, derivs);

  // create Jacobian matrix
  m[0] = m0;
  m[1] = m1;
  m[2] = m2;
  for (int i = 0; i < 3; i++) // initialize matrix
  {
    m0[i] = m1[i] = m2[i] = 0.0;
  }

  for (int j = 0; j < 6; j++)
  {
    this->Points->GetPoint(j, x);
    for (int i = 0; i < 3; i++)
    {
      m0[i] += x[i] * derivs[j];
      m1[i] += x[i] * derivs[6 + j];
      m2[i] += x[i] * derivs[12 + j];
    }
  }

  // now find the inverse
  if (vtkMath::InvertMatrix(m, inverse, 3) == 0)
  {
    vtkErrorMacro(<< "Jacobian inverse not found: "
                  << "Matrix:" << m[0][0] << " " << m[0][1] << " " << m[0][2] << " " << m[1][0]
                  << " " << m[1][1] << " " << m[1][2] << " " << m[2][0] << " " << m[2][1] << " "
                  << m[2][2]);
    return 0;
  }

  return 1;
}

//------------------------------------------------------------------------------
double* vtkWedge::GetParametricCoords()
{
  return ParametricCoords;
}

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

  os << indent << "Line:\n";
  this->Line->PrintSelf(os, indent.GetNextIndent());
  os << indent << "Triangle:\n";
  this->Triangle->PrintSelf(os, indent.GetNextIndent());
  os << indent << "Quad:\n";
  this->Quad->PrintSelf(os, indent.GetNextIndent());
}
VTK_ABI_NAMESPACE_END
