From 4b4e0b29595a3ce16bbef803b50b2a06efdc29e7 Mon Sep 17 00:00:00 2001 From: Andrea Rigoni Date: Thu, 6 Nov 2014 15:05:54 +0000 Subject: [PATCH] [uLib Geometry] adds ImageSpace ImageMap and ImageData stats ProgrammableAccess to data --- src/Core/Array.h | 22 --- src/Core/Named.h | 62 +++++++ src/Core/ProgrammableAccessor.h | 178 ++++++++++++++----- src/Core/testing/FunctionTest.cpp | 71 +++++++- src/Math/CMakeLists.txt | 2 + src/Math/DataSet.h | 52 ++++-- src/Math/Dense.h | 57 ++++++ src/Math/ImageData.cpp | 181 +++++++++++++++++++ src/Math/ImageData.h | 48 ++++- src/Math/ImageMap.h | 14 ++ src/Math/ImageSpace.cpp | 27 +-- src/Math/ImageSpace.h | 9 +- src/Math/Line.h | 4 + src/Math/StructuredData.cpp | 84 ++++----- src/Math/StructuredData.h | 86 ++++----- src/Math/VoxImage.cpp | 275 ++++++++++++++--------------- src/Math/VoxImage.h | 52 +++--- src/Math/VoxImageFilter.h | 4 +- src/Math/VoxImageFilter.hpp | 10 +- src/Math/testing/CMakeLists.txt | 1 + src/Math/testing/ImageDataTest.cpp | 78 ++++++++ src/Vtk/vtkVoxImage.h | 2 +- src/ltk/ltktypes.h | 21 +++ 23 files changed, 962 insertions(+), 378 deletions(-) create mode 100644 src/Core/Named.h create mode 100644 src/Math/testing/ImageDataTest.cpp diff --git a/src/Core/Array.h b/src/Core/Array.h index 0e42f07..fde429f 100644 --- a/src/Core/Array.h +++ b/src/Core/Array.h @@ -43,28 +43,6 @@ namespace uLib { -template< typename T, Size_t size > -class Array : public std::array { - -}; - - -template< typename T, Size_t size > -class SmartArray : public SmartPointer< Array > { - typedef SmartPointer< Array > ptr; -public: - SmartArray() : ptr(new Array()) { } - SmartArray( const SmartArray ©) : ptr(copy) { } - virtual ~SmartArray() {} - - T& operator[](unsigned int p) { - return ptr::get()->at(p); - } - - - -}; - } diff --git a/src/Core/Named.h b/src/Core/Named.h new file mode 100644 index 0000000..9b8c591 --- /dev/null +++ b/src/Core/Named.h @@ -0,0 +1,62 @@ +/*////////////////////////////////////////////////////////////////////////////// +// 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. + +//////////////////////////////////////////////////////////////////////////////*/ + +#ifndef U_CORE_NAMED_H +#define U_CORE_NAMED_H + +#include +#include + +namespace uLib { + +class Named { +public: + Named() {} + Named(const String &string) : m_name(string) {} + Named(const char *name) : m_name(name) {} + + // default generic behaviour // + template < typename T > + Named(T t) : m_name(typeid(t).name()) {} + + const String GetName() const { return m_name; } + + void SetName(const String name) { m_name = name; } + + template < typename T > + void SetName(T t) { m_name = typeid(t).name(); } + +private: + String m_name; +}; + + +} // uLib + + + + + +#endif // NAMED_H diff --git a/src/Core/ProgrammableAccessor.h b/src/Core/ProgrammableAccessor.h index 62f1713..aebeba1 100644 --- a/src/Core/ProgrammableAccessor.h +++ b/src/Core/ProgrammableAccessor.h @@ -4,6 +4,11 @@ #include #include #include +#include + +#include + +#include namespace uLib { @@ -11,11 +16,36 @@ namespace uLib { namespace detail { -template -class ProgrammableAccessor_Base { + + + + +class any_c : public boost::any { + typedef boost::any any; + public: - virtual R Get(void *) const = 0; - virtual void Set(void *,const R) const = 0; + + any_c() : any() {} + + template + any_c(const ValueType & value) : any(value) {} + + any_c(const any & other) : any(other) {} + + // this is needed to tait static_cast using any_cast function // + template + operator _TT () const { return boost::any_cast<_TT>(*this); } + +}; + + +template +class ProgrammableAccessor_Base { +protected: + +public: + virtual D Get(void *) const = 0; + virtual void Set(void *,const D) const = 0; template T Get(void *ob) const { @@ -24,56 +54,56 @@ public: template void Set(void *ob, const T data) const { - this->Set(ob,static_cast(data)); + this->Set(ob,static_cast(data)); } virtual ~ProgrammableAccessor_Base() {} }; -template -class functor_by_ref : public ProgrammableAccessor_Base +template +class functor_by_ref : public ProgrammableAccessor_Base { public: explicit functor_by_ref(R&(T::*__pf)()) : MFRef(__pf) {} - R Get(void * ptr) const - { return static_cast((reinterpret_cast(ptr)->*MFRef)()); } - void Set(void * ptr, const R data) const - { ((reinterpret_cast(ptr)->*MFRef)()) = data; } + D Get(void * ptr) const + { return static_cast((reinterpret_cast(ptr)->*MFRef)()); } + void Set(void * ptr, const D data) const + { ((reinterpret_cast(ptr)->*MFRef)()) = static_cast(data); } private: R&(T::*MFRef)(); }; -template -class functor_by_mfptr_cc : public ProgrammableAccessor_Base +template +class functor_by_mfptr_cc : public ProgrammableAccessor_Base { typedef R(T::*GetterType)() const; typedef void(T::*SetterType)(const R); public: explicit functor_by_mfptr_cc( GetterType _get, SetterType _set ) : GetPtr(_get), SetPtr(_set) {} - R Get(void * ptr) const - { return static_cast((reinterpret_cast(ptr)->*GetPtr)()); } - void Set(void * ptr, const R data) const - { if (SetPtr) (reinterpret_cast(ptr)->*SetPtr)(data); } + D Get(void * ptr) const + { return static_cast((reinterpret_cast(ptr)->*GetPtr)()); } + void Set(void * ptr, const D data) const + { if (SetPtr) (reinterpret_cast(ptr)->*SetPtr)(static_cast(data)); } private: GetterType GetPtr; SetterType SetPtr; }; -template -class functor_by_mfptr_vv : public ProgrammableAccessor_Base +template +class functor_by_mfptr_vv : public ProgrammableAccessor_Base { typedef R(T::*GetterType)(); typedef void(T::*SetterType)(R); public: explicit functor_by_mfptr_vv( GetterType _get, SetterType _set ) : GetPtr(_get), SetPtr(_set) {} - R Get(void * ptr) const - { return static_cast((reinterpret_cast(ptr)->*GetPtr)()); } - void Set(void * ptr, const R data) const - { if (SetPtr) (reinterpret_cast(ptr)->*SetPtr)(data); } + D Get(void * ptr) const + { return static_cast((reinterpret_cast(ptr)->*GetPtr)()); } + void Set(void * ptr, const D data) const + { if (SetPtr) (reinterpret_cast(ptr)->*SetPtr)(static_cast(data)); } private: GetterType GetPtr; @@ -81,15 +111,15 @@ private: }; -template -class functor_by_member : public ProgrammableAccessor_Base +template +class functor_by_member : public ProgrammableAccessor_Base { public: explicit functor_by_member(R T::*__pf) : MDPtr(__pf) {} - R Get(void * ptr) const - { return static_cast(reinterpret_cast(ptr)->*MDPtr); } - void Set(void * ptr, const R data) const - { (reinterpret_cast(ptr)->*MDPtr) = data; } + D Get(void * ptr) const + { return static_cast(reinterpret_cast(ptr)->*MDPtr); } + void Set(void * ptr, const D data) const + { (reinterpret_cast(ptr)->*MDPtr) = static_cast(data); } private: R T::*MDPtr; }; @@ -103,36 +133,86 @@ private: //////////////////////////////////////////////////////////////////////////////// -template -class ProgrammableAccessor { + + + +template +class ProgrammableAccessor : public Named { public: - template - ProgrammableAccessor(R(T::*_pg)()const, void(T::*_ps)(const R) = NULL) : - m_base(new detail::functor_by_mfptr_cc(_pg,_ps)) {} + + struct Wrapper { + Wrapper(const ProgrammableAccessor *ac, void *ob) : + m_access(ac), m_object(ob) + { assert(ob != NULL); } + + template + inline T Get() const { return m_access->Get(m_object); } + + template + inline void Set(const T data) const { return m_access->Set(m_object,data); } + + void *m_object; + const ProgrammableAccessor *m_access; + }; + + ProgrammableAccessor() : Named("no access defined") {} + + template < typename F > + ProgrammableAccessor(F f) : Named(f) + { SetAccessFunctions(f); } + + template < typename F1, typename F2 > + ProgrammableAccessor(F1 f1, F2 f2) : Named(f1) // << FIX + { SetAccessFunctions(f1,f2); } + + + // ----- move to factory // + template + inline void SetAccessFunctions(R(T::*_pg)()const, void(T::*_ps)(const R) = NULL) + { + m_base = SmartPointer< detail::ProgrammableAccessor_Base >(new detail::functor_by_mfptr_cc(_pg,_ps)); + this->SetName(_pg); // << FIX + } + + template + inline void SetAccessFunctions(R(T::*_pg)(), void(T::*_ps)(R) = NULL) { + m_base = SmartPointer< detail::ProgrammableAccessor_Base >(new detail::functor_by_mfptr_vv(_pg,_ps)); + this->SetName(_pg); // << FIX + } + + template + inline void SetAccessFunctions(R&(T::*_pf)()) { + m_base = SmartPointer< detail::ProgrammableAccessor_Base >(new detail::functor_by_ref(_pf)); + this->SetName(_pf); + } + + template + inline void SetAccessFunctions(R T::*_pf) { + m_base = SmartPointer< detail::ProgrammableAccessor_Base >(new detail::functor_by_member(_pf)); + this->SetName(_pf); + } + // ------ // + + const Wrapper operator() (void *ob) const { return Wrapper(this,ob); } + + inline D Get(void *ob) const { return m_base->Get(ob); } + + inline void Set(void *ob, const D data) const { m_base->Set(ob,data); } template - ProgrammableAccessor(R(T::*_pg)(), void(T::*_ps)(R) = NULL) : - m_base(new detail::functor_by_mfptr_vv(_pg,_ps)) {} + inline T Get(void *ob) const { return m_base->Get(ob); } template - ProgrammableAccessor(R&(T::*_pf)()) : - m_base(new detail::functor_by_ref(_pf)) {} - - template - ProgrammableAccessor(R T::*_pf) : - m_base(new detail::functor_by_member(_pf)) {} - - template - inline OtherR Get(void *ob) const { return m_base->Get(ob); } - - template - inline void Set(void *ob, const OtherR data) const { return m_base->Set(ob,data); } + inline void Set(void *ob, const T data) const { return m_base->Set(ob,data); } private: - SmartPointer< detail::ProgrammableAccessor_Base > m_base; + SmartPointer< detail::ProgrammableAccessor_Base > m_base; }; +typedef ProgrammableAccessor ProgrammableAccessorAny; + + } // uLib diff --git a/src/Core/testing/FunctionTest.cpp b/src/Core/testing/FunctionTest.cpp index 9a3b3ca..d16f96d 100644 --- a/src/Core/testing/FunctionTest.cpp +++ b/src/Core/testing/FunctionTest.cpp @@ -9,6 +9,8 @@ #include #include +#include + namespace uLib { class TestVoxel { @@ -22,6 +24,64 @@ public: +//template +//class PointerIterator : +// public std::iterator +//{ +//protected: +// _T* m_pData; +//public: +// typedef std::random_access_iterator_tag iterator_category; +// typedef typename std::iterator::value_type value_type; +// typedef typename std::iterator::difference_type difference_type; +// typedef typename std::iterator::reference reference; +// typedef typename std::iterator::pointer pointer; + +// PointerIterator() : m_pData(NULL) {} + +// template +// PointerIterator(const PointerIterator& r) : m_pData(&(*r)) {} + +// PointerIterator(pointer pData) : m_pData(pData) {} + +// template +// PointerIterator& operator=(const PointerIterator& r) +// { m_pData = &(*r); return *this; } + +// PointerIterator& operator++() // PREFIX +// { ++m_pData; return *this; } +// PointerIterator& operator--() // PREFIX +// { --m_pData; return *this; } +// PointerIterator operator++(int) // POSTFIX +// { return PointerIterator(m_pData++); } +// PointerIterator operator--(int) // POSTFIX +// { return PointerIterator(m_pData--); } +// PointerIterator operator+(const difference_type& n) const +// { return PointerIterator(m_pData + n); } +// PointerIterator& operator+=(const difference_type& n) +// { m_pData += n; return *this; } +// PointerIterator operator-(const difference_type& n) const +// { return PointerIterator(pointer(m_pData - n)); } +// PointerIterator& operator-=(const difference_type& n) { m_pData -= n; return *this; } +// reference operator*() const { return *m_pData; } +// pointer operator->() const { return m_pData; } +// reference operator[](const difference_type& n) const { return m_pData[n]; } +//}; + + + +//class DataSetView { + +// template +// T Get(Id_t id) { return m_view.Get(this->GetPointData(id)); } + + + +//private: +// ProgrammableAccessorAny m_view; +//}; + + } // uLib using namespace uLib; @@ -42,13 +102,18 @@ int main() { v[5].m_data = 5552368.0; - ProgrammableAccessor f1(&TestVoxel::m_data); - ProgrammableAccessor f2(&TestVoxel::GetRef); + ProgrammableAccessor f1(&TestVoxel::m_data); + ProgrammableAccessor f2(&TestVoxel::ConstGet); + ProgrammableAccessorAny f3(&TestVoxel::m_data); + ProgrammableAccessor f4(&TestVoxel::m_data); + + int i=0; foreach (TestVoxel &el,v) { f1.Set(&el,i++); - std::cout << " -> " << f2.Get(&el) << "\n"; + std::cout << " -> " << f2.Get(&el) << " - " << f3.Get(&el) << f4.Get(&el) << f1(&el).Get() << "\n"; + } diff --git a/src/Math/CMakeLists.txt b/src/Math/CMakeLists.txt index c318bb4..e087d93 100644 --- a/src/Math/CMakeLists.txt +++ b/src/Math/CMakeLists.txt @@ -7,6 +7,7 @@ set(HEADERS Transform.h StructuredData.h # StructuredGrid.h + ImageSpace.h ImageMap.h ImageData.h VoxImage.h @@ -32,6 +33,7 @@ set(SOURCES VoxRaytracer.cpp StructuredData.cpp # StructuredGrid.cpp + ImageSpace.cpp ImageMap.cpp ImageData.cpp VoxImage.cpp diff --git a/src/Math/DataSet.h b/src/Math/DataSet.h index 5c7202e..1c9707c 100644 --- a/src/Math/DataSet.h +++ b/src/Math/DataSet.h @@ -35,37 +35,61 @@ #include "Math/StructuredData.h" #include +#include + + namespace uLib { -class DataSet_Base { -public: - DataSet_Base() {} - virtual ~DataSet_Base() {} - virtual const void * GetPtr(Id_t id) const = 0; - virtual void * GetPtr(Id_t id) = 0; - virtual size_t Size() const = 0; +class AbstractArray { +public: + virtual void * GetDataPointer(Id_t id) const = 0; + virtual void SetSize(const size_t size) = 0; + virtual const size_t GetSize() const = 0; + virtual ~AbstractArray() {} +}; + + +class DataSetAttributes { +public: + + template < typename GeT, typename SeT > + inline void SetScalars(GeT get, SeT set) { + m_Scalars.SetAccessFunctions(get,set); + } private: - - + ProgrammableAccessor m_Scalars; }; -template -class VectorData : public DataSet_Base, public Vector { + +template < typename T > +class DataVector : public AbstractArray { public: + void * GetDataPointer(Id_t id) const { return (void *)&m_Data.at(id); } + inline void SetSize(const size_t size) { m_Data.resize(size); } + inline const size_t GetSize() const { return m_Data.size(); } -protected: - const void *GetPtr(Id_t id) const { return &this->at(id); } - void * GetPtr(Id_t id) { return &this->at(id); } + uLibRefMacro(Data,Vector) + uLibRefMacro(Attributes,DataSetAttributes) +private: + DataSetAttributes m_Attributes; + Vector m_Data; }; +//class DataSet { +//public: +// virtual Vector3d GetPoint(const Id_t) const = 0; +// virtual Vector GetCell(const Id_t) const = 0; + +//private: +//}; diff --git a/src/Math/Dense.h b/src/Math/Dense.h index add140a..a2d5f80 100644 --- a/src/Math/Dense.h +++ b/src/Math/Dense.h @@ -53,7 +53,9 @@ #include +#include #include + #include "Core/Types.h" #include "Core/Serializable.h" @@ -106,6 +108,61 @@ EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex, cd) #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES #undef EIGEN_MAKE_TYPEDEFS + + + +//#define EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \ +///** \ingroup arraytypedefs */ \ +//typedef Eigen::Array Array##SizeSuffix##SizeSuffix##TypeSuffix; \ +///** \ingroup arraytypedefs */ \ +//typedef Eigen::Array Array##SizeSuffix##TypeSuffix; + +//#define EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \ +///** \ingroup arraytypedefs */ \ +//typedef Eigen::Array Array##Size##X##TypeSuffix; \ +///** \ingroup arraytypedefs */ \ +//typedef Eigen::Array Array##X##Size##TypeSuffix; + +//#define EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \ +//EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 2, 2) \ +//EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 3, 3) \ +//EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 4, 4) \ +//EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \ +//EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \ +//EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \ +//EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 4) + +//EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(int, i) +//EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(float, f) +//EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(double, d) +//EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex, cf) +//EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex, cd) + +//#undef EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES +//#undef EIGEN_MAKE_ARRAY_TYPEDEFS +//#undef EIGEN_MAKE_ARRAY_TYPEDEFS_LARGE + +//#define EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, SizeSuffix) \ +//using Eigen::Matrix##SizeSuffix##TypeSuffix; \ +//using Eigen::Vector##SizeSuffix##TypeSuffix; \ +//using Eigen::RowVector##SizeSuffix##TypeSuffix; + +//#define EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(TypeSuffix) \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 2) \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 3) \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 4) \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, X) \ + +//#define EIGEN_USING_ARRAY_TYPEDEFS \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(i) \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(f) \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(d) \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(cf) \ +//EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(cd) + + + + } // uLib diff --git a/src/Math/ImageData.cpp b/src/Math/ImageData.cpp index e69de29..d01a446 100644 --- a/src/Math/ImageData.cpp +++ b/src/Math/ImageData.cpp @@ -0,0 +1,181 @@ +#include "Math/ImageData.h" + +using namespace uLib; + + + +void ImageData::SetSize(const Vector3f v) +{ + ImageSpace::SetSize( v.array() / this->GetDims().array().cast() ); +} + +Vector3f ImageData::GetSize() const +{ + return ImageSpace::GetSize().array() * this->GetDims().array().cast(); +} + +bool ImageData::IsInsideBounds(const Vector4f pt) const +{ + Vector4f ptl = ImageSpace::GetLocalPoint(pt); + int result = 0; + for ( int i=0; i<3 ;++i) { + result += ptl(i) > (float)this->GetDims()(i); + result += ptl(i) < 0; + } + return result == 0; +} + + + + +void ImageData::ExportToVtk (const char *file, bool density_type) +{ + FILE * vtk_file = fopen(file,"wb"); + assert(vtk_file); + ImageData *voxels = this; + float norm; + if (density_type) { + norm = 40000; + } else norm = 1.E6; + int nx = voxels->GetDims()(0); + int ny = voxels->GetDims()(1); + int nz = voxels->GetDims()(2); + + fprintf(vtk_file, + "# vtk DataFile Version 2.0\n" + "Image Builder vtk output\n" + "ASCII\n" + "DATASET STRUCTURED_POINTS\n" + "DIMENSIONS %d %d %d\n" + "SPACING %f %f %f\n" + "ORIGIN %f %f %f\n" + "POINT_DATA %d\n" + "SCALARS volume_scalars float 1\n" + "LOOKUP_TABLE default\n", + nx , ny , nz , + voxels->GetSpacing()(0), + voxels->GetSpacing()(1), + voxels->GetSpacing()(2), + voxels->GetPosition()(0), // FIX FOR ORIGIN // + voxels->GetPosition()(1), + voxels->GetPosition()(2), + nx * ny * nz + ); + + Vector3i index(0,0,0); + for (int zv = 0; zv < nz; ++zv) { + for (int yv = 0; yv < ny; ++yv) { + for (int xv = 0; xv < nx; ++xv) { + index << xv,yv,zv; + // write voxel density in mrad^2/cm // + float density = fabs(voxels->GetValue(index)) * norm; + fprintf(vtk_file, "%f ", density); + } + } + } + + fclose(vtk_file); + printf("%s vtk file saved\n",file); +} + +int ImageData::ImportFromVtk(const char *file) +{ + FILE * vtk_file = fopen(file, "r"); + if (!vtk_file) return false; + + char word[20]; + int dx, dy, dz, n_tot; + float sx, sy, sz, ox, oy, oz; + do { + fscanf(vtk_file, "%s", word); + } while (strcmp(word, "DIMENSIONS")); + fscanf(vtk_file, "%d %d %d", &dx, &dy, &dz); + do { + fscanf(vtk_file, "%s", word); + } while (strcmp(word, "SPACING")); + fscanf(vtk_file, "%f %f %f", &sx, &sy, &sz); + do { + fscanf(vtk_file, "%s", word); + } while (strcmp(word, "ORIGIN")); + fscanf(vtk_file, "%f %f %f", &ox, &oy, &oz); + do { + fscanf(vtk_file, "%s", word); + } while (strcmp(word, "POINT_DATA")); + fscanf(vtk_file, "%d", &n_tot); + do { + fscanf(vtk_file, "%s", word); + } while (strcmp(word, "default")); + + this->SetDims(Vector3i(dx,dy,dz)); + this->SetSpacing(Vector3f(sx,sy,sz)); + this->SetPosition(Vector3f(ox,oy,oz)); + + for (int k = 0; k < dz; ++k) { + for (int j = 0; j < dy; ++j) { + for (int i = 0; i < dx; ++i) { + Vector3i idx(i, j, k); + float tmp_val; + fscanf(vtk_file, "%f", &tmp_val); + this->SetValue(idx,fabs(tmp_val)*1E-6); + } + } + } + fclose(vtk_file); + return true; +} + +void ImageData::ExportToVtkXml(const char *file, bool density_type) +{ + // Not implemented yet // + FILE * vtk_file = fopen(file,"wb"); + assert(vtk_file); + ImageData *voxels = this; + float norm; + if (density_type) { + norm = 40000; + } else norm = 1.E6; + int nx = voxels->GetDims()(0); + int ny = voxels->GetDims()(1); + int nz = voxels->GetDims()(2); + + fprintf(vtk_file, + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + "\n", + nx, ny, nz, + voxels->GetPosition()(0), // FIX FOR ORIGIN // + voxels->GetPosition()(1), + voxels->GetPosition()(2), + voxels->GetSpacing()(0), + voxels->GetSpacing()(1), + voxels->GetSpacing()(2), + nx, ny, nz + ); + Vector3i index(0,0,0); + for (int zv = 0; zv < nz; ++zv) { + for (int yv = 0; yv < ny; ++yv) { + for (int xv = 0; xv < nx; ++xv) { + index << xv,yv,zv; + // write voxel density in mrad^2/cm // + float density = fabs(voxels->GetValue(index)) * norm; + fprintf(vtk_file, "%f ", density); + } + } + } + fprintf(vtk_file, + "\n \n \n"); + + fprintf(vtk_file, + "\n \n \n"); + fclose(vtk_file); + printf("%s vtk file saved\n",file); +} diff --git a/src/Math/ImageData.h b/src/Math/ImageData.h index 69feac6..e7cecdc 100644 --- a/src/Math/ImageData.h +++ b/src/Math/ImageData.h @@ -37,18 +37,64 @@ namespace uLib { class ImageData : public ImageSpace, public ImageMap { - ImageData() {} +public: + ImageData() : ImageMap(Vector3i::Zero()) {} + ImageData(const Vector3i &size) : ImageMap(size) {} + + void SetSize(const Vector3f v); + + Vector3f GetSize() const; + + bool IsInsideBounds(const Vector4f pt) const; + + virtual void * GetDataPointer(const Id_t id) const = 0; + + virtual float GetValue(const Vector3i id) const = 0; + virtual float GetValue(const int id) const = 0; + virtual void SetValue(const Vector3i id, float value) = 0; + virtual void SetValue(const int id, float value) = 0; + + virtual void SetDims(const Vector3i &size) = 0; + + void ExportToVtk(const char *file, bool density_type = 0); + void ExportToVtkXml(const char *file, bool density_type = 0); + int ImportFromVtk(const char *file); +}; + + +class DataVectorImage : public ImageData { public: + inline void * GetDataPointer(const Id_t id) const { + return m_Data->GetDataPointer(id); + } + float GetValue(const Vector3i id) const { return m_Scalars.Get(GetDataPointer(Map(id))); } + float GetValue(const int id) const { return m_Scalars.Get(GetDataPointer(id)); } + void SetValue(const Vector3i id, float value) { m_Scalars.Set(GetDataPointer(Map(id)),value); } + void SetValue(const int id, float value) { m_Scalars.Set(GetDataPointer(id),value); } + + void SetDims(const Vector3i &size) { + ImageMap::SetDims(size); + m_Data->SetSize(size.prod()); + } + + uLibRefMacro(Data,SmartPointer) + uLibRefMacro(Scalars,ProgrammableAccessor) +private: + SmartPointer m_Data; + ProgrammableAccessor m_Scalars; }; + + + } // uLib #endif // IMAGEDATA_H diff --git a/src/Math/ImageMap.h b/src/Math/ImageMap.h index e27cd8d..bbda7d0 100644 --- a/src/Math/ImageMap.h +++ b/src/Math/ImageMap.h @@ -31,8 +31,19 @@ #include "Math/Dense.h" #include "Math/DataSet.h" +//#include "boost/range.hpp" +//#include "boost/range/iterator.hpp" + + + + + namespace uLib { + + + + class ImageMap { public: typedef enum { @@ -69,6 +80,9 @@ public: Vector3i UnMap(int index) const; + + + private: Order m_DataOrder; Vector3i m_Dims; diff --git a/src/Math/ImageSpace.cpp b/src/Math/ImageSpace.cpp index 0cb5112..901b32b 100644 --- a/src/Math/ImageSpace.cpp +++ b/src/Math/ImageSpace.cpp @@ -28,19 +28,10 @@ namespace uLib { -ImageSpace::ImageSpace(const Vector3i size) : - StructuredData(size) -{} +//ImageSpace::ImageSpace(const Vector3i size) : +// StructuredData(size) +//{} -//void ImageSpace::SetSize(const Vector3f v) -//{ -// ContainerBox::SetSize( v.array() / this->GetDims().array().cast() ); -//} - -//Vector3f ImageSpace::GetSize() const -//{ -// return ContainerBox::GetSize().array() * this->GetDims().array().cast(); -//} void ImageSpace::SetSpacing(const Vector3f spacing) { @@ -52,18 +43,6 @@ Vector3f ImageSpace::GetSpacing() const return ContainerBox::GetSize(); } - -//bool ImageSpace::IsInsideBounds(const Vector4f pt) const -//{ -// Vector4f ptl = this->GetLocalPoint(pt); -// int result = 0; -// for ( int i=0; i<3 ;++i) { -// result += ptl(i) > (float)this->GetDims()(i); -// result += ptl(i) < 0; -// } -// return result == 0; -//} - Vector3i ImageSpace::Find(const Vector4f pt) const { Vector3i out; diff --git a/src/Math/ImageSpace.h b/src/Math/ImageSpace.h index 2991318..e93dd86 100644 --- a/src/Math/ImageSpace.h +++ b/src/Math/ImageSpace.h @@ -35,25 +35,18 @@ namespace uLib { class ImageSpace : public ContainerBox { typedef ContainerBox BaseClass; public: - ImageSpace(const Vector3i size); using ContainerBox::SetOrigin; -// void SetSize(const Vector3f v); - -// Vector3f GetSize() const; - void SetSpacing(const Vector3f spacing); Vector3f GetSpacing() const; -// bool IsInsideBounds(const Vector4f pt) const; - Vector3i Find(const Vector4f pt) const; void PrintSelf(std::ostream &o); -private: +protected: using ContainerBox::GetSize; using ContainerBox::SetSize; }; diff --git a/src/Math/Line.h b/src/Math/Line.h index a192efd..f1c0d52 100644 --- a/src/Math/Line.h +++ b/src/Math/Line.h @@ -50,6 +50,10 @@ public: Vector & direction() { return this->m_Data[1]; } const Vector & origin() const { return this->m_Data[0]; } const Vector & direction() const { return this->m_Data[1]; } + _Scalar & origin(Id_t id) { return this->m_Data[0](id); } + _Scalar & direction(Id_t id) { return this->m_Data[1](id); } + const _Scalar & origin(Id_t id) const { return this->m_Data[0](id); } + const _Scalar & direction(Id_t id) const { return this->m_Data[1](id); } }; typedef Line Line2f; diff --git a/src/Math/StructuredData.cpp b/src/Math/StructuredData.cpp index 456747a..3625cb5 100644 --- a/src/Math/StructuredData.cpp +++ b/src/Math/StructuredData.cpp @@ -27,54 +27,54 @@ #include "StructuredData.h" -using namespace uLib; +//using namespace uLib; -StructuredData::StructuredData(const Vector3i &size) : - m_Dims(size) -{ - SetDataOrder(); -} +//StructuredData::StructuredData(const Vector3i &size) : +// m_Dims(size) +//{ +// SetDataOrder(); +//} -void StructuredData::SetDims(const Vector3i &size) -{ - this->m_Dims = size; - SetDataOrder(); -} +//void StructuredData::SetDims(const Vector3i &size) +//{ +// this->m_Dims = size; +// SetDataOrder(); +//} -void StructuredData::SetDataOrder(StructuredData::Order order) -{ - int i = order & 0x3; - int j = (order >> 2) & 0x3; - int k = (order >> 4) & 0x3; - this->m_Increments[i] = 1; - this->m_Increments[j] = m_Dims[i]; - this->m_Increments[k] = m_Dims[i] * m_Dims[j]; - this->m_DataOrder = order; -} +//void StructuredData::SetDataOrder(StructuredData::Order order) +//{ +// int i = order & 0x3; +// int j = (order >> 2) & 0x3; +// int k = (order >> 4) & 0x3; +// this->m_Increments[i] = 1; +// this->m_Increments[j] = m_Dims[i]; +// this->m_Increments[k] = m_Dims[i] * m_Dims[j]; +// this->m_DataOrder = order; +//} -bool StructuredData::IsInsideGrid(const Vector3i &v) const -{ - int vok = 1; - vok *= (v(0) >= 0 && v(0) < m_Dims[0]); - vok *= (v(1) >= 0 && v(1) < m_Dims[1]); - vok *= (v(2) >= 0 && v(2) < m_Dims[2]); - return vok; -} +//bool StructuredData::IsInsideGrid(const Vector3i &v) const +//{ +// int vok = 1; +// vok *= (v(0) >= 0 && v(0) < m_Dims[0]); +// vok *= (v(1) >= 0 && v(1) < m_Dims[1]); +// vok *= (v(2) >= 0 && v(2) < m_Dims[2]); +// return vok; +//} -Vector3i StructuredData::UnMap(int index) const -{ - Vector3i v( 0,0,0 ); - Vector3i iv = m_Increments; - int id = 0; - for(int k=0; k<3; ++k) { - int inc = iv.maxCoeff(&id); - v(id) = index / inc; - index -= v(id) * inc; - iv(id) = 0; - } - return v; -} +//Vector3i StructuredData::UnMap(int index) const +//{ +// Vector3i v( 0,0,0 ); +// Vector3i iv = m_Increments; +// int id = 0; +// for(int k=0; k<3; ++k) { +// int inc = iv.maxCoeff(&id); +// v(id) = index / inc; +// index -= v(id) * inc; +// iv(id) = 0; +// } +// return v; +//} diff --git a/src/Math/StructuredData.h b/src/Math/StructuredData.h index 9ccb868..600c670 100644 --- a/src/Math/StructuredData.h +++ b/src/Math/StructuredData.h @@ -28,74 +28,74 @@ #ifndef STRUCTUREDDATA_H #define STRUCTUREDDATA_H -#include "Core/Macros.h" -#include "Core/Object.h" -#include "Math/Dense.h" -#include "Math/DataSet.h" +//#include "Core/Macros.h" +//#include "Core/Object.h" +//#include "Math/Dense.h" +//#include "Math/DataSet.h" -namespace uLib { +//namespace uLib { -class StructuredData { -public: - enum _Order - { - CustomOrder = 0, - XYZ = 0 | 1 << 2 | 2 << 4, - XZY = 0 | 2 << 2 | 1 << 4, - YXZ = 1 | 0 << 2 | 2 << 4, - YZX = 2 | 0 << 2 | 1 << 4, - ZXY = 1 | 2 << 2 | 0 << 4, - ZYX = 2 | 1 << 2 | 0 << 4 - }; +//class StructuredData { +//public: +// enum _Order +// { +// CustomOrder = 0, +// XYZ = 0 | 1 << 2 | 2 << 4, +// XZY = 0 | 2 << 2 | 1 << 4, +// YXZ = 1 | 0 << 2 | 2 << 4, +// YZX = 2 | 0 << 2 | 1 << 4, +// ZXY = 1 | 2 << 2 | 0 << 4, +// ZYX = 2 | 1 << 2 | 0 << 4 +// }; - typedef enum _Order Order; +// typedef enum _Order Order; - StructuredData(const Vector3i &size); +// StructuredData(const Vector3i &size); - StructuredData(const StructuredData ©) : - m_DataOrder(copy.m_DataOrder), - m_Dims(copy.m_Dims), - m_Increments(copy.m_Increments) - {} +// StructuredData(const StructuredData ©) : +// m_DataOrder(copy.m_DataOrder), +// m_Dims(copy.m_Dims), +// m_Increments(copy.m_Increments) +// {} - uLibGetMacro(Dims,Vector3i) +// uLibGetMacro(Dims,Vector3i) - void SetDims(const Vector3i &size); +// void SetDims(const Vector3i &size); - uLibGetSetMacro(Increments,Vector3i) +// uLibGetSetMacro(Increments,Vector3i) - void SetDataOrder(Order order = YXZ); +// void SetDataOrder(Order order = YXZ); - uLibGetMacro(DataOrder,Order) +// uLibGetMacro(DataOrder,Order) - bool IsInsideGrid(const Vector3i &v) const; +// bool IsInsideGrid(const Vector3i &v) const; - inline int Map(Vector3i index) const; +// inline int Map(Vector3i index) const; - Vector3i UnMap(int index) const; +// Vector3i UnMap(int index) const; -private: - Order m_DataOrder; - Vector3i m_Dims; - Vector3i m_Increments; //TODO: make this line matrix // -}; +//private: +// Order m_DataOrder; +// Vector3i m_Dims; +// Vector3i m_Increments; //TODO: make this line matrix // +//}; -// --- INLINES -------------------------------------------------------------- // +//// --- INLINES -------------------------------------------------------------- // -inline int StructuredData::Map(Vector3i index) const -{ - return (m_Increments.transpose() * index); -} +//inline int StructuredData::Map(Vector3i index) const +//{ +// return (m_Increments.transpose() * index); +//} -} +//} #endif // STRUCTUREDDATA_H diff --git a/src/Math/VoxImage.cpp b/src/Math/VoxImage.cpp index a73f94e..ec7bac5 100644 --- a/src/Math/VoxImage.cpp +++ b/src/Math/VoxImage.cpp @@ -27,163 +27,162 @@ #include #include - #include "VoxImage.h" namespace uLib { -void Abstract::VoxImage::ExportToVtk (const char *file, bool density_type) -{ - FILE * vtk_file = fopen(file,"wb"); - assert(vtk_file); - Abstract::VoxImage *voxels = this; - float norm; - if (density_type) { - norm = 40000; - } else norm = 1.E6; - int nx = voxels->GetDims()(0); - int ny = voxels->GetDims()(1); - int nz = voxels->GetDims()(2); +//void Abstract::VoxImage::ExportToVtk (const char *file, bool density_type) +//{ +// FILE * vtk_file = fopen(file,"wb"); +// assert(vtk_file); +// ImageData *voxels = this; +// float norm; +// if (density_type) { +// norm = 40000; +// } else norm = 1.E6; +// int nx = voxels->GetDims()(0); +// int ny = voxels->GetDims()(1); +// int nz = voxels->GetDims()(2); - fprintf(vtk_file, - "# vtk DataFile Version 2.0\n" - "Image Builder vtk output\n" - "ASCII\n" - "DATASET STRUCTURED_POINTS\n" - "DIMENSIONS %d %d %d\n" - "SPACING %f %f %f\n" - "ORIGIN %f %f %f\n" - "POINT_DATA %d\n" - "SCALARS volume_scalars float 1\n" - "LOOKUP_TABLE default\n", - nx , ny , nz , - voxels->GetSpacing()(0), - voxels->GetSpacing()(1), - voxels->GetSpacing()(2), - voxels->GetPosition()(0), // FIX FOR ORIGIN // - voxels->GetPosition()(1), - voxels->GetPosition()(2), - nx * ny * nz - ); +// fprintf(vtk_file, +// "# vtk DataFile Version 2.0\n" +// "Image Builder vtk output\n" +// "ASCII\n" +// "DATASET STRUCTURED_POINTS\n" +// "DIMENSIONS %d %d %d\n" +// "SPACING %f %f %f\n" +// "ORIGIN %f %f %f\n" +// "POINT_DATA %d\n" +// "SCALARS volume_scalars float 1\n" +// "LOOKUP_TABLE default\n", +// nx , ny , nz , +// voxels->GetSpacing()(0), +// voxels->GetSpacing()(1), +// voxels->GetSpacing()(2), +// voxels->GetPosition()(0), // FIX FOR ORIGIN // +// voxels->GetPosition()(1), +// voxels->GetPosition()(2), +// nx * ny * nz +// ); - Vector3i index(0,0,0); - for (int zv = 0; zv < nz; ++zv) { - for (int yv = 0; yv < ny; ++yv) { - for (int xv = 0; xv < nx; ++xv) { - index << xv,yv,zv; - // write voxel density in mrad^2/cm // - float density = fabs(voxels->GetValue(index)) * norm; - fprintf(vtk_file, "%f ", density); - } - } - } +// Vector3i index(0,0,0); +// for (int zv = 0; zv < nz; ++zv) { +// for (int yv = 0; yv < ny; ++yv) { +// for (int xv = 0; xv < nx; ++xv) { +// index << xv,yv,zv; +// // write voxel density in mrad^2/cm // +// float density = fabs(voxels->GetValue(index)) * norm; +// fprintf(vtk_file, "%f ", density); +// } +// } +// } - fclose(vtk_file); - printf("%s vtk file saved\n",file); -} +// fclose(vtk_file); +// printf("%s vtk file saved\n",file); +//} -int Abstract::VoxImage::ImportFromVtk(const char *file) -{ - FILE * vtk_file = fopen(file, "r"); - if (!vtk_file) return false; +//int Abstract::VoxImage::ImportFromVtk(const char *file) +//{ +// FILE * vtk_file = fopen(file, "r"); +// if (!vtk_file) return false; - char word[20]; - int dx, dy, dz, n_tot; - float sx, sy, sz, ox, oy, oz; - do { - fscanf(vtk_file, "%s", word); - } while (strcmp(word, "DIMENSIONS")); - fscanf(vtk_file, "%d %d %d", &dx, &dy, &dz); - do { - fscanf(vtk_file, "%s", word); - } while (strcmp(word, "SPACING")); - fscanf(vtk_file, "%f %f %f", &sx, &sy, &sz); - do { - fscanf(vtk_file, "%s", word); - } while (strcmp(word, "ORIGIN")); - fscanf(vtk_file, "%f %f %f", &ox, &oy, &oz); - do { - fscanf(vtk_file, "%s", word); - } while (strcmp(word, "POINT_DATA")); - fscanf(vtk_file, "%d", &n_tot); - do { - fscanf(vtk_file, "%s", word); - } while (strcmp(word, "default")); +// char word[20]; +// int dx, dy, dz, n_tot; +// float sx, sy, sz, ox, oy, oz; +// do { +// fscanf(vtk_file, "%s", word); +// } while (strcmp(word, "DIMENSIONS")); +// fscanf(vtk_file, "%d %d %d", &dx, &dy, &dz); +// do { +// fscanf(vtk_file, "%s", word); +// } while (strcmp(word, "SPACING")); +// fscanf(vtk_file, "%f %f %f", &sx, &sy, &sz); +// do { +// fscanf(vtk_file, "%s", word); +// } while (strcmp(word, "ORIGIN")); +// fscanf(vtk_file, "%f %f %f", &ox, &oy, &oz); +// do { +// fscanf(vtk_file, "%s", word); +// } while (strcmp(word, "POINT_DATA")); +// fscanf(vtk_file, "%d", &n_tot); +// do { +// fscanf(vtk_file, "%s", word); +// } while (strcmp(word, "default")); - this->SetDims(Vector3i(dx,dy,dz)); - this->SetSpacing(Vector3f(sx,sy,sz)); - this->SetPosition(Vector3f(ox,oy,oz)); +// this->SetDims(Vector3i(dx,dy,dz)); +// this->SetSpacing(Vector3f(sx,sy,sz)); +// this->SetPosition(Vector3f(ox,oy,oz)); - for (int k = 0; k < dz; ++k) { - for (int j = 0; j < dy; ++j) { - for (int i = 0; i < dx; ++i) { - Vector3i idx(i, j, k); - float tmp_val; - fscanf(vtk_file, "%f", &tmp_val); - this->SetValue(idx,fabs(tmp_val)*1E-6); - } - } - } - fclose(vtk_file); - return true; -} +// for (int k = 0; k < dz; ++k) { +// for (int j = 0; j < dy; ++j) { +// for (int i = 0; i < dx; ++i) { +// Vector3i idx(i, j, k); +// float tmp_val; +// fscanf(vtk_file, "%f", &tmp_val); +// this->SetValue(idx,fabs(tmp_val)*1E-6); +// } +// } +// } +// fclose(vtk_file); +// return true; +//} -void Abstract::VoxImage::ExportToVtkXml(const char *file, bool density_type) -{ - // Not implemented yet // - FILE * vtk_file = fopen(file,"wb"); - assert(vtk_file); - Abstract::VoxImage *voxels = this; - float norm; - if (density_type) { - norm = 40000; - } else norm = 1.E6; - int nx = voxels->GetDims()(0); - int ny = voxels->GetDims()(1); - int nz = voxels->GetDims()(2); +//void Abstract::VoxImage::ExportToVtkXml(const char *file, bool density_type) +//{ +// // Not implemented yet // +// FILE * vtk_file = fopen(file,"wb"); +// assert(vtk_file); +// ImageData *voxels = this; +// float norm; +// if (density_type) { +// norm = 40000; +// } else norm = 1.E6; +// int nx = voxels->GetDims()(0); +// int ny = voxels->GetDims()(1); +// int nz = voxels->GetDims()(2); - fprintf(vtk_file, - "\n" +// fprintf(vtk_file, +// "\n" - "\n" +// "\n" - "\n" +// "\n" - "\n" +// "\n" - "\n" - "\n", - nx, ny, nz, - voxels->GetPosition()(0), // FIX FOR ORIGIN // - voxels->GetPosition()(1), - voxels->GetPosition()(2), - voxels->GetSpacing()(0), - voxels->GetSpacing()(1), - voxels->GetSpacing()(2), - nx, ny, nz - ); - Vector3i index(0,0,0); - for (int zv = 0; zv < nz; ++zv) { - for (int yv = 0; yv < ny; ++yv) { - for (int xv = 0; xv < nx; ++xv) { - index << xv,yv,zv; - // write voxel density in mrad^2/cm // - float density = fabs(voxels->GetValue(index)) * norm; - fprintf(vtk_file, "%f ", density); - } - } - } - fprintf(vtk_file, - "\n \n \n"); +// "\n" +// "\n", +// nx, ny, nz, +// voxels->GetPosition()(0), // FIX FOR ORIGIN // +// voxels->GetPosition()(1), +// voxels->GetPosition()(2), +// voxels->GetSpacing()(0), +// voxels->GetSpacing()(1), +// voxels->GetSpacing()(2), +// nx, ny, nz +// ); +// Vector3i index(0,0,0); +// for (int zv = 0; zv < nz; ++zv) { +// for (int yv = 0; yv < ny; ++yv) { +// for (int xv = 0; xv < nx; ++xv) { +// index << xv,yv,zv; +// // write voxel density in mrad^2/cm // +// float density = fabs(voxels->GetValue(index)) * norm; +// fprintf(vtk_file, "%f ", density); +// } +// } +// } +// fprintf(vtk_file, +// "\n \n \n"); - fprintf(vtk_file, - "\n \n \n"); - fclose(vtk_file); - printf("%s vtk file saved\n",file); -} +// fprintf(vtk_file, +// "\n \n \n"); +// fclose(vtk_file); +// printf("%s vtk file saved\n",file); +//} diff --git a/src/Math/VoxImage.h b/src/Math/VoxImage.h index d845949..f537d4d 100644 --- a/src/Math/VoxImage.h +++ b/src/Math/VoxImage.h @@ -45,28 +45,28 @@ namespace uLib { // ABSTRACT VOX IMAGE ////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// -namespace Abstract { -class VoxImage : public uLib::ImageData { -public: - typedef uLib::ImageData BaseClass; +//namespace Abstract { +//class VoxImage : public uLib::ImageData { +//public: +// typedef uLib::ImageData BaseClass; - virtual float GetValue(const Vector3i id) const = 0; - virtual float GetValue(const int id) const = 0; - virtual void SetValue(const Vector3i id, float value) = 0; - virtual void SetValue(const int id, float value) = 0; +// virtual float GetValue(const Vector3i id) const = 0; +// virtual float GetValue(const int id) const = 0; +// virtual void SetValue(const Vector3i id, float value) = 0; +// virtual void SetValue(const int id, float value) = 0; - virtual void SetDims(const Vector3i &size) = 0; +// virtual void SetDims(const Vector3i &size) = 0; - void ExportToVtk(const char *file, bool density_type = 0); - void ExportToVtkXml(const char *file, bool density_type = 0); - int ImportFromVtk(const char *file); +// void ExportToVtk(const char *file, bool density_type = 0); +// void ExportToVtkXml(const char *file, bool density_type = 0); +// int ImportFromVtk(const char *file); -protected: +//protected: - virtual ~VoxImage() {} - VoxImage(const Vector3i &size) : BaseClass(size) {} -}; -} +// virtual ~VoxImage() {} +// VoxImage(const Vector3i &size) : BaseClass(size) {} +//}; +//} //////////////////////////////////////////////////////////////////////////////// // VOXEL //////////////////////////////////////////////////////////////////// @@ -92,9 +92,9 @@ std::basic_ostream & operator << (std::basic_ostream &o, const Voxel &v) { template< typename T > -class VoxImage : public Abstract::VoxImage { +class VoxImage : public ImageData { public: - typedef Abstract::VoxImage BaseClass; + typedef ImageData BaseClass; VoxImage(); @@ -120,14 +120,16 @@ public: inline T& operator[](const Vector3i &id) { return m_Data[Map(id)]; } // this implements Abstract interface // - inline Scalarf GetValue(const Vector3i id) const { return this->At(id)(); } - inline Scalarf GetValue(const int id) const { return this->At(id)(); } - inline void SetValue(const Vector3i id, Scalarf value) { this->operator [](id)() = value; } - inline void SetValue(const int id, float value) { this->operator [](id)() = value; } + inline void * GetDataPointer(const Id_t id) const { return (void *)&this->At(id); } + inline Scalarf GetValue(const Vector3i id) const { return this->At(id).Value; } + inline Scalarf GetValue(const int id) const { return this->At(id).Value; } + inline void SetValue(const Vector3i id, Scalarf value) { this->operator [](id).Value = value; } + inline void SetValue(const int id, float value) { this->operator [](id).Value = value; } + // fix needed inline void SetDims(const Vector3i &size) { this->m_Data.resize(size.prod()); - StructuredData::SetDims(size); + ImageMap::SetDims(size); } inline VoxImage clipImage(const Vector3i begin, const Vector3i end) const; @@ -213,13 +215,11 @@ template VoxImage::VoxImage() : m_Data(0), BaseClass(Vector3i(0,0,0)) {} -//{ Interface::IsA (); /* structural check for T */ } template VoxImage::VoxImage(const Vector3i &size) : m_Data(size.prod()), BaseClass(size) {} -//{ Interface::IsA (); /* structural check for T */ } template diff --git a/src/Math/VoxImageFilter.h b/src/Math/VoxImageFilter.h index c9fcf9d..dafba6a 100644 --- a/src/Math/VoxImageFilter.h +++ b/src/Math/VoxImageFilter.h @@ -55,7 +55,7 @@ class VoxImageFilter { public: virtual void Run() = 0; - virtual void SetImage(Abstract::VoxImage *image) = 0; + virtual void SetImage(ImageData *image) = 0; protected: virtual ~VoxImageFilter() {} @@ -88,7 +88,7 @@ public: uLibGetMacro(Image,VoxImage *) - void SetImage(Abstract::VoxImage *image); + void SetImage(ImageData *image); protected: diff --git a/src/Math/VoxImageFilter.hpp b/src/Math/VoxImageFilter.hpp index 621c208..3e90565 100644 --- a/src/Math/VoxImageFilter.hpp +++ b/src/Math/VoxImageFilter.hpp @@ -39,8 +39,8 @@ namespace uLib { // KERNEL ////////////////////////////////////////////////////////////////////// template < typename T > -class Kernel : public StructuredData { - typedef StructuredData BaseClass; +class Kernel : public ImageMap { + typedef ImageMap BaseClass; public: Kernel(const Vector3i &size); @@ -63,7 +63,7 @@ Kernel::Kernel(const Vector3i &size) : BaseClass(size), m_Data(size.prod()) { - Interface::IsA(); +// Interface::IsA(); } template < typename T > @@ -166,7 +166,7 @@ _TPL_ void VoxImageFilter<_TPLT_>::SetKernelNumericXZY(const Vector &numeric) { // set data order // - StructuredData::Order order = m_KernelData.GetDataOrder(); + ImageMap::Order order = m_KernelData.GetDataOrder(); //m_KernelData.SetDataOrder(StructuredData::XZY); Vector3i id; int index = 0; @@ -258,7 +258,7 @@ void VoxImageFilter<_TPLT_>::SetKernelWeightFunction(ShapeT shape) _TPL_ -void VoxImageFilter<_TPLT_>::SetImage(Abstract::VoxImage *image) +void VoxImageFilter<_TPLT_>::SetImage(ImageData *image) { this->m_Image = reinterpret_cast *> (image); this->SetKernelOffset(); diff --git a/src/Math/testing/CMakeLists.txt b/src/Math/testing/CMakeLists.txt index 278c133..a79e1a6 100644 --- a/src/Math/testing/CMakeLists.txt +++ b/src/Math/testing/CMakeLists.txt @@ -3,6 +3,7 @@ set(TESTS MathVectorTest GeometryTest ContainerBoxTest + ImageDataTest VoxImageTest VoxRaytracerTest StructuredDataTest diff --git a/src/Math/testing/ImageDataTest.cpp b/src/Math/testing/ImageDataTest.cpp new file mode 100644 index 0000000..3fb8010 --- /dev/null +++ b/src/Math/testing/ImageDataTest.cpp @@ -0,0 +1,78 @@ +/*////////////////////////////////////////////////////////////////////////////// +// 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 "testing-prototype.h" +#include "Math/ImageData.h" + +using namespace uLib; + +namespace { +struct MyVoxel { + MyVoxel() : value(0), count(0) {} + float value; + int count; +}; + + +struct VoxelMean : public MyVoxel { + VoxelMean() {} + void SetValue(const float value) { this->value += value; ++count; } + float GetValue() const { return value/count; } +}; +} + + + + +int main() { + + DataVector v; + + DataVectorImage img; + + img.Data() = v; + img.Scalars().SetAccessFunctions(&MyVoxel::value); + img.SetDims(Vector3i(3,3,3)); + + for (int x=0; x " << img.GetValue(x) << "\n"; + } + + DataVector vm; + + img.Data() = vm; + img.SetDims(Vector3i(3,3,3)); + img.Scalars().SetAccessFunctions(&VoxelMean::GetValue,&VoxelMean::SetValue); + + for (int x=0; x " << img.GetValue(x) << "\n"; + } + + +} diff --git a/src/Vtk/vtkVoxImage.h b/src/Vtk/vtkVoxImage.h index 6497f0a..a66ec60 100644 --- a/src/Vtk/vtkVoxImage.h +++ b/src/Vtk/vtkVoxImage.h @@ -48,7 +48,7 @@ public: }; public: - typedef Abstract::VoxImage Content; + typedef ImageData Content; vtkVoxImage(Content &content); diff --git a/src/ltk/ltktypes.h b/src/ltk/ltktypes.h index c243841..0b1bda5 100644 --- a/src/ltk/ltktypes.h +++ b/src/ltk/ltktypes.h @@ -50,6 +50,27 @@ C_BEGIN_DECLS /* ////////////////////////////////////////////////////////////////////////// */ +#define LTK_VOID 0 +#define LTK_BIT 1 +#define LTK_CHAR 2 +#define LTK_SIGNED_CHAR 15 +#define LTK_UNSIGNED_CHAR 3 +#define LTK_SHORT 4 +#define LTK_UNSIGNED_SHORT 5 +#define LTK_INT 6 +#define LTK_UNSIGNED_INT 7 +#define LTK_LONG 8 +#define LTK_UNSIGNED_LONG 9 +#define LTK_FLOAT 10 +#define LTK_DOUBLE 11 +#define LTK_ID_TYPE 12 + + +#define LTK_OBJECT 21 + + + + typedef void * Pointer_t; typedef id_t Id_t; typedef size_t Size_t;