CLAM-Development  1.4.0
PolarTmplDec.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 
23 #ifndef _PolarTmplDec_
24 #define _PolarTmplDec_
25 
26 #include <iosfwd>
27 #include "CLAM_Math.hxx"
28 
29 namespace CLAM
30 {
31 
32  template <class T>
33  class PolarTmpl
34  {
35  private:
36  T mMag, mAng;
37 
38  public:
39  PolarTmpl(T mag = 0.0,T ang = 0.0)// constructor
40  {
41  mMag = mag;
42  mAng = ang;
43  };
44 
45  const T Mag(void) const {return mMag;}; // accessor
46  const T Ang(void) const {return mAng;}; // accessor
47 
48  void SetMag(const T& mag) { mMag = mag;}; // accesor
49  void SetAng(const T& ang) { mAng = ang;}; // accesor
50 
51  // returns real part
52  const T Real (void) const
53  {
54  return fabs(mMag) * CLAM_cos(mAng);
55  }
56 
57  // returns imaginary part
58  const T Imag (void) const
59  {
60  return fabs(mMag) * CLAM_sin(mAng);
61  }
62 
63  // friend function to handle cartesian coordinates
64  friend PolarTmpl<T> ToComplex(const T& real, const T& imag)
65  {
66  return PolarTmpl<T> (CLAM_sqrt (real*real + imag*imag),CLAM_atan2 (imag,real));
67  };
68 
69  // ------ member operators ... ------
70  // complex '=' operator (float)
71  const PolarTmpl<T>& operator = (const float mag)
72  {
73  mMag = mag;
74  mAng = 0;
75  return *this;
76  }
77 
78  // complex '=' operator
80  {
81  mMag = a.mMag;
82  mAng = a.mAng;
83  return *this;
84 
85  }
86 
87  // complex '+=' operator
88  const PolarTmpl<T>& operator += (const PolarTmpl<T>& a);
89 
90  // complex '-=' operator
91  const PolarTmpl<T>& operator -= (const PolarTmpl<T>& a);
92 
93  // polar '-' operator
94  PolarTmpl<T> operator - (const PolarTmpl<T>& b) const;
95 
96  // polar '+' operator
97  PolarTmpl<T> operator + (const PolarTmpl<T>& b) const;
98 
99  // complex '*' operator
101  {
102  PolarTmpl<T> ret(mMag * b.mMag,mAng + b.mAng);
103  return ret;
104  }
105 
106  // complex '/' operator
108  {
109  PolarTmpl<T> ret(mMag / b.mMag,mAng - b.mAng);
110  return ret;
111  }
112 
113  // complex '==' operator
114  bool operator == (const PolarTmpl<T>& b) const
115  {
116  if ((mMag == b.mMag)&&(mAng == b.mAng)) return true;
117  else return false;
118  }
119 
120  // polar '!=' operator
121  bool operator != (const PolarTmpl<T>& b) const
122  {
123  if ((mMag == b.mMag)&&(mAng == b.mAng)) return false;
124  else return true;
125  }
126 
127  };
128 
129  template <class T>
130  std::istream& operator >> (std::istream & stream, PolarTmpl<T> & a);
131 
132  template <class T>
133  std::ostream& operator << (std::ostream & stream, const PolarTmpl<T> & a);
134 
135 } // namespace CLAM
136 
137 #endif // _PolarTmplDec_
138