IOFunctions.h
Go to the documentation of this file.
1 // kate: replace-tabs off; indent-width 4; indent-mode normal
2 // vim: ts=4:sw=4:noexpandtab
3 /*
4 
5 Copyright (c) 2010--2012,
6 François Pomerleau and Stephane Magnenat, ASL, ETHZ, Switzerland
7 You can contact the authors at <f dot pomerleau at gmail dot com> and
8 <stephane at magnenat dot net>
9 
10 All rights reserved.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of the <organization> nor the
20  names of its contributors may be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef __POINTMATCHER_IOFUNCTIONS_H
37 #define __POINTMATCHER_IOFUNCTIONS_H
38 
39 #include <iosfwd>
40 #include <iostream>
41 #include <algorithm>
42 #include <stdexcept>
43 #include <stdio.h>
44 
45 namespace PointMatcherSupport
46 {
47 
48 extern const bool isBigEndian;
49 extern const int oneBigEndian;
50 
51 template <typename T>
53 {
54  union
55  {
56  T v;
57  char bytes[sizeof(T)];
58  };
59 
60  ConverterToAndFromBytes(T v = static_cast<T>(0)) : v(v) {}
61 
62  void swapBytes()
63  {
64  ConverterToAndFromBytes tmp(this->v);
65  std::reverse_copy(tmp.bytes, tmp.bytes + sizeof(T), bytes);
66  }
67 
68  friend std::ostream & operator << (std::ostream & out, const ConverterToAndFromBytes & c)
69  {
70  out.write(c.bytes, sizeof(T));
71  return out;
72  }
73  friend std::istream & operator >> (std::istream & in, ConverterToAndFromBytes & c)
74  {
75  in.read(c.bytes, sizeof(T));
76  return in;
77  }
78 };
79 
80 template<typename Matrix>
81 std::ostream & writeVtkData(bool writeBinary,const Matrix & data, std::ostream & out)
82 {
83  if(writeBinary)
84  {
85  typedef typename Matrix::Scalar TargetDataType;
86  for(int r = 0; r < data.rows(); r++)
87  {
88  for(int c = 0; c < data.cols(); c++)
89  {
90  ConverterToAndFromBytes<TargetDataType> converter(static_cast<TargetDataType>(data(r, c)));
91  if(!isBigEndian)
92  {
93  converter.swapBytes();
94  }
95  out << converter;
96  }
97  }
98  }
99  else
100  {
101  out << data;
102  }
103 
104  return out;
105 }
106 
107 template<typename DataType, typename MatrixRef>
108 std::istream & readVtkData(bool readBinary, MatrixRef into, std::istream & in)
109 {
110  typedef typename MatrixRef::Scalar TargetDataType;
111 
112  for(int r = 0; r < into.rows(); r++)
113  {
114  for(int c = 0; c < into.cols(); c++)
115  {
116  TargetDataType & dest = into(r, c);
117  if(readBinary)
118  {
120  in >> converter;
121  if(!isBigEndian){
122  converter.swapBytes();
123  }
124  dest = converter.v;
125  }
126  else
127  {
128  in >> dest;
129  }
130  }
131  }
132 
133  return in;
134 }
135 
136 template<typename MatrixRef>
137 std::istream & readVtkData(std::string dataType, bool readBinary, MatrixRef into, std::istream & in)
138 {
139  if(dataType == "float")
140  {
141  return readVtkData<float>(readBinary, into, in);
142  }
143  else if (dataType == "double")
144  {
145  return readVtkData<double>(readBinary, into, in);
146  }
147  else if (dataType == "unsigned_int")
148  {
149  return readVtkData<unsigned int>(readBinary, into, in);
150  }
151  else
152  {
153  throw std::runtime_error(std::string("Unsupported data type : " + dataType + "! Expected 'float' or 'double'."));
154  }
155 }
156 
158 std::istream & safeGetLine( std::istream& is, std::string & t);
159 
160 
161 } // PointMatcherSupport
162 
163 #endif // __POINTMATCHER_IOFUNCTIONS_H
PointMatcherSupport::isBigEndian
const bool isBigEndian
true if platform is big endian
Definition: pointmatcher/IO.cpp:62
PointMatcherSupport::readVtkData
std::istream & readVtkData(bool readBinary, MatrixRef into, std::istream &in)
Definition: IOFunctions.h:108
PointMatcherSupport::ConverterToAndFromBytes::v
T v
Definition: IOFunctions.h:56
testing::internal::string
::std::string string
Definition: gtest.h:1979
PointMatcherSupport::writeVtkData
std::ostream & writeVtkData(bool writeBinary, const Matrix &data, std::ostream &out)
Definition: IOFunctions.h:81
PointMatcherSupport::ConverterToAndFromBytes::swapBytes
void swapBytes()
Definition: IOFunctions.h:62
PointMatcherSupport::ConverterToAndFromBytes
Definition: IOFunctions.h:52
kitti-batch-convert.out
string out
Definition: kitti-batch-convert.py:7
PointMatcherSupport::ConverterToAndFromBytes::operator<<
friend std::ostream & operator<<(std::ostream &out, const ConverterToAndFromBytes &c)
Definition: IOFunctions.h:68
PointMatcherSupport::safeGetLine
std::istream & safeGetLine(std::istream &is, std::string &t)
Replaces getline for handling windows style CR/LF line endings.
Definition: IOFunctions.cpp:7
PointMatcherSupport::oneBigEndian
const int oneBigEndian
is always a big endian independent of the platforms endianness
Definition: pointmatcher/IO.cpp:63
PointMatcherSupport::ConverterToAndFromBytes::bytes
char bytes[sizeof(T)]
Definition: IOFunctions.h:57
PointMatcherSupport::ConverterToAndFromBytes::ConverterToAndFromBytes
ConverterToAndFromBytes(T v=static_cast< T >(0))
Definition: IOFunctions.h:60
PointMatcherSupport::ConverterToAndFromBytes::operator>>
friend std::istream & operator>>(std::istream &in, ConverterToAndFromBytes &c)
Definition: IOFunctions.h:73
PointMatcherSupport
Functions and classes that are not dependant on scalar type are defined in this namespace.
Definition: Bibliography.cpp:45


mp2p_icp
Author(s):
autogenerated on Fri Dec 20 2024 03:45:59