Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "ar_track_alvar/FileFormatUtils.h"
00025 #include <stdio.h>
00026 #include <sstream>
00027 #include <limits>
00028
00029 namespace alvar {
00030
00031 bool FileFormatUtils::decodeXMLMatrix(const TiXmlElement *xml_matrix, int &type, int &rows, int &cols) {
00032 const char * xml_type = xml_matrix->Attribute("type");
00033 if (strcmp("CV_32F", xml_type) == 0) type = CV_32F;
00034 else if (strcmp("CV_64F", xml_type) == 0) type = CV_64F;
00035 else return false;
00036
00037 if (xml_matrix->QueryIntAttribute("rows", &rows) != TIXML_SUCCESS) return false;
00038 if (xml_matrix->QueryIntAttribute("cols", &cols) != TIXML_SUCCESS) return false;
00039
00040 return true;
00041 }
00042
00043 CvMat* FileFormatUtils::allocateXMLMatrix(const TiXmlElement *xml_matrix) {
00044 if (!xml_matrix) return NULL;
00045
00046 int type, rows, cols;
00047 if (!decodeXMLMatrix(xml_matrix, type, rows, cols)) return NULL;
00048
00049 return cvCreateMat(rows, cols, type);
00050 }
00051
00052 bool FileFormatUtils::parseXMLMatrix(const TiXmlElement *xml_matrix, CvMat *matrix) {
00053 if (!xml_matrix || !matrix) return false;
00054
00055 int type, rows, cols;
00056 if (!decodeXMLMatrix(xml_matrix, type, rows, cols)) return false;
00057
00058 if (type != cvGetElemType(matrix)) return false;
00059 if (rows != matrix->rows) return false;
00060 if (cols != matrix->cols) return false;
00061
00062 const TiXmlElement *xml_data = xml_matrix->FirstChildElement("data");
00063 for (int r = 0; r < matrix->rows; ++r) {
00064 for (int c = 0; c < matrix->cols; ++c) {
00065 if (!xml_data) return false;
00066 double value = atof(xml_data->GetText());
00067 cvSetReal2D(matrix, r, c, value);
00068 xml_data = (const TiXmlElement *) xml_data->NextSibling("data");
00069 }
00070 }
00071
00072 return true;
00073 }
00074
00075 TiXmlElement* FileFormatUtils::createXMLMatrix(const char* element_name, const CvMat *matrix) {
00076 if (!matrix) return NULL;
00077
00078 TiXmlElement* xml_matrix = new TiXmlElement(element_name);
00079 int precision;
00080 if (cvGetElemType(matrix) == CV_32F) {
00081 xml_matrix->SetAttribute("type", "CV_32F");
00082 precision = std::numeric_limits<float>::digits10 + 2;
00083 }
00084 else if (cvGetElemType(matrix) == CV_64F) {
00085 xml_matrix->SetAttribute("type", "CV_64F");
00086 precision = std::numeric_limits<double>::digits10 + 2;
00087 }
00088 else {
00089 delete xml_matrix;
00090 return NULL;
00091 }
00092
00093 xml_matrix->SetAttribute("rows", matrix->rows);
00094 xml_matrix->SetAttribute("cols", matrix->cols);
00095
00096 for (int r = 0; r < matrix->rows; ++r) {
00097 for (int c = 0; c < matrix->cols; ++c) {
00098 TiXmlElement *xml_data = new TiXmlElement("data");
00099 xml_matrix->LinkEndChild(xml_data);
00100 std::stringstream ss;
00101 ss.precision(precision);
00102 ss<<cvGetReal2D(matrix, r, c);
00103 xml_data->LinkEndChild(new TiXmlText(ss.str().c_str()));
00104 }
00105 }
00106 return xml_matrix;
00107 }
00108 }