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);
167 void resize(
const unsigned int nrows,
const unsigned int ncols,
168 const bool flagNullify =
true)
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*));
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;
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++)
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);
325 bool headerIsDecoded =
false;
327 std::streampos pos = file.tellg();
329 file.getline(line, 256);
330 std::string prefix(
"# ");
331 std::string line_(line);
332 if (line_.compare(0, 2,
"# ") == 0) {
337 h += line_.substr(2);
341 file.seekg (pos, file.beg);
342 headerIsDecoded =
true;
344 }
while(! headerIsDecoded);
346 if (header != NULL) {
347 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
348 sprintf(header,
"%s", h.c_str());
350 _snprintf_s(header, h.size()+1, _TRUNCATE,
"%s", h.c_str());
354 unsigned int rows, cols;
358 if (rows >= (std::numeric_limits<unsigned int>::max)()
359 || cols >= (std::numeric_limits<unsigned int>::max)())
365 for(
unsigned int i = 0; i < rows; i++) {
366 for(
unsigned int j = 0; j < cols; j++) {
376 while ((c !=
'\0')) {
380 if (header != NULL) {
381 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
382 sprintf(header,
"%s", h.c_str());
384 _snprintf_s(header, h.size()+1, _TRUNCATE,
"%s", h.c_str());
388 unsigned int rows, cols;
389 file.read((
char*)&rows,
sizeof(
unsigned int));
390 file.read((
char*)&cols,
sizeof(
unsigned int));
394 for(
unsigned int i = 0; i < rows; i++) {
395 for(
unsigned int j = 0; j < cols; j++) {
396 file.read((
char*)&value,
sizeof(Type));
421 file.open(filename.c_str(), std::fstream::in);
428 unsigned int rows = 0,cols = 0;
430 std::string line,subs;
431 bool inheader =
true;
433 unsigned int lineStart = 0;
435 while ( getline (file,line) ) {
437 if(rows == 0 && line.compare(0,5,
"rows:") == 0) {
438 std::stringstream ss(line);
442 else if (cols == 0 && line.compare(0,5,
"cols:") == 0) {
443 std::stringstream ss(line);
447 else if (line.compare(0,5,
"data:") == 0)
455 if(rows == 0 || cols == 0) {
461 lineStart = (
unsigned int)line.find(
"[") + 1;
463 std::stringstream ss(line.substr(lineStart, line.find(
"]") - lineStart));
465 while(getline(ss, subs,
','))
466 A[i][j++] = atof(subs.c_str());
471 if (header != NULL) {
472 std::string h_ = h.substr(0, h.size() - 1);
473 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
474 sprintf(header,
"%s", h_.c_str());
476 _snprintf_s(header, h_.size()+1, _TRUNCATE,
"%s", h_.c_str());
500 const bool binary =
false,
const char *header =
"")
505 file.open(filename.c_str(), std::fstream::out);
507 file.open(filename.c_str(), std::fstream::out|std::fstream::binary);
517 while (header[i] !=
'\0') {
519 if (header[i] ==
'\n')
525 file << A << std::endl;
529 while (header[headerSize] !=
'\0') headerSize++;
530 file.write(header, headerSize+1);
531 unsigned int matrixSize;
533 file.write((
char*)&matrixSize,
sizeof(
unsigned int));
535 file.write((
char*)&matrixSize,
sizeof(
unsigned int));
537 for(
unsigned int i = 0; i < A.
getRows(); i++) {
538 for(
unsigned int j = 0; j < A.
getCols(); j++) {
540 file.write((
char*)&value,
sizeof(Type));
592 file.open(filename.c_str(), std::fstream::out);
600 bool inIndent =
false;
601 std::string indent =
"";
602 bool checkIndent =
true;
603 while (header[i] !=
'\0') {
609 else if (indent.length() > 0)
612 if (header[i] ==
'\n' || (inIndent && header[i] ==
' '))
622 file <<
"rows: " << A.
getRows() << std::endl;
623 file <<
"cols: " << A.
getCols() << std::endl;
625 if(indent.length() == 0)
628 file <<
"data: " << std::endl;
632 file << indent <<
"- [";
634 file << A[i][j] <<
", ";
635 file << A[i][j] <<
"]" << std::endl;
651 Type *dataptr = data;
654 for (
unsigned int i = 0; i < dsize-1; i++)
656 if (*dataptr < min) min = *dataptr;
669 Type *dataptr = data;
672 for (
unsigned int i = 0; i < dsize-1; i++)
674 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.