Vector3D.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2005 Imetric 3D GmbH                                    *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #include "Vector3D.h"
00026 
00027 using namespace Base;
00028 
00029 template <class _Precision>
00030 Vector3<_Precision>::Vector3 (_Precision fx, _Precision fy, _Precision fz)
00031   : x(fx),
00032     y(fy),
00033     z(fz)
00034 {
00035 }
00036 
00037 template <class _Precision>
00038 Vector3<_Precision>::Vector3 (const Vector3<_Precision>& rcVct)
00039   : x(rcVct.x),
00040     y(rcVct.y),
00041     z(rcVct.z)
00042 {
00043 }
00044 
00045 template <class _Precision>
00046 _Precision& Vector3<_Precision>::operator [] (unsigned short usIndex)
00047 {
00048     switch (usIndex)
00049     {
00050         case 0: return x;
00051         case 1: return y;
00052         case 2: return z;
00053     }
00054     return x;
00055 }
00056 
00057 template <class _Precision>
00058 const _Precision& Vector3<_Precision>::operator [] (unsigned short usIndex) const
00059 {
00060     switch (usIndex)
00061     {
00062         case 0: return x;
00063         case 1: return y;
00064         case 2: return z;
00065     }
00066     return x;
00067 }
00068 
00069 template <class _Precision>
00070 Vector3 <_Precision>Vector3<_Precision>::operator +  (const Vector3<_Precision>& rcVct) const
00071 {
00072     Vector3<_Precision> cVctRes;
00073     cVctRes.x = x + rcVct.x;
00074     cVctRes.y = y + rcVct.y;
00075     cVctRes.z = z + rcVct.z;
00076     return cVctRes;
00077 }
00078 
00079 template <class _Precision>
00080 Vector3<_Precision> Vector3<_Precision>::operator &  (const Vector3<_Precision>& rcVct) const
00081 {
00082     Vector3<_Precision> cVctRes;
00083     cVctRes.x = x * (_Precision) fabs(rcVct.x);
00084     cVctRes.y = y * (_Precision) fabs(rcVct.y);
00085     cVctRes.z = z * (_Precision) fabs(rcVct.z);
00086     return cVctRes;
00087 }
00088 
00089 template <class _Precision>
00090 Vector3<_Precision> Vector3<_Precision>::operator -  (const Vector3<_Precision>& rcVct) const
00091 {
00092     Vector3<_Precision> cVctRes;
00093     cVctRes.x = x - rcVct.x;
00094     cVctRes.y = y - rcVct.y;
00095     cVctRes.z = z - rcVct.z;
00096     return cVctRes;
00097 }
00098  
00099 template <class _Precision>
00100 Vector3<_Precision> Vector3<_Precision>::operator - (void) const
00101 {
00102     return Vector3(-x, -y, -z);
00103 }
00104 
00105 template <class _Precision>
00106 Vector3<_Precision>& Vector3<_Precision>::operator += (const Vector3<_Precision>& rcVct)
00107 {
00108     x += rcVct.x;
00109     y += rcVct.y;
00110     z += rcVct.z;
00111     return *this;
00112 }
00113 
00114 template <class _Precision>
00115 Vector3<_Precision>& Vector3<_Precision>::operator -= (const Vector3<_Precision>& rcVct)
00116 {
00117     x -= rcVct.x;
00118     y -= rcVct.y;
00119     z -= rcVct.z;                    
00120     return *this;
00121 }
00122 
00123 template <class _Precision>
00124 Vector3<_Precision>& Vector3<_Precision>::operator *= (_Precision fScale)
00125 {
00126     x *= fScale;
00127     y *= fScale;
00128     z *= fScale;
00129     return *this;
00130 }
00131 
00132 template <class _Precision>
00133 Vector3<_Precision>& Vector3<_Precision>::operator /= (_Precision fDiv) 
00134 {
00135     x /= fDiv;
00136     y /= fDiv;
00137     z /= fDiv;
00138     return *this;
00139 }
00140 
00141 template <class _Precision>
00142 Vector3<_Precision> Vector3<_Precision>::operator * (_Precision fScale) const
00143 {
00144     return Vector3<_Precision>(this->x*fScale,this->y*fScale,this->z*fScale);
00145 }
00146 
00147 template <class _Precision>
00148 Vector3<_Precision> Vector3<_Precision>::operator / (_Precision fDiv) const
00149 {
00150     return Vector3<_Precision>(this->x/fDiv,this->y/fDiv,this->z/fDiv);
00151 }
00152 
00153 template <class _Precision>
00154 Vector3<_Precision>& Vector3<_Precision>::operator =  (const Vector3<_Precision>& rcVct)
00155 {
00156     x = rcVct.x;
00157     y = rcVct.y;
00158     z = rcVct.z;
00159     return *this;
00160 }
00161 
00162 template <class _Precision>
00163 _Precision Vector3<_Precision>::operator *  (const Vector3<_Precision>& rcVct) const
00164 {
00165     return (x * rcVct.x) + (y * rcVct.y) + (z * rcVct.z);
00166 }
00167 
00168 template <class _Precision>
00169 Vector3<_Precision> Vector3<_Precision>::operator %  (const Vector3<_Precision>& rcVct) const
00170 {
00171     Vector3<_Precision> cVctRes;
00172     cVctRes.x = (y * rcVct.z) - (z * rcVct.y);
00173     cVctRes.y = (z * rcVct.x) - (x * rcVct.z);
00174     cVctRes.z = (x * rcVct.y) - (y * rcVct.x);
00175     return cVctRes;
00176 }
00177 
00178 template <class _Precision>
00179 bool Vector3<_Precision>::operator != (const Vector3<_Precision>& rcVct) const
00180 { 
00181     return !((*this) == rcVct);
00182 }
00183 
00184 template <class _Precision>
00185 bool Vector3<_Precision>::operator == (const Vector3<_Precision>& rcVct) const
00186 {
00187     return (fabs (x - rcVct.x) <= traits_type::epsilon()) &&
00188            (fabs (y - rcVct.y) <= traits_type::epsilon()) &&
00189            (fabs (z - rcVct.z) <= traits_type::epsilon());
00190 }  
00191 
00192 template <class _Precision>
00193 Vector3<_Precision>& Vector3<_Precision>::ProjToPlane (const Vector3<_Precision> &rclBase, 
00194                                                        const Vector3<_Precision> &rclNorm)
00195 {
00196     Vector3<_Precision> clTemp(rclNorm);
00197     *this = *this - (clTemp *=  ((*this - rclBase) * clTemp));
00198     return *this;
00199 }
00200 
00201 template <class _Precision>
00202 _Precision Vector3<_Precision>::DistanceToPlane (const Vector3<_Precision> &rclBase, 
00203                                                  const Vector3<_Precision> &rclNorm) const
00204 {
00205     return (*this - rclBase) * rclNorm;
00206 }
00207 
00208 template <class _Precision>
00209 _Precision Vector3<_Precision>::Length (void) const
00210 {
00211     return (_Precision)sqrt ((x * x) + (y * y) + (z * z));
00212 }
00213 
00214 template <class _Precision>
00215 _Precision Vector3<_Precision>::DistanceToLine (const Vector3<_Precision> &rclBase, 
00216                                                 const Vector3<_Precision> &rclDirect) const
00217 {
00218     return (_Precision) fabs((rclDirect % Vector3(*this - rclBase)).Length() / rclDirect.Length());
00219 }
00220 
00221 template <class _Precision>
00222 Vector3<_Precision>& Vector3<_Precision>::ProjToLine (const Vector3<_Precision> &rclPoint,
00223                                                       const Vector3<_Precision> &rclLine)
00224 {
00225     return (*this = ((((rclPoint * rclLine) / rclLine.Sqr()) * rclLine) - rclPoint));
00226 }
00227 
00228 template <class _Precision>
00229 Vector3<_Precision> Vector3<_Precision>::Perpendicular(const Vector3<_Precision> &rclBase,
00230                                                        const Vector3<_Precision> &rclDir) const
00231 {
00232     _Precision t = ((*this - rclBase) * rclDir) / (rclDir * rclDir);
00233     return rclBase + t * rclDir;
00234 }
00235 
00236 template <class _Precision>
00237 _Precision Vector3<_Precision>::Sqr (void) const
00238 {
00239     return (_Precision) ((x * x) + (y * y) + (z * z));
00240 }
00241 
00242 template <class _Precision>
00243 void Vector3<_Precision>::Set (_Precision fX, _Precision fY, _Precision fZ)
00244 {
00245     x = fX;
00246     y = fY;
00247     z = fZ;
00248 }
00249 
00250 template <class _Precision>
00251 void Vector3<_Precision>::ScaleX (_Precision f)
00252 {
00253     x *= f;
00254 }
00255 
00256 template <class _Precision>
00257 void Vector3<_Precision>::ScaleY (_Precision f)
00258 {
00259     y *= f;
00260 }
00261 
00262 template <class _Precision>
00263 void Vector3<_Precision>::ScaleZ (_Precision f)
00264 {
00265     z *= f;
00266 }
00267 
00268 template <class _Precision>
00269 void Vector3<_Precision>::Scale (_Precision fX, _Precision fY, _Precision fZ)
00270 {
00271     x *= fX;
00272     y *= fY;
00273     z *= fZ;
00274 }
00275 
00276 template <class _Precision>
00277 void Vector3<_Precision>::MoveX (_Precision f)
00278 {
00279     x += f;
00280 }
00281 
00282 template <class _Precision>
00283 void Vector3<_Precision>::MoveY (_Precision f)
00284 {
00285     y += f;
00286 }
00287 
00288 template <class _Precision>
00289 void Vector3<_Precision>::MoveZ (_Precision f)
00290 {
00291     z += f;
00292 }
00293 
00294 template <class _Precision>
00295 void Vector3<_Precision>::Move (_Precision fX, _Precision fY, _Precision fZ)
00296 {
00297     x += fX;
00298     y += fY;
00299     z += fZ;
00300 }
00301 
00302 template <class _Precision>
00303 void Vector3<_Precision>::RotateX (_Precision f)
00304 {
00305     Vector3 cPt (*this);
00306     _Precision fsin, fcos;
00307 
00308     fsin = (_Precision)sin (f);
00309     fcos = (_Precision)cos (f);
00310     y = (cPt.y * fcos) - (cPt.z * fsin);
00311     z = (cPt.y * fsin) + (cPt.z * fcos);
00312 }
00313 
00314 template <class _Precision>
00315 void Vector3<_Precision>::RotateY (_Precision f)
00316 {
00317     Vector3 cPt (*this);
00318     _Precision fsin, fcos;
00319 
00320     fsin = (_Precision)sin (f);
00321     fcos = (_Precision)cos (f);
00322     x = (cPt.z * fsin) + (cPt.x * fcos);
00323     z = (cPt.z * fcos) - (cPt.x * fsin);
00324 }
00325 
00326 template <class _Precision>
00327 void Vector3<_Precision>::RotateZ (_Precision f)
00328 {
00329     Vector3 cPt (*this);
00330     _Precision fsin, fcos;
00331 
00332     fsin = (_Precision)sin (f);
00333     fcos = (_Precision)cos (f);
00334     x = (cPt.x * fcos) - (cPt.y * fsin);
00335     y = (cPt.x * fsin) + (cPt.y * fcos);
00336 }
00337 
00338 template <class _Precision>
00339 Vector3<_Precision> & Vector3<_Precision>::Normalize (void)
00340 {
00341     _Precision fLen = Length ();
00342     if (fLen != 0.0f) {
00343         x /= fLen;
00344         y /= fLen;
00345         z /= fLen;
00346     }
00347     return *this;
00348 }
00349 
00350 template <class _Precision>
00351 _Precision Vector3<_Precision>::GetAngle (const Vector3 &rcVect) const
00352 {
00353     _Precision divid, fNum;
00354 
00355     divid = Length() * ((Vector3<_Precision>&)rcVect).Length();
00356  
00357     if ((divid < -1e-10f) || (divid > 1e-10f)) {
00358         fNum = (*this * rcVect) / divid;
00359         if (fNum < -1)
00360             return traits_type::pi();
00361         else if (fNum > 1)
00362             return 0.0F;
00363         else
00364             return _Precision(acos(fNum));
00365     }
00366     else
00367         return traits_type::maximum(); // division by zero
00368 }
00369 
00370 template <class _Precision>
00371 void Vector3<_Precision>::TransformToCoordinateSystem (const Vector3 &rclBase,
00372                                                        const Vector3 &rclDirX,
00373                                                        const Vector3 &rclDirY)
00374 {
00375     Vector3  clVectX, clVectY, clVectZ, clVectOld;
00376 
00377     clVectX = rclDirX;
00378     clVectY = rclDirY;
00379     clVectZ = rclDirX % rclDirY;
00380     clVectX.Normalize();
00381     clVectY.Normalize();
00382     clVectZ.Normalize();
00383 
00384     clVectOld = *this - rclBase;
00385 
00386     x = clVectX * clVectOld;
00387     y = clVectY * clVectOld;
00388     z = clVectZ * clVectOld;
00389 }
00390 
00391 // explicit template instantiation
00392 template class BaseExport Vector3<float>;
00393 template class BaseExport Vector3<double>;

Generated on Wed Nov 23 19:00:56 2011 for FreeCAD by  doxygen 1.6.1