#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkContourWidget.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include "vtkImageReader.h"
#include <itkImageFileReader.h>
#include <vtkPolyData.h>
#include "vtkImageData.h"
#include <vtkImageDataGeometryFilter.h>
#include <itkImageToVTKImageFilter.h>
#include <vtkInteractorStyleTrackballActor.h>
int main(int, char *[])
{  

	
	typedef short InputPixelType;//Pixel Type
	const unsigned int dimension = 2;
	typedef itk::Image<InputPixelType, dimension> Image2DType;
	typedef itk::ImageToVTKImageFilter<Image2DType>       ConnectorType;
	
	// read the contour
	std::cout<<"1"<<endl;
	typedef itk::ImageFileReader<Image2DType> ImageReaderType;
	ImageReaderType::Pointer reader = ImageReaderType::New();
	reader->SetFileName("C:\\Users\\Abdelkhalek Bakkari\\Desktop\\ContourWidget\\ContourWidget\\ContourWidget\\build\\Release\\tmp\\contour-1.mha");
	reader->Update();
	
	// itk to vtk
	std::cout<<"2"<<endl;
	ConnectorType::Pointer connector = ConnectorType::New();
	connector->SetInput(reader->GetOutput());
	connector->Update();

	//vtk image to vtk image data
	std::cout<<"3"<<endl;
	vtkImageData *imageData = 0;
	/*vtkIdType 	id;
	vtkIdList * 	cellIdList ;*/
	imageData = connector->GetOutput();
	
	//// Poly Data
	//vtkSmartPointer<vtkIdList> cellid =
	//	vtkSmartPointer<vtkIdList>::New();
	//cellid->GetPointCells(id, cellIdList);
	
	// Convert the image to a polydata
	std::cout<<"4"<<endl;
	vtkSmartPointer<vtkImageDataGeometryFilter> imageDataGeometryFilter = 
		vtkSmartPointer<vtkImageDataGeometryFilter>::New();
	imageDataGeometryFilter->SetInput(imageData);
	imageDataGeometryFilter->Update();
	
	vtkSmartPointer<vtkPolyData> imagePoly = vtkSmartPointer<vtkPolyData>::New();
	imagePoly->ShallowCopy((imageDataGeometryFilter->GetOutput()));
	cout<<imagePoly->GetNumberOfPoints()<<endl;
	cout<<"number if cells "<<imagePoly->GetNumberOfCells();
	// Create a mapper and actor
	std::cout<<"5"<<endl;
	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	
	mapper->SetInput(imagePoly);

	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);
//	actor->GetProperty()->SetColor(1,1,1);


	//vtkContourWidget::Initialize( imageData );
	// Create a renderer and render window
	std::cout<<"6"<<endl;
	vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
	//renderer->SetBackground(0.1,0.2,0.3);
	vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	renderWindow->AddRenderer(renderer);
	renderer->AddActor(actor);
	renderer->ResetCamera();
	vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindow->Render();
	vtkSmartPointer<vtkInteractorStyleTrackballActor> style = 
		vtkSmartPointer<vtkInteractorStyleTrackballActor>::New();

	renderWindowInteractor->SetInteractorStyle( style );
	renderWindowInteractor->Start();

	// Create an interactor
	std::cout<<"7"<<endl;
	//vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
	//	vtkSmartPointer<vtkRenderWindowInteractor>::New();
	//renderWindowInteractor->Initialize();
	//renderWindowInteractor->Enable();
	//renderWindowInteractor->SetRenderWindow(renderWindow);
	//const int Enabling = 1 ;
	
	// Create a contour widget
	std::cout<<"8"<<endl;
	vtkSmartPointer<vtkContourWidget> contourWidget = 
		vtkSmartPointer<vtkContourWidget>::New();
	contourWidget->SetEnabled(true);
	contourWidget->Initialize(0,(imagePoly->GetNumberOfCells()));
	contourWidget->SetInteractor(renderWindowInteractor);
	contourWidget->CreateDefaultRepresentation();
	
	//renderWindowInteractor->Initialize();
	/*renderWindow->Render();
	contourWidget->On();
*/
	// Begin mouse interaction
	std::cout<<"9"<<endl;
	//renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}