/**********************************************************
 *                                                        *
 *                   Magnet                               *
 *                                                        *
 * Author : Ubi China - Marc Trabucato                    *
 *                                                        *
 **********************************************************/
 
#include "acp_base.h"
 
#define D_MSMagnet_StructureDefine
/*XB9800506*/
/*#include "incGEO.h"*/
#include "GEO.h"
/*#include "incGLI.h"*/
#include "GLI.h"
/*#include "incGAM.h"*/
#include "mec.h"
#include "fil.h"
#include "snd.h"
#include "pcs.h"
#include "gld.h"
#include "lst.h"
#ifdef USE_IPT_DX5
#include "IPT_DX5.h" /* InPuT (absolutely before GAM.h)*/
#else /* USE_IPT_WIN */
#include "IPT.h" /* InPuT (absolutely before GAM.h)*/
#endif /* USE_IPT_WIN */
/*#include "srf.h"*/
/*#include "env.h"*/
#include "sct.h"
#include "prt.h"
#include "gam.h"
/*End XB*/
 
#include "MGT\MGTStruc.h"
#include "MGTmm.h"
#include "MGTmo.h"
 
#ifdef MGT_RASTER
#include "MGTRast.h"
#endif
/*=======================================================================================
 * Description
 *
 * Creation date    01/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
MGT_tdxHandleToMagnetModification MGT_fn_hFindOrAddMagnetModification( ACP_tdxIndex x_Magnet , ACP_tdxIndex x_ModifiedObject , GEO_tdstGeometricObject *p_stObject )
{
	ACP_tdxIndex						xIndex , xFreeIndex;
	MGT_tdxHandleToMagnetModification	h_MagnetModif;
    MGT_tdxHandleToModifiedObject       h_ModifiedObject;
 
    h_ModifiedObject = MGT_g_aDEF_hModifiedObjects[ x_ModifiedObject ];
 
    xFreeIndex = MGT_MAX_NUMBEROF_MODIFIEDOBJECTS;
	for( xIndex = 0 ; xIndex < MGT_MAX_NUMBEROF_MAGNETS ; xIndex++ )
	{
		h_MagnetModif = h_ModifiedObject -> aDEF_hMagnet[ xIndex ];
		if( h_MagnetModif )
		{
            if( h_MagnetModif -> xIndexOfActivatedMagnet == x_Magnet )
				return h_MagnetModif;
		}
		else if( xFreeIndex == MGT_MAX_NUMBEROF_MODIFIEDOBJECTS )
		{
			xFreeIndex = xIndex;
		}
	}
 
    h_MagnetModif = MGT_fn_hCreateMagnetModification( x_Magnet, x_ModifiedObject );
 
	return h_MagnetModif;
}
 
/*=======================================================================================
 * Description
 *
 * Creation date    01/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
MGT_tdxHandleToMagnetModification MGT_fn_hCreateMagnetModification( ACP_tdxIndex x_Magnet , ACP_tdxIndex x_ModifiedObject )
{
    MGT_tdxHandleToMagnetModification   h_MagnetModif;
    MS_tdxHandleToMSMagnet              h_Magnet;
    MGT_tdxHandleToModifiedObject       h_ModifiedObject;
 
    h_ModifiedObject = MGT_g_aDEF_hModifiedObjects[ x_ModifiedObject ];
    h_Magnet         = M_GetMSHandle( MGT_g_aDEF_hActivatedMagnets[ x_Magnet ] , MSMagnet);
 
	MGT_M_pvMalloc( h_MagnetModif , MGT_tdxHandleToMagnetModification , sizeof ( MGT_tdstMagnetModification ) );
 
    h_MagnetModif -> xIndexOfActivatedMagnet    = x_Magnet;
    h_MagnetModif -> xIndexOfModifiedObject     = x_ModifiedObject;
    h_MagnetModif -> h_SuperObject              = NULL;
    h_MagnetModif -> xNbPoints                  = h_ModifiedObject -> p_stMorphedObject -> xNbPoints;
 
    MGT_fn_vAddMagnetModificationToModifiedObject( h_MagnetModif );
	fn_vMSMagnetAddMagnetModification( h_Magnet, h_MagnetModif );
 
    /**/
    MGT_M_pvMalloc( h_MagnetModif -> d_cFieldBitsInfluencedPoints , char* , MGT_M_BYTESFORBITS( h_MagnetModif -> xNbPoints ) );
    MGT_fn_vClearInfluencedPointsOfMagnetModification( h_MagnetModif );
 
    MGT_M_pvMalloc( h_MagnetModif -> d_cFieldBitsModifiedPoints , char* , MGT_M_BYTESFORBITS( h_MagnetModif -> xNbPoints ) );
    MGT_fn_vClearModifiedPointsOfMagnetModification( h_MagnetModif );
 
    MGT_M_pvMalloc( h_MagnetModif -> d_ulCurrentDuration, unsigned long* , h_MagnetModif -> xNbPoints * sizeof ( unsigned long ) );
    memset( h_MagnetModif -> d_ulCurrentDuration, 0, h_MagnetModif -> xNbPoints * sizeof ( unsigned long ) );
 
	return h_MagnetModif;
}
 
/*=======================================================================================
 * Description
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_vClearModifiedPointsOfMagnetModification( MGT_tdxHandleToMagnetModification h_MagnetModif )
{
    memset( h_MagnetModif -> d_cFieldBitsModifiedPoints, 0, MGT_M_BYTESFORBITS( h_MagnetModif -> xNbPoints ) );
}
 
/*=======================================================================================
 * Description
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_vClearInfluencedPointsOfMagnetModification( MGT_tdxHandleToMagnetModification h_MagnetModif )
{
    memset( h_MagnetModif -> d_cFieldBitsInfluencedPoints, 0, MGT_M_BYTESFORBITS( h_MagnetModif -> xNbPoints ) );
}
 
/*=======================================================================================
 * Description		Free MagnetModification and delete it from ModifiedObject and MSMagnet
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_vDeleteMagnetModification( MGT_tdxHandleToMagnetModification h_MagnetModif )
{
    MGT_M_Free( h_MagnetModif -> d_cFieldBitsInfluencedPoints );
 
    MGT_M_Free( h_MagnetModif -> d_cFieldBitsModifiedPoints );
 
    MGT_M_Free( h_MagnetModif -> d_ulCurrentDuration );
 
    /**/
    if( h_MagnetModif -> xIndexOfModifiedObject != MGT_C_xIndexError )
        MGT_fn_vDeleteMagnetModificationToModifiedObject( h_MagnetModif );
	/**/
	if( ( h_MagnetModif -> xIndexOfActivatedMagnet != MGT_C_xIndexError ) &&
		MGT_g_aDEF_hActivatedMagnets[ h_MagnetModif -> xIndexOfActivatedMagnet ] &&
		M_GetMSHandle( MGT_g_aDEF_hActivatedMagnets[ h_MagnetModif -> xIndexOfActivatedMagnet ] , MSMagnet) )
		fn_vMSMagnetDeleteMagnetModification( M_GetMSHandle( MGT_g_aDEF_hActivatedMagnets[ h_MagnetModif -> xIndexOfActivatedMagnet ] , MSMagnet), h_MagnetModif );
 
	MGT_M_Free( h_MagnetModif );
}
 
/*=======================================================================================
 * Description
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
ACP_tdxBool	MGT_fn_bIsExistModifiedPointsOnMagnetModification( MGT_tdxHandleToMagnetModification h_MagnetModif )
{
	ACP_tdxIndex	xSize , xCurrent;
 
    xSize   = MGT_M_BYTESFORBITS( h_MagnetModif -> xNbPoints );
 
	for( xCurrent = 0 ; xCurrent < xSize ; xCurrent++ )
	{
		if( h_MagnetModif -> d_cFieldBitsModifiedPoints[ xCurrent ] )
			break;
	}
 
	return (xCurrent != xSize);
}
