casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
AnnotationBase.h
Go to the documentation of this file.
00001 //# ComponentShape.h: Base class for component shapes
00002 //# Copyright (C) 1998,1999,2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: ComponentShape.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $
00027 
00028 #ifndef ANNOTATIONS_ANNOTATIONBASE_H
00029 #define ANNOTATIONS_ANNOTATIONBASE_H
00030 
00031 #include <coordinates/Coordinates/CoordinateSystem.h>
00032 #include <boost/regex.hpp>
00033 
00034 #include <measures/Measures/Stokes.h>
00035 
00036 
00037 namespace casa {
00038 
00039 // <summary>Base class for annotations</summary>
00040 
00041 // <use visibility=export>
00042 
00043 // <reviewed reviewer="" date="yyyy/mm/dd">
00044 // </reviewed>
00045 
00046 // <synopsis>
00047 // Base class for annotations
00048 
00049 // In order to minimize maintainability, many parameters are not
00050 // set in the constructor but can be set by mutator methods.
00051 //
00052 // Input directions will be converted to the reference frame of the
00053 // input coordinate system upon construction if necessary.
00054 // </synopsis>
00055 
00056 class AnnotationBase {
00057 public:
00058 
00059         typedef vector<float> RGB;
00060 
00061         typedef Vector<std::pair<Quantity,Quantity> > Direction;
00062 
00063         enum Type {
00064                 // annotations only
00065                 LINE,
00066                 VECTOR,
00067                 TEXT,
00068                 SYMBOL,
00069                 // regions
00070                 RECT_BOX,
00071                 CENTER_BOX,
00072                 ROTATED_BOX,
00073                 POLYGON,
00074                 POLYLINE,
00075                 CIRCLE,
00076                 ANNULUS,
00077                 ELLIPSE
00078         };
00079 
00080         enum Keyword {
00081                 COORD,
00082                 RANGE,
00083                 FRAME,
00084                 CORR,
00085                 VELTYPE,
00086                 RESTFREQ,
00087                 LINEWIDTH,
00088                 LINESTYLE,
00089                 SYMSIZE,
00090                 SYMTHICK,
00091                 COLOR,
00092                 FONT,
00093                 FONTSIZE,
00094                 FONTSTYLE,
00095                 USETEX,
00096                 LABEL,
00097                 LABELCOLOR,
00098                 LABELPOS,
00099                 LABELOFF,
00100                 UNKNOWN_KEYWORD,
00101                 N_KEYS
00102         };
00103 
00104         enum LineStyle {
00105                 SOLID,
00106                 DASHED,
00107                 DOT_DASHED,
00108                 DOTTED
00109         };
00110 
00111         enum FontStyle {
00112                 NORMAL,
00113                 BOLD,
00114                 ITALIC,
00115                 ITALIC_BOLD
00116         };
00117 
00118     static const RGB BLACK;
00119     static const RGB BLUE;
00120     static const RGB CYAN;
00121     static const RGB GRAY;
00122     static const RGB GREEN;
00123     static const RGB MAGENTA;
00124     static const RGB ORANGE;
00125     static const RGB RED;
00126     static const RGB WHITE;
00127     static const RGB YELLOW;
00128 
00129         static const String DEFAULT_LABEL;
00130         static const RGB DEFAULT_COLOR;
00131         static const LineStyle DEFAULT_LINESTYLE;
00132         static const uInt DEFAULT_LINEWIDTH;
00133         static const uInt DEFAULT_SYMBOLSIZE;
00134         static const uInt DEFAULT_SYMBOLTHICKNESS;
00135         static const String DEFAULT_FONT;
00136         static const uInt DEFAULT_FONTSIZE;
00137         static const FontStyle DEFAULT_FONTSTYLE;
00138         static const Bool DEFAULT_USETEX;
00139         static const RGB DEFAULT_LABELCOLOR;
00140         static const String DEFAULT_LABELPOS;
00141         static const vector<Int> DEFAULT_LABELOFF;
00142 
00143         static const boost::regex rgbHexRegex;
00144 
00145         virtual ~AnnotationBase();
00146 
00147         Type getType() const;
00148 
00149         static LineStyle lineStyleFromString(const String& ls);
00150 
00151         // Given a string, return the corresponding annotation type or throw
00152         // an error if the string does not correspond to an allowed type.
00153         static Type typeFromString(const String& type);
00154 
00155         static String typeToString(const Type type);
00156 
00157         static String keywordToString(const Keyword key);
00158 
00159         static String lineStyleToString(const LineStyle linestyle);
00160 
00161         static FontStyle fontStyleFromString(const String& fs);
00162 
00163         static String fontStyleToString(const FontStyle fs);
00164 
00165         void setLabel(const String& label);
00166 
00167         String getLabel() const;
00168 
00169     // <src>color</src> must either be a recognized color name or
00170     // a valid rgb hex string, else an expection is thrown
00171         void setColor(const String& color);
00172         
00173     // color must have three elements all with values between 0 and 255 inclusive
00174     // or an exception is thrown.
00175     void setColor(const RGB& color);
00176 
00177     // returns the color name if it is recognized or its rgb hex string 
00178         String getColorString() const;
00179 
00180         static String colorToString(const RGB& color);
00181 
00182     // get the color associated with this object
00183     RGB getColor() const;
00184 
00185         void setLineStyle(const LineStyle lineStyle);
00186 
00187         LineStyle getLineStyle() const;
00188 
00189         void setLineWidth(const uInt linewidth);
00190 
00191         uInt getLineWidth() const;
00192 
00193         void setSymbolSize(const uInt symbolsize);
00194 
00195         uInt getSymbolSize() const;
00196 
00197         void setSymbolThickness(const uInt symbolthickness);
00198 
00199         uInt getSymbolThickness() const;
00200 
00201         void setFont(const String& font);
00202 
00203         String getFont() const;
00204 
00205         void setFontSize(const uInt fontsize);
00206 
00207         uInt getFontSize() const;
00208 
00209         void setFontStyle(const FontStyle& fontstyle);
00210 
00211         FontStyle getFontStyle() const;
00212 
00213         void setUseTex(const Bool usetex);
00214 
00215         Bool isUseTex() const;
00216 
00217         // is the object a region?
00218         virtual Bool isRegion() const;
00219 
00220         // is the object only an annotation? Can only be false if the object
00221         // is a region
00222         inline virtual Bool isAnnotationOnly() const { return True; }
00223 
00224         // set "pix" as valid unit. This should be called externally
00225         // before creating quantities which have pixel units.
00226         static void unitInit();
00227 
00228     // <src>color</src> must either be a recognized color name or
00229     // a valid rgb hex string, else an expection is thrown
00230         void setLabelColor(const String& color);
00231 
00232     // color must have three elements all with values between 0 and 255 inclusive
00233     // or an exception is thrown.
00234     void setLabelColor(const RGB& color);
00235 
00236     // returns the color name if it is recognized or its rgb hex string
00237 
00238         String getLabelColorString() const;
00239 
00240     // get the color associated with this object's label
00241     RGB getLabelColor() const;
00242 
00243     // returns one of top, bottom, left, or right.
00244         String getLabelPosition() const;
00245 
00246         // <src>position</src> must have a value in top, bottom, left, or right.
00247         // case is ignored.
00248         void setLabelPosition(const String& position);
00249 
00250         // <src>offset</src> must have two elements
00251         void setLabelOffset(const vector<Int>& offset);
00252 
00253         vector<Int> getLabelOffset() const;
00254 
00255         virtual ostream& print(ostream &os) const = 0;
00256 
00257         // These parameters are included at the global scope. Multiple runs
00258         // on the same object are cumulative; if a key exists in the current
00259         // settings but not in <src>globalKeys</src> that key will still exist
00260         // in the globals after setGlobals has run.
00261         void setGlobals(const Vector<Keyword>& globalKeys);
00262 
00263         // print a set of keyword value pairs
00264         static ostream& print(
00265                 ostream& os, const map<Keyword, String>& params
00266         );
00267 
00268         // print a line style representation
00269         static ostream& print(
00270                 ostream& os, const LineStyle ls
00271         );
00272 
00273         // print a font style representation
00274         static ostream& print(
00275                 ostream& os, const FontStyle fs
00276         );
00277 
00278         // Get a list of the user-friendly color names supported
00279         static std::list<std::string> colorChoices();
00280 
00281         // get the coordinate system associated with this object.
00282         // This is the same coordinates system used to construct the object.
00283 
00284         inline const CoordinateSystem& getCsys() const {
00285                 return _csys;
00286         }
00287 
00288         inline Direction getDirections() const {
00289                 return _convertedDQs;
00290         }
00291 
00292         // get the frequency limits converted to the spectral frame of the coordinate
00293         // system of this object. An empty Vector implies all applicable frequencies
00294         // have been selected.
00295         Vector<MFrequency> getFrequencyLimits() const;
00296 
00297         // Get the stokes for which the selection applies. An empty Vector implies
00298         // all applicable stokes have been selected.
00299         Vector<Stokes::StokesTypes> getStokes() const;
00300 
00301 protected:
00302 
00303         AnnotationBase(
00304                 const Type type, const String& dirRefFrameString,
00305                 const CoordinateSystem& csys, const Quantity& beginFreq,
00306                 const Quantity& endFreq,
00307                 const String& freqRefFrame,
00308                 const String& dopplerString,
00309                 const Quantity& restfreq,
00310                 const Vector<Stokes::StokesTypes>& stokes
00311         );
00312 
00313         // use only if the frame of the input directions is the
00314         // same as the frame of the coordinate system. All frequencies
00315         // are used.
00316         AnnotationBase(
00317                 const Type type, const CoordinateSystem& csys,
00318                 const Vector<Stokes::StokesTypes>& stokes
00319         );
00320 
00321         // the implicitly defined copy constructor is fine
00322         // AnnotationBase(const AnnotationBase& other);
00323 
00324         // assignment operator
00325         AnnotationBase& operator= (const AnnotationBase& other);
00326 
00327         static void _checkMixed(
00328                 const String& origin,
00329                 const Direction& dirs
00330         );
00331 
00332         MDirection _directionFromQuantities(
00333                 const Quantity& q0, const Quantity& q1
00334         );
00335 
00336         void _checkAndConvertDirections(
00337                 const String& origin,
00338                 const Direction& dirs
00339         );
00340 
00341         virtual void _printPairs(ostream& os) const;
00342 
00343         inline const Vector<MDirection>& _getConvertedDirections() const {
00344                 return _convertedDirections;
00345         }
00346 
00347         inline const IPosition& _getDirectionAxes() const {
00348                 return _directionAxes;
00349         }
00350 
00351         // direction to string, precision of 0.1 mas
00352         // ra and dec in sexigesimal format, non-equatorial coords in degrees
00353         String _printDirection(
00354                 const Quantity& longitude, const Quantity& latitude
00355         ) const;
00356 
00357         // convert angle to arcsec, precision 0.1 mas
00358         static String _toArcsec(const Quantity& angle);
00359 
00360         // convert angle to degrees, precision 0.1 mas
00361         static String _toDeg(const Quantity& angle);
00362 
00363         inline void _setParam(const Keyword k, const String& s) {
00364                 _params[k] = s;
00365         }
00366 
00367         // return a string representing a pixel value, precision 1.
00368         static String _printPixel(const Double& d);
00369 
00370         // if freqRefFrame=="" -> use the reference frame of the coordinate system
00371         // if dopplerString=="" -> use the doppler system associated with the coordinate system
00372         // if restfreq=Quantity(0, "Hz") -> use the rest frequency associated with the coordinate system
00373         // Tacitly does nothing if the coordinate system has no spectral axis.
00374         // Returns True if frequencies actually need to be set and were set.
00375         virtual Bool _setFrequencyLimits(
00376                 const Quantity& beginFreq,
00377                 const Quantity& endFreq,
00378                 const String& freqRefFrame,
00379                 const String& dopplerString,
00380                 const Quantity& restfreq
00381         );
00382 
00383 private:
00384         Type _type;
00385         MDirection::Types _directionRefFrame;
00386         CoordinateSystem _csys;
00387         IPosition _directionAxes;
00388         String _label, _font, _labelPos;
00389     RGB _color, _labelColor;
00390         FontStyle _fontstyle;
00391         LineStyle _linestyle;
00392         uInt _fontsize, _linewidth, _symbolsize,
00393                 _symbolthickness;
00394         Bool _usetex;
00395         Vector<MDirection> _convertedDirections;
00396         Vector<MFrequency> _convertedFreqLimits;
00397         Quantity _beginFreq, _endFreq, _restFreq;
00398         Vector<Stokes::StokesTypes> _stokes;
00399         MFrequency::Types _freqRefFrame;
00400         MDoppler::Types _dopplerType;
00401 
00402         map<Keyword, Bool> _globals;
00403         map<Keyword, String> _params;
00404         Bool _printGlobals;
00405         vector<Int> _labelOff;
00406     Direction _convertedDQs;
00407 
00408         static Bool _doneUnitInit, _doneColorInit;
00409         static map<String, LineStyle> _lineStyleMap;
00410         static map<String, Type> _typeMap;
00411         static map<string, RGB> _colors;
00412         static map<RGB, string> _rgbNameMap;
00413         static std::list<std::string> _colorNames;
00414 
00415         const static String _class;
00416 
00417         void _init();
00418         void _initParams();
00419 
00420         static void _initColors();
00421 
00422         static RGB _colorStringToRGB(const String& s);
00423 
00424         static Bool _isRGB(const RGB& rgb);
00425 
00426         void _testConvertToPixel() const;
00427 
00428         static void _initTypeMap();
00429 
00430 
00431 
00432         void _checkAndConvertFrequencies();
00433 
00434         String _printFreqRange() const;
00435 
00436         static String _printFreq(const Quantity& freq);
00437 
00438 };
00439 
00440 inline ostream &operator<<(ostream& os, const AnnotationBase& annotation) {
00441         return annotation.print(os);
00442 };
00443 
00444 inline ostream &operator<<(ostream& os, const AnnotationBase::LineStyle& ls) {
00445         return AnnotationBase::print(os, ls);
00446 };
00447 
00448 inline ostream &operator<<(ostream& os, const AnnotationBase::FontStyle& fs) {
00449         return AnnotationBase::print(os, fs);
00450 };
00451 
00452 inline ostream &operator<<(ostream& os, const map<AnnotationBase::Keyword, String>& x) {
00453         return AnnotationBase::print(os, x);
00454 };
00455 
00456 // Just need a identifiable exception class for exception handling.
00457 class WorldToPixelConversionError : public AipsError {
00458 public:
00459         WorldToPixelConversionError(String msg) : AipsError(msg) {}
00460 };
00461 
00462 }
00463 
00464 #endif