Line data Source code
1 : /* 2 : * ALMA - Atacama Large Millimeter Array 3 : * (c) European Southern Observatory, 2002 4 : * (c) Associated Universities Inc., 2002 5 : * Copyright by ESO (in the framework of the ALMA collaboration), 6 : * Copyright by AUI (in the framework of the ALMA collaboration), 7 : * All rights reserved. 8 : * 9 : * This library is free software; you can redistribute it and/or 10 : * modify it under the terms of the GNU Lesser General Public 11 : * License as published by the Free software Foundation; either 12 : * version 2.1 of the License, or (at your option) any later version. 13 : * 14 : * This library is distributed in the hope that it will be useful, 15 : * but WITHOUT ANY WARRANTY, without even the implied warranty of 16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 : * Lesser General Public License for more details. 18 : * 19 : * You should have received a copy of the GNU Lesser General Public 20 : * License along with this library; if not, write to the Free Software 21 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 22 : * MA 02111-1307 USA 23 : * 24 : * File Complex.h 25 : */ 26 : 27 : #ifndef Complex_CLASS 28 : #define Complex_CLASS 29 : 30 : #include <vector> 31 : #include <complex> 32 : #include <iostream> 33 : #include <string> 34 : 35 : #ifndef WITHOUT_ACS 36 : #include <asdmIDLTypesC.h> 37 : #endif 38 : 39 : #include <alma/ASDM/DoubleWrapper.h> 40 : #include <alma/ASDM/StringTokenizer.h> 41 : #include <alma/ASDM/NumberFormatException.h> 42 : 43 : #include <alma/ASDM/EndianStream.h> 44 : 45 : namespace asdm { 46 : 47 : /** 48 : * The Complex class extends the Complex class in the C++ standard library. 49 : * 50 : * @version 1.00 Jan. 7, 2005 51 : * @author Allen Farris 52 : * @version 1.1 Aug 8, 2006 53 : * @author Michel Caillat 54 : * added toBin/fromBin methods. 55 : */ 56 : class Complex : public std::complex<double> { 57 : 58 : public: 59 : static Complex fromString(const std::string&); 60 : static std::string toString(const Complex&); 61 : static Complex getComplex(StringTokenizer &t); 62 : 63 : Complex(); // default constructor 64 : Complex(const Complex &); // X const X& constructor 65 : Complex(const std::string &s); 66 : #ifndef WITHOUT_ACS 67 : Complex(const asdmIDLTypes::IDLComplex &); 68 : #endif 69 : Complex(double re, double im); 70 : 71 : double getReal() const; 72 : double getImg() const; 73 : void setReal(double re); 74 : void setImg(double im); 75 : 76 : bool isZero() const; 77 : bool equals(const Complex &) const; 78 : 79 : std::string toString() const; 80 : #ifndef WITHOUT_ACS 81 : asdmIDLTypes::IDLComplex toIDLComplex() const; 82 : #endif 83 : 84 : /** 85 : * Write the binary representation of this to a EndianOSStream. 86 : */ 87 : void toBin(EndianOSStream& eoss); 88 : 89 : /** 90 : * Write the binary representation of a vector of Complex to a EndianOSStream. 91 : * @param cmplx the vector of Complex to be written 92 : * @param eoss the EndianOSStream to be written to 93 : */ 94 : static void toBin(const std::vector<Complex>& cmplx, EndianOSStream& eoss); 95 : 96 : /** 97 : * Write the binary representation of a vector of vector of Complex to a EndianOSStream. 98 : * @param cmplx the vector of vector of Complex to be written 99 : * @param eoss the EndianOSStream to be written to 100 : */ 101 : static void toBin(const std::vector<std::vector<Complex> >& cmplx, EndianOSStream& eoss); 102 : 103 : /** 104 : * Write the binary representation of a vector of vector of vector of Complex to a EndianOSStream. 105 : * @param cmplx the vector of vector of vector of Complex to be written 106 : * @param eoss the EndianOSStream to be written to 107 : */ 108 : static void toBin(const std::vector<std::vector<std::vector<Complex> > >& cmplx, EndianOSStream& eoss); 109 : 110 : /** 111 : * Read the binary representation of an Complex from a EndianIStream 112 : * and use the read value to set an Complex. 113 : * @param eis the EndianStream to be read 114 : * @return an Complex 115 : */ 116 : static Complex fromBin(EndianIStream& eis); 117 : 118 : /** 119 : * Read the binary representation of a vector of Complex from an EndianIStream 120 : * and use the read value to set a vector of Complex. 121 : * @param dis the EndianIStream to be read 122 : * @return a vector of Complex 123 : */ 124 : static std::vector<Complex> from1DBin(EndianIStream & eis); 125 : 126 : /** 127 : * Read the binary representation of a vector of vector of Complex from an EndianIStream 128 : * and use the read value to set a vector of vector of Complex. 129 : * @param eiis the EndianIStream to be read 130 : * @return a vector of vector of Complex 131 : */ 132 : static std::vector<std::vector<Complex> > from2DBin(EndianIStream & eis); 133 : 134 : /** 135 : * Read the binary representation of a vector of vector of vector of Complex from an EndianIStream 136 : * and use the read value to set a vector of vector of vector of Complex. 137 : * @param eis the EndianIStream to be read 138 : * @return a vector of vector of vector of Complex 139 : */ 140 : static std::vector<std::vector<std::vector<Complex> > > from3DBin(EndianIStream & eis); 141 : 142 : }; 143 : 144 : // Complex constructors 145 0 : inline Complex::Complex() : std::complex<double>(0.0,0.0) { 146 0 : } 147 : 148 1169708 : inline Complex::Complex(const Complex &t) : std::complex<double>(t.real(),t.imag()) { 149 1169708 : } 150 : 151 0 : inline Complex::Complex(const std::string &s) : std::complex<double>(Complex::fromString(s)) { 152 0 : } 153 : 154 : #ifndef WITHOUT_ACS 155 : inline Complex::Complex(const asdmIDLTypes::IDLComplex &l) : std::complex<double>(l.re,l.im) { 156 : } 157 : #endif 158 : 159 272800 : inline Complex::Complex(double r, double i) : std::complex<double>(r,i) { 160 272800 : } 161 : 162 3584 : inline double Complex::getReal() const { 163 3584 : return real(); 164 : } 165 : 166 3584 : inline double Complex::getImg() const { 167 3584 : return imag(); 168 : } 169 : 170 : inline void Complex::setReal(double re) { 171 : *this = Complex(re,imag()); 172 : } 173 : 174 : inline void Complex::setImg(double im) { 175 : *this = Complex(real(),im); 176 : } 177 : 178 : inline bool Complex::isZero() const { 179 : return real() == 0.0 && imag() == 0.0; 180 : } 181 : 182 : inline bool Complex::equals(const Complex &x) const { 183 : return real() == x.real() && imag() == x.imag(); 184 : } 185 : 186 : #ifndef WITHOUT_ACS 187 : inline asdmIDLTypes::IDLComplex Complex::toIDLComplex() const { 188 : asdmIDLTypes::IDLComplex x; 189 : x.re = getReal(); 190 : x.im = getImg(); 191 : return x; 192 : } 193 : #endif 194 : 195 3584 : inline std::string Complex::toString() const { 196 7168 : return Double::toString(getReal()) + " " + Double::toString(getImg()); 197 : } 198 : 199 : } // End namespace asdm 200 : 201 : #endif /* Complex_CLASS */