Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

AnimationCompressor.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * アニメーション圧縮実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Animation/Utility/AnimationCompressor.h"
00027 #include "Animation/VectorInterpolator/VectorInterpolationCompressor.h"
00028 #include "Animation/RotationInterpolator/RotationInterpolationCompressor.h"
00029 #include "Animation/RotationInterpolator/QuaternionArrayInterpolator.h"
00030 #include "Animation/RotationInterpolator/EulerArrayInterpolator.h"
00031 #include "Animation/System/AnimationSet.h"
00032 #include "Animation/Camera/CameraAnimation.h"
00033 #include "Animation/SceneNode/SceneNodeAnimation.h"
00034 #include "Animation/Model/CharacterModelAnimation.h"
00035 
00036 namespace Lamp{
00037 
00038 //------------------------------------------------------------------------------
00039 // コンストラクタ
00040 AnimationCompressor::AnimationCompressor() :
00041     scaleTolerance_(0.01f), rotationTolerance_(Math::PI * 0.01f),
00042     translationTolerance_(0.1f), scaleCompressor_(NULL),
00043     rotationCompressor_(NULL), translationCompressor_(NULL){
00044 }
00045 //------------------------------------------------------------------------------
00046 // デストラクタ
00047 AnimationCompressor::~AnimationCompressor(){
00048     Assert(scaleCompressor_ == NULL);
00049     Assert(rotationCompressor_ == NULL);
00050     Assert(translationCompressor_ == NULL);
00051 }
00052 //------------------------------------------------------------------------------
00053 // 圧縮
00054 //------------------------------------------------------------------------------
00055 // 圧縮
00056 void AnimationCompressor::compress(Animation* animation){
00057     // 前処理
00058     scaleCompressor_ = new VectorInterpolationCompressor();
00059     rotationCompressor_ = new RotationInterpolationCompressor();
00060     translationCompressor_ = new VectorInterpolationCompressor();
00061     // 圧縮
00062     compressAnimation(animation);
00063     // 後始末
00064     SafeDelete(translationCompressor_);
00065     SafeDelete(rotationCompressor_);
00066     SafeDelete(scaleCompressor_);
00067 }
00068 //------------------------------------------------------------------------------
00069 // アニメーションの圧縮
00070 void AnimationCompressor::compressAnimation(Animation* animation){
00071     if(animation->isAnimationSet()){
00072         compressAnimationSet(animation->castAnimationSet());
00073     }else if(animation->isCameraAnimation()){
00074         compressCameraAnimation(animation->castCameraAnimation());
00075     }else if(animation->isSceneNodeAnimation()){
00076         compressSceneNodeAnimation(animation->castSceneNodeAnimation());
00077     }else if(animation->isCharacterModelAnimation()){
00078         compressCharacterModelAnimation(
00079             animation->castCharacterModelAnimation());
00080     }else{ Assert(false); }
00081 }
00082 //------------------------------------------------------------------------------
00083 // アニメーションセットの圧縮
00084 void AnimationCompressor::compressAnimationSet(AnimationSet* animation){
00085     Assert(animation != NULL);
00086     int count = animation->getAnimationCount();
00087     for(int i = 0; i < count; i++){
00088         compressAnimation(animation->getAnimation(i));
00089     }
00090 }
00091 //------------------------------------------------------------------------------
00092 // カメラアニメーションの圧縮
00093 void AnimationCompressor::compressCameraAnimation(CameraAnimation* animation){
00094     Assert(animation != NULL);
00095     CameraAnimationData* data = animation->getCameraAnimationData();
00096     int sequenceCount = data->getSequenceCount();
00097     for(int i = 0; i < sequenceCount; i++){
00098         data->setRotation(i, compressRotation(data->getRotation(i)));
00099         data->setTranslation(i, compressTranslation(data->getTranslation(i)));
00100     }
00101 }
00102 //------------------------------------------------------------------------------
00103 // シーンノードアニメーションの圧縮
00104 void AnimationCompressor::compressSceneNodeAnimation(
00105     SceneNodeAnimation* animation){
00106     Assert(animation != NULL);
00107     SceneNodeAnimationData* data = animation->getSceneNodeAnimationData();
00108     int sequenceCount = data->getSequenceCount();
00109     for(int i = 0; i < sequenceCount; i++){
00110         data->setScale(i, compressScale(data->getScale(i)));
00111         data->setRotation(i, compressRotation(data->getRotation(i)));
00112         data->setTranslation(i, compressTranslation(data->getTranslation(i)));
00113     }
00114 }
00115 //------------------------------------------------------------------------------
00116 // キャラクタモデルアニメーションの圧縮
00117 void AnimationCompressor::compressCharacterModelAnimation(
00118     CharacterModelAnimation* animation){
00119     Assert(animation != NULL);
00120     CharacterModelAnimationData* data =
00121         animation->getCharacterModelAnimationData();
00122     int sequenceCount = data->getSequenceCount();
00123     int boneCount = data->getBoneCount();
00124     for(int i = 0; i < sequenceCount; i++){
00125         for(int j = 0; j < boneCount; j++){
00126             data->setScale(i, j, compressScale(data->getScale(i, j)));
00127             data->setRotation(i, j, compressRotation(data->getRotation(i, j)));
00128             data->setTranslation(
00129                 i, j, compressTranslation(data->getTranslation(i, j)));
00130         }
00131     }
00132 }
00133 //------------------------------------------------------------------------------
00134 // ユーティリティ
00135 //------------------------------------------------------------------------------
00136 // スケールの圧縮
00137 VectorInterpolator* AnimationCompressor::compressScale(
00138     VectorInterpolator* interpolator){
00139     // NULLならNULLを返す
00140     if(interpolator == NULL){ return NULL; }
00141     VectorArrayInterpolator* array =
00142         interpolator->castVectorArrayInterpolator();
00143     // 配列補間でなければ複製を返す
00144     if(array == NULL){ return interpolator->duplicate(); }
00145     float maxValue = interpolator->getBoundingBox().getSize().maximumValue();
00146     return scaleCompressor_->compress(array, scaleTolerance_);
00147 }
00148 //------------------------------------------------------------------------------
00149 // 回転の圧縮
00150 RotationInterpolator* AnimationCompressor::compressRotation(
00151     RotationInterpolator* interpolator){
00152     // NULLならNULLを返す
00153     if(interpolator == NULL){ return NULL; }
00154     QuaternionArrayInterpolator* array =
00155         interpolator->castQuaternionArrayInterpolator();
00156     if(array == NULL){
00157         EulerArrayInterpolator* eulerArray =
00158             interpolator->castEulerArrayInterpolator();
00159         // 配列補間でなければ複製を返す
00160         if(eulerArray == NULL){ return interpolator->duplicate(); }
00161         array = eulerArray->convertQuaternionArrayInterpolator();
00162     }
00163     return rotationCompressor_->compress(array, rotationTolerance_);
00164 }
00165 //------------------------------------------------------------------------------
00166 // 移動の圧縮
00167 VectorInterpolator* AnimationCompressor::compressTranslation(
00168     VectorInterpolator* interpolator){
00169     // NULLならNULLを返す
00170     if(interpolator == NULL){ return NULL; }
00171     VectorArrayInterpolator* array =
00172         interpolator->castVectorArrayInterpolator();
00173     // 配列補間でなければ複製を返す
00174     if(array == NULL){ return interpolator->duplicate(); }
00175     return translationCompressor_->compress(array, translationTolerance_);
00176 }
00177 //------------------------------------------------------------------------------
00178 } // End of namespace Lamp
00179 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:27 2005 for Lamp by doxygen 1.3.2