//=========================================================================
//#
//#  vtkActorToPFTranslator.cxx                        #####     
//#                                                   ##   ##
//#  Author(s): Paul Rajlich                          ##
//#  Email: prajlich@ncsa.uiuc.ed                     ##   ##
//#  Date created:                                     #####
//#
//#  Description: method definitions for class.
//#
//=========================================================================
//# Copyright (C) 1997-2006 Paul Rajlich, NCSA
//# Distributed under the terms of the GNU Library General Public License (LGPL)
//# as published by the Free Software Foundation
//=========================================================================

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

// Performer callback function... see Init();
int updateVTKFunc(pfTraverser *trav, void *userData) {
  vtkActorToPFTranslator *translator;

  translator = (vtkActorToPFTranslator *) userData;
  translator->Update();

  return PFTRAV_CONT;
}

vtkActorToPFTranslator::vtkActorToPFTranslator() {
  theActor = NULL;
  theGeode = NULL;
  this->VerboseOff();
//this->Modified(); // already done in vtkObject
}

// do initial translation and add callback
void vtkActorToPFTranslator::Init() {

  // will do first translation...
  this->Update();

  // Register callback with Performer. Will retranslate (Execute()) when
  // necessary by calling Update() each APP traversal (before rendering).
  //                                    pre         post 
  theGeode->setTravFuncs(PFTRAV_APP, updateVTKFunc, NULL);
  theGeode->setTravData(PFTRAV_APP, this);
}

// Modeled after Update() in vtkFilter class
void vtkActorToPFTranslator::Update() {

  float beforeTime = pfGetTime();
  if (theActor != NULL)
    // call Update() on actor which calls Update() on mapper, which
    // calls Update() on its input which is vtkPolyData
#ifdef VTK4
    this->theActor->GetMapper()->GetInput()->Update();
#else
    this->theActor->Update(); 
#endif
  else {
    vtkErrorMacro(<<"NO ACTOR!");
    return;
  }

  // vtkActor::GetMTime() returns greater of its own MTime and Property MTime 
  // Since actors and mappers do not execute, must also check MTime of
  //   the input to the mapper (vtkPolyData).  
  if (this->theActor->GetMTime() > this->ExecuteTime || 
      this->theActor->GetMapper()->GetMTime() > this->ExecuteTime ||
      this->theActor->GetMapper()->GetInput()->GetMTime() > this->ExecuteTime ||
      this->GetMTime() > this->ExecuteTime )
  {
    if (this->GetVerbose()) {
      cerr << "\n--vtkActorToPFTranslator --" << endl;
      cerr << "took " << pfGetTime() - beforeTime << "s to update vtkActor\n";
    }
    this->Execute();
    this->ExecuteTime.Modified();
  }
}

void vtkActorToPFTranslator::Execute() {
    // when it is NULL the first time, the "theGeode =" is necessary.
    theGeode = vtkActorToPF(theActor, theGeode, this->GetVerbose());
}

void vtkActorToPFTranslator::PrintSelf(ostream& os, vtkIndent indent) {
  os << indent << "vtkActorToPFTranslator: " << endl;
  os << *theActor << endl;
}
