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