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

Vector3.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 VECTOR_3_H_
00026 #define VECTOR_3_H_
00027 
00028 #include <Core/System/Math.h>
00029 #include <Core/Primitive/String.h>
00030 
00031 namespace Lamp{
00032 
00033 //------------------------------------------------------------------------------
00034 /**
00035  * 三次元ベクトル
00036  *
00037  * このクラスは継承しないで下さい。
00038  */
00039 class Vector3{
00040 public:
00041     //--------------------------------------------------------------------------
00042     // メンバ変数
00043     //--------------------------------------------------------------------------
00044     /// メンバ変数
00045     union{
00046         /// 各要素
00047         struct{
00048             /// X値
00049             float x;
00050             /// Y値
00051             float y;
00052             /// Z値
00053             float z;
00054         };
00055 
00056         /// 配列
00057         float array[3];
00058     };
00059 
00060     //--------------------------------------------------------------------------
00061     // 定数
00062     //--------------------------------------------------------------------------
00063     /// ゼロベクトル
00064     static const Vector3 zero;
00065 
00066     /// X軸単位ベクトル
00067     static const Vector3 unitX;
00068 
00069     /// Y軸単位ベクトル
00070     static const Vector3 unitY;
00071 
00072     /// Z軸単位ベクトル
00073     static const Vector3 unitZ;
00074 
00075     /// スケール単位ベクトル
00076     static const Vector3 unitScale;
00077 
00078     //--------------------------------------------------------------------------
00079     // コンストラクタ
00080     //--------------------------------------------------------------------------
00081     /**
00082      * コンストラクタ
00083      *
00084      * このコンストラクタは初期値の設定を行わないため値は不定です。
00085      */
00086     inline Vector3(){}
00087 
00088     /**
00089      * コンストラクタ
00090      * @param sourceX Xの初期値
00091      * @param sourceY Yの初期値
00092      * @param sourceZ Zの初期値
00093      */
00094     inline Vector3(float sourceX, float sourceY, float sourceZ) :
00095         x(sourceX), y(sourceY), z(sourceZ){
00096     }
00097 
00098     /**
00099      * コンストラクタ
00100      * @param source 初期値配列
00101      */
00102     inline explicit Vector3(const float* const source) :
00103         x(source[0]), y(source[1]), z(source[2]){
00104     }
00105 
00106     //--------------------------------------------------------------------------
00107     // 値の設定
00108     //--------------------------------------------------------------------------
00109     /**
00110      * 値の設定
00111      * @param sourceX Xの設定値
00112      * @param sourceY Yの設定値
00113      * @param sourceZ Zの設定値
00114      */
00115     inline void set(float sourceX, float sourceY, float sourceZ){
00116         x = sourceX;
00117         y = sourceY;
00118         z = sourceZ;
00119     }
00120 
00121     /**
00122      * 値の設定
00123      * @param source 設定値配列
00124      */
00125     inline void set(const float* const source){
00126         x = source[0];
00127         y = source[1];
00128         z = source[2];
00129     }
00130 
00131     //--------------------------------------------------------------------------
00132     // 演算
00133     //--------------------------------------------------------------------------
00134     /**
00135      * 加算
00136      * @param addVector 加算するベクトル
00137      * @return 加算されたベクトル
00138      */
00139     inline Vector3 operator +(const Vector3& addVector) const{
00140         return Vector3(
00141             x + addVector.x,
00142             y + addVector.y,
00143             z + addVector.z);
00144     }
00145 
00146     /**
00147      * 減算
00148      * @param subVector 減算するベクトル
00149      * @return 減算されたベクトル
00150      */
00151     inline Vector3 operator -(const Vector3& subVector) const{
00152         return Vector3(
00153             x - subVector.x,
00154             y - subVector.y,
00155             z - subVector.z);
00156     }
00157 
00158     /**
00159      * 乗算
00160      * @param mulValue 乗算する値
00161      * @return 乗算されたベクトル
00162      */
00163     inline Vector3 operator *(float mulValue) const{
00164         return Vector3(x * mulValue, y * mulValue, z * mulValue);
00165     }
00166 
00167     /**
00168      * 乗算
00169      * @param mulValue 乗算する値
00170      * @param mulVector 乗算するベクトル
00171      * @return 乗算されたベクトル
00172      */
00173     inline friend Vector3 operator *(float mulValue, const Vector3& mulVector){
00174         return Vector3(
00175             mulVector.x * mulValue,
00176             mulVector.y * mulValue,
00177             mulVector.z * mulValue);
00178     }
00179 
00180     /**
00181      * +演算子
00182      * @return ベクトルのコピー
00183      */
00184     inline Vector3 operator +() const{ return *this; }
00185 
00186     /**
00187      * -演算子
00188      * @return 値の符号が反転したベクトル
00189      */
00190     inline Vector3 operator -() const{ return Vector3(-x, -y, -z); }
00191 
00192     //--------------------------------------------------------------------------
00193     // 代入演算
00194     //--------------------------------------------------------------------------
00195     /**
00196      * 代入加算
00197      * @param addVector 加算するベクトル
00198      * @return 加算されたベクトル
00199      */
00200     inline Vector3& operator +=(const Vector3& addVector){
00201         x += addVector.x;
00202         y += addVector.y;
00203         z += addVector.z;
00204         return *this;
00205     }
00206 
00207     /**
00208      * 代入減算
00209      * @param subVector 減算するベクトル
00210      * @return 減算されたベクトル
00211      */
00212     inline Vector3& operator -=(const Vector3& subVector){
00213         x -= subVector.x;
00214         y -= subVector.y;
00215         z -= subVector.z;
00216         return *this;
00217     }
00218 
00219     /**
00220      * 代入乗算
00221      * @param mulValue 乗算する値
00222      * @return 乗算されたベクトル
00223      */
00224     inline Vector3& operator *=(float mulValue){
00225         x *= mulValue;
00226         y *= mulValue;
00227         z *= mulValue;
00228         return *this;
00229     }
00230 
00231     /**
00232      * 逆ベクトル
00233      */
00234     inline Vector3& inverse(){
00235         x = -x;
00236         y = -y;
00237         z = -z;
00238         return *this;
00239     }
00240 
00241     /**
00242      * 絶対値
00243      * @return 絶対値にされたベクトル
00244      */
00245     inline Vector3& abs(){
00246         x = Math::abs(x);
00247         y = Math::abs(y);
00248         z = Math::abs(z);
00249         return *this;
00250     }
00251 
00252     //--------------------------------------------------------------------------
00253     // ベクトル演算
00254     //--------------------------------------------------------------------------
00255     /**
00256      * 内積
00257      * @param dotVector 内積をとるベクトル
00258      * @return 内積値
00259      */
00260     inline float dotProduct(const Vector3& dotVector) const{
00261         return x * dotVector.x + y * dotVector.y + z * dotVector.z;
00262     }
00263 
00264     /**
00265      * 外積
00266      * @param crossVector 外積をとるベクトル
00267      * @return 外積ベクトル
00268      */
00269     // テンポラリの作成が必要になるので代入バージョンは作成しない
00270     inline Vector3 crossProduct(const Vector3& crossVector) const{
00271         return Vector3(
00272             y * crossVector.z - z * crossVector.y,
00273             z * crossVector.x - x * crossVector.z,
00274             x * crossVector.y - y * crossVector.x
00275         );
00276     }
00277 
00278     //--------------------------------------------------------------------------
00279     // 長さ関連
00280     //--------------------------------------------------------------------------
00281     /**
00282      * ベクトル長の取得
00283      * @return ベクトル長
00284      */
00285     inline float getLength() const{
00286         return Math::sqrt(x * x + y * y + z * z);
00287     }
00288 
00289     /**
00290      * ベクトル長の設定
00291      * @param length 設定するベクトル長
00292      */
00293     inline Vector3& setLength(float length){
00294         float nowLength = getLength();
00295         // 長さが0の場合における対処はアプリケーションによって違うのでAssert
00296         Assert(nowLength >= Math::epsilon);
00297         (*this) *= (length / nowLength);
00298         return *this;
00299     }
00300 
00301     /**
00302      * ベクトル長の二乗を取得
00303      * @return ベクトル長の二乗
00304      */
00305     inline float getSquaredLength() const{
00306         return (x * x + y * y + z * z);
00307     }
00308 
00309     /**
00310      * 正規化
00311      * @return 正規化されたベクトル
00312      */
00313     inline Vector3& normalize(){
00314         float nowLength = getLength();
00315         // 長さが0の場合における対処はアプリケーションによって違うのでAssert
00316         Assert(nowLength >= Math::epsilon);
00317         (*this) *= (1.f / nowLength);
00318         return *this;
00319     }
00320 
00321     /**
00322      * ゼロベクトルかどうか
00323      * @return ゼロベクトルならtrueを返す
00324      */
00325     inline bool isZero() const{
00326         return ((Math::abs(x) <= Math::epsilon) &&
00327             (Math::abs(y) <= Math::epsilon) &&
00328             (Math::abs(z) <= Math::epsilon));
00329     }
00330 
00331     /**
00332      * 単位ベクトルかどうか
00333      * @return 単位ベクトルならtrueを返す
00334      */
00335     inline bool isUnit() const{
00336         return (Math::abs(getLength() - 1.f) <= Math::epsilon);
00337     }
00338 
00339     /**
00340      * 最大値
00341      * @return 最大値
00342      */
00343     inline float maximumValue() const{
00344         float result = x;
00345         if(y > result){ result = y; }
00346         if(z > result){ result = z; }
00347         return result;
00348     }
00349 
00350     /**
00351      * 最小値
00352      * @return 最小値
00353      */
00354     inline float minimumValue() const{
00355         float result = x;
00356         if(y < result){ result = y; }
00357         if(z < result){ result = z; }
00358         return result;
00359     }
00360 
00361     //--------------------------------------------------------------------------
00362     // 論理演算
00363     //--------------------------------------------------------------------------
00364     /**
00365      * ベクトルが同じかどうか
00366      * @param target 比較するベクトル
00367      * @return 同じ値であればtrueを返す
00368      */
00369     inline bool operator ==(const Vector3& target) const{
00370         return ((x == target.x) && (y == target.y) && (z == target.z));
00371     }
00372 
00373     /**
00374      * ベクトルが同じかどうか
00375      * @param target 比較するベクトル
00376      * @param epsilon 誤差
00377      * @return 誤差の範囲内で同じ値であればtrueを返す
00378      */
00379     inline bool epsilonEquals(const Vector3& target, float epsilon) const{
00380         Assert(epsilon >= 0.f);
00381         return (
00382             (Math::abs(x - target.x) <= epsilon) &&
00383             (Math::abs(y - target.y) <= epsilon) &&
00384             (Math::abs(z - target.z) <= epsilon));
00385     }
00386 
00387     /**
00388      * ベクトルが同じでないかどうか
00389      * @param target 比較するベクトル
00390      * @return 同じでない値であればtrueを返す
00391      */
00392     inline bool operator !=(const Vector3& target) const{
00393         return ((x != target.x) || (y != target.y) || (z != target.z));
00394     }
00395 
00396     /**
00397      * ベクトルが同じでないかどうか
00398      * @param target 比較するベクトル
00399      * @param epsilon 誤差
00400      * @return 誤差の範囲内で同じでない値であればtrueを返す
00401      */
00402     inline bool notEpsilonEquals(const Vector3& target, float epsilon) const{
00403         Assert(epsilon >= 0.f);
00404         return (
00405             (Math::abs(x - target.x) > epsilon) ||
00406             (Math::abs(y - target.y) > epsilon) ||
00407             (Math::abs(z - target.z) > epsilon));
00408     }
00409 
00410     //--------------------------------------------------------------------------
00411     // その他
00412     //--------------------------------------------------------------------------
00413     /**
00414      * 文字列化
00415      * @return ベクトルの文字列表記
00416      */
00417     inline String toString() const{
00418         String returnString;
00419         returnString.format("( %.8f, %.8f, %.8f )", x, y, z);
00420         return returnString;
00421     }
00422 
00423     //--------------------------------------------------------------------------
00424 private:
00425 
00426 };
00427 
00428 //------------------------------------------------------------------------------
00429 } // End of namespace Lamp
00430 #endif // End of VECTOR_3_H_
00431 //------------------------------------------------------------------------------

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