basic/geometry/geo_rangesensor/MgcVector3.cpp

Go to the documentation of this file.
00001 
00002 // Magic Software, Inc.
00003 // http://www.magic-software.com
00004 // Copyright (c) 2000, All Rights Reserved
00005 //
00006 // Source code from Magic Software is supplied under the terms of a license
00007 // agreement and may not be copied or disclosed except in accordance with the
00008 // terms of that agreement.  The various license agreements may be found at
00009 // the Magic Software web site.  This file is subject to the license
00010 //
00011 // FREE SOURCE CODE
00012 // http://www.magic-software.com/License.html/free.pdf
00013 
00014 #include "MgcVector3.h"
00015 
00016 const MgcVector3 MgcVector3::ZERO(0,0,0);
00017 const MgcVector3 MgcVector3::UNIT_X(1,0,0);
00018 const MgcVector3 MgcVector3::UNIT_Y(0,1,0);
00019 const MgcVector3 MgcVector3::UNIT_Z(0,0,1);
00020 
00021 //----------------------------------------------------------------------------
00022 MgcVector3::MgcVector3 ()
00023 {
00024     // For efficiency in construction of large arrays of vectors, the
00025     // default constructor does not initialize the vector.
00026 }
00027 //----------------------------------------------------------------------------
00028 MgcVector3::MgcVector3 (MgcReal fX, MgcReal fY, MgcReal fZ)
00029 {
00030     x = fX;
00031     y = fY;
00032     z = fZ;
00033 }
00034 //----------------------------------------------------------------------------
00035 MgcVector3::MgcVector3 (MgcReal afCoordinate[3])
00036 {
00037     x = afCoordinate[0];
00038     y = afCoordinate[1];
00039     z = afCoordinate[2];
00040 }
00041 //----------------------------------------------------------------------------
00042 MgcVector3::MgcVector3 (const MgcVector3& rkVector)
00043 {
00044     x = rkVector.x;
00045     y = rkVector.y;
00046     z = rkVector.z;
00047 }
00048 //----------------------------------------------------------------------------
00049 MgcReal& MgcVector3::operator[] (int i) const
00050 {
00051     // assert:  0 <= i < 2; x, y, and z are packed into 3*sizeof(MgcReal)
00052     //          bytes
00053     return (MgcReal&) *(&x + i);
00054 }
00055 //----------------------------------------------------------------------------
00056 MgcVector3::operator MgcReal* ()
00057 {
00058     return &x;
00059 }
00060 //----------------------------------------------------------------------------
00061 MgcVector3& MgcVector3::operator= (const MgcVector3& rkVector)
00062 {
00063     x = rkVector.x;
00064     y = rkVector.y;
00065     z = rkVector.z;
00066     return *this;
00067 }
00068 //----------------------------------------------------------------------------
00069 bool MgcVector3::operator== (const MgcVector3& rkVector) const
00070 {
00071     return ( x == rkVector.x && y == rkVector.y && z == rkVector.z );
00072 }
00073 //----------------------------------------------------------------------------
00074 bool MgcVector3::operator!= (const MgcVector3& rkVector) const
00075 {
00076     return ( x != rkVector.x || y != rkVector.y || z != rkVector.z );
00077 }
00078 //----------------------------------------------------------------------------
00079 MgcVector3 MgcVector3::operator+ (const MgcVector3& rkVector) const
00080 {
00081     MgcVector3 kSum;
00082     kSum.x = x + rkVector.x;
00083     kSum.y = y + rkVector.y;
00084     kSum.z = z + rkVector.z;
00085     return kSum;
00086 }
00087 //----------------------------------------------------------------------------
00088 MgcVector3 MgcVector3::operator- (const MgcVector3& rkVector) const
00089 {
00090     MgcVector3 kDiff;
00091     kDiff.x = x - rkVector.x;
00092     kDiff.y = y - rkVector.y;
00093     kDiff.z = z - rkVector.z;
00094     return kDiff;
00095 }
00096 //----------------------------------------------------------------------------
00097 MgcVector3 MgcVector3::operator* (MgcReal fScalar) const
00098 {
00099     MgcVector3 kProd;
00100     kProd.x = fScalar*x;
00101     kProd.y = fScalar*y;
00102     kProd.z = fScalar*z;
00103     return kProd;
00104 }
00105 //----------------------------------------------------------------------------
00106 MgcVector3 MgcVector3::operator- () const
00107 {
00108     MgcVector3 kNeg;
00109     kNeg.x = -x;
00110     kNeg.y = -y;
00111     kNeg.z = -z;
00112     return kNeg;
00113 }
00114 //----------------------------------------------------------------------------
00115 MgcVector3 operator* (MgcReal fScalar, const MgcVector3& rkVector)
00116 {
00117     MgcVector3 kProd;
00118     kProd.x = fScalar*rkVector.x;
00119     kProd.y = fScalar*rkVector.y;
00120     kProd.z = fScalar*rkVector.z;
00121     return kProd;
00122 }
00123 //----------------------------------------------------------------------------
00124 MgcVector3& MgcVector3::operator+= (const MgcVector3& rkVector)
00125 {
00126     x += rkVector.x;
00127     y += rkVector.y;
00128     z += rkVector.z;
00129     return *this;
00130 }
00131 //----------------------------------------------------------------------------
00132 MgcVector3& MgcVector3::operator-= (const MgcVector3& rkVector)
00133 {
00134     x -= rkVector.x;
00135     y -= rkVector.y;
00136     z -= rkVector.z;
00137     return *this;
00138 }
00139 //----------------------------------------------------------------------------
00140 MgcVector3& MgcVector3::operator*= (MgcReal fScalar)
00141 {
00142     x *= fScalar;
00143     y *= fScalar;
00144     z *= fScalar;
00145     return *this;
00146 }
00147 //----------------------------------------------------------------------------
00148 MgcReal MgcVector3::SquaredLength () const
00149 {
00150     return x*x + y*y + z*z;
00151 }
00152 //----------------------------------------------------------------------------
00153 MgcReal MgcVector3::Dot (const MgcVector3& rkVector) const
00154 {
00155     return x*rkVector.x + y*rkVector.y + z*rkVector.z;
00156 }
00157 //----------------------------------------------------------------------------
00158 MgcVector3 MgcVector3::operator/ (MgcReal fScalar) const
00159 {
00160     MgcVector3 kQuot;
00161 
00162     if ( fScalar != 0.0 )
00163     {
00164         MgcReal fInvScalar = 1.0/fScalar;
00165         kQuot.x = fInvScalar*x;
00166         kQuot.y = fInvScalar*y;
00167         kQuot.z = fInvScalar*z;
00168         return kQuot;
00169     }
00170     else
00171     {
00172         return MgcVector3(MgcMath::INFINITY,MgcMath::INFINITY,
00173             MgcMath::INFINITY);
00174     }
00175 }
00176 //----------------------------------------------------------------------------
00177 MgcVector3& MgcVector3::operator/= (MgcReal fScalar)
00178 {
00179     if ( fScalar != 0.0 )
00180     {
00181         MgcReal fInvScalar = 1.0/fScalar;
00182         x *= fInvScalar;
00183         y *= fInvScalar;
00184         z *= fInvScalar;
00185     }
00186     else
00187     {
00188         x = MgcMath::INFINITY;
00189         y = MgcMath::INFINITY;
00190         z = MgcMath::INFINITY;
00191     }
00192 
00193     return *this;
00194 }
00195 //----------------------------------------------------------------------------
00196 MgcReal MgcVector3::Length () const
00197 {
00198     return MgcMath::Sqrt(x*x + y*y + z*z);
00199 }
00200 //----------------------------------------------------------------------------
00201 MgcReal MgcVector3::Unitize (MgcReal fTolerance)
00202 {
00203     MgcReal fLength = Length();
00204 
00205     if ( fLength > fTolerance )
00206     {
00207         MgcReal fInvLength = 1.0/fLength;
00208         x *= fInvLength;
00209         y *= fInvLength;
00210         z *= fInvLength;
00211     }
00212     else
00213     {
00214         fLength = 0.0;
00215     }
00216 
00217     return fLength;
00218 }
00219 //----------------------------------------------------------------------------
00220 MgcVector3 MgcVector3::Cross (const MgcVector3& rkVector) const
00221 {
00222     MgcVector3 kCross;
00223 
00224     kCross.x = y*rkVector.z - z*rkVector.y;
00225     kCross.y = z*rkVector.x - x*rkVector.z;
00226     kCross.z = x*rkVector.y - y*rkVector.x;
00227 
00228     return kCross;
00229 }
00230 //----------------------------------------------------------------------------
00231 MgcVector3 MgcVector3::UnitCross (const MgcVector3& rkVector) const
00232 {
00233     MgcVector3 kCross;
00234 
00235     kCross.x = y*rkVector.z - z*rkVector.y;
00236     kCross.y = z*rkVector.x - x*rkVector.z;
00237     kCross.z = x*rkVector.y - y*rkVector.x;
00238     kCross.Unitize();
00239 
00240     return kCross;
00241 }
00242 //----------------------------------------------------------------------------
00243 void MgcVector3::Orthonormalize (MgcVector3 akVector[3])
00244 {
00245     // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
00246     // orthonormalization produces vectors u0, u1, and u2 as follows,
00247     //
00248     //   u0 = v0/|v0|
00249     //   u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
00250     //   u2 = (v2-(u0*v2)u0-(u1*v2)u1)/|v2-(u0*v2)u0-(u1*v2)u1|
00251     //
00252     // where |A| indicates length of vector A and A*B indicates dot
00253     // product of vectors A and B.
00254 
00255     // compute u0
00256     akVector[0].Unitize();
00257 
00258     // compute u1
00259     MgcReal fDot0 = akVector[0].Dot(akVector[1]); 
00260     akVector[1] -= fDot0*akVector[0];
00261     akVector[1].Unitize();
00262 
00263     // compute u2
00264     MgcReal fDot1 = akVector[1].Dot(akVector[2]);
00265     fDot0 = akVector[0].Dot(akVector[2]);
00266     akVector[2] -= fDot0*akVector[0] + fDot1*akVector[1];
00267     akVector[2].Unitize();
00268 }
00269 //----------------------------------------------------------------------------
00270 void MgcVector3::GenerateOrthonormalBasis (MgcVector3& rkU, MgcVector3& rkV,
00271     MgcVector3& rkW, bool bUnitLengthW)
00272 {
00273     if ( !bUnitLengthW )
00274         rkW.Unitize();
00275 
00276     if ( MgcMath::Abs(rkW.x) >= MgcMath::Abs(rkW.y)
00277     &&   MgcMath::Abs(rkW.x) >= MgcMath::Abs(rkW.z) )
00278     {
00279         rkU.x = -rkW.y;
00280         rkU.y = +rkW.x;
00281         rkU.z = 0.0;
00282     }
00283     else
00284     {
00285         rkU.x = 0.0;
00286         rkU.y = +rkW.z;
00287         rkU.z = -rkW.y;
00288     }
00289 
00290     rkU.Unitize();
00291     rkV = rkW.Cross(rkU);
00292 }
00293 //----------------------------------------------------------------------------

Generated on Sat Apr 1 21:30:31 2006 for Motion Planning Kernel by  doxygen 1.4.6-NO