/*
Copyright (c) 2018, Fabian Prada and Michael Kazhdan
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution. 

Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/

#include <Src/PreProcessing.h>

#include <Misha/CmdLineParser.h> 
#include <Misha/Miscellany.h>
#include <Misha/Exceptions.h>
#include <Misha/FEM.h>
#include <Misha/MultiThreading.h>
#include <Misha/Texels.h>
#include <Src/MeshIO.h>
#include <Src/ImageIO.h>

using namespace MishaK;
using namespace MishaK::TSP;

CmdLineParameterArray< std::string , 2 > Input( "in" );
CmdLineParameter< std::string > Output( "out" ) , OutputTexturePositions( "outP" );
CmdLineParameter< unsigned int > DilationRadius( "radius" , 0 );
CmdLineParameter< double > CollapseEpsilon( "collapse" , 0 );
CmdLineReadable Verbose( "verbose" );

CmdLineReadable* params[] =
{
	&Input , &Output , &OutputTexturePositions , &DilationRadius , &Verbose ,
	&CollapseEpsilon ,
	NULL
};

void ShowUsage( const char* ex )
{
	printf( "Usage %s:\n", ex );
	printf( "\t --%s <input mesh, input texture>\n" , Input.name.c_str() );
	printf( "\t[--%s <output texture>]\n" , Output.name.c_str() );
	printf( "\t[--%s <output texture positions>]\n" , OutputTexturePositions.name.c_str() );
	printf( "\t[--%s <dilation radius>=%d]\n" , DilationRadius.name.c_str() , DilationRadius.value );
	printf( "\t[--%s <collapse epsilon>=%g]\n" , CollapseEpsilon.name.c_str() , CollapseEpsilon.value );
	printf( "\t[--%s]\n" , Verbose.name.c_str() );
}

template< typename Data >
RegularGrid< 2 , Data > FlipVertical( const RegularGrid< 2 , Data > & grid )
{
	RegularGrid< 2 , Data > _grid( grid.res() );
	for( unsigned int i=0 ; i<grid.res(0) ; i++ ) for( unsigned int j=0 ; j<grid.res(1) ; j++ ) _grid( i , grid.res(1)-1-j ) = grid(i,j);
	return _grid;
}

int main( int argc , char* argv[] )
{
	static const unsigned int K = 2;
	static const unsigned int Dim = 3;
	static const bool NodeAtCellCenter = true;
	using Index = unsigned int;
	using TexelInfo = typename Texels< NodeAtCellCenter , Index >::template TexelInfo< K >;


	CmdLineParse( argc-1 , argv+1 , params );
	if( !Input.set ){ ShowUsage( argv[0] ) ; return EXIT_FAILURE; }

	Miscellany::Timer timer;
	std::vector< Point< double , Dim > > vertices;
	std::vector< Point< double , K > > textureCoordinates;
	std::vector< SimplexIndex< K > > simplices;

	auto TextureSimplexFunctor = [&]( size_t sIdx )
		{
			Simplex< double , K , K > simplex;
			for( unsigned int k=0 ; k<=K ; k++ ) simplex[k] = textureCoordinates[ sIdx*(K+1) + k ];
			return simplex;
		};

	ReadTexturedMesh( Input.values[0] , vertices , textureCoordinates , simplices );
	if( CollapseEpsilon.value>0 ) CollapseVertices( vertices , simplices , CollapseEpsilon.value );
	RegularGrid< K , Point< double , 3 > > texture;
	ReadImage< 8 >( texture , Input.values[1] );
	texture = FlipVertical( texture );

	if( Verbose.set ) std::cout << "Vertices / texture coordinates / simplices: " << vertices.size() << " / " << textureCoordinates.size() << " / " << simplices.size() << std::endl;
	if( Verbose.set ) std::cout << "Texture resolution: " << texture.res(0) << " x " << texture.res(1) << std::endl;

	timer.reset();
	RegularGrid< K , TexelInfo > inputTexelInfo = Texels< NodeAtCellCenter , Index >::template GetSupportedTexelInfo< Dim , false >( simplices.size() , [&]( size_t v ){ return vertices[v]; } , [&]( size_t s ){ return simplices[s]; } , TextureSimplexFunctor , texture.res() , 0 , false );
	if( Verbose.set ) std::cout << "Got input texels: " << timer() << std::endl;

	timer.reset();
	RegularGrid< K , TexelInfo > dilatedTexelInfo = Texels< NodeAtCellCenter , Index >::template GetSupportedTexelInfo< Dim , false >( simplices.size() , [&]( size_t v ){ return vertices[v]; } , [&]( size_t s ){ return simplices[s]; } , TextureSimplexFunctor , texture.res() , DilationRadius.value , Verbose.set );
	if( Verbose.set ) std::cout << "Got dilated texels: " << timer() << std::endl;

	auto GetSimplex = [&]( unsigned int si )
		{
			Simplex< double , K , K > s;
			for( unsigned int k=0 ; k<=K ; k++ ) s[k] = textureCoordinates[ si*(K+1) + k ];
			for( unsigned int k=0 ; k<=K ; k++ ) for( unsigned int d=0 ; d<K ; d++ ) s[k][d] *= texture.res(d);
			return s;
		};

	auto SamplePosition = [&]( TexelInfo ti ){ return GetSimplex( ti.sIdx )( ti.bc ); };
	auto SampleValue = [&]( TexelInfo ti ){ return texture( SamplePosition( ti ) ); };

	timer.reset();
	ThreadPool::ParallelFor
		(
			0 , dilatedTexelInfo.resolution() ,
			[&]( unsigned int , size_t i ){ if( dilatedTexelInfo[i].sIdx!=-1 && inputTexelInfo[i].sIdx==-1 ) texture[i] = SampleValue( dilatedTexelInfo[i] ); }
		);
	if( Verbose.set ) std::cout << "Set dilated texels: " << timer() << std::endl;

	auto SimplexEmbeddingFunctor = [&]( size_t sIdx )
		{
			Simplex< double , 3 , 2 > s;
			for( unsigned int i=0 ; i<3 ; i++ ) s[i] = vertices[ simplices[sIdx][i] ];
			return s;
		};
	RegularGrid< K , Point< float , Dim > > texturePositions = Texels< NodeAtCellCenter , Index >::GetTexelPositions< float , Dim >( simplices.size() , SimplexEmbeddingFunctor , dilatedTexelInfo );

	if( Output.set )
	{
		RegularGrid< K , Point< double , 3 > > _texture = FlipVertical( texture );
		WriteImage< 8 >( _texture , Output.value );
	}
	if( OutputTexturePositions.set )
	{
		RegularGrid< K , Point< float , Dim > > _texturePositions = FlipVertical( texturePositions );
		WriteImage< 8 >( _texturePositions , OutputTexturePositions.value );
	}

	return EXIT_SUCCESS;
}
