#include "angles_filter.h"
#include "aspect_ratio_filter.h"
#include "dihedral_angles_filter.h"
#include "surface_points_filter.h"
#include "project_surface_points_on_poly.h"
#include "mesh_fit_filter.h"
#include "point_tris_dist.h"
#include "remove_external_cells_filter.h"

#include <vtkCellData.h>
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkPolyData.h>
#include <vtkCellArrayIterator.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkOBJReader.h>

#ifdef USE_VIEWER
#include <vtkNamedColors.h>
#include <vtkCamera.h>
#include <vtkVolumeProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVolumeMapper.h>
#include <vtkVolume.h>
#include <vtkOpenGLProjectedTetrahedraMapper.h>
#include <vtkPiecewiseFunction.h>
#endif

#include <array>


int main(int argc, char **argv) {
	if (argc != 3) {
		std::cerr << "Usage: " << argv[0] << " tetmesh polydata" << std::endl;
		return EXIT_FAILURE;
	}

	vtkNew<vtkUnstructuredGridReader> tetMeshReader;
	tetMeshReader->SetFileName(argv[1]);

	vtkNew<DihedralAnglesFilter> dihedralAnglesFilter;
	dihedralAnglesFilter->SetInputConnection(tetMeshReader->GetOutputPort());

	// vtkNew<vtkXMLPolyDataReader> polyMeshReader;
	// polyMeshReader->SetFileName(argv[2]);
	vtkNew<vtkOBJReader> polyMeshReader;
	polyMeshReader->SetFileName(argv[2]);

	vtkNew<RemoveExternalCellsFilter> removeExternalCellsFilter;
	removeExternalCellsFilter->SetInputConnection(0,
		dihedralAnglesFilter->GetOutputPort());
	removeExternalCellsFilter->SetInputConnection(1,
		polyMeshReader->GetOutputPort());

	vtkNew<SurfacePointsFilter> surfacePointsFilter;
	surfacePointsFilter->SetInputConnection(
		removeExternalCellsFilter->GetOutputPort());

	vtkNew<ProjectSurfacePointsOnPoly> project;
	project->SetInputConnection(0, surfacePointsFilter->GetOutputPort());
	project->SetInputConnection(1, polyMeshReader->GetOutputPort());

	vtkNew<vtkXMLUnstructuredGridWriter> writer;
	writer->SetInputConnection(project->GetOutputPort());
	writer->SetDataModeToAscii();
	writer->SetFileName("out.vtu");
	writer->Write();

#ifdef USE_VIEWER
	/* Volume rendering properties */
	vtkNew<vtkOpenGLProjectedTetrahedraMapper> volumeMapper;
	volumeMapper->SetInputConnection(externalPointsFilter->GetOutputPort());
	vtkNew<vtkVolume> volume;
	volume->SetMapper(volumeMapper);
	vtkNew<vtkPiecewiseFunction> transferFunction;
	transferFunction->AddPoint(-1, 0);
	transferFunction->AddPoint(1, 1);
	volume->GetProperty()->SetScalarOpacity(transferFunction);
	volume->GetProperty()->SetColor(transferFunction);

	/* Renderer */
	vtkNew<vtkNamedColors> colors;
	std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
	colors->SetColor("BkgColor", bkg.data());
	vtkNew<vtkRenderer> renderer;
	renderer->AddVolume(volume);
	renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
	renderer->ResetCamera();
	renderer->GetActiveCamera()->Zoom(1.5);
	vtkNew<vtkRenderWindow> renderWindow;
	renderWindow->SetSize(300, 300);
	renderWindow->AddRenderer(renderer);
	renderWindow->SetWindowName("PFE");
	vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindow->Render();
	renderWindowInteractor->Start();
#endif
	return EXIT_SUCCESS;
}
