//====================================================================
//#
//#  pfCAVEUGrid.cxx                                   #####     
//#                                                   ##   ##
//#  Author(s): Paul Rajlich                          ##
//#  Email: prajlich@ncsa.uiuc.ed                     ##   ##
//#  Date created:                                     #####
//#
//#  Description: example showing how to use vtkGeometryFilter rather
//#    than vtkDataSetMapper. Based on UGrid.cxx from VTK distribution
//#
//====================================================================

#include <pfcave.h>
#include <stdlib.h>
#include <Performer/pf/pfLightSource.h>
#include <Performer/pf/pfDCS.h>

#include "vtkPoints.h"
#include "vtkUnstructuredGrid.h"
#include "vtkDataSetMapper.h"

#include "vtkActorToPFTranslator.h"

int main( int argc, char *argv[] )
{
  int i;
  static float x[27][3]={{0,0,0}, {1,0,0}, {2,0,0}, {0,1,0}, {1,1,0}, {2,1,0},
                         {0,0,1}, {1,0,1}, {2,0,1}, {0,1,1}, {1,1,1}, {2,1,1},
                         {0,1,2}, {1,1,2}, {2,1,2}, {0,1,3}, {1,1,3}, {2,1,3},
                         {0,1,4}, {1,1,4}, {2,1,4}, {0,1,5}, {1,1,5}, {2,1,5},
                         {0,1,6}, {1,1,6}, {2,1,6}};
  static int pts[12][8]={{0, 1, 4, 3, 6, 7, 10, 9},
                         {1, 2, 5, 4, 7, 8, 11, 10},
                         {6, 10, 9, 12, 0, 0, 0, 0},
                         {8, 11, 10, 14, 0, 0, 0, 0},
                         {16, 17, 14, 13, 12, 15, 0, 0},
                         {18, 15, 19, 16, 20, 17, 0, 0},
                         {22, 23, 20, 19, 0, 0, 0, 0},
                         {21, 22, 18, 0, 0, 0, 0, 0},
                         {22, 19, 18, 0, 0, 0, 0, 0},
                         {23, 26, 0, 0, 0, 0, 0, 0},
                         {21, 24, 0, 0, 0, 0, 0, 0},
                         {25, 0, 0, 0, 0, 0, 0, 0}};

  pfInit();
  pfCAVEConfig(&argc, argv, NULL);
  pfConfig();
  pfCAVEInitChannels();

  pfScene *scene;
  pfGeoState *gstate;
  pfLightSource *light;
  scene = new pfScene;
  gstate = new pfGeoState;
  gstate->setMode(PFSTATE_ENLIGHTING, PF_ON);
  gstate->setMode(PFSTATE_CULLFACE, PFCF_OFF);
  scene->setGState(gstate);

  light = new pfLightSource;
  light->setColor(PFLT_DIFFUSE, 1.0, 1.0, 1.0);
  light->setAmbient(0.3, 0.3, 0.3);
  scene->addChild(light);

  // --- vtk pipeline ---
  
  vtkPoints *points = vtkPoints::New();
  for (i=0; i<27; i++) points->InsertPoint(i,x[i]);
  
  vtkUnstructuredGrid *ugrid = vtkUnstructuredGrid::New();
    ugrid->Allocate(100);
    ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, pts[0]);
    ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, pts[1]);
    ugrid->InsertNextCell(VTK_TETRA, 4, pts[2]);
    ugrid->InsertNextCell(VTK_TETRA, 4, pts[3]);
    ugrid->InsertNextCell(VTK_POLYGON, 6, pts[4]);
    ugrid->InsertNextCell(VTK_TRIANGLE_STRIP, 6, pts[5]);
    ugrid->InsertNextCell(VTK_QUAD, 4, pts[6]);
    ugrid->InsertNextCell(VTK_TRIANGLE, 3, pts[7]);
    ugrid->InsertNextCell(VTK_TRIANGLE, 3, pts[8]);
    ugrid->InsertNextCell(VTK_LINE, 2, pts[9]);
    ugrid->InsertNextCell(VTK_LINE, 2, pts[10]);
    ugrid->InsertNextCell(VTK_VERTEX, 1, pts[11]);

  ugrid->SetPoints(points);
  points->Delete();

  // NOTE: vtkActorToPF will fail if the vtkActor uses a vtkDataSetMapper.
  // Well, what is a vtkDataSetMapper?  A vtkDataSetMapper is simply a
  // combination of a vtkGeometryFilter and a vtkPolyDataMapper. The
  // reason why this crashes vtkActorToPF is that the vtkPolyDataMapper is
  // not created until it is needed (the first call to Render()). Since vtk
  // is not doing the rendering, Render() is never called. In order to get
  //  around this problem, whenever you have a vtkDataSetMapper in your
  //  pipeline, simply replace it with a vtkGeometryFilter followed by a
  //  vtkPolyDataMapper. 

  // instead of:
  //vtkDataSetMapper *ugridMapper = vtkDataSetMapper::New();
  //    ugridMapper->SetInput(ugrid);
  //    ugridMapper->ImmediateModeRenderingOn();

  // use this
  vtkGeometryFilter *gf = vtkGeometryFilter::New();
  gf->SetInput(ugrid);
  vtkPolyDataMapper *pm = vtkPolyDataMapper::New();
  pm->SetInput(gf->GetOutput());

  vtkActor *ugridActor = vtkActor::New();
      //ugridActor->SetMapper(ugridMapper);
      ugridActor->SetMapper(pm);
      ugridActor->GetProperty()->SetColor(.8,.8,.8);
      ugridActor->AddPosition(0,0.001,0);
  vtkActor *wireActor = vtkActor::New();
      //wireActor->SetMapper(ugridMapper);
      wireActor->SetMapper(pm);
      wireActor->GetProperty()->SetRepresentationToWireframe();
      wireActor->GetProperty()->SetColor(0,0,0);

  // --- end vtk pipeline ---
  pfDCS *center = new pfDCS;
  center->setRot(0.0f, 0.0f, 0.0f);
  center->setTrans(0.0f, 3.0f, 3.0f);

  vtkActorToPFTranslator *ugridT = vtkActorToPFTranslator::New(ugridActor);
  vtkActorToPFTranslator *wireT = vtkActorToPFTranslator::New(wireActor);
  center->addChild(ugridT->GetGeode());
  center->addChild(wireT->GetGeode());

  scene->addChild(center);
  pfCAVEMasterChan()->setScene(scene);

  while (!CAVEgetbutton(CAVE_ESCKEY)) {  
    pfSync();
    pfCAVEPreFrame();
    pfFrame();
    pfCAVEPostFrame();
  }
  CAVEHalt();
  pfExit();
}
