/*
 * Angle.c
 *
 *  Created on: 2009/02/20
 *      Author: H.Shouno
 *  This is a wrapper program for angle() in Neocog.c by Prof.Fukushima.
 *
 */

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "mex.h"

void mexFunction( int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[] );

/*
 * Input & Output Arguments
 */

#define U_OUT		lhs[0] // output line pattern
#define U_SIZE_IN	rhs[0] // Size of U
#define U_ANGLES_IN	rhs[1] // Angles of lines


void angle(				// draw an angle of ang degree
	double	**u,		// (out) generated pattern: u[N][N]
	int		N,			// size of the layer
	int		K,			// number of different orientations
	int		k,			// sequence number of the orientation of the edge
	double	ang,		// angle (in degree) of the bend
	double	x0,			// x offset of the center of the pattern
	double	y0			// y offset of the center of the pattern
	);


//
// available expressions...
// U = Angle( SIZE, K )
//


#define NDIMS 3

//
// Gateway function
//
void mexFunction( int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[] )
{
	int i, k, nu, nk;
	double **u, *datp;
	int dims[NDIMS];

	if( nrhs != 2 )		mexErrMsgTxt( "Angle() requires 2 input parameters: U = Angle( SIZE, K )" );
	if( nlhs != 1 )		mexErrMsgTxt( "Angle() requires an output parameter: U = Angle( SIZE, K )" );

	// right side
	nu = (int)mxGetScalar( U_SIZE_IN );
	nk = (int)mxGetScalar( U_ANGLES_IN );

	if( nu < 0 )		mexErrMsgTxt("Size is illegal.");
	dims[0] = dims[1] = nu;
	dims[2] = nk;
	U_OUT = mxCreateNumericArray( NDIMS, dims, mxDOUBLE_CLASS, mxREAL );
	datp = (double*)mxGetPr( U_OUT );
	u = (double**)mxCalloc( sizeof(double*), nu );

	for( k = 0 ; k < nk ; k++ ){
		u[0] = datp;
		for( i = 1 ; i < nu ; i++ )		u[i] = u[i-1] + nu;
		angle( u, nu, nk, k, 180.0, 0.5, -0.5 ); // TODO: Hard parameters should be modifiable
		datp += nu * nu;
	}
	return;
}
