#include <math.h>
#include "GR_misc.h"
#include "GR_Cell.h"
#include "GR_Face.h"
#include "GR_Vertex.h"
#include "GR_Vec.h"

const Vert* PrismCell::pVVert(const int i) const
{
  assert(iFullCheck());
  assert2(i >= 0 && i <= 5, "Vertex index out of range");
  if (i <= 2) {
    const Face* const pFBase = ppFFaces[0];
    if (i == 0) return (pFBase->pVVert(0));
    if (pFBase->pCCellRight() == static_cast<const Cell*>(this))
      return (pFBase->pVVert(i));
    else
      return (pFBase->pVVert(3-i));
  }
  else {
    const Face* const pFTop = ppFFaces[0];
    if (i == 3) return (pFTop->pVVert(0));
    if (pFTop->pCCellRight() == static_cast<const Cell*>(this))
      return (pFTop->pVVert(i-3));
    else
      return (pFTop->pVVert(6-i));
  }
}

double PrismCell::dSize() const
{
  assert2(0, "No volume calculation yet for prisms");
  assert(iFullCheck());
//   const Vert *pV0, *pV1, *pV2;
//   const Face* const pFBase = ppFFaces[0];
//   pV0 = pFBase->pVVert(0);
//   if (pFBase->pCCellRight() == (const Cell*)this) {
//     pV1 = pFBase->pVVert(1);
//     pV2 = pFBase->pVVert(2);
//   }
//   else {
//     pV1 = pFBase->pVVert(2);
//     pV2 = pFBase->pVVert(1);
//   }

  return (0);
}

bool PrismCell::qIsClosed() const
{
#ifndef NDEBUG
  #warning Prism closure check is weak!
#endif
  for (int iF = iNumFaces() - 1; iF >= 0; iF--) {
    const Face *pF = pFFace(iF);
    for (int iV = pF->iNumVerts() - 1; iV >= 0; iV--) {
      if (!qHasVert(pF->pVVert(iV)))
	return false;
    }
  }
  return true;
}

