Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Vector3.h

Go to the documentation of this file.
00001 
00002 //
00003 //    Title     : Vector3.h
00004 //    Project     : OSCAR Version 2.0
00005 //    Created     : 9/21/1994
00006 //      Author          : Chetan Kapoor
00007 //      Platforms       : All
00008 //      Copyright       : Copyright© The University of Texas at Austin, 2002. All rights reserved.
00009 //
00010 //          This software and documentation constitute an unpublished work
00011 //          and contain valuable trade secrets and proprietary information
00012 //          belonging to the University. None of the foregoing material may be
00013 //          copied or duplicated or disclosed without the express, written
00014 //          permission of University. THE UNIVERSITY EXPRESSLY DISCLAIMS ANY
00015 //          AND ALL WARRANTIES CONCERNING THIS SOFTWARE AND DOCUMENTATION,
00016 //          INCLUDING ANY WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
00017 //          PARTICULAR PURPOSE, AND WARRANTIES OF PERFORMANCE, AND ANY WARRANTY
00018 //          THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE.
00019 //          NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF
00020 //          THE SOFTWARE OR DOCUMENTATION. Under no circumstances shall the
00021 //          University be liable for incidental, special, indirect, direct or
00022 //          consequential damages or loss of profits, interruption of business,
00023 //          or related expenses which may arise from use of software or documentation,
00024 //          including but not limited to those resulting from defects in software
00025 //          and/or documentation, or loss or inaccuracy of data of any kind.
00026 //
00027 //    Purpose     :
00028 //
00029 //-----------------------------------------------------------------------------
00030 //
00031 //    $Revisions$
00032 //      Jacy Legault -Added cross operator 4/10/98
00033 //      Troy Harden  -Added + and - operators 6-7-99
00034 //      Ethan Swint - Added Norm2 to return square of norm 1/29/03
00035 //
00036 //    $Log: Vector3.h,v $
00037 //    Revision 1.30  2007/07/09 19:31:42  kapoorc
00038 //    Changed abort() calls to exit(EXIT_FAILURE). Exit calls clear up global memory vs. abort that do not. also, abort() is not supported in WINCE
00039 //
00040 //    Revision 1.29  2005/11/04 19:07:18  chetan
00041 //    Undoing my removal of static TLS
00042 //
00043 //    Revision 1.27  2005/05/17 20:09:15  edjung
00044 //    Revised STATIC_THREAD_LOCAL macros and updated all libraries to use them
00045 //
00046 //    Revision 1.26  2005/04/26 22:23:28  edjung
00047 //    -Cleaned up some header includes by removing TSS_T.h
00048 //
00049 //    Revision 1.25  2005/03/21 17:26:28  aspencer
00050 //    Changed the "Distance" and "DistanceSqr" functions from taking non-const Vector3's to const Vector3's.
00051 //
00052 //    Revision 1.24  2005/03/18 22:16:09  chetan
00053 //    Enabled W4 level warnings and fixed warnings
00054 //
00055 //    Revision 1.23  2005/03/14 22:04:28  pmarch
00056 //    Added to namespace OSCAR and removed "RR" from front of class names.
00057 //
00058 //    Revision 1.22  2005/02/23 14:33:08  aspencer
00059 //    Added "Distance" and "Distance Sqr" functions
00060 //
00061 //    Revision 1.21  2005/02/03 22:51:49  edjung
00062 //    Made changes to add Thread Local Storage using ACE
00063 //    Also some loop unrolling
00064 //
00065 //    Revision 1.20  2005/01/20 18:23:22  chetan
00066 //    Code change for speed optimization. eliminated copying
00067 //
00068 //    Revision 1.19  2005/01/06 22:51:02  chetan
00069 //    Fixed TLS and added default constructors
00070 //
00071 //    Revision 1.18  2005/01/06 20:46:48  chetan
00072 //    Added Thread Local Storage to static variables
00073 //
00074 //    Revision 1.17  2005/01/05 22:25:00  josh
00075 //    Added more efficient functions of vector addition, subtraction, multiplication, and division.
00076 //
00077 //    Revision 1.16  2005/01/05 19:10:10  josh
00078 //    Changed the assignment operators to take a reference instead of a value.
00079 //
00080 //    Revision 1.15  2004/06/08 23:00:03  chetan
00081 //    Performance improvements and better error handling
00082 //
00083 //    Revision 1.14  2004/06/07 22:50:58  chetan
00084 //    Added quite a few efficient methods for operator+,-,*,/+=,*=,/=,-=, etc. Added Dot product, opeator=, operator==, etc.
00085 //
00086 //    Revision 1.13  2004/05/26 20:01:54  pmarch
00087 //    Added /defgroup and /ingroup tags.
00088 //
00089 //    Revision 1.12  2004/01/26 20:54:32  pmarch
00090 //    Fixed documentation errors as reported by Doxygen.
00091 //
00092 //    Revision 1.11  2003/10/28 21:55:47  eswint
00093 //    - Reinstated Vector3::operator-(Vector3) that returns Vector3
00094 //
00095 //    Revision 1.10  2003/10/28 21:48:01  eswint
00096 //    - Reinstated operator+(Vector3) that returns Vector3
00097 //
00098 //    Revision 1.9  2003/10/06 09:16:06  edjung
00099 //    -Changed destructor to virtual to fix memory leak
00100 //
00101 //    Revision 1.8  2003/09/09 17:13:05  eswint
00102 //    - Changed #include "Math/..." to #include "..."
00103 //
00104 //    Revision 1.7  2003/09/02 20:50:02  eswint
00105 //    - Made construction from double* explicit
00106 //
00107 //    Revision 1.6  2003/08/26 22:23:58  eswint
00108 //    - Added const methods for X(), Y(), and Z().  The GetX(),GetY(),GetZ() methods are deprecated.
00109 //
00110 //    Revision 1.5  2003/08/25 17:27:59  eswint
00111 //    - Removed duplicate data member
00112 //    - Removed duplicate mathematical operators and constructors
00113 //
00114 //    Revision 1.4  2003/08/22 20:06:38  eswint
00115 //    - Changed Vector3 to inherit from FixedSizeArray
00116 //
00117 //    Revision 1.3  2003/08/19 18:22:45  eswint
00118 //    -Cleaned up commenting
00119 //
00120 //    Revision 1.2  2003/08/19 18:09:28  eswint
00121 //    - Added Orthog method to VectorNumeric to orthogonalize function parameter against calling object
00122 //    - Moved Norm, Normalize, and Unit from Vector3 to VectorNumeric
00123 //
00124 //    Revision 1.1  2003/08/18 15:23:22  eswint
00125 //    Initial Commit
00126 //
00127 //
00129 #ifndef Vector3_hpp
00130 #define Vector3_hpp
00131 
00132 #include "FixedSizeMatrix.h"
00133 #include "Vector.h"
00134 
00136 namespace OSCAR {
00137 
00147   class Vector3 : public FixedSizeArray<Vector, 3>
00148   {
00149   public:
00150 
00156     Vector3();
00157 
00166     Vector3(double x, double y, double z);
00167 
00174     explicit Vector3(double *array);
00175 
00183     Vector3(const Vector& arg);
00184 
00190     virtual ~Vector3();
00191 
00198     double& X() {
00199       return element[0];
00200     }
00201 
00208     const double& X() const {
00209       return element[0];
00210     }
00211 
00218     double& Y() {
00219       return element[1];
00220     }
00221 
00228     const double& Y() const {
00229       return element[1];
00230     }
00231 
00238     double& Z() {
00239       return element[2];
00240     }
00241 
00248     const double& Z() const {
00249       return element[2];
00250     }
00251 
00258     double GetX() const {
00259       return element[0];
00260     }
00261 
00268     double GetY() const {
00269       return element[1];
00270     }
00271 
00278     double GetZ() const {
00279       return element[2];
00280     }
00281 
00288     double Distance(const Vector3& vec) const {
00289         return sqrt(DistanceSqr(vec));
00290     }
00291 
00298     double DistanceSqr(const Vector3& vec) const {
00299         return (element[0]-vec.at(0))*(element[0]-vec.at(0))+(element[1]-vec.at(1))*(element[1]-vec.at(1))+(element[2]-vec.at(2))*(element[2]-vec.at(2));
00300     }
00301 
00308     void X(double x) {
00309       element[0] = x;
00310     }
00311 
00318     void Y(double y) {
00319       element[1] = y;
00320     }
00321 
00328     void Z(double z) {
00329       element[2] = z;
00330     }
00331 
00332 
00339     bool operator==(const Vector3& rhs) const
00340     {
00341       if(at(0) != rhs.at(0) || at(1) != rhs.at(1) || at(2) != rhs.at(2)){
00342         return false;
00343       }
00344       return true;
00345     }
00346 
00354     Vector3 Cross(const Vector3& v2) const;
00355 
00360     void Null(){
00361       at(0) = at(1) = at(2) = 0.0;
00362     }
00363 
00371     double Dot(const Vector3& rhs) const
00372     {
00373       return at(0)*rhs.at(0) + at(1)*rhs.at(1)+ at(2)*rhs.at(2);
00374     }
00375 
00382     Matrix Cross();
00383 
00384     Vector3& operator=(const Vector3& rhs) {
00385       at(0) = rhs.at(0);
00386       at(1) = rhs.at(1);
00387       at(2) = rhs.at(2);
00388       return *this;
00389     }
00390 
00391 
00392     Vector3& operator=(const Vector& rhs) {
00393       if(rhs.GetSize() != GetSize()){
00394         DisplayError(arraySizeMismatch);
00395                 exit(EXIT_FAILURE);      
00396           }
00397       at(0) = rhs.at(0);
00398       at(1) = rhs.at(1);
00399       at(2) = rhs.at(2);
00400       return *this;
00401     }
00402 
00403     Vector3& operator=(double rhs) {
00404       at(0) = at(1) = at(2) = rhs;
00405       return *this;
00406     }
00407 
00408     Vector3& operator+=(const Vector3& rhs){
00409       at(0) += rhs.at(0);
00410       at(1) += rhs.at(1);
00411       at(2) += rhs.at(2);
00412       return *this;
00413     }
00414 
00415     Vector3& operator-=(const Vector3& rhs){
00416       at(0) -= rhs.at(0);
00417       at(1) -= rhs.at(1);
00418       at(2) -= rhs.at(2);
00419       return *this;
00420     }
00421 
00422     Vector3& operator*=(const Vector3& rhs){
00423       at(0) *= rhs.at(0);
00424       at(1) *= rhs.at(1);
00425       at(2) *= rhs.at(2);
00426       return *this;
00427     }
00428 
00429     Vector3& operator+=(double rhs) {
00430       at(0) += rhs;
00431       at(1) += rhs;
00432       at(2) += rhs;
00433       return *this;
00434     }
00435 
00436     Vector3& operator-=(double rhs) {
00437       at(0) -= rhs;
00438       at(1) -= rhs;
00439       at(2) -= rhs;
00440       return *this;
00441     }
00442 
00443     Vector3& operator*=(double rhs) {
00444       at(0) *= rhs;
00445       at(1) *= rhs;
00446       at(2) *= rhs;
00447       return *this;
00448     }
00449 
00450     Vector3& operator/=(double rhs) {
00451       if(rhs == 0.0){
00452         DisplayError(divideByZero);
00453                 exit(EXIT_FAILURE);      
00454           }
00455       at(0) /= rhs;
00456       at(1) /= rhs;
00457       at(2) /= rhs;
00458       return *this;
00459     }
00460 
00461 
00462     Vector3 operator+(const Vector3& rhs) const {
00463       //static TLS Vector3 result;
00464       STATIC_THREAD_LOCAL(Vector3, p_result);
00465       /* result = *this;
00466       result += rhs; */
00467       // instead of the above, do this
00468       p_result->at(0) = this->at(0) + rhs.at(0);
00469       p_result->at(1) = this->at(1) + rhs.at(1);
00470       p_result->at(2) = this->at(2) + rhs.at(2);
00471 
00472       return *p_result;
00473     }
00474 
00481     void Add(const Vector3& rhs, Vector3& result) const {
00482       /* result = *this;
00483       result += rhs; */
00484       // instead of the above, do this
00485       result.at(0) = this->at(0) + rhs.at(0);
00486       result.at(1) = this->at(1) + rhs.at(1);
00487       result.at(2) = this->at(2) + rhs.at(2);
00488     }
00489 
00490     Vector3 operator-(const Vector3& rhs) const {
00491       //static TLS Vector3 result;
00492       STATIC_THREAD_LOCAL(Vector3, p_result);
00493       /* result = *this;
00494       result -= rhs;  */
00495       // instead of the above, do this
00496       p_result->at(0) = this->at(0) - rhs.at(0);
00497       p_result->at(1) = this->at(1) - rhs.at(1);
00498       p_result->at(2) = this->at(2) - rhs.at(2);
00499 
00500       return *p_result;
00501     }
00502 
00509     void Subtract(const Vector3& rhs, Vector3& result) const {
00510       /* result = *this;
00511       result -= rhs; */
00512       // instead of the above, do this
00513       result.at(0) = this->at(0) - rhs.at(0);
00514       result.at(1) = this->at(1) - rhs.at(1);
00515       result.at(2) = this->at(2) - rhs.at(2);
00516     }
00517 
00518     Vector3 operator*(const Vector3& rhs) const {
00519       //static TLS Vector3 result;
00520       STATIC_THREAD_LOCAL(Vector3, p_result);
00521       /*result = *this;
00522       result *= rhs;  */
00523       // instead of the above, do this
00524     
00525       p_result->at(0) = this->at(0) * rhs.at(0);
00526       p_result->at(1) = this->at(1) * rhs.at(1);
00527       p_result->at(2) = this->at(2) * rhs.at(2);
00528 
00529       return *p_result;
00530     }
00531 
00538     void Multiply(const Vector3& rhs, Vector3& result) const {
00539       /* result = *this;
00540       result *= rhs;  */
00541       // instead of the above, do this
00542       result.at(0) = this->at(0) * rhs.at(0);
00543       result.at(1) = this->at(1) * rhs.at(1);
00544       result.at(2) = this->at(2) * rhs.at(2);
00545     }
00546 
00547     Vector3 operator*(double rhs) const {
00548       //static TLS Vector3 result;
00549       STATIC_THREAD_LOCAL(Vector3, p_result);
00550     /*    result = *this;
00551       result *= rhs; */
00552       // instead of the above, do this
00553       p_result->at(0) = this->at(0) * rhs;
00554       p_result->at(1) = this->at(1) * rhs;
00555       p_result->at(2) = this->at(2) * rhs;
00556       return *p_result;
00557     }
00558 
00565     void Multiply(double rhs, Vector3& result) const {
00566       /* result = *this;
00567       result *= rhs; */
00568       // instead of the above, do this
00569       result.at(0) = this->at(0) * rhs;
00570       result.at(1) = this->at(1) * rhs;
00571       result.at(2) = this->at(2) * rhs;
00572     }
00573 
00574     Vector3 operator+(double rhs) const {
00575       //static TLS Vector3 result;
00576       STATIC_THREAD_LOCAL(Vector3, p_result);
00577       /* result = *this;
00578       result += rhs; */
00579       // instead of the above, do this
00580       p_result->at(0) = this->at(0) + rhs;
00581       p_result->at(1) = this->at(1) + rhs;
00582       p_result->at(2) = this->at(2) + rhs;
00583 
00584       return *p_result;
00585     }
00586 
00593     void Add(double rhs, Vector3& result) const {
00594       /*result = *this;
00595       result += rhs; */
00596       // instead of the above, do this
00597       result.at(0) = this->at(0) + rhs;
00598       result.at(1) = this->at(1) + rhs;
00599       result.at(2) = this->at(2) + rhs;
00600     }
00601 
00602     Vector3 operator-(double rhs) const {
00603       //static TLS Vector3 result;
00604       STATIC_THREAD_LOCAL(Vector3, p_result);
00605       /*    result = *this;
00606       result -= rhs;  */
00607       // instead of the above, do this
00608       p_result->at(0) = this->at(0) - rhs;
00609       p_result->at(1) = this->at(1) - rhs;
00610       p_result->at(2) = this->at(2) - rhs;
00611 
00612       return *p_result;
00613     }
00614 
00621     void Subtract(double rhs, Vector3& result) const {
00622       /*    result = *this;
00623       result -= rhs;  */
00624       // instead of the above, do this
00625       result.at(0) = this->at(0) - rhs;
00626       result.at(1) = this->at(1) - rhs;
00627       result.at(2) = this->at(2) - rhs;
00628     }
00629 
00638     Vector3 operator/(double rhs) const {
00639       //static TLS Vector3 result;
00640       STATIC_THREAD_LOCAL(Vector3, p_result);
00641       /*result = *this;
00642       result /= rhs;  */
00643       // instead of the above, do this
00644       if(rhs == 0.0){
00645         DisplayError(divideByZero);
00646         return Vector3();
00647       }
00648       p_result->at(0) = this->at(0)/ rhs;
00649       p_result->at(1) = this->at(1)/rhs;
00650       p_result->at(2) = this->at(2)/rhs;
00651       return *p_result;
00652     }
00653 
00663     bool Divide(double rhs, Vector3& result) const {
00664       /*    result = *this;
00665       result /= rhs;  */
00666       // instead of the above, do this
00667       if(rhs == 0.0){
00668         DisplayError(divideByZero);
00669         return false;
00670       }
00671       result.at(0) = this->at(0)/ rhs;
00672       result.at(1) = this->at(1)/rhs;
00673       result.at(2) = this->at(2)/rhs;
00674       return true;
00675     }
00676 
00677   protected:
00678 
00685     void init(const double* array = 0);
00686   };
00687 
00688 
00689   #include "Vector3.ipp"
00690 
00691   typedef Vector3   Vector3D;
00692 }
00693 #endif  // Vector3_hpp
RRG Homepage OSCAR Overview OSCAR Tutorials Simulations