#include "CUDATemporalAveragingRImageFilter.h"


//----------------------------------------------------------------------------
template<class TRImage, class TRGBRImage>
CUDATemporalAveragingRImageFilter<TRImage,TRGBRImage>
::CUDATemporalAveragingRImageFilter()
{
	m_RangeFilter = RangeFilterType::New();
	m_RGBFilter = RGBFilterType::New();
}


//----------------------------------------------------------------------------
template<class TRImage, class TRGBRImage>
CUDATemporalAveragingRImageFilter<TRImage,TRGBRImage>
::~CUDATemporalAveragingRImageFilter()
{
}


//----------------------------------------------------------------------------
template<class TRImage, class TRGBRImage>
void
CUDATemporalAveragingRImageFilter<TRImage,TRGBRImage>
::SetNumRangeDataFrames(unsigned int NumFrames)
{
	m_RangeFilter->SetNumFrames(NumFrames);
}


//----------------------------------------------------------------------------
template<class TRImage, class TRGBRImage>
void
CUDATemporalAveragingRImageFilter<TRImage,TRGBRImage>
::SetNumRGBDataFrames(unsigned int NumFrames)
{
	m_RGBFilter->SetNumFrames(NumFrames);
}


//----------------------------------------------------------------------------
template<class TRImage, class TRGBRImage>
void
CUDATemporalAveragingRImageFilter<TRImage,TRGBRImage>
::Reset()
{
	m_RGBFilter->Reset();
	m_RangeFilter->Reset();
}


//----------------------------------------------------------------------------
template<class TRImage, class TRGBRImage>
void
CUDATemporalAveragingRImageFilter<TRImage,TRGBRImage>
::DoFiltering()
{
	// Due to fps evaluation
	//cudaThreadSynchronize();

	// Filter range image
	if ( (Superclass::GetFilterMode()&Superclass::RANGE) == Superclass::RANGE )
	{
		m_RangeFilter->SetInput(this->GetInput()->GetRangeImage());
		m_RangeFilter->UpdateLargestPossibleRegion();
		this->GetOutput()->GetRangeImage()->Graft(m_RangeFilter->GetOutput());
	}

	// Filter RGB image
	if ( (Superclass::GetFilterMode()&Superclass::RGB) == Superclass::RGB )
	{
		RGBRImageConstPointer InputRGBRImage = dynamic_cast<const RGBRImageType*>((const RGBRImageType*)this->GetInput());
		RGBRImagePointer OutputRGBRImage = dynamic_cast<RGBRImageType*>((RGBRImageType*)this->GetOutput());
		if ( InputRGBRImage && OutputRGBRImage )
		{
			m_RGBFilter->SetInput(InputRGBRImage->GetRGBImage());
			m_RGBFilter->UpdateLargestPossibleRegion();
			OutputRGBRImage->GetRGBImage()->Graft(m_RGBFilter->GetOutput());
		}
	}
	
	// Filtering of world coords is not supported
	if ( (Superclass::GetFilterMode()&Superclass::WORLDCOORD) == Superclass::WORLDCOORD )
	{
		itkExceptionMacro(<<"Filtering for world coords NOT supported!");
	}

	// Due to fps evaluation
	//cudaThreadSynchronize();
}
