#include <stdio.h>
#include "GR_misc.h"
#include "GR_events.h"
#include "GR_Face.h"
#include "GR_Cell.h"
#include "GR_BFace.h"
#include "GR_Vertex.h"
#include "GR_SurfMesh.h"
#include "GR_Bdry3D.h"
void vWriteFile_Surface(SurfMesh& OutMesh,
                        const char strBaseFileName[],
                        const char strExtraFileSuffix[])
{
  SUMAA_LOG_EVENT_BEGIN(OUTPUT);
  assert(OutMesh.qSimplicial());
  int iVertOffset = 0, iFaceOffset = 0;
  int iCellOffset = 0, iBFaceOffset = 1;
  FILE *pFOutFile = fopen("/dev/zero", "r");
  char strFileName[1024];
  OutMesh.vPurge();
  sprintf(strFileName, "%s.vtk%s", strBaseFileName, strExtraFileSuffix);

  /*newfile vtk*/
  printf("Opening output file %s\n", strFileName);
  if (pFOutFile) fclose(pFOutFile);
  pFOutFile = fopen(strFileName, "w");
  if (NULL == pFOutFile)
    vFatalError("Couldn't open output file for writing",
                "surface mesh output");

  /*"# vtk DataFile Version" discard_float*/
  fprintf(pFOutFile, "# vtk DataFile Version 1.1.1\n");
  /*discard_line*/
  fprintf(pFOutFile, "GRUMMP-generated surface mesh file\n");
  /*ASCII*/
  fprintf(pFOutFile, "ASCII\n");
  /*DATASET UNSTRUCTURED_GRID*/
  fprintf(pFOutFile, "DATASET UNSTRUCTURED_GRID\n");
  /*POINTS nverts float*/
  fprintf(pFOutFile, "POINTS %d float\n", OutMesh.iNumVerts());

  for (int iV = 0; iV < OutMesh.iNumVerts(); iV++) 
    /*verts: coords*/
    fprintf(pFOutFile, "%.14g %.14g %.14g\n", OutMesh.pVVert(iV)->dX(), OutMesh.pVVert(iV)->dY(), OutMesh.pVVert(iV)->dZ());

  /*"CELLS" ncells discard_integer*/
  fprintf(pFOutFile, "CELLS %d %d\n", OutMesh.iNumCells(),
	  OutMesh.iNumCells()*4);

  for (int iC = 0; iC < OutMesh.iNumCells(); iC++) {
    Cell* pC = OutMesh.pCCell(iC);
    fprintf(pFOutFile, "3 ");
    for (int iV = 0; iV < pC->iNumVerts(); iV++) {
      fprintf(pFOutFile, "%d ", OutMesh.iVertIndex(pC->pVVert(iV))+iVertOffset);
    }
    /*cells: 3 verts*/
    fprintf(pFOutFile, "\n");
  }

  /*CELL_TYPES ncells*/
  fprintf(pFOutFile, "CELL_TYPES %d\n", OutMesh.iNumCells());

  for (int iC = 0; iC < OutMesh.iNumCells(); iC++) {
    Cell* pC = OutMesh.pCCell(iC);
    /*cells: 5*/
    fprintf(pFOutFile, "5\n");
  }

  /* Make sure compilers won't complain that these variables */
  /* aren't used. */
  iVertOffset++;
  iFaceOffset++;
  iBFaceOffset++;
  iCellOffset++;
  fclose(pFOutFile);
  SUMAA_LOG_EVENT_END(OUTPUT);
}
