ViSP  2.8.0
vpSubMatrix.cpp
1 /****************************************************************************
2  *
3  * $Id: vpSubMatrix.cpp 4057 2013-01-05 13:10:29Z fspindle $
4  *
5  * Copyright (C) 2005 - 2013 Inria. All rights reserved.
6  *
7  * This file is part of the ViSP software.
8  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
9  *
10  * This software is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * ("GPL") version 2 as published by the Free Software Foundation.
13  * See the file LICENSE.txt at the root directory of this source
14  * distribution for additional information about the GNU GPL.
15  *
16  * For using ViSP with software that can not be combined with the GNU
17  * GPL, please contact INRIA about acquiring a ViSP Professional
18  * Edition License.
19  *
20  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
21  *
22  * This software was developed at:
23  * INRIA Rennes - Bretagne Atlantique
24  * Campus Universitaire de Beaulieu
25  * 35042 Rennes Cedex
26  * France
27  * http://www.irisa.fr/lagadic
28  *
29  * If you have questions regarding the use of this file, please contact
30  * INRIA at visp@inria.fr
31  *
32  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
33  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
34  *
35  * Description:
36  * Mask on a vpMatrix .
37  *
38  * Authors:
39  * Laneurit Jean
40  *
41  *****************************************************************************/
42 
43 #include <visp/vpSubMatrix.h>
44 #include <visp/vpException.h>
45 #include <visp/vpMatrixException.h>
46 #include <visp/vpDebug.h>
47 #include <stdlib.h>
48 
50  data=NULL;
51  parent=NULL;
52  rowPtrs=NULL;
53  rowNum=0;
54  colNum=0;
55  pRowNum=0;
56  pColNum=0;
57  dsize=0;
58  trsize=0;
59 }
60 
69 vpSubMatrix::vpSubMatrix(vpMatrix &m, const unsigned int & row, const unsigned int &col , const unsigned int & nrows , const unsigned int & ncols){
70  init(m,row,col,nrows,ncols);
71 }
72 
81 void vpSubMatrix::init(vpMatrix &m, const unsigned int & row, const unsigned int &col , const unsigned int & nrows , const unsigned int & ncols){
82 
83  if(! m.data){
84  vpERROR_TRACE("\n\t\t SubMatrix parent matrix is not allocated") ;
86  "\n\t\t SubMatrix parent matrix is not allocated")) ;
87  }
88 
89  if(row+nrows <= m.getRows() && col+ncols <= m.getCols()){
90  data=m.data;
91  parent =&m;
92  rowNum = nrows;
93  colNum = ncols;
94  pRowNum=m.getRows();
95  pColNum=m.getCols();
96 
97  if(rowPtrs)
98  free(rowPtrs);
99 
100  rowPtrs=(double**) malloc(nrows * sizeof(double*));
101  for(unsigned int r=0;r<nrows;r++)
102  rowPtrs[r]= m.data+col+(r+row)*pColNum;
103 
104  dsize = pRowNum*pColNum ;
105  trsize =0 ;
106  }else{
107  vpERROR_TRACE("Submatrix cannot be contain in parent matrix") ;
108  throw(vpMatrixException(vpMatrixException::incorrectMatrixSizeError,"Submatrix cannot be contain in parent matrix")) ;
109  }
110 }
111 
117  if(!data){
118  vpERROR_TRACE("\n\t\t vpSubMatrix parent vpMatrix has been destroyed");
120  "\n\t\t \n\t\t vpSubMatrix parent vpMatrix has been destroyed")) ;
121  }
122  if(pRowNum!=parent->getRows() || pColNum!=parent->getCols()){
123  vpERROR_TRACE("\n\t\t vpSubMatrix size of parent vpMatrix has been changed");
125  "\n\t\t \n\t\t vpSubMatrix size of parent vpMatrix has been changed")) ;
126  }
127 }
128 
134 
135  if ((colNum != B.getCols())||(rowNum != B.getRows()))
136  {
137  vpERROR_TRACE("\n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix") ;
139  "\n\t\t \n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix")) ;
140  }
141 
142  for (unsigned int i=0;i<rowNum;i++)
143  for(unsigned int j=0;j<colNum;j++)
144  rowPtrs[i][j] = B[i][j];
145 
146  return *this;
147 }
148 
154 
155  if ((colNum != B.getCols())||(rowNum != B.getRows()))
156  {
157  vpERROR_TRACE("\n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix") ;
159  "\n\t\t \n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix")) ;
160  }
161 
162 
163  double ** BrowPtrs=B.rowPtrs;
164 
165  for (unsigned int i=0;i<rowNum;i++)
166  for(unsigned int j=0;j<colNum;j++)
167  rowPtrs[i][j] = BrowPtrs[i][j];
168 
169  return *this;
170 }
171 
177  for (unsigned int i=0;i<rowNum;i++)
178  for(unsigned int j=0;j<colNum;j++)
179  rowPtrs[i][j] = x;
180 
181  return *this;
182 }
183 
184 
186 {
187  data=NULL;
188 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
void checkParentStatus()
Check is parent vpRowVector has changed since initialization.
void init()
Initialization of the object matrix.
Definition: vpMatrix.cpp:93
#define vpERROR_TRACE
Definition: vpDebug.h:379
unsigned int pColNum
Definition: vpSubMatrix.h:79
Definition of the vpSubMatrix vpSubMatrix class provides a mask on a vpMatrix all properties of vpMat...
Definition: vpSubMatrix.h:66
double * data
address of the first element of the data array
Definition: vpMatrix.h:116
unsigned int trsize
Total row space.
Definition: vpMatrix.h:124
double ** rowPtrs
address of the first element of each rows
Definition: vpMatrix.h:119
~vpSubMatrix()
Destructor.
unsigned int pRowNum
Definition: vpSubMatrix.h:78
vpMatrix * parent
Definition: vpSubMatrix.h:80
unsigned int rowNum
number of rows
Definition: vpMatrix.h:110
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:159
vpSubMatrix & operator=(const vpSubMatrix &B)
Operation such as subA = subB.
error that can be emited by the vpMatrix class and its derivates
unsigned int dsize
Current size (rowNum * colNum)
Definition: vpMatrix.h:122
unsigned int colNum
number of columns
Definition: vpMatrix.h:112
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:157
vpSubMatrix()
Default constructor.
Definition: vpSubMatrix.cpp:49