/**********************************************************
 *                                                        *
 *                   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 "MGTmo.h"
#include "MGTmm.h"
 
#ifdef MGT_RASTER
#include "MGTRast.h"
#endif
 
HIE_tdxHandleToSuperObject      MGT_g_aDEF_hActivatedMagnets[MGT_MAX_NUMBEROF_MAGNETS];  /* handle of activated magnets */
ACP_tdxIndex                    MGT_g_xMaxIndexOfActivatedMagnet;
MGT_tdxHandleToModifiedObject   MGT_g_aDEF_hModifiedObjects[MGT_MAX_NUMBEROF_MODIFIEDOBJECTS];
ACP_tdxIndex                    MGT_g_xMaxIndexOfModifiedObject;
 
/*=======================================================================================
 * Description
 *
 * Creation date    01/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
ACP_tdxIndex MGT_fn_xCreateModifiedObject( GEO_tdstGeometricObject *p_stObject )
{
	MGT_tdxHandleToModifiedObject h_ModifiedObject , *p_h_ModifiedObject;
    ACP_tdxIndex				  xIndex;
 
	MGT_M_pvMalloc( h_ModifiedObject, MGT_tdxHandleToModifiedObject , sizeof ( MGT_tdstModifiedObject ) );
 
	h_ModifiedObject -> p_stMorphedObject = p_stObject;
 
    for( xIndex = 0 ; xIndex < MGT_MAX_NUMBEROF_MAGNETS ; xIndex++ )
        h_ModifiedObject -> aDEF_hMagnet[ xIndex ] = NULL;
 
    MGT_M_pvMalloc( h_ModifiedObject -> d_cFieldBitsAlreadyInfluencedPoints , char* , MGT_M_BYTESFORBITS( p_stObject -> xNbPoints ) );
    MGT_fn_vClearAlreadyInfluencedPointsOfModifiedObject( h_ModifiedObject );
 
    MGT_M_pvMalloc( h_ModifiedObject -> d_stOriginalPosition , MTH3D_tdstVector* , p_stObject -> xNbPoints * sizeof( MTH3D_tdstVector ) );
    memcpy( h_ModifiedObject -> d_stOriginalPosition , p_stObject -> d_stListOfPoints , p_stObject -> xNbPoints * sizeof( MTH3D_tdstVector ) );
 
	h_ModifiedObject -> p_stOctree = p_stObject -> p_stOctree;
	p_stObject -> p_stOctree = NULL;
 
	h_ModifiedObject -> h_SuperObject = NULL;
 
	/* add to global list*/
    for ( xIndex = 0 , p_h_ModifiedObject = MGT_g_aDEF_hModifiedObjects ;
		  xIndex < MGT_g_xMaxIndexOfModifiedObject ;
		  xIndex++, p_h_ModifiedObject++ )
    {
		if( (*p_h_ModifiedObject) == NULL )
		{
			*p_h_ModifiedObject = h_ModifiedObject;
            return xIndex;
			break;
		}
	}
 
    if( MGT_g_xMaxIndexOfModifiedObject < MGT_MAX_NUMBEROF_MODIFIEDOBJECTS )
    {
        *p_h_ModifiedObject = h_ModifiedObject;  /* xIndex = MGT_g_xMaxIndexOfModifiedObject*/
        MGT_g_xMaxIndexOfModifiedObject++;
        return xIndex;
    }
    return MGT_C_xIndexError;
}
 
/*=======================================================================================
 * Description
 *
 * Creation date    01/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
ACP_tdxIndex MGT_fn_xFindModifiedObject( GEO_tdstGeometricObject *p_stObject )
{
    MGT_tdxHandleToModifiedObject   *p_h_ModifiedObject;
    ACP_tdxIndex                    xObjectIndex;
 
    for ( xObjectIndex = 0 , p_h_ModifiedObject = MGT_g_aDEF_hModifiedObjects ; xObjectIndex < MGT_g_xMaxIndexOfModifiedObject ; xObjectIndex++, p_h_ModifiedObject++ )
    {
        if( *p_h_ModifiedObject && ( (*p_h_ModifiedObject) -> p_stMorphedObject == p_stObject ) )
            return xObjectIndex;
    }
    return MGT_C_xIndexError;
}
 
/*=======================================================================================
 * Description
 *
 * Creation date    01/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_vAddMagnetModificationToModifiedObject( MGT_tdxHandleToMagnetModification h_MagnetModif )
{
    ACP_tdxIndex                    xIndex;
    MGT_tdxHandleToModifiedObject   h_ModifiedObject;
 
    h_ModifiedObject = MGT_g_aDEF_hModifiedObjects[ h_MagnetModif -> xIndexOfModifiedObject ];
 
    for( xIndex = 0 ; xIndex < MGT_MAX_NUMBEROF_MAGNETS ; xIndex++ )
	{
        if( h_ModifiedObject -> aDEF_hMagnet[ xIndex ] == NULL )
        {
            h_ModifiedObject -> aDEF_hMagnet[ xIndex ] = h_MagnetModif;
			break;
        }
	}
}
 
/*=======================================================================================
 * Description
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_vClearAlreadyInfluencedPointsOfModifiedObject( MGT_tdxHandleToModifiedObject h_ModifiedObject )
{
    memset( h_ModifiedObject -> d_cFieldBitsAlreadyInfluencedPoints, 0, MGT_M_BYTESFORBITS( h_ModifiedObject -> p_stMorphedObject -> xNbPoints ) );
}
 
/*=======================================================================================
 * Description		Delete MagnetModification from ModifiedObject
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_vDeleteMagnetModificationToModifiedObject( MGT_tdxHandleToMagnetModification h_MagnetModif )
{
    ACP_tdxIndex                    xIndex;
    MGT_tdxHandleToModifiedObject   h_ModifiedObject;
 
    h_ModifiedObject = MGT_g_aDEF_hModifiedObjects[ h_MagnetModif -> xIndexOfModifiedObject ];
 
    for( xIndex = 0 ; xIndex < MGT_MAX_NUMBEROF_MAGNETS ; xIndex++ )
    {
        if( h_ModifiedObject -> aDEF_hMagnet[ xIndex ] == h_MagnetModif )
        {
            h_ModifiedObject -> aDEF_hMagnet[ xIndex ] = NULL;
			break;
        }
    }
}
 
/*=======================================================================================
 * Description		Free ModifiedObject and delete it from global list
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_vDeleteModifiedObject( ACP_tdxIndex x_ModifiedObject )
{
    GEO_tdstGeometricObject         *p_stObject;
    MGT_tdxHandleToModifiedObject   h_ModifiedObject;
 
    h_ModifiedObject = MGT_g_aDEF_hModifiedObjects[ x_ModifiedObject ];
    p_stObject       = h_ModifiedObject -> p_stMorphedObject;
 
    MGT_M_Free( h_ModifiedObject -> d_cFieldBitsAlreadyInfluencedPoints );
 
    memcpy( p_stObject -> d_stListOfPoints , h_ModifiedObject -> d_stOriginalPosition , p_stObject -> xNbPoints * sizeof( MTH3D_tdstVector ) );
    MGT_M_Free( h_ModifiedObject -> d_stOriginalPosition );
	p_stObject -> p_stOctree = h_ModifiedObject -> p_stOctree;
	GEO_vEndModifyObject2(p_stObject);
 
    MGT_g_aDEF_hModifiedObjects[ x_ModifiedObject ] = NULL;
    if( x_ModifiedObject == MGT_g_xMaxIndexOfModifiedObject-1 )
        MGT_g_xMaxIndexOfModifiedObject--;
 
    MGT_M_Free( h_ModifiedObject );
}
 
/*=======================================================================================
 * Description		Free ModifiedObject and delete it from global list
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_hDeleteIfExistsMagnetModification( ACP_tdxIndex x_ModifiedObject, ACP_tdxIndex x_Magnet )
{
    ACP_tdxIndex                      xIndex;
    MGT_tdxHandleToMagnetModification *p_h_MagnetModif;
	MGT_tdxHandleToModifiedObject		h_ModifiedObject;	
 
	h_ModifiedObject = MGT_g_aDEF_hModifiedObjects[ x_ModifiedObject ];
 
    for( xIndex = 0 , p_h_MagnetModif = h_ModifiedObject -> aDEF_hMagnet ;
		 xIndex < MGT_MAX_NUMBEROF_MAGNETS ;
		 xIndex++, p_h_MagnetModif++ )
    {
		if( *p_h_MagnetModif && ( (*p_h_MagnetModif) -> xIndexOfActivatedMagnet == x_Magnet ) )
		{
			MGT_fn_vDeleteMagnetModification( *p_h_MagnetModif );
			break;
        }
    }
}
 
/*=======================================================================================
 * Description		Delete Modified Object if it has no MagnetModification
 *
 * Creation date    02/03/98
 * Author           Marc Trabucato
 *=====================================================================================*/
void MGT_fn_vDeleteModifiedObjectIfNeeded( ACP_tdxIndex x_ModifiedObject )
{
    ACP_tdxIndex                    xIndex;
    MGT_tdxHandleToModifiedObject   h_ModifiedObject;
 
    h_ModifiedObject = MGT_g_aDEF_hModifiedObjects[ x_ModifiedObject ];
 
    for( xIndex = 0 ; xIndex < MGT_MAX_NUMBEROF_MAGNETS ; xIndex++ )
    {
		if( h_ModifiedObject -> aDEF_hMagnet[ xIndex ] != NULL )
			break;
	}
	if( xIndex == MGT_MAX_NUMBEROF_MAGNETS )
        MGT_fn_vDeleteModifiedObject( x_ModifiedObject );
}
