[uLib Geometry]

adds ImageSpace ImageMap and ImageData
stats ProgrammableAccess to data
This commit is contained in:
Andrea Rigoni
2014-11-06 15:05:54 +00:00
parent d5531caa0c
commit 4b4e0b2959
23 changed files with 962 additions and 378 deletions

View File

@@ -43,28 +43,6 @@
namespace uLib { namespace uLib {
template< typename T, Size_t size >
class Array : public std::array<T,size> {
};
template< typename T, Size_t size >
class SmartArray : public SmartPointer< Array<T,size> > {
typedef SmartPointer< Array<T,size> > ptr;
public:
SmartArray() : ptr(new Array<T,size>()) { }
SmartArray( const SmartArray &copy) : ptr(copy) { }
virtual ~SmartArray() {}
T& operator[](unsigned int p) {
return ptr::get()->at(p);
}
};
} }

62
src/Core/Named.h Normal file
View File

@@ -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 <Core/Types.h>
#include <Core/String.h>
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

View File

@@ -4,6 +4,11 @@
#include <Core/Macros.h> #include <Core/Macros.h>
#include <Core/Types.h> #include <Core/Types.h>
#include <Core/SmartPointer.h> #include <Core/SmartPointer.h>
#include <Core/Named.h>
#include <Core/Function.h>
#include <boost/any.hpp>
namespace uLib { namespace uLib {
@@ -11,11 +16,36 @@ namespace uLib {
namespace detail { namespace detail {
template <typename R>
class ProgrammableAccessor_Base {
class any_c : public boost::any {
typedef boost::any any;
public: public:
virtual R Get(void *) const = 0;
virtual void Set(void *,const R) const = 0; any_c() : any() {}
template<typename ValueType>
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 <typename _TT>
operator _TT () const { return boost::any_cast<_TT>(*this); }
};
template <typename D>
class ProgrammableAccessor_Base {
protected:
public:
virtual D Get(void *) const = 0;
virtual void Set(void *,const D) const = 0;
template <typename T> template <typename T>
T Get(void *ob) const { T Get(void *ob) const {
@@ -24,56 +54,56 @@ public:
template <typename T> template <typename T>
void Set(void *ob, const T data) const { void Set(void *ob, const T data) const {
this->Set(ob,static_cast<R>(data)); this->Set(ob,static_cast<D>(data));
} }
virtual ~ProgrammableAccessor_Base() {} virtual ~ProgrammableAccessor_Base() {}
}; };
template<typename R, typename T> template<typename D, typename R, typename T>
class functor_by_ref : public ProgrammableAccessor_Base<R> class functor_by_ref : public ProgrammableAccessor_Base<D>
{ {
public: public:
explicit functor_by_ref(R&(T::*__pf)()) : MFRef(__pf) {} explicit functor_by_ref(R&(T::*__pf)()) : MFRef(__pf) {}
R Get(void * ptr) const D Get(void * ptr) const
{ return static_cast<R>((reinterpret_cast<T *>(ptr)->*MFRef)()); } { return static_cast<D>((reinterpret_cast<T *>(ptr)->*MFRef)()); }
void Set(void * ptr, const R data) const void Set(void * ptr, const D data) const
{ ((reinterpret_cast<T *>(ptr)->*MFRef)()) = data; } { ((reinterpret_cast<T *>(ptr)->*MFRef)()) = static_cast<const R>(data); }
private: private:
R&(T::*MFRef)(); R&(T::*MFRef)();
}; };
template<typename R, typename T> template<typename D, typename R, typename T>
class functor_by_mfptr_cc : public ProgrammableAccessor_Base<R> class functor_by_mfptr_cc : public ProgrammableAccessor_Base<D>
{ {
typedef R(T::*GetterType)() const; typedef R(T::*GetterType)() const;
typedef void(T::*SetterType)(const R); typedef void(T::*SetterType)(const R);
public: public:
explicit functor_by_mfptr_cc( GetterType _get, SetterType _set ) : explicit functor_by_mfptr_cc( GetterType _get, SetterType _set ) :
GetPtr(_get), SetPtr(_set) {} GetPtr(_get), SetPtr(_set) {}
R Get(void * ptr) const D Get(void * ptr) const
{ return static_cast<R>((reinterpret_cast<T *>(ptr)->*GetPtr)()); } { return static_cast<D>((reinterpret_cast<T *>(ptr)->*GetPtr)()); }
void Set(void * ptr, const R data) const void Set(void * ptr, const D data) const
{ if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(data); } { if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(static_cast<const R>(data)); }
private: private:
GetterType GetPtr; GetterType GetPtr;
SetterType SetPtr; SetterType SetPtr;
}; };
template<typename R, typename T> template<typename D, typename R, typename T>
class functor_by_mfptr_vv : public ProgrammableAccessor_Base<R> class functor_by_mfptr_vv : public ProgrammableAccessor_Base<D>
{ {
typedef R(T::*GetterType)(); typedef R(T::*GetterType)();
typedef void(T::*SetterType)(R); typedef void(T::*SetterType)(R);
public: public:
explicit functor_by_mfptr_vv( GetterType _get, SetterType _set ) : explicit functor_by_mfptr_vv( GetterType _get, SetterType _set ) :
GetPtr(_get), SetPtr(_set) {} GetPtr(_get), SetPtr(_set) {}
R Get(void * ptr) const D Get(void * ptr) const
{ return static_cast<R>((reinterpret_cast<T *>(ptr)->*GetPtr)()); } { return static_cast<D>((reinterpret_cast<T *>(ptr)->*GetPtr)()); }
void Set(void * ptr, const R data) const void Set(void * ptr, const D data) const
{ if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(data); } { if (SetPtr) (reinterpret_cast<T *>(ptr)->*SetPtr)(static_cast<const R>(data)); }
private: private:
GetterType GetPtr; GetterType GetPtr;
@@ -81,15 +111,15 @@ private:
}; };
template<typename R, typename T> template<typename D, typename R, typename T>
class functor_by_member : public ProgrammableAccessor_Base<R> class functor_by_member : public ProgrammableAccessor_Base<D>
{ {
public: public:
explicit functor_by_member(R T::*__pf) : MDPtr(__pf) {} explicit functor_by_member(R T::*__pf) : MDPtr(__pf) {}
R Get(void * ptr) const D Get(void * ptr) const
{ return static_cast<R>(reinterpret_cast<T *>(ptr)->*MDPtr); } { return static_cast<D>(reinterpret_cast<T *>(ptr)->*MDPtr); }
void Set(void * ptr, const R data) const void Set(void * ptr, const D data) const
{ (reinterpret_cast<T *>(ptr)->*MDPtr) = data; } { (reinterpret_cast<T *>(ptr)->*MDPtr) = static_cast<const D>(data); }
private: private:
R T::*MDPtr; R T::*MDPtr;
}; };
@@ -103,36 +133,86 @@ private:
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename R>
class ProgrammableAccessor {
template <typename D >
class ProgrammableAccessor : public Named {
public: public:
template <typename T>
ProgrammableAccessor(R(T::*_pg)()const, void(T::*_ps)(const R) = NULL) : struct Wrapper {
m_base(new detail::functor_by_mfptr_cc<R,T>(_pg,_ps)) {} Wrapper(const ProgrammableAccessor<D> *ac, void *ob) :
m_access(ac), m_object(ob)
{ assert(ob != NULL); }
template <typename T>
inline T Get() const { return m_access->Get<T>(m_object); }
template <typename T>
inline void Set(const T data) const { return m_access->Set<T>(m_object,data); }
void *m_object;
const ProgrammableAccessor<D> *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 <typename R, typename T>
inline void SetAccessFunctions(R(T::*_pg)()const, void(T::*_ps)(const R) = NULL)
{
m_base = SmartPointer< detail::ProgrammableAccessor_Base<D> >(new detail::functor_by_mfptr_cc<D,R,T>(_pg,_ps));
this->SetName(_pg); // << FIX
}
template <typename R, typename T>
inline void SetAccessFunctions(R(T::*_pg)(), void(T::*_ps)(R) = NULL) {
m_base = SmartPointer< detail::ProgrammableAccessor_Base<D> >(new detail::functor_by_mfptr_vv<D,R,T>(_pg,_ps));
this->SetName(_pg); // << FIX
}
template <typename R, typename T>
inline void SetAccessFunctions(R&(T::*_pf)()) {
m_base = SmartPointer< detail::ProgrammableAccessor_Base<D> >(new detail::functor_by_ref<D,R,T>(_pf));
this->SetName(_pf);
}
template <typename R, typename T>
inline void SetAccessFunctions(R T::*_pf) {
m_base = SmartPointer< detail::ProgrammableAccessor_Base<D> >(new detail::functor_by_member<D,R,T>(_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 <typename T> template <typename T>
ProgrammableAccessor(R(T::*_pg)(), void(T::*_ps)(R) = NULL) : inline T Get(void *ob) const { return m_base->Get<T>(ob); }
m_base(new detail::functor_by_mfptr_vv<R,T>(_pg,_ps)) {}
template <typename T> template <typename T>
ProgrammableAccessor(R&(T::*_pf)()) : inline void Set(void *ob, const T data) const { return m_base->Set<T>(ob,data); }
m_base(new detail::functor_by_ref<R,T>(_pf)) {}
template <typename T>
ProgrammableAccessor(R T::*_pf) :
m_base(new detail::functor_by_member<R,T>(_pf)) {}
template <typename OtherR>
inline OtherR Get(void *ob) const { return m_base->Get<OtherR>(ob); }
template <typename OtherR>
inline void Set(void *ob, const OtherR data) const { return m_base->Set<OtherR>(ob,data); }
private: private:
SmartPointer< detail::ProgrammableAccessor_Base<R> > m_base; SmartPointer< detail::ProgrammableAccessor_Base<D> > m_base;
}; };
typedef ProgrammableAccessor<detail::any_c> ProgrammableAccessorAny;
} // uLib } // uLib

View File

@@ -9,6 +9,8 @@
#include <Core/Vector.h> #include <Core/Vector.h>
#include <Core/ProgrammableAccessor.h> #include <Core/ProgrammableAccessor.h>
#include <iterator>
namespace uLib { namespace uLib {
class TestVoxel { class TestVoxel {
@@ -22,6 +24,64 @@ public:
//template<typename _T>
//class PointerIterator :
// public std::iterator<std::random_access_iterator_tag, _T>
//{
//protected:
// _T* m_pData;
//public:
// typedef std::random_access_iterator_tag iterator_category;
// typedef typename std::iterator<std::random_access_iterator_tag, _T>::value_type value_type;
// typedef typename std::iterator<std::random_access_iterator_tag, _T>::difference_type difference_type;
// typedef typename std::iterator<std::random_access_iterator_tag, _T>::reference reference;
// typedef typename std::iterator<std::random_access_iterator_tag, _T>::pointer pointer;
// PointerIterator() : m_pData(NULL) {}
// template<typename T2>
// PointerIterator(const PointerIterator<T2>& r) : m_pData(&(*r)) {}
// PointerIterator(pointer pData) : m_pData(pData) {}
// template<typename T2>
// PointerIterator& operator=(const PointerIterator<T2>& 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 <typename T>
// T Get(Id_t id) { return m_view.Get<T>(this->GetPointData(id)); }
//private:
// ProgrammableAccessorAny m_view;
//};
} // uLib } // uLib
using namespace uLib; using namespace uLib;
@@ -42,13 +102,18 @@ int main() {
v[5].m_data = 5552368.0; v[5].m_data = 5552368.0;
ProgrammableAccessor<float> f1(&TestVoxel::m_data); ProgrammableAccessor<int> f1(&TestVoxel::m_data);
ProgrammableAccessor<float> f2(&TestVoxel::GetRef); ProgrammableAccessor<char> f2(&TestVoxel::ConstGet);
ProgrammableAccessorAny f3(&TestVoxel::m_data);
ProgrammableAccessor<detail::any_c> f4(&TestVoxel::m_data);
int i=0; int i=0;
foreach (TestVoxel &el,v) { foreach (TestVoxel &el,v) {
f1.Set(&el,i++); f1.Set(&el,i++);
std::cout << " -> " << f2.Get<double>(&el) << "\n"; std::cout << " -> " << f2.Get(&el) << " - " << f3.Get<float>(&el) << f4.Get<int>(&el) << f1(&el).Get<float>() << "\n";
} }

View File

@@ -7,6 +7,7 @@ set(HEADERS
Transform.h Transform.h
StructuredData.h StructuredData.h
# StructuredGrid.h # StructuredGrid.h
ImageSpace.h
ImageMap.h ImageMap.h
ImageData.h ImageData.h
VoxImage.h VoxImage.h
@@ -32,6 +33,7 @@ set(SOURCES
VoxRaytracer.cpp VoxRaytracer.cpp
StructuredData.cpp StructuredData.cpp
# StructuredGrid.cpp # StructuredGrid.cpp
ImageSpace.cpp
ImageMap.cpp ImageMap.cpp
ImageData.cpp ImageData.cpp
VoxImage.cpp VoxImage.cpp

View File

@@ -35,37 +35,61 @@
#include "Math/StructuredData.h" #include "Math/StructuredData.h"
#include <Core/ProgrammableAccessor.h> #include <Core/ProgrammableAccessor.h>
#include <vtkInformation.h>
namespace uLib { namespace uLib {
class DataSet_Base {
public:
DataSet_Base() {}
virtual ~DataSet_Base() {} class AbstractArray {
virtual const void * GetPtr(Id_t id) const = 0; public:
virtual void * GetPtr(Id_t id) = 0; virtual void * GetDataPointer(Id_t id) const = 0;
virtual size_t Size() 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: private:
ProgrammableAccessor<double> m_Scalars;
}; };
template <typename T>
class VectorData : public DataSet_Base, public Vector<T> { template < typename T >
class DataVector : public AbstractArray {
public: 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: uLibRefMacro(Data,Vector<T>)
const void *GetPtr(Id_t id) const { return &this->at(id); } uLibRefMacro(Attributes,DataSetAttributes)
void * GetPtr(Id_t id) { return &this->at(id); } private:
DataSetAttributes m_Attributes;
Vector<T> m_Data;
}; };
//class DataSet {
//public:
// virtual Vector3d GetPoint(const Id_t) const = 0;
// virtual Vector<Id_t> GetCell(const Id_t) const = 0;
//private:
//};

View File

@@ -53,7 +53,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <Eigen/Core>
#include <Eigen/Dense> #include <Eigen/Dense>
#include "Core/Types.h" #include "Core/Types.h"
#include "Core/Serializable.h" #include "Core/Serializable.h"
@@ -106,6 +108,61 @@ EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
#undef EIGEN_MAKE_TYPEDEFS #undef EIGEN_MAKE_TYPEDEFS
//#define EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
///** \ingroup arraytypedefs */ \
//typedef Eigen::Array<Type, Size, Size> Array##SizeSuffix##SizeSuffix##TypeSuffix; \
///** \ingroup arraytypedefs */ \
//typedef Eigen::Array<Type, Size, 1> Array##SizeSuffix##TypeSuffix;
//#define EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
///** \ingroup arraytypedefs */ \
//typedef Eigen::Array<Type, Size, Dynamic> Array##Size##X##TypeSuffix; \
///** \ingroup arraytypedefs */ \
//typedef Eigen::Array<Type, Dynamic, Size> 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<float>, cf)
//EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex<double>, 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 } // uLib

View File

@@ -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<float>() );
}
Vector3f ImageData::GetSize() const
{
return ImageSpace::GetSize().array() * this->GetDims().array().cast<float>();
}
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,
"<?xml version=\"1.0\"?>\n"
"<VTKFile type=\"ImageData\" version=\"0.1\" "
"byte_order=\"LittleEndian\">\n"
"<ImageData WholeExtent=\"0 %d 0 %d 0 %d\" "
"Origin=\"%f %f %f\" Spacing=\"%f %f %f\">\n"
"<Piece Extent=\"0 %d 0 %d 0 %d\">\n"
"<CellData Scalars=\"lambda\">\n"
"<DataArray format=\"ascii\" Name=\"lambda\" type=\"Float32\" >\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 </DataArray>\n </CellData>\n");
fprintf(vtk_file,
"</Piece>\n </ImageData>\n </VTKFile>\n");
fclose(vtk_file);
printf("%s vtk file saved\n",file);
}

View File

@@ -37,18 +37,64 @@ namespace uLib {
class ImageData : public ImageSpace, public ImageMap { 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: public:
inline void * GetDataPointer(const Id_t id) const {
return m_Data->GetDataPointer(id);
}
float GetValue(const Vector3i id) const { return m_Scalars.Get<float>(GetDataPointer(Map(id))); }
float GetValue(const int id) const { return m_Scalars.Get<float>(GetDataPointer(id)); }
void SetValue(const Vector3i id, float value) { m_Scalars.Set<float>(GetDataPointer(Map(id)),value); }
void SetValue(const int id, float value) { m_Scalars.Set<float>(GetDataPointer(id),value); }
void SetDims(const Vector3i &size) {
ImageMap::SetDims(size);
m_Data->SetSize(size.prod());
}
uLibRefMacro(Data,SmartPointer<AbstractArray>)
uLibRefMacro(Scalars,ProgrammableAccessor<double>)
private:
SmartPointer<AbstractArray> m_Data;
ProgrammableAccessor<double> m_Scalars;
}; };
} // uLib } // uLib
#endif // IMAGEDATA_H #endif // IMAGEDATA_H

View File

@@ -31,8 +31,19 @@
#include "Math/Dense.h" #include "Math/Dense.h"
#include "Math/DataSet.h" #include "Math/DataSet.h"
//#include "boost/range.hpp"
//#include "boost/range/iterator.hpp"
namespace uLib { namespace uLib {
class ImageMap { class ImageMap {
public: public:
typedef enum { typedef enum {
@@ -69,6 +80,9 @@ public:
Vector3i UnMap(int index) const; Vector3i UnMap(int index) const;
private: private:
Order m_DataOrder; Order m_DataOrder;
Vector3i m_Dims; Vector3i m_Dims;

View File

@@ -28,19 +28,10 @@
namespace uLib { namespace uLib {
ImageSpace::ImageSpace(const Vector3i size) : //ImageSpace::ImageSpace(const Vector3i size) :
StructuredData(size) // StructuredData(size)
{} //{}
//void ImageSpace::SetSize(const Vector3f v)
//{
// ContainerBox::SetSize( v.array() / this->GetDims().array().cast<float>() );
//}
//Vector3f ImageSpace::GetSize() const
//{
// return ContainerBox::GetSize().array() * this->GetDims().array().cast<float>();
//}
void ImageSpace::SetSpacing(const Vector3f spacing) void ImageSpace::SetSpacing(const Vector3f spacing)
{ {
@@ -52,18 +43,6 @@ Vector3f ImageSpace::GetSpacing() const
return ContainerBox::GetSize(); 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 ImageSpace::Find(const Vector4f pt) const
{ {
Vector3i out; Vector3i out;

View File

@@ -35,25 +35,18 @@ namespace uLib {
class ImageSpace : public ContainerBox { class ImageSpace : public ContainerBox {
typedef ContainerBox BaseClass; typedef ContainerBox BaseClass;
public: public:
ImageSpace(const Vector3i size);
using ContainerBox::SetOrigin; using ContainerBox::SetOrigin;
// void SetSize(const Vector3f v);
// Vector3f GetSize() const;
void SetSpacing(const Vector3f spacing); void SetSpacing(const Vector3f spacing);
Vector3f GetSpacing() const; Vector3f GetSpacing() const;
// bool IsInsideBounds(const Vector4f pt) const;
Vector3i Find(const Vector4f pt) const; Vector3i Find(const Vector4f pt) const;
void PrintSelf(std::ostream &o); void PrintSelf(std::ostream &o);
private: protected:
using ContainerBox::GetSize; using ContainerBox::GetSize;
using ContainerBox::SetSize; using ContainerBox::SetSize;
}; };

View File

@@ -50,6 +50,10 @@ public:
Vector & direction() { return this->m_Data[1]; } Vector & direction() { return this->m_Data[1]; }
const Vector & origin() const { return this->m_Data[0]; } const Vector & origin() const { return this->m_Data[0]; }
const Vector & direction() const { return this->m_Data[1]; } 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<float,2> Line2f; typedef Line<float,2> Line2f;

View File

@@ -27,54 +27,54 @@
#include "StructuredData.h" #include "StructuredData.h"
using namespace uLib; //using namespace uLib;
StructuredData::StructuredData(const Vector3i &size) : //StructuredData::StructuredData(const Vector3i &size) :
m_Dims(size) // m_Dims(size)
{ //{
SetDataOrder(); // SetDataOrder();
} //}
void StructuredData::SetDims(const Vector3i &size) //void StructuredData::SetDims(const Vector3i &size)
{ //{
this->m_Dims = size; // this->m_Dims = size;
SetDataOrder(); // SetDataOrder();
} //}
void StructuredData::SetDataOrder(StructuredData::Order order) //void StructuredData::SetDataOrder(StructuredData::Order order)
{ //{
int i = order & 0x3; // int i = order & 0x3;
int j = (order >> 2) & 0x3; // int j = (order >> 2) & 0x3;
int k = (order >> 4) & 0x3; // int k = (order >> 4) & 0x3;
this->m_Increments[i] = 1; // this->m_Increments[i] = 1;
this->m_Increments[j] = m_Dims[i]; // this->m_Increments[j] = m_Dims[i];
this->m_Increments[k] = m_Dims[i] * m_Dims[j]; // this->m_Increments[k] = m_Dims[i] * m_Dims[j];
this->m_DataOrder = order; // this->m_DataOrder = order;
} //}
bool StructuredData::IsInsideGrid(const Vector3i &v) const //bool StructuredData::IsInsideGrid(const Vector3i &v) const
{ //{
int vok = 1; // int vok = 1;
vok *= (v(0) >= 0 && v(0) < m_Dims[0]); // vok *= (v(0) >= 0 && v(0) < m_Dims[0]);
vok *= (v(1) >= 0 && v(1) < m_Dims[1]); // vok *= (v(1) >= 0 && v(1) < m_Dims[1]);
vok *= (v(2) >= 0 && v(2) < m_Dims[2]); // vok *= (v(2) >= 0 && v(2) < m_Dims[2]);
return vok; // return vok;
} //}
Vector3i StructuredData::UnMap(int index) const //Vector3i StructuredData::UnMap(int index) const
{ //{
Vector3i v( 0,0,0 ); // Vector3i v( 0,0,0 );
Vector3i iv = m_Increments; // Vector3i iv = m_Increments;
int id = 0; // int id = 0;
for(int k=0; k<3; ++k) { // for(int k=0; k<3; ++k) {
int inc = iv.maxCoeff(&id); // int inc = iv.maxCoeff(&id);
v(id) = index / inc; // v(id) = index / inc;
index -= v(id) * inc; // index -= v(id) * inc;
iv(id) = 0; // iv(id) = 0;
} // }
return v; // return v;
} //}

View File

@@ -28,74 +28,74 @@
#ifndef STRUCTUREDDATA_H #ifndef STRUCTUREDDATA_H
#define STRUCTUREDDATA_H #define STRUCTUREDDATA_H
#include "Core/Macros.h" //#include "Core/Macros.h"
#include "Core/Object.h" //#include "Core/Object.h"
#include "Math/Dense.h" //#include "Math/Dense.h"
#include "Math/DataSet.h" //#include "Math/DataSet.h"
namespace uLib { //namespace uLib {
class StructuredData { //class StructuredData {
public: //public:
enum _Order // enum _Order
{ // {
CustomOrder = 0, // CustomOrder = 0,
XYZ = 0 | 1 << 2 | 2 << 4, // XYZ = 0 | 1 << 2 | 2 << 4,
XZY = 0 | 2 << 2 | 1 << 4, // XZY = 0 | 2 << 2 | 1 << 4,
YXZ = 1 | 0 << 2 | 2 << 4, // YXZ = 1 | 0 << 2 | 2 << 4,
YZX = 2 | 0 << 2 | 1 << 4, // YZX = 2 | 0 << 2 | 1 << 4,
ZXY = 1 | 2 << 2 | 0 << 4, // ZXY = 1 | 2 << 2 | 0 << 4,
ZYX = 2 | 1 << 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 &copy) : // StructuredData(const StructuredData &copy) :
m_DataOrder(copy.m_DataOrder), // m_DataOrder(copy.m_DataOrder),
m_Dims(copy.m_Dims), // m_Dims(copy.m_Dims),
m_Increments(copy.m_Increments) // 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: //private:
Order m_DataOrder; // Order m_DataOrder;
Vector3i m_Dims; // Vector3i m_Dims;
Vector3i m_Increments; //TODO: make this line matrix // // Vector3i m_Increments; //TODO: make this line matrix //
}; //};
// --- INLINES -------------------------------------------------------------- // //// --- INLINES -------------------------------------------------------------- //
inline int StructuredData::Map(Vector3i index) const //inline int StructuredData::Map(Vector3i index) const
{ //{
return (m_Increments.transpose() * index); // return (m_Increments.transpose() * index);
} //}
} //}
#endif // STRUCTUREDDATA_H #endif // STRUCTUREDDATA_H

View File

@@ -27,163 +27,162 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "VoxImage.h" #include "VoxImage.h"
namespace uLib { namespace uLib {
void Abstract::VoxImage::ExportToVtk (const char *file, bool density_type) //void Abstract::VoxImage::ExportToVtk (const char *file, bool density_type)
{ //{
FILE * vtk_file = fopen(file,"wb"); // FILE * vtk_file = fopen(file,"wb");
assert(vtk_file); // assert(vtk_file);
Abstract::VoxImage *voxels = this; // ImageData *voxels = this;
float norm; // float norm;
if (density_type) { // if (density_type) {
norm = 40000; // norm = 40000;
} else norm = 1.E6; // } else norm = 1.E6;
int nx = voxels->GetDims()(0); // int nx = voxels->GetDims()(0);
int ny = voxels->GetDims()(1); // int ny = voxels->GetDims()(1);
int nz = voxels->GetDims()(2); // int nz = voxels->GetDims()(2);
fprintf(vtk_file, // fprintf(vtk_file,
"# vtk DataFile Version 2.0\n" // "# vtk DataFile Version 2.0\n"
"Image Builder vtk output\n" // "Image Builder vtk output\n"
"ASCII\n" // "ASCII\n"
"DATASET STRUCTURED_POINTS\n" // "DATASET STRUCTURED_POINTS\n"
"DIMENSIONS %d %d %d\n" // "DIMENSIONS %d %d %d\n"
"SPACING %f %f %f\n" // "SPACING %f %f %f\n"
"ORIGIN %f %f %f\n" // "ORIGIN %f %f %f\n"
"POINT_DATA %d\n" // "POINT_DATA %d\n"
"SCALARS volume_scalars float 1\n" // "SCALARS volume_scalars float 1\n"
"LOOKUP_TABLE default\n", // "LOOKUP_TABLE default\n",
nx , ny , nz , // nx , ny , nz ,
voxels->GetSpacing()(0), // voxels->GetSpacing()(0),
voxels->GetSpacing()(1), // voxels->GetSpacing()(1),
voxels->GetSpacing()(2), // voxels->GetSpacing()(2),
voxels->GetPosition()(0), // FIX FOR ORIGIN // // voxels->GetPosition()(0), // FIX FOR ORIGIN //
voxels->GetPosition()(1), // voxels->GetPosition()(1),
voxels->GetPosition()(2), // voxels->GetPosition()(2),
nx * ny * nz // nx * ny * nz
); // );
Vector3i index(0,0,0); // Vector3i index(0,0,0);
for (int zv = 0; zv < nz; ++zv) { // for (int zv = 0; zv < nz; ++zv) {
for (int yv = 0; yv < ny; ++yv) { // for (int yv = 0; yv < ny; ++yv) {
for (int xv = 0; xv < nx; ++xv) { // for (int xv = 0; xv < nx; ++xv) {
index << xv,yv,zv; // index << xv,yv,zv;
// write voxel density in mrad^2/cm // // // write voxel density in mrad^2/cm //
float density = fabs(voxels->GetValue(index)) * norm; // float density = fabs(voxels->GetValue(index)) * norm;
fprintf(vtk_file, "%f ", density); // fprintf(vtk_file, "%f ", density);
} // }
} // }
} // }
fclose(vtk_file); // fclose(vtk_file);
printf("%s vtk file saved\n",file); // printf("%s vtk file saved\n",file);
} //}
int Abstract::VoxImage::ImportFromVtk(const char *file) //int Abstract::VoxImage::ImportFromVtk(const char *file)
{ //{
FILE * vtk_file = fopen(file, "r"); // FILE * vtk_file = fopen(file, "r");
if (!vtk_file) return false; // if (!vtk_file) return false;
char word[20]; // char word[20];
int dx, dy, dz, n_tot; // int dx, dy, dz, n_tot;
float sx, sy, sz, ox, oy, oz; // float sx, sy, sz, ox, oy, oz;
do { // do {
fscanf(vtk_file, "%s", word); // fscanf(vtk_file, "%s", word);
} while (strcmp(word, "DIMENSIONS")); // } while (strcmp(word, "DIMENSIONS"));
fscanf(vtk_file, "%d %d %d", &dx, &dy, &dz); // fscanf(vtk_file, "%d %d %d", &dx, &dy, &dz);
do { // do {
fscanf(vtk_file, "%s", word); // fscanf(vtk_file, "%s", word);
} while (strcmp(word, "SPACING")); // } while (strcmp(word, "SPACING"));
fscanf(vtk_file, "%f %f %f", &sx, &sy, &sz); // fscanf(vtk_file, "%f %f %f", &sx, &sy, &sz);
do { // do {
fscanf(vtk_file, "%s", word); // fscanf(vtk_file, "%s", word);
} while (strcmp(word, "ORIGIN")); // } while (strcmp(word, "ORIGIN"));
fscanf(vtk_file, "%f %f %f", &ox, &oy, &oz); // fscanf(vtk_file, "%f %f %f", &ox, &oy, &oz);
do { // do {
fscanf(vtk_file, "%s", word); // fscanf(vtk_file, "%s", word);
} while (strcmp(word, "POINT_DATA")); // } while (strcmp(word, "POINT_DATA"));
fscanf(vtk_file, "%d", &n_tot); // fscanf(vtk_file, "%d", &n_tot);
do { // do {
fscanf(vtk_file, "%s", word); // fscanf(vtk_file, "%s", word);
} while (strcmp(word, "default")); // } while (strcmp(word, "default"));
this->SetDims(Vector3i(dx,dy,dz)); // this->SetDims(Vector3i(dx,dy,dz));
this->SetSpacing(Vector3f(sx,sy,sz)); // this->SetSpacing(Vector3f(sx,sy,sz));
this->SetPosition(Vector3f(ox,oy,oz)); // this->SetPosition(Vector3f(ox,oy,oz));
for (int k = 0; k < dz; ++k) { // for (int k = 0; k < dz; ++k) {
for (int j = 0; j < dy; ++j) { // for (int j = 0; j < dy; ++j) {
for (int i = 0; i < dx; ++i) { // for (int i = 0; i < dx; ++i) {
Vector3i idx(i, j, k); // Vector3i idx(i, j, k);
float tmp_val; // float tmp_val;
fscanf(vtk_file, "%f", &tmp_val); // fscanf(vtk_file, "%f", &tmp_val);
this->SetValue(idx,fabs(tmp_val)*1E-6); // this->SetValue(idx,fabs(tmp_val)*1E-6);
} // }
} // }
} // }
fclose(vtk_file); // fclose(vtk_file);
return true; // return true;
} //}
void Abstract::VoxImage::ExportToVtkXml(const char *file, bool density_type) //void Abstract::VoxImage::ExportToVtkXml(const char *file, bool density_type)
{ //{
// Not implemented yet // // // Not implemented yet //
FILE * vtk_file = fopen(file,"wb"); // FILE * vtk_file = fopen(file,"wb");
assert(vtk_file); // assert(vtk_file);
Abstract::VoxImage *voxels = this; // ImageData *voxels = this;
float norm; // float norm;
if (density_type) { // if (density_type) {
norm = 40000; // norm = 40000;
} else norm = 1.E6; // } else norm = 1.E6;
int nx = voxels->GetDims()(0); // int nx = voxels->GetDims()(0);
int ny = voxels->GetDims()(1); // int ny = voxels->GetDims()(1);
int nz = voxels->GetDims()(2); // int nz = voxels->GetDims()(2);
fprintf(vtk_file, // fprintf(vtk_file,
"<?xml version=\"1.0\"?>\n" // "<?xml version=\"1.0\"?>\n"
"<VTKFile type=\"ImageData\" version=\"0.1\" " // "<VTKFile type=\"ImageData\" version=\"0.1\" "
"byte_order=\"LittleEndian\">\n" // "byte_order=\"LittleEndian\">\n"
"<ImageData WholeExtent=\"0 %d 0 %d 0 %d\" " // "<ImageData WholeExtent=\"0 %d 0 %d 0 %d\" "
"Origin=\"%f %f %f\" Spacing=\"%f %f %f\">\n" // "Origin=\"%f %f %f\" Spacing=\"%f %f %f\">\n"
"<Piece Extent=\"0 %d 0 %d 0 %d\">\n" // "<Piece Extent=\"0 %d 0 %d 0 %d\">\n"
"<CellData Scalars=\"lambda\">\n" // "<CellData Scalars=\"lambda\">\n"
"<DataArray format=\"ascii\" Name=\"lambda\" type=\"Float32\" >\n", // "<DataArray format=\"ascii\" Name=\"lambda\" type=\"Float32\" >\n",
nx, ny, nz, // nx, ny, nz,
voxels->GetPosition()(0), // FIX FOR ORIGIN // // voxels->GetPosition()(0), // FIX FOR ORIGIN //
voxels->GetPosition()(1), // voxels->GetPosition()(1),
voxels->GetPosition()(2), // voxels->GetPosition()(2),
voxels->GetSpacing()(0), // voxels->GetSpacing()(0),
voxels->GetSpacing()(1), // voxels->GetSpacing()(1),
voxels->GetSpacing()(2), // voxels->GetSpacing()(2),
nx, ny, nz // nx, ny, nz
); // );
Vector3i index(0,0,0); // Vector3i index(0,0,0);
for (int zv = 0; zv < nz; ++zv) { // for (int zv = 0; zv < nz; ++zv) {
for (int yv = 0; yv < ny; ++yv) { // for (int yv = 0; yv < ny; ++yv) {
for (int xv = 0; xv < nx; ++xv) { // for (int xv = 0; xv < nx; ++xv) {
index << xv,yv,zv; // index << xv,yv,zv;
// write voxel density in mrad^2/cm // // // write voxel density in mrad^2/cm //
float density = fabs(voxels->GetValue(index)) * norm; // float density = fabs(voxels->GetValue(index)) * norm;
fprintf(vtk_file, "%f ", density); // fprintf(vtk_file, "%f ", density);
} // }
} // }
} // }
fprintf(vtk_file, // fprintf(vtk_file,
"\n </DataArray>\n </CellData>\n"); // "\n </DataArray>\n </CellData>\n");
fprintf(vtk_file, // fprintf(vtk_file,
"</Piece>\n </ImageData>\n </VTKFile>\n"); // "</Piece>\n </ImageData>\n </VTKFile>\n");
fclose(vtk_file); // fclose(vtk_file);
printf("%s vtk file saved\n",file); // printf("%s vtk file saved\n",file);
} //}

View File

@@ -45,28 +45,28 @@ namespace uLib {
// ABSTRACT VOX IMAGE ////////////////////////////////////////////////////// // ABSTRACT VOX IMAGE //////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
namespace Abstract { //namespace Abstract {
class VoxImage : public uLib::ImageData { //class VoxImage : public uLib::ImageData {
public: //public:
typedef uLib::ImageData BaseClass; // typedef uLib::ImageData BaseClass;
virtual float GetValue(const Vector3i id) const = 0; // virtual float GetValue(const Vector3i id) const = 0;
virtual float GetValue(const int id) const = 0; // virtual float GetValue(const int id) const = 0;
virtual void SetValue(const Vector3i id, float value) = 0; // virtual void SetValue(const Vector3i id, float value) = 0;
virtual void SetValue(const int 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 ExportToVtk(const char *file, bool density_type = 0);
void ExportToVtkXml(const char *file, bool density_type = 0); // void ExportToVtkXml(const char *file, bool density_type = 0);
int ImportFromVtk(const char *file); // int ImportFromVtk(const char *file);
protected: //protected:
virtual ~VoxImage() {} // virtual ~VoxImage() {}
VoxImage(const Vector3i &size) : BaseClass(size) {} // VoxImage(const Vector3i &size) : BaseClass(size) {}
}; //};
} //}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// VOXEL //////////////////////////////////////////////////////////////////// // VOXEL ////////////////////////////////////////////////////////////////////
@@ -92,9 +92,9 @@ std::basic_ostream<T> & operator << (std::basic_ostream<T> &o, const Voxel &v) {
template< typename T > template< typename T >
class VoxImage : public Abstract::VoxImage { class VoxImage : public ImageData {
public: public:
typedef Abstract::VoxImage BaseClass; typedef ImageData BaseClass;
VoxImage(); VoxImage();
@@ -120,14 +120,16 @@ public:
inline T& operator[](const Vector3i &id) { return m_Data[Map(id)]; } inline T& operator[](const Vector3i &id) { return m_Data[Map(id)]; }
// this implements Abstract interface // // this implements Abstract interface //
inline Scalarf GetValue(const Vector3i id) const { return this->At(id)(); } inline void * GetDataPointer(const Id_t id) const { return (void *)&this->At(id); }
inline Scalarf GetValue(const int id) const { return this->At(id)(); } inline Scalarf GetValue(const Vector3i id) const { return this->At(id).Value; }
inline void SetValue(const Vector3i id, Scalarf value) { this->operator [](id)() = value; } inline Scalarf GetValue(const int id) const { return this->At(id).Value; }
inline void SetValue(const int id, float value) { this->operator [](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) { inline void SetDims(const Vector3i &size) {
this->m_Data.resize(size.prod()); this->m_Data.resize(size.prod());
StructuredData::SetDims(size); ImageMap::SetDims(size);
} }
inline VoxImage<T> clipImage(const Vector3i begin, const Vector3i end) const; inline VoxImage<T> clipImage(const Vector3i begin, const Vector3i end) const;
@@ -213,13 +215,11 @@ template<typename T>
VoxImage<T>::VoxImage() : VoxImage<T>::VoxImage() :
m_Data(0), m_Data(0),
BaseClass(Vector3i(0,0,0)) {} BaseClass(Vector3i(0,0,0)) {}
//{ Interface::IsA <T,Interface::Voxel>(); /* structural check for T */ }
template<typename T> template<typename T>
VoxImage<T>::VoxImage(const Vector3i &size) : VoxImage<T>::VoxImage(const Vector3i &size) :
m_Data(size.prod()), m_Data(size.prod()),
BaseClass(size) {} BaseClass(size) {}
//{ Interface::IsA <T,Interface::Voxel>(); /* structural check for T */ }
template <typename T> template <typename T>

View File

@@ -55,7 +55,7 @@ class VoxImageFilter {
public: public:
virtual void Run() = 0; virtual void Run() = 0;
virtual void SetImage(Abstract::VoxImage *image) = 0; virtual void SetImage(ImageData *image) = 0;
protected: protected:
virtual ~VoxImageFilter() {} virtual ~VoxImageFilter() {}
@@ -88,7 +88,7 @@ public:
uLibGetMacro(Image,VoxImage<VoxelT> *) uLibGetMacro(Image,VoxImage<VoxelT> *)
void SetImage(Abstract::VoxImage *image); void SetImage(ImageData *image);
protected: protected:

View File

@@ -39,8 +39,8 @@ namespace uLib {
// KERNEL ////////////////////////////////////////////////////////////////////// // KERNEL //////////////////////////////////////////////////////////////////////
template < typename T > template < typename T >
class Kernel : public StructuredData { class Kernel : public ImageMap {
typedef StructuredData BaseClass; typedef ImageMap BaseClass;
public: public:
Kernel(const Vector3i &size); Kernel(const Vector3i &size);
@@ -63,7 +63,7 @@ Kernel<T>::Kernel(const Vector3i &size) :
BaseClass(size), BaseClass(size),
m_Data(size.prod()) m_Data(size.prod())
{ {
Interface::IsA<T,Interface::Voxel>(); // Interface::IsA<T,Interface::Voxel>();
} }
template < typename T > template < typename T >
@@ -166,7 +166,7 @@ _TPL_
void VoxImageFilter<_TPLT_>::SetKernelNumericXZY(const Vector<float> &numeric) void VoxImageFilter<_TPLT_>::SetKernelNumericXZY(const Vector<float> &numeric)
{ {
// set data order // // set data order //
StructuredData::Order order = m_KernelData.GetDataOrder(); ImageMap::Order order = m_KernelData.GetDataOrder();
//m_KernelData.SetDataOrder(StructuredData::XZY); //m_KernelData.SetDataOrder(StructuredData::XZY);
Vector3i id; Vector3i id;
int index = 0; int index = 0;
@@ -258,7 +258,7 @@ void VoxImageFilter<_TPLT_>::SetKernelWeightFunction(ShapeT shape)
_TPL_ _TPL_
void VoxImageFilter<_TPLT_>::SetImage(Abstract::VoxImage *image) void VoxImageFilter<_TPLT_>::SetImage(ImageData *image)
{ {
this->m_Image = reinterpret_cast<VoxImage<VoxelT> *> (image); this->m_Image = reinterpret_cast<VoxImage<VoxelT> *> (image);
this->SetKernelOffset(); this->SetKernelOffset();

View File

@@ -3,6 +3,7 @@ set(TESTS
MathVectorTest MathVectorTest
GeometryTest GeometryTest
ContainerBoxTest ContainerBoxTest
ImageDataTest
VoxImageTest VoxImageTest
VoxRaytracerTest VoxRaytracerTest
StructuredDataTest StructuredDataTest

View File

@@ -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<MyVoxel> v;
DataVectorImage img;
img.Data() = v;
img.Scalars().SetAccessFunctions(&MyVoxel::value);
img.SetDims(Vector3i(3,3,3));
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;
img.Data() = vm;
img.SetDims(Vector3i(3,3,3));
img.Scalars().SetAccessFunctions(&VoxelMean::GetValue,&VoxelMean::SetValue);
for (int x=0; x<img.GetDims().prod(); ++x){
img.SetValue(x,x);
img.SetValue(x,x+2);
img.SetValue(x,x-1);
std::cout << img.UnMap(x).transpose() << " -> " << img.GetValue(x) << "\n";
}
}

View File

@@ -48,7 +48,7 @@ public:
}; };
public: public:
typedef Abstract::VoxImage Content; typedef ImageData Content;
vtkVoxImage(Content &content); vtkVoxImage(Content &content);

View File

@@ -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 void * Pointer_t;
typedef id_t Id_t; typedef id_t Id_t;
typedef size_t Size_t; typedef size_t Size_t;