//====================================================================
//#
//#  pfCellData.cxx                                    #####     
//#                                                   ##   ##
//#  Author(s): Paul Rajlich                          ##
//#  Email: prajlich@ncsa.uiuc.ed                     ##   ##
//#  Date created:                                     #####
//#
//#  Description: testing vtkActorToPF with cell data.
//#    Based on randomCellColors.tcl from VTK distribtion.
//#
//====================================================================

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

#include "vtkProgrammableAttributeDataFilter.h"
#include "vtkCellDataToPointData.h"
#include "vtkMath.h"
#include "vtkSphereSource.h"

#include "vtkActorToPFTranslator.h"

void colorCells(void *arg) {

  vtkProgrammableAttributeDataFilter *randomColors = 
    (vtkProgrammableAttributeDataFilter *) arg;

  vtkMath *randomColorGenerator = vtkMath::New();
  vtkDataSet *input = randomColors->GetInput();
  vtkDataSet *output = randomColors->GetOutput();
  int numCells = input->GetNumberOfCells();
  vtkScalars *colors = vtkScalars::New();
  colors->SetNumberOfScalars(numCells);


  for (int i=0; i<numCells; i++)
    colors->SetScalar(i, randomColorGenerator->Random(0, 1));

  output->GetCellData()->CopyScalarsOff();
  output->GetCellData()->PassData(input->GetCellData());
  output->GetCellData()->SetScalars(colors);

  colors->Delete(); //reference counting - it's ok
  randomColorGenerator->Delete();
} 
  

int main(int argc, char **argv) {

  pfInit();
  pfMultiprocess(PFMP_DEFAULT);  
  pfConfig();

  pfPipe *p = pfGetPipe(0);
  pfPipeWindow *pw = new pfPipeWindow(p);
  pw->setWinType(PFPWIN_TYPE_X);
  pw->setName("vtkActorToPF Cell Data");
  pw->setOriginSize(100, 100, 500, 500);
  pw->open();

  pfChannel *chan = new pfChannel(p);

  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 ---

  vtkSphereSource *sphere = vtkSphereSource::New();
  sphere->SetThetaResolution(20);
  sphere->SetPhiResolution(40);

  vtkProgrammableAttributeDataFilter *randomColors = vtkProgrammableAttributeDataFilter::New();
  randomColors->SetInput(sphere->GetOutput());
  randomColors->SetExecuteMethod(colorCells, randomColors);

  //vtkCellDataToPointData *moveDataToPoints = vtkCellDataToPointData::New();
  //moveDataToPoints->SetInput(randomColors->GetOutput());

  vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
  mapper->SetInput(randomColors->GetPolyDataOutput());
  //mapper->SetInput(moveDataToPoints->GetPolyDataOutput());
  mapper->SetScalarRange(randomColors->GetPolyDataOutput()->GetScalarRange());
 
  vtkActor *sphereActor = vtkActor::New();
  sphereActor->SetMapper(mapper);

  // --- end vtk pipeline

  pfDCS *center = new pfDCS;
  center->setRot(0.0f, 0.0f, 0.0f);
  center->setTrans(0.0f, 2.0f, 0.0f);

  vtkActorToPFTranslator *sphereT = vtkActorToPFTranslator::New(sphereActor);
  center->addChild(sphereT->GetGeode());
  
  scene->addChild(center);
  chan->setScene(scene);
 
  float t = 0.0f;
  while (t < 15.0f) {
    pfSync();
    pfFrame();
    t = pfGetTime();
    center->setRot(t*20.0f, 0.0f, 0.0f);
  }
  pfExit();
}

  
  


