Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpSubMatrix.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Mask on a vpMatrix .
32  *
33  * Authors:
34  * Laneurit Jean
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpSubMatrix.h>
39 #include <visp3/core/vpException.h>
40 #include <visp3/core/vpMatrixException.h>
41 #include <visp3/core/vpDebug.h>
42 #include <stdlib.h>
43 
45  : pRowNum(0), pColNum(0), parent(NULL)
46 {
47 }
48 
57 vpSubMatrix::vpSubMatrix(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset,
58  const unsigned int & nrows, const unsigned int & ncols)
59  : pRowNum(0), pColNum(0), parent(NULL)
60 {
61  init(m,row_offset,col_offset,nrows,ncols);
62 }
63 
72 void vpSubMatrix::init(vpMatrix &m, const unsigned int &row_offset, const unsigned int &col_offset , const unsigned int & nrows , const unsigned int & ncols){
73 
74  if(! m.data){
75  vpERROR_TRACE("\n\t\t SubMatrix parent matrix is not allocated") ;
77  "\n\t\t SubMatrix parent matrix is not allocated")) ;
78  }
79 
80  if(row_offset+nrows <= m.getRows() && col_offset+ncols <= m.getCols()){
81  data=m.data;
82  parent =&m;
83  rowNum = nrows;
84  colNum = ncols;
85  pRowNum=m.getRows();
86  pColNum=m.getCols();
87 
88  if(rowPtrs)
89  free(rowPtrs);
90 
91  rowPtrs=(double**) malloc(nrows * sizeof(double*));
92  for(unsigned int r=0;r<nrows;r++)
93  rowPtrs[r]= m.data+col_offset+(r+row_offset)*pColNum;
94 
96  }else{
97  vpERROR_TRACE("Submatrix cannot be contain in parent matrix") ;
98  throw(vpMatrixException(vpMatrixException::incorrectMatrixSizeError,"Submatrix cannot be contain in parent matrix")) ;
99  }
100 }
101 
107  if(!data){
108  vpERROR_TRACE("\n\t\t vpSubMatrix parent vpMatrix has been destroyed");
110  "\n\t\t \n\t\t vpSubMatrix parent vpMatrix has been destroyed")) ;
111  }
112  if(pRowNum!=parent->getRows() || pColNum!=parent->getCols()){
113  vpERROR_TRACE("\n\t\t vpSubMatrix size of parent vpMatrix has been changed");
115  "\n\t\t \n\t\t vpSubMatrix size of parent vpMatrix has been changed")) ;
116  }
117 }
118 
124 
125  if ((colNum != B.getCols())||(rowNum != B.getRows()))
126  {
127  vpERROR_TRACE("\n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix") ;
129  "\n\t\t \n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix")) ;
130  }
131 
132  for (unsigned int i=0;i<rowNum;i++)
133  for(unsigned int j=0;j<colNum;j++)
134  rowPtrs[i][j] = B[i][j];
135 
136  return *this;
137 }
138 
144 
145  if ((colNum != B.getCols())||(rowNum != B.getRows()))
146  {
147  vpERROR_TRACE("\n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix") ;
149  "\n\t\t \n\t\t vpSubMatrix mismatch in operator vpSubMatrix=vpMatrix")) ;
150  }
151 
152 
153  double ** BrowPtrs=B.rowPtrs;
154 
155  for (unsigned int i=0;i<rowNum;i++)
156  for(unsigned int j=0;j<colNum;j++)
157  rowPtrs[i][j] = BrowPtrs[i][j];
158 
159  return *this;
160 }
161 
167  for (unsigned int i=0;i<rowNum;i++)
168  for(unsigned int j=0;j<colNum;j++)
169  rowPtrs[i][j] = x;
170 
171  return *this;
172 }
173 
174 
176 {
177  data=NULL;
178 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
#define vpERROR_TRACE
Definition: vpDebug.h:391
void checkParentStatus() const
Check is parent vpRowVector has changed since initialization.
unsigned int pColNum
Definition: vpSubMatrix.h:75
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:84
unsigned int getCols() const
Return the number of columns of the 2D array.
Definition: vpArray2D.h:154
Definition of the vpSubMatrix vpSubMatrix class provides a mask on a vpMatrix all properties of vpMat...
Definition: vpSubMatrix.h:62
unsigned int rowNum
Number of rows in the array.
Definition: vpArray2D.h:74
~vpSubMatrix()
Destructor.
vp_deprecated void init()
Definition: vpMatrix.h:587
unsigned int getRows() const
Return the number of rows of the 2D array.
Definition: vpArray2D.h:152
unsigned int pRowNum
Definition: vpSubMatrix.h:74
unsigned int colNum
Number of columns in the array.
Definition: vpArray2D.h:76
vpMatrix * parent
Definition: vpSubMatrix.h:76
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 array size (rowNum * colNum)
Definition: vpArray2D.h:80
double ** rowPtrs
Address of the first element of each rows.
Definition: vpArray2D.h:78
vpSubMatrix()
Default constructor.
Definition: vpSubMatrix.cpp:44