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

Triangle.h

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 #ifndef TRIANGLE_H_
00026 #define TRIANGLE_H_
00027 
00028 #include <Core/Primitive/Vector3.h>
00029 #include <Core/Primitive/Matrix33.h>
00030 #include <Core/Primitive/Matrix34.h>
00031 #include <Core/Primitive/Matrix44.h>
00032 
00033 namespace Lamp{
00034 
00035 class Intersection;
00036 class AxisAlignedBox;
00037 class Capsule;
00038 class Cone;
00039 class Line;
00040 class OrientedBox;
00041 class Plane;
00042 class Ray;
00043 class Segment;
00044 class Sphere;
00045 
00046 //------------------------------------------------------------------------------
00047 /**
00048  * 三角
00049  *
00050  * このクラスは継承しないで下さい。
00051  */
00052 class Triangle{
00053 public:
00054     //--------------------------------------------------------------------------
00055     // 定数
00056     //--------------------------------------------------------------------------
00057     /// ゼロ三角
00058     static const Triangle zero;
00059 
00060     //--------------------------------------------------------------------------
00061     // コンストラクタ
00062     //--------------------------------------------------------------------------
00063     /**
00064      * コンストラクタ
00065      *
00066      * このコンストラクタは初期値の設定を行わないため値は不定です。
00067      */
00068     inline Triangle(){}
00069 
00070     /**
00071      * コンストラクタ
00072      * @param vertex0 頂点0の初期値
00073      * @param vertex1 頂点1の初期値
00074      * @param vertex2 頂点2の初期値
00075      */
00076     inline Triangle(const Vector3& vertex0, const Vector3& vertex1,
00077         const Vector3& vertex2){
00078         vertex_[0] = vertex0;
00079         vertex_[1] = vertex1;
00080         vertex_[2] = vertex2;
00081     }
00082 
00083     /**
00084      * コンストラクタ
00085      * @param vertex0x 頂点0のX初期値
00086      * @param vertex0y 頂点0のY初期値
00087      * @param vertex0z 頂点0のZ初期値
00088      * @param vertex1x 頂点1のX初期値
00089      * @param vertex1y 頂点1のY初期値
00090      * @param vertex1z 頂点1のZ初期値
00091      * @param vertex2x 頂点2のX初期値
00092      * @param vertex2y 頂点2のY初期値
00093      * @param vertex2z 頂点2のZ初期値
00094      */
00095     inline Triangle(float vertex0x, float vertex0y, float vertex0z,
00096         float vertex1x, float vertex1y, float vertex1z,
00097         float vertex2x, float vertex2y, float vertex2z){
00098         vertex_[0].set(vertex0x, vertex0y, vertex0z);
00099         vertex_[1].set(vertex1x, vertex1y, vertex1z);
00100         vertex_[2].set(vertex2x, vertex2y, vertex2z);
00101     }
00102 
00103     /**
00104      * コンストラクタ
00105      * @param source 初期値配列
00106      */
00107     inline explicit Triangle(const Vector3* const source){
00108         vertex_[0] = source[0];
00109         vertex_[1] = source[1];
00110         vertex_[2] = source[2];
00111     }
00112 
00113     //--------------------------------------------------------------------------
00114     // 値の設定
00115     //--------------------------------------------------------------------------
00116     /**
00117      * 値の設定
00118      * @param vertex0 設定する頂点0
00119      * @param vertex1 設定する頂点1
00120      * @param vertex2 設定する頂点2
00121      */
00122     inline void set(const Vector3& vertex0, const Vector3& vertex1,
00123         const Vector3& vertex2){
00124         vertex_[0] = vertex0;
00125         vertex_[1] = vertex1;
00126         vertex_[2] = vertex2;
00127     }
00128 
00129     /**
00130      * 値の設定
00131      * @param vertex0x 設定する頂点0のX値
00132      * @param vertex0y 設定する頂点0のY値
00133      * @param vertex0z 設定する頂点0のZ値
00134      * @param vertex1x 設定する頂点1のX値
00135      * @param vertex1y 設定する頂点1のY値
00136      * @param vertex1z 設定する頂点1のZ値
00137      * @param vertex2x 設定する頂点2のX値
00138      * @param vertex2y 設定する頂点2のY値
00139      * @param vertex2z 設定する頂点2のZ値
00140      */
00141     inline void set(float vertex0x, float vertex0y, float vertex0z,
00142         float vertex1x, float vertex1y, float vertex1z,
00143         float vertex2x, float vertex2y, float vertex2z){
00144         vertex_[0].set(vertex0x, vertex0y, vertex0z);
00145         vertex_[1].set(vertex1x, vertex1y, vertex1z);
00146         vertex_[2].set(vertex2x, vertex2y, vertex2z);
00147     }
00148 
00149     /**
00150      * 値の設定
00151      * @param source 設定値配列
00152      */
00153     inline void set(const Vector3* const source){
00154         vertex_[0] = source[0];
00155         vertex_[1] = source[1];
00156         vertex_[2] = source[2];
00157     }
00158 
00159     /**
00160      * 頂点の設定
00161      * @param index 頂点のインデックス
00162      * @param vertex 設定する頂点
00163      */
00164     inline void setVertex(int index, const Vector3& vertex){
00165         Assert((index >= 0) && (index < 3));
00166         vertex_[index] = vertex;
00167     }
00168 
00169     //--------------------------------------------------------------------------
00170     // 値の取得
00171     //--------------------------------------------------------------------------
00172     /**
00173      * 頂点の取得
00174      * @param index 頂点のインデックス
00175      * @return 頂点
00176      */
00177     inline const Vector3& getVertex(int index) const{
00178         Assert((index >= 0) && (index < 3));
00179         return vertex_[index];
00180     }
00181 
00182     /**
00183      * 法線の取得
00184      * @return 法線
00185      */
00186     inline Vector3 getNormal() const{
00187         return Math3D::calculateNormal(vertex_[0], vertex_[1], vertex_[2]);
00188     }
00189 
00190     //--------------------------------------------------------------------------
00191     // 三角演算
00192     //--------------------------------------------------------------------------
00193     /**
00194      * ゼロ三角かどうか
00195      * @return ゼロ三角ならtrueを返す
00196      */
00197     inline bool isZero() const{
00198         return epsilonEquals(zero, Math::epsilon);
00199     }
00200 
00201     //--------------------------------------------------------------------------
00202     // トランスフォーム
00203     //--------------------------------------------------------------------------
00204     /**
00205      * トランスフォーム
00206      * @param matrix 乗算する行列
00207      * @return 変換後の三角
00208      */
00209     inline Triangle transform(const Matrix33& matrix) const{
00210         return Triangle(
00211             matrix * vertex_[0], matrix * vertex_[1], matrix * vertex_[2]);
00212     }
00213 
00214     /**
00215      * トランスフォーム
00216      * @param matrix 乗算する行列
00217      * @return 変換後の三角
00218      */
00219     inline Triangle transform(const Matrix34& matrix) const{
00220         return Triangle(
00221             matrix * vertex_[0], matrix * vertex_[1], matrix * vertex_[2]);
00222     }
00223 
00224     /**
00225      * トランスフォーム
00226      * @param matrix 乗算する行列
00227      * @return 変換後の三角
00228      */
00229     inline Triangle transform(const Matrix44& matrix) const{
00230         return Triangle(
00231             matrix * vertex_[0], matrix * vertex_[1], matrix * vertex_[2]);
00232     }
00233 
00234     //--------------------------------------------------------------------------
00235     // 距離
00236     //--------------------------------------------------------------------------
00237     /**
00238      * 点距離
00239      * @param point 距離判定する点
00240      * @return 距離
00241      */
00242     float getDistance(const Vector3& point) const{
00243         return Math::sqrt(getSquaredDistance(point));
00244     }
00245 
00246     /**
00247      * 点距離の二乗
00248      * @param point 距離判定する点
00249      * @return 距離の二乗
00250      */
00251     float getSquaredDistance(const Vector3& point) const;
00252 
00253     //--------------------------------------------------------------------------
00254     /**
00255      * 軸沿いボックス距離
00256      * @param axisAlignedBox 距離判定する軸沿いボックス
00257      * @return 距離
00258      */
00259     float getDistance(const AxisAlignedBox& axisAlignedBox) const{
00260         return Math::sqrt(getSquaredDistance(axisAlignedBox));
00261     }
00262 
00263     /**
00264      * 軸沿いボックス距離の二乗
00265      * @param axisAlignedBox 距離判定する軸沿いボックス
00266      * @return 距離の二乗
00267      */
00268     float getSquaredDistance(const AxisAlignedBox& axisAlignedBox) const;
00269 
00270     //--------------------------------------------------------------------------
00271     /**
00272      * カプセル距離
00273      * @param capsule 距離判定するカプセル
00274      * @return 距離
00275      */
00276     float getDistance(const Capsule& capsule) const{
00277         return Math::sqrt(getSquaredDistance(capsule));
00278     }
00279 
00280     /**
00281      * カプセル距離の二乗
00282      * @param capsule 距離判定するカプセル
00283      * @return 距離の二乗
00284      */
00285     float getSquaredDistance(const Capsule& capsule) const;
00286 
00287     //--------------------------------------------------------------------------
00288     /**
00289      * コーン距離
00290      * @param cone 距離判定するコーン
00291      * @return 距離
00292      */
00293     float getDistance(const Cone& cone) const{
00294         return Math::sqrt(getSquaredDistance(cone));
00295     }
00296 
00297     /**
00298      * コーン距離の二乗
00299      * @param cone 距離判定するコーン
00300      * @return 距離の二乗
00301      */
00302     float getSquaredDistance(const Cone& cone) const;
00303 
00304     //--------------------------------------------------------------------------
00305     /**
00306      * ライン距離
00307      * @param line 距離判定するライン
00308      * @return 距離
00309      */
00310     float getDistance(const Line& line) const{
00311         return Math::sqrt(getSquaredDistance(line));
00312     }
00313 
00314     /**
00315      * ライン距離の二乗
00316      * @param line 距離判定するライン
00317      * @return 距離の二乗
00318      */
00319     float getSquaredDistance(const Line& line) const;
00320 
00321     //--------------------------------------------------------------------------
00322     /**
00323      * 指向性ボックス距離
00324      * @param orientedBox 距離判定する指向性ボックス
00325      * @return 距離
00326      */
00327     float getDistance(const OrientedBox& orientedBox) const{
00328         return Math::sqrt(getSquaredDistance(orientedBox));
00329     }
00330 
00331     /**
00332      * 指向性ボックス距離の二乗
00333      * @param orientedBox 距離判定する指向性ボックス
00334      * @return 距離の二乗
00335      */
00336     float getSquaredDistance(const OrientedBox& orientedBox) const;
00337 
00338     //--------------------------------------------------------------------------
00339     /**
00340      * 平面距離
00341      * @param plane 距離判定する平面
00342      * @return 距離
00343      */
00344     float getDistance(const Plane& plane) const;
00345 
00346     /**
00347      * 平面距離の二乗
00348      * @param plane 距離判定する平面
00349      * @return 距離の二乗
00350      */
00351     float getSquaredDistance(const Plane& plane) const{
00352         float distance = getDistance(plane);
00353         return (distance * distance);
00354     }
00355 
00356     //--------------------------------------------------------------------------
00357     /**
00358      * レイ距離
00359      * @param ray 距離判定するレイ
00360      * @return 距離
00361      */
00362     float getDistance(const Ray& ray) const{
00363         return Math::sqrt(getSquaredDistance(ray));
00364     }
00365 
00366     /**
00367      * レイ距離の二乗
00368      * @param ray 距離判定するレイ
00369      * @return 距離の二乗
00370      */
00371     float getSquaredDistance(const Ray& ray) const;
00372 
00373     //--------------------------------------------------------------------------
00374     /**
00375      * セグメント距離
00376      * @param segment 距離判定するセグメント
00377      * @return 距離
00378      */
00379     float getDistance(const Segment& segment) const{
00380         return Math::sqrt(getSquaredDistance(segment));
00381     }
00382 
00383     /**
00384      * セグメント距離の二乗
00385      * @param segment 距離判定するセグメント
00386      * @return 距離の二乗
00387      */
00388     float getSquaredDistance(const Segment& segment) const;
00389 
00390     //--------------------------------------------------------------------------
00391     /**
00392      * 球距離
00393      * @param sphere 距離判定する球
00394      * @return 距離
00395      */
00396     float getDistance(const Sphere& sphere) const{
00397         return Math::sqrt(getSquaredDistance(sphere));
00398     }
00399 
00400     /**
00401      * 球距離の二乗
00402      * @param sphere 距離判定する球
00403      * @return 距離の二乗
00404      */
00405     float getSquaredDistance(const Sphere& sphere) const;
00406 
00407     //--------------------------------------------------------------------------
00408     /**
00409      * 三角距離
00410      * @param triangle 距離判定する三角
00411      * @return 距離
00412      */
00413     float getDistance(const Triangle& triangle) const{
00414         return Math::sqrt(getSquaredDistance(triangle));
00415     }
00416 
00417     /**
00418      * 三角距離の二乗
00419      * @param triangle 距離判定する三角
00420      * @return 距離の二乗
00421      */
00422     float getSquaredDistance(const Triangle& triangle) const;
00423 
00424     //--------------------------------------------------------------------------
00425     // 交差
00426     //--------------------------------------------------------------------------
00427     /**
00428      * 点交差
00429      * @param point 交差判定する点
00430      * @param range 交差範囲
00431      * @return 交差していればtrue
00432      */
00433     bool intersect(const Vector3& point, float range = Math::epsilon) const;
00434 
00435     //--------------------------------------------------------------------------
00436     /**
00437      * 軸沿いボックス交差
00438      * @param axisAlignedBox 交差判定する軸沿いボックス
00439      * @return 交差していればtrue
00440      */
00441     bool intersect(const AxisAlignedBox& axisAlignedBox) const;
00442 
00443     //--------------------------------------------------------------------------
00444     /**
00445      * カプセル交差
00446      * @param capsule 交差判定するカプセル
00447      * @return 交差していればtrue
00448      */
00449     bool intersect(const Capsule& capsule) const;
00450 
00451     //--------------------------------------------------------------------------
00452     /**
00453      * コーン交差
00454      * @param cone 交差判定するコーン
00455      * @return 交差していればtrue
00456      */
00457     bool intersect(const Cone& cone) const;
00458 
00459     //--------------------------------------------------------------------------
00460     /**
00461      * ライン交差
00462      * @param line 交差判定するライン
00463      * @return 交差していればtrue
00464      */
00465     bool intersect(const Line& line) const;
00466 
00467     //--------------------------------------------------------------------------
00468     /**
00469      * 指向性ボックス交差
00470      * @param orientedBox 交差判定する指向性ボックス
00471      * @return 交差していればtrue
00472      */
00473     bool intersect(const OrientedBox& orientedBox) const;
00474 
00475     //--------------------------------------------------------------------------
00476     /**
00477      * 平面交差
00478      * @param plane 交差判定する平面
00479      * @return 交差していればtrue
00480      */
00481     bool intersect(const Plane& plane) const;
00482 
00483     //--------------------------------------------------------------------------
00484     /**
00485      * レイ交差
00486      * @param ray 交差判定するレイ
00487      * @return 交差していればtrue
00488      */
00489     bool intersect(const Ray& ray) const;
00490 
00491     //--------------------------------------------------------------------------
00492     /**
00493      * セグメント交差
00494      * @param segment 交差判定するセグメント
00495      * @return 交差していればtrue
00496      */
00497     bool intersect(const Segment& segment) const;
00498 
00499     //--------------------------------------------------------------------------
00500     /**
00501      * 球交差
00502      * @param sphere 交差判定する球
00503      * @return 交差していればtrue
00504      */
00505     bool intersect(const Sphere& sphere) const;
00506 
00507     /**
00508      * 球交差
00509      * @param intersection 交差、返り値がtrueなら設定されている
00510      * @param sphere 交差判定する球
00511      * @return 交差していればtrue
00512      */
00513     bool intersect(Intersection* intersection, const Sphere& sphere) const;
00514 
00515     //--------------------------------------------------------------------------
00516     /**
00517      * 三角交差
00518      * @param triangle 交差判定する三角
00519      * @return 交差していればtrue
00520      */
00521     bool intersect(const Triangle& triangle) const;
00522 
00523     //--------------------------------------------------------------------------
00524     // 論理演算
00525     //--------------------------------------------------------------------------
00526     /**
00527      * 三角が同じかどうか
00528      * @param target 比較する三角
00529      * @return 同じ値であればtrueを返す
00530      */
00531     inline bool operator ==(const Triangle& target) const{
00532         return (
00533             (vertex_[0] == target.vertex_[0]) &&
00534             (vertex_[1] == target.vertex_[1]) &&
00535             (vertex_[2] == target.vertex_[2]));
00536     }
00537 
00538     /**
00539      * 三角が同じかどうか
00540      * @param target 比較する三角
00541      * @param epsilon 誤差
00542      * @return 誤差の範囲内で同じ値であればtrueを返す
00543      */
00544     inline bool epsilonEquals(
00545         const Triangle& target, float epsilon) const{
00546         Assert(epsilon >= 0.f);
00547         return (
00548             (vertex_[0].epsilonEquals(target.vertex_[0], epsilon)) &&
00549             (vertex_[1].epsilonEquals(target.vertex_[1], epsilon)) &&
00550             (vertex_[2].epsilonEquals(target.vertex_[2], epsilon)));
00551     }
00552 
00553     /**
00554      * 三角が同じでないかどうか
00555      * @param target 比較する三角
00556      * @return 同じでない値であればtrueを返す
00557      */
00558     inline bool operator !=(const Triangle& target) const{
00559         return (
00560             (vertex_[0] != target.vertex_[0]) ||
00561             (vertex_[1] != target.vertex_[1]) ||
00562             (vertex_[2] != target.vertex_[2]));
00563     }
00564 
00565     /**
00566      * 三角が同じでないかどうか
00567      * @param target 比較する三角
00568      * @param epsilon 誤差
00569      * @return 誤差の範囲内で同じでない値であればtrueを返す
00570      */
00571     inline bool notEpsilonEquals(
00572         const Triangle& target, float epsilon) const{
00573         Assert(epsilon >= 0.f);
00574         return (
00575             (vertex_[0].notEpsilonEquals(target.vertex_[0], epsilon)) ||
00576             (vertex_[1].notEpsilonEquals(target.vertex_[1], epsilon)) ||
00577             (vertex_[2].notEpsilonEquals(target.vertex_[2], epsilon)));
00578     }
00579 
00580     //--------------------------------------------------------------------------
00581     // その他
00582     //--------------------------------------------------------------------------
00583     /**
00584      * 文字列化
00585      * @return 三角の文字列表記
00586      */
00587     inline String toString() const{
00588         String returnString;
00589         returnString.format("{ ( %.8f, %.8f, %.8f ) ( %.8f, %.8f, %.8f ) "
00590             "( %.8f, %.8f, %.8f ) }",
00591             vertex_[0].x, vertex_[0].y, vertex_[0].z,
00592             vertex_[1].x, vertex_[1].y, vertex_[1].z,
00593             vertex_[2].x, vertex_[2].y, vertex_[2].z);
00594         return returnString;
00595     }
00596 
00597 private:
00598     //--------------------------------------------------------------------------
00599     // メンバ変数
00600     //--------------------------------------------------------------------------
00601     /// 頂点
00602     Vector3 vertex_[3];
00603 
00604 };
00605 
00606 //------------------------------------------------------------------------------
00607 } // End of namespace Lamp
00608 #endif // End of TRIANGLE_H_
00609 //------------------------------------------------------------------------------

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