From 891f46d50226b58dc275f4d961b6e02745f4ac03 Mon Sep 17 00:00:00 2001 From: Andrea Rigoni Date: Sat, 29 Nov 2014 07:25:35 +0000 Subject: [PATCH] [uLib Geometry] - Adds Type combiner. - change names to image map (WARNING some classess may not be already uploaded) --- CMake/uLibCommon.cmake | 14 +- CMake/uLibFindDependencies.cmake | 7 +- src/Core/CMakeLists.txt | 2 + src/Core/ClassFactory.h | 330 ++++++++++++++++++ src/Core/Mpl.h | 10 +- src/Core/MplSequenceCombiner.h | 160 +++++++++ src/Core/ObjectProps.h | 2 +- src/Core/ProgrammableAccessor.h | 19 +- src/Core/StaticInterface.h | 41 ++- src/Core/Vector.h | 2 + src/Core/testing/CMakeLists.txt | 3 + src/Core/testing/ClassFactoryTest.cpp | 323 +++++++++++++++++ src/Core/testing/FunctionTest.cpp | 13 + src/Core/testing/MplSequenceCombinerTest.cpp | 73 ++++ src/Core/testing/ObjectPropableTest.cpp | 6 +- src/Core/testing/ProgrammableAccessorTest.cpp | 64 ++++ src/Core/testing/SerializeTest.cpp | 2 +- src/Core/testing/StaticInterfaceTest.cpp | 42 +-- src/Core/testing/VectorTest.cpp | 9 + src/Math/DataSet.h | 2 + src/Math/ImageData.h | 7 +- src/Math/Transform.h | 27 +- src/Math/VectorSpace.h | 34 +- src/Math/VoxRaytracer.cpp | 4 +- src/Math/VoxRaytracer.h | 4 +- src/Math/testing/ImageDataTest.cpp | 43 ++- src/Vtk/vtkVoxImage.h | 2 +- 27 files changed, 1146 insertions(+), 99 deletions(-) create mode 100644 src/Core/ClassFactory.h create mode 100644 src/Core/MplSequenceCombiner.h create mode 100644 src/Core/testing/ClassFactoryTest.cpp create mode 100644 src/Core/testing/MplSequenceCombinerTest.cpp create mode 100644 src/Core/testing/ProgrammableAccessorTest.cpp diff --git a/CMake/uLibCommon.cmake b/CMake/uLibCommon.cmake index ad120ad..636c15f 100644 --- a/CMake/uLibCommon.cmake +++ b/CMake/uLibCommon.cmake @@ -57,7 +57,19 @@ endif() 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_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}") diff --git a/CMake/uLibFindDependencies.cmake b/CMake/uLibFindDependencies.cmake index 828f3ec..cf651de 100644 --- a/CMake/uLibFindDependencies.cmake +++ b/CMake/uLibFindDependencies.cmake @@ -13,7 +13,12 @@ set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) 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) set(HAVE_BOOST true) endif(Boost_FOUND) diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt index 862ee5b..fcc9757 100644 --- a/src/Core/CMakeLists.txt +++ b/src/Core/CMakeLists.txt @@ -22,6 +22,8 @@ set(HEADERS Vector.h CommaInitializer.h ProgrammableAccessor.h + Named.h + MplSequenceCombiner.h ) SET(SOURCES diff --git a/src/Core/ClassFactory.h b/src/Core/ClassFactory.h new file mode 100644 index 0000000..df71c82 --- /dev/null +++ b/src/Core/ClassFactory.h @@ -0,0 +1,330 @@ +#ifndef U_CORE_CLASSFACTORY_H +#define U_CORE_CLASSFACTORY_H + + +#include +#include + +#include + +#include "Core/Mpl.h" +#include "Core/MplSequenceCombiner.h" +#include "Core/Types.h" + + +namespace uLib { + + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// generator // + +template +static BaseType * newClass() { return new T; } + + + + + + + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Run time named class factory // + +template +class NamedClassFactory { + typedef BaseType*(*_newClassfn)(); + typedef std::map MapType; + +public: + template + void AddType(const char* name) { + m_map.insert(std::make_pair(name, &newClass)); + } + + 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 RegisteredClassFactory { + typedef BaseType*(*_newClassfn)(); + typedef std::vector<_newClassfn> TypeVectorType; + + struct AddTypeOp { + AddTypeOp( RegisteredClassFactory *_p) : m_parent(_p) {} + template void operator ()(U x) { + m_parent->addType(); + } + RegisteredClassFactory *m_parent; + }; + + template < typename T > + void addType() { m_map.push_back(&newClass); } + +public: + + RegisteredClassFactory() { + mpl::for_each(AddTypeOp(this)); + } + + template < typename T > + int Find() { + typedef typename mpl::find::type iter; + return iter::pos::value; + } + + template < typename T > + BaseType *Create() { + int id = Find(); + typename TypeVectorType::iterator itr = m_map.begin() + id; + return (*itr)(); + } + +private: + TypeVectorType m_map; +}; + + + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +template < + class BaseType, + template 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 + >); } + + struct AddTypeSeqOp { + AddTypeSeqOp( RegisteredClassFactory1 *_p) : m_parent(_p) {} + template < typename Seq > + void operator()(Seq) { + m_parent->addType< + typename mpl::at >::type + > (); + } + RegisteredClassFactory1 *m_parent; + }; + + typedef mpl::combine_view< mpl::vector > AlgView; +public: + + RegisteredClassFactory1() { + mpl::for_each< AlgView >(AddTypeSeqOp(this)); + } + + template < typename T > + static inline int FindS1() { + typedef typename mpl::find::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::type::value; +}; + + + + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + +template < + class BaseType, + template 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 + >); } + + struct AddTypeSeqOp { + AddTypeSeqOp( RegisteredClassFactory2 *_p) : m_parent(_p) {} + template < typename Seq > + void operator()(Seq) { + m_parent->addType< + typename mpl::at >::type, + typename mpl::at >::type + > (); + } + RegisteredClassFactory2 *m_parent; + }; + + typedef mpl::combine_view< mpl::vector > AlgView; +public: + + RegisteredClassFactory2() { + mpl::for_each< AlgView >(AddTypeSeqOp(this)); + } + + template < typename T > + static inline int FindS1() { + typedef typename mpl::find::type iter; + return iter::pos::value; + } + + template < typename T > + static inline int FindS2() { + typedef typename mpl::find::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::type::value; + static const int m_size2 = mpl::size::type::value; +}; + + + + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + + +template < + class BaseType, + template 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 + >); } + + struct AddTypeSeqOp { + AddTypeSeqOp( RegisteredClassFactory3 *_p) : m_parent(_p) {} + template < typename Seq > + void operator()(Seq) { + m_parent->addType< + typename mpl::at >::type, + typename mpl::at >::type, + typename mpl::at >::type + > (); + } + RegisteredClassFactory3 *m_parent; + }; + + typedef mpl::combine_view< mpl::vector > AlgView; +public: + + RegisteredClassFactory3() { + mpl::for_each< AlgView >(AddTypeSeqOp(this)); + } + + template < typename T > + static inline int FindS1() { + typedef typename mpl::find::type iter; + return iter::pos::value; + } + + template < typename T > + static inline int FindS2() { + typedef typename mpl::find::type iter; + return iter::pos::value; + } + + template < typename T > + static inline int FindS3() { + typedef typename mpl::find::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::type::value; + static const int m_size2 = mpl::size::type::value; + static const int m_size3 = mpl::size::type::value; +}; + + + + + + + + + + + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + +} // uLib + + + + +#endif // CLASSFACTORY_H diff --git a/src/Core/Mpl.h b/src/Core/Mpl.h index d7f0130..0fbf83d 100644 --- a/src/Core/Mpl.h +++ b/src/Core/Mpl.h @@ -40,13 +40,18 @@ #include #include +#include + #include #include #include #include +#include #include #include +#include + #include #include @@ -65,6 +70,7 @@ namespace mpl { using namespace boost::mpl; using namespace boost::mpl::placeholders; + //using boost::enable_if; namespace detail { @@ -132,8 +138,10 @@ struct nvp { }; -} // mpl + + +} // mpl } // uLib #endif // MPL_H diff --git a/src/Core/MplSequenceCombiner.h b/src/Core/MplSequenceCombiner.h new file mode 100644 index 0000000..490e34f --- /dev/null +++ b/src/Core/MplSequenceCombiner.h @@ -0,0 +1,160 @@ +#ifndef U_CORE_MPLSEQUENCECOMBINER_H +#define U_CORE_MPLSEQUENCECOMBINER_H + +#include +#include +#include +#include +#include +#include + +#include + + +namespace uLib { +namespace mpl { + + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + +template +class SequenceCombiner { + template < class _Seq = mpl::vector > > + 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 >::type in_seq; + typedef typename mpl::at >::type in_itr; + + typedef typename mpl::push_back::type new_seq; + public: + typedef typename set_state >::type type; + }; + + template < typename Out, typename In > + class apply > { + typedef typename Out::sequence seq; + typedef typename Out::state state; + typedef typename mpl::at >::type in_seq; + typedef typename mpl::at >::type in_itr; + + typedef typename mpl::begin::type Itr_begin; + typedef typename mpl::next::type Itr_next; + typedef typename mpl::end::type Itr_end; + + typedef typename mpl::if_< boost::is_same, + typename mpl::push_back::type, + typename mpl::push_back::type + >::type + new_seq; + + typedef typename mpl::if_< boost::is_same, + mpl::int_<1>, + mpl::int_<0> + >::type + new_state; + public: + typedef typename set_state::type type; + }; + }; + + typedef typename mpl::fold< + typename mpl::zip_view< mpl::vector >::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 >::type Itrs_begin; + typedef typename mpl::transform >::type Itrs_end; +public: + typedef combine_iterator begin; + typedef combine_iterator end; + + typedef combine_view type; +}; + + + +} // mpl +} // uLib + + + + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + +namespace boost { +namespace mpl { + +template +struct next< uLib::mpl::combine_iterator > { + typedef typename uLib::mpl::SequenceCombiner::next next_Pos; + typedef uLib::mpl::combine_iterator< Seq, next_Pos > type; +}; + +} // mpl +} // boost + + + + + +#endif // MPLSEQUENCECOMBINER_H diff --git a/src/Core/ObjectProps.h b/src/Core/ObjectProps.h index b789d84..8ee1981 100644 --- a/src/Core/ObjectProps.h +++ b/src/Core/ObjectProps.h @@ -52,7 +52,7 @@ typedef uLib::mpl::bool_::type propable_trait; \ public: struct DLL_PUBLIC ObjectProps -#define properties() ULIB_props() +#define uLib_properties() ULIB_props() #define default(vlaue) diff --git a/src/Core/ProgrammableAccessor.h b/src/Core/ProgrammableAccessor.h index aebeba1..946121d 100644 --- a/src/Core/ProgrammableAccessor.h +++ b/src/Core/ProgrammableAccessor.h @@ -32,6 +32,7 @@ public: any_c(const any & other) : any(other) {} + // FIX !! // // this is needed to tait static_cast using any_cast function // template operator _TT () const { return boost::any_cast<_TT>(*this); } @@ -77,10 +78,10 @@ private: template class functor_by_mfptr_cc : public ProgrammableAccessor_Base { - typedef R(T::*GetterType)() const; - typedef void(T::*SetterType)(const R); + typedef R(T::*GeT)() const; + typedef void(T::*SeT)(const R); public: - explicit functor_by_mfptr_cc( GetterType _get, SetterType _set ) : + explicit functor_by_mfptr_cc( GeT _get, SeT _set ) : GetPtr(_get), SetPtr(_set) {} D Get(void * ptr) const { return static_cast((reinterpret_cast(ptr)->*GetPtr)()); } @@ -88,8 +89,8 @@ public: { if (SetPtr) (reinterpret_cast(ptr)->*SetPtr)(static_cast(data)); } private: - GetterType GetPtr; - SetterType SetPtr; + GeT GetPtr; + SeT SetPtr; }; template @@ -114,7 +115,12 @@ private: template class functor_by_member : public ProgrammableAccessor_Base { -public: + template < R T::*_pf > + class StaticWrapper { + static D Get(void *ptr) { return static_cast(reinterpret_cast(ptr)->*_pf); } + static void Set(void *ptr, const D data) { (reinterpret_cast(ptr)->*_pf) = static_cast(data); } + }; +public: explicit functor_by_member(R T::*__pf) : MDPtr(__pf) {} D Get(void * ptr) const { return static_cast(reinterpret_cast(ptr)->*MDPtr); } @@ -193,6 +199,7 @@ public: } // ------ // + const Wrapper operator() (void *ob) const { return Wrapper(this,ob); } inline D Get(void *ob) const { return m_base->Get(ob); } diff --git a/src/Core/StaticInterface.h b/src/Core/StaticInterface.h index cb26039..413bfbc 100644 --- a/src/Core/StaticInterface.h +++ b/src/Core/StaticInterface.h @@ -30,6 +30,23 @@ namespace uLib { + +// SFINAE // +/* +template +struct has_type_info { + typedef char yes[1]; + typedef char no[2]; + template static yes& test(typename U::type_info::BaseList*); + template static no& test(...); + // struct apply { + static const bool value = sizeof(test(0)) == sizeof(yes); + typedef boost::mpl::bool_ type; + // }; +}; +*/ + + #define MAKE_TRAITS ; // TODO #define uLibCheckFunction(class_name,name,out, ... ) \ @@ -56,22 +73,24 @@ namespace Interface { //}; template -static inline void IsA() { +static inline void IsA(T *t = 0) { (void) ((void (SI::*)()) &SI::template check_structural); } -// not working -//template -//struct StaticIsA { -// void test() { -// static const void (SI::*x)() = SI::template check_structural; -// } -//}; - +template +static inline void IsA(T &t) { + (void) ((void (SI::*)()) &SI::template check_structural); } +template +struct StaticIsA { + StaticIsA() { + void (SI::*x)() = &SI::template check_structural; + (void) x; + } +}; - -} +} // Interface +} // uLib #endif // STATICINTERFACE_H diff --git a/src/Core/Vector.h b/src/Core/Vector.h index ace265b..9a595f1 100644 --- a/src/Core/Vector.h +++ b/src/Core/Vector.h @@ -53,8 +53,10 @@ public: Vector(unsigned int size) : BaseClass(size) {} Vector(unsigned int size, T &value) : BaseClass(size,value) {} + Vector(const Vector ©) : BaseClass(copy) {} Vector() : BaseClass(0) {} + inline VectorCommaInit operator <<(T scalar) { return VectorCommaInit(this, scalar); } diff --git a/src/Core/testing/CMakeLists.txt b/src/Core/testing/CMakeLists.txt index ca9d68c..236c4f1 100644 --- a/src/Core/testing/CMakeLists.txt +++ b/src/Core/testing/CMakeLists.txt @@ -21,6 +21,9 @@ set( TESTS UuidTest TypeIntrospectionTraversal OptionsTest + ProgrammableAccessorTest + ClassFactoryTest + MplSequenceCombinerTest ) set(LIBRARIES diff --git a/src/Core/testing/ClassFactoryTest.cpp b/src/Core/testing/ClassFactoryTest.cpp new file mode 100644 index 0000000..48d9b9c --- /dev/null +++ b/src/Core/testing/ClassFactoryTest.cpp @@ -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 +#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 ©) {} + + 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(); + 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(); + Base * newbase = m_factory.Create(m_id1,id); + *newbase = *m_base; + delete m_base; + m_base = newbase; + } + + Base * operator -> () { return m_base; } + +private: + RegisteredClassFactory2 m_factory; + int m_id1, m_id2; + Base * m_base; +}; + + + +//int main() { +// ProgrammableAlgHolder ah; +// ah.SetA1(); +// 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 &ref ) { + return ref.size(); + } +}; + +class VoxCountOver { +public: + VoxCountOver() : m_threshold(0) {} + + template < class T > + int Count(const Vector &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; iSize(); ++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 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(); + 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(); + 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, mpl::vector > 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(); + + for(int i=0; i<10; ++i) + std::cout << factory->Get(i) << " "; + std::cout << "\n"; + + std::cout << "vox count = " << factory->Count() << "\n"; + + + + +} + + + + + + + + + + + + + + diff --git a/src/Core/testing/FunctionTest.cpp b/src/Core/testing/FunctionTest.cpp index d16f96d..2bbf6d7 100644 --- a/src/Core/testing/FunctionTest.cpp +++ b/src/Core/testing/FunctionTest.cpp @@ -84,6 +84,19 @@ public: } // uLib +namespace uLib { + +namespace detail { +} // detail + + + +} // uLib + + + + + using namespace uLib; int main() { diff --git a/src/Core/testing/MplSequenceCombinerTest.cpp b/src/Core/testing/MplSequenceCombinerTest.cpp new file mode 100644 index 0000000..af0c556 --- /dev/null +++ b/src/Core/testing/MplSequenceCombinerTest.cpp @@ -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 +#include +#include +#include + +#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 + void operator()(T) const + { std::cout << typeid(T).name() << " "; } +}; +struct PrintSeq { + template < typename T > + void operator()(T) { + mpl::for_each( PrintTypeId() ); + std::cout << "\n"; + } +}; +} + +int main() { + BEGIN_TESTING( Mpl Sequence Combiner Test); + + typedef mpl::vector seq1; + typedef mpl::vector seq2; + typedef mpl::vector seq3; + + typedef mpl::combine_view< mpl::vector > cv; + mpl::for_each< cv >( PrintSeq() ); + + END_TESTING; +} + diff --git a/src/Core/testing/ObjectPropableTest.cpp b/src/Core/testing/ObjectPropableTest.cpp index 9e319db..7efaa4d 100644 --- a/src/Core/testing/ObjectPropableTest.cpp +++ b/src/Core/testing/ObjectPropableTest.cpp @@ -60,7 +60,7 @@ struct A : virtual Object { uLibTypeMacro(A, Object) - properties() { + uLib_properties() { int p_a; Vector3f p_3f; }; @@ -84,7 +84,7 @@ ULIB_SERIALIZE_OBJECT_PROPS(A) { ar & AR(p_a) & AR(p_3f); } struct B : A { uLibTypeMacro(B,A) - properties() { + uLib_properties() { std::string p; }; @@ -118,7 +118,7 @@ ULIB_SERIALIZE(C) { ar & AR(m_c) & AR(m_str); } struct D : virtual Object, B { uLibTypeMacro(D,Object,B) - properties() { + uLib_properties() { C p_c; }; }; diff --git a/src/Core/testing/ProgrammableAccessorTest.cpp b/src/Core/testing/ProgrammableAccessorTest.cpp new file mode 100644 index 0000000..fb160b9 --- /dev/null +++ b/src/Core/testing/ProgrammableAccessorTest.cpp @@ -0,0 +1,64 @@ + +#include +#include +#include + +#include "testing-prototype.h" +#include "Core/Types.h" + +#include +#include + +#include + +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 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 f1(&TestVoxel::m_data); + ProgrammableAccessor f2(&TestVoxel::ConstGet); + + + + int i=0; + foreach (TestVoxel &el,v) { + f1.Set(&el,i++); + std::cout << " -> " << f2.Get(&el) << " - " << f1(&el).Get() << "\n"; + + } + + + END_TESTING; +} + diff --git a/src/Core/testing/SerializeTest.cpp b/src/Core/testing/SerializeTest.cpp index f15660a..6a09513 100644 --- a/src/Core/testing/SerializeTest.cpp +++ b/src/Core/testing/SerializeTest.cpp @@ -115,7 +115,7 @@ class A : public virtual Object { public: A() : m_a(5552368) {} - properties() { + uLib_properties() { std::string p_a; }; diff --git a/src/Core/testing/StaticInterfaceTest.cpp b/src/Core/testing/StaticInterfaceTest.cpp index a618c44..a19034c 100644 --- a/src/Core/testing/StaticInterfaceTest.cpp +++ b/src/Core/testing/StaticInterfaceTest.cpp @@ -45,33 +45,13 @@ struct Test { uLibCheckMember(Self,testmemb,int); } }; -} +} // Interface struct Test { bool test(int i, float f){} int testmemb; }; - - -//// INTERFAC TO SIMPLE CLASS /////////// - -namespace Interface { -struct Simple { - MAKE_TRAITS - template void check_structural() { - uLibCheckMember(Self,memb1,int); - uLibCheckMember(Self,memb2,float); - } -}; -} - -struct Simple { - int memb1; - float memb2; -}; - - ///////////////////////// template @@ -85,27 +65,31 @@ public: } }; + template -class UseSimple { +class UseTest2 { + Interface::StaticIsA x; public: - UseSimple() { - Interface::IsA(); + UseTest2() { + T t; + int i; float f; + t.test(i,f); } }; +} // uLib -} + +using namespace uLib; int main() { BEGIN_TESTING(Static Interface); + UseTest u; - - uLib::UseTest u; - - uLib::UseSimple s; + UseTest2 u2; END_TESTING; } diff --git a/src/Core/testing/VectorTest.cpp b/src/Core/testing/VectorTest.cpp index 6fee192..8a830bd 100644 --- a/src/Core/testing/VectorTest.cpp +++ b/src/Core/testing/VectorTest.cpp @@ -67,7 +67,16 @@ int main() uLib::Vector 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; + uLib::Vector 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()); diff --git a/src/Math/DataSet.h b/src/Math/DataSet.h index 1c9707c..5a9914b 100644 --- a/src/Math/DataSet.h +++ b/src/Math/DataSet.h @@ -63,6 +63,8 @@ private: }; + + template < typename T > class DataVector : public AbstractArray { public: diff --git a/src/Math/ImageData.h b/src/Math/ImageData.h index e7cecdc..32330ee 100644 --- a/src/Math/ImageData.h +++ b/src/Math/ImageData.h @@ -65,6 +65,7 @@ public: class DataVectorImage : public ImageData { + typedef float ScalarT; public: inline void * GetDataPointer(const Id_t id) const { @@ -81,11 +82,13 @@ public: m_Data->SetSize(size.prod()); } +// uLibRefMacro(Data,AbstractArray*) uLibRefMacro(Data,SmartPointer) - uLibRefMacro(Scalars,ProgrammableAccessor) + uLibRefMacro(Scalars,ProgrammableAccessor) private: +// AbstractArray* m_Data; SmartPointer m_Data; - ProgrammableAccessor m_Scalars; + ProgrammableAccessor m_Scalars; }; diff --git a/src/Math/Transform.h b/src/Math/Transform.h index 634f3a2..1d841c2 100644 --- a/src/Math/Transform.h +++ b/src/Math/Transform.h @@ -113,14 +113,23 @@ public: inline void PreRotate(const Matrix3f &m) { this->m_T.prerotate(m); } - inline void QuaternionRotate(const Vector4f &q) - { this->m_T.rotate(Eigen::Quaternion(q)); } + inline void Rotate(const float angle, Vector3f axis) + { + axis.normalize(); // prehaps not necessary ( see eigens ) + Eigen::AngleAxisf ax(angle,axis); + this->m_T.rotate(Eigen::Quaternion(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; - mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY()) - * Eigen::AngleAxisf(e.y(), Vector3f::UnitZ()) - * Eigen::AngleAxisf(e.z(), Vector3f::UnitY()); + mat = Eigen::AngleAxisf(euler_angles.x(), Vector3f::UnitY()) + * Eigen::AngleAxisf(euler_angles.y(), Vector3f::UnitZ()) + * Eigen::AngleAxisf(euler_angles.z(), Vector3f::UnitY()); m_T.rotate(mat); } @@ -133,8 +142,12 @@ public: }; +typedef Eigen::Quaternion Quaternionf; +typedef Eigen::Quaternion Quaterniond; -} + + +} // uLib diff --git a/src/Math/VectorSpace.h b/src/Math/VectorSpace.h index f45146f..61600cd 100644 --- a/src/Math/VectorSpace.h +++ b/src/Math/VectorSpace.h @@ -9,13 +9,13 @@ -//#include "boost/geometry.hpp" -//#include "boost/geometry/geometries/adapted/c_array.hpp" -//#include "boost/geometry/geometries/adapted/boost_array.hpp" -//#include "boost/geometry/geometries/adapted/std_pair_as_segment.hpp" +#include "boost/geometry.hpp" +#include "boost/geometry/geometries/adapted/c_array.hpp" +#include "boost/geometry/geometries/adapted/boost_array.hpp" +#include "boost/geometry/geometries/adapted/std_pair_as_segment.hpp" -//#include "boost/geometry/geometries/register/point.hpp" -//#include "boost/geometry/geometries/register/linestring.hpp" +#include "boost/geometry/geometries/register/point.hpp" +#include "boost/geometry/geometries/register/linestring.hpp" 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); } -// this is an example of function imported from eigen // -//using boost::geometry::distance; +// this is an example of function imported from boost // +using boost::geometry::distance; } // 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_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::Vector4i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2)) +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::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_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_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::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_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_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::Vector4d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2)) diff --git a/src/Math/VoxRaytracer.cpp b/src/Math/VoxRaytracer.cpp index 01094b3..cea0f10 100644 --- a/src/Math/VoxRaytracer.cpp +++ b/src/Math/VoxRaytracer.cpp @@ -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()); pt = m_Image->GetLocalPoint(line.origin()); @@ -140,7 +140,7 @@ bool VoxRaytracer::GetEntryPoint(const HLine3f line, Vector4f &pt) return false; } -bool VoxRaytracer::GetExitPoint(const HLine3f line, Vector4f &pt) +bool VoxRaytracer::GetExitPoint(const HLine3f line, Vector4f &pt) const { HLine3f out = line; out.direction() *= -1; diff --git a/src/Math/VoxRaytracer.h b/src/Math/VoxRaytracer.h index 02b69a1..fae697c 100644 --- a/src/Math/VoxRaytracer.h +++ b/src/Math/VoxRaytracer.h @@ -67,9 +67,9 @@ public: public: 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; diff --git a/src/Math/testing/ImageDataTest.cpp b/src/Math/testing/ImageDataTest.cpp index 3fb8010..672d095 100644 --- a/src/Math/testing/ImageDataTest.cpp +++ b/src/Math/testing/ImageDataTest.cpp @@ -23,8 +23,12 @@ //////////////////////////////////////////////////////////////////////////////*/ + #include "testing-prototype.h" #include "Math/ImageData.h" +#include + +#include using namespace uLib; @@ -38,7 +42,7 @@ struct MyVoxel { struct VoxelMean : public MyVoxel { VoxelMean() {} - void SetValue(const float value) { this->value += value; ++count; } + void SetValue(const float _value) { value += _value; ++count; } float GetValue() const { return value/count; } }; } @@ -48,31 +52,42 @@ struct VoxelMean : public MyVoxel { int main() { - DataVector v; +// DataVector v; DataVectorImage img; - img.Data() = v; - img.Scalars().SetAccessFunctions(&MyVoxel::value); - img.SetDims(Vector3i(3,3,3)); +// img.Data() = v; +// img.Scalars().SetAccessFunctions(&MyVoxel::value); +// img.SetDims(Vector3i(3,3,3)); - for (int x=0; x " << img.GetValue(x) << "\n"; - } +// for (int x=0; x " << img.GetValue(x) << "\n"; +// } DataVector vm; + Vector vm_2; + vm_2.resize(Vector3i(300,300,300).prod()); + img.Data() = vm; - img.SetDims(Vector3i(3,3,3)); + img.SetDims(Vector3i(300,300,300)); img.Scalars().SetAccessFunctions(&VoxelMean::GetValue,&VoxelMean::SetValue); +// TRandom random; + + + for(int i=0; i< 100; ++i) for (int x=0; x " << img.GetValue(x) << "\n"; +// vm.Data()[x].value += 1; vm.Data()[x].count++; +// vm.Data()[x].SetValue(1); + vm_2[x].SetValue(1); +// 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"); } + diff --git a/src/Vtk/vtkVoxImage.h b/src/Vtk/vtkVoxImage.h index a66ec60..6235b1e 100644 --- a/src/Vtk/vtkVoxImage.h +++ b/src/Vtk/vtkVoxImage.h @@ -42,7 +42,7 @@ namespace Vtk { class vtkVoxImage : public Puppet { uLibTypeMacro(vtkVoxImage,Puppet) public: - properties() + uLib_properties() { float writer_factor; };