//===========================================================================//
// File:	bndgbox.hh                                                          //
// Project: MUNGA           Brick: Spatializer Library                       //
// Contents: Interface specification of bounding-box based spatialization    //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 01/08/95  JMA  Initial port back to C++                                   //
//---------------------------------------------------------------------------//
// Copyright (C) 1993-1995, Virtual World Entertainment, Inc.                //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#if !defined(BOXLIST_HPP)
#	define BOXLIST_HPP

#	if !defined(BNDGBOX_HPP)
#		include <bndgbox.hpp>
#	endif

#	if !defined(MEMBLOCK_HPP)
#		include <memblock.hpp>
#	endif

#	if !defined(POINT3D_HPP)
#		include <point3d.hpp>
#	endif

	//##########################################################################
	//#######################    BoundingBoxListNode    ########################
	//##########################################################################

	class BoundingBoxList;
	class BoxedSolidList;
   class BoundingBoxTree;

	class BoundingBoxListNode SIGNATURED
	{
		friend class BoundingBoxList;
      friend class BoxedSolidList;

	//##########################################################################
	// Memory Allocation
	//
	private:
		static MemoryBlock
			AllocatedMemory;
		void*
			operator new(size_t)
				{return AllocatedMemory.New();}
		void
			operator delete(void *where)
				{AllocatedMemory.Delete(where);}

	//##########################################################################
	// Construction and Destruction
	//
	protected:
		BoundingBox
			*boundingBox;
		BoundingBoxListNode
			*previousNode;

		BoundingBoxListNode(
			BoundingBox *volume,
			const ExtentBox &slice
		):
			boundingBox(volume),
			solidSlice(slice),
			previousNode(NULL)
				{Check_Pointer(this); Check(volume);}
		~BoundingBoxListNode()
			{Check(this);}

	public:
		ExtentBox
			solidSlice;
		BoundingBox*
			GetBoundingBox()
				{Check(this); return boundingBox;}
		BoundingBoxListNode*
			GetNextNode()
				{Check(this); return previousNode;}

	//##########################################################################
	// Test Support
	//
	public:
		Logical
			TestInstance() const;
	};

	//##########################################################################
	//########################    BoundingBoxList    ###########################
	//##########################################################################

	class BoundingBoxList SIGNATURED
	{

	//##########################################################################
	// Construction and Destruction
	//
	protected:
		BoundingBoxListNode
			*root;
		int
			nodeCount;
		int*
			scoreBoard;
		BoundingBox
			**boundingBoxIndex;
		Logical
			isXMajorAxis;

	public:
		BoundingBoxList();
		~BoundingBoxList();

		void
			Add(
				BoundingBox* volume,
				const ExtentBox &slice
			);
		void
			Remove(BoundingBox* volume);
		void
			EraseList();

		BoundingBoxListNode*
			GetRoot()
				{Check(this); return root;}

	//##########################################################################
	// Tree Traversal Functions
	//
	public:
		BoundingBox*
			FindBoundingBoxContaining(const Point3D &point);

		void
			FindBoundingBoxesContaining(
				BoundingBox *volume,
				BoundingBoxCollisionList &list
			);

		BoundingBox*
			FindBoundingBoxUnder(
				const Point3D &point,
				Scalar *height
			);

		BoundingBox*
			FindBoundingBoxHitBy(Line *line);

		void
			Reduce();
		void
			SortForTree();

	protected:
		void
			Sort(
				BoundingBoxList &active_solids,
				ExtentBox &clipping_box,
				BoundingBoxTree &tree_so_far
			);

	//##########################################################################
	// Test Support
	//
	public:
		Logical
			TestInstance() const;
	};

#endif
