//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * バウンディングボックス計算フィルタ実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Graphics/SceneFilter/CalculateBoundingBoxFilter/\
CalculateBoundingBoxFilter.h"
#include "Core/Utility/StringTokenizer.h"
#include "Graphics/Mesh/MeshManager.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
CalculateBoundingBoxFilter::CalculateBoundingBoxFilter(Scene* scene) :
	SceneFilterInterface(scene), characterScale_(1.f){
}
//------------------------------------------------------------------------------
// デストラクタ
CalculateBoundingBoxFilter::~CalculateBoundingBoxFilter(){
}
//------------------------------------------------------------------------------
// フィルタ
bool CalculateBoundingBoxFilter::filter(const String& command){
	StringTokenizer tokenizer_(command);
	if(!tokenizer_.hasMoreTokens()){
		ErrorOut("CalculateBoundingBoxFilter::filter() Not found filter name");
		return false;
	}
	String filterName = tokenizer_.getNextToken();
	if(filterName != "CalculateBoundingBox"){
		ErrorOut("CalculateBoundingBoxFilter::filter() Invalid filter name %s",
			filterName.getBytes());
		return false;
	}
	// オプションの解析
	while(tokenizer_.hasMoreTokens()){
		String token = tokenizer_.getNextToken();
		// キャラクタスケールオプション
		if(token == "characterScale"){
			if(!tokenizer_.hasMoreTokens()){
				ErrorOut("CalculateBoundingBoxFilter::filter() "
					"Not found characterScale value");
				return false;
			}
			String characterScaleString = tokenizer_.getNextToken();
			if(!characterScaleString.parseFloat(&characterScale_)){
				ErrorOut(String("CalculateBoundingBoxFilter::filter() "
					"Invalid characterScale value ") + characterScaleString);
				return false;
			}
		}else{
			ErrorOut(String("CalculateBoundingBoxFilter::filter() "
				"Unknown option ") + token);
			return false;
		}
	}
	return filterScene();
}
//------------------------------------------------------------------------------
// シーンのフィルタ
bool CalculateBoundingBoxFilter::filterScene(){
	if(!filterMesh()){ return false; }
	return true;
}
//------------------------------------------------------------------------------
// メッシュのフィルタ
bool CalculateBoundingBoxFilter::filterMesh(){
	int count = meshManager_->getCount();
	// 各メッシュのチェック
	for(int i = 0; i < count; i++){
		if(!filterMesh(meshManager_->get(i))){ return false; }
	}
	return true;
}
//------------------------------------------------------------------------------
// メッシュのフィルタ
bool CalculateBoundingBoxFilter::filterMesh(Mesh* mesh){
	int vertexCount = mesh->getVertexCount();
	if(vertexCount == 0){
		ErrorOut("CalculateBoundingBoxFilter::filterMesh() "
			"頂点を持たないメッシュデータがあります %s",
			mesh->getName().getBytes());
		return false;
	}
	AxisAlignedBox boundingBox(mesh->getPosition(0), mesh->getPosition(0));
	for(int i = 1; i < vertexCount; i++){
		boundingBox.merge(mesh->getPosition(i));
	}
	// キャラクタスケールの適用
	if((characterScale_ != 1.f) && mesh->isCharacterMesh()){
		Vector3 minimum = boundingBox.getMinimum();
		Vector3 maximum = boundingBox.getMaximum();
		Vector3 center = boundingBox.getCenter();
		minimum = center + (minimum - center) * characterScale_;
		maximum = center + (maximum - center) * characterScale_;
		boundingBox.set(minimum, maximum);
	}
	mesh->setBoundingBox(boundingBox);
	return true;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
