//=========================================================================
//#
//#  vtkActorToPFTranslatorParallel.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 "vtkActorToPFTranslatorParallel.h"

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

  translator = (vtkActorToPFTranslatorParallel *) userData;

  // check for new geometry and swap if there is some
  translator->pfUpdate();

  return PFTRAV_CONT;
}

// overload new operator
void *vtkActorToPFTranslatorParallel::operator new (size_t size)
{
  cerr << "allocating translator from shared memory using pfMalloc" << endl;
  return (pfMalloc (size, pfGetSharedArena()));
}

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

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

  Swap = 0;

  // if geode is null, create new one...
  if (theGeode == NULL) 
    theGeode = new pfGeode;

  // will do first translation...
  //this->Update(); // updates are done on VTK process side now.

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

// Performer update
void vtkActorToPFTranslatorParallel::pfUpdate() {

  // if new geometry is available...
  if (Swap) {

    // remove old gsets and delete them
    int i;
    int numGSets = theGeode->getNumGSets();
    for (i=0; i<numGSets; i++) {
      pfGeoSet *g = theGeode->getGSet(0);
      theGeode->removeGSet(g);
      pfDelete(g);
    }

    // put in new gsets
    for (i=0; i<4; i++)
      if (newGeoSets[i] != NULL)
        theGeode->addGSet(newGeoSets[i]);

    // new geometry already swapped...
    Swap = 0;
    if (this->GetVerbose())
      cerr << "Swapped" << endl;
  }
}

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

  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 (!Swap &&  // if current geometry already in scenegraph
     (this->theActor->GetMTime() > this->ExecuteTime || 
      this->theActor->GetMapper()->GetMTime() > this->ExecuteTime ||
      this->theActor->GetMapper()->GetInput()->GetMTime() > this->ExecuteTime ||
      this->GetMTime() > this->ExecuteTime ))
  {
    this->Execute();
    this->ExecuteTime.Modified();
    Swap = 1; // new geometry is now available
  }
}

void vtkActorToPFTranslatorParallel::Execute() {
    if (this->GetVerbose())
      cerr << "Execute()" << endl;
    vtkActorToGeoSets(theActor, newGeoSets, this->GetVerbose());
}

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