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

# DeformedMeshGeometry.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 "Geometry/Mesh/DeformedMeshGeometry.h"
    00027 #include "Geometry/System/IntersectionResult.h"
    00028
    00029 namespace Lamp{
    00030
    00031 //------------------------------------------------------------------------------
    00032 // 生成、破棄
    00033 //------------------------------------------------------------------------------
    00034 // コンストラクタ
    00035 DeformedMeshGeometry::DeformedMeshGeometry() : MeshGeometry(),
    00036     boundingBox_(AxisAlignedBox::zero), boundingSphere_(Sphere::zero),
    00037     triangles_(NULL), triangleCount_(0){
    00038 }
    00039 //------------------------------------------------------------------------------
    00040 // コピーコンストラクタ
    00041 DeformedMeshGeometry::DeformedMeshGeometry(const DeformedMeshGeometry& copy) :
    00042     MeshGeometry(copy), boundingBox_(AxisAlignedBox::zero),
    00043     boundingSphere_(Sphere::zero), triangles_(NULL), triangleCount_(0){
    00044     copyDeformedMeshGeometryData(copy);
    00045 }
    00046 //------------------------------------------------------------------------------
    00047 // 代入コピー
    00048 const DeformedMeshGeometry& DeformedMeshGeometry::operator =(
    00049     const DeformedMeshGeometry& copy){
    00050     // 自分自身ならリターン
    00051     if(this == &copy){ return *this; }
    00052     MeshGeometry::operator =(copy);
    00053     copyDeformedMeshGeometryData(copy);
    00054     return *this;
    00055 }
    00056 //------------------------------------------------------------------------------
    00057 // 変形メッシュジオメトリデータのコピー
    00058 void DeformedMeshGeometry::copyDeformedMeshGeometryData(
    00059     const DeformedMeshGeometry& copy){
    00060     int triangleCount = copy.getTriangleCount();
    00061     setTriangleCount(triangleCount);
    00062     for(int i = 0; i < triangleCount; i++){
    00063         setTriangle(i, copy.getTriangle(i));
    00064     }
    00065     setBoundingBox(copy.getBoundingBox());
    00066     setBoundingSphere(copy.getBoundingSphere());
    00067 }
    00068 //------------------------------------------------------------------------------
    00069 // デストラクタ
    00070 DeformedMeshGeometry::~DeformedMeshGeometry(){
    00071     SafeArrayDelete(triangles_);
    00072 }
    00073 //------------------------------------------------------------------------------
    00074 // 交差
    00075 //------------------------------------------------------------------------------
    00076 // 球交差
    00077 bool DeformedMeshGeometry::intersect(const Sphere& sphere) const{
    00078     // バウンディング交差
    00079     if(!intersectBounding(sphere)){ return false; }
    00080     // メッシュ交差
    00081     return intersectMesh(sphere);
    00082 }
    00083 //------------------------------------------------------------------------------
    00084 // 球交差
    00085 void DeformedMeshGeometry::intersect(
    00086     IntersectionResult* result, const Sphere& sphere) const{
    00087     // バウンディング交差
    00088     if(!intersectBounding(sphere)){ return; }
    00089     // メッシュ交差
    00090     intersectMesh(result, sphere);
    00091 }
    00092 //------------------------------------------------------------------------------
    00093 // 球バウンディング交差
    00094 bool DeformedMeshGeometry::intersectBounding(const Sphere& sphere) const{
    00095     // バウンディングスフィア
    00096     if(!boundingSphere_.intersect(sphere)){ return false; }
    00097     // バウンディングボックス
    00098     if(!boundingBox_.intersect(sphere)){ return false; }
    00099     return true;
    00100 }
    00101 //------------------------------------------------------------------------------
    00102 // 球メッシュ交差
    00103 bool DeformedMeshGeometry::intersectMesh(const Sphere& sphere) const{
    00104     for(int i = 0; i < triangleCount_; i++){
    00105         if(triangles_[i].intersect(sphere)){ return true; }
    00106     }
    00107     return false;
    00108 }
    00109 //------------------------------------------------------------------------------
    00110 // 球メッシュ交差
    00111 void DeformedMeshGeometry::intersectMesh(
    00112     IntersectionResult* result, const Sphere& sphere) const{
    00113     Intersection intersection;
    00114     for(int i = 0; i < triangleCount_; i++){
    00115         if(triangles_[i].intersect(&intersection, sphere)){
    00116             result->add(intersection);
    00117         }
    00118     }
    00119 }
    00120 //------------------------------------------------------------------------------
    00121 // トライアングル
    00122 //------------------------------------------------------------------------------
    00123 // トライアングル数の設定
    00124 void DeformedMeshGeometry::setTriangleCount(int triangleCount){
    00125     Assert(triangleCount >= 0);
    00126     SafeArrayDelete(triangles_);
    00127     triangleCount_ = triangleCount;
    00128     if(triangleCount_ == 0){ return; }
    00129     triangles_ = new Triangle[triangleCount_];
    00130 }
    00131 //------------------------------------------------------------------------------
    00132 // バウンディング
    00133 //------------------------------------------------------------------------------
    00134 // バウンディングの算出
    00135 void DeformedMeshGeometry::calculateBounding(){
    00136     if(triangleCount_ == 0){ return; }
    00137
    00138     // バウンディングボックス
    00139     const Vector3& initializePosition = triangles_[0].getVertex(0);
    00140     boundingBox_.set(initializePosition, initializePosition);
    00141     for(int i = 0; i < triangleCount_; i++){
    00142         const Triangle& triangle = triangles_[i];
    00143         for(int j = 0; j < 3; j++){
    00144             boundingBox_.merge(triangle.getVertex(j));
    00145         }
    00146     }
    00147
    00148     // バウンディングスフィア
    00149     boundingSphere_.set(boundingBox_.getCenter(), 0.f);
    00150     for(int i = 0; i < triangleCount_; i++){
    00151         const Triangle& triangle = triangles_[i];
    00152         for(int j = 0; j < 3; j++){
    00153             boundingSphere_.append(triangle.getVertex(j));
    00154         }
    00155     }
    00156 }
    00157 //------------------------------------------------------------------------------
    00158 } // End of namespace Lamp
    00159 //------------------------------------------------------------------------------