casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ComplexWrapper.h
Go to the documentation of this file.
00001 /*
00002  * ALMA - Atacama Large Millimeter Array
00003  * (c) European Southern Observatory, 2002
00004  * (c) Associated Universities Inc., 2002
00005  * Copyright by ESO (in the framework of the ALMA collaboration),
00006  * Copyright by AUI (in the framework of the ALMA collaboration),
00007  * All rights reserved.
00008  * 
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  * 
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY, without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00022  * MA 02111-1307  USA
00023  *
00024  * File Complex.h
00025  */
00026 
00027 #ifndef Complex_CLASS
00028 #define Complex_CLASS
00029 
00030 #include <vector>
00031 #include <complex>
00032 #include <iostream>
00033 #include <string>
00034 using namespace std;
00035 using std::complex;
00036 
00037 #ifndef WITHOUT_ACS
00038 #include <asdmIDLTypesC.h>
00039 using asdmIDLTypes::IDLComplex;
00040 #endif
00041 
00042 #include <DoubleWrapper.h>
00043 #include <StringTokenizer.h>
00044 #include <NumberFormatException.h>
00045 using asdm::Double;
00046 using asdm::StringTokenizer;
00047 using asdm::NumberFormatException;
00048 
00049 #include "EndianStream.h"
00050 using asdm::EndianOSStream;
00051 using asdm::EndianIStream;
00052 
00053 namespace asdm {
00054 
00064 class Complex : public std::complex<double> {
00065 
00066 public:
00067         static Complex fromString(const string&) throw(NumberFormatException);
00068         static string toString(const Complex&);
00069         static Complex getComplex(StringTokenizer &t) throw(NumberFormatException);
00070 
00071         Complex();                                              // default constructor
00072         Complex(const Complex &);                                               // X const X& constructor
00073         Complex(const string &s);
00074 #ifndef WITHOUT_ACS
00075         Complex(const IDLComplex &);
00076 #endif
00077         Complex(double re, double im);
00078 
00079         double getReal() const;
00080         double getImg() const;
00081         void setReal(double re);
00082         void setImg(double im);
00083 
00084         bool isZero() const;
00085         bool equals(const Complex &) const;
00086 
00087         string toString() const;
00088 #ifndef WITHOUT_ACS
00089         IDLComplex toIDLComplex() const;
00090 #endif
00091 
00095         void toBin(EndianOSStream& eoss);
00096 
00102         static void toBin(const vector<Complex>& cmplx,  EndianOSStream& eoss);
00103         
00109         static void toBin(const vector<vector<Complex> >& cmplx,  EndianOSStream& eoss);
00110         
00116         static void toBin(const vector<vector<vector<Complex> > >& cmplx,  EndianOSStream& eoss);
00117 
00124         static Complex fromBin(EndianIStream& eis);
00125         
00132          static vector<Complex> from1DBin(EndianIStream & eis);
00133          
00140          static vector<vector<Complex> > from2DBin(EndianIStream & eis);
00141          
00148          static vector<vector<vector<Complex> > > from3DBin(EndianIStream & eis);        
00149 
00150 };
00151 
00152 // Complex constructors
00153 inline Complex::Complex() : std::complex<double>(0.0,0.0) {
00154 }
00155 
00156 inline Complex::Complex(const Complex &t) : std::complex<double>(t.real(),t.imag()) {
00157 }
00158 
00159 inline Complex::Complex(const string &s) : std::complex<double>(Complex::fromString(s)) {
00160 }
00161 
00162 #ifndef WITHOUT_ACS
00163 inline Complex::Complex(const IDLComplex &l) : std::complex<double>(l.re,l.im) {
00164 }
00165 #endif
00166 
00167 inline Complex::Complex(double r, double i) : std::complex<double>(r,i) {
00168 }
00169 
00170 inline double Complex::getReal() const {
00171         return real();
00172 }
00173 
00174 inline double Complex::getImg() const {
00175         return imag();
00176 }
00177 
00178 inline void Complex::setReal(double re) {
00179         *this = Complex(re,imag());
00180 }
00181 
00182 inline void Complex::setImg(double im) {
00183         *this = Complex(real(),im);
00184 }
00185 
00186 inline bool Complex::isZero() const {
00187         return real() == 0.0 && imag() == 0.0;
00188 }
00189 
00190 inline bool Complex::equals(const Complex &x) const {
00191         return real() == x.real() && imag() == x.imag();
00192 }
00193 
00194 #ifndef WITHOUT_ACS
00195 inline IDLComplex Complex::toIDLComplex() const {
00196         IDLComplex x;
00197         x.re = getReal();
00198         x.im = getImg();
00199         return x;
00200 }
00201 #endif
00202 
00203 inline string Complex::toString() const {
00204         return Double::toString(getReal()) + " " + Double::toString(getImg());
00205 }
00206 
00207 } // End namespace asdm
00208 
00209 #endif /* Complex_CLASS */