00001 /* Copyright (C) 2002 Dominic Letourneau (dominic.letourneau@usherbrooke.ca) 00002 00003 This library is free software; you can redistribute it and/or 00004 modify it under the terms of the GNU Lesser General Public 00005 License as published by the Free Software Foundation; either 00006 version 2.1 of the License, or (at your option) any later version. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Lesser General Public License for more details. 00012 00013 You should have received a copy of the GNU Lesser General Public 00014 License along with this library; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00016 */ 00017 #ifndef _MY_CRECT_ 00018 #define _MY_CRECT_ 00019 00020 #include "math.h" 00021 #include <vector> 00022 #include <list> 00023 #include <stdlib.h> 00024 #include <stdio.h> 00025 #include "Object.h" 00026 #include "CPoint.h" 00027 #include "Cell.h" 00028 00029 namespace RobotFlow { 00030 00031 /** CRect. 00032 00033 A CRect is a rectangle defined by its upper left corner and its lower 00034 right corner and contains ImageCell elements of multiple lines. 00035 00036 @author Dominic Letourneau 00037 @version $Revision: 1.7 $ 00038 \begin{verbatim} 00039 Creation : 23/02/2001 00040 Last Modification: $Date: 2005/03/29 15:20:47 $ 00041 \end{verbatim} 00042 */ 00043 class CRect : public FD::Object { 00044 00045 public: 00046 00047 /** 00048 Ctor default with (x1,y1,x2,y2) = (0,0,0,0) 00049 */ 00050 CRect(); 00051 00052 /** 00053 Ctor with corner values 00054 @param x1 ulx 00055 @param y1 uly 00056 @param x2 lrx 00057 @param y2 lry 00058 */ 00059 CRect(int x1, int y1, int x2, int y2); 00060 00061 00062 /** 00063 Copy ctor 00064 @param r The CRect to copy 00065 */ 00066 CRect(const CRect& r); 00067 00068 /** 00069 Ctor with CPoints for ulc and lrc 00070 @param p1 ulc 00071 @param p2 lrc 00072 */ 00073 CRect(CPoint& p1,CPoint& p2); 00074 00075 /** 00076 Dtor Destructor 00077 */ 00078 virtual ~CRect(); 00079 00080 /** 00081 Cleanup for unused cells 00082 */ 00083 void cell_cleanup(); 00084 00085 /** 00086 Intersection between two CRect 00087 @param inter the CRect we are doing intersection with (destination) 00088 @param rect2 the Crect we are doing intersectino with (source) 00089 */ 00090 void IntersectRect(CRect &inter,const CRect &rect2); 00091 00092 /** 00093 Union between two CRect 00094 @param uni the CRect we are doing intersection with (destination) 00095 @param rect2 the Crect we are doing intersectino with (source) 00096 */ 00097 void UnionRect(CRect &uni,const CRect &rect2); 00098 00099 /** 00100 Sets the values of its coordinates 00101 @param p1 ulc 00102 @param p2 lrc 00103 00104 */ 00105 void SetRect(CPoint& p1,CPoint& p2); 00106 00107 /** 00108 Verifies if the CRect is empty 00109 @return bool True if the CRect is empty 00110 */ 00111 bool IsRectEmpty() const; 00112 00113 /** 00114 Returns the height of the CRect 00115 @return int The height 00116 */ 00117 int Height() const; 00118 00119 /** 00120 Returns the width of the CRect 00121 @return int The width 00122 */ 00123 int Width() const; 00124 00125 /** 00126 Returns the mean of the cell width 00127 @return int the mean of the cell width 00128 */ 00129 int MeanCellWidth(int* pCellMin, int* pCellMax, int nSize, int nWidthMax); 00130 00131 00132 /** 00133 Returns the mean cell width for a CRect 00134 @return int the mean_width 00135 */ 00136 float MeanCellWidth(); 00137 00138 /** 00139 Returns the Area of the CRect. The Area is not the Area of the rectangle 00140 but what's contained in it. This is the area of all ImageCell put together 00141 that are added in the CRect. 00142 @return int Area 00143 */ 00144 int Area () const; 00145 00146 /** 00147 Returns the upper left corner 00148 @return CPoint The ulc 00149 */ 00150 CPoint TopLeft(); 00151 00152 /** 00153 Returns the lower right corner 00154 @return CPoint The lrc 00155 */ 00156 CPoint BottomRight(); 00157 00158 /** 00159 Verifies if two CRect are identical 00160 @return bool True if they are identical 00161 */ 00162 bool operator==(const CRect &rect) const; 00163 00164 /** 00165 Verifies if the area is smaller 00166 @return bool True if smaller 00167 */ 00168 bool operator < (const CRect & rect); 00169 00170 /** 00171 Verifies if the area is larger 00172 @return bool True if larger 00173 */ 00174 bool operator > (const CRect & rect); 00175 00176 /** 00177 Assignation operator between two CRect 00178 @param rect a CRect Instance to equals 00179 @return CRect& a CRect Instance 00180 */ 00181 CRect& operator=(const CRect &rect); 00182 00183 /** 00184 Adds a ImageCell to the CRect 00185 @param cell, The ImageCell to be added 00186 @param update, True if the area and the coordinates are to be updated 00187 */ 00188 void add_cell(ImageCell *cell, bool update = false, bool serialized = false); 00189 00190 /** 00191 Returns the list of cell in the CRect 00192 @return list<ImageCell*>& The list of cells 00193 */ 00194 std::list<ImageCell*>& get_cell_list(); 00195 00196 /** 00197 Sets the corner values of the CRect. 00198 @param x1 ulx 00199 @param y1 uly 00200 @param x2 lrx 00201 @param y2 lry 00202 */ 00203 void set_values(int x1,int y1, int x2,int y2); 00204 00205 /** 00206 Returns the coordinates of the upper left corner and the lower right corner 00207 @param x1 ulx 00208 @param y1 uly 00209 @param x2 lrx 00210 @param y2 lry 00211 */ 00212 void get_values(int &x1,int &y1, int &x2,int &y2); 00213 00214 /** 00215 Verifies if a Crect is inside another one 00216 @param rect The rect to verify 00217 @return bool true if the rect is inside 00218 */ 00219 bool inside (CRect *rect); 00220 00221 /** 00222 Calculates the center of gravity from the ImageCells 00223 */ 00224 void center_of_gravity (); 00225 00226 /** 00227 Gets the center of gravity. You must call center_of_gravity() first to calculate it. 00228 @param center_x the x center of gravity 00229 @param center_y the y center of gravity 00230 */ 00231 void get_center_of_gravity(double ¢er_x, double ¢er_y); 00232 00233 void printOn(std::ostream &out = std::cout) const; 00234 00235 void readFrom(std::istream &in); 00236 00237 /** 00238 Merge two CRect (adding cells) 00239 @param rect The rect to merge with 00240 */ 00241 void merge(CRect *rect); 00242 00243 /** 00244 Distance (cog to cog) of two CRect 00245 @return double the distance 00246 */ 00247 double dist (CRect *rect); 00248 00249 double dist (double x, double y); 00250 00251 private: 00252 00253 ///Center of gravity y 00254 double m_center_y; 00255 00256 ///Center of gravity x 00257 double m_center_x; 00258 00259 ///Area of the ImageCells 00260 int m_area; 00261 00262 ///ulx 00263 int m_x1; 00264 00265 ///uly 00266 int m_y1; 00267 00268 ///lrx 00269 int m_x2; 00270 00271 ///lry 00272 int m_y2; 00273 00274 ///The list of cells 00275 std::list<ImageCell*> m_cell_list; 00276 00277 ///The temp serialized cells list 00278 std::list<ImageCell*> m_serialized_list; 00279 00280 }; 00281 }//namespace RobotFlow 00282 #endif