//===========================================================================//
// File:        segment.cc                                                   //
// Project: MUNGA           Brick: Model Manager                             //
// Contents: Skeleton Heirachical element							                 //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 10/11/95  JM  Initial coding.                                             //
//---------------------------------------------------------------------------//
// Copyright (C) 1995, Virtual World Entertainment, Inc.                     //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include <munga.hpp>
#pragma hdrstop

#if !defined(SEGMENT_HPP)
#	include <segment.hpp>
#endif

#if !defined(JOINT_HPP)
#	include <joint.hpp>
#endif

//#############################################################################
// Segment::VideoObjectNames Support
//

CString*
	EntitySegment::VideoObjectNames::GetVideoObjectName(
		Enumeration damage_graphic_state
	)
{
	Check(this);
	CStringPlug *my_plug = videoObjectNames.Find(damage_graphic_state);
	if (my_plug)
	{
		Check(my_plug);
		return my_plug->GetPointer();
	}
	return NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
EntitySegment::VideoObjectNames::VideoObjectNames()  :
	videoObjectNames(NULL, False)
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
EntitySegment::VideoObjectNames::~VideoObjectNames()
{
	VChainIteratorOf<CStringPlug*, Enumeration> iterator(videoObjectNames);
	iterator.DeletePlugs();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	EntitySegment::VideoObjectNames::AddVideoObjectName(
		const CString &new_name,
		Enumeration damage_graphic_state
	)
{
	Check(this);
	CStringPlug *new_string = new CStringPlug(new_name);
	Register_Object(new_string);

	videoObjectNames.AddValue(new_string, damage_graphic_state);
}


//#############################################################################
// Segment Support
//

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
EntitySegment::EntitySegment(
	CString	segment_name,
	int      this_index,
	int		parent_index,
	int		joint_index,
	Joint		*joint_ptr,
	LinearMatrix &lin_matrix,
	EntitySegment *parent_segment,
	Logical	is_site,
	int primary_damage_zone
)  :
	baseOffset(LinearMatrix::Identity),
	childIndexTable(NULL, True),
	damageZoneTable(NULL,True),
	videoObjectTable(NULL,True),
	childPointerTable(NULL,True)
{
	segmentModified = True;
	thisIndex = this_index;
	parentIndex = parent_index;
	jointIndex = joint_index;
	jointPointer = joint_ptr;
	baseOffset = lin_matrix;
	parentSegment =parent_segment;
	segmentName = segment_name;
	siteSegment = is_site;
	primaryDamageZone = primary_damage_zone;	// Add To constructor Call

	segmentToParent = baseOffset;
	segmentToEntity = baseOffset;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
CString*
	EntitySegment::GetVideoObjectName(
		SkeletonType skl_type,
		Enumeration damage_graphic_state
	)
{
	Check(this);
	VideoObjectNames *video_names = videoObjectTable.Find(skl_type);
	if(video_names)
	{
		return video_names->GetVideoObjectName(damage_graphic_state);
	}
	else
	{
		return NULL;
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
EntitySegment::~EntitySegment()
{
	IntegerTableIterator iterator_child( childIndexTable);
	iterator_child.DeletePlugs();

	IntegerTableIterator iterator_damage(damageZoneTable);
	iterator_damage.DeletePlugs();

	VideoTableIterator iterator_video(videoObjectTable);
	iterator_video.DeletePlugs();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	EntitySegment::AddChildIndex(int  child_index, int index)
{
	Check(this);
	IntegerPlug *child_plug = new IntegerPlug(child_index);
	Register_Object(child_plug);
	childIndexTable.AddValue(child_plug, index);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	EntitySegment::AddChildPointer(EntitySegment*  child_ptr, int index)
{
	Check(this);
	childPointerTable.AddValue(child_ptr, index);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	EntitySegment::AddDamageZone(int damage_index, int index)
{
	Check(this);
	Check_Pointer(&damageZoneTable);
	IntegerPlug *damage_plug = new IntegerPlug(damage_index);
	Register_Object(damage_plug);
	damageZoneTable.AddValue(damage_plug, index);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	EntitySegment::AddVideoObjectName(
		CString video_object_name,
		Enumeration skl_type,
		Enumeration damage_graphic_state
	)
{
	Check(this);
	Check_Pointer(&videoObjectTable);
	VideoTableIterator iterator(videoObjectTable);

	VideoObjectNames	*names_plug = iterator.Find(skl_type);
	if(!names_plug)
	{
		names_plug = new VideoObjectNames();
		Register_Object(names_plug);
		videoObjectTable.AddValue(names_plug, skl_type);
	}
	names_plug->AddVideoObjectName(video_object_name, damage_graphic_state);

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
const LinearMatrix&
	EntitySegment::GetSegmentToParent()
{
	Check(this);

	//
	//-----------------------------------------------------------------------
	// Recalculate the segment to parent matrix if we have a joint and it has
	// changed
	//-----------------------------------------------------------------------
	//
	if (jointPointer)
	{
		Check(jointPointer);
		if (jointPointer->IsJointModified())
		{
			jointPointer->JointModified(False);
			LinearMatrix
				t1 = baseOffset,
				t2;

			switch(jointPointer->GetJointType())
			{
				case  Joint::BallJointType:
					{
						LinearMatrix euler_angles = LinearMatrix::Identity;
						euler_angles = jointPointer->GetEulerAngles();
						t2.Invert(euler_angles);
						segmentToParent.Multiply(t1,t2);
					}
					break;

				case  Joint::HingeXJointType:
				case  Joint::HingeYJointType:
				case  Joint::HingeZJointType:
					{
						LinearMatrix hinge_joint = LinearMatrix::Identity;
						hinge_joint = jointPointer->GetHinge();
						t2.Invert(hinge_joint);
						segmentToParent.Multiply(t1,t2);
					}
					break;

				case  Joint::BallTranslationJointType:
					{
						//
						//-------------------------------------
						// We only want to invert the rotations
						//-------------------------------------
						//
						LinearMatrix euler_angles = LinearMatrix::Identity;
						euler_angles = jointPointer->GetEulerAngles();
						segmentToParent.Invert(euler_angles);
						t2.Multiply(t1,segmentToParent);
						t1 = LinearMatrix::Identity;
						t1 = jointPointer->GetTranslation();
						segmentToParent.Multiply(t2,t1);
					}
					break;
			}
		}
	}
	return segmentToParent;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
const LinearMatrix&
	EntitySegment::GetSegmentToEntity()
{
	Check(this);

	//
	//---------------------------------------------------------------------
	// Recalculate the segment to entity transform if something has changed
	//---------------------------------------------------------------------
	//
	if (segmentModified && parentSegment)
	{
		Check(parentSegment);
		segmentToEntity.Multiply(
			GetSegmentToParent(),
			parentSegment->GetSegmentToEntity()
		);
		ModifySegment(False);
	}
	return segmentToEntity;
}

