#include <stdlib.h>
#include "stack-c.h"
#include "api_scilab.h"
#include "Scierror.h"
#include "sciprint.h"

#include "Ridge.h"
#include "Erreur.h"

/********************************************
 * SCILAB function : TestCast               *
 ********************************************/

int gtw_TestCast ( char * fname )
{
	int * MYINTS ;
	float * MYFLOATS ;
	int m, n, l, N;
	int un=1;

	CheckRhs(1,1);
	CheckLhs(1,1);

	/* Variable MYINTS */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckVector(1, m, n);
	MYINTS = istk(l);
	N = m*n ;
	
	/* Appel à l'API */

	if ( TestCast ( MYINTS , N , & MYFLOATS ) )	
		Scierror (999, GetErreur());

	/* Création de la variable de sortie sur le stack : lecture comme entiers */

	CreateVarFromPtr ( Rhs + 1 , "i", & N , & un , & MYFLOATS );
	LhsVar(1) = Rhs + 1;
	
	free(MYFLOATS);
	
	return(0);
	
}


/********************************************
 * SCILAB function : TestEnsembles          *
 ********************************************/

int gtw_TestEnsembles ( char * fname )
{
	int * INNERBOUNDARY, * OUTERBOUNDARY , * INNERSUBBOUNDARY, * OUTERSUBBOUNDARY ;
	int * INNERBOUNDARY1, * OUTERBOUNDARY1 ;
	int m, n, l, ni, no, nis, nos, ni1, no1, ncols, nrows ;
	int un=1;

	CheckRhs(6,6);
	CheckLhs(2,2);

	/* Variable INNERBOUNDARY */

	GetRhsVar(1,"i",&m,&n,&l);
	INNERBOUNDARY = istk(l);
	ni = m*n ;
	
	/* Variable OUTERBOUNDARY */

	GetRhsVar(2,"i",&m,&n,&l);
	OUTERBOUNDARY = istk(l);
	no = m*n ;

	/* Variable INNERSUBBOUNDARY */

	GetRhsVar(3,"i",&m,&n,&l);
	INNERSUBBOUNDARY = istk(l);
	nis = m*n ;
	
	/* Variable OUTERSUBBOUNDARY */

	GetRhsVar(4,"i",&m,&n,&l);
	OUTERSUBBOUNDARY = istk(l);
	nos = m*n ;

	/* Variable ncols */

	GetRhsVar(5,"i",&m,&n,&l);
	CheckScalar(5,m,n);
	ncols = *istk(l);

	/* Variable nrows */

	GetRhsVar(6,"i",&m,&n,&l);
	CheckScalar(6,m,n);
	nrows = *istk(l);
	
	/* Appel à l'API */

	if ( TestEnsembles ( INNERBOUNDARY , ni ,
			     OUTERBOUNDARY , no ,
			     INNERSUBBOUNDARY , nis ,
			     OUTERSUBBOUNDARY , nos ,
			     ncols , nrows ,
			     & INNERBOUNDARY1 , & ni1 ,
			     & OUTERBOUNDARY1 , & no1 ) )	
		Scierror (999, GetErreur());

	/* Création de la variable de sortie sur le stack : lecture comme entiers */

	CreateVarFromPtr ( Rhs + 1 , "i", & ni1 , & un , & INNERBOUNDARY1 );
	LhsVar(1) = Rhs + 1;

	CreateVarFromPtr ( Rhs + 2 , "i", & no1 , & un , & OUTERBOUNDARY1 );
	LhsVar(2) = Rhs + 2;
	
	free(INNERBOUNDARY1);
	free(OUTERBOUNDARY1);
	
	return(0);
	
}

/********************************************
 * SCILAB function : RidgeMagnitudeArea     *
 ********************************************/

int gtw_RidgeMagnitudeArea ( char * fname )
{
	int m, n, l ;
	int HandleFlowDir, HandleDistance, outlet, HandleOutput ;
	float k ;
	int un=1;

	CheckRhs(5,5);
	CheckLhs(0,1);

	/* Variable HandleFlowDir */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1,m,n);
	HandleFlowDir = *istk(l);
	
	/* Variable HandleDistance */

	GetRhsVar(2,"i",&m,&n,&l);
	CheckScalar(2,m,n);
	HandleDistance = *istk(l);

	/* Variable outlet */

	GetRhsVar(3,"i",&m,&n,&l);
	CheckScalar(3,m,n);
	outlet = *istk(l);
	
	/* Variable k */

	GetRhsVar(4,"r",&m,&n,&l);
	CheckScalar(4,m,n);
	k = *sstk(l);

	/* Variable HandleOutput */

	GetRhsVar(5,"i",&m,&n,&l);
	CheckScalar(5,m,n);
	HandleOutput = *istk(l);
	
	/* Appel à l'API */

	if ( RidgeMagnitudeArea ( HandleFlowDir , HandleDistance , outlet , k , HandleOutput ) )	
		Scierror (999, GetErreur());
	
	return(0);
	
}


/********************************************
 * SCILAB function : RidgeMagnitudeDrop     *
 ********************************************/

int gtw_RidgeMagnitudeDrop ( char * fname )
{
	int m, n, l ;
	int HandleFlowDir, HandleElevation, outlet, HandleOutput ;
	int un=1;

	CheckRhs(4,4);
	CheckLhs(0,1);

	/* Variable HandleFlowDir */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1,m,n);
	HandleFlowDir = *istk(l);
	
	/* Variable HandleElevation */

	GetRhsVar(2,"i",&m,&n,&l);
	CheckScalar(2,m,n);
	HandleElevation = *istk(l);

	/* Variable outlet */

	GetRhsVar(3,"i",&m,&n,&l);
	CheckScalar(3,m,n);
	outlet = *istk(l);
	
	/* Variable HandleOutput */

	GetRhsVar(4,"i",&m,&n,&l);
	CheckScalar(4,m,n);
	HandleOutput = *istk(l);
	
	/* Appel à l'API */

	if ( RidgeMagnitudeDrop ( HandleFlowDir , HandleElevation , outlet , HandleOutput ) )	
		Scierror (999, GetErreur());
	
	return(0);
	
}

/********************************************
 * SCILAB function : LinkBodies             *
 ********************************************/

int gtw_LinkBodies ( char * fname )
{
	int m, n, l ;
	int HandleFlowDir, HandleIndic, outlet,ndown, HandleOutput,nnodes ;
	int * NODES ;
	int un=1;

	CheckRhs(5,5);
	CheckLhs(1,1);

	/* Variable HandleFlowDir */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1,m,n);
	HandleFlowDir = *istk(l);
	
	/* Variable HandleIndic */

	GetRhsVar(2,"i",&m,&n,&l);
	CheckScalar(2,m,n);
	HandleIndic = *istk(l);

	/* Variable outlet */

	GetRhsVar(3,"i",&m,&n,&l);
	CheckScalar(3,m,n);
	outlet = *istk(l);
	
	/* Variable ndown */

	GetRhsVar(4,"i",&m,&n,&l);
	CheckScalar(4,m,n);
	ndown = *istk(l);

	/* Variable HandleOutput */

	GetRhsVar(5,"i",&m,&n,&l);
	CheckScalar(5,m,n);
	HandleOutput = *istk(l);
	
	/* Appel à l'API */

	if ( LinkBodies ( HandleFlowDir , HandleIndic , outlet , ndown , HandleOutput , & NODES , & nnodes ) )	
		Scierror (999, GetErreur());
	
	CreateVarFromPtr ( Rhs + 1 , "i", & nnodes , & un , & NODES );
	LhsVar(1) = Rhs + 1;

	free(NODES);

	return(0);
	
}

