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

Ray.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 RAY_H_
00026 #define RAY_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 Segment;
00042 class Sphere;
00043 class Triangle;
00044 
00045 //------------------------------------------------------------------------------
00046 /**
00047  * レイ
00048  *
00049  * このクラスは継承しないで下さい。
00050  */
00051 class Ray{
00052 public:
00053     //--------------------------------------------------------------------------
00054     // 定数
00055     //--------------------------------------------------------------------------
00056     /// ゼロレイ
00057     static const Ray zero;
00058 
00059     //--------------------------------------------------------------------------
00060     // コンストラクタ
00061     //--------------------------------------------------------------------------
00062     /**
00063      * コンストラクタ
00064      *
00065      * このコンストラクタは初期値の設定を行わないため値は不定です。
00066      */
00067     Ray(){}
00068 
00069     /**
00070      * コンストラクタ
00071      * @param origin 原点の初期値
00072      * @param direction 方向の初期値
00073      */
00074     inline Ray(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 Ray(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 Ray(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     /**
00158      * 原点の取得
00159      * @return 原点
00160      */
00161     inline const Vector3& getOrigin() const{ return origin_; }
00162 
00163     /**
00164      * 方向の取得
00165      * @return 方向
00166      */
00167     inline const Vector3& getDirection() const{ return direction_; }
00168 
00169     //--------------------------------------------------------------------------
00170     // レイ演算
00171     //--------------------------------------------------------------------------
00172     /**
00173      * ゼロレイかどうか
00174      * @return ゼロレイならtrue
00175      */
00176     inline bool isZero() const{
00177         return (direction_.epsilonEquals(Vector3::zero, Math::epsilon));
00178     }
00179 
00180     //--------------------------------------------------------------------------
00181     // トランスフォーム
00182     //--------------------------------------------------------------------------
00183     /**
00184      * トランスフォーム
00185      * @param matrix 乗算する行列
00186      * @return 変換後のレイ
00187      */
00188     inline Ray transform(const Matrix33& matrix) const{
00189         return Ray(matrix * origin_, matrix * direction_);
00190     }
00191 
00192     /**
00193      * トランスフォーム
00194      * @param matrix 乗算する行列
00195      * @return 変換後のレイ
00196      */
00197     inline Ray transform(const Matrix34& matrix) const{
00198         return Ray(matrix * origin_, matrix.multiply33(direction_));
00199     }
00200 
00201     /**
00202      * トランスフォーム
00203      * @param matrix 乗算する行列
00204      * @return 変換後のレイ
00205      */
00206     inline Ray transform(const Matrix44& matrix) const{
00207         return Ray(matrix * origin_, matrix.multiply33(direction_));
00208     }
00209 
00210     //--------------------------------------------------------------------------
00211     // 距離
00212     //--------------------------------------------------------------------------
00213     /**
00214      * 点距離
00215      * @param point 距離判定する点
00216      * @return 距離
00217      */
00218     float getDistance(const Vector3& point) const{
00219         return Math::sqrt(getSquaredDistance(point));
00220     }
00221 
00222     /**
00223      * 点距離の二乗
00224      * @param point 距離判定する点
00225      * @return 距離の二乗
00226      */
00227     float getSquaredDistance(const Vector3& point) const;
00228 
00229     //--------------------------------------------------------------------------
00230     /**
00231      * 軸沿いボックス距離
00232      * @param axisAlignedBox 距離判定する軸沿いボックス
00233      * @return 距離
00234      */
00235     float getDistance(const AxisAlignedBox& axisAlignedBox) const{
00236         return Math::sqrt(getSquaredDistance(axisAlignedBox));
00237     }
00238 
00239     /**
00240      * 軸沿いボックス距離の二乗
00241      * @param axisAlignedBox 距離判定する軸沿いボックス
00242      * @return 距離の二乗
00243      */
00244     float getSquaredDistance(const AxisAlignedBox& axisAlignedBox) const;
00245 
00246     //--------------------------------------------------------------------------
00247     /**
00248      * カプセル距離
00249      * @param capsule 距離判定するカプセル
00250      * @return 距離
00251      */
00252     float getDistance(const Capsule& capsule) const{
00253         return Math::sqrt(getSquaredDistance(capsule));
00254     }
00255 
00256     /**
00257      * カプセル距離の二乗
00258      * @param capsule 距離判定するカプセル
00259      * @return 距離の二乗
00260      */
00261     float getSquaredDistance(const Capsule& capsule) const;
00262 
00263     //--------------------------------------------------------------------------
00264     /**
00265      * コーン距離
00266      * @param cone 距離判定するコーン
00267      * @return 距離
00268      */
00269     float getDistance(const Cone& cone) const{
00270         return Math::sqrt(getSquaredDistance(cone));
00271     }
00272 
00273     /**
00274      * コーン距離の二乗
00275      * @param cone 距離判定するコーン
00276      * @return 距離の二乗
00277      */
00278     float getSquaredDistance(const Cone& cone) const;
00279 
00280     //--------------------------------------------------------------------------
00281     /**
00282      * ライン距離
00283      * @param line 距離判定するライン
00284      * @return 距離
00285      */
00286     float getDistance(const Line& line) const{
00287         return Math::sqrt(getSquaredDistance(line));
00288     }
00289 
00290     /**
00291      * ライン距離の二乗
00292      * @param line 距離判定するライン
00293      * @return 距離の二乗
00294      */
00295     float getSquaredDistance(const Line& line) const;
00296 
00297     //--------------------------------------------------------------------------
00298     /**
00299      * 指向性ボックス距離
00300      * @param orientedBox 距離判定する指向性ボックス
00301      * @return 距離
00302      */
00303     float getDistance(const OrientedBox& orientedBox) const{
00304         return Math::sqrt(getSquaredDistance(orientedBox));
00305     }
00306 
00307     /**
00308      * 指向性ボックス距離の二乗
00309      * @param orientedBox 距離判定する指向性ボックス
00310      * @return 距離の二乗
00311      */
00312     float getSquaredDistance(const OrientedBox& orientedBox) const;
00313 
00314     //--------------------------------------------------------------------------
00315     /**
00316      * 平面距離
00317      * @param plane 距離判定する平面
00318      * @return 距離
00319      */
00320     float getDistance(const Plane& plane) const;
00321 
00322     /**
00323      * 平面距離の二乗
00324      * @param plane 距離判定する平面
00325      * @return 距離の二乗
00326      */
00327     float getSquaredDistance(const Plane& plane) const{
00328         float distance = getDistance(plane);
00329         return (distance * distance);
00330     }
00331 
00332     //--------------------------------------------------------------------------
00333     /**
00334      * レイ距離
00335      * @param ray 距離判定するレイ
00336      * @return 距離
00337      */
00338     float getDistance(const Ray& ray) const{
00339         return Math::sqrt(getSquaredDistance(ray));
00340     }
00341 
00342     /**
00343      * レイ距離の二乗
00344      * @param ray 距離判定するレイ
00345      * @return 距離の二乗
00346      */
00347     float getSquaredDistance(const Ray& ray) const;
00348 
00349     //--------------------------------------------------------------------------
00350     /**
00351      * セグメント距離
00352      * @param segment 距離判定するセグメント
00353      * @return 距離
00354      */
00355     float getDistance(const Segment& segment) const{
00356         return Math::sqrt(getSquaredDistance(segment));
00357     }
00358 
00359     /**
00360      * セグメント距離の二乗
00361      * @param segment 距離判定するセグメント
00362      * @return 距離の二乗
00363      */
00364     float getSquaredDistance(const Segment& segment) const;
00365 
00366     //--------------------------------------------------------------------------
00367     /**
00368      * 球距離
00369      * @param sphere 距離判定する球
00370      * @return 距離
00371      */
00372     float getDistance(const Sphere& sphere) const{
00373         return Math::sqrt(getSquaredDistance(sphere));
00374     }
00375 
00376     /**
00377      * 球距離の二乗
00378      * @param sphere 距離判定する球
00379      * @return 距離の二乗
00380      */
00381     float getSquaredDistance(const Sphere& sphere) const;
00382 
00383     //--------------------------------------------------------------------------
00384     /**
00385      * 三角距離
00386      * @param triangle 距離判定する三角
00387      * @return 距離
00388      */
00389     float getDistance(const Triangle& triangle) const{
00390         return Math::sqrt(getSquaredDistance(triangle));
00391     }
00392 
00393     /**
00394      * 三角距離の二乗
00395      * @param triangle 距離判定する三角
00396      * @return 距離の二乗
00397      */
00398     float getSquaredDistance(const Triangle& triangle) const;
00399 
00400     //--------------------------------------------------------------------------
00401     // 交差
00402     //--------------------------------------------------------------------------
00403     /**
00404      * 点交差
00405      * @param point 交差判定する点
00406      * @param range 交差範囲
00407      * @return 交差していればtrue
00408      */
00409     bool intersect(const Vector3& point, float range = Math::epsilon) const;
00410 
00411     //--------------------------------------------------------------------------
00412     /**
00413      * 軸沿いボックス交差
00414      * @param axisAlignedBox 交差判定する軸沿いボックス
00415      * @return 交差していればtrue
00416      */
00417     bool intersect(const AxisAlignedBox& axisAlignedBox) const;
00418 
00419     //--------------------------------------------------------------------------
00420     /**
00421      * カプセル交差
00422      * @param capsule 交差判定するカプセル
00423      * @return 交差していればtrue
00424      */
00425     bool intersect(const Capsule& capsule) const;
00426 
00427     //--------------------------------------------------------------------------
00428     /**
00429      * コーン交差
00430      * @param cone 交差判定するコーン
00431      * @return 交差していればtrue
00432      */
00433     bool intersect(const Cone& cone) const;
00434 
00435     //--------------------------------------------------------------------------
00436     /**
00437      * ライン交差
00438      * @param line 交差判定するライン
00439      * @param range 交差範囲
00440      * @return 交差していればtrue
00441      */
00442     bool intersect(const Line& line, float range = Math::epsilon) const;
00443 
00444     //--------------------------------------------------------------------------
00445     /**
00446      * 指向性ボックス交差
00447      * @param orientedBox 交差判定する指向性ボックス
00448      * @return 交差していればtrue
00449      */
00450     bool intersect(const OrientedBox& orientedBox) const;
00451 
00452     //--------------------------------------------------------------------------
00453     /**
00454      * 平面交差
00455      * @param plane 交差判定する平面
00456      * @return 交差していればtrue
00457      */
00458     bool intersect(const Plane& plane) const;
00459 
00460     //--------------------------------------------------------------------------
00461     /**
00462      * レイ交差
00463      * @param ray 交差判定するレイ
00464      * @param range 交差範囲
00465      * @return 交差していればtrue
00466      */
00467     bool intersect(const Ray& ray, float range = Math::epsilon) const;
00468 
00469     //--------------------------------------------------------------------------
00470     /**
00471      * セグメント交差
00472      * @param segment 交差判定するセグメント
00473      * @param range 交差範囲
00474      * @return 交差していればtrue
00475      */
00476     bool intersect(const Segment& segment, float range = Math::epsilon) const;
00477 
00478     //--------------------------------------------------------------------------
00479     /**
00480      * 球交差
00481      * @param sphere 交差判定する球
00482      * @return 交差していればtrue
00483      */
00484     bool intersect(const Sphere& sphere) const;
00485 
00486     //--------------------------------------------------------------------------
00487     /**
00488      * 三角交差
00489      * @param triangle 交差判定する三角
00490      * @return 交差していればtrue
00491      */
00492     bool intersect(const Triangle& triangle) const;
00493 
00494     //--------------------------------------------------------------------------
00495     // 論理演算
00496     //--------------------------------------------------------------------------
00497     /**
00498      * レイが同じかどうか
00499      * @param target 比較するレイ
00500      * @return 同じ値であればtrueを返す
00501      */
00502     inline bool operator ==(const Ray& target) const{
00503         return ((origin_ == target.origin_) &&
00504             (direction_ == target.direction_));
00505     }
00506 
00507     /**
00508      * レイが同じかどうか
00509      * @param target 比較するレイ
00510      * @param epsilon 誤差
00511      * @return 誤差の範囲内で同じ値であればtrueを返す
00512      */
00513     inline bool epsilonEquals(
00514         const Ray& target, float epsilon) const{
00515         Assert(epsilon >= 0.f);
00516         return (origin_.epsilonEquals(target.origin_, epsilon) &&
00517             direction_.epsilonEquals(target.direction_, epsilon));
00518     }
00519 
00520     /**
00521      * レイが同じでないかどうか
00522      * @param target 比較するレイ
00523      * @return 同じでない値であればtrueを返す
00524      */
00525     inline bool operator !=(const Ray& target) const{
00526         return ((origin_ != target.origin_) ||
00527             (direction_ != target.direction_));
00528     }
00529 
00530     /**
00531      * レイが同じでないかどうか
00532      * @param target 比較するレイ
00533      * @param epsilon 誤差
00534      * @return 誤差の範囲内で同じでない値であればtrueを返す
00535      */
00536     inline bool notEpsilonEquals(
00537         const Ray& target, float epsilon) const{
00538         Assert(epsilon >= 0.f);
00539         return (origin_.notEpsilonEquals(target.origin_, epsilon) ||
00540             direction_.notEpsilonEquals(target.direction_, epsilon));
00541     }
00542 
00543     //--------------------------------------------------------------------------
00544     // その他
00545     //--------------------------------------------------------------------------
00546     /**
00547      * 文字列化
00548      * @return レイの文字列表記
00549      */
00550     inline String toString() const{
00551         String returnString;
00552         returnString.format("{ ( %.8f, %.8f, %.8f ) ( %.8f, %.8f, %.8f ) }",
00553             origin_.x, origin_.y, origin_.z,
00554             direction_.x, direction_.y, direction_.z);
00555         return returnString;
00556     }
00557 
00558 private:
00559     //--------------------------------------------------------------------------
00560     // メンバ変数
00561     //--------------------------------------------------------------------------
00562     // 原点
00563     Vector3 origin_;
00564     // 方向
00565     Vector3 direction_;
00566 
00567 };
00568 
00569 //------------------------------------------------------------------------------
00570 } // End of namespace Lamp
00571 #endif // End of RAY_H_
00572 //------------------------------------------------------------------------------

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