//------------------------------------------------------------------------------
// 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/VectorInterpolator/VectorArrayInterpolator.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
VectorArrayInterpolator::VectorArrayInterpolator() :
	array_(NULL), size_(0), length_(0.f){
}
//------------------------------------------------------------------------------
// デストラクタ
VectorArrayInterpolator::~VectorArrayInterpolator(){
	SafeArrayDelete(array_);
}
//------------------------------------------------------------------------------
// コピーコンストラクタ
VectorArrayInterpolator::VectorArrayInterpolator(
	const VectorArrayInterpolator& 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_);
}
//------------------------------------------------------------------------------
// 代入演算子
VectorArrayInterpolator& VectorArrayInterpolator::operator =(
	const VectorArrayInterpolator& 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;
}
//------------------------------------------------------------------------------
// バウンディング
//------------------------------------------------------------------------------
// バウンディングボックスの取得
AxisAlignedBox VectorArrayInterpolator::getBoundingBox() const{
	AxisAlignedBox result(AxisAlignedBox::zero);
	if(size_ == 0){ return result; }
	result.set(array_[0], array_[0]);
	for(int i = 1; i < size_; i++){ result.merge(array_[i]); }
	return result;
}
//------------------------------------------------------------------------------
// 補間
//------------------------------------------------------------------------------
// 補間
Vector3 VectorArrayInterpolator::interpolate(float time){
	Assert(array_ != NULL);
	// 時間が前にあふれている
	if(time <= 0.f){ return array_[0]; }
	// 時間が後ろにあふれている
	if(time >= length_){ return array_[size_ - 1]; }
	// 線形補間
	float integer, rate;
	rate = Math::modf(time, &integer);
	int index = (int)integer;
	Vector3 preValue = array_[index];
	preValue *= (1.f - rate);
	Vector3 postValue = array_[index + 1];
	postValue *= rate;
	preValue += postValue;
	return preValue;
}
//------------------------------------------------------------------------------
// 値
//------------------------------------------------------------------------------
// サイズ設定
void VectorArrayInterpolator::setSize(int size){
	// 補間の為には2以上のsizeが必要
	Assert(size > 1);
	size_ = size;
	length_ = (float)(size_ - 1);
	SafeArrayDelete(array_);
	array_ = new Vector3[size_];
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
