ViSP  2.9.0
vpSubMatrix.cpp
1 /****************************************************************************
2  *
3  * $Id: vpSubMatrix.cpp 4649 2014-02-07 14:57:11Z fspindle $
4  *
5  * Copyright (C) 2005 - 2014 Inria. All rights reserved.
6  *
7  * This file is part of the ViSP software.
8  * Copyright (C) 2005 - 2014 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  : pRowNum(0), pColNum(0), parent(NULL)
51 {
52 }
53 
62 vpSubMatrix::vpSubMatrix(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset,
63  const unsigned int & nrows, const unsigned int & ncols)
64  : pRowNum(0), pColNum(0), parent(NULL)
65 {
66  init(m,row_offset,col_offset,nrows,ncols);
67 }
68 
77 void vpSubMatrix::init(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset , const unsigned int & nrows , const unsigned int & ncols){
78 
79  if(! m.data){
80  vpERROR_TRACE("\n\t\t SubMatrix parent matrix is not allocated") ;
82  "\n\t\t SubMatrix parent matrix is not allocated")) ;
83  }
84 
85  if(row_offset+nrows <= m.getRows() && col_offset+ncols <= m.getCols()){
86  data=m.data;
87  parent =&m;
88  rowNum = nrows;
89  colNum = ncols;
90  pRowNum=m.getRows();
91  pColNum=m.getCols();
92 
93  if(rowPtrs)
94  free(rowPtrs);
95 
96  rowPtrs=(double**) malloc(nrows * sizeof(double*));
97  for(unsigned int r=0;r<nrows;r++)
98  rowPtrs[r]= m.data+col_offset+(r+row_offset)*pColNum;
99 
100  dsize = pRowNum*pColNum ;
101  trsize =0 ;
102  }else{
103  vpERROR_TRACE("Submatrix cannot be contain in parent matrix") ;
104  throw(vpMatrixException(vpMatrixException::incorrectMatrixSizeError,"Submatrix cannot be contain in parent matrix")) ;
105  }
106 }
107 
113  if(!data){
114  vpERROR_TRACE("\n\t\t vpSubMatrix parent vpMatrix has been destroyed");
116  "\n\t\t \n\t\t vpSubMatrix parent vpMatrix has been destroyed")) ;
117  }
118  if(pRowNum!=parent->getRows() || pColNum!=parent->getCols()){
119  vpERROR_TRACE("\n\t\t vpSubMatrix size of parent vpMatrix has been changed");
121  "\n\t\t \n\t\t vpSubMatrix size of parent vpMatrix has been changed")) ;
122  }
123 }
124 
130 
131  if ((colNum != B.getCols())||(rowNum != B.getRows()))
132  {
133  vpERROR_TRACE("\n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix") ;
135  "\n\t\t \n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix")) ;
136  }
137 
138  for (unsigned int i=0;i<rowNum;i++)
139  for(unsigned int j=0;j<colNum;j++)
140  rowPtrs[i][j] = B[i][j];
141 
142  return *this;
143 }
144 
150 
151  if ((colNum != B.getCols())||(rowNum != B.getRows()))
152  {
153  vpERROR_TRACE("\n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix") ;
155  "\n\t\t \n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix")) ;
156  }
157 
158 
159  double ** BrowPtrs=B.rowPtrs;
160 
161  for (unsigned int i=0;i<rowNum;i++)
162  for(unsigned int j=0;j<colNum;j++)
163  rowPtrs[i][j] = BrowPtrs[i][j];
164 
165  return *this;
166 }
167 
173  for (unsigned int i=0;i<rowNum;i++)
174  for(unsigned int j=0;j<colNum;j++)
175  rowPtrs[i][j] = x;
176 
177  return *this;
178 }
179 
180 
182 {
183  data=NULL;
184 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void checkParentStatus()
Check is parent vpRowVector has changed since initialization.
void init()
Initialization of the object matrix.
Definition: vpMatrix.cpp:98
#define vpERROR_TRACE
Definition: vpDebug.h:395
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:118
unsigned int trsize
Total row space.
Definition: vpMatrix.h:126
double ** rowPtrs
address of the first element of each rows
Definition: vpMatrix.h:121
~vpSubMatrix()
Destructor.
unsigned int pRowNum
Definition: vpSubMatrix.h:78
vpMatrix * parent
Definition: vpSubMatrix.h:80
unsigned int rowNum
number of rows
Definition: vpMatrix.h:112
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:163
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:124
unsigned int colNum
number of columns
Definition: vpMatrix.h:114
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:161
vpSubMatrix()
Default constructor.
Definition: vpSubMatrix.cpp:49