Polar.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program 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 General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 
00023 #include "Polar.hxx"
00024 #include <iostream>
00025 #include <sstream>
00026 
00027 namespace CLAM
00028 {
00029         // complex '+=' operator
00030         const Polar& Polar::operator += (const Polar& a)
00031         {
00032                 TData r1 = fabs(a.mMag) * CLAM_cos(a.mAng); 
00033                 TData i1 = fabs(a.mMag) * CLAM_sin(a.mAng); 
00034                 TData r2 = fabs(mMag) * CLAM_cos(mAng);
00035                 TData i2 = fabs(mMag) * CLAM_sin(mAng);
00036                 
00037                 TData r3 = r1+r2;
00038                 TData i3 = i1+i2;
00039                 
00040                 mMag = sqrt (r3*r3 + i3*i3);
00041                 mAng = atan2 (i3,r3); 
00042         
00043                 return *this;
00044         }
00045 
00046         //  complex '-=' operator 
00047         const Polar& Polar::operator -= (const Polar& a)
00048         {
00049                 TData r1 = fabs(a.mMag) * CLAM_cos(a.mAng); 
00050                 TData i1 = fabs(a.mMag) * CLAM_sin(a.mAng); 
00051                 TData r2 = fabs(mMag) * CLAM_cos(mAng);
00052                 TData i2 = fabs(mMag) * CLAM_sin(mAng);
00053                 
00054                 TData r3 = r2-r1;
00055                 TData i3 = i2-i1;
00056                 
00057                 mMag = CLAM_sqrt (r3*r3 + i3*i3);
00058                 mAng = CLAM_atan2 (i3,r3); 
00059         
00060                 return *this;
00061         }
00062 
00063         // polar '+' operator 
00064         Polar Polar::operator + (const Polar& b) const
00065         {
00066                 TData r1 = fabs(mMag) * CLAM_cos(mAng); 
00067                 TData i1 = fabs(mMag) * CLAM_sin(mAng); 
00068                 TData r2 = fabs(b.mMag) * CLAM_cos(b.mAng);
00069                 TData i2 = fabs(b.mMag) * CLAM_sin(b.mAng);
00070                 
00071                 TData r3 = r1+r2;
00072                 TData i3 = i1+i2;
00073                 
00074                 Polar ret(CLAM_sqrt (r3*r3 + i3*i3),CLAM_atan2 (i3,r3)); 
00075                 return ret;
00076         }
00077 
00078          //  polar '-' operator 
00079         Polar Polar::operator - (const Polar& b) const
00080         {
00081                 TData r1 = fabs(mMag) * CLAM_cos(mAng); 
00082                 TData i1 = fabs(mMag) * CLAM_sin(mAng); 
00083                 TData r2 = fabs(b.mMag) * CLAM_cos(b.mAng);
00084                 TData i2 = fabs(b.mMag) * CLAM_sin(b.mAng);
00085                 
00086                 TData r3 = r1-r2;
00087                 TData i3 = i1-i2;
00088                 
00089                 Polar ret(CLAM_sqrt (r3*r3 + i3*i3),CLAM_atan2 (i3,r3)); 
00090                 return ret;
00091         }
00092 
00093         std::istream& operator >> (std::istream & is,
00094                         Polar & a)
00095         {
00096                 if (is.flags() & std::ios::skipws) {
00097                         char c = '\0';
00098                         do
00099                                 is.get(c);
00100                         while (is && isspace(c));
00101                         if (is) is.putback(c);
00102                 }
00103                 char c = '\0';
00104                 is >> c;
00105                 if (c!='{') {
00106                         if (is) is.putback(c);
00107 //                      std::cerr << "A polar starting with '" << c << "'" << std::endl;
00108                         return is;
00109                 }
00110                 TData m;
00111                 TData p; 
00112                 if (!(is >> m)) return is;
00113                 if (!(is >> p)) return is;
00114                 if (is.flags() & std::ios::skipws) {
00115                         char c = '\0';
00116                         do
00117                                 is.get(c);
00118                         while (is && isspace(c));
00119                         if (is) is.putback(c);
00120                 }
00121                 if (!is.get(c) || c!='}') return is;
00122 
00123                 a.SetMag(m);
00124                 a.SetAng(p);
00125                 return is;
00126         }
00127 
00128         std::ostream& operator << (std::ostream& myStream, const Polar& a)
00129         {
00130                 return myStream 
00131                         << "{"
00132                         << a.Mag()
00133                         << " "
00134                         << a.Ang()
00135                         << "}";
00136         }
00137 
00138 } // namespace CLAM
00139 
00140 
Generated by  doxygen 1.6.3