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

Segment.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 SEGMENT_H_
00026 #define SEGMENT_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 AxisAlignedBox;
00036 class Capsule;
00037 class Cone;
00038 class Line;
00039 class OrientedBox;
00040 class Plane;
00041 class Ray;
00042 class Sphere;
00043 class Triangle;
00044 
00045 //------------------------------------------------------------------------------
00046 /**
00047  * セグメント
00048  *
00049  * このクラスは継承しないで下さい。
00050  */
00051 class Segment{
00052 public:
00053     //--------------------------------------------------------------------------
00054     // 定数
00055     //--------------------------------------------------------------------------
00056     /// ゼロセグメント
00057     static const Segment zero;
00058 
00059     //--------------------------------------------------------------------------
00060     // コンストラクタ
00061     //--------------------------------------------------------------------------
00062     /**
00063      * コンストラクタ
00064      *
00065      * このコンストラクタは初期値の設定を行わないため値は不定です。
00066      */
00067     Segment(){}
00068 
00069     /**
00070      * コンストラクタ
00071      * @param origin 原点の初期値
00072      * @param direction 方向の初期値
00073      */
00074     inline Segment(const Vector3& origin, const Vector3& direction) :
00075         origin_(origin), direction_(direction){
00076     }
00077 
00078     /**
00079      * コンストラクタ
00080      * @param originX 原点Xの初期値
00081      * @param originY 原点Yの初期値
00082      * @param originZ 原点Zの初期値
00083      * @param directionX 方向Xの初期値
00084      * @param directionY 方向Yの初期値
00085      * @param directionZ 方向Zの初期値
00086      */
00087     inline Segment(float originX, float originY, float originZ,
00088         float directionX, float directionY, float directionZ) :
00089         origin_(originX, originY, originZ),
00090         direction_(directionX, directionY, directionZ){
00091     }
00092 
00093     /**
00094      * コンストラクタ
00095      * @param source 初期値配列
00096      */
00097     inline explicit Segment(const float* const source) :
00098         origin_(source[0], source[1], source[2]),
00099         direction_(source[3], source[4], source[5]){
00100     }
00101 
00102     //--------------------------------------------------------------------------
00103     // 値の設定
00104     //--------------------------------------------------------------------------
00105     /**
00106      * 値の設定
00107      * @param origin 設定する原点
00108      * @param direction 設定する方向
00109      */
00110     inline void set(const Vector3& origin, const Vector3& direction){
00111         origin_ = origin;
00112         direction_ = direction;
00113     }
00114 
00115     /**
00116      * 値の設定
00117      * @param originX 設定する原点X
00118      * @param originY 設定する原点Y
00119      * @param originZ 設定する原点Z
00120      * @param directionX 設定する方向X
00121      * @param directionY 設定する方向Y
00122      * @param directionZ 設定する方向Z
00123      */
00124     inline void set(float originX, float originY, float originZ,
00125         float directionX, float directionY, float directionZ){
00126         origin_.set(originX, originY, originZ);
00127         direction_.set(directionX, directionY, directionZ);
00128     }
00129 
00130     /**
00131      * 値の設定
00132      * @param source 設定値配列
00133      */
00134     inline void set(const float* const source){
00135         origin_.set(source[0], source[1], source[2]);
00136         direction_.set(source[3], source[4], source[5]);
00137     }
00138 
00139     //--------------------------------------------------------------------------
00140     /**
00141      * 原点の設定
00142      * @param origin 設定する原点
00143      */
00144     inline void setOrigin(const Vector3& origin){ origin_ = origin; }
00145 
00146     /**
00147      * 方向の設定
00148      * @param direction 設定する方向
00149      */
00150     inline void setDirection(const Vector3& direction){
00151         direction_ = direction;
00152     }
00153 
00154     //--------------------------------------------------------------------------
00155     /**
00156      * 位置の設定
00157      * @param source ソース位置
00158      * @param target ターゲット位置
00159      */
00160     inline void setPositions(const Vector3& source, const Vector3& target){
00161         origin_ = source;
00162         direction_ = (target - source);
00163     }
00164 
00165     //--------------------------------------------------------------------------
00166     // 値の取得
00167     //--------------------------------------------------------------------------
00168     /**
00169      * 原点の取得
00170      * @return 原点
00171      */
00172     inline const Vector3& getOrigin() const{ return origin_; }
00173 
00174     /**
00175      * 方向の取得
00176      * @return 方向
00177      */
00178     inline const Vector3& getDirection() const{ return direction_; }
00179 
00180     //--------------------------------------------------------------------------
00181     /**
00182      * ソース位置の取得
00183      * @return ソース位置
00184      */
00185     inline const Vector3& getSourcePosition() const{ return origin_; }
00186 
00187     /**
00188      * ターゲット位置の取得
00189      * @return ターゲット位置
00190      */
00191     inline Vector3 getTargetPosition() const{ return (origin_ + direction_); }
00192 
00193     //--------------------------------------------------------------------------
00194     // セグメント演算
00195     //--------------------------------------------------------------------------
00196     /**
00197      * ゼロセグメントかどうか
00198      * @return ゼロセグメントならtrue
00199      */
00200     inline bool isZero() const{
00201         return (direction_.epsilonEquals(Vector3::zero, Math::epsilon));
00202     }
00203 
00204     //--------------------------------------------------------------------------
00205     // トランスフォーム
00206     //--------------------------------------------------------------------------
00207     /**
00208      * トランスフォーム
00209      * @param matrix 乗算する行列
00210      * @return 変換後のセグメント
00211      */
00212     inline Segment transform(const Matrix33& matrix) const{
00213         return Segment(matrix * origin_, matrix * direction_);
00214     }
00215 
00216     /**
00217      * トランスフォーム
00218      * @param matrix 乗算する行列
00219      * @return 変換後のセグメント
00220      */
00221     inline Segment transform(const Matrix34& matrix) const{
00222         return Segment(matrix * origin_, matrix.multiply33(direction_));
00223     }
00224 
00225     /**
00226      * トランスフォーム
00227      * @param matrix 乗算する行列
00228      * @return 変換後のセグメント
00229      */
00230     inline Segment transform(const Matrix44& matrix) const{
00231         return Segment(matrix * origin_, matrix.multiply33(direction_));
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      * @param range 交差範囲
00464      * @return 交差していればtrue
00465      */
00466     bool intersect(const Line& line, float range = Math::epsilon) const;
00467 
00468     //--------------------------------------------------------------------------
00469     /**
00470      * 指向性ボックス交差
00471      * @param orientedBox 交差判定する指向性ボックス
00472      * @return 交差していればtrue
00473      */
00474     bool intersect(const OrientedBox& orientedBox) const;
00475 
00476     //--------------------------------------------------------------------------
00477     /**
00478      * 平面交差
00479      * @param plane 交差判定する平面
00480      * @return 交差していればtrue
00481      */
00482     bool intersect(const Plane& plane) const;
00483 
00484     //--------------------------------------------------------------------------
00485     /**
00486      * レイ交差
00487      * @param ray 交差判定するレイ
00488      * @param range 交差範囲
00489      * @return 交差していればtrue
00490      */
00491     bool intersect(const Ray& ray, float range = Math::epsilon) const;
00492 
00493     //--------------------------------------------------------------------------
00494     /**
00495      * セグメント交差
00496      * @param segment 交差判定するセグメント
00497      * @param range 交差範囲
00498      * @return 交差していればtrue
00499      */
00500     bool intersect(const Segment& segment, float range = Math::epsilon) const;
00501 
00502     //--------------------------------------------------------------------------
00503     /**
00504      * 球交差
00505      * @param sphere 交差判定する球
00506      * @return 交差していればtrue
00507      */
00508     bool intersect(const Sphere& sphere) const;
00509 
00510     //--------------------------------------------------------------------------
00511     /**
00512      * 三角交差
00513      * @param triangle 交差判定する三角
00514      * @return 交差していればtrue
00515      */
00516     bool intersect(const Triangle& triangle) const;
00517 
00518     //--------------------------------------------------------------------------
00519     // 論理演算
00520     //--------------------------------------------------------------------------
00521     /**
00522      * セグメントが同じかどうか
00523      * @param target 比較するセグメント
00524      * @return 同じ値であればtrueを返す
00525      */
00526     inline bool operator ==(const Segment& target) const{
00527         return ((origin_ == target.origin_) &&
00528             (direction_ == target.direction_));
00529     }
00530 
00531     /**
00532      * セグメントが同じかどうか
00533      * @param target 比較するセグメント
00534      * @param epsilon 誤差
00535      * @return 誤差の範囲内で同じ値であればtrueを返す
00536      */
00537     inline bool epsilonEquals(
00538         const Segment& target, float epsilon) const{
00539         Assert(epsilon >= 0.f);
00540         return (origin_.epsilonEquals(target.origin_, epsilon) &&
00541             direction_.epsilonEquals(target.direction_, epsilon));
00542     }
00543 
00544     /**
00545      * セグメントが同じでないかどうか
00546      * @param target 比較するセグメント
00547      * @return 同じでない値であればtrueを返す
00548      */
00549     inline bool operator !=(const Segment& target) const{
00550         return ((origin_ != target.origin_) ||
00551             (direction_ != target.direction_));
00552     }
00553 
00554     /**
00555      * セグメントが同じでないかどうか
00556      * @param target 比較するセグメント
00557      * @param epsilon 誤差
00558      * @return 誤差の範囲内で同じでない値であればtrueを返す
00559      */
00560     inline bool notEpsilonEquals(
00561         const Segment& target, float epsilon) const{
00562         Assert(epsilon >= 0.f);
00563         return (origin_.notEpsilonEquals(target.origin_, epsilon) ||
00564             direction_.notEpsilonEquals(target.direction_, epsilon));
00565     }
00566 
00567     //--------------------------------------------------------------------------
00568     // その他
00569     //--------------------------------------------------------------------------
00570     /**
00571      * 文字列化
00572      * @return セグメントの文字列表記
00573      */
00574     inline String toString() const{
00575         String returnString;
00576         returnString.format("{ ( %.8f, %.8f, %.8f ) ( %.8f, %.8f, %.8f ) }",
00577             origin_.x, origin_.y, origin_.z,
00578             direction_.x, direction_.y, direction_.z);
00579         return returnString;
00580     }
00581 
00582 private:
00583     //--------------------------------------------------------------------------
00584     // メンバ変数
00585     //--------------------------------------------------------------------------
00586     // 原点
00587     Vector3 origin_;
00588     // 方向
00589     Vector3 direction_;
00590 
00591 };
00592 
00593 //------------------------------------------------------------------------------
00594 } // End of namespace Lamp
00595 #endif // End of SEGMENT_H_
00596 //------------------------------------------------------------------------------

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