IOFunctions.h
Go to the documentation of this file.
00001 // kate: replace-tabs off; indent-width 4; indent-mode normal
00002 // vim: ts=4:sw=4:noexpandtab
00003 /*
00004 
00005 Copyright (c) 2010--2012,
00006 François Pomerleau and Stephane Magnenat, ASL, ETHZ, Switzerland
00007 You can contact the authors at <f dot pomerleau at gmail dot com> and
00008 <stephane at magnenat dot net>
00009 
00010 All rights reserved.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014     * Redistributions of source code must retain the above copyright
00015       notice, this list of conditions and the following disclaimer.
00016     * Redistributions in binary form must reproduce the above copyright
00017       notice, this list of conditions and the following disclaimer in the
00018       documentation and/or other materials provided with the distribution.
00019     * Neither the name of the <organization> nor the
00020       names of its contributors may be used to endorse or promote products
00021       derived from this software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00026 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
00027 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00028 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00030 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #ifndef __POINTMATCHER_IOFUNCTIONS_H
00037 #define __POINTMATCHER_IOFUNCTIONS_H
00038 
00039 #include <iosfwd>
00040 #include <iostream>
00041 #include <algorithm>
00042 #include <stdexcept>
00043 #include <stdio.h>
00044 
00045 namespace PointMatcherSupport
00046 {
00047 
00048 extern const bool isBigEndian; 
00049 extern const int oneBigEndian; 
00050 
00051 template <typename T>
00052 struct ConverterToAndFromBytes 
00053 {
00054         union 
00055         {
00056                 T v;
00057                 char bytes[sizeof(T)];
00058         };
00059 
00060         ConverterToAndFromBytes(T v = static_cast<T>(0)) : v(v) {}
00061 
00062         void swapBytes()
00063         {
00064                 ConverterToAndFromBytes tmp(this->v);
00065                 std::reverse_copy(tmp.bytes, tmp.bytes + sizeof(T), bytes);
00066         }
00067 
00068         friend std::ostream & operator << (std::ostream & out, const ConverterToAndFromBytes & c)
00069         {
00070                 out.write(c.bytes, sizeof(T));
00071                 return out;
00072         }
00073         friend std::istream & operator >> (std::istream & in, ConverterToAndFromBytes & c)
00074         {
00075                 in.read(c.bytes, sizeof(T));
00076                 return in;
00077         }
00078 };
00079 
00080 template<typename Matrix>
00081 std::ostream & writeVtkData(bool writeBinary,const Matrix & data, std::ostream & out)
00082 {
00083         if(writeBinary)
00084         {
00085                 typedef typename Matrix::Scalar TargetDataType;
00086                 for(int r = 0; r < data.rows(); r++)
00087                 {
00088                         for(int c = 0; c < data.cols(); c++)
00089                         {
00090                                 ConverterToAndFromBytes<TargetDataType> converter(static_cast<TargetDataType>(data(r, c)));
00091                                 if(!isBigEndian)
00092                                 {
00093                                         converter.swapBytes();
00094                                 }
00095                                 out << converter;
00096                         }
00097                 }
00098         }
00099         else 
00100         {
00101                 out << data;
00102         }
00103 
00104         return out;
00105 }
00106 
00107 template<typename DataType, typename MatrixRef>
00108 std::istream & readVtkData(bool readBinary, MatrixRef into, std::istream & in)
00109 {
00110         typedef typename MatrixRef::Scalar TargetDataType;
00111         
00112         for(int r = 0; r < into.rows(); r++)
00113         {
00114                 for(int c = 0; c < into.cols(); c++)
00115                 {
00116                         TargetDataType & dest = into(r, c);
00117                         if(readBinary)
00118                         {
00119                                 ConverterToAndFromBytes<DataType> converter;
00120                                 in >> converter;
00121                                 if(!isBigEndian){
00122                                         converter.swapBytes();
00123                                 }
00124                                 dest = converter.v;
00125                         }
00126                         else 
00127                         {
00128                                 in >> dest;
00129                         }
00130                 }
00131         }
00132 
00133         return in;
00134 }
00135 
00136 template<typename MatrixRef>
00137 std::istream & readVtkData(std::string dataType, bool readBinary, MatrixRef into, std::istream & in)
00138 {
00139         if(dataType == "float")
00140         {
00141                 return readVtkData<float>(readBinary, into, in);
00142         } 
00143         else if (dataType == "double") 
00144         {
00145                 return readVtkData<double>(readBinary, into, in);
00146         } 
00147         else if (dataType == "unsigned_int") 
00148         {
00149                 return readVtkData<unsigned int>(readBinary, into, in);
00150         }
00151         else 
00152         {
00153                 throw std::runtime_error(std::string("Unsupported data type : " + dataType + "! Expected 'float' or 'double'."));
00154         }
00155 }
00156 
00158 std::istream & safeGetLine( std::istream& is, std::string & t);
00159 
00160 
00161 } // PointMatcherSupport
00162 
00163 #endif // __POINTMATCHER_IOFUNCTIONS_H


libpointmatcher
Author(s):
autogenerated on Thu Jun 20 2019 19:51:31