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 
00033 #include <measures/Measures/Stokes.h>
00034 
00035 #include <list>
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         using RGB = vector<float>;
00060     
00061         // The pairs have longitude as the first member and latitude as the second
00062         using Direction = Vector<std::pair<Quantity,Quantity> >;
00063 
00064         enum Type {
00065                 // annotations only
00066                 LINE,
00067                 VECTOR,
00068                 TEXT,
00069                 SYMBOL,
00070                 // regions
00071                 RECT_BOX,
00072                 CENTER_BOX,
00073                 ROTATED_BOX,
00074                 POLYGON,
00075                 POLYLINE,
00076                 CIRCLE,
00077                 ANNULUS,
00078                 ELLIPSE
00079         };
00080 
00081         enum Keyword {
00082                 COORD,
00083                 RANGE,
00084                 FRAME,
00085                 CORR,
00086                 VELTYPE,
00087                 RESTFREQ,
00088                 LINEWIDTH,
00089                 LINESTYLE,
00090                 SYMSIZE,
00091                 SYMTHICK,
00092                 COLOR,
00093                 FONT,
00094                 FONTSIZE,
00095                 FONTSTYLE,
00096                 USETEX,
00097                 LABEL,
00098                 LABELCOLOR,
00099                 LABELPOS,
00100                 LABELOFF,
00101                 UNKNOWN_KEYWORD,
00102                 N_KEYS
00103         };
00104 
00105         enum LineStyle {
00106                 SOLID,
00107                 DASHED,
00108                 DOT_DASHED,
00109                 DOTTED
00110         };
00111 
00112         enum FontStyle {
00113                 NORMAL,
00114                 BOLD,
00115                 ITALIC,
00116                 ITALIC_BOLD
00117         };
00118 
00119     static const RGB BLACK;
00120     static const RGB BLUE;
00121     static const RGB CYAN;
00122     static const RGB GRAY;
00123     static const RGB GREEN;
00124     static const RGB MAGENTA;
00125     static const RGB ORANGE;
00126     static const RGB RED;
00127     static const RGB WHITE;
00128     static const RGB YELLOW;
00129 
00130         static const String DEFAULT_LABEL;
00131         static const RGB DEFAULT_COLOR;
00132         static const LineStyle DEFAULT_LINESTYLE;
00133         static const uInt DEFAULT_LINEWIDTH;
00134         static const uInt DEFAULT_SYMBOLSIZE;
00135         static const uInt DEFAULT_SYMBOLTHICKNESS;
00136         static const String DEFAULT_FONT;
00137         static const uInt DEFAULT_FONTSIZE;
00138         static const FontStyle DEFAULT_FONTSTYLE;
00139         static const Bool DEFAULT_USETEX;
00140         static const RGB DEFAULT_LABELCOLOR;
00141         static const String DEFAULT_LABELPOS;
00142         static const vector<Int> DEFAULT_LABELOFF;
00143 
00144         static const Regex rgbHexRegex;
00145 
00146         virtual ~AnnotationBase();
00147 
00148         Type getType() const;
00149 
00150         static LineStyle lineStyleFromString(const String& ls);
00151 
00152         // Given a string, return the corresponding annotation type or throw
00153         // an error if the string does not correspond to an allowed type.
00154         static Type typeFromString(const String& type);
00155 
00156         static String typeToString(const Type type);
00157 
00158         static String keywordToString(const Keyword key);
00159 
00160         static String lineStyleToString(const LineStyle linestyle);
00161 
00162         static FontStyle fontStyleFromString(const String& fs);
00163 
00164         static String fontStyleToString(const FontStyle fs);
00165 
00166         void setLabel(const String& label);
00167 
00168         String getLabel() const;
00169 
00170     // <src>color</src> must either be a recognized color name or
00171     // a valid rgb hex string, else an expection is thrown
00172         void setColor(const String& color);
00173         
00174     // color must have three elements all with values between 0 and 255 inclusive
00175     // or an exception is thrown.
00176     void setColor(const RGB& color);
00177 
00178     // returns the color name if it is recognized or its rgb hex string 
00179         String getColorString() const;
00180 
00181         static String colorToString(const RGB& color);
00182 
00183     // get the color associated with this object
00184     RGB getColor() const;
00185 
00186         void setLineStyle(const LineStyle lineStyle);
00187 
00188         LineStyle getLineStyle() const;
00189 
00190         void setLineWidth(const uInt linewidth);
00191 
00192         uInt getLineWidth() const;
00193 
00194         void setSymbolSize(const uInt symbolsize);
00195 
00196         uInt getSymbolSize() const;
00197 
00198         void setSymbolThickness(const uInt symbolthickness);
00199 
00200         uInt getSymbolThickness() const;
00201 
00202         void setFont(const String& font);
00203 
00204         String getFont() const;
00205 
00206         void setFontSize(const uInt fontsize);
00207 
00208         uInt getFontSize() const;
00209 
00210         void setFontStyle(const FontStyle& fontstyle);
00211 
00212         FontStyle getFontStyle() const;
00213 
00214         void setUseTex(const Bool usetex);
00215 
00216         Bool isUseTex() const;
00217 
00218         // is the object a region?
00219         virtual Bool isRegion() const;
00220 
00221         // is the object only an annotation? Can only be false if the object
00222         // is a region
00223         inline virtual Bool isAnnotationOnly() const { return True; }
00224 
00225         // set "pix" as valid unit. This should be called externally
00226         // before creating quantities which have pixel units.
00227         static void unitInit();
00228 
00229     // <src>color</src> must either be a recognized color name or
00230     // a valid rgb hex string, else an expection is thrown
00231         void setLabelColor(const String& color);
00232 
00233     // color must have three elements all with values between 0 and 255 inclusive
00234     // or an exception is thrown.
00235     void setLabelColor(const RGB& color);
00236 
00237     // returns the color name if it is recognized or its rgb hex string
00238 
00239         String getLabelColorString() const;
00240 
00241     // get the color associated with this object's label
00242     RGB getLabelColor() const;
00243 
00244     // returns one of top, bottom, left, or right.
00245         String getLabelPosition() const;
00246 
00247         // <src>position</src> must have a value in top, bottom, left, or right.
00248         // case is ignored.
00249         void setLabelPosition(const String& position);
00250 
00251         // <src>offset</src> must have two elements
00252         void setLabelOffset(const vector<Int>& offset);
00253 
00254         vector<Int> getLabelOffset() const;
00255 
00256         virtual ostream& print(ostream &os) const = 0;
00257 
00258         // These parameters are included at the global scope. Multiple runs
00259         // on the same object are cumulative; if a key exists in the current
00260         // settings but not in <src>globalKeys</src> that key will still exist
00261         // in the globals after setGlobals has run.
00262         void setGlobals(const Vector<Keyword>& globalKeys);
00263 
00264         // print a set of keyword value pairs
00265         static ostream& print(
00266                 ostream& os, const map<Keyword, String>& params
00267         );
00268 
00269         // print a line style representation
00270         static ostream& print(
00271                 ostream& os, const LineStyle ls
00272         );
00273 
00274         // print a font style representation
00275         static ostream& print(
00276                 ostream& os, const FontStyle fs
00277         );
00278 
00279         static ostream& print(
00280                 ostream& os, const Direction d
00281         );
00282 
00283         // Get a list of the user-friendly color names supported
00284         static std::list<std::string> colorChoices();
00285 
00286         // get the coordinate system associated with this object.
00287         // This is the same coordinates system used to construct the object.
00288 
00289         inline const CoordinateSystem& getCsys() const {
00290                 return _csys;
00291         }
00292 
00293         // DEPRECATED Please use getConvertedDirections()
00294         // the pair elements have longitude as the first member and latitude as the second.
00295         // FIXME make this return of vector of MVDirections
00296         // Returns the same angles as getConvertedDirections()
00297         Direction getDirections() const;
00298 
00299         // get the frequency limits converted to the spectral frame of the coordinate
00300         // system of this object. An empty Vector implies all applicable frequencies
00301         // have been selected.
00302         Vector<MFrequency> getFrequencyLimits() const;
00303 
00304         // Get the stokes for which the selection applies. An empty Vector implies
00305         // all applicable stokes have been selected.
00306         Vector<Stokes::StokesTypes> getStokes() const;
00307 
00308         // if freqRefFrame=="" -> use the reference frame of the coordinate system
00309         // if dopplerString=="" -> use the doppler system associated with the coordinate system
00310         // if restfreq=Quantity(0, "Hz") -> use the rest frequency associated with the coordinate system
00311         // Tacitly does nothing if the coordinate system has no spectral axis.
00312         // Returns True if frequencies actually need to be set and were set.
00313         virtual Bool setFrequencyLimits(
00314                 const Quantity& beginFreq,
00315                 const Quantity& endFreq,
00316                 const String& freqRefFrame,
00317                 const String& dopplerString,
00318                 const Quantity& restfreq
00319         );
00320 
00321         // same as getDirections, only returns proper MDirections
00322         inline const Vector<MDirection>& getConvertedDirections() const {
00323                 return _convertedDirections;
00324         }
00325 
00326 protected:
00327 
00328         // if <src>freqRefFrame</src> or <src>dopplerString</src> are empty,
00329         // the values from the spectral coordinate of csys will be used, if one
00330         // exists. if restfreq=Quantity(0, "Hz") -> use the rest frequency associated with the coordinate system
00331         AnnotationBase(
00332                 const Type type, const String& dirRefFrameString,
00333                 const CoordinateSystem& csys, const Quantity& beginFreq,
00334                 const Quantity& endFreq,
00335                 const String& freqRefFrame,
00336                 const String& dopplerString,
00337                 const Quantity& restfreq,
00338                 const Vector<Stokes::StokesTypes>& stokes
00339         );
00340 
00341         // use only if the frame of the input directions is the
00342         // same as the frame of the coordinate system. All frequencies
00343         // are used.
00344         AnnotationBase(
00345                 const Type type, const CoordinateSystem& csys,
00346                 const Vector<Stokes::StokesTypes>& stokes
00347         );
00348 
00349         // the implicitly defined copy constructor is fine
00350         // AnnotationBase(const AnnotationBase& other);
00351 
00352         // assignment operator
00353         AnnotationBase& operator= (const AnnotationBase& other);
00354 
00355         static void _checkMixed(
00356                 const String& origin,
00357                 const Direction& dirs
00358         );
00359 
00360         MDirection _directionFromQuantities(
00361                 const Quantity& q0, const Quantity& q1
00362         );
00363 
00364         void _checkAndConvertDirections(
00365                 const String& origin,
00366                 const Direction& dirs
00367         );
00368 
00369         virtual void _printPairs(ostream& os) const;
00370 
00371 
00372 
00373         inline const IPosition& _getDirectionAxes() const {
00374                 return _directionAxes;
00375         }
00376 
00377         // direction to string, precision of 0.1 mas
00378         // ra and dec in sexigesimal format, non-equatorial coords in degrees
00379         String _printDirection(
00380                 const Quantity& longitude, const Quantity& latitude
00381         ) const;
00382 
00383         // convert angle to arcsec, precision 0.1 mas
00384         static String _toArcsec(const Quantity& angle);
00385 
00386         // convert angle to degrees, precision 0.1 mas
00387         static String _toDeg(const Quantity& angle);
00388 
00389         inline void _setParam(const Keyword k, const String& s) {
00390                 _params[k] = s;
00391         }
00392 
00393         // return a string representing a pixel value, precision 1.
00394         static String _printPixel(const Double& d);
00395 
00396         MDirection::Types _getDirectionRefFrame() const { return _directionRefFrame; }
00397 
00398 private:
00399         Type _type;
00400         MDirection::Types _directionRefFrame;
00401         CoordinateSystem _csys;
00402         IPosition _directionAxes;
00403         String _label, _font, _labelPos;
00404     RGB _color, _labelColor;
00405         FontStyle _fontstyle;
00406         LineStyle _linestyle;
00407         uInt _fontsize, _linewidth, _symbolsize,
00408                 _symbolthickness;
00409         Bool _usetex;
00410         Vector<MDirection> _convertedDirections;
00411         Vector<MFrequency> _convertedFreqLimits;
00412         Quantity _beginFreq, _endFreq, _restFreq;
00413         Vector<Stokes::StokesTypes> _stokes;
00414         MFrequency::Types _freqRefFrame;
00415         MDoppler::Types _dopplerType;
00416 
00417         map<Keyword, Bool> _globals;
00418         map<Keyword, String> _params;
00419         Bool _printGlobals;
00420         vector<Int> _labelOff;
00421 
00422         static Bool _doneUnitInit, _doneColorInit;
00423         static map<String, LineStyle> _lineStyleMap;
00424         static map<String, Type> _typeMap;
00425         static map<string, RGB> _colors;
00426         static map<RGB, string> _rgbNameMap;
00427         static std::list<std::string> _colorNames;
00428 
00429         const static String _class;
00430 
00431         void _init();
00432         void _initParams();
00433 
00434         static void _initColors();
00435 
00436         static RGB _colorStringToRGB(const String& s);
00437 
00438         static Bool _isRGB(const RGB& rgb);
00439 
00440         void _testConvertToPixel() const;
00441 
00442         static void _initTypeMap();
00443 
00444         void _checkAndConvertFrequencies();
00445 
00446         String _printFreqRange() const;
00447 
00448         static String _printFreq(const Quantity& freq);
00449 
00450 };
00451 
00452 inline ostream &operator<<(ostream& os, const AnnotationBase& annotation) {
00453         return annotation.print(os);
00454 };
00455 
00456 inline ostream &operator<<(ostream& os, const AnnotationBase::LineStyle& ls) {
00457         return AnnotationBase::print(os, ls);
00458 };
00459 
00460 inline ostream &operator<<(ostream& os, const AnnotationBase::FontStyle& fs) {
00461         return AnnotationBase::print(os, fs);
00462 };
00463 
00464 inline ostream &operator<<(ostream& os, const map<AnnotationBase::Keyword, String>& x) {
00465         return AnnotationBase::print(os, x);
00466 };
00467 
00468 inline ostream &operator<<(ostream& os, const AnnotationBase::Direction x) {
00469         return AnnotationBase::print(os, x);
00470 };
00471 
00472 // Just need a identifiable exception class for exception handling.
00473 class WorldToPixelConversionError : public AipsError {
00474 public:
00475         WorldToPixelConversionError(String msg) : AipsError(msg) {}
00476 };
00477 
00478 }
00479 
00480 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1