/*
///////////////////////////////////////////////////////////////////////////////////////////////////
// Description : TanObj.cpp
//   
//  Definition of the tangent objects            
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// inherit from  :	CPA_Object
//					CPA_EdMot<ACP_tdxHandleOfLink>
///////////////////////////////////////////////////////////////////////////////////////////////////
// Creation date: 29 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 "TanObj.hpp"
#include "Inter.hpp"
#include "LinkBez.hpp"


long Tangent::ms_lNbTangent=0;
Waypoint_Interface*	Tangent::ms_poInterface;



//------ static
void Tangent::fn_vInitObject (Waypoint_Interface* poInterface)
{
	ms_poInterface = poInterface;
}

					  
Tangent::Tangent( CPA_ObjectDLLBase *p_oDLL, CPA_BaseObject *p_oOwner, eTangentLenghtMode eLenghtMode, eTangentOrientationMode eOrientationMode, eTangentNeighbourMode eNeighbourMode, eTangentChangeAllMode eChangeAllMode, GEO_tdstColor* pstColor ):
	CPA_BaseObject ( p_oDLL, "Tangent" )
{
	CPA_SuperObject* poSuperTangent =  ms_poInterface->GetInterface()->GetNewSuperObject ( E_ss_NoSave, C_NoType );
	poSuperTangent->SetObject(this);
	SetSuperObject (poSuperTangent);
	poSuperTangent->SetTypeSO (C_Protected);

	// name
	CString csName;
	csName.Format ("Tangent%d", ms_lNbTangent);
	ms_lNbTangent++;
	fn_eRename (csName);

	// init
	MTH3D_M_vNullVector(&m_stFixVertex);
	MTH3D_M_vNullVector(&m_stTangentVector);
	m_xDim = 0.05f;
	m_bOrientationAllow=FALSE;
	m_bNotifyWay=FALSE;
	
	// color
	if (pstColor) m_stColor = *pstColor;
	else
	{
		m_stColor.xR = 0.f;
		m_stColor.xG = 0.8f;
		m_stColor.xB = 0.f;
	}
	m_eTangentLenghtMode = eLenghtMode;
	m_eTangentOrientationMode = eOrientationMode;
	m_eTangentNeighbourMode = eNeighbourMode;
	m_eTangentChangeAllMode = eChangeAllMode;

	// graphic object
	CPA_SuperObject* poGraphic = GetInterface()->fn_pGetNewTangentGraphicObject (&m_stColor);
	poGraphic->SetSuperObjectOwner (GetSuperObject());	
	GetSuperObject()->AddTail (poGraphic);
	GEO_xComputeObjectNormals ( (ACP_tdxHandleOfObject)HIE_fn_hGetSuperObjectObject(poGraphic->GetStruct()));
	poGraphic->SetEditProtected (TRUE); 

	m_poParent = (Link*)p_oOwner; 
}

long Tangent::GetDataType (void)
{
	return C_ucTangent;
}

void* Tangent::GetData (void)
{
	return NULL;
}

void Tangent::fn_vSetAbsoluteVertex ( MTH3D_tdstVector* pstFixAbsoluteVertex, MTH3D_tdstVector* pstMoveAbsoluteVertex )
{
	POS_tdstCompletePosition InvMatrix;
	POS_fn_vSetIdentityMatrix(&InvMatrix);
	GetInterface()->fn_vComputeAbsoluteMatrix (GetSuperObject());
	GEO_tdxHandleToMatrix hAbsoluteLineMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (GetSuperObject()->GetStruct());
	
	POS_fn_vInvertMatrix( &InvMatrix, hAbsoluteLineMatrix );
	POS_fn_vMulMatrixVertex( &m_stFixVertex, &InvMatrix, pstFixAbsoluteVertex ) ;
	
	m_stTangentVector = *pstMoveAbsoluteVertex;
	m_stMoveVertex = *pstMoveAbsoluteVertex;
	MTH3D_M_vSubVector( &m_stTangentVector, &m_stTangentVector, pstFixAbsoluteVertex );

	m_bOrientationAllow=TRUE;
	fn_vDraw ();
	m_bOrientationAllow=FALSE;
}

void Tangent::fn_vSetTangent ( MTH3D_tdstVector* pstMoveAbsoluteVertex )
{
	MTH3D_tdstVector st_FixVertex;
	GetInterface()->fn_vComputeAbsoluteMatrix (GetSuperObject());
	GEO_tdxHandleToMatrix hAbsoluteLineMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (GetSuperObject()->GetStruct());
	POS_fn_vMulMatrixVertex( &st_FixVertex, hAbsoluteLineMatrix, &m_stFixVertex ) ;
	
	switch (m_eTangentLenghtMode)
	{
	case eVariableLenghtTangent:
		m_stTangentVector = *pstMoveAbsoluteVertex;
		MTH3D_M_vSubVector(&m_stTangentVector, &m_stTangentVector, &st_FixVertex );
		break;

	case eFixLenghtTangent:
		{
			GLI_tdxValue xLenght, xModifLenght, xRatio;
			xLenght = MTH3D_M_xNormVector( &m_stTangentVector );
			m_stTangentVector = *pstMoveAbsoluteVertex;
			MTH3D_M_vSubVector( &m_stTangentVector, &m_stTangentVector, &st_FixVertex );
			xModifLenght = MTH3D_M_xNormVector( &m_stTangentVector );
			xRatio = xModifLenght>0 ? xLenght/xModifLenght : 1.f;
			MTH3D_M_vMulScalarVector(&m_stTangentVector,xRatio, &m_stTangentVector);
		} break;
	}

	m_bOrientationAllow=TRUE;
	m_poParent->fn_vDraw ();
	m_bOrientationAllow=FALSE;
}

void Tangent::fn_vSetTangentDirection ( MTH3D_tdstVector* pstAbsoluteDirectionVertex, BOOL bOppositeDirection )
{
	MTH_tdxReal xLenght = MTH3D_M_xNormVector( &m_stTangentVector );
	m_stTangentVector = *pstAbsoluteDirectionVertex;
	MTH3D_M_vNormalizeVector(&m_stTangentVector,&m_stTangentVector);
	if(bOppositeDirection) MTH3D_M_vMulScalarVector( &m_stTangentVector, -xLenght, &m_stTangentVector )
	else MTH3D_M_vMulScalarVector( &m_stTangentVector, xLenght, &m_stTangentVector );

	m_bOrientationAllow=TRUE;
	fn_vDraw ();
	m_bOrientationAllow=FALSE;
}

void Tangent::fn_pGetAbsoluteTangent (MTH3D_tdstVector* pstTangent)
{
	*pstTangent = m_stTangentVector;
}

void Tangent::fn_vConstructGraphic (void)
{
	CPA_SuperObject* poGraphic = GetInterface()->fn_pGetNewTangentGraphicObject(&m_stColor);
	poGraphic->SetSuperObjectOwner (GetSuperObject());	
	GetSuperObject()->AddTail (poGraphic);
	GEO_xComputeObjectNormals ( (ACP_tdxHandleOfObject)HIE_fn_hGetSuperObjectObject(poGraphic->GetStruct()));
	poGraphic->SetEditProtected (TRUE); 
}

void Tangent::fn_vDraw (void)						
{
	if (m_eTangentOrientationMode==eVariableOrientationTangent && !m_bOrientationAllow) 
	{
		MTH3D_tdstVector stU, stV;
		GEO_tdxHandleToMatrix hAbsoluteMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (GetSuperObject()->GetStruct());
		GLI_tdxValue xlenght = MTH3D_M_xNormVector( &m_stTangentVector );
		POS_fn_vGetRotationMatrix( hAbsoluteMatrix , &stU, &stV, &m_stTangentVector );
		MTH3D_M_vMulScalarVector ( &m_stTangentVector, xlenght, &m_stTangentVector );
		return;
	}

	MTH3D_tdstVector stAbsoluteFixVertex, stAbsoluteMoveVertex;
	GEO_tdxHandleToMatrix hAbsoluteMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (GetSuperObject()->GetStruct());
	
	GetInterface()->fn_vComputeAbsoluteMatrix (GetSuperObject());

	POS_fn_vNormalizeMatrix( hAbsoluteMatrix ) ;
	POS_fn_vMulMatrixVertex( &stAbsoluteFixVertex, hAbsoluteMatrix, &m_stFixVertex ) ;
	
	stAbsoluteMoveVertex = stAbsoluteFixVertex;
	MTH3D_M_vAddVector(&stAbsoluteMoveVertex, &stAbsoluteMoveVertex, &m_stTangentVector );

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

	CPA_SuperObject* psoObject = (CPA_SuperObject*)GetSuperObject()->GetHead();
	
	GLI_tdxValue d=m_xDim;
	GLI_tdxValue md=-m_xDim;
	GLI_tdxValue d2=(GLI_tdxValue)(m_xDim*2.);
	GLI_tdxValue md2=(GLI_tdxValue)(-m_xDim*2.);
	GLI_tdxValue zero=0.;
	MTH3D_tdstVector n, u, v;
	MTH3D_tdstVector Ox = { 1., 0., 0. };

	// Version finale 	
	ACP_tdxHandleOfObject hMot = (ACP_tdxHandleOfObject) HIE_fn_hGetSuperObjectObject(psoObject->GetStruct());

	n = stAbsoluteMoveVertex;
	MTH3D_M_vSubVector(&n,&n,&stAbsoluteFixVertex);
	GLI_tdxValue lenght = MTH3D_M_xNormVector( &n );
	GLI_tdxValue lenght8 = (GLI_tdxValue)(lenght*0.8);
	
	MTH3D_tdstVector a8_stPoint [13];
	
	MTH3D_M_vSetVectorElements (a8_stPoint, md,md,zero);
	MTH3D_M_vSetVectorElements (a8_stPoint+1, d,md,zero);
	MTH3D_M_vSetVectorElements (a8_stPoint+2, d,d,zero);
	MTH3D_M_vSetVectorElements (a8_stPoint+3, md,d,zero);
	
	MTH3D_M_vSetVectorElements (a8_stPoint+4, md,md,lenght8);
	MTH3D_M_vSetVectorElements (a8_stPoint+5, d,md,lenght8);
	MTH3D_M_vSetVectorElements (a8_stPoint+6, d,d,lenght8);
	MTH3D_M_vSetVectorElements (a8_stPoint+7, md,d,lenght8);
	
	MTH3D_M_vSetVectorElements (a8_stPoint+8, md2,md2,lenght8);
	MTH3D_M_vSetVectorElements (a8_stPoint+9, d2,md2,lenght8);
	MTH3D_M_vSetVectorElements (a8_stPoint+10, d2,d2,lenght8);
	MTH3D_M_vSetVectorElements (a8_stPoint+11, md2,d2,lenght8);
	
	MTH3D_M_vSetVectorElements (a8_stPoint+12, 0,0,lenght);

	
	
	GEO_vSetListOfPointsOfObject 
	(
		hMot, 
        a8_stPoint, 
        13, 
        0
	);

	MTH3D_M_vNormalizeVector(&n,&n);
	
	// here : to rotate the beam around its principal axis
	// by defaults find section align with plan z=0
	
	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));

	// change the local matrix
	GEO_tdxHandleToMatrix hAbsoluteLineMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (psoObject->GetStruct());
	POS_fn_vSetIdentityMatrix( hAbsoluteLineMatrix );
	POS_fn_vSetTranslationVector(  hAbsoluteLineMatrix, &stAbsoluteFixVertex );
	POS_fn_vSetRotationMatrix( hAbsoluteLineMatrix , &u, &v, &n );

	GetInterface()->fn_vComputeNewRelativeMatrix (psoObject->GetStruct());
}

Waypoint_Interface* Tangent::GetInterface (void)
{
	return ms_poInterface; 
}

void Tangent::fn_vStartMove ( MTH3D_tdstVector* pstStartVertex, BOOL bShift, BOOL bControl )
{
	MTH3D_tdstVector stExtremVector, stPickVector;
	MTH3D_tdstVector st_FixVertex;
	GetInterface()->fn_vComputeAbsoluteMatrix (GetSuperObject());
	GEO_tdxHandleToMatrix hAbsoluteLineMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (GetSuperObject()->GetStruct());
	POS_fn_vMulMatrixVertex( &st_FixVertex, hAbsoluteLineMatrix, &m_stFixVertex ) ;

	stExtremVector = m_stMoveVertex;
	stPickVector = *pstStartVertex;
	MTH3D_M_vSubVector( &stExtremVector, &stExtremVector, &st_FixVertex);
	MTH3D_M_vSubVector( &stPickVector, &stPickVector, &st_FixVertex);
	m_xMoveRatio = MTH3D_M_xNormVector(&stExtremVector)/MTH3D_M_xNormVector(&stPickVector);

	fn_vChangeNeighbourTangent (bShift, bControl);
	fn_vChangeAllTangent (bShift, bControl);

	GetInterface()->fn_vEdit (m_poParent);
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
}

void Tangent::fn_vMove ( MTH3D_tdstVector* pstTranslation, BOOL bShift, BOOL bControl )
{
	MTH3D_tdstVector stTranslation = * pstTranslation;
	MTH3D_M_vMulScalarVector(&stTranslation,m_xMoveRatio, &stTranslation);
	
	MTH3D_M_vAddVector(&m_stMoveVertex, &m_stMoveVertex, &stTranslation);
	fn_vSetTangent ( &m_stMoveVertex );
	
	fn_vChangeNeighbourTangent (bShift, bControl);
	fn_vChangeAllTangent (bShift, bControl);

	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
	m_bNotifyWay=TRUE;
}

void Tangent::fn_vEndMove (void)
{
	if (m_bNotifyWay)
	{
		m_poParent->fn_vWayNotifySave();
		m_bNotifyWay=FALSE;
	}
}

void Tangent::fn_vChangeNeighbourTangent (BOOL bShift, BOOL bControl)
{
	switch (m_eTangentNeighbourMode)
	{
	case eAlwaysChangeNeighbour :
		m_poParent->fn_vChangeNeighbourTangent (this);
		break;
	case eShiftDownChangeNeighbour :
		if (bShift) m_poParent->fn_vChangeNeighbourTangent (this);
		break;
	case eControlDownChangeNeighbour :
		if (bControl) m_poParent->fn_vChangeNeighbourTangent (this);
		break;
	case eShiftUpChangeNeighbour :
		if (!bShift) m_poParent->fn_vChangeNeighbourTangent (this);
		break;
	case eControlUpChangeNeighbour :
		if (!bControl) m_poParent->fn_vChangeNeighbourTangent (this);
		break;
	}
}

void Tangent::fn_vChangeAllTangent (void)
{
	m_poParent->fn_vChangeAllTangent (this);
}

void Tangent::fn_vChangeAllTangent (BOOL bShift, BOOL bControl)
{
	switch (m_eTangentChangeAllMode)
	{
	case eAlwaysChangeAll :
		m_poParent->fn_vChangeAllTangent (this);
		break;
	case eShiftDownChangeAll :
		if (bShift) m_poParent->fn_vChangeAllTangent (this);
		break;
	case eControlDownChangeAll :
		if (bControl) m_poParent->fn_vChangeAllTangent (this);
		break;
	case eShiftUpChangeAll :
		if (!bShift) m_poParent->fn_vChangeAllTangent (this);
		break;
	case eControlUpChangeAll :
		if (!bControl) m_poParent->fn_vChangeAllTangent (this);
		break;
	}
}

*/
//ENDANNECY Shaitan Nettoyage }
