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
00025
00026
00027
00028
00029
00030
00031
00032
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 }
00162
00163 #endif // __POINTMATCHER_IOFUNCTIONS_H