FileFormatUtils.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
00003  *
00004  * Copyright 2007-2012 VTT Technical Research Centre of Finland
00005  *
00006  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
00007  *          <http://www.vtt.fi/multimedia/alvar.html>
00008  *
00009  * ALVAR is free software; you can redistribute it and/or modify it under the
00010  * terms of the GNU Lesser General Public License as published by the Free
00011  * Software Foundation; either version 2.1 of the License, or (at your option)
00012  * any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful, but WITHOUT
00015  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00017  * for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with ALVAR; if not, see
00021  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
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 }


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Jun 6 2019 21:12:54