//------------------------------------------------------------------------------
// 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 "Animation/RotationInterpolator/EulerArrayInterpolator.h"
#include "Animation/RotationInterpolator/QuaternionArrayInterpolator.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
EulerArrayInterpolator::EulerArrayInterpolator() :
	array_(NULL), size_(0), length_(0.f){
}
//------------------------------------------------------------------------------
// デストラクタ
EulerArrayInterpolator::~EulerArrayInterpolator(){
	SafeArrayDelete(array_);
}
//------------------------------------------------------------------------------
// コピーコンストラクタ
EulerArrayInterpolator::EulerArrayInterpolator(
	const EulerArrayInterpolator& copy){
	size_ = copy.size_;
	length_ = copy.length_;
	// コピーコンストラクタは強制的にNULL設定
	array_ = NULL;
	if(size_ == 0){ return; }
	array_ = new Vector3[size_];
	std::memcpy(array_, copy.array_, sizeof(Vector3) * size_);
}
//------------------------------------------------------------------------------
// 代入演算子
EulerArrayInterpolator& EulerArrayInterpolator::operator =(
	const EulerArrayInterpolator& copy){
	// 自分自身ならリターン
	if(this == &copy){ return *this; }
	size_ = copy.size_;
	length_ = copy.length_;
	// 代入の際は解放してNULLを設定
	SafeArrayDelete(array_);
	if(size_ == 0){ return *this; }
	array_ = new Vector3[size_];
	std::memcpy(array_, copy.array_, sizeof(Vector3) * size_);
	return *this;
}
//------------------------------------------------------------------------------
// 補間
//------------------------------------------------------------------------------
// オイラー補間
Vector3 EulerArrayInterpolator::eulerInterpolate(float time){
	Assert(array_ != NULL);
	// 時間が前にあふれている
	if(time <= 0.f){ return array_[0]; }
	// 時間が後ろにあふれている
	if(time >= length_){ return array_[size_ - 1]; }
	// 四元数で補間してオイラー角に変換する
	Quaternion resultQuaternion = quaternionInterpolate(time);
	Vector3 result;
	resultQuaternion.getRotationXYZ(&result);
#ifdef _DEBUG
	if((!Math::classCheck(result.x)) ||
		(!Math::classCheck(result.y)) ||
		(!Math::classCheck(result.z))){
		_asm{ int 3 }
	}
#endif
	return result;
}
//------------------------------------------------------------------------------
// 四元数補間
Quaternion EulerArrayInterpolator::quaternionInterpolate(float time){
	Assert(array_ != NULL);
	Quaternion result;
	// 時間が前にあふれている
	if(time <= 0.f){
		result.setRotationXYZ(array_[0]);
		return result;
	}
	// 時間が後ろにあふれている
	if(time >= length_){
		result.setRotationXYZ(array_[size_ - 1]);
		return result;
	}
	// 補間
	float integer, rate;
	rate = Math::modf(time, &integer);
	int index = (int)integer;
	// 四元数補間
	Quaternion pre, post;
	pre.setRotationXYZ(array_[index]);
	post.setRotationXYZ(array_[index + 1]);
	// 補正付線形補間を行う
	result = Quaternion::correctSlerp(pre, post, rate);
#ifdef _DEBUG
	if((!Math::classCheck(result.x)) ||
		(!Math::classCheck(result.y)) ||
		(!Math::classCheck(result.z)) ||
		(!Math::classCheck(result.w))){
		_asm{ int 3 }
	}
#endif
	return result;
}
//------------------------------------------------------------------------------
// 値
//------------------------------------------------------------------------------
// サイズ設定
void EulerArrayInterpolator::setSize(int size){
	// 補間の為には2以上のsizeが必要
	Assert(size > 1);
	size_ = size;
	length_ = (float)(size_ - 1);
	SafeArrayDelete(array_);
	array_ = new Vector3[size_];
}
//------------------------------------------------------------------------------
// 値の設定
void EulerArrayInterpolator::setValue(int index, const Vector3& value){
	Assert(array_ != NULL);
	Assert(index >= 0);
	Assert(index < size_);
	// +-PIにクランプ
	Vector3 correctValue;
	for(int i = 0; i < 3; i++){
		float temp = value.array[i];
		if((temp <= Math::PI) && (temp >= -Math::PI)){
			correctValue.array[i] = temp;
			continue;
		}
		temp += Math::PI;
		if(temp < 0.f){
			temp = Math::abs(temp);
			temp = Math::fmod(temp, Math::doublePI) - Math::PI;
			temp *= -1;
		}else{
			temp = Math::fmod(temp, Math::doublePI) - Math::PI;
		}
		correctValue.array[i] = temp;
	}
	array_[index] = correctValue;
}
//------------------------------------------------------------------------------
// 変換
//------------------------------------------------------------------------------
// 四元数回転配列補間への変換
QuaternionArrayInterpolator*
	EulerArrayInterpolator::convertQuaternionArrayInterpolator() const{
	QuaternionArrayInterpolator* result = new QuaternionArrayInterpolator();
	int size = getSize();
	result->setSize(size);
	Quaternion quaternion;
	for(int i = 0; i < size; i++){
		quaternion.setRotationXYZ(getValue(i));
		result->setValue(i, quaternion);
	}
	// 値を補正しておく
	result->correctValue();
	return result;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
