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

# CalculateBoundingBoxFilter.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 "Graphics/SceneFilter/CalculateBoundingBoxFilter/\
    00027 CalculateBoundingBoxFilter.h"
    00028 #include "Core/Utility/StringTokenizer.h"
    00029 #include "Graphics/Mesh/MeshManager.h"
    00030
    00031 namespace Lamp{
    00032
    00033 //------------------------------------------------------------------------------
    00034 // コンストラクタ
    00035 CalculateBoundingBoxFilter::CalculateBoundingBoxFilter(Scene* scene) :
    00036     SceneFilterInterface(scene), characterScale_(1.f){
    00037 }
    00038 //------------------------------------------------------------------------------
    00039 // デストラクタ
    00040 CalculateBoundingBoxFilter::~CalculateBoundingBoxFilter(){
    00041 }
    00042 //------------------------------------------------------------------------------
    00043 // フィルタ
    00044 bool CalculateBoundingBoxFilter::filter(const String& command){
    00045     StringTokenizer tokenizer_(command);
    00046     if(!tokenizer_.hasMoreTokens()){
    00047         ErrorOut("CalculateBoundingBoxFilter::filter() Not found filter name");
    00048         return false;
    00049     }
    00050     String filterName = tokenizer_.getNextToken();
    00051     if(filterName != "CalculateBoundingBox"){
    00052         ErrorOut("CalculateBoundingBoxFilter::filter() Invalid filter name %s",
    00053             filterName.getBytes());
    00054         return false;
    00055     }
    00056     // オプションの解析
    00057     while(tokenizer_.hasMoreTokens()){
    00058         String token = tokenizer_.getNextToken();
    00059         // キャラクタスケールオプション
    00060         if(token == "characterScale"){
    00061             if(!tokenizer_.hasMoreTokens()){
    00062                 ErrorOut("CalculateBoundingBoxFilter::filter() "
    00063                     "Not found characterScale value");
    00064                 return false;
    00065             }
    00066             String characterScaleString = tokenizer_.getNextToken();
    00067             if(!characterScaleString.parseFloat(&characterScale_)){
    00068                 ErrorOut(String("CalculateBoundingBoxFilter::filter() "
    00069                     "Invalid characterScale value ") + characterScaleString);
    00070                 return false;
    00071             }
    00072         }else{
    00073             ErrorOut(String("CalculateBoundingBoxFilter::filter() "
    00074                 "Unknown option ") + token);
    00075             return false;
    00076         }
    00077     }
    00078     return filterScene();
    00079 }
    00080 //------------------------------------------------------------------------------
    00081 // シーンのフィルタ
    00082 bool CalculateBoundingBoxFilter::filterScene(){
    00083     if(!filterMesh()){ return false; }
    00084     return true;
    00085 }
    00086 //------------------------------------------------------------------------------
    00087 // メッシュのフィルタ
    00088 bool CalculateBoundingBoxFilter::filterMesh(){
    00089     int count = meshManager_->getCount();
    00090     // 各メッシュのチェック
    00091     for(int i = 0; i < count; i++){
    00092         if(!filterMesh(meshManager_->get(i))){ return false; }
    00093     }
    00094     return true;
    00095 }
    00096 //------------------------------------------------------------------------------
    00097 // メッシュのフィルタ
    00098 bool CalculateBoundingBoxFilter::filterMesh(Mesh* mesh){
    00099     int vertexCount = mesh->getVertexCount();
    00100     if(vertexCount == 0){
    00101         ErrorOut("CalculateBoundingBoxFilter::filterMesh() "
    00102             "頂点を持たないメッシュデータがあります %s",
    00103             mesh->getName().getBytes());
    00104         return false;
    00105     }
    00106     AxisAlignedBox boundingBox(mesh->getPosition(0), mesh->getPosition(0));
    00107     for(int i = 1; i < vertexCount; i++){
    00108         boundingBox.merge(mesh->getPosition(i));
    00109     }
    00110     // キャラクタスケールの適用
    00111     if((characterScale_ != 1.f) && mesh->isCharacterMesh()){
    00112         Vector3 minimum = boundingBox.getMinimum();
    00113         Vector3 maximum = boundingBox.getMaximum();
    00114         Vector3 center = boundingBox.getCenter();
    00115         minimum = center + (minimum - center) * characterScale_;
    00116         maximum = center + (maximum - center) * characterScale_;
    00117         boundingBox.set(minimum, maximum);
    00118     }
    00119     mesh->setBoundingBox(boundingBox);
    00120     return true;
    00121 }
    00122 //------------------------------------------------------------------------------
    00123 } // End of namespace Lamp
    00124 //------------------------------------------------------------------------------