[uLib Geometry]

- Adds Type combiner.
- change names to image map (WARNING some classess may not be already uploaded)
This commit is contained in:
Andrea Rigoni
2014-11-29 07:25:35 +00:00
parent 41fa82d2ef
commit 891f46d502
27 changed files with 1146 additions and 99 deletions

View File

@@ -57,7 +57,19 @@ endif()
set(CMAKE_CXX_WARNING_OPTION "" CACHE STRING "Warning level -WAll to verbose all warnings") set(CMAKE_CXX_WARNING_OPTION "" CACHE STRING "Warning level -WAll to verbose all warnings")
set(CMAKE_VERBOSE_MAKEFILE FALSE CACHE STRING "Verbose compile output switch") set(CMAKE_VERBOSE_MAKEFILE FALSE CACHE STRING "Verbose compile output switch")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x ${CMAKE_CXX_WARNING_OPTION}")
## FAST MATH OPTIONS -------------------------------------------------------- ##
set(CMAKE_CXX_FAST_MATH_OPTIONS "" CACHE STRING "Fast math options for compiler")
set_property(CACHE CMAKE_CXX_FAST_MATH_OPTIONS PROPERTY STRINGS
""
"-ftree-vectorize -march=native -ffast-math"
"-ftree-vectorize -march=native -fassociative-math"
"-ftree-vectorize -march=native -ffast-math -ftree-vectorizer-verbose=7 -fopt-info-vec -fopt-info-vec-missed"
"-ftree-vectorize -march=native -fassociative-math -ftree-vectorizer-verbose=7 -fopt-info-vec -fopt-info-vec-missed"
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x ${CMAKE_CXX_WARNING_OPTION} ${CMAKE_CXX_FAST_MATH_OPTIONS}")

View File

@@ -13,7 +13,12 @@ set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON) set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS serialization signals program_options REQUIRED) find_package(Boost 1.45.0 COMPONENTS
serialization
signals
program_options
REQUIRED)
if(Boost_FOUND) if(Boost_FOUND)
set(HAVE_BOOST true) set(HAVE_BOOST true)
endif(Boost_FOUND) endif(Boost_FOUND)

View File

@@ -22,6 +22,8 @@ set(HEADERS
Vector.h Vector.h
CommaInitializer.h CommaInitializer.h
ProgrammableAccessor.h ProgrammableAccessor.h
Named.h
MplSequenceCombiner.h
) )
SET(SOURCES SET(SOURCES

330
src/Core/ClassFactory.h Normal file
View File

@@ -0,0 +1,330 @@
#ifndef U_CORE_CLASSFACTORY_H
#define U_CORE_CLASSFACTORY_H
#include <map>
#include <vector>
#include <boost/mpl/unpack_args.hpp>
#include "Core/Mpl.h"
#include "Core/MplSequenceCombiner.h"
#include "Core/Types.h"
namespace uLib {
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// generator //
template <class BaseType, class T>
static BaseType * newClass() { return new T; }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Run time named class factory //
template <class BaseType>
class NamedClassFactory {
typedef BaseType*(*_newClassfn)();
typedef std::map<const char*,_newClassfn> MapType;
public:
template<typename Type>
void AddType(const char* name) {
m_map.insert(std::make_pair(name, &newClass<BaseType,Type>));
}
BaseType *Create(const char *type) {
typename MapType::iterator itr = m_map.find(type);
return itr->second();
}
void List(std::ostream &o) {
for(typename MapType::iterator itr = m_map.begin(); itr!=m_map.end(); itr++ )
o << itr->first << "\n";
}
private:
MapType m_map;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// MPL sequence registered class factory //
template <class BaseType, typename RegSeqT>
class RegisteredClassFactory {
typedef BaseType*(*_newClassfn)();
typedef std::vector<_newClassfn> TypeVectorType;
struct AddTypeOp {
AddTypeOp( RegisteredClassFactory *_p) : m_parent(_p) {}
template <typename U> void operator ()(U x) {
m_parent->addType<U>();
}
RegisteredClassFactory *m_parent;
};
template < typename T >
void addType() { m_map.push_back(&newClass<BaseType,T>); }
public:
RegisteredClassFactory() {
mpl::for_each<RegSeqT>(AddTypeOp(this));
}
template < typename T >
int Find() {
typedef typename mpl::find<RegSeqT,T>::type iter;
return iter::pos::value;
}
template < typename T >
BaseType *Create() {
int id = Find<T>();
typename TypeVectorType::iterator itr = m_map.begin() + id;
return (*itr)();
}
private:
TypeVectorType m_map;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template <
class BaseType,
template <class T1> class DerivedType,
typename S1
>
class RegisteredClassFactory1 {
typedef BaseType*(*_newClassfn)();
typedef std::vector<_newClassfn> TypeVectorType;
template < typename U >
void addType() { m_map.push_back(&newClass<
BaseType,
DerivedType<U>
>); }
struct AddTypeSeqOp {
AddTypeSeqOp( RegisteredClassFactory1 *_p) : m_parent(_p) {}
template < typename Seq >
void operator()(Seq) {
m_parent->addType<
typename mpl::at<Seq, mpl::int_<0> >::type
> ();
}
RegisteredClassFactory1 *m_parent;
};
typedef mpl::combine_view< mpl::vector<S1> > AlgView;
public:
RegisteredClassFactory1() {
mpl::for_each< AlgView >(AddTypeSeqOp(this));
}
template < typename T >
static inline int FindS1() {
typedef typename mpl::find<S1,T>::type iter;
return iter::pos::value;
}
BaseType *Create(int id1) {
typename TypeVectorType::iterator itr = m_map.begin() + id1;
return (*itr)();
}
private:
TypeVectorType m_map;
static const int m_size1 = mpl::size<S1>::type::value;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template <
class BaseType,
template <class T1, class T2> class DerivedType,
typename S1,
typename S2
>
class RegisteredClassFactory2 {
typedef BaseType*(*_newClassfn)();
typedef std::vector<_newClassfn> TypeVectorType;
template < typename U, typename V >
void addType() { m_map.push_back(&newClass<
BaseType,
DerivedType<U,V>
>); }
struct AddTypeSeqOp {
AddTypeSeqOp( RegisteredClassFactory2 *_p) : m_parent(_p) {}
template < typename Seq >
void operator()(Seq) {
m_parent->addType<
typename mpl::at<Seq, mpl::int_<0> >::type,
typename mpl::at<Seq, mpl::int_<1> >::type
> ();
}
RegisteredClassFactory2 *m_parent;
};
typedef mpl::combine_view< mpl::vector<S1,S2> > AlgView;
public:
RegisteredClassFactory2() {
mpl::for_each< AlgView >(AddTypeSeqOp(this));
}
template < typename T >
static inline int FindS1() {
typedef typename mpl::find<S1,T>::type iter;
return iter::pos::value;
}
template < typename T >
static inline int FindS2() {
typedef typename mpl::find<S2,T>::type iter;
return iter::pos::value;
}
BaseType *Create(int id1, int id2) {
typename TypeVectorType::iterator itr = m_map.begin() + id1 + m_size1 * id2;
return (*itr)();
}
private:
TypeVectorType m_map;
static const int m_size1 = mpl::size<S1>::type::value;
static const int m_size2 = mpl::size<S2>::type::value;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template <
class BaseType,
template <class T1, class T2, class T3> class DerivedType,
typename S1,
typename S2,
typename S3
>
class RegisteredClassFactory3 {
typedef BaseType*(*_newClassfn)();
typedef std::vector<_newClassfn> TypeVectorType;
template < typename T1, typename T2, typename T3 >
void addType() { m_map.push_back(&newClass<
BaseType,
DerivedType<T1,T2,T3>
>); }
struct AddTypeSeqOp {
AddTypeSeqOp( RegisteredClassFactory3 *_p) : m_parent(_p) {}
template < typename Seq >
void operator()(Seq) {
m_parent->addType<
typename mpl::at<Seq, mpl::int_<0> >::type,
typename mpl::at<Seq, mpl::int_<1> >::type,
typename mpl::at<Seq, mpl::int_<2> >::type
> ();
}
RegisteredClassFactory3 *m_parent;
};
typedef mpl::combine_view< mpl::vector<S1,S2,S3> > AlgView;
public:
RegisteredClassFactory3() {
mpl::for_each< AlgView >(AddTypeSeqOp(this));
}
template < typename T >
static inline int FindS1() {
typedef typename mpl::find<S1,T>::type iter;
return iter::pos::value;
}
template < typename T >
static inline int FindS2() {
typedef typename mpl::find<S2,T>::type iter;
return iter::pos::value;
}
template < typename T >
static inline int FindS3() {
typedef typename mpl::find<S3,T>::type iter;
return iter::pos::value;
}
BaseType *Create(int id1, int id2, int id3) {
typename TypeVectorType::iterator itr = m_map.begin() + id1 + m_size1 * id2 + m_size1 * m_size3 * id3;
return (*itr)();
}
private:
TypeVectorType m_map;
static const int m_size1 = mpl::size<S1>::type::value;
static const int m_size2 = mpl::size<S2>::type::value;
static const int m_size3 = mpl::size<S3>::type::value;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
} // uLib
#endif // CLASSFACTORY_H

View File

@@ -40,13 +40,18 @@
#include <boost/mpl/vector/vector20.hpp> #include <boost/mpl/vector/vector20.hpp>
#include <boost/mpl/copy.hpp> #include <boost/mpl/copy.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/transform.hpp> #include <boost/mpl/transform.hpp>
#include <boost/mpl/transform_view.hpp> #include <boost/mpl/transform_view.hpp>
#include <boost/mpl/filter_view.hpp> #include <boost/mpl/filter_view.hpp>
#include <boost/mpl/list.hpp> #include <boost/mpl/list.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/string.hpp> #include <boost/mpl/string.hpp>
#include <boost/mpl/for_each.hpp> #include <boost/mpl/for_each.hpp>
#include <boost/mpl/same_as.hpp>
#include <boost/mpl/has_xxx.hpp> #include <boost/mpl/has_xxx.hpp>
#include <boost/utility/enable_if.hpp> #include <boost/utility/enable_if.hpp>
@@ -65,6 +70,7 @@ namespace mpl {
using namespace boost::mpl; using namespace boost::mpl;
using namespace boost::mpl::placeholders; using namespace boost::mpl::placeholders;
//using boost::enable_if; //using boost::enable_if;
namespace detail { namespace detail {
@@ -132,8 +138,10 @@ struct nvp {
}; };
} // mpl
} // mpl
} // uLib } // uLib
#endif // MPL_H #endif // MPL_H

View File

@@ -0,0 +1,160 @@
#ifndef U_CORE_MPLSEQUENCECOMBINER_H
#define U_CORE_MPLSEQUENCECOMBINER_H
#include <boost/mpl/unpack_args.hpp>
#include <boost/mpl/back.hpp>
#include <boost/mpl/pop_back.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/zip_view.hpp>
#include <boost/mpl/prior.hpp>
#include <Core/Mpl.h>
namespace uLib {
namespace mpl {
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template <class Seq, class ItrSeq>
class SequenceCombiner {
template < class _Seq = mpl::vector<int_<1> > >
struct StateSeq {
typedef typename pop_front<_Seq>::type sequence;
typedef typename mpl::at< _Seq, int_<0> >::type state;
typedef _Seq type;
};
template < class _Seq, class _State >
struct set_state {
typedef StateSeq< typename push_front<_Seq, _State >::type > type;
};
struct NextOp {
template < typename Out, typename In, typename Enable = typename Out::state >
class apply {
typedef typename Out::sequence seq;
typedef typename Out::state new_state;
typedef typename mpl::at<In,int_<0> >::type in_seq;
typedef typename mpl::at<In,int_<1> >::type in_itr;
typedef typename mpl::push_back<seq, in_itr >::type new_seq;
public:
typedef typename set_state<new_seq, int_<0> >::type type;
};
template < typename Out, typename In >
class apply<Out,In,mpl::int_<1> > {
typedef typename Out::sequence seq;
typedef typename Out::state state;
typedef typename mpl::at<In,int_<0> >::type in_seq;
typedef typename mpl::at<In,int_<1> >::type in_itr;
typedef typename mpl::begin<in_seq>::type Itr_begin;
typedef typename mpl::next<in_itr>::type Itr_next;
typedef typename mpl::end<in_seq>::type Itr_end;
typedef typename mpl::if_< boost::is_same<Itr_next,Itr_end>,
typename mpl::push_back<seq,Itr_begin>::type,
typename mpl::push_back<seq,Itr_next>::type
>::type
new_seq;
typedef typename mpl::if_< boost::is_same<Itr_next,Itr_end>,
mpl::int_<1>,
mpl::int_<0>
>::type
new_state;
public:
typedef typename set_state<new_seq, new_state>::type type;
};
};
typedef typename mpl::fold<
typename mpl::zip_view< mpl::vector<Seq, ItrSeq > >::type,
StateSeq<>,
NextOp
>::type
StateResult;
public:
typedef typename mpl::if_< boost::is_same< typename StateResult::state, int_<1> >,
typename mpl::transform< Seq, mpl::end<_1> >::type,
typename StateResult::sequence >::type
next;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template < typename Seq, typename Itrs >
struct combine_iterator {
typedef mpl::forward_iterator_tag category;
typedef Seq seq;
typedef typename transform <
Itrs,
deref<_1>
>::type
type;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template < class Seq >
class combine_view {
typedef typename mpl::transform<Seq, mpl::begin<_1> >::type Itrs_begin;
typedef typename mpl::transform<Seq, mpl::end<_1> >::type Itrs_end;
public:
typedef combine_iterator<Seq, Itrs_begin> begin;
typedef combine_iterator<Seq, Itrs_end> end;
typedef combine_view type;
};
} // mpl
} // uLib
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
namespace boost {
namespace mpl {
template <class Seq, class Pos>
struct next< uLib::mpl::combine_iterator<Seq, Pos> > {
typedef typename uLib::mpl::SequenceCombiner<Seq,Pos>::next next_Pos;
typedef uLib::mpl::combine_iterator< Seq, next_Pos > type;
};
} // mpl
} // boost
#endif // MPLSEQUENCECOMBINER_H

View File

@@ -52,7 +52,7 @@
typedef uLib::mpl::bool_<true>::type propable_trait; \ typedef uLib::mpl::bool_<true>::type propable_trait; \
public: struct DLL_PUBLIC ObjectProps public: struct DLL_PUBLIC ObjectProps
#define properties() ULIB_props() #define uLib_properties() ULIB_props()
#define default(vlaue) #define default(vlaue)

View File

@@ -32,6 +32,7 @@ public:
any_c(const any & other) : any(other) {} any_c(const any & other) : any(other) {}
// FIX !! //
// this is needed to tait static_cast using any_cast function // // this is needed to tait static_cast using any_cast function //
template <typename _TT> template <typename _TT>
operator _TT () const { return boost::any_cast<_TT>(*this); } operator _TT () const { return boost::any_cast<_TT>(*this); }
@@ -77,10 +78,10 @@ private:
template<typename D, typename R, typename T> template<typename D, typename R, typename T>
class functor_by_mfptr_cc : public ProgrammableAccessor_Base<D> class functor_by_mfptr_cc : public ProgrammableAccessor_Base<D>
{ {
typedef R(T::*GetterType)() const; typedef R(T::*GeT)() const;
typedef void(T::*SetterType)(const R); typedef void(T::*SeT)(const R);
public: public:
explicit functor_by_mfptr_cc( GetterType _get, SetterType _set ) : explicit functor_by_mfptr_cc( GeT _get, SeT _set ) :
GetPtr(_get), SetPtr(_set) {} GetPtr(_get), SetPtr(_set) {}
D Get(void * ptr) const D Get(void * ptr) const
{ return static_cast<D>((reinterpret_cast<T *>(ptr)->*GetPtr)()); } { return static_cast<D>((reinterpret_cast<T *>(ptr)->*GetPtr)()); }
@@ -88,8 +89,8 @@ public:
{ if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(static_cast<const R>(data)); } { if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(static_cast<const R>(data)); }
private: private:
GetterType GetPtr; GeT GetPtr;
SetterType SetPtr; SeT SetPtr;
}; };
template<typename D, typename R, typename T> template<typename D, typename R, typename T>
@@ -114,6 +115,11 @@ private:
template<typename D, typename R, typename T> template<typename D, typename R, typename T>
class functor_by_member : public ProgrammableAccessor_Base<D> class functor_by_member : public ProgrammableAccessor_Base<D>
{ {
template < R T::*_pf >
class StaticWrapper {
static D Get(void *ptr) { return static_cast<D>(reinterpret_cast<T *>(ptr)->*_pf); }
static void Set(void *ptr, const D data) { (reinterpret_cast<T *>(ptr)->*_pf) = static_cast<const D>(data); }
};
public: public:
explicit functor_by_member(R T::*__pf) : MDPtr(__pf) {} explicit functor_by_member(R T::*__pf) : MDPtr(__pf) {}
D Get(void * ptr) const D Get(void * ptr) const
@@ -193,6 +199,7 @@ public:
} }
// ------ // // ------ //
const Wrapper operator() (void *ob) const { return Wrapper(this,ob); } const Wrapper operator() (void *ob) const { return Wrapper(this,ob); }
inline D Get(void *ob) const { return m_base->Get(ob); } inline D Get(void *ob) const { return m_base->Get(ob); }

View File

@@ -30,6 +30,23 @@
namespace uLib { namespace uLib {
// SFINAE //
/*
template <typename T>
struct has_type_info {
typedef char yes[1];
typedef char no[2];
template <typename U> static yes& test(typename U::type_info::BaseList*);
template <typename > static no& test(...);
// struct apply {
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
typedef boost::mpl::bool_<value> type;
// };
};
*/
#define MAKE_TRAITS ; // TODO #define MAKE_TRAITS ; // TODO
#define uLibCheckFunction(class_name,name,out, ... ) \ #define uLibCheckFunction(class_name,name,out, ... ) \
@@ -56,22 +73,24 @@ namespace Interface {
//}; //};
template <class T, class SI> template <class T, class SI>
static inline void IsA() { static inline void IsA(T *t = 0) {
(void) ((void (SI::*)()) &SI::template check_structural<T>); (void) ((void (SI::*)()) &SI::template check_structural<T>);
} }
// not working template <class T, class SI>
//template <class T, class SI> static inline void IsA(T &t) {
//struct StaticIsA { (void) ((void (SI::*)()) &SI::template check_structural<T>);
// void test() {
// static const void (SI::*x)() = SI::template check_structural<T>;
// }
//};
} }
template <class T, class SI>
struct StaticIsA {
StaticIsA() {
void (SI::*x)() = &SI::template check_structural<T>;
(void) x;
}
};
} // Interface
} } // uLib
#endif // STATICINTERFACE_H #endif // STATICINTERFACE_H

View File

@@ -53,8 +53,10 @@ public:
Vector(unsigned int size) : BaseClass(size) {} Vector(unsigned int size) : BaseClass(size) {}
Vector(unsigned int size, T &value) : BaseClass(size,value) {} Vector(unsigned int size, T &value) : BaseClass(size,value) {}
Vector(const Vector<T> &copy) : BaseClass(copy) {}
Vector() : BaseClass(0) {} Vector() : BaseClass(0) {}
inline VectorCommaInit operator <<(T scalar) { inline VectorCommaInit operator <<(T scalar) {
return VectorCommaInit(this, scalar); return VectorCommaInit(this, scalar);
} }

View File

@@ -21,6 +21,9 @@ set( TESTS
UuidTest UuidTest
TypeIntrospectionTraversal TypeIntrospectionTraversal
OptionsTest OptionsTest
ProgrammableAccessorTest
ClassFactoryTest
MplSequenceCombinerTest
) )
set(LIBRARIES set(LIBRARIES

View File

@@ -0,0 +1,323 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#include <iostream>
#include "Core/MplSequenceCombiner.h"
#include "Core/ClassFactory.h"
#include "Core/Serializable.h"
#include "testing-prototype.h"
using namespace uLib;
//namespace {
class Base {
public:
Base() {}
Base(const Base &copy) {}
virtual void Go() {}
};
class Derived : public Base {
public:
void Go() { std::cout << "Derived Go\n"; }
};
class Derived2 : public Base {
public:
void Go() { std::cout << "Derived2 Go\n"; }
};
class Derived3 : public Base {
public:
void Go() { std::cout << "Derived3 Go\n"; }
};
class Derived4 : public Base {
public:
void Go() { std::cout << "Derived4 Go\n"; }
};
template <
class A1,
class A2
>
class AlgHolder : public Base {
A1 m_a1;
A2 m_a2;
public:
void Go() {
std::cout << "AlgHolder Go: \n";
std::cout << " A1 -> "; m_a1.Go();
std::cout << " A2 -> "; m_a2.Go();
}
};
//}
typedef mpl::vector< Derived, Derived2, Derived3, Derived4 > seq;
class ProgrammableAlgHolder {
public:
ProgrammableAlgHolder() : m_id1(0), m_id2(0)
{ m_base = m_factory.Create(0,0); }
template < typename T >
void SetA1(const T *alg = NULL) {
int id = m_factory.FindS1<T>();
Base * newbase = m_factory.Create(id,m_id2);
*newbase = *m_base;
delete m_base;
m_base = newbase;
}
template < typename T >
void SetA2(const T *alg = NULL) {
int id = m_factory.FindS2<T>();
Base * newbase = m_factory.Create(m_id1,id);
*newbase = *m_base;
delete m_base;
m_base = newbase;
}
Base * operator -> () { return m_base; }
private:
RegisteredClassFactory2<Base, AlgHolder, seq, seq> m_factory;
int m_id1, m_id2;
Base * m_base;
};
//int main() {
// ProgrammableAlgHolder ah;
// ah.SetA1<Derived4>();
// ah->Go();
// return 0;
//}
////////////////////////////////////////////////////////////////////////////////
// WORKING EXAMPLE //
namespace Test {
class Voxel {
public:
virtual void Set(float) = 0;
virtual float Get() const = 0;
virtual ~Voxel() {}
};
class VoxelMean : public Voxel {
public:
VoxelMean() : m_data(0), m_count(0) {}
void Set(float data) { m_data += data; ++m_count; }
float Get() const { return m_data / m_count; }
private:
float m_data;
int m_count;
};
class VoxelVal : public Voxel {
public:
VoxelVal() : m_data(0) {}
void Set(float data) { m_data = data; }
float Get() const { return m_data; }
private:
float m_data;
};
class VoxCount {
public:
template < class T >
int Count(const Vector<T> &ref ) {
return ref.size();
}
};
class VoxCountOver {
public:
VoxCountOver() : m_threshold(0) {}
template < class T >
int Count(const Vector<T> &ref ) {
int count = 0 ;
foreach (const T &el, ref) {
if( el.Get() >= m_threshold ) ++count;
}
return count;
}
float m_threshold;
};
class VoxelVectorBase {
public:
virtual float Get(int) const = 0;
virtual void Set(int,float) = 0;
virtual int Size() const = 0;
virtual int Count() const = 0;
virtual ~VoxelVectorBase() {}
};
template < class T >
void copy(T *src, T *dst) { }
void copy(VoxelVectorBase *src, VoxelVectorBase *dst) {
for(int i=0; i<src->Size(); ++i) {
dst->Set(i,src->Get(i));
}
}
template < class VoxType,
class CounterType >
class VoxelVector : public VoxelVectorBase {
public:
float Get(int id) const { return m_data[id].Get(); }
void Set(int id, float data) {
if(id >= Size()) m_data.resize(id+1);
m_data[id].Set(data);
}
int Size() const { return m_data.size(); }
int Count() const { return m_counter.Count(m_data); }
private:
friend class VoxelVectorFactory;
CounterType m_counter;
Vector<VoxType> m_data;
};
class VoxelVectorFactory {
public:
VoxelVectorFactory() : m_id1(0), m_id2(0) { m_base = m_factory.Create(0,0); }
VoxelVectorBase * operator -> () const { return m_base; }
template < typename T >
void SetVoxel() {
int id = m_factory.FindS1<T>();
VoxelVectorBase * newbase = m_factory.Create(id,m_id2);
Test::copy(m_base,newbase); // *newbase = *m_base;
delete m_base;
m_base = newbase;
}
template < typename T >
void SetCounter(const T &ref) {
int id = m_factory.FindS2<T>();
VoxelVectorBase * newbase = m_factory.Create(m_id1,id);
Test::copy(m_base,newbase); // *newbase = *m_base;
// ????? come copiare i membri ???? ////
delete m_base;
m_base = newbase;
}
private:
RegisteredClassFactory2< VoxelVectorBase, VoxelVector, mpl::vector<VoxelMean,VoxelVal>, mpl::vector<VoxCount,VoxCountOver> > m_factory;
int m_id1, m_id2;
VoxelVectorBase * m_base;
};
} // Test
int main() {
Test::VoxelVectorFactory factory;
for(int i=0; i<10; ++i)
factory->Set(i,i);
factory.SetVoxel<Test::VoxelVal>();
for(int i=0; i<10; ++i)
std::cout << factory->Get(i) << " ";
std::cout << "\n";
std::cout << "vox count = " << factory->Count() << "\n";
}

View File

@@ -84,6 +84,19 @@ public:
} // uLib } // uLib
namespace uLib {
namespace detail {
} // detail
} // uLib
using namespace uLib; using namespace uLib;
int main() { int main() {

View File

@@ -0,0 +1,73 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#include <iostream>
#include <Core/Types.h>
#include <Core/MplSequenceCombiner.h>
#include <Core/ClassFactory.h>
#include "testing-prototype.h"
using namespace uLib;
struct A {};
struct B {};
struct C {};
struct D {};
struct E {};
struct F {};
struct G {};
struct H {};
struct I {};
namespace {
struct PrintTypeId {
template <class T>
void operator()(T) const
{ std::cout << typeid(T).name() << " "; }
};
struct PrintSeq {
template < typename T >
void operator()(T) {
mpl::for_each<T>( PrintTypeId() );
std::cout << "\n";
}
};
}
int main() {
BEGIN_TESTING( Mpl Sequence Combiner Test);
typedef mpl::vector<A,B,C> seq1;
typedef mpl::vector<D,E,F> seq2;
typedef mpl::vector<G,H,I> seq3;
typedef mpl::combine_view< mpl::vector<seq1,seq2,seq3> > cv;
mpl::for_each< cv >( PrintSeq() );
END_TESTING;
}

View File

@@ -60,7 +60,7 @@ struct A : virtual Object {
uLibTypeMacro(A, Object) uLibTypeMacro(A, Object)
properties() { uLib_properties() {
int p_a; int p_a;
Vector3f p_3f; Vector3f p_3f;
}; };
@@ -84,7 +84,7 @@ ULIB_SERIALIZE_OBJECT_PROPS(A) { ar & AR(p_a) & AR(p_3f); }
struct B : A { struct B : A {
uLibTypeMacro(B,A) uLibTypeMacro(B,A)
properties() { uLib_properties() {
std::string p; std::string p;
}; };
@@ -118,7 +118,7 @@ ULIB_SERIALIZE(C) { ar & AR(m_c) & AR(m_str); }
struct D : virtual Object, B { struct D : virtual Object, B {
uLibTypeMacro(D,Object,B) uLibTypeMacro(D,Object,B)
properties() { uLib_properties() {
C p_c; C p_c;
}; };
}; };

View File

@@ -0,0 +1,64 @@
#include <iostream>
#include <typeinfo>
#include <functional>
#include "testing-prototype.h"
#include "Core/Types.h"
#include <Core/Vector.h>
#include <Core/ProgrammableAccessor.h>
#include <iterator>
namespace uLib {
class TestVoxel {
public:
float m_data;
float Get() { return m_data; }
float ConstGet() const { return m_data; }
float & GetRef() { return m_data; }
const float & ConstGetRef() const { return m_data; }
};
} // uLib
using namespace uLib;
int main() {
BEGIN_TESTING(Function Test);
Vector<TestVoxel> v;
v.push_back(TestVoxel());
v.push_back(TestVoxel());
v.push_back(TestVoxel());
v.push_back(TestVoxel());
v.push_back(TestVoxel());
v.push_back(TestVoxel());
v.push_back(TestVoxel());
v[5].m_data = 5552368.0;
ProgrammableAccessor<int> f1(&TestVoxel::m_data);
ProgrammableAccessor<char> f2(&TestVoxel::ConstGet);
int i=0;
foreach (TestVoxel &el,v) {
f1.Set(&el,i++);
std::cout << " -> " << f2.Get(&el) << " - " << f1(&el).Get<float>() << "\n";
}
END_TESTING;
}

View File

@@ -115,7 +115,7 @@ class A : public virtual Object {
public: public:
A() : m_a(5552368) {} A() : m_a(5552368) {}
properties() { uLib_properties() {
std::string p_a; std::string p_a;
}; };

View File

@@ -45,33 +45,13 @@ struct Test {
uLibCheckMember(Self,testmemb,int); uLibCheckMember(Self,testmemb,int);
} }
}; };
} } // Interface
struct Test { struct Test {
bool test(int i, float f){} bool test(int i, float f){}
int testmemb; int testmemb;
}; };
//// INTERFAC TO SIMPLE CLASS ///////////
namespace Interface {
struct Simple {
MAKE_TRAITS
template<class Self> void check_structural() {
uLibCheckMember(Self,memb1,int);
uLibCheckMember(Self,memb2,float);
}
};
}
struct Simple {
int memb1;
float memb2;
};
///////////////////////// /////////////////////////
template <class T> template <class T>
@@ -85,27 +65,31 @@ public:
} }
}; };
template <class T> template <class T>
class UseSimple { class UseTest2 {
Interface::StaticIsA<T, Interface::Test> x;
public: public:
UseSimple() { UseTest2() {
Interface::IsA<T,Interface::Simple>(); T t;
int i; float f;
t.test(i,f);
} }
}; };
} // uLib
}
using namespace uLib;
int main() int main()
{ {
BEGIN_TESTING(Static Interface); BEGIN_TESTING(Static Interface);
UseTest<Test> u;
UseTest2<Test> u2;
uLib::UseTest<uLib::Test> u;
uLib::UseSimple<uLib::Simple> s;
END_TESTING; END_TESTING;
} }

View File

@@ -67,7 +67,16 @@ int main()
uLib::Vector<float> v; uLib::Vector<float> v;
v << 5,4,3,2,6,1,2,3,65,7,32,23,4,3,45,4,34,3,4,4,3,3,4,2,2,3; v << 5,4,3,2,6,1,2,3,65,7,32,23,4,3,45,4,34,3,4,4,3,3,4,2,2,3;
uLib::Vector<float> v2;
v2 = v;
std::cout << "v2: " << v2;
{
int i = 0;
foreach (i , v) {
TEST1(v[i] == v2[i]);
}
}
int id = VectorSplice(v.begin(),v.end(),3,__Cmp<float>()); int id = VectorSplice(v.begin(),v.end(),3,__Cmp<float>());

View File

@@ -63,6 +63,8 @@ private:
}; };
template < typename T > template < typename T >
class DataVector : public AbstractArray { class DataVector : public AbstractArray {
public: public:

View File

@@ -65,6 +65,7 @@ public:
class DataVectorImage : public ImageData { class DataVectorImage : public ImageData {
typedef float ScalarT;
public: public:
inline void * GetDataPointer(const Id_t id) const { inline void * GetDataPointer(const Id_t id) const {
@@ -81,11 +82,13 @@ public:
m_Data->SetSize(size.prod()); m_Data->SetSize(size.prod());
} }
// uLibRefMacro(Data,AbstractArray*)
uLibRefMacro(Data,SmartPointer<AbstractArray>) uLibRefMacro(Data,SmartPointer<AbstractArray>)
uLibRefMacro(Scalars,ProgrammableAccessor<double>) uLibRefMacro(Scalars,ProgrammableAccessor<ScalarT>)
private: private:
// AbstractArray* m_Data;
SmartPointer<AbstractArray> m_Data; SmartPointer<AbstractArray> m_Data;
ProgrammableAccessor<double> m_Scalars; ProgrammableAccessor<ScalarT> m_Scalars;
}; };

View File

@@ -113,14 +113,23 @@ public:
inline void PreRotate(const Matrix3f &m) { this->m_T.prerotate(m); } inline void PreRotate(const Matrix3f &m) { this->m_T.prerotate(m); }
inline void QuaternionRotate(const Vector4f &q) inline void Rotate(const float angle, Vector3f axis)
{ this->m_T.rotate(Eigen::Quaternion<float>(q)); } {
axis.normalize(); // prehaps not necessary ( see eigens )
Eigen::AngleAxisf ax(angle,axis);
this->m_T.rotate(Eigen::Quaternion<float>(ax));
}
inline void EulerYZYRotate(const Vector3f &e) { inline void Rotate(const Vector3f euler_axis) {
float angle = euler_axis.norm();
Rotate(angle,euler_axis);
}
inline void EulerYZYRotate(const Vector3f &euler_angles) {
Matrix3f mat; Matrix3f mat;
mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY()) mat = Eigen::AngleAxisf(euler_angles.x(), Vector3f::UnitY())
* Eigen::AngleAxisf(e.y(), Vector3f::UnitZ()) * Eigen::AngleAxisf(euler_angles.y(), Vector3f::UnitZ())
* Eigen::AngleAxisf(e.z(), Vector3f::UnitY()); * Eigen::AngleAxisf(euler_angles.z(), Vector3f::UnitY());
m_T.rotate(mat); m_T.rotate(mat);
} }
@@ -133,8 +142,12 @@ public:
}; };
typedef Eigen::Quaternion<float> Quaternionf;
typedef Eigen::Quaternion<double> Quaterniond;
}
} // uLib

View File

@@ -9,13 +9,13 @@
//#include "boost/geometry.hpp" #include "boost/geometry.hpp"
//#include "boost/geometry/geometries/adapted/c_array.hpp" #include "boost/geometry/geometries/adapted/c_array.hpp"
//#include "boost/geometry/geometries/adapted/boost_array.hpp" #include "boost/geometry/geometries/adapted/boost_array.hpp"
//#include "boost/geometry/geometries/adapted/std_pair_as_segment.hpp" #include "boost/geometry/geometries/adapted/std_pair_as_segment.hpp"
//#include "boost/geometry/geometries/register/point.hpp" #include "boost/geometry/geometries/register/point.hpp"
//#include "boost/geometry/geometries/register/linestring.hpp" #include "boost/geometry/geometries/register/linestring.hpp"
namespace uLib { namespace uLib {
@@ -42,8 +42,8 @@ inline Vector4f HPoint3f(float x, float y, float z) { return Vector4f(x,y,z,1);
inline Vector4f HPoint3f(const Vector4f &v) { return Vector4f(v(0),v(1),v(2),1); } inline Vector4f HPoint3f(const Vector4f &v) { return Vector4f(v(0),v(1),v(2),1); }
// this is an example of function imported from eigen // // this is an example of function imported from boost //
//using boost::geometry::distance; using boost::geometry::distance;
} // uLib } // uLib
@@ -51,17 +51,17 @@ inline Vector4f HPoint3f(const Vector4f &v) { return Vector4f(v(0),v(1),v(2),1);
// BOOST GEOMETRY REGISTRATION // // BOOST GEOMETRY REGISTRATION //
//BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1)) BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2)) BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2)) BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1)) BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2)) BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2)) BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1)) BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2)) BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2)) BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))

View File

@@ -91,7 +91,7 @@ void VoxRaytracer::RayData::PrintSelf(std::ostream &o)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool VoxRaytracer::GetEntryPoint(const HLine3f line, Vector4f &pt) bool VoxRaytracer::GetEntryPoint(const HLine3f line, Vector4f &pt) const
{ {
Vector4f s = m_Image->GetLocalPoint(line.direction()); Vector4f s = m_Image->GetLocalPoint(line.direction());
pt = m_Image->GetLocalPoint(line.origin()); pt = m_Image->GetLocalPoint(line.origin());
@@ -140,7 +140,7 @@ bool VoxRaytracer::GetEntryPoint(const HLine3f line, Vector4f &pt)
return false; return false;
} }
bool VoxRaytracer::GetExitPoint(const HLine3f line, Vector4f &pt) bool VoxRaytracer::GetExitPoint(const HLine3f line, Vector4f &pt) const
{ {
HLine3f out = line; HLine3f out = line;
out.direction() *= -1; out.direction() *= -1;

View File

@@ -67,9 +67,9 @@ public:
public: public:
VoxRaytracer(ImageData &image) : m_Image(&image) {} VoxRaytracer(ImageData &image) : m_Image(&image) {}
bool GetEntryPoint(const HLine3f line, Vector4f &pt); bool GetEntryPoint(const HLine3f line, Vector4f &pt) const;
bool GetExitPoint(const HLine3f line, Vector4f &pt); bool GetExitPoint(const HLine3f line, Vector4f &pt) const;
RayData TraceBetweenPoints(const Vector4f &in, const Vector4f &out) const; RayData TraceBetweenPoints(const Vector4f &in, const Vector4f &out) const;

View File

@@ -23,8 +23,12 @@
//////////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////////*/
#include "testing-prototype.h" #include "testing-prototype.h"
#include "Math/ImageData.h" #include "Math/ImageData.h"
#include <Core/Mpl.h>
#include <root/TRandom.h>
using namespace uLib; using namespace uLib;
@@ -38,7 +42,7 @@ struct MyVoxel {
struct VoxelMean : public MyVoxel { struct VoxelMean : public MyVoxel {
VoxelMean() {} VoxelMean() {}
void SetValue(const float value) { this->value += value; ++count; } void SetValue(const float _value) { value += _value; ++count; }
float GetValue() const { return value/count; } float GetValue() const { return value/count; }
}; };
} }
@@ -48,31 +52,42 @@ struct VoxelMean : public MyVoxel {
int main() { int main() {
DataVector<MyVoxel> v; // DataVector<MyVoxel> v;
DataVectorImage img; DataVectorImage img;
img.Data() = v; // img.Data() = v;
img.Scalars().SetAccessFunctions(&MyVoxel::value); // img.Scalars().SetAccessFunctions(&MyVoxel::value);
img.SetDims(Vector3i(3,3,3)); // img.SetDims(Vector3i(3,3,3));
for (int x=0; x<img.GetDims().prod(); ++x){ // for (int x=0; x<img.GetDims().prod(); ++x){
img.SetValue(x,x); // img.SetValue(x,x);
std::cout << img.UnMap(x).transpose() << " -> " << img.GetValue(x) << "\n"; // std::cout << img.UnMap(x).transpose() << " -> " << img.GetValue(x) << "\n";
} // }
DataVector<VoxelMean> vm; DataVector<VoxelMean> vm;
Vector<VoxelMean> vm_2;
vm_2.resize(Vector3i(300,300,300).prod());
img.Data() = vm; img.Data() = vm;
img.SetDims(Vector3i(3,3,3)); img.SetDims(Vector3i(300,300,300));
img.Scalars().SetAccessFunctions(&VoxelMean::GetValue,&VoxelMean::SetValue); img.Scalars().SetAccessFunctions(&VoxelMean::GetValue,&VoxelMean::SetValue);
// TRandom random;
for(int i=0; i< 100; ++i)
for (int x=0; x<img.GetDims().prod(); ++x){ for (int x=0; x<img.GetDims().prod(); ++x){
img.SetValue(x,x); // vm.Data()[x].value += 1; vm.Data()[x].count++;
img.SetValue(x,x+2); // vm.Data()[x].SetValue(1);
img.SetValue(x,x-1); vm_2[x].SetValue(1);
std::cout << img.UnMap(x).transpose() << " -> " << img.GetValue(x) << "\n"; // img.SetValue(x,1);
// boost::bind(&VoxelMean::SetValue,&vm.Data()[x], _1)(1);
// std::cout << img.UnMap(x).transpose() << " -> " << img.GetValue(x) << "\n";
} }
// img.ExportToVtk("test.vtk");
} }

View File

@@ -42,7 +42,7 @@ namespace Vtk {
class vtkVoxImage : public Puppet { class vtkVoxImage : public Puppet {
uLibTypeMacro(vtkVoxImage,Puppet) uLibTypeMacro(vtkVoxImage,Puppet)
public: public:
properties() uLib_properties()
{ {
float writer_factor; float writer_factor;
}; };