/*
///////////////////////////////////////////////////////////////////////////////////////////////////
// Description : Link2D.cpp
//   
//  Definition of links defined in a 2D plane            
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// inherit from  :	Link
///////////////////////////////////////////////////////////////////////////////////////////////////
// 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 "incitf.h"
#include "incgam.h"
#include "incai.h"
#include "WPObj.hpp"
#include "WayObj.hpp"
#include "LinkObj.hpp"
#include "Inter.hpp"
#include "Link2D.hpp"

Link2D::Link2D ( CPA_ObjectDLLBase *p_oDLL, CPA_BaseObject *p_oOwner, WP_tdhLink hLink )
	: Link (  p_oDLL, p_oOwner, hLink )
{
	m_xe = 0.025f;
}

Link2D::Link2D ( CPA_ObjectDLLBase *p_oDLL, CPA_BaseObject *p_oOwner )
	: Link (  p_oDLL, p_oOwner )
{
	m_xe = 0.025f;
}


void Link2D::fn_vConstructPlane (void)
{
	m_poGraphic = GetInterface()->fn_pGetNewPlaneGraphicObject();
	m_poGraphic->SetSuperObjectOwner (GetSuperObject());	
	GetInterface()->GetInterface()->fn_bInsertObjectInHierarchy (m_poGraphic,GetSuperObject(),FALSE,FALSE,FALSE);
	GEO_xComputeObjectNormals ( (ACP_tdxHandleOfObject)HIE_fn_hGetSuperObjectObject(m_poGraphic->GetStruct()));
	m_poGraphic->SetEditProtected (TRUE); 
}


void Link2D::fn_vDestroyPlane (void)
{
	GetInterface()->fn_vSetModifDeleteOk (TRUE);
	GetInterface()->GetInterface()->fn_bDeleteObjectInHierarchy (m_poGraphic,FALSE,FALSE,TRUE,FALSE);
	GetInterface()->fn_vSetModifDeleteOk (FALSE);
	m_poGraphic = NULL;
}

void Link2D::fn_vDrawPlane (void)
{
	MTH3D_tdstVector stFirstVertex, stSecondVertex;
	GLI_tdxValue d5, md5;

	tdeLocalColor tdColor = GetSuperObject()->GetLocalColor ();

	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stFirstVertex );
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stSecondVertex );

	MTH3D_tdstVector n, u, v;

	ACP_tdxHandleOfObject hMot = (ACP_tdxHandleOfObject) HIE_fn_hGetSuperObjectObject(m_poGraphic->GetStruct());

	n = stSecondVertex;
	MTH3D_M_vSubVector(&n,&n,&stFirstVertex);
	GLI_tdxValue lenght = MTH3D_M_xNormVector( &n );
	d5 = (GLI_tdxValue)(lenght*0.5);
	md5 = -d5;
	
	MTH3D_tdstVector a8_stPoint [24];
	
	MTH3D_M_vSetVectorElements (a8_stPoint,   0,-0.5,m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+1, 1.,-0.5,m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+2, 1.,0.5,m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+3, 0,0.5,m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+4, 0,-0.5,-m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+5, 1.,-0.5,-m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+6, 1.,0.5,-m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+7, 0,0.5,-m_xe);
	
	MTH3D_M_vSetVectorElements (a8_stPoint+8, 0.4f, -0.6f, m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+9, 0.6f, -0.6f, m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+10, 0.6f, -0.5f, m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+11, 0.4f, -0.5f, m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+12, 0.4f, -0.6f, -m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+13, 0.6f, -0.6f, -m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+14, 0.6f, -0.5f, -m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+15, 0.4f, -0.5f, -m_xe);

	MTH3D_M_vSetVectorElements (a8_stPoint+16, 0.4f, 0.5f, m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+17, 0.6f, 0.5f, m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+18, 0.6f, 0.6f, m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+19, 0.4f, 0.6f, m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+20, 0.4f, 0.5f, -m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+21, 0.6f, 0.5f, -m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+22, 0.6f, 0.6f, -m_xe);
	MTH3D_M_vSetVectorElements (a8_stPoint+23, 0.4f, 0.6f, -m_xe);

	GEO_vSetListOfPointsOfObject 
	(
		hMot, 
        a8_stPoint, 
        24, 
        0
	);
	GEO_xComputeObjectNormals ( hMot );

	MTH3D_M_vNormalizeVector(&n,&n);
	
	if (n.xX||n.xY) { MTH3D_M_vSetVectorElements ((&u),(-n.xY),(n.xX),0.); }
	else { MTH3D_M_vSetVectorElements((&u),0.,(-n.xZ),n.xY); } 
	MTH3D_M_vNormalizeVector(&u,&u);
	MTH3D_M_vCrossProductVector((&v),(&n),(&u));

	fn_vComputePlaneAxis ();	
}

// moves
void Link2D::fn_vStartMove ( MTH3D_tdstVector* pstStartVertex )
{
	m_stPlaneVertex = *pstStartVertex;
	fn_vDrawPlane ();
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
}

void Link2D::fn_vMove ( MTH3D_tdstVector* pstTranslation )
{
	MTH3D_tdstVector stInitialValue = m_stPlaneVertex;
	MTH3D_M_vAddVector(&m_stPlaneVertex,&m_stPlaneVertex,pstTranslation);
	fn_vComputePlaneAxis (&m_stPlaneVertex);
	fn_vProjVertex ( &m_stPlaneVertex );
	
	
	fn_vDrawPlane ();
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
	
	*pstTranslation = m_stPlaneVertex;
	MTH3D_M_vSubVector(pstTranslation, pstTranslation, &stInitialValue);
}

void Link2D::fn_vEndMove (void)
{
	fn_vWayNotifySave ();
}

void Link2D::fn_vComputePlaneAxis (MTH3D_tdstVector* pstVertex)
{
	MTH3D_tdstVector stFirstVertex, stSecondVertex;
	MTH3D_tdstVector Ox, Oy, Oz;
	MTH3D_tdstVector v;
	MTH3D_tdstVector stVectorU, stVectorV, stVectorN;

	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stFirstVertex );
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stSecondVertex );

	Ox = stSecondVertex;
	MTH3D_M_vSubVector(&Ox,&Ox,&stFirstVertex);
	GLI_tdxValue lenght = MTH3D_M_xNormVector( &Ox );

	if (pstVertex)
	{
		v = *pstVertex;
		MTH3D_M_vSubVector(&v,&v,&stFirstVertex);
		MTH3D_M_vCrossProductVector((&Oz),(&Ox),(&v));
	
		// we keep the normal on the same side !!
		if (MTH3D_M_xDotProductVector(&Oz,&m_stNormalVertex)<=0) MTH3D_M_vMulScalarVector (&Oz,-1,&Oz);
	}
	else Oz = m_stNormalVertex;
	
	MTH3D_M_vCrossProductVector((&Oy),(&Oz),(&Ox));
	MTH3D_M_vCrossProductVector((&Oz),(&Ox),(&Oy));

	MTH3D_M_vNormalizeVector(&Oy,&Oy);
	MTH3D_M_vNormalizeVector(&Oz,&Oz);
	MTH3D_M_vNormalizeVector(&Ox,&Ox);

	GEO_tdxHandleToMatrix hAbsolutePlaneMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (m_poGraphic->GetStruct());
	POS_fn_vSetIdentityMatrix( hAbsolutePlaneMatrix );
	POS_fn_vSetTranslationVector(  hAbsolutePlaneMatrix, &stFirstVertex );
	POS_fn_vSetRotationMatrix( hAbsolutePlaneMatrix , &Ox, &Oy, &Oz );
	
	POS_fn_vGetScaleMatrix( hAbsolutePlaneMatrix, &stVectorU, &stVectorV, &stVectorN );

	MTH3D_M_vMulScalarVector( &stVectorU, lenght, &stVectorU);
	MTH3D_M_vMulScalarVector( &stVectorV, lenght, &stVectorV);
	POS_fn_vSetScaleMatrix( hAbsolutePlaneMatrix, &stVectorU, &stVectorV, &stVectorN );

	POS_fn_vNormalizeMatrix( hAbsolutePlaneMatrix ) ;
	GetInterface()->fn_vComputeNewRelativeMatrix (m_poGraphic->GetStruct());

	m_stNormalVertex = Oz;
}

void Link2D::fn_vSetPlaneHorizontal (void)
{
	MTH3D_M_vSetVectorElements (&m_stNormalVertex, 0., 0., 1.);
	fn_vComputePlaneAxis ();
	fn_vDrawPlane ();
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
}

void Link2D::fn_vSetPlaneVertical (void)
{
	MTH3D_tdstVector Ox, Oy;
	MTH3D_tdstVector stFirstVertex, stSecondVertex;

	MTH3D_M_vSetVectorElements (&Oy, 0., 0., 1.);

	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stFirstVertex );
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stSecondVertex );

	Ox = stSecondVertex;
	MTH3D_M_vSubVector(&Ox,&Ox,&stFirstVertex);

	MTH3D_M_vCrossProductVector ( &m_stNormalVertex, &Ox, &Oy );
	
	fn_vComputePlaneAxis ();
	fn_vDrawPlane ();
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
}

void Link2D::fn_vProjVertex ( MTH3D_tdstVector* pstVertex )
{
	POS_tdstCompletePosition stInvMatrix;
	MTH3D_tdstVector stLocalVertex;
	MTH_tdxReal sty;

	POS_fn_vSetIdentityMatrix(&stInvMatrix);
	GEO_tdxHandleToMatrix hAbsoluteMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (m_poGraphic->GetStruct());
	POS_fn_vInvertMatrix( &stInvMatrix , hAbsoluteMatrix );
	POS_fn_vMulMatrixVertex(  &stLocalVertex, &stInvMatrix, pstVertex);
	MTH3D_M_vSetXofVector(&stLocalVertex,0.5);
	sty = MTH3D_M_xGetYofVector(&stLocalVertex);
	if (sty>0.) MTH3D_M_vSetYofVector(&stLocalVertex,0.55f)
	else MTH3D_M_vSetYofVector(&stLocalVertex,-0.55f);
	MTH3D_M_vSetZofVector(&stLocalVertex,0.);
	POS_fn_vMulMatrixVertex(  pstVertex ,hAbsoluteMatrix, &stLocalVertex);
}

*/
//ENDANNECY Shaitan Nettoyage }

