Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLMath/src/ICLMath/MatrixSubRectIterator.h ** 00010 ** Module : ICLMath ** 00011 ** Authors: Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <iterator> 00034 00035 namespace icl{ 00036 namespace math{ 00038 00068 template <typename Type> 00069 class MatrixSubRectIterator : public std::iterator<std::forward_iterator_tag,Type>{ 00070 protected: 00071 inline void init () { 00072 m_lineStep = m_matrixWidth - m_subRectWidth + 1; 00073 m_dataEnd = m_dataCurr; 00074 if (m_subRectWidth > 0){ 00075 m_dataEnd += m_subRectWidth + (m_subRectHeight-1) * m_matrixWidth; 00076 } 00077 m_currLineEnd = m_dataCurr + m_subRectWidth - 1; 00078 } 00079 00080 public: 00081 00082 static inline const MatrixSubRectIterator<Type> create_end_iterator(const Type *dataOrigin, int matrixWidth, int subRectX, 00083 int subRectY, int subRectWidth, int subRectHeight){ 00084 MatrixSubRectIterator<Type> i(const_cast<Type*>(dataOrigin),matrixWidth,subRectX, subRectY, subRectWidth,subRectHeight); 00085 i.m_dataCurr = i.m_dataEnd - subRectWidth + matrixWidth; 00086 i.m_currLineEnd = i.m_dataCurr + subRectWidth; 00087 return i; 00088 } 00089 00091 00092 inline MatrixSubRectIterator(): 00093 m_matrixWidth(0),m_subRectWidth(0),m_subRectHeight(0), 00094 m_dataOrigin(0), m_dataCurr(0) {init();} 00095 00104 inline MatrixSubRectIterator(Type *ptData,int matrixWidth,int subRectX, int subRectY, int subRectWidth, int subRectHeight): 00105 m_matrixWidth(matrixWidth),m_subRectWidth(subRectWidth),m_subRectHeight(subRectHeight), 00106 m_dataOrigin(ptData),m_dataCurr(ptData+subRectX+subRectY*matrixWidth) {init();} 00107 00108 inline MatrixSubRectIterator &operator=(const MatrixSubRectIterator &other){ 00109 m_matrixWidth = other.m_matrixWidth; 00110 m_subRectWidth = other.m_subRectWidth; 00111 m_subRectHeight = other.m_subRectHeight; 00112 m_lineStep = other.m_lineStep; 00113 m_dataOrigin = other.m_dataOrigin; 00114 m_dataCurr = other.m_dataCurr; 00115 m_dataEnd = other.m_dataEnd; 00116 m_currLineEnd = other.m_currLineEnd; 00117 return *this; 00118 } 00119 00120 inline const MatrixSubRectIterator& operator=(const MatrixSubRectIterator &other) const{ 00121 return (*const_cast<MatrixSubRectIterator*>(this)) = other; 00122 } 00123 00125 00128 inline const Type &operator*() const { return *m_dataCurr; } 00129 00131 00134 inline Type &operator*(){ return *m_dataCurr; } 00135 00137 00163 inline MatrixSubRectIterator& operator++(){ 00164 if ( ICL_UNLIKELY(m_dataCurr == m_currLineEnd) ){ 00165 m_dataCurr += m_lineStep; 00166 m_currLineEnd += m_matrixWidth; 00167 }else{ 00168 m_dataCurr++; 00169 } 00170 return *this; 00171 } 00172 00174 inline const MatrixSubRectIterator& operator++() const{ 00175 return ++(*const_cast<MatrixSubRectIterator*>(this)); 00176 } 00177 00183 inline MatrixSubRectIterator operator++(int){ 00184 MatrixSubRectIterator current (*this); 00185 ++(*this); // call prefix operator 00186 return current; // return previous 00187 } 00188 00190 inline const MatrixSubRectIterator operator++(int) const{ 00191 return (*const_cast<MatrixSubRectIterator*>(this))++; 00192 } 00193 00194 00196 00200 inline bool inSubRect() const{ 00201 return m_dataCurr < m_dataEnd; 00202 } 00203 00204 00205 00207 inline bool operator!=(const MatrixSubRectIterator<Type> &it) const{ 00208 return m_dataCurr != it.m_dataCurr; 00209 } 00211 inline bool operator==(const MatrixSubRectIterator<Type> &it) const{ 00212 return m_dataCurr == it.m_dataCurr; 00213 } 00215 inline bool operator<(const MatrixSubRectIterator<Type> &it) const{ 00216 return m_dataCurr < it.m_dataCurr; 00217 } 00219 inline bool operator>(const MatrixSubRectIterator<Type> &it) const{ 00220 return m_dataCurr > it.m_dataCurr; 00221 } 00223 inline bool operator<=(const MatrixSubRectIterator<Type> &it) const{ 00224 return m_dataCurr <= it.m_dataCurr; 00225 } 00227 inline bool operator>=(const MatrixSubRectIterator<Type> &it) const{ 00228 return m_dataCurr >= it.m_dataCurr; 00229 } 00230 00231 00233 00235 inline int getSubRectWidth() const{ 00236 return m_subRectWidth; 00237 } 00238 00239 inline int getSubRectHeight() const{ 00240 return m_subRectHeight; 00241 } 00242 00244 00248 inline void incRow(int numLines=1) const { 00249 m_dataCurr += numLines * m_matrixWidth; 00250 m_currLineEnd += numLines * m_matrixWidth; 00251 } 00252 00254 00255 inline int x(){ 00256 return (m_dataCurr-m_dataOrigin) % m_matrixWidth; 00257 } 00258 00260 00261 inline int y(){ 00262 return (m_dataCurr-m_dataOrigin) / m_matrixWidth; 00263 } 00264 00265 protected: 00267 int m_matrixWidth; 00268 00270 int m_subRectWidth, m_subRectHeight; 00271 00273 int m_lineStep; 00274 00276 Type *m_dataOrigin; 00277 00279 mutable Type *m_dataCurr; 00280 00282 Type *m_dataEnd; 00283 00285 mutable Type *m_currLineEnd; 00286 00287 }; 00288 } // namespace math 00289 }