casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LCPolygon.h
Go to the documentation of this file.
00001 //# LCPolygon.h: Define a 2-dimensional region by a polygon
00002 //# Copyright (C) 1998,2000
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: LCPolygon.h 21038 2011-04-04 12:16:23Z gervandiepen $
00027 
00028 #ifndef LATTICES_LCPOLYGON_H
00029 #define LATTICES_LCPOLYGON_H
00030 
00031 //# Includes
00032 #include <lattices/Lattices/LCRegionFixed.h>
00033 #include <casa/Arrays/Vector.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 //# Forward Declarations
00038 template<class T> class Matrix;
00039 
00040 
00041 // <summary>
00042 // Define a 2-dimensional region by a polygon.
00043 // </summary>
00044 
00045 // <use visibility=export>
00046 
00047 // <reviewed reviewer="" date="" tests="">
00048 // </reviewed>
00049 
00050 // <prerequisite>
00051 //   <li> <linkto class=LCRegion>LCRegion</linkto>
00052 // </prerequisite>
00053 
00054 // <synopsis> 
00055 // The LCPolygon class is a specialization of class
00056 // <linkto class=LCRegion>LCRegion</linkto>.
00057 // It makes it possible to define a 2-dimensional region by means
00058 // an ordered collection of points with straight lines connecting
00059 // adjacent points. The last points can be equal to the first one.
00060 // If not, an extra point gets added to get the closing line.
00061 // <p>
00062 // The polygon can be as complex as one likes. E.g. it is possible to
00063 // have a rectangle with an inner rectangle to exclude interior points.
00064 // <p>
00065 // The points defining the polygon do not need to coincide with pixel points.
00066 // At least one point of the polygon must be within the lattice space.
00067 // Points may be outside the lattice meaining that only part of the
00068 // polygon surface is actually used.
00069 // </synopsis> 
00070 
00071 // <example>
00072 // <srcblock>
00073 //    // A simple (tilted) square.
00074 //    Vector<Float> x(4), y(4);
00075 //    x(0)=3; y(0)=3;
00076 //    x(1)=6; y(1)=6;
00077 //    x(2)=3; y(2)=9;
00078 //    x(3)=0; y(3)=6;
00079 //    LCPolygon region(x, y, IPosition(2,128,128));
00080 //
00081 //    // A rectangle with an inner region to exclude interior points.
00082 //    // Note that the last point is equal to the first point, thus
00083 //    // the last line is given explicitly.
00084 //    Vector<Float> x(11), y(11);
00085 //    x(0)=3; y(0)=3;
00086 //    x(1)=9; y(1)=3;
00087 //    x(2)=9; y(2)=8;
00088 //    x(3)=3; y(3)=8;
00089 //    x(4)=3; y(4)=3;
00090 //    x(5)=5; y(5)=5;
00091 //    x(6)=8; y(6)=4;
00092 //    x(7)=7; y(7)=7;
00093 //    x(8)=5; y(8)=7;
00094 //    x(9)=5; y(9)=5;
00095 //    x(10)=3; y(10)=3;
00096 //    LCPolygon region(x, y, IPosition(2,128,128));
00097 // </srcblock>
00098 // </example>
00099 
00100 //# <todo asof="1997/11/11">
00101 //# <li>
00102 //# </todo>
00103 
00104 
00105 class LCPolygon: public LCRegionFixed
00106 {
00107 public:
00108     LCPolygon();
00109 
00110     // Construct from the given x and y values.
00111     // The latticeShape must define a 2-dimensional lattice.
00112     // <br>LCPolygon can be used for an N-dimensional lattice by making
00113     // another lattice representing any 2 axes from the original lattice.
00114     // <group>
00115     LCPolygon (const Vector<Float>& x, const Vector<Float>& y,
00116                const IPosition& latticeShape);
00117     LCPolygon (const Vector<Double>& x, const Vector<Double>& y,
00118                const IPosition& latticeShape);
00119     // </group>
00120 
00121     // Copy constructor (reference semantics).
00122     LCPolygon (const LCPolygon& other);
00123 
00124     virtual ~LCPolygon();
00125 
00126     // Assignment (copy semantics).
00127     LCPolygon& operator= (const LCPolygon& other);
00128 
00129     // Comparison
00130     virtual Bool operator== (const LCRegion& other) const;
00131 
00132     // Make a copy of the derived object.
00133     virtual LCRegion* cloneRegion() const;
00134 
00135     // Get the X-values.
00136     const Vector<Float>& x() const;
00137 
00138     // Get the Y-values.
00139     const Vector<Float>& y() const;
00140 
00141     // Get the class name (to store in the record).
00142     static String className();
00143 
00144     // Get the region type.  Returns className()
00145     virtual String type() const;
00146 
00147     // Convert the (derived) object to a record.
00148     virtual TableRecord toRecord (const String& tableName) const;
00149 
00150     // Convert correct object from a record.
00151     static LCPolygon* fromRecord (const TableRecord&,
00152                                   const String& tablename);
00153 
00154 protected:
00155     // Construct another LCPolygon (for e.g. another lattice) by moving
00156     // this one. It recalculates the bounding box.
00157     // A positive translation value indicates "to right".
00158     virtual LCRegion* doTranslate (const Vector<Float>& translateVector,
00159                                    const IPosition& newLatticeShape) const;
00160 
00161 private:
00162     // Make the bounding box.
00163     void defineBox();
00164 
00165     // Define the mask to indicate which elements are inside the polygon.
00166     void defineMask();
00167 
00168     // Fill the mask from the given points.
00169     void fillMask (Bool* mask, Int nx, Int ny, Int blcx, Int blcy,
00170                    const Float* ptrX, const Float* ptrY, uInt nrline);
00171 
00172     // Truncate a start value to a pixel point.
00173     // A pixel point is taken if near the value, otherwise floor(value+1).
00174     // The returned value is never < 0.
00175     Int truncateStart (Float v);
00176 
00177     // Truncate an end value to a pixel point.
00178     // A pixel point is taken if near the value, otherwise floor(value).
00179     // The returned value is never > maxEnd.
00180     Int truncateEnd (Float v, Int maxEnd);
00181 
00182     
00183     Vector<Float> itsX;
00184     Vector<Float> itsY;
00185 };
00186 
00187 
00188 inline const Vector<Float>& LCPolygon::x() const
00189 {
00190     return itsX;
00191 }
00192 inline const Vector<Float>& LCPolygon::y() const
00193 {
00194     return itsY;
00195 }
00196 
00197 
00198 
00199 } //# NAMESPACE CASA - END
00200 
00201 #endif