// Author : Chris Tolley
// Date   : 7/13/2004
// LeafQuadNode.h contains the class declaration for a leaf node class 
// to use in a quad tree.

// IOSTREAM IS DEBUG ONLY
#include <iostream>
#include <vector>

using namespace std;

#include "LeafQuadNode.h"
#include "Object.h"
#include "logger.h"

// Destructor
LeafQuadNode::~LeafQuadNode()
{}


// Removes an object from the vector
void LeafQuadNode::Remove( SmartPointer& sp )
{
	// Find obj in the vector
	vector< SmartPointer >::iterator ito = m_vObjects.begin();
	bool found = false;
	for( ito; ( ito != m_vObjects.end() ) && ( ! found ); ++ito )
	{
		// If this element equals obj
		if( *ito == sp )
		{
			// Remove it and break the loop
			m_vObjects.erase( ito );
			found = true;
		}// End if
	}// End for ito

	// Log it if we did not find a match
	if( ! found )
	{
		logger::event( "LeafQuadNode::Remove", "Did not find a match", 0 );
		logger::event( "LeafQuadNode::Remove", "Looking for obj", sp->GetId() );
		logger::event( "LeafQuadNode::Remove", "Looking for a ", sp->GetType() );
	}// End if

}// End LeafQuadNode::Remove


// Removes all SmartPointers stored in this leaf
void LeafQuadNode::RemoveAll()
{
	m_vObjects.clear();
}// End LeafQuadNode::RemoveAll


// Stores all object contained on this leaf node that are in the sphere
// formed by x,z and radius in objects
void LeafQuadNode::GetAllInSphere( float x, float z, float radius,
								   vector< SmartPointer >& objects )
{
	// For each object stored in this leaf node
	int size = m_vObjects.size();
	for( int n = 0; n < size; ++n )
	{
		// If the n'th object is inside the sphere
		if( m_vObjects[n]->InSphere( x, z, radius ) )
		{
			// Store a pointer to it in objects
			objects.push_back( m_vObjects[n] );
		}// End if
	}// End for n
}// End LeafQuadNode::GetAllInSphere
