//====================================================================
//#
//#  pfExampleOOD.cxx                                  #####     
//#                                                   ##   ##
//#  Author(s): Paul Rajlich                          ##
//#  Email: prajlich@ncsa.uiuc.ed                     ##   ##
//#  Date created:                                     #####
//#
//#  Description: Simple Performer example that uses
//#    vtkActorToPfTranslator class
//#
//====================================================================

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

#include "vtkSphereSource.h"
#include "vtkConeSource.h"
#include "vtkGlyph3D.h"
#include "vtkPolyDataNormals.h"

#include "vtkActorToPFTranslator.h"

#ifdef VTK44
typedef double vtkReal;
#else
typedef float vtkReal;
#endif

#ifdef VTK4
#include "vtkProperty.h"
#include "vtkDataSet.h"
#endif

static void createScene(pfChannel *, int, char **);
void updateVTK(float t);

// globals...
vtkSphereSource *sphere;

// normal pf application - nothing weird here!
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");
  pw->setOriginSize(100, 100, 500, 500);
  pw->open();

  pfChannel *chan = new pfChannel(p);
  createScene(chan, argc, argv);

  // enable sorting in bins... used so that transparent objects rendered last
  chan->setTravMode(PFTRAV_CULL, PFCULL_SORT);

  float t = 0.0;
  while(t < 10.0f) {
    updateVTK(t);
    pfSync();
    pfFrame();

    t = pfGetTime();
  }

  pfExit();
  return 0;
}

void updateVTK(float t) {
  float r;
  if (t < 5.0) 
    r = 0.5 + t/4.0;
  else
    r = 0.5 + (10.0-t)/4.0;

  fprintf(stderr, "NEW RADIUS: %f\n", r);
  sphere->SetRadius(r);
}

static void createScene(pfChannel *chan, int argc, char **argv) {
  pfScene *scene;
  pfGeoState *gstate;
  pfLightSource *light;
  scene = new pfScene;
  gstate = new pfGeoState;
  gstate->setMode(PFSTATE_ENLIGHTING, PF_ON);
  gstate->setMode(PFSTATE_CULLFACE, PFCF_OFF);
  //gstate->setMode(PFSTATE_TRANSPARENCY, PFTR_ON);
  scene->setGState(gstate);
  
  light = new pfLightSource;
  light->setColor(PFLT_DIFFUSE, 1.0, 1.0, 1.0);
  light->setAmbient(0.0, 0.0, 0.0);
  scene->addChild(light);

  // ****** setup vtk pipeline
  vtkConeSource *cone;
  vtkGlyph3D *glyph;
  vtkPolyDataMapper *sphereMapper, *spikeMapper;

  sphere = vtkSphereSource::New();
  sphereMapper = vtkPolyDataMapper::New();
    sphereMapper->SetInput(sphere->GetOutput());
  vtkActor *sphereActor = vtkActor::New();
    sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetColor(0.2, 0.7, 0.2);
  if (argc < 2)
    sphereActor->GetProperty()->SetOpacity(1.0);
  else
    sphereActor->GetProperty()->SetOpacity(atof(argv[1]));


  cone = vtkConeSource::New();

  glyph = vtkGlyph3D::New();
#ifdef VTK4
  glyph->SetInput((vtkDataSet *) sphere->GetOutput());
#else
  glyph->SetInput(sphere->GetOutput());
#endif
  glyph->SetSource(cone->GetOutput());
  glyph->SetVectorModeToUseNormal();
  glyph->SetScaleModeToScaleByVector();
  glyph->SetScaleFactor(0.25);

  vtkPolyDataNormals *spikeNormals = vtkPolyDataNormals::New();
  spikeNormals->SetInput(glyph->GetOutput());

  spikeMapper = vtkPolyDataMapper::New();
  spikeMapper->SetInput(spikeNormals->GetOutput());

  vtkActor *spikeActor = vtkActor::New();
  spikeActor->SetMapper(spikeMapper);
  spikeActor->GetProperty()->SetColor(0.2, 0.7, 0.2);
  //spikeActor->GetProperty()->SetOpacity(0.5); 
  // ****** end vtk stuff

  // create pfSCS that will center/scale the geometry
  pfMatrix mtxCenter;
  vtkReal *center;
  center = sphereActor->GetCenter();
  mtxCenter.makeTrans(-center[0], -center[1]+5.0, -center[2]);

  pfMatrix mtxScale;
  vtkReal *bounds = sphereActor->GetBounds();
  float scaleFactor = 5;
  mtxScale.makeScale(scaleFactor/(bounds[1]-bounds[0]),
		       scaleFactor/(bounds[3]-bounds[2]),
		       scaleFactor/(bounds[5]-bounds[4]));
  mtxCenter.postMult(mtxScale);

  pfSCS *scsCenter = new pfSCS(mtxCenter);
  scene->addChild(scsCenter);

  // ****** set up vtkActorToPFTranslators
  // two ways:
  // 1) have translator create new geode
  vtkActorToPFTranslator *translator = vtkActorToPFTranslator::New();
  translator->SetActor(sphereActor);
  //sphereActor->GetProperty()->SetRepresentationToWireframe();
  //translator->LightingOff();
  translator->Init();

  // 2) supply your own geode (if it has GSets already, they will be
  //    replaced.
  pfGeode *geode = new pfGeode();
  vtkActorToPFTranslator *translator2 = vtkActorToPFTranslator::New();
  translator2->SetActor(spikeActor);
  translator2->SetGeode(geode);
  //spikeActor->GetProperty()->SetRepresentationToWireframe();
  //translator2->LightingOff();
  translator2->Init();
  // ****** end set up

  // add geodes to scenegraph
  scsCenter->addChild(translator->GetGeode());
  scsCenter->addChild(geode); // could have also done translator2->getGeode()
  chan->setScene(scene);
}

