37 #ifndef __vpArray2D_h_
38 #define __vpArray2D_h_
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpException.h>
100 resize(A.rowNum, A.colNum);
101 memcpy(data, A.data, rowNum*colNum*
sizeof(Type));
141 rowNum = colNum = dsize = 0;
167 void resize(
const unsigned int nrows,
const unsigned int ncols,
168 const bool flagNullify =
true)
170 if ((nrows == rowNum) && (ncols == colNum)) {
171 if (flagNullify && this->data != NULL) {
172 memset(this->data, 0, this->dsize*
sizeof(Type));
176 const bool recopyNeeded = (ncols !=
this ->colNum);
177 Type * copyTmp = NULL;
178 unsigned int rowTmp = 0, colTmp=0;
182 if (recopyNeeded && this->data != NULL) {
183 copyTmp =
new Type[this->
dsize];
184 memcpy (copyTmp,
this ->data,
sizeof(Type)*this->dsize);
189 this->dsize = nrows*ncols;
190 this->data = (Type*)realloc(this->data, this->dsize*
sizeof(Type));
191 if ((NULL == this->data) && (0 != this->
dsize)) {
192 if (copyTmp != NULL)
delete [] copyTmp;
194 "Memory allocation error when allocating 2D array data")) ;
197 this->rowPtrs = (Type**)realloc (this->rowPtrs, nrows*
sizeof(Type*));
198 if ((NULL == this->rowPtrs) && (0 != this->
dsize)) {
199 if (copyTmp != NULL)
delete [] copyTmp;
201 "Memory allocation error when allocating 2D array rowPtrs")) ;
207 for (
unsigned int i=0; i<
dsize; i+=ncols) {
208 *t_++ = this->data + i;
212 this->rowNum = nrows; this->colNum = ncols;
216 memset(this->data,0,this->dsize*
sizeof(Type));
218 else if (recopyNeeded && this->rowPtrs != NULL) {
220 const unsigned int minRow = (this->rowNum<rowTmp)?this->rowNum:rowTmp;
221 const unsigned int minCol = (this->colNum<colTmp)?this->colNum:colTmp;
222 for (
unsigned int i=0; i<this->
rowNum; ++i) {
223 for (
unsigned int j=0; j<this->
colNum; ++j) {
224 if ((minRow > i) && (minCol > j)) {
225 (*this)[i][j] = copyTmp [i*colTmp+j];
241 for (
unsigned int i=0;i<
rowNum;i++)
242 for(
unsigned int j=0;j<
colNum;j++)
254 memcpy(data, A.
data, rowNum*colNum*
sizeof(Type));
259 inline Type *
operator[](
unsigned int i) {
return rowPtrs[i]; }
261 inline Type *
operator[](
unsigned int i)
const {
return rowPtrs[i];}
267 friend std::ostream &operator<<(std::ostream &s, const vpArray2D<Type> &A)
271 std::ios_base::fmtflags original_flags = s.flags();
274 for (
unsigned int i=0;i<A.getRows();i++) {
275 for (
unsigned int j=0;j<A.getCols() -1;j++){
279 s << A[i][A.getCols() -1];
281 if (i < A.getRows()-1)
285 s.flags(original_flags);
309 const bool binary =
false,
char *header = NULL)
314 file.open(filename.c_str(), std::fstream::in);
316 file.open(filename.c_str(), std::fstream::in|std::fstream::binary);
327 while ((c !=
'\0') && (c !=
'\n')) {
332 strncpy(header, h.c_str(), h.size() + 1);
334 unsigned int rows, cols;
338 if (rows > (std::numeric_limits<unsigned int>::max)()
339 || cols > (std::numeric_limits<unsigned int>::max)())
345 for(
unsigned int i = 0; i < rows; i++) {
346 for(
unsigned int j = 0; j < cols; j++) {
355 while ((c !=
'\0') && (c !=
'\n')) {
360 strncpy(header, h.c_str(), h.size() + 1);
362 unsigned int rows, cols;
363 file.read((
char*)&rows,
sizeof(
unsigned int));
364 file.read((
char*)&cols,
sizeof(
unsigned int));
368 for(
unsigned int i = 0; i < rows; i++) {
369 for(
unsigned int j = 0; j < cols; j++) {
370 file.read((
char*)&value,
sizeof(Type));
396 file.open(filename.c_str(), std::fstream::in);
403 unsigned int rows = 0,cols = 0;
405 std::string line,subs;
406 bool inheader =
true;
408 unsigned int lineStart = 0;
410 while ( getline (file,line) ) {
412 if(rows == 0 && line.compare(0,5,
"rows:") == 0) {
413 std::stringstream ss(line);
417 else if (cols == 0 && line.compare(0,5,
"cols:") == 0) {
418 std::stringstream ss(line);
422 else if (line.compare(0,5,
"data:") == 0)
430 if(rows == 0 || cols == 0) {
436 lineStart = (
unsigned int)line.find(
"[") + 1;
438 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
440 while(getline(ss, subs,
','))
441 A[i][j++] = atof(subs.c_str());
447 strncpy(header, h.substr(0,h.length()-1).c_str(), h.size());
469 const bool binary =
false,
const char *header =
"")
474 file.open(filename.c_str(), std::fstream::out);
476 file.open(filename.c_str(), std::fstream::out|std::fstream::binary);
487 while (header[i] !=
'\0') {
489 if (header[i] ==
'\n')
496 file << A << std::endl;
500 while (header[headerSize] !=
'\0') headerSize++;
501 file.write(header,headerSize+1);
502 unsigned int matrixSize;
504 file.write((
char*)&matrixSize,
sizeof(
int));
506 file.write((
char*)&matrixSize,
sizeof(
int));
508 for(
unsigned int i = 0; i < A.
getRows(); i++) {
509 for(
unsigned int j = 0; j < A.
getCols(); j++) {
511 file.write((
char*)&value,
sizeof(Type));
564 file.open(filename.c_str(), std::fstream::out);
572 bool inIndent =
false;
573 std::string indent =
"";
574 bool checkIndent =
true;
575 while (header[i] !=
'\0') {
581 else if (indent.length() > 0)
584 if (header[i] ==
'\n' || (inIndent && header[i] ==
' '))
594 file <<
"rows: " << A.
getRows() << std::endl;
595 file <<
"cols: " << A.
getCols() << std::endl;
597 if(indent.length() == 0)
600 file <<
"data: " << std::endl;
604 file << indent <<
"- [";
606 file << A[i][j] <<
", ";
607 file << A[i][j] <<
"]" << std::endl;
623 Type *dataptr = data;
626 for (
unsigned int i = 0; i < dsize-1; i++)
628 if (*dataptr < min) min = *dataptr;
641 Type *dataptr = data;
644 for (
unsigned int i = 0; i < dsize-1; i++)
646 if (*dataptr > max) max = *dataptr;
static bool save(const std::string &filename, const vpArray2D< Type > &A, const bool binary=false, const char *header="")
static bool saveYAML(const std::string &filename, const vpArray2D< Type > &A, const char *header="")
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
static bool loadYAML(const std::string &filename, vpArray2D< Type > &A, char *header=NULL)
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
error that can be emited by ViSP classes.
Type * data
Address of the first element of the data array.
Implementation of a generic 2D array used as vase class of matrices and vectors.
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getCols() const
Return the number of columns of the 2D array.
unsigned int rowNum
Number of rows in the array.
vpArray2D< Type > & operator=(const vpArray2D< Type > &A)
static bool load(const std::string &filename, vpArray2D< Type > &A, const bool binary=false, char *header=NULL)
unsigned int getRows() const
Return the number of rows of the 2D array.
unsigned int colNum
Number of columns in the array.
Type * operator[](unsigned int i)
Set element using A[i][j] = x.
Type * operator[](unsigned int i) const
Get element using x = A[i][j].
unsigned int dsize
Current array size (rowNum * colNum)
Type ** rowPtrs
Address of the first element of each rows.