/*
///////////////////////////////////////////////////////////////////////////////////////////////////
// Description : LinkArc.cpp
//   
//  Definition of the circle arc            
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// inherit from  :	Link2D
///////////////////////////////////////////////////////////////////////////////////////////////////
// Creation date: 30 jan 1997                        Author: J Thénoz
///////////////////////////////////////////////////////////////////////////////////////////////////
// Modification date:			                      Author: 
// 
//
// 
//
///////////////////////////////////////////////////////////////////////////////////////////////////
*/
//ANNECY Shaitan Nettoyage (12/05/98) {
/*

#include "stdafx.h"
#include "acp_base.h"
#include "resource.h"
#include "incitf.h"
#include "incgam.h"
#define _ENG_CIRCLE_FRIEND_
#include "incai.h"
#undef _ENG_CIRCLE_FRIEND_
#include "WpObj.hpp"
#include "WayObj.hpp"
#include "LinkObj.hpp"
#include "CurObj.hpp"
#include "Link2D.hpp"
#include "LinkArc.hpp"
#include "LkArcDia.h"
#include "Inter.hpp"


//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 

char LinkArc::ms_cType = WP_ConnType_ucCircle;						
char LinkArc::ms_szName [10] = "Arc";

int iJustToLinkCiArBidouille;
void JustToLinkCiAr (void) {   iJustToLinkCiArBidouille=1;  }


void LinkArc::fn_vRegisterObject (void)
{
	tdst_LinkObject* pstLinkObject = new tdst_LinkObject;
	strcpy ( pstLinkObject->szName, ms_szName );
	pstLinkObject->cType = ms_cType;
	pstLinkObject->fn_pConstruct = fn_pConstruct;
	ms_oListLinkObject.AddTail (pstLinkObject);
	JustToLinkCiAr ();
}

LinkArc::LinkArc ( CPA_ObjectDLLBase *p_oDLL, CPA_BaseObject *p_oOwner, WP_tdhLink hLink )
	: Link2D (  p_oDLL, p_oOwner, hLink )
{
	m_bInitCenter = FALSE;
	
	MTH3D_tdstVector stNulVector = { 0, 0, 0 };
	hLink = (WP_tdhLink)GetEngineStruct();
	
//-------------------------------------
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
	m_hArcObject.CreateFromCurve
	(
		WP_fnv_Link_GetConnectionTypeCircleArcCurve (hLink),		
		EDWAY_C_ucModeNoObject,
		NULL,
		&WP_fnh_Link_GetEngineCircleArcObject(hLink)->m_dstListOfPoints
	);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 
//-------------------------------------

	m_bEditPlane = FALSE;
	m_poCursor=NULL;
	m_stCenterVertex=stNulVector;
	m_bFirstEdition=TRUE;
}

LinkArc::LinkArc ( CPA_ObjectDLLBase *p_oDLL, CPA_BaseObject *p_oOwner )
	: Link2D (  p_oDLL, p_oOwner )
{
	m_bInitCenter = TRUE;

	MTH3D_tdstVector stNulVector = { 0, 0, 0 };
	WP_tdhLink hLink = (WP_tdhLink)GetEngineStruct();
	
	
	// allocate circle structure
	WP_fnv_Link_Allocate
		(
			hLink,
			WP_ConnType_ucCircle,
			WP_fneuc_Link_GetConnectionDynamicType (hLink)
		);

//------------------------------------------------
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
	m_hArcObject.CreateFromCurve
	(
		WP_fnv_Link_GetConnectionTypeCircleArcCurve (hLink),		
		EDWAY_C_ucModeNoObject,
		NULL,
		&WP_fnh_Link_GetEngineCircleArcObject(hLink)->m_dstListOfPoints
	);
	// init sampling rate values
	WP_fnv_Link_SetCurrentSample (hLink, 15);
//	EDWAY_fnb_CircleArcObject_ChangeSamplingRate ( m_hArcObject, 15 );
	m_hArcObject.ChangeSamplingRate (15 );
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 
//------------------------------------------------

	m_bEditPlane = FALSE;
	m_poCursor=NULL;
	m_stCenterVertex=stNulVector;
	m_bFirstEdition=TRUE;
}


CDialog* LinkArc::fn_pGetSpecificDialog (CWnd* pParentWnd)
{
	ArcDia* pDialog = new ArcDia (this);
	pDialog->Create ( IDD_ARC_DIALOG, pParentWnd );
	pDialog->fn_vTutRegister ();
	return (CDialog*) pDialog;
}

void LinkArc::fn_vFreeSpecificDialog (CDialog* pDialog)
{
	((ArcDia*)pDialog)->fn_vTutUnregister ();
}


void LinkArc::fn_vConstructGraphic (void)
{
}

void LinkArc::fn_vRotationGraphic (BOOL bRotation)
{
	Link::fn_vRotationGraphic (bRotation);
	if (bRotation)
	{
		if (m_bEditPlane)
			fn_vStopEdit ();
	}
	else
	{
	
	}
}

void LinkArc::fn_vDrawNormal (void)
{
	fn_vComputeAxis ();
	if (m_bEditPlane) {
		fn_vDrawPlane ();
		m_poCursor->fn_vDraw ();
		fn_vSetEngineStructure ();
	}
}

void LinkArc::fn_vDrawOutHierarchy (GLD_tdstViewportAttributes *pViewportAttributes)
{
	if (m_bEditPlane) 
	{
		fn_vUpdateCenter ();
	}
	fn_vSetEngineStructure ();

//----------------------------------------------
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
	m_hArcObject.SetColor(0xFFFFFFFF);
	m_hArcObject.SetViewPortAttributes	(pViewportAttributes);
	m_hArcObject.Draw();
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 
//----------------------------------------------
}

void LinkArc::fn_vSetWaypoints ( WayPoint* poFirstWP, WayPoint* poSecondWP )
{
	Link::fn_vSetWaypoints ( poFirstWP, poSecondWP );
}

void LinkArc::fn_vInitGraphic (void)
{
	Link::fn_vInitGraphic ();
	fn_vInitCenter (m_bInitCenter);
	m_bInitCenter=FALSE;
}


void LinkArc::fn_vStartEdit (void)
{
	if (m_bRotationMode) return;
	
	fn_vSetDrawOutHierarchy (FALSE);
	
	//---- plane
	fn_vConstructPlane();
	if (m_bFirstEdition)
	{
		fn_vInitPlaneOrientation ();
		m_bFirstEdition=FALSE;
	}
	fn_vComputeAxis ();
	m_bEditPlane = TRUE;
	fn_vDrawPlane ();
	
	//----- cursor
	// create
	m_poCursor= new Cursor (  GetInterface(), m_poGraphic, eCursorCircularLimit );

	// insert in hierarchy
	m_poCursor->GetSuperObject()->SetSuperObjectOwner (m_poGraphic);	
	GetInterface()->GetInterface()->fn_bInsertObjectInHierarchy ( m_poCursor->GetSuperObject(), m_poGraphic, FALSE, FALSE, FALSE );
	m_poCursor->GetSuperObject()->SetEditProtected (TRUE); 
	// compute absolute matrix
	GetInterface()->fn_vComputeAbsoluteMatrix (m_poCursor->GetSuperObject());

	// compute the third point with the center
	MTH3D_tdstVector stCursorVertex;
	fn_vComputeCursorVertex ( &stCursorVertex );
	m_poCursor->fn_vSetAbsoluteVertex (&stCursorVertex);

	m_poCursor->fn_vDraw ();
	fn_vSetDrawOutHierarchy (TRUE);
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
}

void LinkArc::fn_vStopEdit (void)
{
	m_bEditPlane = FALSE;
	fn_vDestroyPlane();
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
}

void LinkArc::fn_vSetEngineStructure (void)
{
	MTH3D_tdstVector	stStartPoint;
	MTH3D_tdstVector	stEndPoint;
	MTH3D_tdstVector	stCenterPoint;

	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stStartPoint );
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stEndPoint );
	
	fn_vGetAbsoluteCenter (&stCenterPoint);
	
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
//	EDWAY_fnv_CircleArcObject_ChangeParams (m_hArcObject,&stStartPoint, &stEndPoint, &stCenterPoint);
	m_hArcObject.ChangeParams (&stStartPoint, &stEndPoint, &stCenterPoint);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 
	if ( m_poCursor )
		if ( m_poCursor->fn_bCursorMove() ) fn_vWayNotifySave ();
}

void LinkArc::fn_vUpdateCenter (void)
{
	MTH3D_tdstVector stStartPoint, stEndPoint;
	MTH3D_tdstVector stLocalFirstVertex, stLocalSecondVertex, stLocalThirdVertex;
	MTH3D_tdstVector stAbsoluteCenter;
	MTH3D_tdstVector stCenter;
	
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stStartPoint );
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stEndPoint );
	
	// local vertex
	CPA_SuperObject* psoPlane = (CPA_SuperObject*)GetSuperObject()->GetHead();
	GetInterface()->fn_vComputeAbsoluteMatrix (psoPlane);
	GEO_tdxHandleToMatrix hAbsolutePlaneMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (psoPlane->GetStruct());
	POS_tdstCompletePosition InvMatrix;
	
	POS_fn_vSetIdentityMatrix(&InvMatrix);
	POS_fn_vInvertMatrix( &InvMatrix , hAbsolutePlaneMatrix );
	POS_fn_vMulMatrixVertex( &stLocalFirstVertex, &InvMatrix, &stStartPoint ) ;
	POS_fn_vMulMatrixVertex( &stLocalSecondVertex, &InvMatrix, &stEndPoint ) ;
	m_poCursor->fn_vGetAbsoluteVertex ( &stAbsoluteCenter );
	POS_fn_vMulMatrixVertex( &stLocalThirdVertex, &InvMatrix, &stAbsoluteCenter ) ;
	
	
	MTH3D_fnv_CircleArcCurve_FindCenter
	(
		&stLocalFirstVertex,
		&stLocalSecondVertex,
		&stLocalThirdVertex,
		&stCenter 
	);
	
	POS_fn_vMulMatrixVertex( &stAbsoluteCenter, hAbsolutePlaneMatrix, &stCenter );
	fn_vSetAbsoluteCenter ( &stAbsoluteCenter );
}

void LinkArc::fn_vInitCenter (BOOL bInit)
{
	if (bInit)
	{
		MTH3D_tdstVector stFirstVertex, stSecondVertex;
		MTH3D_tdstVector u, v, w;
		MTH3D_tdstVector stAbsoluteCenter;
		WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stFirstVertex );
		WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stSecondVertex );
		u = stSecondVertex;
		MTH3D_M_vSubVector(&u,&u,&stFirstVertex);
		MTH3D_M_vMulScalarVector(&u,0.5f,&u);
		MTH3D_M_vSetVectorElements ( &w, 0.f, 0.f, 1.f );
		MTH3D_M_vCrossProductVector ( &v, &w, &u );
		MTH3D_M_vNormalizeVector(&v,&v);
		MTH3D_M_vMulScalarVector(&v,1e-3f,&v);
		stAbsoluteCenter = stFirstVertex;
		MTH3D_M_vAddVector(&stAbsoluteCenter,&stAbsoluteCenter,&u);
		MTH3D_M_vAddVector(&stAbsoluteCenter,&stAbsoluteCenter,&v);
		fn_vSetAbsoluteCenter (&stAbsoluteCenter);
	}
	else
	{
		MTH3D_tdstVector stFirstPoint, stSecondPoint, stCenterPoint;
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
//		EDWAY_fnv_CircleArcObject_GetParams (m_hArcObject, &stFirstPoint, &stSecondPoint, &stCenterPoint);
		m_hArcObject.GetParams (&stFirstPoint, &stSecondPoint, &stCenterPoint);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 

		fn_vSetAbsoluteCenter (&stCenterPoint);
	}
}

void LinkArc::fn_vGetAbsoluteCenter (MTH3D_tdstVector* pstCenter)
{
	GetInterface()->fn_vComputeAbsoluteMatrix (GetSuperObject());
	GEO_tdxHandleToMatrix hAbsoluteMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (GetSuperObject()->GetStruct());
	POS_fn_vMulMatrixVertex( pstCenter, hAbsoluteMatrix, &m_stCenterVertex );
}

void LinkArc::fn_vSetAbsoluteCenter (MTH3D_tdstVector* pstCenter)
{
	GetInterface()->fn_vComputeAbsoluteMatrix (GetSuperObject());
	GEO_tdxHandleToMatrix hAbsoluteMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (GetSuperObject()->GetStruct());
	POS_tdstCompletePosition InvMatrix;
	POS_fn_vSetIdentityMatrix(&InvMatrix);
	POS_fn_vInvertMatrix( &InvMatrix , hAbsoluteMatrix );
	POS_fn_vMulMatrixVertex( &m_stCenterVertex, &InvMatrix, pstCenter );
}

void LinkArc::fn_vComputeCursorVertex ( MTH3D_tdstVector* pstCursorVertex )
{
	MTH3D_tdstVector stFirstVertex, stSecondVertex;
	MTH3D_tdstVector stCenterVertex;
	MTH3D_tdstVector stMildVertex;
	MTH3D_tdstVector stVector;
	MTH3D_tdstVector stRadiusVector;

	
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stFirstVertex );
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stSecondVertex );
	fn_vGetAbsoluteCenter ( &stCenterVertex );
	
	stMildVertex = stFirstVertex;
	MTH3D_M_vAddVector(&stMildVertex,&stMildVertex,&stSecondVertex);
	MTH3D_M_vMulScalarVector(&stMildVertex,0.5f,&stMildVertex);
	
	stVector = stMildVertex;
	MTH3D_M_vSubVector(&stVector,&stVector,&stCenterVertex);
	MTH3D_M_vNormalizeVector(&stVector,&stVector);

	stRadiusVector = stFirstVertex;
	MTH3D_M_vSubVector(&stRadiusVector,&stRadiusVector,&stCenterVertex);
	MTH_tdxReal stRadius = GLI_M_Norme(&stRadiusVector);

	MTH3D_M_vMulScalarVector(&stVector,stRadius,&stVector);

	*pstCursorVertex = stCenterVertex;
	MTH3D_M_vAddVector(pstCursorVertex,pstCursorVertex,&stVector);
}


void LinkArc::fn_vGetSaveInfo ( char dszParam [20][20], long& lNbParam )
{
	unsigned char ucSamplingRate;
	MTH3D_tdstVector stFirstPoint, stSecondPoint, stCenterPoint;
	
	ucSamplingRate = WP_fnuc_Link_GetCurrentSample (GetStruct());
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
//	EDWAY_fnv_CircleArcObject_GetParams (m_hArcObject, &stFirstPoint, &stSecondPoint, &stCenterPoint);
	m_hArcObject.GetParams (&stFirstPoint, &stSecondPoint, &stCenterPoint);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 


	strcpy ( dszParam [0], "Circle" );
	lNbParam = 2;

	sprintf ( dszParam[lNbParam++], "%d", ucSamplingRate); 

	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetXofVector(&stCenterPoint) );
	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetYofVector(&stCenterPoint) );
	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetZofVector(&stCenterPoint) );
}

void LinkArc::fn_vInitPlaneOrientation (void)
{
	MTH3D_tdstVector stFirstPoint, stSecondPoint, stCenterPoint;
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
//	EDWAY_fnv_CircleArcObject_GetParams (m_hArcObject, &stFirstPoint, &stSecondPoint, &stCenterPoint);
	m_hArcObject.GetParams (&stFirstPoint, &stSecondPoint, &stCenterPoint);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 

	fn_vComputePlaneAxis ( &stCenterPoint );
}

*/
//ENDANNECY Shaitan Nettoyage }
