/* Pattern.c */
/* David Weenink, 19990221 */

#include "Pattern.h"

class_methods (Pattern, Matrix)
class_methods_end

int Pattern_init (I, long ny, long nx)
{
    iam (Pattern);
    my ny = ny;
    my nx = nx;
    if (! Matrix_init (me, 1, nx, nx, 1, 1, 1, ny, ny, 1, 1)) return 0;
    return 1;
}

Any Pattern_create (long ny, long nx)
{
    Pattern me = new (Pattern);
    if (! me || ! Pattern_init (me, ny, nx)) forget (me);
    return me;
}

void Pattern_normalize (I, int choice, double pmin, double pmax)
{
    iam (Pattern); long i, j;
    if (pmin == pmax) (void) Matrix_getWindowExtrema (me, 1, my nx, 1, my ny, & pmin, & pmax);
    if (pmin == pmax) return;
    if (choice == 1)
    {
		for (i=1; i <= my ny; i++) for (j=1; j <= my nx; j++)
			my z[i][j] = ( my z[i][j] - pmin) / ( pmax - pmin);
    }
    else /* default choice */
    {
		for (i=1; i <= my ny; i++)
		{
			double sum = 0;
			for (j=1; j <= my nx; j++) sum += ( my z[i][j] -= pmin);
			for (j=1; j <= my nx; j++) my z[i][j] *= 1.0 / sum;
		}
	}
}

void Pattern_draw (I, Graphics g, long pattern, double xmin, double xmax,
	double ymin, double ymax, int garnish)
{
	iam (Pattern);
	Matrix_drawRows (me, g, xmin, xmax, pattern - 0.5, pattern + 0.5, ymin, ymax);
	if (garnish)
	{
		Graphics_drawInnerBox (g);
    	Graphics_marksBottom (g, 2, 1, 1, 0);
    	Graphics_marksLeft (g, 2, 1, 1, 0);		
	}
}

Pattern Matrix_to_Pattern (I, int join)
{
    iam (Matrix); long i, j, r = 0, c; Pattern thee = NULL;
    if (join < 1) join = 1;
	if ((my ny % join) != 0) return Melder_errorp("Matrix_to_Pattern:"
		"number of rows is not a multiple of join."); 
	if (! (thee = Pattern_create (my ny / join, join * my nx))) return thee;
	for (i=1; i <= my ny; i++)
	{
		if ((i - 1) % join == 0) { r++; c = 1; }
		for (j=1; j <= my nx; j++) thy z[r][c++] = my z[i][j];
	}
    return thee;
}

Matrix Pattern_to_Matrix (Pattern me)
{
	Matrix thee = Data_copy (me);
	if (thee) Thing_overrideClass (thee, classMatrix);
	return thee;
}

/* End of file Pattern.c */
