/**************************************************************************\

							tB^[֐

  : 
    SampleCode filter̃Cve[V

  o[W:

\**************************************************************************/

#include "stdafx.h"
#include "SampleCode.h"
#include <stdio.h>
#include "MAapi.h"

#define MODULE_NAME "SampleCode filter module"

/**************************************************************************\
                             MAuser_function()

@F
   MAuser_function֐́AMicroAVS̃j[ł̃vOC[hꂽ
   ɃR[܂BMicroAVSŕ\f[^̕ύXsɂ́Å֐
   MicroAVSAPIR[ĂB
\**************************************************************************/
int __stdcall MAuser_function()
{
	int ndim, ncomp;
	xp_long nnodes;
	int veclen;
	int result;

	// m[h擾
	result = MAget_nnodes(&nnodes);
	if(result == MAERROR)
	{
		MessageBox(NULL, "MAget_nnodes failed.", MODULE_NAME, MB_OK);
		return 0;
	}

	// fBW̏擾
	result = MAget_ndim(&ndim);
	if(result == MAERROR)
	{
		MessageBox(NULL, "MAget_ndim failed.", MODULE_NAME, MB_OK);
		return 0;
	}

	// z̃TCY擾
	xp_long points;
	result = MAget_npoints(&points);
	if(result == MAERROR)
	{
		MessageBox(NULL, "MAget_npoints failed.", MODULE_NAME, MB_OK);
		return 0;
	}

	// m
	float* pPoints = (float*)MAalloc(MA_DTYPE_FLOAT, points);
	if(pPoints == NULL)
	{
		MessageBox(NULL, "Not enough memory.", MODULE_NAME, MB_OK);
		return 0;
	}

	// Uniform^ARectilinear^̏ꍇ̍W̎擾
	xp_long size;
	result = MAget_points(&pPoints, &size);
	if(result == MAERROR)
	{
		MAfree(pPoints);
		MessageBox(NULL, "MAget_points failed.", MODULE_NAME, MB_OK);
		return 0;
	}

	// Uniform^ARectilinear^̏ꍇ̍W̐ݒs
	result = MAset_points(pPoints, size);
	MAfree(pPoints);
	if(result == MAERROR)
	{
		MessageBox(NULL, "MAset_points failed.", MODULE_NAME, MB_OK);
		return 0;
	}

	// W̎擾
	float* pCoordinates = (float*)MAalloc(MA_DTYPE_FLOAT, nnodes*ndim);
	if(pCoordinates == NULL)
	{
		MessageBox(NULL, "Not enough memory.", MODULE_NAME, MB_OK);
		return 0;
	}

	result = MAget_coordinates(&pCoordinates, &size);
	if(result == MAERROR)
	{
		MAfree(pCoordinates);
		MessageBox(NULL, "MAget_coordinates failed.", MODULE_NAME, MB_OK);
		return 0;
	}

	// W̐ݒ
	result = MAset_coordinates(pCoordinates, size);
	MAfree(pCoordinates);
	if(result == MAERROR)
	{
		MessageBox(NULL, "MAset_coordinates failed.", MODULE_NAME, MB_OK);
		return 0;
	}

	// f[^̐ݒ肷
	result = MAget_node_ncomp(&ncomp);
	if(result == MAERROR)
	{
		MessageBox(NULL, "MAget_node_ncomp failed.", MODULE_NAME, MB_OK);
		return 0;
	}

	for(int i=0; i<ncomp; i++)
	{
		result = MAget_node_data_veclen(i, &veclen);
		if(result == MAERROR)
		{
			MessageBox(NULL, "MAset_node_veclen failed.", MODULE_NAME, MB_OK);
			return 0;
		}

		xp_long data_size = nnodes*veclen;

#ifdef WIN64
		xp_long* pValue = (xp_long*)MAalloc(MA_DTYPE_LONG, data_size);
#else
		xp_long* pValue = (xp_long*)MAalloc(MA_DTYPE_INT, data_size);
#endif
		if(pValue == NULL)
		{
			MessageBox(NULL, "Out of memory.", MODULE_NAME, MB_OK);
			return 0;
		}

		xp_long* p = pValue;
		xp_long j;
		for(j=0; j<data_size; j++)
		{
			*p++ = 10;
		}

		//VɃf[^ݒ肷
#ifdef WIN64
		result = MAset_node_data(i, (char*)pValue, data_size, MA_DTYPE_LONG);
#else
		result = MAset_node_data(i, (char*)pValue, data_size, MA_DTYPE_INT);
#endif
		if(result == MAERROR)
		{
			MessageBox(NULL, "MAset_node_data failed.", MODULE_NAME, MB_OK);
			MAfree(pValue);
			return 0;
		}
		MAfree(pValue);
	}
	
	return 1;
}

/**************************************************************************\
                             MAuser_function_name()

@F
   MAuser_function_name֐́AMicroAVS̋NɁAvOC̃j[
   \镶ݒ肵܂B
\**************************************************************************/
void __stdcall MAuser_function_name(char* szFilterName)
{
	strcpy(szFilterName, "SampleCode filter");
}

