uLib-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Dense.h
Go to the documentation of this file.
1 /*//////////////////////////////////////////////////////////////////////////////
2 // CMT Cosmic Muon Tomography project //////////////////////////////////////////
4 
5  Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
6  All rights reserved
7 
8  Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
9 
10  ------------------------------------------------------------------
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library.
23 
25 
26 
27 
28 
29 /*
30  * <one line to give the program's name and a brief idea of what it does.>
31  * Copyright (C) 2012 Andrea Rigoni Garola <andrea@pcimg05>
32  *
33  * This library is free software; you can redistribute it and/or modify
34  * it under the terms of the GNU Lesser General Public License as
35  * published by the Free Software Foundation; either version 2.1 of the
36  * License, or (at your option) any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but
39  * WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41  * Lesser General Public License for more details.
42  *
43  * You should have received a copy of the GNU Lesser General Public
44  * License along with this library; if not, write to the Free Software
45  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
46  * 02110-1301 USA
47  *
48  */
49 
50 
51 #ifndef ULIB_DENSEMATRIX_H
52 #define ULIB_DENSEMATRIX_H
53 
54 #include <stdlib.h>
55 
56 #include <Eigen/Dense>
57 #include "Core/Types.h"
58 #include "Core/Serializable.h"
59 
60 
61 
63 
64 #include <boost/algorithm/string.hpp>
65 #include <boost/algorithm/string/split.hpp>
66 #include <boost/algorithm/string/trim.hpp>
67 #include <boost/lexical_cast.hpp>
68 #include <boost/serialization/string.hpp>
69 #include <boost/serialization/array.hpp>
70 
71 
72 namespace boost {
73 namespace serialization {
74 
75 template<class Archive, class Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
76 void serialize(Archive & ar, ::Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime> & m, const unsigned int /*version*/) {
77  ar & boost::serialization::make_array(m.data(), RowsAtCompileTime * ColsAtCompileTime);
78 }
79 
80 } // serialization
81 } // boost
82 
84 
86 // EIGEN VECTOR STREAM INTERACTIONS ////////////////////////////////////////////
87 // this is needed by boost::lexical_cast to cope with Eigens Vectors ///////////
88 namespace Eigen {
89 template <typename T, int size>
90 std::istream & operator >> (std::istream &is, Eigen::Matrix<T,size,1> &vec) {
91  std::string str;
92  for( unsigned int i=0; i<size; i++) {
93  is >> std::skipws;
94  is >> str;
95  if(is.fail()) vec(i) = 0;
96  else vec(i) = boost::lexical_cast<T>(str);
97  }
98  return is;
99 }
100 template <typename T, int size>
101 std::ostream & operator << (std::ostream &os, const Eigen::Matrix<T,size,1> &vec) {
102  os << vec.transpose();
103  return os;
104 }
105 } // Eigen
107 
108 
109 
110 namespace uLib {
111 
112 
113 typedef int Scalari;
114 typedef unsigned int Scalarui;
115 typedef long Scalarl;
116 typedef unsigned long Scalarul;
117 typedef float Scalarf;
118 typedef double Scalard;
119 
120 
121 
122 typedef Eigen::Matrix<int,1,1> Matrix1i;
126 
127 typedef Eigen::Matrix<float,1,1> Matrix1f;
131 
132 typedef Eigen::Matrix<int,1,1> Vector1i;
136 
137 typedef Eigen::Matrix<float,1,1> Vector1f;
141 
142 
143 
144 
146 // Vector String interaction ///////////////////////////////////////////////////
148 
158 template <typename T, int size>
159 void VectorxT_StringTo(Eigen::Matrix<T,size,1> &vec, std::string str, const char *delim = " ,;\t\n") {
160  std::vector<std::string> strvec;
161 
162  boost::algorithm::trim_if( str, boost::algorithm::is_any_of(delim));
163  boost::algorithm::split(strvec,str,boost::algorithm::is_any_of(delim), boost::algorithm::token_compress_on);
164 
165  for( unsigned int i=0; i<size; i++) {
166  vec(i) = boost::lexical_cast<T>(strvec[i]);
167  }
168 }
169 
170 template <typename T, int size>
171 std::string VectorxT_ToString(const Eigen::Matrix<T,size,1> &vec) {
172  std::stringstream sst;
173  sst << vec.transpose();
174  return sst.str();
175 }
176 
177 
178 //template <typename T, int size>
179 //Eigen::Matrix<T,size,1> & operator >> (std::istream &is, Eigen::Matrix<T,size,1> &vec) {
180 //}
181 
182 template <typename T, int size>
183 void operator>> (std::string& str, Eigen::Matrix<T,size,1> &vec){
184  VectorxT_StringTo(vec,str);
185 }
186 
187 
191 
192 template <bool p>
193 class _HPoint3f : public Eigen::Matrix< Scalarf,4,1 > {
194 public:
195  typedef Eigen::Matrix< Scalarf,4,1 > BaseClass;
196 
197  _HPoint3f<p>() : BaseClass(0,0,0,p) {}
198  _HPoint3f<p>(float x,float y,float z) : BaseClass(x,y,z,p) {}
199  _HPoint3f<p>(Vector3f &in) : BaseClass(in.homogeneous()) { this->operator()(3) = p; }
200 
201  void operator delete(void* _p, size_t _s) {}
202 
203  // This constructor allows to construct MyVectorType from Eigen expressions
204  template<typename OtherDerived>
205  inline _HPoint3f<p>(const Eigen::MatrixBase<OtherDerived>& other)
206  : BaseClass(other)
207  { }
208 
209  // This method allows to assign Eigen expressions to Vector3H
210  template<typename OtherDerived>
211  inline _HPoint3f<p> & operator= (const Eigen::MatrixBase <OtherDerived>& other)
212  {
213  this->BaseClass::operator=(other);
214  return *this;
215  }
216 
217 };
220 
221 
222 
223 
227 
228 
229 struct _HLine3f
230 {
233 };
234 typedef struct _HLine3f HLine3f;
235 
236 inline std::ostream&
237 operator<< (std::ostream& stream, const HLine3f &line) {
238  stream << "HLine3f(" << "pt[" << line.origin.transpose() <<"] , dr[" << line.direction.transpose() << "]) ";
239  return stream;
240 }
241 
242 
243 
244 
245 struct _HError3f
246 {
249 };
250 typedef struct _HError3f HError3f;
251 
252 inline std::ostream&
253 operator<< (std::ostream& stream, const HError3f &err) {
254  stream << "HError3f(" << "ept[" << err.position_error.transpose() <<"] , edr[" << err.direction_error.transpose() << "]) ";
255  return stream;
256 }
257 
258 
259 }
260 
261 
265 // SERIALIZTION //
266 
268 
270 
271 
273 
275 
276 
277 
278 
279 
280 #endif // U_DENSEMATRIX_H
void operator>>(std::string &str, Eigen::Matrix< T, size, 1 > &vec)
Definition: Dense.h:183
Eigen::Matrix3f Matrix3f
Definition: Dense.h:129
Eigen::Matrix< float, 1, 1 > Matrix1f
Definition: Dense.h:127
long Scalarl
Definition: Dense.h:115
Eigen::Matrix< Scalarf, 4, 1 > BaseClass
Definition: Dense.h:195
std::string VectorxT_ToString(const Eigen::Matrix< T, size, 1 > &vec)
Definition: Dense.h:171
Definition: Dense.h:245
Eigen::Matrix3i Matrix3i
Definition: Dense.h:124
unsigned long Scalarul
Definition: Dense.h:116
Eigen::Vector4f Vector4f
Definition: Dense.h:140
Eigen::Vector3i Vector3i
Definition: Dense.h:134
HVector3f position_error
Definition: Dense.h:247
unsigned int Scalarui
Definition: Dense.h:114
struct _HLine3f HLine3f
Definition: Dense.h:234
Definition: Dense.h:193
#define ULIB_SERIALIZABLE(_Ob)
Definition: Serializable.h:173
HVector3f direction
Definition: Dense.h:232
Eigen::Vector3f Vector3f
Definition: Dense.h:139
HPoint3f origin
Definition: Dense.h:231
std::istream & operator>>(std::istream &is, Eigen::Matrix< T, size, 1 > &vec)
Definition: Dense.h:90
Eigen::Matrix2i Matrix2i
Definition: Dense.h:123
void serialize(Archive &ar,::Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > &m, const unsigned int)
Definition: Dense.h:76
struct _HError3f HError3f
Definition: Dense.h:250
double Scalard
Definition: Dense.h:118
Definition: Dense.h:229
_HPoint3f< true > HPoint3f
Definition: Dense.h:219
HVector3f direction_error
Definition: Dense.h:248
Eigen::Matrix4f Matrix4f
Definition: Dense.h:130
float Scalarf
Definition: Dense.h:117
std::ostream & operator<<(std::ostream &stream, const MuonEventData &mu)
Definition: MuonEvent.h:58
Eigen::Matrix< float, 1, 1 > Vector1f
Definition: Dense.h:137
Eigen::Vector4i Vector4i
Definition: Dense.h:135
Eigen::Matrix2f Matrix2f
Definition: Dense.h:128
Eigen::Vector2i Vector2i
Definition: Dense.h:133
void VectorxT_StringTo(Eigen::Matrix< T, size, 1 > &vec, std::string str, const char *delim=" ,;\t\n")
Definition: Dense.h:159
_HPoint3f< false > HVector3f
Definition: Dense.h:218
#define p()
Definition: ObjectProps.h:59
_HPoint3f< p > & operator=(const Eigen::MatrixBase< OtherDerived > &other)
Definition: Dense.h:211
Eigen::Vector2f Vector2f
Definition: Dense.h:138
Eigen::Matrix< int, 1, 1 > Vector1i
Definition: Dense.h:132
int Scalari
Definition: Dense.h:113
Eigen::Matrix4i Matrix4i
Definition: Dense.h:125
Eigen::Matrix< int, 1, 1 > Matrix1i
Definition: Dense.h:122