/*======================================================================================

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkFilterPlugin.cxx
	Version:		$Revision: 1761 $
	Date:			$Date: 2011-09-23 17:25:41 +0200 (Fr, 23 Sep 2011) $

	Copyright (c) 2010-2011
	Pattern Recognition Lab, 
    University Erlangen-Nuremberg, Germany
    All rights reserved.

	This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
	See RITKCopyright.txt for more information.

	http://www5.informatik.uni-erlangen.de/research/software/ritk

========================================================================================*/

#include "ritkFilterPlugin.h"
#include "ritkManager.h"

namespace ritk
{

FilterPlugin::FilterPlugin()
{
	// Not filter and no pipeline
	m_FilterPointer = NULL;
	m_AcquisitionPipeline = 0;

	// // Default filtering
	m_FilterRGB = false;
	m_FilterRange = true;
	m_FilterWorldCoord = false;	
}


FilterPlugin::~FilterPlugin()
{
}


//----------------------------------------------------------------------------
bool
FilterPlugin::StartPlugin(void *Pipeline)
{
	// Sanity checks
	if ( !Pipeline )
	{
		LOG_DEB("Pipeline pointer is NULL");
		return false;
	}

	AcquisitionPipeline* Pipe = dynamic_cast<AcquisitionPipeline*>((AcquisitionPipeline*)Pipeline);
	if ( !Pipe )
	{
		LOG_DEB("Pointer is not a pipeline!");
		return false;
	}
	
	if ( !GetFilter() )
	{
		LOG_DEB("Invalid filter object!");
		return false;
	}

	// Call superclass method (this should only set the flag)
	if ( !PluginBase::StartPlugin() )
		return false;

	// Set the pipeline
	m_AcquisitionPipeline = Pipe;

	// Set the internal filter pointer
	m_FilterPointer = GetFilter();
	m_AcquisitionPipeline->AddFilter(m_FilterPointer);

	return true;
}


//----------------------------------------------------------------------------
bool
FilterPlugin::StopPlugin()
{
	PluginBase::StopPlugin();
	return m_AcquisitionPipeline->RemoveFilter(m_FilterPointer);
}


//----------------------------------------------------------------------------
void 
FilterPlugin::GetRuntime(double *Runtime, double *RuntimeStdDeviation)
{
	// Sanity check
	if ( !m_FilterPointer )
	{
		LOG_DEB("Can't evaluate runtime as no filter is available!");
		return;
	}

	// Runtime computation is done by the wrapped filter
	m_FilterPointer->GetElapsedTime(Runtime,RuntimeStdDeviation);
}


//----------------------------------------------------------------------------
void
FilterPlugin::SetFilterMode(FilterModeType Mode)
{
	if ( m_FilterMode == Mode )
		return;

	// Update member
	m_FilterMode = Mode;

	// Lock pipeline
	LockPipeline();

	// Set the filter mode
	GetFilter()->SetFilterMode(Mode);

	// Unlock pipeline
	UnlockPipeline();
}


//----------------------------------------------------------------------------
void
FilterPlugin::UpdateFilterMode()
{
	FilterModeType FilterMode = FilterType::NONE;
	if ( m_FilterRGB )
		FilterMode |= FilterType::RGB;
	if ( m_FilterRange )
		FilterMode |= FilterType::RANGE;
	if ( m_FilterWorldCoord )
		FilterMode |= FilterType::WORLDCOORD;

	SetFilterMode(FilterMode);
}


//----------------------------------------------------------------------------
void
FilterPlugin::SetRGBFilterMode(bool b)
{
	m_FilterRGB = b;
	UpdateFilterMode();
}


//----------------------------------------------------------------------------
void
FilterPlugin::SetRangeFilterMode(bool b)
{
	m_FilterRange = b;
	UpdateFilterMode();
}


//----------------------------------------------------------------------------
void
FilterPlugin::SetWorldCoordFilterMode(bool b)
{
	m_FilterWorldCoord = b;
	UpdateFilterMode();
}


//----------------------------------------------------------------------------
void
FilterPlugin::LockPipeline()
{
	if ( m_AcquisitionPipeline )
		m_AcquisitionPipeline->Lock();
}


//----------------------------------------------------------------------------
void
FilterPlugin::UnlockPipeline()
{
	if ( m_AcquisitionPipeline )
		m_AcquisitionPipeline->Unlock();
}


} // end namespace ritk
