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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkVisualizationWidget3D.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 "ritkVisualizationWidget3D.h"

#include <vtkImageViewer.h>
#include <vtkImageMapper.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPointData.h>
#include <vtkUnsignedCharArray.h>
#include <vtkProperty.h>
#include <vtkLookupTable.h>
#include <vtkSphereSource.h>
#include <vtkActorCollection.h>

#include <QApplication>
#include <QDesktopWidget>


namespace ritk
{

VisualizationWidget3D::VisualizationWidget3D(QWidget *parent) :
	QWidget(parent)
{
	setupUi(this);

	// Register this smart pointer
	qRegisterMetaType<vtkSmartPointer<vtkActor> >("vtkSmartPointer<vtkActor>");

	// Initialize camera
	m_Camera = vtkSmartPointer<vtkCamera>::New();
	m_Camera->ComputeViewPlaneNormal();
	m_Camera->SetPosition(0,0,0.1);
	m_Camera->SetFocalPoint(0,0,0);
	m_Camera->SetClippingRange( 0.1, 1000.);
	m_Camera->SetViewUp(0,-1,0);
	m_Camera->Zoom(1.5);
	m_CameraInitFlag = false; // ???

	// Set up render pipeline
	m_Renderer = vtkSmartPointer<vtkRenderer>::New();
	m_Renderer->SetActiveCamera(m_Camera);
	//m_Renderer->SetAmbient(0,0,0);
	//m_Renderer->LightFollowCameraOn();
	m_Renderer->GradientBackgroundOn();
	m_Renderer->SetBackground(0.8,0.8,0.8);
	m_Renderer->SetBackground2(0.5,0.5,0.5);
	

	m_Renderer->ResetCamera();
	m_RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	m_RenderWindow->AddRenderer(m_Renderer);
	m_RenderWindow->SetStereoCapableWindow(1);
	m_RenderWindow->SetStereoTypeToRedBlue();
	m_StereoStatus = false;
	m_RenderWindow->SetStereoRender(m_StereoStatus);

	m_VTKWidget->SetRenderWindow(m_RenderWindow);

	QAction* CloseKey = new QAction( tr( "Close" ), this );
	CloseKey->setShortcut(QKeySequence(Qt::Key_Escape));
	connect(CloseKey, SIGNAL( triggered() ), this, SLOT(CloseFullScreen()));
	addAction(CloseKey);

	QAction* OpenKey = new QAction( tr( "Close" ), this );
	OpenKey->setShortcut(QKeySequence(Qt::Key_F1));
	connect(OpenKey, SIGNAL( triggered() ), this, SLOT(OpenFullScreen()));
	addAction(OpenKey);
}


VisualizationWidget3D::~VisualizationWidget3D()
{
}


//----------------------------------------------------------------------------
vtkRenderWindow*
VisualizationWidget3D::GetRenderWindow()
{
	return m_VTKWidget->GetRenderWindow();	
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::SetFullScreenMode(bool b)
{
	if ( b )
	{
		// Make our window without panels
		this->setWindowFlags( Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint );
		// Resize refer to desktop
		//this->resize( QApplication::desktop()->size() );
		this->setFocus();
		this->setFocusPolicy( Qt::StrongFocus );
		this->setAttribute(Qt::WA_QuitOnClose, true);
		this->showFullScreen();
	}
	else
	{
		this->setWindowFlags(0);
		this->showNormal();
	}
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::CloseFullScreen()
{
	SetFullScreenMode(false);
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::OpenFullScreen()
{
	SetFullScreenMode(true);
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::UpdateGUI()
{

	if ( !m_CameraInitFlag )
		m_Renderer->ResetCamera();

	// Initialize camera
	if ( !m_CameraInitFlag )
	{
		m_Camera->Zoom(1.5);
		m_CameraInitFlag = true;
	}

	m_VTKWidget->update();	
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::AddActor(vtkSmartPointer<vtkActor> Actor)
{
	this->m_VTKWidget->Lock();
	m_Renderer->AddViewProp(Actor);
	this->m_VTKWidget->Unlock();
	m_VTKWidget->update();
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::RemoveActor(vtkSmartPointer<vtkActor> Actor)
{
	this->m_VTKWidget->Lock();
	m_Renderer->RemoveViewProp(Actor);	
	this->m_VTKWidget->Unlock();
	m_VTKWidget->update();
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::AddProp3D(vtkSmartPointer<vtkProp3D> Prop3D)
{
	this->m_VTKWidget->Lock();
	m_Renderer->AddViewProp(Prop3D);
	this->m_VTKWidget->Unlock();
	m_VTKWidget->update();
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::AddProp(vtkSmartPointer<vtkProp> Prop)
{
	this->m_VTKWidget->Lock();
	m_Renderer->AddViewProp(Prop);
	this->m_VTKWidget->Unlock();
	m_VTKWidget->update();
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::RemoveProp(vtkSmartPointer<vtkProp> Prop)
{
	this->m_VTKWidget->Lock();
	m_Renderer->RemoveViewProp(Prop);	
	this->m_VTKWidget->Unlock();
	m_VTKWidget->update();
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::RemoveProp3D(vtkSmartPointer<vtkProp3D> Prop3D)
{
	this->m_VTKWidget->Lock();
	m_Renderer->RemoveViewProp(Prop3D);	
	this->m_VTKWidget->Unlock();
	m_VTKWidget->update();
}


//----------------------------------------------------------------------------
void
VisualizationWidget3D::SetStereo(bool Status)
{
	m_StereoStatus = Status;
	m_RenderWindow->SetStereoRender(m_StereoStatus);
}

} // end namespace ritk
