osg::ref_ptr<osg::Geode> VTKActorToOSG(vtkActor *actor, osg::ref_ptr<osg::Geode> geode, int verbose)
{
	// make actor current
	actor->GetMapper()->Update();

	// this could possibly be any type of DataSet, VTKActorToOSG assumes polyData
	if (strcmp(actor->GetMapper()->GetInput()->GetClassName(), "vtkPolyData"))
	{
		std::cerr << "ERROR! Actor must use a vtkPolyDataMapper." << std::endl;
		std::cerr << "If you are using a vtkDataSetMapper, use vtkGeometryFilter instead." << std::endl;
		return NULL;
	}

	// if geode doesn't exist, then create a new one
	if (!geode.valid())
		geode = new osg::Geode();

	// get poly data
	vtkPolyData *polyData = (vtkPolyData *) actor->GetMapper()->GetInput();

	// get primitive arrays
	osg::ref_ptr<osg::Geometry> points, lines, polys, strips;

	// create new Geometry for the Geode
	points = processPrimitive(actor, polyData->GetVerts(), osg::PrimitiveSet::POINTS, verbose);
	lines = processPrimitive(actor, polyData->GetLines(), osg::PrimitiveSet::LINE_STRIP, verbose);
	polys = processPrimitive(actor, polyData->GetPolys(), osg::PrimitiveSet::POLYGON, verbose);
	strips = processPrimitive(actor, polyData->GetStrips(), osg::PrimitiveSet::TRIANGLE_STRIP, verbose);

	// remove old gsets and delete them
	geode->removeDrawables(0, geode->getNumDrawables());

	if (points.valid())
		geode->addDrawable(points.get());
	if (lines.valid())
		geode->addDrawable(lines.get());
	if (polys.valid())
		geode->addDrawable(polys.get());
	if (strips.valid())
		geode->addDrawable(strips.get());

	return geode;
}
void VTKPolyDataMapper::execute(void)
{
#ifdef OSG_WITH_VTK
    if(_pActor == NULL)
    {
        return;
    }

    vtkMapper *pMapper = _pActor->GetMapper();
    
    if(pMapper == NULL)
        return;

    pMapper->GetInput()->Update();
    
    if( (_pActor ->            GetMTime() < this->_executeTime) && 
        ( pMapper->            GetMTime() < this->_executeTime) &&
        ( pMapper->GetInput()->GetMTime() < this->_executeTime) &&
        ( this->_modifiedTime             < this->_executeTime)  )
    {
        return;
    }
    else
    {
        fprintf(stderr, "%ld %ld | %ld %ld %ld\n",
                _executeTime.GetMTime(),
                _modifiedTime.GetMTime(),
                _pActor->GetMTime(),
                _pActor->GetMapper()->GetMTime(),
                _pActor->GetMapper()->GetInput()->GetMTime());
    }

    if(_sfRoot.getValue() == NULL)
    {
        initGeometries();
    }

    fprintf(stderr, "Mapper::execute %p\n", _pActor);

    // get poly data
    vtkPolyData *polyData = 
        static_cast<vtkPolyData *>(_pActor->GetMapper()->GetInput());

    if(polyData == NULL)
    {
        fprintf(stderr, "Strange no data\n");
        return;
    }

    // get primitive arrays
    vtkCellArray *points, *lines, *polys, *strips;
    
    points = polyData->GetVerts();
    lines  = polyData->GetLines();
    polys  = polyData->GetPolys();
    strips = polyData->GetStrips();
    
    int numPts    = points->GetNumberOfCells();
    int numLines  = lines ->GetNumberOfCells();
    int numPolys  = polys ->GetNumberOfCells();
    int numStrips = strips->GetNumberOfCells();

    fprintf(stderr, "P : %d | L : %d | Poly : %d | S : %d\n",
            numPts,
            numLines,
            numPolys,
            numStrips);    

    const VTKPolyDataMapper *pThis = this;

    if(numPts != 0)
    {
        bool rc = processPrimitive(pThis->_mfPositions[0],
                                   pThis->_mfColors   [0],
                                   pThis->_mfNormals  [0],
                                   pThis->_mfLength   [0],
                                   pThis->_mfTypes    [0],

                                    _pActor,
                                     points,
                                     GL_POINTS,
                                     true);
   
        if(rc == true)
        {
            pThis->_mfGeoRoots[0]->setTravMask(
                OSG::TypeTraits<OSG::UInt32>::BitsSet);

            pThis->_mfGeoRoots[0]->invalidateVolume();
        }
    }

    if(numLines != 0)
    {
        bool rc = processPrimitive(pThis->_mfPositions[1],
                                   pThis->_mfColors   [1],
                                   pThis->_mfNormals  [1],
                                   pThis->_mfLength   [1],
                                   pThis->_mfTypes    [1],

                                   _pActor,
                                    lines,
                                    GL_LINE_STRIP,
                                    true);
   
        if(rc == true)
        {
            pThis->_mfGeoRoots[1]->setTravMask(
                OSG::TypeTraits<OSG::UInt32>::BitsSet);

            pThis->_mfGeoRoots[1]->invalidateVolume();
        }
    }


    if(numPolys != 0)
    {
        bool rc = processPrimitive(pThis->_mfPositions[2],
                                   pThis->_mfColors   [2],
                                   pThis->_mfNormals  [2],
                                   pThis->_mfLength   [2],
                                   pThis->_mfTypes    [2],
 
                                   _pActor,
                                    polys,
                                    GL_POLYGON,
                                    true);
   
        if(rc == true)
        {
            pThis->_mfGeoRoots[2]->setTravMask(
                OSG::TypeTraits<OSG::UInt32>::BitsSet);

            pThis->_mfGeoRoots[2]->invalidateVolume();
        }
    }

    if(numStrips != 0)
    {
        bool rc = processPrimitive(pThis->_mfPositions[3],
                                   pThis->_mfColors   [3],
                                   pThis->_mfNormals  [3],
                                   pThis->_mfLength   [3],
                                   pThis->_mfTypes    [3],
 
                                   _pActor,
                                    strips,
                                    GL_TRIANGLE_STRIP,
                                    true);
   
        if(rc == true)
        {
            pThis->_mfGeoRoots[3]->setTravMask(
                OSG::TypeTraits<OSG::UInt32>::BitsSet);

            pThis->_mfGeoRoots[3]->invalidateVolume();
        }
    }

    _sfRoot.getValue()->updateVolume();

    this->_executeTime.Modified();
        
    OSG::Thread::getCurrentChangeList()->commitChanges();
#endif
}