/**********************************************************
 * Version $Id$
 *********************************************************/

///////////////////////////////////////////////////////////
//                                                       //
//                         SAGA                          //
//                                                       //
//      System for Automated Geoscientific Analyses      //
//                                                       //
//                     Tool Library                      //
//                     grid analysis                     //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//                     Grid_IMCORR.h                     //
//                                                       //
//                 Copyright (C) 2012 by                 //
//                     Magnus Bremer                     //
//                                                       //
//-------------------------------------------------------//
//                                                       //
// This file is part of 'SAGA - System for Automated     //
// Geoscientific Analyses'. SAGA is free software; you   //
// can redistribute it and/or modify it under the terms  //
// of the GNU General Public License as published by the //
// Free Software Foundation, either version 2 of the     //
// License, or (at your option) any later version.       //
//                                                       //
// SAGA 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 the GNU General Public        //
// License for more details.                             //
//                                                       //
// You should have received a copy of the GNU General    //
// Public License along with this program; if not, see   //
// <http://www.gnu.org/licenses/>.                       //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//    e-mail:     magnus.bremer@uibk.ac.at               //
//                                                       //
//    contact:    Magnus Bremer                          //
//                Institute of Geography                 //
//                University of Innsbruck                //
//                Innrain 52                             //
//                6020 Innsbruck                         //
//                Austria                                //
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#ifndef HEADER_INCLUDED__Grid_IMCORR_H
#define HEADER_INCLUDED__Grid_IMCORR_H

//---------------------------------------------------------
#include "MLB_Interface.h"
#include <cmath>
#include <vector>


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
class CGrid_IMCORR : public CSG_Tool_Grid
{
public:
	CGrid_IMCORR(void);


protected:

	virtual int				On_Parameters_Enable	(CSG_Parameters *pParameters, CSG_Parameter *pParameter);

	virtual bool			On_Execute		(void);


private:

	typedef struct result {
		bool   success { false };
		int    x, y;
		double xReal, yReal, xReal2, yReal2;
		double disp, xDisp, yDisp, DirNormX, DirNormY;
        double strength, xerr, yerr, Aspect;
        double zReal, zReal2, dispReal, Slope;
		sLong  id_m;
	}RESULT;


	void	Get_Chip(std::vector<double> &chip, CSG_Grid *pGrid, int ix, int iy, int size);

	void	gcorr(std::vector<double> ser, std::vector<double> ref, double csmin, int mfit, double ddmx,
		          std::vector<double> ioffrq, std::vector<double> nomoff, int& iacrej, double& streng, std::vector<double>& bfoffs,
		          std::vector<double>& tlerrs, double ddact);

	int		decimal(std::vector<int> Bin);
	void	binary(std::vector<int>& Bin, int number);
	void	cross(std::vector<double>& UNORMC, std::vector<double> ser, std::vector<double> ref_unpadded);
	void	fft2(std::vector<double>& data, std::vector<int> nel, int isign);
	void	gnorm(std::vector<double>& CCNORM , std::vector<double>& pkval, std::vector<int>& ipkcol, std::vector<int>& ipkrow, std::vector<double>& sums,
		          std::vector<double> ser, std::vector<double> ref, std::vector<double> UNORMC );
	void	eval(int ncol, int nrow, std::vector<double> CCNORM, std::vector<double> pkval, std::vector<int> ipkcol, std::vector<int> ipkrow,
		         std::vector<double> sums, double& csmin, double& streng, int& iacrej, std::vector<double>& cpval);
	void	fitreg(std::vector<double> cpval, int mfit, std::vector<double>& pkoffs, std::vector<double>& tlerrs);
	void	sums(std::vector<double> cpval, int mfit, std::vector<double>& z, std::vector<double>& wghts, std::vector<std::vector<float> >& b,
		         std::vector<double>& vector);
	void	kvert(std::vector<std::vector<float> >& V);
	float	detrm(float a[25][25], float k);
	void	cofact(float num[25][25], float f, std::vector<std::vector<float> >& INV);
	void	trans(float num[25][25], float fac[25][25], float r, std::vector<std::vector<float> >& INV);
	void	esterr(std::vector<double> z, std::vector<double> wghts, std::vector<std::vector<float> > bnvrs, std::vector<double> coeffs,
		           std::vector<double>& pkoffs, std::vector<double>& tlerrs);

	void	_Add_Fields(CSG_Shapes *pShapes, bool b3D, bool bIDm);
	void	_Add_Shape(CSG_Shapes *pShapes, TSG_Shape_Type ShapeType, bool b3D, bool bIDm, sLong iShape, RESULT res);
};


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#endif // #ifndef HEADER_INCLUDED__Grid_IMCORR_H
