last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2004 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * Copyright (C) 2007-2008 00005 * Pablo Alvarado 00006 * 00007 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00008 * 00009 * The CVR-Lib is free software; you can redistribute it and/or 00010 * modify it under the terms of the BSD License. 00011 * 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * 1. Redistributions of source code must retain the above copyright notice, 00018 * this list of conditions and the following disclaimer. 00019 * 00020 * 2. Redistributions in binary form must reproduce the above copyright notice, 00021 * this list of conditions and the following disclaimer in the documentation 00022 * and/or other materials provided with the distribution. 00023 * 00024 * 3. Neither the name of the authors nor the names of its contributors may be 00025 * used to endorse or promote products derived from this software without 00026 * specific prior written permission. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00029 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00030 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00031 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00032 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00033 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00034 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00035 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00036 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00037 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00038 * POSSIBILITY OF SUCH DAMAGE. 00039 */ 00040 00041 /** 00042 * \file cvrGenericMatrix.h 00043 * Contains a template class to describe matrices of data, in a way 00044 * that each row can be accessed as a cvr::genericVector 00045 * \author Pablo Alvarado 00046 * \date 09.04.1999 00047 * 00048 * $Id: cvrGenericMatrix.h,v 1.19 2007/09/15 03:36:57 alvarado Exp $ 00049 */ 00050 00051 #ifndef _CVR_GENERIC_MATRIX_H_ 00052 #define _CVR_GENERIC_MATRIX_H_ 00053 00054 #include "cvrContainer.h" 00055 #include "cvrGenericVector.h" 00056 #include "cvrPoint.h" 00057 #include "cvrRectangle.h" 00058 #include "cvrAssert.h" 00059 #include "cvrResizeType.h" 00060 #include "cvrDebugIterator.h" 00061 #include "cvrDeprecated.h" 00062 00063 namespace cvr { 00064 /** 00065 * Container class for generic matrices. 00066 * 00067 * The cvr::genericMatrix class is a container class implemented as a 00068 * template. 00069 * 00070 * The cvr::genericMatrix class allows the representation of \e n x \e m 00071 * matrices of any type that does not use any form of dynamic memory 00072 * allocation. The rows will be indexed between 0 and n-1, and the columns 00073 * between 0 and m-1. 00074 * 00075 * All types defined in cvrTypes.h use static members and can be contained 00076 * by the cvr::genericVector and cvr::genericMatrix classes. 00077 * 00078 * If you need to create a genericMatrix of floats with 20 rows and 00079 * 15 columns, all elements initialized with an initial value of 00080 * 4.27 just create it: 00081 * 00082 * \code 00083 * cvr::genericMatrix<float> myMat(20,15,4.27f) // creates genericMatrix 00084 * // with 300 elements 00085 * // all initialized with 4.27f 00086 * \endcode 00087 * 00088 * To access the cvr::genericMatrix elements use the access operators. 00089 * There are many possibilities. With at(const int row, const int 00090 * col) it is possible to access an element directly. With getRow(const 00091 * int row) you can get the row vector. You cannot for instance 00092 * resize nor change the memory referenced in this vector (see 00093 * cvr::vector::resize). For example: 00094 * 00095 * \code 00096 * float accu = 0; // initialize accumulator 00097 * cvr::genericMatrix<float> myMat(20,15,4.27f) 00098 * cvr::vector<float> myVct; 00099 * 00100 * for (int j = 0; j < myMat.rows(); j++) { 00101 * for (int i = 0; i < myMat.columns(); i++) { 00102 * tmp += myMat.at(j,i); // access each element of the genericMatrix: 00103 * // j is the row and i the column 00104 * } 00105 * } 00106 * 00107 * myMat.getRowCopy(5,myVct); // copy the sixth row in myVct! 00108 * myVct.resize(6); // Valid, the vector has its own memory! 00109 * myMat.getRow(5).resize(6) // ERROR!! the vector is not resizable! 00110 * 00111 * \endcode 00112 * 00113 * The image representation in the CVR-Lib is derived indirectly from the 00114 * cvr::genericMatrix class. It is quite confusing to use first the 00115 * y-coordinate and then the x-coordinate to access the image elements. To 00116 * avoid confusion use the cvr::ipoint class to access the elements of the 00117 * genericMatrix: 00118 * 00119 * \code 00120 * 00121 * cvr::channel8 aChannel(20,15); // creates an 8bit image 00122 * cvr::channel8::value_type tmp; // tmp is of the element type of the 00123 * // channel8! 00124 * cvr::ipoint p; 00125 * 00126 * 00127 * for (p.y = 0; p.y < aChannel.rows(); p.y++) { 00128 * for (p.x = 0; p.x < aChannel.columns(); p.x++) { 00129 * tmp += aChannel.at(p); // access each element of the genericMatrix: 00130 * // equivalent to: tmp += aChannel.at(p.y,p.x)! 00131 * } 00132 * } 00133 * 00134 * \endcode 00135 * 00136 * Matrices in the CVR-Lib use always a continuous block of memory, which 00137 * allows efficient elementwise operations. The previous version of the 00138 * library (LTI-Lib) provided a lined-mode of the matrices, which is now 00139 * contained in its own class cvr::genericSubmatrix. 00140 * 00141 * @see cvr::genericSubmatrix, cvr::matrix 00142 * 00143 * The genericMatrix has following methods: 00144 * - Constructors Constructors 00145 * - You can construct an empty genericMatrix with the default constructor 00146 * (genericMatrix()). 00147 * - If you know the number of rows and columns use 00148 * genericMatrix(const int rows,const int columns,const T& 00149 * initialValue) 00150 * - Access members 00151 * - at(), operator[]() 00152 * - The rows() member returns the number of rows of the genericMatrix. 00153 * - The columns() member returns the number of columns of the 00154 genericMatrix. 00155 * - Fill and Copy members 00156 * - With the fill() members you can fill the genericMatrix with a given 00157 * constant value or with values taken from other matrices. 00158 * - With the copy() member you can copy another genericMatrix. 00159 * - You can specify, that the genericMatrix should be used just as a 00160 * wrapper-object to access external memory regions: useExternData(). 00161 * To check if a genericMatrix is a wrapper-object you can use 00162 * ownsData(). 00163 * - Iterators 00164 * - It is possible to iterate within the genericMatrix by making use of 00165 * the genericMatrix iterators. (see begin() for more information) 00166 * - Instead of reverse_iterators as in the STL we use iterators 00167 * going backwards, due to faster execution times (see 00168 * inverseBegin() for more information) 00169 * 00170 * @ingroup gLinearAlgebra 00171 * @ingroup gAggregate 00172 */ 00173 template<class T> 00174 class genericMatrix : public container { 00175 public: 00176 00177 /** 00178 * \name Internal types and classes 00179 */ 00180 //@{ 00181 00182 /** 00183 * Exception thrown when a constant reference is violated. 00184 */ 00185 typedef cvr::constReferenceException constReferenceException; 00186 00187 /** 00188 * Type of the contained data 00189 */ 00190 typedef T value_type; 00191 00192 /** 00193 * Return type of the size() member 00194 */ 00195 typedef ipoint size_type; 00196 00197 /** 00198 * Vector type of each row 00199 */ 00200 typedef genericVector<T> row_type; 00201 00202 # ifdef NDEBUG 00203 /** 00204 * Iterator type (allows read and write operations) 00205 * 00206 * The use of the iterator classes is similar to the iterators of 00207 * the STL (Standard Template Library). See cvr::genericMatrix::begin() 00208 * for an example . 00209 * 00210 * For the debugging version of the iterators, boundary check will be 00211 * done! This explains the low speed of the iterators of the debug 00212 * version. In the release version, no boundary check will be done, 00213 * and the iterators are sometimes a factor 10 faster than the 00214 * debug iterators. 00215 * 00216 * The use of the access operator at() is faster than the iterators in the 00217 * debug version only. If you need to iterate on a genericMatrix use 00218 * iterators instead (in the release version iterators are approximately a 00219 * factor 3 faster than "at()"). 00220 * 00221 * \warning Try to use the prefix incremental operator (i.e. ++it) over the 00222 * postfix operator (i.e. it++) to allow efficient code also in 00223 * debug-modus! 00224 */ 00225 typedef T* iterator; 00226 00227 /** 00228 * Constant iterator type (allows read-only operations) 00229 * 00230 * The use of the iterator classes is similar to the iterators of 00231 * the STL (Standard Template Library). See cvr::genericMatrix::begin() 00232 * for an example. 00233 * 00234 * For the debugging version of the iterators, boundary check will be 00235 * done! This explains the low speed of the iterators of the debug 00236 * version. In the release version, no boundary check will be done, 00237 * and the iterators are sometimes a factor 10 faster than the 00238 * debug iterators. 00239 * 00240 * The use of the access operator at() is faster than the iterators in the 00241 * debug version only. If you need to iterate on a genericMatrix use 00242 * iterators instead (in the release version iterators are approximately a 00243 * factor 3 faster than "at()"). 00244 * 00245 * \warning Try to use the prefix incremental operator (i.e. ++it) instead 00246 * of the postfix operator (i.e. it++) to allow efficient code also in 00247 * debug-modus! 00248 */ 00249 typedef const T* const_iterator; 00250 00251 # else 00252 00253 /** 00254 * Iterator type (allows read and write operations) 00255 * 00256 * The use of the iterator classes is similar to the iterators of 00257 * the STL (Standard Template Library). See cvr::genericMatrix::begin() 00258 * for an example . 00259 * 00260 * For the debugging version of the iterators, boundary check will be 00261 * done! This explains the low speed of the iterators of the debug 00262 * version. In the release version, no boundary check will be done, 00263 * and the iterators are sometimes a factor 10 faster than the 00264 * debug iterators. 00265 * 00266 * The use of the access operator at() is faster than the iterators in the 00267 * debug version only. If you need to iterate on a genericMatrix use 00268 * iterators instead (in the release version iterators are approximately a 00269 * factor 3 faster than "at()"). 00270 * 00271 * \warning Try to use the prefix incremental operator (i.e. ++it) over 00272 * the postfix operator (i.e. it++) to allow efficient code also in 00273 * debug-modus! 00274 */ 00275 typedef internal::debugIterator<genericMatrix<T>,false> iterator; 00276 00277 /** 00278 * Constant iterator type (allows read-only operations) 00279 * 00280 * The use of the iterator classes is similar to the iterators of 00281 * the STL (Standard Template Library). See cvr::genericMatrix::begin() 00282 * for an example. 00283 * 00284 * For the debugging version of the iterators, boundary check will be 00285 * done! This explains the low speed of the iterators of the debug 00286 * version. In the release version, no boundary check will be done, 00287 * and the iterators are sometimes a factor 10 faster than the 00288 * debug iterators. 00289 * 00290 * The use of the access operator at() is faster than the iterators in the 00291 * debug version only. If you need to iterate on a genericMatrix use 00292 * iterators instead (in the release version iterators are approximately a 00293 * factor 3 faster than "at()"). 00294 * 00295 * \warning Try to use the prefix incremental operator (i.e. ++it) instead 00296 * of the postfix operator (i.e. it++) to allow efficient code also in 00297 * debug-modus! 00298 */ 00299 typedef internal::debugIterator<genericMatrix<T>,true> const_iterator; 00300 # endif 00301 //@} 00302 00303 /** 00304 * Default constructor creates an empty genericMatrix 00305 */ 00306 genericMatrix(); 00307 00308 /** 00309 * Create a \a rows x \a cols genericMatrix with uninitialized elements. 00310 * 00311 * \warning This is an interface change with the previous library. 00312 * It has been done to be consistent with the more basic features 00313 * of the C++ language. If you write for example \c int \c c; , 00314 * the content of \c c is not defined, and in the same way, if you 00315 * want a vector with initialized data, you have to specify 00316 * explicitely the value with which the elements have to be 00317 * initialized. 00318 * 00319 * @param rows number of rows of the genericMatrix 00320 * @param cols number of columns of the genericMatrix 00321 */ 00322 genericMatrix(const int rows,const int cols); 00323 00324 /** 00325 * Create a \a size.x x \a size.y genericMatrix with 00326 * uninitialized elements. 00327 * 00328 * \warning This is an interface change with the previous library. 00329 * It has been done to be consistent with the more basic features 00330 * of the C++ language. If you write for example \c int \c c; , 00331 * the content of \c c is not defined, and in the same way, if you 00332 * want a vector with initialized data, you have to specify 00333 * explicitely the value with which the elements have to be 00334 * initialized. 00335 * 00336 * @param size size of the matrix (size.x columns and size.y rows) 00337 */ 00338 genericMatrix(const size_type& size); 00339 00340 /** 00341 * Create a \a rows x \a cols genericMatrix and 00342 * initializes all elements with \a iniValue. 00343 * 00344 * @param rows number of rows of the genericMatrix 00345 * @param cols number of columns of the genericMatrix 00346 * @param iniValue all elements will be initialized with this value 00347 */ 00348 genericMatrix(const int rows,const int cols,const T& iniValue); 00349 00350 /** 00351 * This constructor creates a \a size.y x \a size.x 00352 * GenericMatrix and initializes all elements with \a iniValue 00353 * 00354 * @param size the size of the genericMatrix (size.x is the number of 00355 * columns and size.y the number of rows) 00356 * @param iniValue all elements will be initialized with this value 00357 */ 00358 genericMatrix(const size_type& size,const T& iniValue); 00359 00360 /** 00361 * Create a \a rows x \a cols genericMatrix and initialize all 00362 * elements with the data pointed by \a data. 00363 * 00364 * The first \a cols-elements of the data will be \b copied on the 00365 * first row, the next ones on the second row and so on. 00366 * 00367 * @param rows number of rows of the genericMatrix 00368 * @param cols number of columns of the genericMatrix 00369 * @param data pointer to the memory block with the data to be initialized 00370 * with. 00371 */ 00372 genericMatrix(const int rows,const int cols,const T data[]); 00373 00374 /** 00375 * Create a \a dim.y x \a dim.x genericMatrix and initialize all 00376 * elements with the data pointed by \a data. 00377 * 00378 * The first \a dim.x -elements of the data will be \b copied on the 00379 * first row, the next ones on the second row and so on. 00380 * 00381 * @param dim matrix dimensions 00382 * @param data pointer to the memory block with the data to be initialized 00383 * with. 00384 */ 00385 genericMatrix(const size_type& dim,const T data[]); 00386 00387 /** 00388 * Create a \a rows x \a cols genericMatrix and use the given \a data as 00389 * if it was given through the useExternData(). 00390 * 00391 * @see useExternData() 00392 * 00393 * The first \a cols-elements of the data will be copied on the 00394 * first row, the next ones on the second row and so on. 00395 * 00396 * \note Please observe that the third argument makes a huge difference to 00397 * the constructors that do not have it. Here the data is \b not 00398 * copied, just a reference is kept. Without the third argument, the 00399 * data is always copied. 00400 * 00401 * @param rows number of rows of the genericMatrix 00402 * @param cols number of columns of the genericMatrix 00403 * @param data pointer to the memory block with the data to be initialized 00404 * with. 00405 * @param constRef usually you want this to be \c ConstantReference 00406 */ 00407 genericMatrix(const int rows,const int cols, 00408 T data[], 00409 const eConstantReference constRef); 00410 00411 00412 /** 00413 * Create a \a rows x \a cols genericMatrix and use the given \a data as 00414 * if it was given through the useExternData(). 00415 * 00416 * @see useExternData() 00417 * 00418 * The first \a cols-elements of the data will be copied on the 00419 * first row, the next ones on the second row and so on. 00420 * 00421 * \note Please observe that the third argument makes a huge difference to 00422 * the constructors that do not have it. Here the data is \b not 00423 * copied, just a reference is kept. Without the third argument, the 00424 * data is always copied. 00425 * 00426 * @param theSize size of the matrix 00427 * @param data pointer to the memory block with the data to be initialized 00428 * with. 00429 * @param constRef usually you want this to be \c ConstantReference 00430 */ 00431 genericMatrix(const size_type& theSize, 00432 T data[], 00433 const eConstantReference constRef); 00434 00435 /** 00436 * Copy constructor. 00437 * 00438 * Create this genericMatrix as a copy of another genericMatrix. 00439 * 00440 * @param other the genericMatrix to be copied. 00441 */ 00442 genericMatrix(const genericMatrix<T>& other); 00443 00444 00445 /** 00446 * Copy constructor. 00447 * 00448 * Create this genericMatrix as a copy of another genericMatrix for this 00449 * const version, the data will be always copied! 00450 * 00451 * @param other the genericMatrix to be copied. 00452 * @param fromRow initial row of the other genericMatrix to be copied 00453 * @param fromCol initial column of the other genericMatrix to be copied 00454 * @param toRow last row to be copied of the other genericMatrix 00455 * @param toCol last column to be copied of the other genericMatrix 00456 * 00457 * Example: 00458 * \code 00459 * cvr::genericMatrix<int> m(4,6,0); // integer genericMatrix with 25 00460 * // elements 00461 * // ... 00462 * // initialize GenericMatrix with: 00463 * // 0 1 2 3 4 5 00464 * // 2 1 5 4 0 3 00465 * // 1 2 1 2 3 2 00466 * // 3 3 2 1 2 3 00467 * 00468 * cvr::genericMatrix<int> sm(m,0,2,1,3) // last line will lead to 00469 * // following contents in sm: 00470 * // 1 2 3 00471 * // 1 5 4 00472 * // 2 1 2 00473 * \endcode 00474 */ 00475 genericMatrix(const genericMatrix<T>& other, 00476 const int fromRow, 00477 const int fromCol, 00478 const int toRow=MaxIndex, 00479 const int toCol=MaxIndex); 00480 00481 00482 /** 00483 * Copy constructor. 00484 * 00485 * Create this genericMatrix as a copy of a submatrix of another 00486 * genericMatrix. 00487 * 00488 * @param other the genericMatrix to be copied. 00489 * @param from initial position in the other genericMatrix to be copied 00490 * @param to last position to be copied of the other genericMatrix 00491 * 00492 * Example: 00493 * \code 00494 * cvr::genericMatrix<int> m(4,6,0); // integer genericMatrix with 25 00495 * // elements 00496 * // ... 00497 * // initialize GenericMatrix with: 00498 * // 0 1 2 3 4 5 00499 * // 2 1 5 4 0 3 00500 * // 1 2 1 2 3 2 00501 * // 3 3 2 1 2 3 00502 * 00503 * cvr::genericMatrix<int> sm(m,0,2,1,3) // last line will lead to 00504 * // following contents in sm: 00505 * // 1 2 3 00506 * // 1 5 4 00507 * // 2 1 2 00508 * \endcode 00509 */ 00510 genericMatrix(const genericMatrix<T>& other, 00511 const size_type& from, 00512 const size_type& to); 00513 00514 00515 /** 00516 * Copy constructor. 00517 * 00518 * Create this genericMatrix as a copy of another genericMatrix taking only 00519 * the rows indicated by the vector. 00520 * 00521 * The data will be always copied! Multiple occurence of one row index in 00522 * \a rows is allowed. 00523 * 00524 * @param other the genericMatrix to be copied. 00525 * @param rows indices of the rows to be copied 00526 * 00527 * Example: 00528 * \code 00529 * cvr::vector<int> rows(2); 00530 * // initialize with 00531 * // 1 3 00532 * cvr::genericMatrix<int> m(4,6,0); // integer genericMatrix with 25 00533 * // elements 00534 * // ... 00535 * // initialize GenericMatrix with: 00536 * // 0 1 2 3 4 5 00537 * // 2 1 5 4 0 3 00538 * // 1 2 1 2 3 2 00539 * // 3 3 2 1 2 3 00540 * 00541 * cvr::genericMatrix<int> sm(m,rows) // last line will lead to 00542 * // following contents in sm: 00543 * // 2 1 5 4 0 3 00544 * // 3 3 2 1 2 3 00545 * \endcode 00546 */ 00547 genericMatrix(const genericMatrix<T>& other, 00548 const genericVector<int>& rows); 00549 00550 /** 00551 * Destructor 00552 */ 00553 virtual ~genericMatrix(); 00554 00555 /** 00556 * Owns this %object the data? 00557 * returns <em>false</em> if this genericMatrix contains a reference to 00558 * extern data. 00559 */ 00560 inline bool ownsData() const; 00561 00562 /** 00563 * If this object does not own its data, this member will create a new 00564 * memory buffer with the same data and will make this genericMatrix as its 00565 * owner. If this genericMatrix already owns its data nothing happens. 00566 */ 00567 void restoreOwnership(); 00568 00569 /** 00570 * Reference to extern data. 00571 * 00572 * This member allows the use of this %object as an access-functor for 00573 * the 'data'. An access to the element at (r,c) is equivalent to 00574 * data[r*columns() + c]. 00575 * 00576 * The user must take care for memory allocation and deallocation: 00577 * this object will never delete the given data!. 00578 * 00579 * If <em>rows</em> and <em>cols</em> are invalid dimensions, the 00580 * behaviour will be unpredictible. 00581 * 00582 * In case the provided data is the same than the one being used 00583 * by this matrix, and additionally the provided rows and columns 00584 * coincide with the dimensions in use, nothing will be changed, 00585 * not even the ownership of the data. 00586 * 00587 * @param rows number of rows 00588 * @param cols number of columns 00589 * @param data a pointer to the memory block to be used 00590 * @param constRef usually you want this to be \c ConstantReference 00591 * 00592 * For an example see attach() 00593 */ 00594 void useExternData(const int rows,const int cols, 00595 T data[], 00596 const eConstantReference constRef=VariableReference); 00597 00598 /** 00599 * Reference to extern data. 00600 * 00601 * This member allows the use of this %object as an access-functor for 00602 * the 'data'. An access to the element at (r,c) is equivalent to 00603 * data[r*columns() + c]. 00604 * The user must take care for memory allocation and deallocation: 00605 * this object will never delete the data!. 00606 * If <em>rows</em> and <em>cols</em> are invalid dimensions, the 00607 * behaviour will be unpredictible. 00608 * 00609 * @param theSize size of the matrix 00610 * @param data a pointer to the memory block to be used 00611 * @param constRef usually you want this to be \c ConstantReference 00612 * 00613 * For an example see attach() 00614 */ 00615 inline void 00616 useExternData(const size_type& theSize, 00617 T data[], 00618 const eConstantReference constRef=VariableReference); 00619 00620 /** 00621 * Attach extern data to the genericMatrix. 00622 * 00623 * This member allows the use of this %object as an access-functor for 00624 * the 'data'. An access to the element at (r,c) is equivalent to 00625 * data[r*columns() + c]. 00626 * If <em>rows</em> and <em>cols</em> are invalid dimensions, the 00627 * behaviour will be unpredictible. 00628 * 00629 * The memory will be administrated by this genericMatrix object, 00630 * and may be deleted if required (genericMatrix deleted or 00631 * resized!). The user should not try to manipulate the memory 00632 * allocation of the data after the attachment! See also 00633 * useExternData(). 00634 * 00635 * @param rows number of rows 00636 * @param cols number of columns 00637 * @param data a pointer to the memory block to be used 00638 * 00639 * Example: 00640 * \code 00641 * cvr::genericMatrix<int> myMat; 00642 * int block1[25]; 00643 * int* block2; 00644 * block2 = new int[25]; 00645 * 00646 * myMat.useExternData(5,5,block1); // ok 00647 * myMat.attach(5,5,block1); // wrong!!! genericMatrix will try 00648 * // to manipulate stack memory: 00649 * // DO NOT DO THIS!!!!! 00650 * myMat.attach(5,5,block2); // ok! but do not try to delete the memory 00651 * // block2!! 00652 * \endcode 00653 */ 00654 void attach(const int rows,const int cols,T* data); 00655 00656 /** 00657 * Attach extern data to the genericMatrix. 00658 * 00659 * This member allows the use of this %object as an access-functor for 00660 * the 'data'. An access to the element at (r,c) is equivalent to 00661 * data[r*columns() + c]. 00662 * If <em>rows</em> and <em>cols</em> are invalid dimensions, the 00663 * behaviour will be unpredictible. 00664 * 00665 * The memory will be administrated by this genericMatrix object, 00666 * and may be deleted if required (genericMatrix deleted or 00667 * resized!). The user should not try to manipulate the memory 00668 * allocation of the data after the attachment! See also 00669 * useExternData(). 00670 * 00671 * @param theSize \c x number of columns and \c y number of rows 00672 * @param data a pointer to the memory block to be used 00673 * 00674 * Example: 00675 * \code 00676 * cvr::genericMatrix<int> myMat; 00677 * int block1[25]; 00678 * int* block2; 00679 * block2 = new int[25]; 00680 * ipoint p(5,5); 00681 * myMat.useExternData(p,block1); // ok 00682 * myMat.attach(p,block1); // wrong!!! genericMatrix will try 00683 * // to manipulate stack memory: 00684 * // DO NOT DO THIS!!!!! 00685 * myMat.attach(p,block2); // ok! but do not try to delete the memory 00686 * // block2!! 00687 * \endcode 00688 */ 00689 inline void attach(const size_type& theSize,T* data); 00690 00691 /** 00692 * Free the data of this object and hand it over to the 00693 * "receiver". The value of ownsData is also transfered to the 00694 * receiver. (see Note). 00695 * 00696 * This function makes a "memory block transfusion" to another 00697 * genericMatrix. It is a very efficient way to make a copy of 00698 * this genericMatrix, if you don't need the source data anymore! 00699 * 00700 * If the current matrix does not own its data, neither will the receiver 00701 * matrix, which will also be just a wrapper of the same data. 00702 * 00703 * If the current matrix is lined, then the other matrix will end as an 00704 * exact copy if this one, and this one will be emptied after the 00705 * detachment. In other words, it works similar like a swap() followed by 00706 * clear(). In extremely time critical situations you may want to use 00707 * swap() as it does not need to deallocate any memory of the receiver and 00708 * is therefore a little bit faster. 00709 * 00710 * Please take special care to the memory management issues involved if the 00711 * current object does not own its data, as its control will still be 00712 * yours. 00713 * 00714 * At the end of the detachment, this genericMatrix will be empty. 00715 * 00716 * @param receiver the genericMatrix which will receive the memory 00717 * block. All data of that genericMatrix will be first deleted! 00718 */ 00719 void detach(genericMatrix<T>& receiver); 00720 00721 /** 00722 * Free the data of this object and hand it over to the 00723 * "receiver". The value of ownsData is also transfered to the 00724 * receiver. (see Note). 00725 * 00726 * This function makes a "memory block transfusion" to a vector by 00727 * concatenating the rows of the genericMatrix. It is a very efficient 00728 * way to move the data of this genericMatrix into a vector, if you don't 00729 * need the source data anymore! 00730 * 00731 * \b Note: If the attach() or useExternData() methods of this 00732 * genericMatrix have been called before detachment, the same rules for 00733 * memory management apply now for the \c receiver. 00734 * 00735 * At the end of the detachment, this genericMatrix will be empty. 00736 * 00737 * @param receiver the genericMatrix which will receive the memory block. 00738 * All data of that genericMatrix will be first deleted! 00739 */ 00740 void detach(genericVector<T>& receiver); 00741 00742 /** 00743 * Exchange (in a fast way) the data between this and the other 00744 * genericMatrix. 00745 * 00746 * Similar to detach(), this method will exchange the complete memory 00747 * blocks, avoiding an element-wise copy. 00748 * @param other the genericMatrix with which the data will be exchanged. 00749 */ 00750 void swap(genericMatrix<T>& other); 00751 00752 /** 00753 * Number of rows of the genericMatrix 00754 */ 00755 inline int rows() const; 00756 00757 /** 00758 * Number of columns of the genericMatrix 00759 */ 00760 inline int columns() const; 00761 00762 /** 00763 * Index of the last row (rows()-1) 00764 */ 00765 inline int lastRow() const; 00766 00767 /** 00768 * Index of the last columns (columns()-1) 00769 */ 00770 inline int lastColumn() const; 00771 00772 /** 00773 * Returns the size of the %genericMatrix in a cvr::ipoint structure. 00774 * 00775 * @return cvr::ipoint with the number of columns in its 00776 * \a x coordinate and the number of rows in its 00777 * \a y coordinate. 00778 */ 00779 inline const size_type& size() const; 00780 00781 /** 00782 * Returns a read-only reference to the last index of the matrix 00783 * 00784 * @return cvr::ipoint with the number of columns in its 00785 * \a x coordinate and the number of rows in its 00786 * \a y coordinate. 00787 */ 00788 inline const size_type& lastIndex() const; 00789 00790 /** 00791 * Return iterator to the begin of the genericMatrix. 00792 * 00793 * The use of the interators is similar to the iterators of the 00794 * Standard Template Library (STL). 00795 * If you need to iterate on all elements of the genericMatrix, you can 00796 * use following code: 00797 * \code 00798 * int tmp,accu; // a temporal variable 00799 * cvr::genericMatrix<int> myMat(10,8,1); // a vector with 10 elements 00800 * cvr::genericMatrix<int>::iterator it; // an iterator 00801 * 00802 * for (it=myMat.begin();it!=myMat.end();it++) { 00803 * tmp = *it; // tmp has value of element pointed 00804 * // by the iterator. 00805 * accu += tmp; 00806 * (*it) = accu; // change the value in the genericMatrix. 00807 * } 00808 * \endcode 00809 * Please note that if you define \c it as a const_iterator, 00810 * you can not do something like <code>*it=accu</code>. 00811 */ 00812 inline iterator begin(); 00813 00814 /** 00815 * Return first element of the genericMatrix as a const_iterator. 00816 * 00817 * Note that you can not change the values of the genericMatrix 00818 * elements when you use a const_iterator. See also begin() for an example 00819 */ 00820 inline const_iterator begin() const; 00821 00822 /** 00823 * returns iterator to the end of the genericMatrix 00824 */ 00825 inline iterator end(); 00826 00827 /** 00828 * returns iterator to the end of the genericMatrix 00829 */ 00830 inline const_iterator end() const; 00831 00832 00833 /** 00834 * This method returns an iterator that points to the \b last 00835 * valid element of the genericMatrix. It is used for inverse order 00836 * iteration through the genericMatrix using normal iterators (as opposed 00837 * to reverse iterators). This has the advantage that iterators 00838 * going from front to end and in the inverse direction are the 00839 * same and can thus be compared, copied etc. Further the 00840 * implementation of reverse_iterators is not as fast as that of 00841 * iterators and thus not desired in the CVR-Lib. 00842 * 00843 * See cvr::genericVector<T>::inverseBegin() for an example. 00844 */ 00845 inline iterator inverseBegin(); 00846 00847 /** 00848 * This method returns an iterator that points to the \b last 00849 * valid element of the genericMatrix. See inverseBegin() for more details. 00850 */ 00851 inline const_iterator inverseBegin() const; 00852 00853 /** 00854 * This method returns an iterator that points to the element \b 00855 * before the \b first valid element of the genericMatrix. It is used to 00856 * mark the end for inverse order iteration through the genericMatrix 00857 * using normal iterators (as opposed to reverse iterators). This 00858 * has the advantage that iterators going from front to end and in 00859 * the inverse direction are the same and can thus be compared, 00860 * copied etc. 00861 */ 00862 inline iterator inverseEnd(); 00863 00864 /** 00865 * This method returns an iterator that points to the element \b 00866 * before the \b first valid element of the genericMatrix. 00867 */ 00868 inline const_iterator inverseEnd() const; 00869 00870 /** 00871 * Change the dimensions of the genericMatrix. 00872 * 00873 * @param newRows new number of rows 00874 * @param newCols new number of columns 00875 * @param iniValue the initialization value. 00876 * @param resizeType specifies what should happen with the data of the 00877 * resized matrix. 00878 * 00879 * For example: 00880 * \code 00881 * cvr::genericMatrix<int> myMat; // creates empty genericMatrix 00882 * myMat.resize(5,5,0); // genericMatrix with 5x5 elements 00883 * // initialized with 0 00884 * myMat.resize(10,7,2); // genericMatrix has now 10x7 elements: the 00885 * // submatrix 5x5 at (0,0) has still 0s 00886 * // and the rest have a 2 00887 * myMat.resize(20,10,0,cvr::AllocateOnly); // now the genericMatrix has 00888 * // 20 elements but their 00889 * // values are unknown. 00890 * myMat.resize(5,5,1,Copy); // the genericMatrix has now 5x5 00891 * // elements all initialized with 1 00892 * \endcode 00893 * 00894 * If the resize is possible (see useExternData()), this %object 00895 * will always owns the data! 00896 * 00897 * @see eResizeType 00898 */ 00899 void resize(const int newRows,const int newCols, 00900 const T& iniValue, 00901 const eResizeType resizeType=CopyAndInit); 00902 00903 /** 00904 * Change the dimensions of the genericMatrix. 00905 * 00906 * @param newDim new dimensions of the genericMatrix 00907 * @param iniValue the initialization value. 00908 * @param resizeType specifies what should happen with the data of the 00909 * resized matrix. 00910 * 00911 * If the resize is possible (see useExternData()), this %object 00912 * will always own the data! 00913 * 00914 * This is equivalent to call 00915 * resize(newDim.y,newDim.x,iniValue,resizeType) 00916 * 00917 * @see eResizeType 00918 */ 00919 inline void resize(const size_type& newDim, 00920 const T& iniValue, 00921 const eResizeType resizeType=CopyAndInit); 00922 00923 /** 00924 * Change the dimensions of the genericMatrix. 00925 * 00926 * @param newRows new number of rows 00927 * @param newCols new number of columns 00928 * 00929 * The old values will be copied, but the new data will be kept 00930 * uninitialized. In principle this is an alias for 00931 * \c resize(newRows,newCols,T(),Copy). 00932 * 00933 * If the resize is possible (see useExternData()), this %object 00934 * will always own the data! 00935 */ 00936 inline void resize(const int newRows,const int newCols); 00937 00938 /** 00939 * Change the dimensions of the genericMatrix. 00940 * 00941 * @param newSize new size of the matrix. 00942 * 00943 * The old values will be copied, but the new data will be kept 00944 * uninitialized. In principle this is an alias for 00945 * \c resize(newRows,newCols,T(),Copy). 00946 * 00947 * If the resize is possible (see useExternData()), this %object 00948 * will always own the data! 00949 */ 00950 inline void resize(const size_type& newSize); 00951 00952 /** 00953 * Change the dimensions of the genericMatrix and leave ALL data 00954 * uninitialized. 00955 * 00956 * @param newRows new number of rows 00957 * @param newCols new number of columns 00958 * 00959 * The old values will be discarded and the new data will be kept 00960 * uninitialized. In principle this is an alias for 00961 * \c resize(newRows,newCols,T(),AllocateOnly). 00962 * 00963 * If the resize is possible (see useExternData()), this %object 00964 * will always own the data! 00965 */ 00966 inline void allocate(const int newRows,const int newCols); 00967 00968 /** 00969 * Change the dimensions of the genericMatrix and leave ALL data 00970 * uninitialized. 00971 * 00972 * @param newSize new size of the matrix. 00973 * 00974 * The old values will be discarded and the new data will be kept 00975 * uninitialized. In principle this is an alias for 00976 * \c resize(newSize.y, newSize.x, T(), AllocateOnly). 00977 * 00978 * If the resize is possible (see useExternData()), this %object 00979 * will always own the data! 00980 */ 00981 inline void allocate(const size_type& newSize); 00982 00983 /** 00984 * Change the dimensions of the genericMatrix and initialize ALL data 00985 * with the given value. 00986 * 00987 * The old values will be discarded and all data will be initialized with 00988 * initValue. In principle this is an alias for \c 00989 * resize(newRows, newCols, initValue, Init). 00990 * 00991 * If the resize is possible (see useExternData()), this %object 00992 * will always own the data! 00993 * 00994 * @param newRows new number of rows 00995 * @param newCols new number of columns 00996 * @param initValue value to be assigned to all matrix elements 00997 */ 00998 inline void assign(const int newRows, 00999 const int newCols, 01000 const T& initValue); 01001 01002 /** 01003 * Change the dimensions of the genericMatrix and initialize ALL data 01004 * with the given value. 01005 * 01006 * The old values will be discarded and all data will be initialized with 01007 * initValue. In principle this is an alias for \c 01008 * resize(newSize.y, newSize.x, initValue, Init). 01009 * 01010 * If the resize is possible (see useExternData()), this %object 01011 * will always own the data! 01012 * 01013 * @param newSize new size of the matrix. 01014 * @param initValue value to be assigned to all matrix elements 01015 */ 01016 inline void assign(const size_type& newSize, 01017 const T& initValue); 01018 01019 /** 01020 * Clears the genericMatrix (at the end this will be an empty 01021 * genericMatrix) 01022 */ 01023 void clear(); 01024 01025 /** 01026 * Returns true if the genericMatrix is empty 01027 */ 01028 inline bool empty() const; 01029 01030 /** 01031 * Fills genericMatrix elements with \a iniValue. 01032 * The fill "area" is limited by \a fromCol,\a toCol, 01033 * \a fromRow and \a toRow. 01034 * If these values are out of bounds, they will be (internally) 01035 * adjusted to correct values. 01036 * 01037 * @param iniValue the elements will be initialized with this value 01038 * @param fromRow first row of the submatrix to be filled 01039 * @param fromCol first column of the submatrix to be filled 01040 * @param toRow last row of the submatrix to be filled 01041 * @param toCol last column of the submatrix to be filled 01042 */ 01043 void fill(const T& iniValue, 01044 const int fromRow=0, 01045 const int fromCol=0, 01046 const int toRow=MaxIndex, 01047 const int toCol=MaxIndex); 01048 01049 /** 01050 * Fills genericMatrix elements with \a iniValue. 01051 * The fill "area" is limited by \a from and \a to 01052 * points 01053 * If these values are out of bounds, they will be (internally) 01054 * adjusted to correct values. 01055 * 01056 * @param iniValue the elements will be initialized with this value 01057 * @param from first position of the submatrix to be filled 01058 * @param to last row of the submatrix to be filled 01059 */ 01060 inline void fill(const T& iniValue, 01061 const size_type& from, 01062 const size_type& to=size_type(MaxIndex,MaxIndex)); 01063 01064 /** 01065 * Fills genericMatrix elements with \a iniValue. 01066 * The fill "area" is limited by \a window. 01067 * If these values are out of bounds, they will be (internally) 01068 * adjusted to correct values. 01069 * @param iniValue the elements will be initialized with this value 01070 * @param window the window to be filled 01071 */ 01072 inline void fill(const T& iniValue,const irectangle& window); 01073 01074 /** 01075 * Fills genericMatrix elements with the data pointed by \a data. 01076 * The fill "area" is limited by \a fromCol,\a toCol, 01077 * \a fromRow and \a toRow. 01078 * If these values are out of bounds, they will be (internally) 01079 * adjusted to correct values. 01080 * 01081 * @param data pointer to the data to be copied. 01082 * @param fromRow first row of the submatrix to be filled 01083 * @param fromCol first column of the submatrix to be filled 01084 * @param toRow last row of the submatrix to be filled 01085 * @param toCol last column of the submatrix to be filled 01086 */ 01087 void fill(const T data[], 01088 const int fromRow=0, 01089 const int fromCol=0, 01090 const int toRow=MaxIndex, 01091 const int toCol=MaxIndex); 01092 01093 /** 01094 * Fills genericMatrix elements with the data pointed by \a data. 01095 * The fill "area" is limited by \a fromCol,\a toCol, 01096 * \a fromRow and \a toRow. 01097 * If these values are out of bounds, they will be (internally) 01098 * adjusted to correct values. 01099 * 01100 * @param data pointer to the data to be copied. 01101 * @param from first position of the submatrix to be filled 01102 * @param to last position of the submatrix to be filled 01103 */ 01104 inline void fill(const T data[], 01105 const size_type& from, 01106 const size_type& to=size_type(MaxIndex,MaxIndex)); 01107 01108 /** 01109 * Fills genericMatrix elements with \a iniValue. 01110 * 01111 * The fill "area" is limited by \a window. 01112 * 01113 * If these values are out of bounds, they will be (internally) adjusted to 01114 * correct values. 01115 * 01116 * @param data pointer to the data to be copied 01117 * @param window the window to be filled 01118 */ 01119 inline void fill(const T data[],const irectangle& window); 01120 01121 /** 01122 * Fills this genericMatrix between the "from's" and "to's" with 01123 * the contents of the genericMatrix \a mat starting at 01124 * \a startAtRow and \a startAtCol 01125 * 01126 * @param mat the genericMatrix with the data to be copied 01127 * @param fromRow first row of the submatrix to be filled 01128 * @param fromCol first column of the submatrix to be filled 01129 * @param toRow last row of the submatrix to be filled 01130 * @param toCol last column of the submatrix to be filled 01131 * @param startAtRow starting row of \a mat where the data is 01132 * located. 01133 * @param startAtCol starting column of \a mat where the data 01134 * is located. 01135 */ 01136 void fill(const genericMatrix<T>& mat, 01137 const int fromRow=0, 01138 const int fromCol=0, 01139 const int toRow=MaxIndex, 01140 const int toCol=MaxIndex, 01141 const int startAtRow=0, 01142 const int startAtCol=0); 01143 01144 /** 01145 * Fills this genericMatrix between the "from's" and "to's" with 01146 * the contents of the genericMatrix \a mat starting at 01147 * \a startAtRow and \a startAtCol 01148 * 01149 * @param mat the genericMatrix with the data to be copied 01150 * @param from first position of the submatrix to be filled 01151 * @param to last position of the submatrix to be filled 01152 * @param startAt starting position of \a mat where the data is 01153 * located. 01154 */ 01155 inline void fill(const genericMatrix<T>& mat, 01156 const size_type& from, 01157 const size_type& to=size_type(MaxIndex,MaxIndex), 01158 const size_type& startAt=size_type(0,0)); 01159 01160 /** 01161 * Fills the region of this genericMatrix specified by \a window with the 01162 * contents of the genericMatrix \a mat starting at \a start. If these 01163 * values are out of bounds, they will be (internally) adjusted to correct 01164 * values. 01165 * 01166 * @param mat pointer to the data to be copied 01167 * @param window the window to be filled 01168 * @param start the start position of the region to be copied of the 01169 * genericMatrix \a mat 01170 */ 01171 inline void fill(const genericMatrix<T>& mat, 01172 const irectangle& window, 01173 const size_type& start=size_type(0,0)); 01174 01175 /** 01176 * Access element at the given row and column 01177 * 01178 * @param row the row of the element 01179 * @param col the column of the element 01180 * @return a reference to the genericMatrix element 01181 */ 01182 inline T& at(const int row, const int col); 01183 01184 /** 01185 * Read-only access at the given row and column 01186 * 01187 * @param row the row of the element 01188 * @param col the column of the element 01189 * @return a reference to the genericMatrix element 01190 */ 01191 inline const T& at(const int row, const int col) const; 01192 01193 /** 01194 * Access operator of genericMatrix element as a point in a 2D-Map 01195 * 01196 * @param p position of the element (this is equivalent to at(p.y,p.x)) 01197 * @return a reference to the genericMatrix element 01198 */ 01199 inline T& at(const size_type& p); 01200 01201 /** 01202 * Const access operator of genericMatrix element as a point in a 2D-Map 01203 * 01204 * @param p position of the element (this is equivalent to at(p.y,p.x)) 01205 * @return a const reference to the vector element 01206 */ 01207 inline const T& at(const size_type& p) const; 01208 01209 /** 01210 * Access element at the given position. 01211 * 01212 * With this operator the genericMatrix can be accessed as a 01213 * vector, where the rows of the genericMatrix are concatenated. 01214 * The access to the genericMatrix with at(row,col) is equivalent 01215 * to elem(row*columns()+col). 01216 * 01217 * @param pos the index of the element of the genericMatrix 01218 * 01219 * @return a reference to the genericMatrix element 01220 */ 01221 inline T& elem(const int pos); 01222 01223 /** 01224 * Access element at the given position. 01225 * 01226 * With this operator the genericMatrix can be accessed as a 01227 * vector, where the rows of the genericMatrix are concatenated. 01228 * The access to the genericMatrix with at(row,col) is equivalent 01229 * to elem(row*columns()+col). 01230 * 01231 * @param pos the index of the element of the genericMatrix 01232 * 01233 * @return a reference to the genericMatrix element 01234 */ 01235 inline const T& elem(const int pos) const; 01236 01237 /** 01238 * Return genericMatrix-row as a vector. 01239 * 01240 * This method works fast, since it returns a reference to the row vector. 01241 * The data will NOT be copied. 01242 * 01243 * @param row the row to be accessed 01244 * @return a reference to the vector row 01245 */ 01246 inline row_type& getRow(const int row); 01247 01248 /** 01249 * Return genericMatrix-row as a const vector. 01250 * This method works fast, since it returns a reference to the row vector. 01251 * The data will NOT be copied. 01252 * 01253 * @param row the row to be accessed 01254 * @return a const reference to the vector row 01255 */ 01256 inline const row_type& getRow(const int row) const; 01257 01258 /** 01259 * Alias for getRow() 01260 */ 01261 inline row_type& operator[](const int row); 01262 01263 /** 01264 * Alias for getRow() 01265 */ 01266 inline const row_type& operator[](const int row) const; 01267 01268 /** 01269 * Copy a row vector in the given parameter. 01270 * 01271 * This method copies the data of a given row of the genericMatrix 01272 * in the given vector. 01273 * 01274 * @param row the number of the row to be copied 01275 * @param theRow the vector, where the data will be copied 01276 * @see getRow() 01277 */ 01278 inline void getRowCopy(const int row,row_type& theRow) const; 01279 01280 /** 01281 * Return genericMatrix-row as a vector. 01282 * 01283 * This method copies the data of the genericMatrix, therefore is not as 01284 * fast as getRow(). 01285 * 01286 * @param row the number of the row to be copied 01287 * @return a vector with the contents of the row of the genericMatrix 01288 */ 01289 inline row_type getRowCopy(const int row) const; 01290 01291 /** 01292 * Return genericMatrix-column as a vector. 01293 * 01294 * This method copies the data of the genericMatrix, therefore is not as 01295 * fast as getRow(). 01296 * 01297 * @param col the number of the column to be copied 01298 * @param theCol a vector, where the column vector of the genericMatrix 01299 * should be copied. 01300 */ 01301 void getColumnCopy(const int col,row_type& theCol) const; 01302 01303 /** 01304 * Return genericMatrix-column as a vector. 01305 * 01306 * This method copies the data of the genericMatrix, therefore is not as 01307 * fast as getRow() 01308 * @param col the number of the column to be copied 01309 * @return a vector with the contents of the column of the genericMatrix 01310 */ 01311 inline row_type getColumnCopy(const int col) const; 01312 01313 /** 01314 * Return the diagonal elements of the genericMatrix as vector. 01315 * 01316 * This method copies the diagonal elements of the genericMatrix into 01317 * the vector. If the genericMatrix is non-symmetrical, the vector will 01318 * be of dimension min(rows(),columns()). 01319 * @param diag a vector, where the diagonal of the genericMatrix 01320 * should be copied. 01321 */ 01322 void getDiagonal(row_type& diag) const; 01323 01324 /** 01325 * Return the diagonal elements of the genericMatrix as vector. 01326 * This method copies the diagonal elements of the genericMatrix into 01327 * the vector. If the genericMatrix is non-symmetrical, the vector will 01328 * be of dimension min(rows(),columns()). 01329 * @return a vector with the diagonal elements of the genericMatrix. 01330 */ 01331 inline row_type getDiagonal() const; 01332 01333 /** 01334 * Sets the diagonal of the genericMatrix to the values given in 01335 * the genericVector \a diag. Let \c r be the number of rows and 01336 * \c c be the number of columns of the matrix. Then \c minRC is 01337 * \c min(r,c). Also let \c d be the size of \a diag. Only \c 01338 * min(minRC,d) values are copied from \a diag. If \c d is smaller 01339 * than \c minRC the remaining values on the diagonal are left 01340 * untouched. The copying always starts at (0,0) of the matrix. 01341 * 01342 * @param diag values to be copied into the diagonal of the matrix 01343 */ 01344 void setDiagonal(const row_type& diag); 01345 01346 /** 01347 * Copy the data of a vector in a given row. 01348 * 01349 * @param row the row that receives the data. 01350 * @param theRow the vector with the data to be copied 01351 */ 01352 inline void setRow(const int row,const row_type& theRow); 01353 01354 /** 01355 * Copy the data of a vector in a given column 01356 * @param col the column that receives the data. 01357 * @param theCol the vector with the data to be copied 01358 */ 01359 void setColumn(const int col,const row_type& theCol); 01360 01361 /** 01362 * Assigment operator. 01363 * 01364 * Copy the contents of \a other in this object. 01365 * 01366 * @param other the other genericMatrix to be copied 01367 */ 01368 genericMatrix<T>& copy(const genericMatrix<T>& other); 01369 01370 /** 01371 * Assigment operator. 01372 * 01373 * Copy the contents of a submatrix of \a other into this object. The 01374 * content of the current object is removed and at the end it will contain 01375 * a copy the submatrix only. 01376 * 01377 * @param other the other genericMatrix to be copied 01378 * @param fromRow initial row of the other genericMatrix to be copied 01379 * @param fromCol initial column of the other genericMatrix to be copied 01380 * @param toRow last row to be copied of the other genericMatrix 01381 * @param toCol last column to be copied of the other genericMatrix 01382 */ 01383 genericMatrix<T>& copy(const genericMatrix<T>& other, 01384 const int fromRow, 01385 const int fromCol, 01386 const int toRow=MaxIndex, 01387 const int toCol=MaxIndex); 01388 01389 01390 /** 01391 * Assigment operator. 01392 * 01393 * Copy the contents of a submatrix of \a other into this object. The 01394 * content of the current object is removed and at the end it will contain 01395 * only the submatrix. 01396 * 01397 * @param other the other genericMatrix to be copied 01398 * @param from initial submatrix indices (x,y) to be copied 01399 * @param to last submatrix indices (x,y) to be copied (inclusive) 01400 */ 01401 inline genericMatrix<T>& copy(const genericMatrix<T>& other, 01402 const size_type& from, 01403 const size_type& to); 01404 01405 /** 01406 * Assigment operator. 01407 * 01408 * Copy the contents of a submatrix of \a other into this object. The 01409 * content of the current object is removed and at the end it will contain 01410 * only the copy of the submatrix. 01411 * 01412 * @param other the other genericMatrix to be copied 01413 * @param window rectangle define the copy area 01414 */ 01415 inline genericMatrix<T>& copy(const genericMatrix<T>& other, 01416 const irectangle& window); 01417 01418 /** 01419 * Assigment operator. 01420 * 01421 * Copy the contents of the specified rows of \a other into this 01422 * object. Multiple occurence of one row index in \a idx is allowed. 01423 * 01424 * @param other the other genericMatrix to be copied 01425 * @param idx indices of the rows to be copied. 01426 */ 01427 genericMatrix<T>& copyRows(const genericMatrix<T>& other, 01428 const genericVector<int>& idx); 01429 01430 /** 01431 * Assigment operator. 01432 * 01433 * Copy the contents of the specified columns of \a other into this 01434 * object. Multiple occurence of one column index in \a idx is allowed. 01435 * 01436 * @param other the other genericMatrix to be copied 01437 * @param idx indices of the rows to be copied. 01438 */ 01439 genericMatrix<T>& copyColumns(const genericMatrix<T>& other, 01440 const genericVector<int>& idx); 01441 01442 /** 01443 * Copy the \a other genericMatrix by casting each of 01444 * its elements 01445 * 01446 * @param other The genericMatrix to be casted 01447 * 01448 * Example: 01449 * \code 01450 * cvr::genericMatrix<int> matA(10,10,1);// a genericMatrix of integers 01451 * cvr::genericMatrix<double> matB; // a genericMatrix of doubles 01452 * 01453 * matB.castFrom(matA); // this will copy matA in matB!! 01454 * \endcode 01455 */ 01456 template<typename U> 01457 genericMatrix<T>& castFrom(const genericMatrix<U>& other); 01458 01459 /** 01460 * This is just an alias for copy(const genericMatrix<T>&) to facilitate 01461 * generic programming. 01462 * 01463 * @param other The genericMatrix to be copied. 01464 */ 01465 genericMatrix<T>& castFrom(const genericMatrix<T>& other); 01466 01467 /** 01468 * Copy a submatrix of the \a other genericMatrix by casting each of 01469 * its elements. 01470 * 01471 * @param other The genericMatrix to be casted 01472 * @param fromRow initial row of the other genericMatrix to be copied 01473 * @param fromCol initial column of the other genericMatrix to be copied 01474 * @param toRow last row to be copied of the other genericMatrix 01475 * @param toCol last column to be copied of the other genericMatrix 01476 * 01477 * @return a reference to the current casted matrix. 01478 */ 01479 template<typename U> 01480 genericMatrix<T>& castFrom(const genericMatrix<U>& other, 01481 const int fromRow, 01482 const int fromCol, 01483 const int toRow=MaxIndex, 01484 const int toCol=MaxIndex); 01485 01486 /** 01487 * This is just an alias for copy(const genericMatrix<T>&, const int 01488 * fromRow, const int fromCol, const int toRow, const int toCol) to 01489 * facilitate generic programming. 01490 * 01491 * @param other The genericMatrix to be copied. 01492 * @param fromRow initial row of the other genericMatrix to be copied 01493 * @param fromCol initial column of the other genericMatrix to be copied 01494 * @param toRow last row to be copied of the other genericMatrix 01495 * @param toCol last column to be copied of the other genericMatrix 01496 * 01497 * @return a reference to the current casted matrix. 01498 */ 01499 genericMatrix<T>& castFrom(const genericMatrix<T>& other, 01500 const int fromRow, 01501 const int fromCol, 01502 const int toRow=MaxIndex, 01503 const int toCol=MaxIndex); 01504 01505 /** 01506 * Returns the name of this type. 01507 */ 01508 virtual const std::string& name() const; 01509 01510 /** 01511 * Create a clone of this genericMatrix 01512 * @return a pointer to a copy of this genericMatrix 01513 */ 01514 virtual genericMatrix<T>* clone() const; 01515 01516 /** 01517 * Create a new instance of genericMatrix 01518 * @return a pointer to a new instance of genericMatrix 01519 */ 01520 virtual genericMatrix<T>* newInstance() const; 01521 01522 /** 01523 * Compare this genericMatrix with other. 01524 * 01525 * @param other the other genericMatrix to be compared with 01526 * @return true if both matrices have the same elements and same size 01527 */ 01528 bool equals(const genericMatrix<T>& other) const; 01529 01530 /** 01531 * Compare the contents of each element of this genericMatrix with the 01532 * other one. It assumes the type T can be compared using the 01533 * operator==. 01534 * 01535 * @param other the other genericMatrix to be compared with 01536 * @return true if both matrices have the same elements and same size 01537 */ 01538 inline bool operator==(const genericMatrix<T>& other) const; 01539 01540 /** 01541 * Assigment operator (alias for copy(other)). 01542 * 01543 * @param other the genericMatrix to be copied 01544 * @return a reference to the actual genericMatrix 01545 */ 01546 inline genericMatrix<T>& operator=(const genericMatrix<T>& other); 01547 01548 /** 01549 * \name Apply Methods 01550 * 01551 * Following methods are used to apply simple functions to each element 01552 * of the vector. 01553 */ 01554 //@{ 01555 01556 /** 01557 * Applies a C-function to each element of the genericMatrix. 01558 * @param function a pointer to a C-function 01559 * @return a reference to the actual genericMatrix 01560 */ 01561 genericMatrix<T>& apply(T (*function)(T)); 01562 01563 /** 01564 * Applies a C-function to each element of the other genericMatrix 01565 * @param other the genericMatrix which elements will go through the given 01566 * function. 01567 * @param function a pointer to a C-function 01568 * @return a reference to the actual genericMatrix 01569 */ 01570 genericMatrix<T>& apply(const genericMatrix<T>& other,T (*function)(T)); 01571 01572 /** 01573 * Applies a C-function to each element of the genericMatrix. 01574 * @param function a pointer to a C-function 01575 * @return a reference to the actual genericMatrix 01576 */ 01577 genericMatrix<T>& apply(T (*function)(const T&)); 01578 01579 /** 01580 * Applies a C-function to each element of the other genericMatrix 01581 * @param other the genericMatrix which elements will go through the given 01582 * function. 01583 * @param function a pointer to a C-function 01584 * @return a reference to the actual genericMatrix 01585 */ 01586 genericMatrix<T>& apply(const genericMatrix<T>& other, 01587 T (*function)(const T&)); 01588 01589 /** 01590 * A two-parameter C-function receives the i-th elements of this 01591 * and the given genericMatrix and the result will be left in this 01592 * genericMatrix. Note that both matrices must have the same size! 01593 * @param other the second genericMatrix to be considered (the first 01594 * genericMatrix will be this object!) 01595 * @param function a pointer to C-function with two parameters 01596 * @return a reference to the actual genericMatrix 01597 */ 01598 genericMatrix<T>& apply(const genericMatrix<T>& other, 01599 T (*function)(const T&,const T&)); 01600 01601 /** 01602 * A two-parameter C-function receives the i-th elements of this 01603 * and the given genericMatrix and the result will be left in this 01604 * genericMatrix. Note that both matrices must have the same size! 01605 * @param other the second genericMatrix to be considered (the first 01606 * genericMatrix will be this object!) 01607 * @param function a pointer to C-function with two parameters 01608 * @return a reference to the actual genericMatrix 01609 */ 01610 genericMatrix<T>& apply(const genericMatrix<T>& other, 01611 T (*function)(T,T)); 01612 01613 /** 01614 * A two-parameter C-function receives the i-th elements of both given 01615 * matrices and leaves the result here. 01616 * Note that both matrices must have the same size! 01617 * @param a the first genericMatrix 01618 * @param b the second genericMatrix 01619 * @param function a pointer to C-function with two parameters 01620 * @return a reference to the actual genericMatrix 01621 */ 01622 genericMatrix<T>& apply(const genericMatrix<T>& a, 01623 const genericMatrix<T>& b, 01624 T (*function)(const T&,const T&)); 01625 01626 /** 01627 * A two-parameter C-function receives the i-th elements of both given 01628 * matrices and leaves the result here. 01629 * Note that both matrices must have the same size! 01630 * @param a the first genericMatrix 01631 * @param b the second genericMatrix 01632 * @param function a pointer to C-function with two parameters 01633 * @return a reference to the actual genericMatrix 01634 */ 01635 genericMatrix<T>& apply(const genericMatrix<T>& a, 01636 const genericMatrix<T>& b, 01637 T (*function)(T,T)); 01638 01639 //@} 01640 01641 /** 01642 * @name Input and Output 01643 */ 01644 //@{ 01645 01646 /** 01647 * Write the object in the given ioHandler 01648 */ 01649 virtual bool write(ioHandler& handler,const bool complete = true) const; 01650 01651 /** 01652 * Read the object from the given ioHandler 01653 */ 01654 virtual bool read(ioHandler& handler,const bool complete = true); 01655 //@} 01656 01657 protected: 01658 01659 /** 01660 * @name Memory allocation and deallocation 01661 * 01662 * Restrict all memory allocation and deallocation to these functions. 01663 */ 01664 //@{ 01665 /** 01666 * Allocate \a n number of rows or the appropriate type. 01667 */ 01668 inline virtual row_type* allocNewRows(const int n); 01669 01670 /** 01671 * Reserve just a memory block of the given size; 01672 */ 01673 inline T* reserveNewMem(const int theSize) const; 01674 01675 /** 01676 * Reserve memory. 01677 * 01678 * This method allocates memory for the elements, for the rows and 01679 * distributes the memory into the blocks 01680 */ 01681 void reserveMem(const int rows,const int cols); 01682 01683 /** 01684 * Release the given block of memory. 01685 */ 01686 void releaseMem(T*& block) const; 01687 01688 /** 01689 * Release the internal memory blocks, including 01690 * theElements_ and the row array. 01691 */ 01692 void releaseMem(); 01693 01694 /** 01695 * Allocate \a n number of rows or the appropriate type. 01696 */ 01697 inline void releaseRows(row_type*& theRows) const; 01698 01699 /** 01700 * Take the memory block and distribute it on the 01701 * allocated vectors pointed by theRows. 01702 */ 01703 void distributeMem(const int theRows, 01704 const int theCols, 01705 T* block, 01706 row_type* memRows) const; 01707 //@} 01708 01709 /** 01710 * Size of the matrix as point 01711 */ 01712 size_type theSize_; 01713 01714 /** 01715 * Index of the last row, i.e. last_.x=theSize.x-1, and last_.y=theSize.y-1 01716 */ 01717 size_type last_; 01718 01719 /** 01720 * Size of theElements_ memory block, i.e. theSize_.x*theSize_.y; 01721 */ 01722 int totalSize_; 01723 01724 /** 01725 * Indicates if \a theElements points to own data or to 01726 * external data. 01727 */ 01728 bool ownData_; 01729 01730 /** 01731 * If constReference=true, is not possible to resize or change 01732 * the reference of this genericVector. 01733 * 01734 * Very important for an efficient matrix class!! 01735 * (see useExternData()) 01736 */ 01737 eConstantReference constReference_; 01738 01739 /** 01740 * Pointer to the elements of the genericMatrix 01741 */ 01742 T* theElements_; 01743 01744 /** 01745 * Table of pointers to the rows 01746 */ 01747 row_type* rowAddressTable_; 01748 }; 01749 } 01750 01751 namespace std { 01752 /* 01753 * Outputs the elements of the vector on a stream 01754 */ 01755 template <class T> 01756 ostream& operator<<(ostream& s,const cvr::genericMatrix<T>& amat); 01757 } 01758 01759 #include "cvrGenericMatrix_inline.h" 01760 #include "cvrGenericMatrix_template.h" 01761 01762 #else 01763 #include "cvrGenericMatrix_template.h" 01764 #endif