FileFormatUtils.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
3  *
4  * Copyright 2007-2012 VTT Technical Research Centre of Finland
5  *
6  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
7  * <http://www.vtt.fi/multimedia/alvar.html>
8  *
9  * ALVAR is free software; you can redistribute it and/or modify it under the
10  * terms of the GNU Lesser General Public License as published by the Free
11  * Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with ALVAR; if not, see
21  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
22  */
23 
25 #include <stdio.h>
26 #include <sstream>
27 #include <limits>
28 
29 namespace alvar {
30 
31  bool FileFormatUtils::decodeXMLMatrix(const TiXmlElement *xml_matrix, int &type, int &rows, int &cols) {
32  const char * xml_type = xml_matrix->Attribute("type");
33  if (strcmp("CV_32F", xml_type) == 0) type = CV_32F;
34  else if (strcmp("CV_64F", xml_type) == 0) type = CV_64F;
35  else return false;
36 
37  if (xml_matrix->QueryIntAttribute("rows", &rows) != TIXML_SUCCESS) return false;
38  if (xml_matrix->QueryIntAttribute("cols", &cols) != TIXML_SUCCESS) return false;
39 
40  return true;
41  }
42 
43  CvMat* FileFormatUtils::allocateXMLMatrix(const TiXmlElement *xml_matrix) {
44  if (!xml_matrix) return NULL;
45 
46  int type, rows, cols;
47  if (!decodeXMLMatrix(xml_matrix, type, rows, cols)) return NULL;
48 
49  return cvCreateMat(rows, cols, type);
50  }
51 
52  bool FileFormatUtils::parseXMLMatrix(const TiXmlElement *xml_matrix, CvMat *matrix) {
53  if (!xml_matrix || !matrix) return false;
54 
55  int type, rows, cols;
56  if (!decodeXMLMatrix(xml_matrix, type, rows, cols)) return false;
57 
58  if (type != cvGetElemType(matrix)) return false;
59  if (rows != matrix->rows) return false;
60  if (cols != matrix->cols) return false;
61 
62  const TiXmlElement *xml_data = xml_matrix->FirstChildElement("data");
63  for (int r = 0; r < matrix->rows; ++r) {
64  for (int c = 0; c < matrix->cols; ++c) {
65  if (!xml_data) return false;
66  double value = atof(xml_data->GetText());
67  cvSetReal2D(matrix, r, c, value);
68  xml_data = (const TiXmlElement *) xml_data->NextSibling("data");
69  }
70  }
71 
72  return true;
73  }
74 
75  TiXmlElement* FileFormatUtils::createXMLMatrix(const char* element_name, const CvMat *matrix) {
76  if (!matrix) return NULL;
77 
78  TiXmlElement* xml_matrix = new TiXmlElement(element_name);
79  int precision;
80  if (cvGetElemType(matrix) == CV_32F) {
81  xml_matrix->SetAttribute("type", "CV_32F");
82  precision = std::numeric_limits<float>::digits10 + 2;
83  }
84  else if (cvGetElemType(matrix) == CV_64F) {
85  xml_matrix->SetAttribute("type", "CV_64F");
86  precision = std::numeric_limits<double>::digits10 + 2;
87  }
88  else {
89  delete xml_matrix;
90  return NULL;
91  }
92 
93  xml_matrix->SetAttribute("rows", matrix->rows);
94  xml_matrix->SetAttribute("cols", matrix->cols);
95 
96  for (int r = 0; r < matrix->rows; ++r) {
97  for (int c = 0; c < matrix->cols; ++c) {
98  TiXmlElement *xml_data = new TiXmlElement("data");
99  xml_matrix->LinkEndChild(xml_data);
100  std::stringstream ss;
101  ss.precision(precision);
102  ss<<cvGetReal2D(matrix, r, c);
103  xml_data->LinkEndChild(new TiXmlText(ss.str().c_str()));
104  }
105  }
106  return xml_matrix;
107  }
108 }
Main ALVAR namespace.
Definition: Alvar.h:174
static bool decodeXMLMatrix(const TiXmlElement *xml_matrix, int &type, int &rows, int &cols)
Reads matrix type, rows and cols from XML element.
static TiXmlElement * createXMLMatrix(const char *element_name, const CvMat *matrix)
Allocates new XML element and populates it with a CvMat data.
This file implements utilities that assist in reading and writing files.
r
static CvMat * allocateXMLMatrix(const TiXmlElement *xml_matrix)
Allocates CvMat of a correct type and size.
static bool parseXMLMatrix(const TiXmlElement *xml_matrix, CvMat *matrix)
Reads contents of alvar:matrix into CvMat.


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:04