/*
	Trampoline Sim.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GAME_SHAPE_BOX_H
#define GAME_SHAPE_BOX_H

//=====================================================================================================================
//=====================================================================================================================

#include "Physics/Shape.h"

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================
	//=================================================================================================================

	class ShapePlane;

	//=================================================================================================================
	/// @brief		This shape represents a box shape
	//=================================================================================================================
	
	class ShapeBox : public Shape
	{
		public:
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				//=====================================================================================================
				/// @brief		Creates the box shape
				/// @param		size - half extents of the box
				/// @param		position - position the shape is initially in
				/// @param		orientation - orientation of the shape
				//=====================================================================================================
				
				ShapeBox
				( 
					const Vec3f	&			size		= Vec3f::One()				, 
					const Vec3f	&			position	= Vec3f::Zero()				, 
					const Quaternionf &		orientation = Quaternionf::Identity()
				)
				: Shape(position,orientation ,SHAPE_BOX)
				{
					SetSize(size);
				}

				//=====================================================================================================
				/// @brief		Sets the size of the box
				/// @param		size - size of the box
				/// @pre		Size must be valid
				/// @pre		Size must be > 0 on all axes
				/// @remarks	Size represents the half extents of the box
				//=====================================================================================================

				inline void SetSize( const Vec3f & size )
				{
					// Make sure ok

					ENSURE( size.x > 0 , "Size must be > 0 on all axes" )
					ENSURE( size.y > 0 , "Size must be > 0 on all axes" )
					ENSURE( size.z > 0 , "Size must be > 0 on all axes" )

					ENSURE( Float::IsValid(size.x) , "Size must be valid" )
					ENSURE( Float::IsValid(size.y) , "Size must be valid" )
					ENSURE( Float::IsValid(size.z) , "Size must be valid" )

					m_size = size;
					
					// Save the bounding sphere radius:
					
					m_bs_radius = m_size.x;
					
					if ( m_size.y > m_bs_radius ) m_bs_radius = m_size.y;
					if ( m_size.z > m_bs_radius ) m_bs_radius = m_size.z;
				}

				//=====================================================================================================
				/// @brief		Gets the size of the box
				/// @returns	Size of the box
				//=====================================================================================================

				inline const Vec3f & GetSize() const { return m_size; }
				
				//=====================================================================================================
				/// @brief		Returns the bounding sphere radius of the box
				/// @returns	Bounding sphere radius of the box
				//=====================================================================================================
				
				inline float GetBSRadius() const { return m_bs_radius; }

				//=====================================================================================================
				/// @brief		Gets the transformed vertices of the box in world coordinates, saving them to the 
				///				given array.
				/// @param		vertices - array to save the vertices to
				//=====================================================================================================

				void GetWorldVertices( Vec3f vertices[] ) const; 

				//=====================================================================================================
				/// @brief		Generates a list of collision contacts with the other shape
				/// @param		other - other shape to generate a list of for 
				/// @param		contacts - where to save the resulting contacts
				//=====================================================================================================

				virtual void GenerateContacts( const Shape & other , std::vector<Contact> & contacts ) const;

				//=====================================================================================================
				/// @brief		Raycasts against this shape and returns the result
				/// @param		ray - ray to cast against the shape
				/// @returns	Result of the raycast
				//=====================================================================================================

				virtual RaycastResult Raycast( Ray & ray ) const;
				
		private:

			//=========================================================================================================
			// Functions
			//=========================================================================================================

				/// Generates contacts for this shape with a plane

				void PlaneContacts( const ShapePlane & other , std::vector<Contact> & contacts ) const;

				/// Generates contacts for this shape with a box

				void BoxContacts( const ShapeBox & other , std::vector<Contact> & contacts ) const;

				/// Does point/face collision between two boxes

				static void BoxPointFaceContacts
				(
					const ShapeBox &			box1		,
					const ShapeBox &			box2		,
					std::vector<Contact> &		contacts	,
					bool						reverse
				);

				/// Does edge/edge collision between two boxes

				static void BoxEdgeEdgeContacts
				(
					const ShapeBox &			box1		,
					const ShapeBox &			box2		,
					std::vector<Contact> &		contacts
				);

				/// Computes the closest points on both given lines to each other. Returns false if there is none.

				static bool ClosestLinePoints
				(
					const Vec3f &	p1	,
					const Vec3f &	e1	,
					const Vec3f &	p2	,
					const Vec3f &	e2	,
					Vec3f &			cp1 ,
					Vec3f &			cp2
				);
				
				/// Copying is disallowed
				
				ShapeBox( const ShapeBox & other );

				/// Copying is disallowed
				
				ShapeBox & operator = ( const ShapeBox & other );
		
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// This is the size of the box: half extents

				Vec3f m_size;
				
				/// This is the bounding sphere radius of the box
				
				float m_bs_radius;
	};
}

#endif
