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

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

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