[uLib geometry]

- adds some to Programmable accessors
- Static interface test
- Mpl sequence
This commit is contained in:
Andrea Rigoni
2014-12-19 17:37:44 +00:00
parent d3ac2b9e86
commit 39ee7e4a94
17 changed files with 1379 additions and 384 deletions

View File

@@ -33,6 +33,8 @@
#include "Math/Dense.h"
#include "Math/StructuredData.h"
#include <Core/ClassCompound.h>
#include <Core/ProgrammableAccessor.h>
#include <vtkInformation.h>
@@ -41,61 +43,162 @@
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 void * GetDataPointer(Id_t id) const = 0;
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 ~AbstractArray() {}
};
class DataSetAttributes {
public:
template < typename GeT, typename SeT >
inline void SetScalars(GeT get, SeT set) {
m_Scalars.SetAccessFunctions(get,set);
template < typename T >
class DataAttributes {
public:
template < typename F >
void AddAttribute(const char *name, F f) {
ProgrammableAccessor<T> pa(name);
pa.SetAccessFunctions(f);
m_Accessors.push_back(pa);
if(m_Accessors.size() == 1)
SetActive(name);
}
template < typename F1, typename F2 >
void AddAttribute(const char *name, F1 f1, F2 f2) {
ProgrammableAccessor<T> pa(name);
pa.SetAccessFunctions(f1,f2);
m_Accessors.push_back(pa);
if(m_Accessors.size() == 1)
SetActive(name);
}
ProgrammableAccessor<T> * GetAttribute(const char *name) /*const*/ {
foreach (ProgrammableAccessor<T> &el, m_Accessors) {
if(el.GetName() == name)
return &el;
}
return NULL;
}
void SetActive(const char *name) {
m_Active = this->GetAttribute(name);
}
uLibGetMacro(Active,ProgrammableAccessor<T> *)
uLibRefMacro(Accessors, Vector< ProgrammableAccessor<T> >);
private:
ProgrammableAccessor<double> m_Scalars;
Vector< ProgrammableAccessor<T> > m_Accessors;
ProgrammableAccessor<T> *m_Active;
};
template < typename T >
class DataVector : public AbstractArray {
class DataSet : public AbstractArray {
public:
void * GetDataPointer(Id_t id) const { return (void *)&m_Data.at(id); }
DataSet() {}
virtual ~DataSet() {}
template < typename F >
inline void AddScalarAccess(const char *name, F f) { Scalars().AddAttribute(name,f); }
template < typename F1, typename F2 >
inline void AddScalarAccess(const char *name, F1 f1, F2 f2) { Scalars().AddAttribute(name,f1,f2); }
double GetScalar(int id) {
ProgrammableAccessor<double> *acc = Scalars().GetActive();
if (acc) return acc->Get(this->GetDataPointer(id));
}
double GetScalar(const char *name, int id) {
ProgrammableAccessor<double> *acc = Scalars().GetAttribute(name);
if (acc) return acc->Get(this->GetDataPointer(id));
}
void SetScalar(int id, double val) {
ProgrammableAccessor<double> *acc = Scalars().GetActive();
if (acc) acc->Set(this->GetDataPointer(id),val);
}
void SetScalar(const char *name, int id, double val) {
ProgrammableAccessor<double> *acc = Scalars().GetAttribute(name);
if (acc) acc->Set(this->GetDataPointer(id),val);
}
inline void SetActiveScalars(const char *name) { Scalars().SetActive(name); }
inline ProgrammableAccessor<double> * GetActiveScalars() { return Scalars().GetActive(); }
uLibRefMacro(Scalars,DataAttributes< double >);
uLibRefMacro(Vectors,DataAttributes< Vector3d >);
private:
DataAttributes< double > m_Scalars;
DataAttributes< Vector3d > m_Vectors; // da finire ... //
};
template <
typename T,
class ScalarAccess
>
class DataVectorCompound :
public DataSet,
public ClassCompound< ScalarAccess >
{
typedef DataVectorCompound<T,ScalarAccess> ThisClass;
typedef ClassCompound< ScalarAccess > Compound;
typedef T DataType;
public:
DataVectorCompound() {}
template < class Other >
DataVectorCompound(const Other &cp) :
Compound(cp),
m_Data(cp.Data())
{}
const void * GetDataPointer(Id_t id) const { return (const void *)&m_Data.at(id); }
void * GetDataPointer(Id_t id) { return (void *)&m_Data.at(id); }
inline DataType & operator [] (Id_t id) { return m_Data.operator [](id); }
inline const DataType & operator [] (Id_t id) const { return m_Data.operator [](id); }
inline void SetSize(const size_t size) { m_Data.resize(size); }
inline const size_t GetSize() const { return m_Data.size(); }
uLibRefMacro(Data,Vector<T>)
uLibRefMacro(Attributes,DataSetAttributes)
uLibRefMacro(Data,Vector<DataType>)
uLibConstRefMacro(Data,Vector<T>)
private:
DataSetAttributes m_Attributes;
Vector<T> m_Data;
Vector<DataType> m_Data;
};
//class DataSet {
//public:
// virtual Vector3d GetPoint(const Id_t) const = 0;
// virtual Vector<Id_t> GetCell(const Id_t) const = 0;
//private:
//};
} // uLib

View File

@@ -29,7 +29,7 @@
#include "Core/Macros.h"
#include "Core/Object.h"
#include "Math/Dense.h"
#include "Math/DataSet.h"
//#include "Math/DataSet.h"
//#include "boost/range.hpp"
//#include "boost/range/iterator.hpp"

View File

@@ -25,8 +25,10 @@
#include "testing-prototype.h"
#include "Math/DataSet.h"
#include "Math/ImageData.h"
#include <Core/Mpl.h>
#include <root/TRandom.h>
@@ -35,59 +37,74 @@ using namespace uLib;
namespace {
struct MyVoxel {
MyVoxel() : value(0), count(0) {}
MyVoxel(float v, int c) : value(v), count(c) {}
float value;
int count;
};
struct MyVoxelMeanAccess {
static void Set(MyVoxel &vox, double val) {
vox.value = val;
++vox.count;
}
static double Get(const MyVoxel &vox) { return vox.value/vox.count; }
};
struct VoxelMean : public MyVoxel {
VoxelMean() {}
void SetValue(const float _value) { value += _value; ++count; }
float GetValue() const { return value/count; }
struct MyVoxelValueAccess {
static void Set(MyVoxel &vox, double val) {
vox.value = val;
vox.count = 1;
}
static double Get(const MyVoxel &vox) { return vox.value; }
};
struct MyVoxelJitterAccess {
MyVoxelJitterAccess() : min(-1), max(1) { srand (static_cast <unsigned> (time(0))); }
MyVoxelJitterAccess(float min, float max) : min(min), max(max) { srand (static_cast <unsigned> (time(0))); }
float random() const {
return min + static_cast <float> (rand()) / ( static_cast <float> (RAND_MAX/(max-min)));
}
void Set(MyVoxel &vox, double val) {
vox.value = val + random();
vox.count = 1;
}
double Get(const MyVoxel &vox) const { return vox.value / vox.count + random(); }
float min, max;
};
}
int main() {
// DataVector<MyVoxel> v;
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) );
DataVectorImage img;
data[3].value = 123;
// img.Data() = v;
// img.Scalars().SetAccessFunctions(&MyVoxel::value);
// img.SetDims(Vector3i(3,3,3));
DataVectorCompound< MyVoxel, MyVoxelValueAccess > data2 = data;
// for (int x=0; x<img.GetDims().prod(); ++x){
// img.SetValue(x,x);
// std::cout << img.UnMap(x).transpose() << " -> " << img.GetValue(x) << "\n";
// }
DataVector<VoxelMean> vm;
Vector<VoxelMean> vm_2;
vm_2.resize(Vector3i(300,300,300).prod());
img.Data() = vm;
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.GetDims().prod(); ++x){
// 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";
std::cout << "image data test \n";
foreach (MyVoxel &el, data2.Data()) {
std::cout << "-> " << el.value << " - " << el.count << "\n";
}
// img.ExportToVtk("test.vtk");
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.GetSize(); ++i) {
std::cout << " -> " << data3.GetScalar(i) << " - " << data3.GetScalar("counts",i) << "\n";
}
}