latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiPointList.h 00027 * authors ....: Suat Akyol 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 02.11.00 00030 * revisions ..: $Id: ltiPointList.h,v 1.9 2009/06/12 16:41:23 alvarado Exp $ 00031 */ 00032 00033 #ifndef _LTI_POINTLIST_H_ 00034 #define _LTI_POINTLIST_H_ 00035 00036 #include "ltiObject.h" 00037 #include "ltiIoObject.h" 00038 #include "ltiTypes.h" 00039 #include "ltiPoint.h" 00040 #include "ltiRectangle.h" 00041 #include "ltiException.h" 00042 #include "ltiVector.h" 00043 00044 #include <iostream> 00045 00046 #include <list> 00047 00048 namespace lti { 00049 /** 00050 * tpointList template class. 00051 * The %lti::tpointList class allows the storage of a list of tpoints<T>. 00052 * The elements of the tpointList can be accessed through iterators. 00053 * 00054 * Example: 00055 * 00056 * \code 00057 * lti::pointList pts; // a list of points with integer coodinates 00058 * 00059 * // create 10 points 00060 * for (int i=0;i<10;++i) { 00061 * pts.push_back(point(i,i)); 00062 * } 00063 * 00064 * // iterate on the list of points to add 1 to x and 2 to y: 00065 * lti::pointList::iterator it; 00066 * for (it=pts.begin();it!=pts.end();++pts) { 00067 * (*pts).add(point(1,2)); 00068 * } 00069 * \endcode 00070 * 00071 * @ingroup gAggregate 00072 */ 00073 template<class T> 00074 class tpointList : public ioObject { 00075 public: 00076 typedef std::list<tpoint<T> > list_type; 00077 00078 /** 00079 * iterator type (allows read and write operations) 00080 * The use of the iterator classes is similar to the iterators of 00081 * the STL (Standard Template Library). See lti::pointList::begin() 00082 * for an example 00083 */ 00084 typedef typename list_type::iterator iterator; 00085 00086 /** 00087 * const iterator type (allows read-only operations) 00088 * The use of the iterator classes is similar to the iterators of 00089 * the STL (Standard Template Library). See lti::pointList::begin() 00090 * for an example. 00091 */ 00092 typedef typename list_type::const_iterator const_iterator; 00093 00094 /** 00095 * reference type (allows read and write operations) 00096 * The use of the reference classes is similar to the references of 00097 * the STL (Standard Template Library). 00098 */ 00099 typedef typename list_type::reference reference; 00100 00101 /** 00102 * const_reference type (allows read-only operations) 00103 * The use of the reference classes is similar to the references of 00104 * the STL (Standard Template Library). 00105 */ 00106 typedef typename list_type::const_reference const_reference; 00107 00108 /** 00109 * default constructor creates an empty pointList; 00110 */ 00111 tpointList(); 00112 00113 /** 00114 * create this pointList as a copy of another pointList 00115 * @param other the pointList to be copied. 00116 */ 00117 tpointList(const tpointList<T>& other); 00118 00119 /** 00120 * create this pointList as a copy of a list_type of tpoint<T> 00121 * @param other the pointList to be copied. 00122 */ 00123 tpointList(const list_type& other); 00124 00125 /** 00126 * destructor 00127 */ 00128 virtual ~tpointList(); 00129 00130 /** 00131 * returns the name of this class: tpointList<T> 00132 */ 00133 const char* getTypeName() const {return "tpointList<T>";}; 00134 00135 /** 00136 * returns the number of elements of the pointList 00137 */ 00138 int size() const; 00139 00140 /** 00141 * compares the size of this list with the size of the other point list 00142 * and returns true if this list has fewer points than the other one. 00143 */ 00144 bool operator<(const tpointList<T>& other) const; 00145 00146 /** 00147 * compares the size of this list with the size of the other point list 00148 * and returns true if this list has more points than the other one. 00149 */ 00150 bool operator>(const tpointList<T>& other) const; 00151 00152 /** 00153 * returns first element as a const_iterator. 00154 * Note that you can not change the values of the pointList 00155 * elements when you use a const_iterator. See also begin() 00156 */ 00157 const_iterator begin() const; 00158 00159 /** 00160 * returns an iterator pointing to the first element of the pointList 00161 * The use of the interators is similar to the iterators of the 00162 * Standard Template Library (STL). 00163 * If you need to iterate on all elements of the pointList, you can 00164 * use following code: 00165 * \code 00166 * lti::tpointList<int> myPL; // an empty pointList 00167 * lti::tpointList<int>::iterator it; // an iterator 00168 * 00169 * // Fill pointList with some arbitrary points 00170 * for (int i=0; i<10; i++) { 00171 * myPL.push_back(lti::point(0,i)); 00172 * } 00173 * 00174 * // Swap x and y for all points in list 00175 * for (it=myPL.begin();it!=myPL.end();it++) { 00176 * int temp; 00177 * temp = (*it).x; 00178 * (*it).x = (*it).y; 00179 * (*it).y = temp; 00180 * } 00181 * \endcode 00182 */ 00183 iterator begin(); 00184 00185 /** 00186 * returns last element as a const iterator. 00187 * For an example see begin() 00188 */ 00189 const_iterator end() const; 00190 00191 /** 00192 * returns last element as an iterator 00193 * For an example see begin() 00194 */ 00195 iterator end(); 00196 00197 /** 00198 * deletes all points from list and leaves empty pointList. 00199 */ 00200 void clear(); 00201 00202 /** 00203 * erases point, which is denoted by it. Returns iterator to next element. 00204 */ 00205 iterator erase(const iterator& it); 00206 00207 /** 00208 * erases points between first and last. Returns iterator to next element. 00209 */ 00210 iterator erase(const iterator& first, const iterator& last); 00211 00212 /** 00213 * inserts point before position denoted by it. 00214 * returns iterator to inserted element. 00215 */ 00216 iterator insert(const iterator& it, const tpoint<T>& thePoint); 00217 00218 /** 00219 * inserts points before position denoted by it. 00220 * Returns iterator to first element of inserted elements. 00221 */ 00222 iterator insert(const iterator& it, 00223 const int& n, 00224 const tpoint<T>& thePoint); 00225 00226 /** 00227 * inserts the elements from first to last, before position denoted by it. 00228 * Returns iterator to first element of inserted elements. 00229 */ 00230 iterator insert(const iterator& it, 00231 const_iterator first, 00232 const_iterator last); 00233 00234 /** 00235 * Transfer all elements in the second list into this one. 00236 * At the end of the operation, the second list will be empty 00237 */ 00238 void splice(const iterator& pos, 00239 tpointList<T>& other); 00240 00241 /** 00242 * inserts element at begin of pointList 00243 */ 00244 void push_front(const tpoint<T>& thePoint); 00245 00246 /** 00247 * removes element at begin of pointList 00248 */ 00249 void pop_front(); 00250 00251 /** 00252 * inserts element at end of pointList 00253 */ 00254 void push_back(const tpoint<T>& thePoint); 00255 00256 /** 00257 * removes element at end of pointList 00258 */ 00259 void pop_back(); 00260 00261 /** 00262 * returns a reference to the first element 00263 */ 00264 reference front(); 00265 00266 /** 00267 * returns a const reference to the first element 00268 */ 00269 const_reference front() const; 00270 00271 /** 00272 * returns a reference to the last element 00273 */ 00274 reference back(); 00275 00276 /** 00277 * returns a const reference to the last element 00278 */ 00279 const_reference back() const; 00280 00281 /** 00282 * assigment operator. 00283 * copy the contents of <code>other</code> in this %object. 00284 * @param other the source pointList to be copied. 00285 */ 00286 tpointList<T>& copy(const tpointList<T>& other); 00287 00288 /** 00289 * assigment operator (alias for copy(other)). 00290 * @param other the pointList to be copied 00291 * @return a reference to the actual pointList 00292 */ 00293 tpointList<T>& operator=(const tpointList<T>& other); 00294 00295 /** 00296 * create a clone of this pointList 00297 * @return a pointer to a copy of this pointList 00298 */ 00299 virtual object* clone() const; 00300 00301 /** 00302 * @name Conversion Methods 00303 */ 00304 //@{ 00305 00306 /** 00307 * copy the elements of the other standard list of tpoint<T> in this 00308 * %object 00309 * @param other the source pointList to be copied. 00310 */ 00311 tpointList<T>& castFrom(const list_type& other); 00312 00313 /** 00314 * copy the <code>other</code> point-list by casting each of its elements 00315 * @param other The point list to be casted 00316 */ 00317 template<class U> 00318 tpointList<T>& castFrom(const tpointList<U>& other) { 00319 clear(); 00320 typename tpointList<U>::const_iterator it,eit; 00321 for (it=other.begin(),eit=other.end();it!=eit;++it) { 00322 push_back(tpoint<T>(static_cast<T>((*it).x), 00323 static_cast<T>((*it).y))); 00324 } 00325 return (*this); 00326 }; 00327 00328 /** 00329 * cast the given vector of points into a list, where the 00330 * first element in the vector will be the first element 00331 * in the list. 00332 * @param other the vector of points with the points 00333 * @return a reference to this instance 00334 */ 00335 template<class U> 00336 tpointList<T>& castFrom(const vector< tpoint<U> >& other) { 00337 clear(); 00338 typename vector< tpoint<U> >::const_iterator it,eit; 00339 for (it=other.begin(),eit=other.end();it!=eit;++it) { 00340 push_back(*it); 00341 } 00342 return *this; 00343 } 00344 00345 /** 00346 * cast the given vector of points into a list, where the 00347 * first element in the vector will be the first element 00348 * in the list. 00349 */ 00350 template<class U> 00351 tpointList<T>& castFrom(const vector< tpoint<U> >& other) const { 00352 clear(); 00353 typename vector< tpoint<U> >::const_iterator it,eit; 00354 for (it=other.begin(),eit=other.end();it!=eit;++it) { 00355 push_back(*it); 00356 } 00357 return *this; 00358 } 00359 00360 /** 00361 * cast this list of points into a lti::vector, which can be 00362 * accessed in a faster way than the list. 00363 */ 00364 template<class U> 00365 void castTo(vector< tpoint<U> >& other) const { 00366 other.resize(size(),tpoint<U>(),false,false); 00367 typename tpointList<T>::const_iterator it; 00368 int i; 00369 for (i=0,it=begin();i<other.size();++i,++it) { 00370 other.at(i).copy(*it); 00371 } 00372 } 00373 //@} 00374 00375 /** 00376 * compare this pointList with other 00377 * @param other the other pointList to be compared with 00378 * @return true if both pointLists have the same elements and same size 00379 */ 00380 bool equals(const tpointList<T>& other) const; 00381 00382 /** 00383 * compare this pointList with other 00384 * @param other the other pointList to be compared with 00385 * @return true if both pointLists have the same elements and same size 00386 */ 00387 bool operator==(const tpointList<T>& other) const; 00388 00389 /** 00390 * returns true if the list is empty 00391 */ 00392 bool empty() const; 00393 00394 /** 00395 * sort the list using std::less<T>, i.e. operator<() 00396 */ 00397 void sort(); 00398 00399 # ifndef _LTI_MSC_6 00400 // MSC++ does not support template members in a template 00401 00402 /** 00403 * sort the list using \p Compare. \p Compare must be a struct 00404 * that declares 00405 * 00406 * \code 00407 * bool operator() (const T& x, const T& y) const 00408 * \endcode 00409 * 00410 * or a function with the same signature (note that this function 00411 * pointer approach is usually slower) 00412 */ 00413 template<class Compare> 00414 void sort(Compare func); 00415 #endif 00416 00417 /** 00418 * returns the last set or calculated boundary. 00419 * 00420 * The boundary is the smallest rectangle that contains all the 00421 * points in the list. Note that the boundary must be set by the 00422 * user, or the user must explicitly specify that it must be 00423 * updated (see lti::tpointList<T>::updateBoundary). It will NOT 00424 * be updated automatically 00425 */ 00426 const trectangle<T>& getBoundary() const {return boundary;}; 00427 00428 /** 00429 * set the boundary of the points. 00430 * 00431 * The boundary is the smallest rectangle that contains all the 00432 * points in the list. Note that the boundary must be set by the 00433 * user using this method, or the user must explicitly specify 00434 * that it must be updated (see lti::tpointList<T>::updateBoundary). 00435 * It will \b NOT be updated automatically with each point you insert or 00436 * delete. 00437 */ 00438 void setBoundary(const trectangle<T>& r) {boundary.copy(r);}; 00439 00440 /** 00441 * calculate the boundary box. 00442 * 00443 * The boundary is the smallest rectangle that contains all the 00444 * points in the list. Note that the boundary must be set by the 00445 * user, or the user must explicitly specify that it must be 00446 * calculated. 00447 * 00448 * This member computes the boundary, but it does not set the compute 00449 * one into the internal boundary attribute. See also updateBoundary(). 00450 * 00451 * @return a the calculated boundary 00452 */ 00453 const trectangle<T> computeBoundary() const; 00454 00455 /** 00456 * calculate and update the boundary box. 00457 * 00458 * The boundary is the smallest rectangle that contains all the 00459 * points in the list. Note that the boundary must be set by the 00460 * user, or the user must explicitly specify that it must be 00461 * calculated. 00462 * 00463 * This member computes the boundary AND set the internal boundary 00464 * attribute. See also computeBoundary(). 00465 * 00466 * @return a reference to the calculated boundary 00467 */ 00468 const trectangle<T>& updateBoundary(); 00469 00470 /** 00471 * write the point list in the given ioHandler 00472 */ 00473 virtual bool write(ioHandler& handler,const bool complete = true) const; 00474 00475 /** 00476 * read the point list from the given ioHandler 00477 */ 00478 virtual bool read(ioHandler& handler,const bool complete = true); 00479 00480 00481 protected: 00482 /** 00483 * this pointList class is implemented with a list_type<> instance 00484 */ 00485 list_type thePointList; 00486 00487 /** 00488 * boundary is the smallest rectangle which includes all the points 00489 * in the list 00490 */ 00491 trectangle<T> boundary; 00492 }; 00493 00494 /** 00495 * read the vector from the given ioHandler. The complete flag indicates 00496 * if the enclosing begin and end should be also be readed 00497 * 00498 * @ingroup gStorable 00499 */ 00500 template<class T> 00501 bool read(ioHandler& handler,tpointList<T>& plst,const bool complete=true) { 00502 return plst.read(handler,complete); 00503 } 00504 00505 /** 00506 * write the vector in the given ioHandler. The complete flag indicates 00507 * if the enclosing begin and end should be also be written or not 00508 * 00509 * @ingroup gStorable 00510 */ 00511 template<class T> 00512 bool write(ioHandler& handler, const tpointList<T>& plst, 00513 const bool complete=true) { 00514 return plst.write(handler,complete); 00515 } 00516 00517 /** 00518 * pointList is a list of points with integer coordinates 00519 */ 00520 typedef tpointList<int> pointList; 00521 00522 } // namespace lti 00523 00524 namespace std { 00525 00526 template<class T> 00527 std::ostream& operator<<(std::ostream& s,const lti::tpointList<T>& pts) { 00528 00529 typename lti::tpointList<T>::const_iterator it,eit; 00530 if (pts.empty()) { 00531 return s; 00532 } 00533 00534 it = pts.begin(); 00535 eit = pts.end(); 00536 00537 s << *it; 00538 for (++it;it!=eit;++it) { 00539 s << " " << *it; 00540 } 00541 00542 return s; 00543 } 00544 00545 } 00546 00547 #endif