From 0b3efddeada95dcd4483e365da88ce267798eb71 Mon Sep 17 00:00:00 2001 From: Andrea Rigoni Date: Wed, 24 Dec 2014 09:58:56 +0000 Subject: [PATCH] [uLib geometry] - adds some getters/setters to Programmable accessor - DataSet and Attributes ... start working on .. to be continued --- src/Core/ProgrammableAccessor.h | 53 ++++++------- src/Core/testing/ProgrammableAccessorTest.cpp | 4 +- src/Core/testing/VectorTest.cpp | 3 +- src/Math/DataSet.h | 36 +++------ src/Math/ImageData.cpp | 47 +++++++++++ src/Math/ImageData.h | 79 +++++++++++++------ src/Math/ImageMap.h | 3 - src/Math/testing/ImageDataTest.cpp | 57 ++++++++----- 8 files changed, 178 insertions(+), 104 deletions(-) diff --git a/src/Core/ProgrammableAccessor.h b/src/Core/ProgrammableAccessor.h index 6d0cf30..e36a209 100644 --- a/src/Core/ProgrammableAccessor.h +++ b/src/Core/ProgrammableAccessor.h @@ -35,22 +35,9 @@ struct is_iterator { 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 { -// return static_cast(this->Get(ob)); -// } - -// template -// void Set(void *ob, const T data) const { -// this->Set(ob,static_cast(data)); -// } - virtual ~ProgrammableAccessor_Base() {} }; @@ -181,25 +168,19 @@ template class ProgrammableAccessor : public Named { public: -// struct Wrapper { -// Wrapper(const ProgrammableAccessor *ac, void *ob) : -// m_access(ac), m_object(ob) -// { assert(ob != NULL); } - -// template -// inline T Get() const { return static_cast(m_access->Get(m_object)); } - -// template -// inline void Set(const T data) const { return m_access->Set(m_object,static_cast(data)); } - -// void *m_object; -// const ProgrammableAccessor *m_access; -// }; ProgrammableAccessor() : Named("") {} ProgrammableAccessor(const char *name) : Named(name) {} + template < typename F > + ProgrammableAccessor(const char *name, F f) : Named(name) + { SetAccessFunctions(f); } + + template < typename F1, typename F2 > + ProgrammableAccessor(const char *name, F1 f1, F2 f2) : Named(name) + { SetAccessFunctions(f1,f2); } + template < typename F > ProgrammableAccessor(F f) : Named(f) { SetAccessFunctions(f); } @@ -209,8 +190,7 @@ public: { SetAccessFunctions(f1,f2); } - // ----- move to factory // - + // ----- factory functions // template inline void SetAccessFunctions(R(*_pg)(const T1 *), void(*_ps)(T1*,R) = NULL) { m_base = SmartPointer< detail::ProgrammableAccessor_Base >(new detail::functor_by_static_acc(_pg,_ps)); @@ -243,8 +223,23 @@ public: } // ------ // +/* + // struct Wrapper { + // Wrapper(const ProgrammableAccessor *ac, void *ob) : + // m_access(ac), m_object(ob) + // { assert(ob != NULL); } + // template + // inline T Get() const { return static_cast(m_access->Get(m_object)); } + + // template + // inline void Set(const T data) const { return m_access->Set(m_object,static_cast(data)); } + + // void *m_object; + // const ProgrammableAccessor *m_access; + // }; // 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/testing/ProgrammableAccessorTest.cpp b/src/Core/testing/ProgrammableAccessorTest.cpp index 1bbbc59..4457da4 100644 --- a/src/Core/testing/ProgrammableAccessorTest.cpp +++ b/src/Core/testing/ProgrammableAccessorTest.cpp @@ -62,6 +62,8 @@ namespace uLib { using namespace uLib; + + template < typename T > void print_is_iter(const T &t, typename boost::disable_if< detail::is_iterator >::type *dummy = 0 ) { std::cout << "no"; @@ -146,7 +148,7 @@ int main() { std::cout << va2.Get(&v) << "\n"; } - { + { // test is iterator .. Vector::Iterator it; Vox v; std::cout << "is iter? "; diff --git a/src/Core/testing/VectorTest.cpp b/src/Core/testing/VectorTest.cpp index 8a830bd..f88e229 100644 --- a/src/Core/testing/VectorTest.cpp +++ b/src/Core/testing/VectorTest.cpp @@ -85,7 +85,8 @@ int main() for(uLib::Vector::Iterator it = v.begin(); it!=v.end(); it++) std::cout << *it <<" "; std::cout << std::endl; -// std::sort(v.begin(),v.end(),LT()); + + // std::sort(v.begin(),v.end(),LT()); END_TESTING; diff --git a/src/Math/DataSet.h b/src/Math/DataSet.h index c56b085..3636d39 100644 --- a/src/Math/DataSet.h +++ b/src/Math/DataSet.h @@ -43,33 +43,12 @@ namespace uLib { -//typedef std::iterator SequenceIterator; - -//template < typename T > -//struct Enumeration { - -//}; - -//template < typename T > -//struct Iterator { - -//}; - -//class AbstractDenseSequence { -//public: - -// virtual bool Empty() const = 0; - - -//}; class AbstractArray { public: - virtual const void * GetDataPointer(Id_t id) const = 0; - virtual void * GetDataPointer(Id_t id) = 0; - virtual void SetSize(const size_t size) = 0; - virtual const size_t GetSize() const = 0; + // virtual const void * GetDataPointer(Id_t id) const = 0; + virtual void * GetDataPointer(Id_t id) = 0; virtual ~AbstractArray() {} }; @@ -78,13 +57,14 @@ public: template < typename T > class DataAttributes { public: + DataAttributes() : m_Active(NULL) {} + template < typename F > void AddAttribute(const char *name, F f) { ProgrammableAccessor pa(name); pa.SetAccessFunctions(f); m_Accessors.push_back(pa); - if(m_Accessors.size() == 1) - SetActive(name); + m_Active = &m_Accessors.back(); } template < typename F1, typename F2 > @@ -92,8 +72,7 @@ public: ProgrammableAccessor pa(name); pa.SetAccessFunctions(f1,f2); m_Accessors.push_back(pa); - if(m_Accessors.size() == 1) - SetActive(name); + m_Active = &m_Accessors.back(); } ProgrammableAccessor * GetAttribute(const char *name) /*const*/ { @@ -159,6 +138,9 @@ private: }; + + +// TO BE MOVED OR REMOVED ... template < typename T, class ScalarAccess diff --git a/src/Math/ImageData.cpp b/src/Math/ImageData.cpp index d01a446..dc658bc 100644 --- a/src/Math/ImageData.cpp +++ b/src/Math/ImageData.cpp @@ -179,3 +179,50 @@ void ImageData::ExportToVtkXml(const char *file, bool density_type) fclose(vtk_file); printf("%s vtk file saved\n",file); } + + + + +//////////////////////////////////////////////////////////////////////////////// +// DATASET IMAGE + +//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; +//} + + + + + + + + + + + + + + + + + + + + diff --git a/src/Math/ImageData.h b/src/Math/ImageData.h index 32330ee..f474a8b 100644 --- a/src/Math/ImageData.h +++ b/src/Math/ImageData.h @@ -41,8 +41,7 @@ public: ImageData() : ImageMap(Vector3i::Zero()) {} ImageData(const Vector3i &size) : ImageMap(size) {} - void SetSize(const Vector3f v); - + void SetSize(const Vector3f v); Vector3f GetSize() const; bool IsInsideBounds(const Vector4f pt) const; @@ -51,8 +50,8 @@ public: 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 SetValue(const Vector3i id, float value) = 0; + virtual void SetValue(const int id, float value) = 0; virtual void SetDims(const Vector3i &size) = 0; @@ -62,39 +61,73 @@ public: }; +template < class SeqCnt > +class DataSetImage : public DataSet, public ImageSpace, public ImageMap { - -class DataVectorImage : public ImageData { - typedef float ScalarT; public: + DataSetImage() : ImageMap(Vector3i::Zero()), m_Sequence() {} + DataSetImage(const Vector3i &size) : ImageMap(size), m_Sequence(new SeqCnt(size.prod())) {} - inline void * GetDataPointer(const Id_t id) const { - return m_Data->GetDataPointer(id); + void SetSize(const Vector3f v) { +// ImageSpace::SetSize( v.array() / this->GetDims().array().cast() ); } - 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()); + Vector3f GetSize() const { +// return ImageSpace::GetSize().array() * this->GetDims().array().cast(); } -// uLibRefMacro(Data,AbstractArray*) - uLibRefMacro(Data,SmartPointer) - uLibRefMacro(Scalars,ProgrammableAccessor) + bool 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 SetDims(const Vector3i &size) { m_Sequence->resize(size.prod()); } + + inline void * GetDataPointer(Id_t id) { return &m_Sequence->at(id); } + private: -// AbstractArray* m_Data; - SmartPointer m_Data; - ProgrammableAccessor m_Scalars; + SmartPointer m_Sequence; }; +//class DataVectorImage : public ImageData { +// typedef float ScalarT; +//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; +//}; + + + + + + diff --git a/src/Math/ImageMap.h b/src/Math/ImageMap.h index e6c1efe..d748e82 100644 --- a/src/Math/ImageMap.h +++ b/src/Math/ImageMap.h @@ -80,9 +80,6 @@ public: Vector3i UnMap(int index) const; - - - private: Order m_DataOrder; Vector3i m_Dims; diff --git a/src/Math/testing/ImageDataTest.cpp b/src/Math/testing/ImageDataTest.cpp index 334509b..2f1ea06 100644 --- a/src/Math/testing/ImageDataTest.cpp +++ b/src/Math/testing/ImageDataTest.cpp @@ -80,31 +80,48 @@ struct MyVoxelJitterAccess { int main() { - DataVectorCompound< MyVoxel, MyVoxelMeanAccess > data; - data.Data().push_back( MyVoxel(5,1) ); - data.Data().push_back( MyVoxel(10,2) ); - data.Data().push_back( MyVoxel(15,3) ); - data.Data().push_back( MyVoxel(2368,1) ); +// { +// DataVectorCompound< MyVoxel, MyVoxelMeanAccess > data; +// data.Data().push_back( MyVoxel(5,1) ); +// data.Data().push_back( MyVoxel(10,2) ); +// data.Data().push_back( MyVoxel(15,3) ); +// data.Data().push_back( MyVoxel(2368,1) ); - data[3].value = 123; +// data[3].value = 123; - DataVectorCompound< MyVoxel, MyVoxelValueAccess > data2 = data; +// DataVectorCompound< MyVoxel, MyVoxelValueAccess > data2 = data; - std::cout << "image data test \n"; - foreach (MyVoxel &el, data2.Data()) { - std::cout << "-> " << el.value << " - " << el.count << "\n"; +// std::cout << "image data test \n"; +// foreach (MyVoxel &el, data2.Data()) { +// std::cout << "-> " << el.value << " - " << el.count << "\n"; +// } + +// DataVectorCompound< MyVoxel, MyVoxelJitterAccess > data3 = data2; +// data3.A0().min = -1; +// data3.A0().max = 1; +// data3.AddScalarAccess("scalars",&MyVoxel::value); +// data3.AddScalarAccess("counts",&MyVoxel::count); + +// std::cout << "image data test \n"; +// for(int i=0; i " << data3.GetScalar(i) << " - " << data3.GetScalar("counts",i) << "\n"; +// } +// } + + + + DataSetImage< Vector > img( Vector3i(3,3,3) ); + img.AddScalarAccess("value",&MyVoxel::value); + img.AddScalarAccess("count",&MyVoxel::count); + + for(int x = 0; x < img.GetDims().prod(); ++x) { + img.SetActiveScalars("value"); + img.SetScalar(x,x); + img.SetScalar("count",x,2*x); + std::cout << " [" << img.GetScalar("value",x) << "," << img.GetScalar("count", x) << "]"; } - DataVectorCompound< MyVoxel, MyVoxelJitterAccess > data3 = data2; - data3.A0().min = -1; - data3.A0().max = 1; - data3.AddScalarAccess("scalars",&MyVoxel::value); - data3.AddScalarAccess("counts",&MyVoxel::count); - - std::cout << "image data test \n"; - for(int i=0; i " << data3.GetScalar(i) << " - " << data3.GetScalar("counts",i) << "\n"; - } + std::cout << "\n"; }