[uLib Geometry]

non working version!

+ adds ProgrammableAccessor
+ renaming of some Image structures ...
This commit is contained in:
Andrea Rigoni
2014-11-03 10:27:52 +00:00
commit 99e771a223
403 changed files with 61684 additions and 0 deletions

334
src/Math/Accumulator.h Normal file
View File

@@ -0,0 +1,334 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_DATABINNING_H
#define U_DATABINNING_H
#include "Core/Vector.h"
#include "Dense.h"
namespace uLib {
// TODO: USE BOOST ACCUMULATORS //
template <typename T>
class Accumulator_Mean {
typedef std::pair<T,unsigned long> Tmean;
public:
Accumulator_Mean() {
m_Means.push_back( Tmean(0,0) );
}
void operator()(const T data) {
T tmp = 0;
// for(typename std::vector<Tmean>::iterator it = m_Means.begin(); it < m_Means.back(); ++it)
// tmp += it->first/it->second;
for(int i=0; i<m_Means.size()-1;++i)
tmp += m_Means[i].first / m_Means[i].second;
m_Means.back().first += data - tmp;
m_Means.back().second += 1;
}
T operator()() const {
T mean = 0;
// for(typename std::vector<Tmean>::iterator it = m_Means.begin(); it < m_Means.end(); ++it) {
// mean += it->first/it->second; }
for(int i=0; i<m_Means.size();++i)
mean += m_Means[i].first / m_Means[i].second;
return mean;
}
void AddPass() { m_Means.push_back( Tmean(0,0) ); }
private:
Vector< Tmean > m_Means;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// accumulator Trim //
template < typename T, int subsample_size=200 >
class Accumulator_ABTrim {
public:
Accumulator_ABTrim() :
m_Avg(0),
m_InternalCount(0),
m_SizeA(0),
m_SizeB(0),
m_IdA(0),
m_IdB(0)
{}
Accumulator_ABTrim(const Accumulator_ABTrim &c) {
# pragma omp critical
{
m_Avg = c.m_Avg;
m_InternalCount = c.m_InternalCount;
m_SizeA = c.m_SizeA;
m_SizeB = c.m_SizeB;
m_IdA = c.m_IdA;
m_IdB = c.m_IdB;
memcpy (m_Av, c.m_Av, sizeof (m_Av));
}
}
void operator += (T value) {
if(m_InternalCount > subsample_size) {
// array complete and counter over subsample //
if( m_SizeA > 0 && value < m_ValA ) {
;// m_Avg += m_ValA;
}
else if (m_SizeB > 0 && value > m_ValB)
{
;// m_Avg += m_ValB;
}
else
{
m_Avg += value;
m_InternalCount++;
}
}
else if(m_InternalCount >=0) {
// array complete
if(m_SizeA > 0 && value < m_ValA)
{
m_Avg += m_ValA;
m_Av[m_IdA] = value;
for (unsigned int i=0; i < m_SizeA; i++)
if(m_Av[i] > m_Av[m_IdA])
{ m_IdA = i; m_ValA = m_Av[i]; }
}
else if(m_SizeB > 0 && value > m_ValB)
{
m_Avg += m_ValB;
m_Av[m_IdB] = value;
for (unsigned int i=m_SizeA; i < m_SizeA+m_SizeB; i++)
if(m_Av[i] < m_Av[m_IdB])
{ m_IdB = i; m_ValB = m_Av[i]; }
}
else {
m_Avg += value;
}
m_InternalCount++;
}
else { // m_InternalCount < 0
// array is not fullfilled
m_Av[m_SizeA+m_SizeB+m_InternalCount] = value;
m_InternalCount++;
if(m_InternalCount == 0) {
std::sort(m_Av,m_Av+m_SizeA+m_SizeB);
if(m_SizeA > 0) {
m_IdA = m_SizeA-1;
m_ValA = m_Av[m_IdA];
}
if(m_SizeB > 0) {
m_IdB = m_SizeA;
m_ValB = m_Av[m_SizeA];
}
}
}
}
T operator()() {
if(m_InternalCount <= 0) {
std::sort(m_Av, m_Av+m_SizeA+m_SizeB+m_InternalCount);
return m_Av[ (m_SizeA+m_SizeB+m_InternalCount) / 2]; // median value //
}
else {
// return (m_Avg + m_ValA * m_SizeA + m_ValB * m_SizeB) /
// (m_InternalCount + m_SizeA + m_SizeB);
return (m_Avg) / m_InternalCount;
}
}
void SetABTrim(int a, int b) {
if(a+b > subsample_size/2) {
m_SizeA = a/(a+b) * subsample_size/2;
m_SizeB = b/(a+b) * subsample_size/2;
}
else {
m_SizeA = a;
m_SizeB = b;
}
m_Avg = 0;
m_InternalCount = -m_SizeA-m_SizeB;
}
private:
T m_Av[subsample_size/2];
T m_Avg, m_ValA, m_ValB;
int m_IdA, m_IdB, m_InternalCount;
int m_SizeA, m_SizeB;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Clip Accumulator //
template < typename T, int subsample_size=200 >
class Accumulator_ABClip {
public:
Accumulator_ABClip() :
m_Avg(0),
m_InternalCount(0),
m_SizeA(0),
m_SizeB(0),
m_IdA(0),
m_IdB(0)
{}
Accumulator_ABClip(const Accumulator_ABClip &c) {
# pragma omp critical
{
m_Avg = c.m_Avg;
m_InternalCount = c.m_InternalCount;
m_SizeA = c.m_SizeA;
m_SizeB = c.m_SizeB;
m_IdA = c.m_IdA;
m_IdB = c.m_IdB;
memcpy (m_Av, c.m_Av, sizeof (m_Av));
}
}
void operator += (T value) {
if(m_InternalCount > subsample_size) {
// array complete and counter over subsample //
if( m_SizeA > 0 && value < m_ValA ) {
m_Avg += m_ValA;
}
else if (m_SizeB > 0 && value > m_ValB) {
m_Avg += m_ValB;
}
else {
m_Avg += value;
}
m_InternalCount++;
}
else if(m_InternalCount >=0) {
// array complete
if(m_SizeA > 0 && value < m_ValA)
{
m_Avg += m_ValA;
m_Av[m_IdA] = value;
for (unsigned int i=0; i < m_SizeA; i++)
if(m_Av[i] > m_Av[m_IdA])
{ m_IdA = i; m_ValA = m_Av[i]; }
}
else if(m_SizeB > 0 && value > m_ValB)
{
m_Avg += m_ValB;
m_Av[m_IdB] = value;
for (unsigned int i=m_SizeA; i < m_SizeA+m_SizeB; i++)
if(m_Av[i] < m_Av[m_IdB])
{ m_IdB = i; m_ValB = m_Av[i]; }
}
else {
m_Avg += value;
}
m_InternalCount++;
}
else { // m_InternalCount < 0
// array is not fullfilled
m_Av[m_SizeA+m_SizeB+m_InternalCount] = value;
m_InternalCount++;
if(m_InternalCount == 0) {
std::sort(m_Av,m_Av+m_SizeA+m_SizeB);
if(m_SizeA > 0) {
m_IdA = m_SizeA-1;
m_ValA = m_Av[m_IdA];
}
if(m_SizeB > 0) {
m_IdB = m_SizeA;
m_ValB = m_Av[m_SizeA];
}
}
}
}
T operator()() {
if(m_InternalCount <= 0) {
std::sort(m_Av, m_Av+m_SizeA+m_SizeB+m_InternalCount);
return m_Av[ (m_SizeA+m_SizeB+m_InternalCount) / 2]; // median value //
}
else {
return (m_Avg + m_ValA * m_SizeA + m_ValB * m_SizeB) /
(m_InternalCount + m_SizeA + m_SizeB);
}
}
void SetABTrim(int a, int b) {
if(a+b > subsample_size/2) {
m_SizeA = a/(a+b) * subsample_size/2;
m_SizeB = b/(a+b) * subsample_size/2;
}
else {
m_SizeA = a;
m_SizeB = b;
}
m_Avg = 0;
m_InternalCount = -m_SizeA-m_SizeB;
}
private:
T m_Av[subsample_size/2];
T m_Avg, m_ValA, m_ValB;
int m_IdA, m_IdB, m_InternalCount;
int m_SizeA, m_SizeB;
};
} // uLib
#endif // U_DATABINNING_H

259
src/Math/BitCode.h Normal file
View File

@@ -0,0 +1,259 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_MATH_BITCODE_H
#define U_MATH_BITCODE_H
#include <boost/static_assert.hpp>
#include <boost/type_traits.hpp>
//#include <Core/CommaInitializer.h>
#include <Math/Dense.h>
#include <Core/Mpl.h>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/set_c.hpp>
namespace uLib {
template < typename ContainerT, typename ContentT >
struct CommaInitializerBitCode
{
inline explicit CommaInitializerBitCode(ContainerT *container, ContentT s)
: container(container)
{
this->index = 0;
this->container->operator()().field1 = s;
}
inline CommaInitializerBitCode & operator, (ContentT s) {
this->index++;
if(index < container->size()) {
if(index == 1) container->operator()().field2 = s;
if(index == 2) container->operator()().field3 = s;
if(index == 3) container->operator()().field4 = s;
}
return *this;
}
ContainerT *container;
unsigned int index;
};
template <typename T, uint L1, uint L2>
class BitCode2
{
protected:
typedef T Type;
typedef BitCode2<T,L1,L2> ThisClass;
typedef CommaInitializerBitCode< ThisClass, T > CommaInit;
BOOST_STATIC_ASSERT_MSG( boost::is_unsigned<T>::value == 1, "CODE TYPE MUST BE UNSIGNED" );
BOOST_STATIC_ASSERT( L1+L2 == sizeof(T)*8 );
public:
struct BitField {
T field1 : L1;
T field2 : L2;
};
union CodeSet {
BitField bitf;
T value;
} m_data;
BitCode2() {}
BitCode2(const T& data) { m_data.value = data; }
BitCode2(const Vector2i & data) {
(*this) << data(0),data(1);
}
inline CommaInit operator <<(T scalar) { return CommaInit(this, scalar); }
static const int size() { return 2; }
BitField & operator()() { return m_data.bitf; }
const BitField & operator()() const { return m_data.bitf; }
operator Vector2i () { return Vector2i(m_data.bitf.field1,
m_data.bitf.field2); }
void set(const T data) { m_data.value = data; }
T get() const { return m_data.value; }
};
template <typename T, uint L1, uint L2>
std::ostream &
operator << (std::ostream &o, const BitCode2<T,L1,L2> &code) {
o << code().field1 << "," << code().field2;
return o;
}
template <typename T, uint L1, uint L2, uint L3>
class BitCode3
{
protected:
typedef T Type;
typedef BitCode3<T,L1,L2,L3> ThisClass;
typedef CommaInitializerBitCode< ThisClass, T > CommaInit;
BOOST_STATIC_ASSERT_MSG( boost::is_unsigned<T>::value == 1, "CODE TYPE MUST BE UNSIGNED" );
BOOST_STATIC_ASSERT( L1+L2+L3 == sizeof(T)*8 );
public:
struct BitField {
T field1 : L1;
T field2 : L2;
T field3 : L3;
};
union CodeSet {
BitField bitf;
T value;
} m_data;
BitCode3() {}
BitCode3(const T& data) { m_data.value = data; }
BitCode3(const Vector3i & data) {
(*this) << data(0),data(1),data(2);
}
inline CommaInit operator <<(T scalar) { return CommaInit(this, scalar); }
static const int size() { return 3; }
BitField & operator()() { return m_data.bitf; }
const BitField & operator()() const { return m_data.bitf; }
operator Vector3i () { return Vector3i(m_data.bitf.field1,
m_data.bitf.field2,
m_data.bitf.field3); }
void set(const T data) { m_data.value = data; }
T get() const { return m_data.value; }
};
template <typename T, uint L1, uint L2, uint L3>
std::ostream &
operator << (std::ostream &o, const BitCode3<T,L1,L2,L3> &code) {
o << code().field1 << "," << code().field2 << "," << code().field3;
return o;
}
template <typename T, uint L1, uint L2, uint L3, uint L4>
class BitCode4
{
protected:
typedef T Type;
typedef BitCode4<T,L1,L2,L3,L4> ThisClass;
typedef CommaInitializerBitCode< ThisClass, T > CommaInit;
BOOST_STATIC_ASSERT_MSG( boost::is_unsigned<T>::value == 1, "CODE TYPE MUST BE UNSIGNED" );
BOOST_STATIC_ASSERT( L1+L2+L3+L4 == sizeof(T)*8 );
public:
struct BitField {
T field1 : L1;
T field2 : L2;
T field3 : L3;
T field4 : L4;
};
union CodeSet {
BitField bitf;
T value;
} m_data;
BitCode4() {}
BitCode4(const T& data) { m_data.value = data; }
BitCode4(const Vector4i & data) {
(*this) << data(0),data(1),data(2),data(3);
}
inline CommaInit operator << (T scalar) { return CommaInit(this, scalar); }
static const int size() { return 4; }
BitField & operator()() { return m_data.bitf; }
const BitField & operator()() const { return m_data.bitf; }
operator Vector4i () { return Vector4i(m_data.bitf.field1,
m_data.bitf.field2,
m_data.bitf.field3,
m_data.bitf.field4); }
void set(const T data) { m_data.value = data; }
T get() const { return m_data.value; }
};
template <typename T, uint L1, uint L2, uint L3, uint L4>
std::ostream &
operator << (std::ostream &o, const BitCode4<T,L1,L2,L3,L4> &code) {
o << code().field1 << "," << code().field2 << "," << code().field3 << "," << code().field4;
return o;
}
template <typename T, uint L1, uint L2 = 0, uint L3 = 0>
class BitCode {
typedef boost::mpl::vector_c<T,L1,L2,L3> BitSet;
typedef boost::mpl::vector_c<T,0,L1,L1+L2> BitSft;
// static constexpr uint bset[3] = {L1,L2,L3};
// static constexpr uint bsft[3] = {0,L1,L1+L2};
BOOST_STATIC_ASSERT_MSG( boost::is_unsigned<T>::value == 1, "CODE TYPE MUST BE UNSIGNED" );
//BOOST_STATIC_ASSERT( L1+L2+L3 == sizeof(T)*8 );
T m_data;
public:
void PrintSelf( ) {
// for(int i=0; i< boost::mpl::size<BitSet>() ;++i)
// std::cout << "BitSet " << i << " [" << bset[i] << "] -> " << Get(i) << "\n";
}
T Get(unsigned short field) const {
// return (m_data >> bsft[field]) & (1<<bset[field]-1);
}
void Set(unsigned short field, const T data) {
// TODO: optimize Set BitCode
// T mask = (1<<bset[field]-1) << bsft[field];
// T d1 = (data << bsft[field]) & mask;
// T d2 = m_data & !mask;
// m_data = d1 | d2;
}
};
} // uLib
#endif // BITCODE_H

54
src/Math/CMakeLists.txt Normal file
View File

@@ -0,0 +1,54 @@
# HEADERS
set(HEADERS
ContainerBox.h
Dense.h
Geometry.h
Transform.h
StructuredData.h
# StructuredGrid.h
ImageMap.h
ImageData.h
VoxImage.h
VoxRaytracer.h
Utils.h
VoxImageFilter.h
VoxImageFilter.hpp
VoxImageFilterLinear.hpp
VoxImageFilterMedian.hpp
VoxImageFilterABTrim.hpp
VoxImageFilterBilateral.hpp
VoxImageFilterThreshold.hpp
VoxImageFilter2ndStat.hpp
VoxImageFilterCustom.hpp
Accumulator.h
TriangleMesh.h
BitCode.h
)
# SOURCES
set(SOURCES
VoxRaytracer.cpp
StructuredData.cpp
# StructuredGrid.cpp
ImageMap.cpp
ImageData.cpp
VoxImage.cpp
TriangleMesh.cpp
Dense.cpp
)
set(LIBRARIES
${Boost_SERIALIZATION_LIBRARY}
${Boost_SIGNALS_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Eigen_LIBRARY}
${ROOT_LIBRARIES}
${PACKAGE_LIBPREFIX}Core
${PACKAGE_LIBPREFIX}Math
)
uLib_add_shared_library(${uLib-module})
add_subdirectory(testing)

95
src/Math/ContainerBox.h Normal file
View File

@@ -0,0 +1,95 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_CONTAINERBOX_H
#define U_CONTAINERBOX_H
#include "Geometry.h"
namespace uLib {
class ContainerBox : public AffineTransform {
public:
ContainerBox() : m_LocalT(this) {}
ContainerBox(const ContainerBox &copy) :
m_LocalT(this),
AffineTransform(copy)
{
// FIX for performance //
this->SetOrigin(copy.GetOrigin());
this->SetSize(copy.GetSize());
}
inline void SetOrigin(const Vector3f &v) { m_LocalT.SetPosition(v); }
inline Vector3f GetOrigin() const { return m_LocalT.GetPosition(); }
void SetSize(const Vector3f &v) {
Vector3f pos = this->GetOrigin();
m_LocalT = AffineTransform(this);
m_LocalT.Scale(v);
m_LocalT.SetPosition(pos);
}
inline Vector3f GetSize() const { return m_LocalT.GetScale(); }
// WARNING: FIX... //
inline void FlipLocalAxes(int first, int second)
{ m_LocalT.FlipAxes(first,second); }
Matrix4f GetWorldMatrix() const { return m_LocalT.GetWorldMatrix(); }
inline Vector4f GetWorldPoint(const Vector4f &v) const {
return m_LocalT.GetWorldMatrix() * v;
}
inline Vector4f GetWorldPoint(const float x, const float y, const float z) {
return this->GetWorldPoint(Vector4f(x,y,z,1));
}
inline Vector4f GetLocalPoint(const Vector4f &v) const {
return m_LocalT.GetWorldMatrix().inverse() * v;
}
inline Vector4f GetLocalPoint(const float x, const float y, const float z) {
return this->GetLocalPoint(Vector4f(x,y,z,1));
}
private:
AffineTransform m_LocalT;
};
}
#endif // CONTAINERBOX_H

77
src/Math/DataSet.h Normal file
View File

@@ -0,0 +1,77 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_MATH_DATASET_H
#define U_MATH_DATASET_H
#include <Core/Macros.h>
#include <Core/Vector.h>
#include <Core/Function.h>
#include "Math/Dense.h"
#include "Math/StructuredData.h"
#include <Core/ProgrammableAccessor.h>
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;
private:
};
template <typename T>
class VectorData : public DataSet_Base, public Vector<T> {
public:
protected:
const void *GetPtr(Id_t id) const { return &this->at(id); }
void * GetPtr(Id_t id) { return &this->at(id); }
};
} // uLib
#endif // DATASET_H

64
src/Math/Dense.cpp Normal file
View File

@@ -0,0 +1,64 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "Dense.h"
#include "Core/Archives.h"
//ULIB_SERIALIZE(uLib::HPoint3f) {
// ar & boost::serialization::make_nvp(NULL,boost::serialization::base_object<uLib::HPoint3f::BaseClass,uLib::HPoint3f>(ob));
//}
//ULIB_SERIALIZE(uLib::Vector4f) {
// ar & boost::serialization::make_nvp(NULL,boost::serialization::base_object<uLib::Vector4f::BaseClass,uLib::Vector4f>(ob));
//}
//ULIB_SERIALIZE(uLib::HLine3f) {
// ar
// & "HLine -> ( origin: " & AR(origin) & "; direction: " & AR(direction) & ") ";
//}
//ULIB_SERIALIZE(uLib::HError3f) {
// ar
// & "HLine -> ( poserr: " & AR(position_error) & "; direrr: " & AR(direction_error) & ") ";
//}
// NEW //
ULIB_SERIALIZE(uLib::Vector2i) { ar & boost::serialization::make_nvp(NULL,ob); }
ULIB_SERIALIZE(uLib::Vector3i) { ar & boost::serialization::make_nvp(NULL,ob); }
ULIB_SERIALIZE(uLib::Vector4i) { ar & boost::serialization::make_nvp(NULL,ob); }
ULIB_SERIALIZE(uLib::Vector2f) { ar & boost::serialization::make_nvp(NULL,ob); }
ULIB_SERIALIZE(uLib::Vector3f) { ar & boost::serialization::make_nvp(NULL,ob); }
ULIB_SERIALIZE(uLib::Vector4f) { ar & boost::serialization::make_nvp(NULL,ob); }
ULIB_SERIALIZE(uLib::Vector2d) { ar & boost::serialization::make_nvp(NULL,ob); }
ULIB_SERIALIZE(uLib::Vector3d) { ar & boost::serialization::make_nvp(NULL,ob); }
ULIB_SERIALIZE(uLib::Vector4d) { ar & boost::serialization::make_nvp(NULL,ob); }

364
src/Math/Dense.h Normal file
View File

@@ -0,0 +1,364 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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.
//////////////////////////////////////////////////////////////////////////////*/
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2012 Andrea Rigoni Garola <andrea@pcimg05>
*
* 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 2.1 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; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#ifndef ULIB_DENSEMATRIX_H
#define ULIB_DENSEMATRIX_H
#include <stdlib.h>
#include <Eigen/Dense>
#include "Core/Types.h"
#include "Core/Serializable.h"
///////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// NEW PART //
namespace uLib {
////////////////////////////////////////////////////////////////////////////////
// DENSE MATRIX DEFINITION /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
using Eigen::Matrix;
////////////////////////////////////////////////////////////////////////////////
// DENSE TYPES DEFINITIONS /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
typedef Eigen::Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
typedef Eigen::Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
typedef Eigen::Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix; \
// /* IMPORT INTERFACES */ \
// typedef Interface::DenseMatrix<Type, mpl::int_<Size>, mpl::int_<Size> > IMatrix##SizeSuffix##TypeSuffix; \
// typedef Interface::DenseArray<Type, mpl::int_<Size> > IVector##SizeSuffix##TypeSuffix; \
// typedef Interface::DenseArray<Type, mpl::int_<Size> > IRowVector##SizeSuffix##TypeSuffix;
#define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4)
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
#undef EIGEN_MAKE_TYPEDEFS
} // uLib
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// OLD PART //
//// BOOST SERIALIZATION ///////////////////////////////////////////////////////
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/array.hpp>
namespace boost {
namespace serialization {
template<class Archive, class Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
void serialize(Archive & ar, ::Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime> & m, const unsigned int /*version*/) {
ar & boost::serialization::make_array(m.data(), RowsAtCompileTime * ColsAtCompileTime);
}
} // serialization
} // boost
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// EIGEN VECTOR STREAM INTERACTIONS ////////////////////////////////////////////
// this is needed by boost::lexical_cast to cope with Eigens Vectors ///////////
namespace Eigen {
template <typename C, typename T, int size>
std::istream & operator >> (std::basic_istream<C> &is, Eigen::Matrix<T,size,1> &vec) {
std::basic_string<C> str;
for( unsigned int i=0; i<size; i++) {
is >> std::skipws >> str;
if(is.fail()) vec(i) = 0;
else vec(i) = boost::lexical_cast<T>(str);
}
return is;
}
template <typename C, typename T, int size>
Eigen::Matrix<T,size,1> & operator >> (const std::basic_string<C> &str, Eigen::Matrix<T,size,1> &vec) {
std::stringstream ss(str);
ss >> vec;
return vec;
}
template <typename C, typename T, int size>
std::basic_ostream<C> & operator << (std::basic_ostream<C> &os, const Eigen::Matrix<T,size,1> &vec) {
// os << "[" << vec.transpose() << "]T"; // TODO: read and write Transposition with a proper symbol
os << vec.transpose();
return os;
}
template <typename C, typename T, int size>
std::basic_string<C> & operator << (std::basic_string<C> &str, const Eigen::Matrix<T,size,1> &vec) {
std::basic_stringstream<C> ss;
ss << vec;
str += ss.str();
return str;
}
} // Eigen
////////////////////////////////////////////////////////////////////////////////
namespace uLib {
typedef int Scalari;
typedef unsigned int Scalarui;
typedef long Scalarl;
typedef unsigned long Scalarul;
typedef float Scalarf;
typedef double Scalard;
} // uLib
//typedef Eigen::Matrix<int,1,1> Matrix1i;
//typedef Eigen::Matrix2i Matrix2i;
//typedef Eigen::Matrix3i Matrix3i;
//typedef Eigen::Matrix4i Matrix4i;
//typedef Eigen::Matrix<float,1,1> Matrix1f;
//typedef Eigen::Matrix2f Matrix2f;
//typedef Eigen::Matrix3f Matrix3f;
//typedef Eigen::Matrix4f Matrix4f;
//typedef Eigen::Matrix<int,1,1> Vector1i;
//typedef Eigen::Vector2i Vector2i;
//typedef Eigen::Vector3i Vector3i;
//typedef Eigen::Vector4i Vector4i;
//typedef Eigen::Matrix<float,1,1> Vector1f;
//typedef Eigen::Vector2f Vector2f;
//typedef Eigen::Vector3f Vector3f;
//typedef Eigen::Vector4f Vector4f;
////////////////////////////////////////////////////////////////////////////////
// Vector String interaction ///////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
///*! Given a string consisting of a series of doubles with some
//* delimiter, return an Eigen::Vector populated with those
//* values, in the same order as they are given in the string.
//*
//* \param vec A double vector to be populated with the results
//* \param str A string to be parsed as a series of doubles.
//* \param delim Delimiters of the text (a typical default is " ," for comma and space-delimited text
//*
//*/
//template <typename T, int size>
//void VectorxT_StringTo(Eigen::Matrix<T,size,1> &vec, std::string str, const char *delim = " ,;\t\n") {
// std::vector<std::string> strvec;
// boost::algorithm::trim_if( str, boost::algorithm::is_any_of(delim));
// boost::algorithm::split(strvec,str,boost::algorithm::is_any_of(delim), boost::algorithm::token_compress_on);
// for( unsigned int i=0; i<size; i++) {
// vec(i) = boost::lexical_cast<T>(strvec[i]);
// }
//}
//template <typename T, int size>
//std::string VectorxT_ToString(const Eigen::Matrix<T,size,1> &vec) {
// std::stringstream sst;
// sst << vec.transpose();
// return sst.str();
//}
////template <typename T, int size>
////Eigen::Matrix<T,size,1> & operator >> (std::istream &is, Eigen::Matrix<T,size,1> &vec) {
////}
//template <typename T, int size>
//void operator>> (std::string& str, Eigen::Matrix<T,size,1> &vec){
// VectorxT_StringTo(vec,str);
//}
////////////////////////////////////////////////////////////////////////////////
////// HOMOGENEOUS VECTORS //////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//template <bool p>
//class _HPoint3f : public Eigen::Matrix< Scalarf,4,1 > {
//public:
// typedef Eigen::Matrix< Scalarf,4,1 > BaseClass;
// _HPoint3f<p>() : BaseClass(0,0,0,p) {}
// _HPoint3f<p>(float x,float y,float z) : BaseClass(x,y,z,p) {}
// _HPoint3f<p>(Vector3f &in) : BaseClass(in.homogeneous()) { this->w = p; }
// // This constructor allows to construct MyVectorType from Eigen expressions
// template<typename OtherDerived>
// inline _HPoint3f<p>(const Eigen::MatrixBase<OtherDerived>& other)
// : BaseClass(other)
// { }
// // This method allows to assign Eigen expressions to Vector3H
// template<typename OtherDerived>
// inline _HPoint3f<p> & operator= (const Eigen::MatrixBase <OtherDerived>& other)
// {
// this->BaseClass::operator=(other);
// return *this;
// }
//};
//typedef _HPoint3f<false> Vector4f;
//typedef _HPoint3f<true> HPoint3f;
////////////////////////////////////////////////////////////////////////////////
////// HOMOGENEOUS LINE //////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//struct _HLine3f
//{
// HPoint3f origin;
// Vector4f direction;
//};
//typedef struct _HLine3f HLine3f;
//inline std::ostream&
//operator<< (std::ostream& stream, const HLine3f &line) {
// stream << "HLine3f(" << "pt[" << line.origin.transpose() <<"] , dr[" << line.direction.transpose() << "]) ";
// return stream;
//}
//struct _HError3f
//{
// Vector4f position_error;
// Vector4f direction_error;
//};
//typedef struct _HError3f HError3f;
//inline std::ostream&
//operator<< (std::ostream& stream, const HError3f &err) {
// stream << "HError3f(" << "ept[" << err.position_error.transpose() <<"] , edr[" << err.direction_error.transpose() << "]) ";
// return stream;
//}
//}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// SERIALIZTION //
// NEW //
ULIB_SERIALIZABLE(uLib::Vector2i)
ULIB_SERIALIZABLE(uLib::Vector3i)
ULIB_SERIALIZABLE(uLib::Vector4i)
ULIB_SERIALIZABLE(uLib::Vector2f)
ULIB_SERIALIZABLE(uLib::Vector3f)
ULIB_SERIALIZABLE(uLib::Vector4f)
ULIB_SERIALIZABLE(uLib::Vector2d)
ULIB_SERIALIZABLE(uLib::Vector3d)
ULIB_SERIALIZABLE(uLib::Vector4d)
//ULIB_SERIALIZABLE(uLib::HPoint3f)
//ULIB_SERIALIZABLE(uLib::Vector4f)
//ULIB_SERIALIZABLE(uLib::HLine3f)
//ULIB_SERIALIZABLE(uLib::HError3f)
#endif // U_DENSEMATRIX_H

60
src/Math/Geometry.h Normal file
View File

@@ -0,0 +1,60 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_GEOMETRY_H
#define U_GEOMETRY_H
#include "Core/Object.h"
#include "Math/Dense.h"
#include "Math/Transform.h"
namespace uLib {
class Geometry : public AffineTransform {
public:
inline Vector4f GetWorldPoint(const Vector4f &v) const {
return this->GetWorldMatrix() * v;
}
inline Vector4f GetWorldPoint(const float x, const float y, const float z) {
return this->GetWorldPoint(Vector4f(x,y,z,1));
}
inline Vector4f GetLocalPoint(const Vector4f &v) const {
return this->GetWorldMatrix().inverse() * v;
}
inline Vector4f GetLocalPoint(const float x, const float y, const float z) {
return this->GetLocalPoint(Vector4f(x,y,z,1));
}
};
}
#endif // GEOMETRY_H

0
src/Math/ImageData.cpp Normal file
View File

54
src/Math/ImageData.h Normal file
View File

@@ -0,0 +1,54 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_MATH_IMAGEDATA_H
#define U_MATH_IMAGEDATA_H
#include <Math/ImageSpace.h>
#include <Math/ImageMap.h>
#include <Math/DataSet.h>
#include <Math/StructuredData.h>
namespace uLib {
class ImageData : public ImageSpace, public ImageMap {
ImageData() {}
public:
};
} // uLib
#endif // IMAGEDATA_H

80
src/Math/ImageMap.cpp Normal file
View File

@@ -0,0 +1,80 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "ImageMap.h"
using namespace uLib;
ImageMap::ImageMap(const Vector3i &size) :
m_Dims(size)
{
SetDataOrder();
}
void ImageMap::SetDims(const Vector3i &size)
{
this->m_Dims = size;
SetDataOrder();
}
void ImageMap::SetDataOrder(ImageMap::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 ImageMap::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 ImageMap::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;
}

95
src/Math/ImageMap.h Normal file
View File

@@ -0,0 +1,95 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_MATH_IMAGEMAP_H
#define U_MATH_IMAGEMAP_H
#include "Core/Macros.h"
#include "Core/Object.h"
#include "Math/Dense.h"
#include "Math/DataSet.h"
namespace uLib {
class ImageMap {
public:
typedef enum {
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
} Order;
ImageMap(const Vector3i &size);
ImageMap(const ImageMap &copy) :
m_DataOrder(copy.m_DataOrder),
m_Dims(copy.m_Dims),
m_Increments(copy.m_Increments)
{}
uLibGetMacro(Dims,Vector3i)
void SetDims(const Vector3i &size);
uLibGetSetMacro(Increments,Vector3i)
void SetDataOrder(Order order = YXZ);
uLibGetMacro(DataOrder,Order)
bool IsInsideGrid(const Vector3i &v) const;
inline int Map(Vector3i index) const;
Vector3i UnMap(int index) const;
private:
Order m_DataOrder;
Vector3i m_Dims;
Vector3i m_Increments; //TODO: make this line matrix //
};
// --- INLINES -------------------------------------------------------------- //
inline int ImageMap::Map(Vector3i index) const
{
return (m_Increments.transpose() * index);
}
} // uLib
#endif // IMAGEMAP_H

87
src/Math/ImageSpace.cpp Normal file
View File

@@ -0,0 +1,87 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "ImageSpace.h"
namespace uLib {
ImageSpace::ImageSpace(const Vector3i 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)
{
ContainerBox::SetSize(spacing);
}
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;
Vector4f p = GetLocalPoint(pt);
out << (int)floor(p(0)),
(int)floor(p(1)),
(int)floor(p(2));
return out;
}
void ImageSpace::PrintSelf(std::ostream &o)
{
o << "uLib Structured Grid: \n"
// << "dim = " << this->GetDims().transpose() << "\n"
<< "spacing = " << this->GetSpacing().transpose() << "\n";
}
} // uLib

67
src/Math/ImageSpace.h Normal file
View File

@@ -0,0 +1,67 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_MATH_IMAGESPACE_H
#define U_MATH_IMAGESPACE_H
#include "Core/Object.h"
#include "Math/ContainerBox.h"
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:
using ContainerBox::GetSize;
using ContainerBox::SetSize;
};
} // uLib
#endif // IMAGESPACE_H

69
src/Math/Line.h Normal file
View File

@@ -0,0 +1,69 @@
#ifndef U_GEO_LINE_H
#define U_GEO_LINE_H
#include "Core/Macros.h"
#include "Core/Mpl.h"
#include "Core/Serializable.h"
#include "Math/Dense.h"
namespace uLib {
////////////////////////////////////////////////////////////////////////////////
// VECTOR PAIR /////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template <typename _Scalar, int _Dim>
class VectorPair {
public:
typedef Matrix<_Scalar,_Dim,1> Vector;
VectorPair() {}
VectorPair(const Vector &first,
const Vector &second) {
this->m_data[0] = first;
this->m_data[1] = second;
}
uLibArrayMacro(Data,Vector *)
protected:
EIGEN_ALIGN16 Vector m_Data[2];
};
////////////////////////////////////////////////////////////////////////////////
// LINE ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template <typename _Scalar, int _Dim>
class Line : public VectorPair<_Scalar, _Dim> {
public:
ULIB_SERIALIZE_ACCESS
typedef VectorPair<_Scalar, _Dim> BaseClass;
typedef typename BaseClass::Vector Vector;
Line() {}
Line(const Vector &origin, const Vector &direction) : BaseClass(origin,direction) {}
Vector & origin() { return this->m_Data[0]; }
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]; }
};
typedef Line<float,2> Line2f;
typedef Line<float,3> Line3f;
typedef Line<float,4> Line4f;
typedef Line4f HLine3f;
} // uLib
//ULIB_SERIALIZABLE(uLib::Line2f)
//ULIB_SERIALIZABLE(uLib::Line3f)
//ULIB_SERIALIZABLE(uLib::Line4f)
#endif // LINE_H

42
src/Math/Makefile.am Normal file
View File

@@ -0,0 +1,42 @@
SUBDIRS = .
include $(top_srcdir)/Common.am
library_includedir = $(includedir)/libmutom-${PACKAGE_VERSION}/Math
library_include_HEADERS = ContainerBox.h \
Dense.h \
Geometry.h \
Transform.h \
StructuredData.h\
StructuredGrid.h\
VoxImage.h \
VoxRaytracer.h \
Utils.h \
VoxImageFilter.h\
VoxImageFilter.hpp \
VoxImageFilterLinear.hpp \
VoxImageFilterMedian.hpp \
VoxImageFilterABTrim.hpp \
VoxImageFilterBilateral.hpp \
VoxImageFilterThreshold.hpp \
VoxImageFilter2ndStat.hpp \
VoxImageFilterCustom.hpp \
Accumulator.h \
TriangleMesh.h
_MATH_SOURCES = \
VoxRaytracer.cpp \
StructuredData.cpp \
StructuredGrid.cpp \
VoxImage.cpp \
TriangleMesh.cpp \
Dense.cpp
noinst_LTLIBRARIES = libmutommath.la
libmutommath_la_SOURCES = ${_MATH_SOURCES}

48
src/Math/Polydata.h Normal file
View File

@@ -0,0 +1,48 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 POLYDATA_H
#define POLYDATA_H
#include "Core/Object.h"
namespace uLib {
class Polydata : public Object {
public:
};
}
#endif // POLYDATA_H

49
src/Math/Quote.h Normal file
View File

@@ -0,0 +1,49 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_MATH_QUOTE_H
#define U_MATH_QUOTE_H
#include "Math/Dense.h"
namespace uLib {
class Quote : public Vector2f {
public:
uLibRefMacro(Offset,Scalarf);
private:
Scalarf m_Offset;
};
} // uLib
#endif // QUOTE_H

View File

@@ -0,0 +1,80 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "StructuredData.h"
using namespace uLib;
StructuredData::StructuredData(const Vector3i &size) :
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;
}
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;
}

101
src/Math/StructuredData.h Normal file
View File

@@ -0,0 +1,101 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 STRUCTUREDDATA_H
#define STRUCTUREDDATA_H
#include "Core/Macros.h"
#include "Core/Object.h"
#include "Math/Dense.h"
#include "Math/DataSet.h"
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
};
typedef enum _Order Order;
StructuredData(const Vector3i &size);
StructuredData(const StructuredData &copy) :
m_DataOrder(copy.m_DataOrder),
m_Dims(copy.m_Dims),
m_Increments(copy.m_Increments)
{}
uLibGetMacro(Dims,Vector3i)
void SetDims(const Vector3i &size);
uLibGetSetMacro(Increments,Vector3i)
void SetDataOrder(Order order = YXZ);
uLibGetMacro(DataOrder,Order)
bool IsInsideGrid(const Vector3i &v) const;
inline int Map(Vector3i index) const;
Vector3i UnMap(int index) const;
private:
Order m_DataOrder;
Vector3i m_Dims;
Vector3i m_Increments; //TODO: make this line matrix //
};
// --- INLINES -------------------------------------------------------------- //
inline int StructuredData::Map(Vector3i index) const
{
return (m_Increments.transpose() * index);
}
}
#endif // STRUCTUREDDATA_H

View File

@@ -0,0 +1,87 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "StructuredGrid.h"
//namespace uLib {
//ImageData::ImageData(const Vector3i size) :
// StructuredData(size)
//{}
//void ImageData::SetSize(const Vector3f v)
//{
// ContainerBox::SetSize( v.array() / this->GetDims().array().cast<float>() );
//}
//Vector3f ImageData::GetSize() const
//{
// return ContainerBox::GetSize().array() * this->GetDims().array().cast<float>();
//}
//void ImageData::SetSpacing(const Vector3f spacing)
//{
// ContainerBox::SetSize(spacing);
//}
//Vector3f ImageData::GetSpacing() const
//{
// return ContainerBox::GetSize();
//}
//bool ImageData::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 ImageData::Find(const Vector4f pt) const
//{
// Vector3i out;
// Vector4f p = GetLocalPoint(pt);
// out << (int)floor(p(0)),
// (int)floor(p(1)),
// (int)floor(p(2));
// return out;
//}
//void ImageData::PrintSelf(std::ostream &o)
//{
// o << "uLib Structured Grid: \n"
// << "dim = " << this->GetDims().transpose() << "\n"
// << "spacing = " << this->GetSpacing().transpose() << "\n";
//}
//}

65
src/Math/StructuredGrid.h Normal file
View File

@@ -0,0 +1,65 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_MATH_STRUCTUREDGRID_H
#define U_MATH_STRUCTUREDGRID_H
//#include "Core/Object.h"
//#include "Math/ContainerBox.h"
//#include "Math/StructuredData.h"
//namespace uLib {
//class ImageData : public ContainerBox, public StructuredData {
// typedef ContainerBox BaseClass;
//public:
// ImageData(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);
//};
//}
#endif // STRUCTUREDGRID_H

141
src/Math/Transform.h Normal file
View File

@@ -0,0 +1,141 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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.
//////////////////////////////////////////////////////////////////////////////*/
/*
* Copyright (C) 2012 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 2.1 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; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#ifndef U_TRANSFORM_H
#define U_TRANSFORM_H
#include <Core/Macros.h>
#include <Eigen/Geometry>
namespace uLib {
////////////////////////////////////////////////////////////////////////////////
///////// AFFINE TRANSFORM WRAPPER //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
class AffineTransform {
protected:
Eigen::Affine3f m_T;
AffineTransform *m_Parent;
public:
AffineTransform() :
m_T(Matrix4f::Identity()),
m_Parent(NULL)
{}
AffineTransform(AffineTransform *parent) :
m_T(Matrix4f::Identity()),
m_Parent(parent)
{}
AffineTransform(const AffineTransform &copy) :
m_T(copy.m_T),
m_Parent(copy.m_Parent)
{}
Eigen::Affine3f& GetTransform() { return m_T; }
uLibGetSetMacro(Parent,AffineTransform *)
inline void SetMatrix (Matrix4f &mat) { m_T.matrix() = mat; }
inline Matrix4f& GetMatrix () { return m_T.matrix(); }
Matrix4f GetWorldMatrix() const
{
if(!m_Parent) return m_T.matrix();
else return m_Parent->GetWorldMatrix() * m_T.matrix(); // T = B * A //
}
inline void SetPosition(const Vector3f &v) { this->m_T.translation() = v; }
inline Vector3f GetPosition() const { return this->m_T.translation(); }
inline void SetRotation(const Matrix3f &m) { this->m_T.linear() = m; }
inline Matrix3f GetRotation() const { return this->m_T.rotation(); }
inline Vector3f GetOrientation(uint i0 = 0, uint i1 = 1, uint i2 = 2) const { return this->m_T.linear().eulerAngles(i0,i1,i2); }
inline void Translate(const Vector3f &v) { this->m_T.translate(v); }
inline void Scale(const Vector3f &v) { this->m_T.scale(v); }
inline Vector3f GetScale() const { return this->m_T.linear() * Vector3f(1,1,1); } // FIXXXXXXX
inline void Rotate(const Matrix3f &m) { this->m_T.rotate(m); }
inline void PreRotate(const Matrix3f &m) { this->m_T.prerotate(m); }
inline void QuaternionRotate(const Vector4f &q)
{ this->m_T.rotate(Eigen::Quaternion<float>(q)); }
inline void EulerYZYRotate(const Vector3f &e) {
Matrix3f mat;
mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY())
* Eigen::AngleAxisf(e.y(), Vector3f::UnitZ())
* Eigen::AngleAxisf(e.z(), Vector3f::UnitY());
m_T.rotate(mat);
}
inline void FlipAxes(int first, int second)
{
Matrix3f mat = Matrix3f::Identity();
mat.col(first).swap(mat.col(second));
m_T.rotate(mat);
}
};
}
#endif//U_TRANSFORM_H

69
src/Math/TriangleMesh.cpp Normal file
View File

@@ -0,0 +1,69 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "TriangleMesh.h"
namespace uLib {
void TriangleMesh::PrintSelf(std::ostream &o)
{
o << " // ------- TRIANGLE MESH ------- // \n" ;
o << " #Points : " << m_Points.size() << "\n";
o << " #Triang : " << m_Triangles.size() << "\n";
for(int i=0; i < m_Triangles.size(); ++i ) {
o << " - triangle[" << i << "]" <<
" " << m_Triangles[i](0) <<
"->(" << m_Points[m_Triangles[i](0)].transpose() << ") " <<
" " << m_Triangles[i](1) <<
"->(" << m_Points[m_Triangles[i](1)].transpose() << ") " <<
" " << m_Triangles[i](2) <<
"->(" << m_Points[m_Triangles[i](2)].transpose() << ") " <<
" \n";
}
o << " // ----------------------------- // \n";
}
void TriangleMesh::AddPoint(const Vector3f &pt)
{
this->m_Points.push_back(pt);
}
void TriangleMesh::AddTriangle(const Id_t *id)
{
Vector3i triangle(id[0],id[1],id[2]);
this->m_Triangles.push_back(triangle);
}
void TriangleMesh::AddTriangle(const Vector3i &id)
{
this->m_Triangles.push_back(id);
}
}

59
src/Math/TriangleMesh.h Normal file
View File

@@ -0,0 +1,59 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 TRIANGLEMESH_H
#define TRIANGLEMESH_H
#include "Core/Object.h"
#include "Core/Vector.h"
#include "Math/Dense.h"
namespace uLib {
class TriangleMesh : public Object {
public:
void PrintSelf(std::ostream &o);
void AddPoint(const Vector3f &pt);
void AddTriangle(const Id_t *id);
void AddTriangle(const Vector3i &id);
uLibRefMacro(Points,Vector<Vector3f>)
uLibRefMacro(Triangles,Vector<Vector3i>)
private:
Vector<Vector3f> m_Points;
Vector<Vector3i> m_Triangles;
};
}
#endif // TRIANGLEMESH_H

50
src/Math/Utils.h Normal file
View File

@@ -0,0 +1,50 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_UTILS_H
#define U_UTILS_H
#include <math.h>
#include <limits>
#include <float.h>
#define fastSign(x) ((x>0)-(x<0))
namespace uLib {
inline bool isFinite(float x) {
return (x<=FLT_MAX && x>= -FLT_MAX);
}
inline bool isFinite(double x) {
return (x<=DBL_MAX && x>= -DBL_MAX);
}
}
#endif // UTILS_H

70
src/Math/VectorSpace.h Normal file
View File

@@ -0,0 +1,70 @@
#ifndef U_GEO_VECTORSPACE_H
#define U_GEO_VECTORSPACE_H
#include "Core/Macros.h"
#include "Core/Mpl.h"
#include "Math/Dense.h"
#include "Math/Line.h"
//#include "boost/geometry.hpp"
//#include "boost/geometry/geometries/adapted/c_array.hpp"
//#include "boost/geometry/geometries/adapted/boost_array.hpp"
//#include "boost/geometry/geometries/adapted/std_pair_as_segment.hpp"
//#include "boost/geometry/geometries/register/point.hpp"
//#include "boost/geometry/geometries/register/linestring.hpp"
namespace uLib {
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// VECTOR SPACE ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// VECTOR UTILS ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
inline Vector4f HVector3f(float x, float y, float z) { return Vector4f(x,y,z,0); }
inline Vector4f HVector3f(const Vector4f &v) { return Vector4f(v(0),v(1),v(2),0); }
inline Vector4f HPoint3f(float x, float y, float z) { return Vector4f(x,y,z,1); }
inline Vector4f HPoint3f(const Vector4f &v) { return Vector4f(v(0),v(1),v(2),1); }
// this is an example of function imported from eigen //
//using boost::geometry::distance;
} // uLib
// BOOST GEOMETRY REGISTRATION //
//BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4i, int, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4f, float, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_2D(uLib::Vector2d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector3d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
//BOOST_GEOMETRY_REGISTER_POINT_3D(uLib::Vector4d, double, boost::geometry::cs::cartesian, operator()(0), operator()(1), operator()(2))
#endif // VECTORSPACE_H

192
src/Math/VoxImage.cpp Normal file
View File

@@ -0,0 +1,192 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 <stdlib.h>
#include <stdio.h>
#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);
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 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"));
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 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);
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);
}
}

286
src/Math/VoxImage.h Normal file
View File

@@ -0,0 +1,286 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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_MATH_VOXIMAGE_H
#define U_MATH_VOXIMAGE_H
#include "Core/Vector.h"
#include "Core/StaticInterface.h"
#include "Math/Dense.h"
#include "Math/ImageData.h"
#include <iostream>
#include <stdlib.h>
namespace uLib {
////////////////////////////////////////////////////////////////////////////////
// ABSTRACT VOX IMAGE //////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
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 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);
protected:
virtual ~VoxImage() {}
VoxImage(const Vector3i &size) : BaseClass(size) {}
};
}
////////////////////////////////////////////////////////////////////////////////
// VOXEL ////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
struct Voxel {
Scalarf Value;
float & operator()() { return Value; }
const float & operator()() const { return Value; }
};
template <typename T>
std::basic_ostream<T> & operator << (std::basic_ostream<T> &o, const Voxel &v) {
o << v.Value;
return o;
}
////////////////////////////////////////////////////////////////////////////////
// VOX IMAGE /////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
template< typename T >
class VoxImage : public Abstract::VoxImage {
public:
typedef Abstract::VoxImage BaseClass;
VoxImage();
VoxImage(const Vector3i &size);
VoxImage(const VoxImage<T> &copy) :
BaseClass(copy)
{
this->m_Data = copy.m_Data;
}
uLibRefMacro(Data,Vector<T>)
inline const Vector<T>& ConstData() const { return m_Data; }
inline const T& At(int i) const { return m_Data.at(i); }
inline const T& At(const Vector3i &id) const { return m_Data.at(Map(id)); }
inline T& At(int i) { return m_Data.at(i); }
inline T& At(const Vector3i &id) { return m_Data.at(Map(id)); }
inline const T& operator[](unsigned int i) const { return m_Data[i]; }
inline const T& operator[](const Vector3i &id) const { return m_Data[Map(id)]; }
inline T& operator[](unsigned int i) { return m_Data[i]; }
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 SetDims(const Vector3i &size) {
this->m_Data.resize(size.prod());
StructuredData::SetDims(size);
}
inline VoxImage<T> clipImage(const Vector3i begin, const Vector3i end) const;
inline VoxImage<T> clipImage(const Vector4f begin, const Vector4f end) const;
inline VoxImage<T> clipImage(const float density) const;
inline void SelectScalarfComponent(T &element, Scalarf *scalar);
inline void InitVoxels(T t);
// MATH OPERATORS //
inline void operator *=(Scalarf scalar) {
for(unsigned int i = 0; i < m_Data.size(); ++i)
m_Data[i].Value *= scalar;
}
inline void operator +=(Scalarf scalar) {
for(unsigned int i = 0; i < m_Data.size(); ++i)
m_Data[i].Value += scalar;
}
inline void operator /=(Scalarf scalar) {
for(unsigned int i = 0; i < m_Data.size(); ++i)
m_Data[i].Value /= scalar;
}
inline void operator -=(Scalarf scalar) {
for(unsigned int i = 0; i < m_Data.size(); ++i)
m_Data[i].Value -= scalar;
}
// MATH VoxImage Operators //
template <typename S>
void operator +=(VoxImage<S> &sibling) {
if (this->GetDims() != sibling.GetDims()) {
//printf("Warning when adding VoxImages: I'm NOT doing it!\n");
return;
}// WARNING! You must Warn the user!
for(unsigned int i = 0; i < m_Data.size(); ++i) {
m_Data[i].Value += sibling.At(i).Value;
}
}
template <typename S>
void operator -=(VoxImage<S> &sibling) {
if (this->GetDims() != sibling.GetDims()) {
//printf("Warning when subtracting VoxImages: I'm NOT doing it!\n");
return;
}// WARNING! You must Warn the user!
for(unsigned int i = 0; i < m_Data.size(); ++i) {
m_Data[i].Value -= sibling.At(i).Value;
}
}
template <typename S>
void operator *=(VoxImage<S> &sibling) {
if (this->GetDims() != sibling.GetDims()) {
//printf("Warning when multiplying VoxImages: I'm NOT doing it!\n");
return;
}// WARNING! You must Warn the user!
for(unsigned int i = 0; i < m_Data.size(); ++i) {
m_Data[i].Value *= sibling.At(i).Value;
}
}
template <typename S>
void operator /=(VoxImage<S> &sibling) {
if (this->GetDims() != sibling.GetDims()) {
//printf("Warning when dividing VoxImages: I'm NOT doing it!\n");
return;
}// WARNING! You must Warn the user!
for(unsigned int i = 0; i < m_Data.size(); ++i) {
m_Data[i].Value /= sibling.At(i).Value;
}
}
private:
Vector<T> m_Data;
};
template<typename T>
VoxImage<T>::VoxImage() :
m_Data(0),
BaseClass(Vector3i(0,0,0)) {}
//{ Interface::IsA <T,Interface::Voxel>(); /* structural check for T */ }
template<typename T>
VoxImage<T>::VoxImage(const Vector3i &size) :
m_Data(size.prod()),
BaseClass(size) {}
//{ Interface::IsA <T,Interface::Voxel>(); /* structural check for T */ }
template <typename T>
VoxImage<T> VoxImage<T>::clipImage(const Vector3i begin, const Vector3i end) const
{
Vector3i dim = (end-begin)+Vector3i(1,1,1);
VoxImage<T> out(*this);
out.SetDims(dim);
// WARNING Origin or Position???
out.SetPosition(this->GetPosition() + this->GetSpacing().cwiseProduct(begin.cast<float>()) );
for(uint x = 0; x<dim(0); ++x )
for(uint y = 0; y<dim(1); ++y )
for(uint z = 0; z<dim(2); ++z )
{
Vector3i id = Vector3i(x,y,z);
out[id] = this->At(begin + id);
}
return out;
}
template <typename T>
VoxImage<T> VoxImage<T>::clipImage(const Vector4f begin, const Vector4f end) const
{
Vector3i v1 = this->Find(begin);
Vector3i v2 = this->Find(end);
return this->clipImage(v1,v2);
}
template <typename T>
VoxImage<T> VoxImage<T>::clipImage(const float density) const
{
Vector3i v1 = this->GetDims();
Vector3i v2 = Vector3i(0,0,0);
for(uint i=0; i< this->m_Data.size(); ++i) {
if( this->GetValue(i) >= density ) {
Vector3i id = this->UnMap(i);
v1 = v1.array().min(id.array());
v2 = v2.array().max(id.array());
}
}
return this->clipImage(v1,v2);
}
template<typename T>
void VoxImage<T>::InitVoxels(T t)
{
std::fill( m_Data.begin(), m_Data.end(), t ); // warning... stl function //
}
} // uLib
#endif // VOXIMAGE_H

129
src/Math/VoxImageFilter.h Normal file
View File

@@ -0,0 +1,129 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXIMAGEFILTER_H
#define VOXIMAGEFILTER_H
#include "Core/StaticInterface.h"
#include "Core/Object.h"
#include "Math/Dense.h"
#include "Math/VoxImage.h"
namespace uLib {
namespace Interface {
struct VoxImageFilterShape {
template <class Self> void check_structural() {
uLibCheckFunction(Self,operator(),float,float);
uLibCheckFunction(Self,operator(),float,const Vector3f&);
}
};
}
template < typename VoxelT > class Kernel;
namespace Abstract {
class VoxImageFilter {
public:
virtual void Run() = 0;
virtual void SetImage(Abstract::VoxImage *image) = 0;
protected:
virtual ~VoxImageFilter() {}
};
}
template < typename VoxelT, typename AlgorithmT >
class VoxImageFilter : public Abstract::VoxImageFilter, public Object
{
public:
VoxImageFilter(const Vector3i &size);
void Run();
void SetKernelNumericXZY(const Vector<float> &numeric);
void SetKernelSpherical(float (*shape)(float));
template < class ShapeT >
void SetKernelSpherical( ShapeT shape );
void SetKernelWeightFunction(float (*shape)(const Vector3f &));
template < class ShapeT >
void SetKernelWeightFunction( ShapeT shape );
uLibGetMacro(KernelData,Kernel<VoxelT>)
uLibGetMacro(Image,VoxImage<VoxelT> *)
void SetImage(Abstract::VoxImage *image);
protected:
float Convolve(const VoxImage<VoxelT> &buffer, int index); // remove //
void SetKernelOffset();
float Distance2(const Vector3i &v);
// protected members for algorithm access //
Kernel<VoxelT> m_KernelData;
VoxImage<VoxelT> *m_Image;
private:
AlgorithmT *t_Algoritm;
};
}
#endif // VOXIMAGEFILTER_H
#include "VoxImageFilter.hpp"
#include "VoxImageFilterLinear.hpp"
#include "VoxImageFilterThreshold.hpp"
#include "VoxImageFilterMedian.hpp"
#include "VoxImageFilterABTrim.hpp"
#include "VoxImageFilterBilateral.hpp"
#include "VoxImageFilter2ndStat.hpp"
#include "VoxImageFilterCustom.hpp"

302
src/Math/VoxImageFilter.hpp Normal file
View File

@@ -0,0 +1,302 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXIMAGEFILTER_HPP
#define VOXIMAGEFILTER_HPP
#include <Math/Dense.h>
#include "Math/StructuredData.h"
#include "Math/VoxImage.h"
#include "VoxImageFilter.h"
namespace uLib {
// KERNEL //////////////////////////////////////////////////////////////////////
template < typename T >
class Kernel : public StructuredData {
typedef StructuredData BaseClass;
public:
Kernel(const Vector3i &size);
inline T& operator[](const Vector3i &id) { return m_Data[Map(id)]; }
inline T& operator[](const int &id) { return m_Data[id]; }
inline int GetCenterData() const;
uLibRefMacro(Data,Vector<T>)
inline const Vector<T>& ConstData() const { return this->m_Data; }
void PrintSelf(std::ostream &o) const;
private:
Vector<T> m_Data;
};
template < typename T >
Kernel<T>::Kernel(const Vector3i &size) :
BaseClass(size),
m_Data(size.prod())
{
Interface::IsA<T,Interface::Voxel>();
}
template < typename T >
inline int Kernel<T>::GetCenterData() const
{
static int center = Map(this->GetDims() / 2);
return center;
}
template < typename T >
void Kernel<T>::PrintSelf(std::ostream &o) const
{
o << " Filter Kernel Dump [XZ_Y]: \n";
Vector3i index;
o << "\n Value: \n\n"
<< "------------------------------------------------- \n";
for (int y = 0 ; y < this->GetDims()(1); ++y ) {
o << "[y=" << y << "]\n";
for (int z = 0 ; z < this->GetDims()(2); ++z ) {
for (int x = 0 ; x < this->GetDims()(0); ++x ) {
index << x,y,z;
o << m_Data[Map(index)].Value << " ";
} o << "\n";
} o << " --------------------------------------------------- \n";
}
o << "\n Offset: \n"
<< "------------------------------------------------- \n";
for (int y = 0 ; y < this->GetDims()(1); ++y ) {
o << "[y=" << y << "]\n";
for (int z = 0 ; z < this->GetDims()(2); ++z ) {
for (int x = 0 ; x < this->GetDims()(0); ++x ) {
index << x,y,z;
o << m_Data[Map(index)].Count << " ";
} o << "\n";
} o << " --------------------------------------------------- \n";
}
}
////////////////////////////////////////////////////////////////////////////////
#define _TPL_ template < typename VoxelT , typename AlgorithmT >
#define _TPLT_ VoxelT,AlgorithmT
_TPL_
VoxImageFilter<_TPLT_>::VoxImageFilter(const Vector3i &size) :
m_KernelData(size),
t_Algoritm(static_cast<AlgorithmT *>(this))
{
}
_TPL_
void VoxImageFilter<_TPLT_>::Run()
{
VoxImage<VoxelT> buffer = *m_Image;
#pragma omp parallel for
for(int i=0 ; i < m_Image->Data().size() ; ++i)
m_Image->operator [](i).Value = this->t_Algoritm->Evaluate(buffer,i);
#pragma omp barrier
}
_TPL_
void VoxImageFilter<_TPLT_>::SetKernelOffset()
{
Vector3i id(0,0,0);
for( int z=0 ; z < m_KernelData.GetDims()(2); ++z ) {
for( int x=0 ; x < m_KernelData.GetDims()(0); ++x ) {
for( int y=0 ; y < m_KernelData.GetDims()(1); ++y ) {
id << x,y,z;
m_KernelData[id].Count = id.transpose() * m_Image->GetIncrements();
}
}
}
}
_TPL_
float VoxImageFilter<_TPLT_>::Distance2(const Vector3i &v)
{
Vector3i tmp = v;
const Vector3i &dim = this->m_KernelData.GetDims();
Vector3i center = dim / 2;
tmp = tmp - center;
center = center.cwiseProduct(center);
tmp = tmp.cwiseProduct(tmp);
return (float)(tmp.sum()) / (float)( center.sum() + 0.25 *
(3 - (dim(0) % 2) - (dim(1) % 2) - (dim(2) % 2)));
}
_TPL_
void VoxImageFilter<_TPLT_>::SetKernelNumericXZY(const Vector<float> &numeric)
{
// set data order //
StructuredData::Order order = m_KernelData.GetDataOrder();
//m_KernelData.SetDataOrder(StructuredData::XZY);
Vector3i id;
int index = 0;
for( int y=0 ; y < m_KernelData.GetDims()(1); ++y ) {
for( int z=0 ; z < m_KernelData.GetDims()(2); ++z ) {
for( int x=0 ; x < m_KernelData.GetDims()(0); ++x ) {
id << x,y,z;
m_KernelData[id].Value = numeric[index++];
}
}
}
//m_KernelData.SetDataOrder(order);
}
_TPL_
void VoxImageFilter<_TPLT_>::SetKernelSpherical(float(* shape)(float))
{
Vector3i id;
for( int y=0 ; y < m_KernelData.GetDims()(1); ++y ) {
for( int z=0 ; z < m_KernelData.GetDims()(2); ++z ) {
for( int x=0 ; x < m_KernelData.GetDims()(0); ++x ) {
id << x,y,z;
m_KernelData[id].Value = shape(this->Distance2(id));
}
}
}
}
_TPL_ template <class ShapeT>
void VoxImageFilter<_TPLT_>::SetKernelSpherical(ShapeT shape)
{
Interface::IsA<ShapeT,Interface::VoxImageFilterShape>();
Vector3i id;
for( int y=0 ; y < m_KernelData.GetDims()(1); ++y ) {
for( int z=0 ; z < m_KernelData.GetDims()(2); ++z ) {
for( int x=0 ; x < m_KernelData.GetDims()(0); ++x ) {
id << x,y,z;
m_KernelData[id].Value = shape(this->Distance2(id));
}
}
}
}
_TPL_
void VoxImageFilter<_TPLT_>::SetKernelWeightFunction(float (*shape)(const Vector3f &))
{
const Vector3i &dim = m_KernelData.GetDims();
Vector3i id;
Vector3f pt;
for( int y=0 ; y < dim(1); ++y ) {
for( int z=0 ; z < dim(2); ++z ) {
for( int x=0 ; x < dim(0); ++x ) {
// get voxels centroid coords from kernel center //
id << x,y,z;
pt << id(0) - dim(0)/2 + 0.5 * !(dim(0) % 2),
id(1) - dim(1)/2 + 0.5 * !(dim(1) % 2),
id(2) - dim(2)/2 + 0.5 * !(dim(2) % 2);
// compute function using given shape //
m_KernelData[id].Value = shape(pt);
}
}
}
}
_TPL_ template < class ShapeT >
void VoxImageFilter<_TPLT_>::SetKernelWeightFunction(ShapeT shape)
{
Interface::IsA<ShapeT,Interface::VoxImageFilterShape>();
const Vector3i &dim = m_KernelData.GetDims();
Vector3i id;
Vector3f pt;
for( int y=0 ; y < dim(1); ++y ) {
for( int z=0 ; z < dim(2); ++z ) {
for( int x=0 ; x < dim(0); ++x ) {
// get voxels centroid coords from kernel center //
id << x,y,z;
pt << id(0) - dim(0)/2 + 0.5 * !(dim(0) % 2),
id(1) - dim(1)/2 + 0.5 * !(dim(1) % 2),
id(2) - dim(2)/2 + 0.5 * !(dim(2) % 2);
// compute function using given shape //
m_KernelData[id].Value = shape(pt);
}
}
}
}
_TPL_
void VoxImageFilter<_TPLT_>::SetImage(Abstract::VoxImage *image)
{
this->m_Image = reinterpret_cast<VoxImage<VoxelT> *> (image);
this->SetKernelOffset();
}
_TPL_
float VoxImageFilter<_TPLT_>::Convolve(const VoxImage<VoxelT> &buffer, int index)
{
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = m_KernelData.ConstData();
int vox_size = vbuf.size();
int ker_size = vker.size();
int pos;
float conv = 0, ksum = 0;
for (int ik = 0; ik < ker_size; ++ik) {
pos = index + vker[ik].Count - vker[m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
conv += vbuf[pos].Value * vker[ik].Value;
ksum += vker[ik].Value;
}
return conv / ksum;
}
#undef _TPLT_
#undef _TPL_
}
#endif // VOXIMAGEFILTER_HPP

View File

@@ -0,0 +1,83 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXIMAGEFILTER2NDSTAT_HPP
#define VOXIMAGEFILTER2NDSTAT_HPP
#include <Math/Dense.h>
#include "Math/VoxImage.h"
#include "VoxImageFilter.h"
////////////////////////////////////////////////////////////////////////////////
///// VOXIMAGE FILTER ABTRIM /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
namespace uLib {
template <typename VoxelT>
class VoxFilterAlgorithm2ndStat :
public VoxImageFilter<VoxelT, VoxFilterAlgorithm2ndStat<VoxelT> > {
public:
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithm2ndStat<VoxelT> > BaseClass;
VoxFilterAlgorithm2ndStat(const Vector3i &size) :
BaseClass(size)
{ }
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = this->m_KernelData.ConstData();
int vox_size = vbuf.size();
int ker_size = vker.size();
int pos;
// mean //
float conv = 0, ksum = 0;
for (int ik = 0; ik < ker_size; ++ik) {
pos = index + vker[ik].Count - vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
conv += vbuf[pos].Value * vker[ik].Value;
ksum += vker[ik].Value;
}
float mean = conv / ksum;
// rms //
conv = 0;
for (int ik = 0; ik < ker_size; ++ik) {
pos = index + vker[ik].Count - vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
conv += pow((vbuf[pos].Value * vker[ik].Value) - mean , 2);
}
return conv / (vker.size() - 1) ;
}
};
}
#endif // VOXIMAGEFILTER2NDSTAT_HPP

View File

@@ -0,0 +1,179 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXIMAGEFILTERABTRIM_HPP
#define VOXIMAGEFILTERABTRIM_HPP
#include <Math/Dense.h>
#include "Math/VoxImage.h"
#include "VoxImageFilter.h"
////////////////////////////////////////////////////////////////////////////////
///// VOXIMAGE FILTER ABTRIM /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
namespace uLib {
template <typename VoxelT>
class VoxFilterAlgorithmAbtrim :
public VoxImageFilter<VoxelT, VoxFilterAlgorithmAbtrim<VoxelT> > {
struct KernelSortAscending
{
bool operator()(const VoxelT& e1, const VoxelT& e2)
{ return e1.Value < e2.Value; }
};
public:
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithmAbtrim<VoxelT> > BaseClass;
VoxFilterAlgorithmAbtrim(const Vector3i &size) :
BaseClass(size)
{
mAtrim = 0;
mBtrim = 0;
}
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = this->m_KernelData.ConstData();
int vox_size = vbuf.size();
int ker_size = vker.size();
int pos;
Vector<VoxelT> mfh(ker_size);
for (int i = 0; i < ker_size; ++i)
mfh[i].Count = i; //index key for ordering function
for (int ik = 0; ik < ker_size; ik++) {
pos = index + vker[ik].Count - vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
mfh[ik].Value = vbuf[pos].Value;
}
std::sort(mfh.begin(), mfh.end(), KernelSortAscending());
float ker_sum = 0;
float fconv = 0;
for (int ik = 0; ik < mAtrim; ik++)
ker_sum += vker[ mfh[ik].Count ].Value;
for (int ik = mAtrim; ik < ker_size - mBtrim; ik++) {
fconv += mfh[ik].Value * vker[ mfh[ik].Count ].Value; // convloution //
ker_sum += vker[ mfh[ik].Count ].Value;
}
for (int ik = ker_size - mBtrim; ik < ker_size; ik++)
ker_sum += vker[ mfh[ik].Count ].Value;
return fconv / ker_sum;
}
inline void SetABTrim(int a, int b) { mAtrim = a; mBtrim = b; }
private:
int mAtrim;
int mBtrim;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Roberspierre Filter //
template <typename VoxelT>
class VoxFilterAlgorithmSPR :
public VoxImageFilter<VoxelT, VoxFilterAlgorithmSPR<VoxelT> > {
struct KernelSortAscending
{
bool operator()(const VoxelT& e1, const VoxelT& e2)
{ return e1.Value < e2.Value; }
};
public:
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithmSPR<VoxelT> > BaseClass;
VoxFilterAlgorithmSPR(const Vector3i &size) :
BaseClass(size)
{
mAtrim = 0;
mBtrim = 0;
}
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = this->m_KernelData.ConstData();
int vox_size = vbuf.size();
int ker_size = vker.size();
int pos;
Vector<VoxelT> mfh(ker_size);
for (int i = 0; i < ker_size; ++i)
mfh[i].Count = i; //index key for ordering function
for (int ik = 0; ik < ker_size; ik++) {
pos = index + vker[ik].Count -
vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
mfh[ik].Value = vbuf[pos].Value;
}
std::sort(mfh.begin(), mfh.end(), KernelSortAscending());
float spr = vbuf[index].Value;
if( (mAtrim > 0 && spr <= mfh[mAtrim-1].Value) ||
(mBtrim > 0 && spr >= mfh[ker_size - mBtrim].Value) )
{
float ker_sum = 0;
float fconv = 0;
for (int ik = 0; ik < mAtrim; ik++)
ker_sum += vker[ mfh[ik].Count ].Value;
for (int ik = mAtrim; ik < ker_size - mBtrim; ik++) {
fconv += mfh[ik].Value * vker[ mfh[ik].Count ].Value;
ker_sum += vker[ mfh[ik].Count ].Value;
}
for (int ik = ker_size - mBtrim; ik < ker_size; ik++)
ker_sum += vker[ mfh[ik].Count ].Value;
return fconv / ker_sum;
}
else
return spr;
}
inline void SetABTrim(int a, int b) { mAtrim = a; mBtrim = b; }
private:
int mAtrim;
int mBtrim;
};
}
#endif // VOXIMAGEFILTERABTRIM_HPP

View File

@@ -0,0 +1,152 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXIMAGEFILTERBILATERAL_HPP
#define VOXIMAGEFILTERBILATERAL_HPP
#include <Math/Dense.h>
#include "Math/VoxImage.h"
#include "VoxImageFilter.h"
////////////////////////////////////////////////////////////////////////////////
///// VOXIMAGE FILTER LINEAR /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
namespace uLib {
template <typename VoxelT>
class VoxFilterAlgorithmBilateral :
public VoxImageFilter<VoxelT, VoxFilterAlgorithmBilateral<VoxelT> > {
public:
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithmBilateral<VoxelT> > BaseClass;
VoxFilterAlgorithmBilateral(const Vector3i &size) : BaseClass(size) {
m_sigma = 1;
}
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = this->m_KernelData.ConstData();
int vox_size = vbuf.size();
int ker_size = vker.size();
int pos;
float conv = 0, ksum = 0;
float gamma_smooth;
for (int ik = 0; ik < ker_size; ++ik) {
// if (ik==this->m_KernelData.GetCenterData()) continue;
pos = index + vker[ik].Count - vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
gamma_smooth = compute_gauss( fabs(vbuf[index].Value - vbuf[pos].Value) * 1.E6 );
conv += vbuf[pos].Value * vker[ik].Value * gamma_smooth;
ksum += vker[ik].Value * gamma_smooth;
}
return conv / ksum;
}
inline void SetIntensitySigma(const float s) { m_sigma = s; }
private:
inline float compute_gauss(const float x) {
return 1/(sqrt(2*M_PI)* m_sigma) * exp(-0.5*(x*x)/(m_sigma*m_sigma));
}
Scalarf m_sigma;
};
template <typename VoxelT>
class VoxFilterAlgorithmBilateralTrim :
public VoxImageFilter<VoxelT, VoxFilterAlgorithmBilateralTrim<VoxelT> > {
typedef std::pair<float,float> FPair;
struct KernelSortAscending
{
bool operator()(const FPair& e1, const FPair& e2)
{ return e1.second < e2.second; }
};
public:
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithmBilateralTrim<VoxelT> > BaseClass;
VoxFilterAlgorithmBilateralTrim(const Vector3i &size) : BaseClass(size) {
m_sigma = 1;
mAtrim = 0;
mBtrim = 0;
}
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = this->m_KernelData.ConstData();
int img_size = vbuf.size();
int ker_size = vker.size();
int pos;
Vector<FPair> mfh(ker_size);
for (int i = 0; i < ker_size; ++i)
mfh[i].first = vker[i].Value; // kernel value in first
for (int ik = 0; ik < ker_size; ik++) {
pos = index + vker[ik].Count - vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + img_size) % img_size;
mfh[ik].second = vbuf[pos].Value; // image value in second
}
std::sort(mfh.begin(), mfh.end(), KernelSortAscending());
float conv = 0, ksum = 0;
float gamma_smooth;
// for (int ik = 0; ik < mAtrim; ik++)
// ksum += mfh[ik].first;
for (int ik = mAtrim; ik < ker_size - mBtrim; ik++) {
gamma_smooth = compute_gauss( fabs(vbuf[index].Value - mfh[ik].second) * 1.E6 );
conv += mfh[ik].first * mfh[ik].second * gamma_smooth;
ksum += mfh[ik].first * gamma_smooth;
}
// for (int ik = ker_size - mBtrim; ik < ker_size; ik++)
// ksum += mfh[ik].first;
return conv / ksum;
}
inline void SetIntensitySigma(const float s) { m_sigma = s; }
inline void SetABTrim(int a, int b) { mAtrim = a; mBtrim = b; }
private:
inline float compute_gauss(const float x) {
return 1/(sqrt(2*M_PI)* m_sigma) * exp(-0.5*(x*x)/(m_sigma*m_sigma));
}
Scalarf m_sigma;
int mAtrim;
int mBtrim;
};
}
#endif // VOXIMAGEFILTERBILATERAL_HPP

View File

@@ -0,0 +1,88 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXIMAGEFILTERCUSTOM_HPP
#define VOXIMAGEFILTERCUSTOM_HPP
#include <Math/Dense.h>
#include "Math/VoxImage.h"
#include "VoxImageFilter.h"
////////////////////////////////////////////////////////////////////////////////
///// VOXIMAGE FILTER CUSTOM /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
namespace uLib {
template <typename VoxelT>
class VoxFilterAlgorithmCustom :
public VoxImageFilter<VoxelT, VoxFilterAlgorithmCustom<VoxelT> > {
typedef float (* FunctionPt)(const Vector<Scalarf> &);
public:
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithmCustom<VoxelT> > BaseClass;
VoxFilterAlgorithmCustom(const Vector3i &size) :
BaseClass(size), m_CustomEvaluate(NULL)
{}
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
if(likely(m_CustomEvaluate)) {
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = this->m_KernelData.ConstData();
int vox_size = vbuf.size();
int ker_size = vker.size();
int pos;
float ker_sum = 0;
Vector<Scalarf> mfh(ker_size);
for (int ik = 0; ik < ker_size; ik++) {
pos = index + vker[ik].Count - vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
mfh[ik] = vbuf[pos].Value * vker[ik].Value;
ker_sum += vker[ik].Value;
}
return this->m_CustomEvaluate(mfh);
}
else
std::cerr << "Custom evaluate function is NULL \n" <<
"No operation performed by filter.\n";
}
uLibSetMacro(CustomEvaluate,FunctionPt)
private:
FunctionPt m_CustomEvaluate;
};
}
#endif // VOXIMAGEFILTERCUSTOM_HPP

View File

@@ -0,0 +1,69 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXIMAGEFILTERLINEAR_HPP
#define VOXIMAGEFILTERLINEAR_HPP
#include <Math/Dense.h>
#include "Math/VoxImage.h"
#include "VoxImageFilter.h"
////////////////////////////////////////////////////////////////////////////////
///// VOXIMAGE FILTER LINEAR /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
namespace uLib {
template <typename VoxelT>
class VoxFilterAlgorithmLinear :
public VoxImageFilter<VoxelT, VoxFilterAlgorithmLinear<VoxelT> > {
public:
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithmLinear<VoxelT> > BaseClass;
VoxFilterAlgorithmLinear(const Vector3i &size) : BaseClass(size) {}
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = this->m_KernelData.ConstData();
int vox_size = vbuf.size();
int ker_size = vker.size();
int pos;
float conv = 0, ksum = 0;
for (int ik = 0; ik < ker_size; ++ik) {
pos = index + vker[ik].Count - vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
conv += vbuf[pos].Value * vker[ik].Value;
ksum += vker[ik].Value;
}
return conv / ksum;
}
};
}
#endif // VOXIMAGEFILTERLINEAR_HPP

View File

@@ -0,0 +1,75 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXIMAGEFILTERMEDIAN_HPP
#define VOXIMAGEFILTERMEDIAN_HPP
#include <Math/Dense.h>
#include "Math/VoxImage.h"
#include "VoxImageFilter.h"
////////////////////////////////////////////////////////////////////////////////
///// VOXIMAGE FILTER MEDIAN /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
namespace uLib {
template <typename VoxelT>
class VoxFilterAlgorithmMedian :
public VoxImageFilter<VoxelT, VoxFilterAlgorithmMedian<VoxelT> > {
public:
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithmMedian<VoxelT> > BaseClass;
VoxFilterAlgorithmMedian(const Vector3i &size) : BaseClass(size) {}
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
const Vector<VoxelT> &vbuf = buffer.ConstData();
const Vector<VoxelT> &vker = this->m_KernelData.ConstData();
int vox_size = vbuf.size();
int ker_size = vker.size();
int pos;
Vector<float> mfh(ker_size);
for (int ik = 0; ik < ker_size; ik++) {
pos = index + vker[ik].Count - vker[this->m_KernelData.GetCenterData()].Count;
pos = (pos + vox_size) % vox_size;
mfh[ik] = vbuf[pos].Value * vker[ik].Value;
}
std::sort(mfh.begin(), mfh.end());
pos = 0;
// count zeroes in filter kernel to move it out of median //
for (int i = 0; i < ker_size; ++i)
if (vker[i].Value == 0.0) pos++;
// median //
pos += (ker_size - pos) / 2;
return mfh[pos];
}
};
}
#endif // VOXIMAGEFILTERMEDIAN_HPP

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.
//////////////////////////////////////////////////////////////////////////////*/
#ifndef VOXIMAGEFILTERTHRESHOLD_HPP
#define VOXIMAGEFILTERTHRESHOLD_HPP
#include <Math/Dense.h>
#include "Math/VoxImage.h"
#include "VoxImageFilter.h"
////////////////////////////////////////////////////////////////////////////////
///// VOXIMAGE FILTER THRESHOLD //////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
namespace uLib {
template <typename VoxelT>
class VoxFilterAlgorithmThreshold :
public VoxImageFilter<VoxelT, VoxFilterAlgorithmThreshold<VoxelT> > {
typedef VoxImageFilter<VoxelT, VoxFilterAlgorithmThreshold<VoxelT> > BaseClass;
// ULIB_OBJECT_PARAMETERS(BaseClass) {
// float threshold;
// };
float m_threshold;
public:
VoxFilterAlgorithmThreshold(const Vector3i &size) : BaseClass(size)
{
// init_parameters();
m_threshold = 0;
}
inline void SetThreshold(float th) { m_threshold = th; }
float Evaluate(const VoxImage<VoxelT> &buffer, int index)
{
return static_cast<float>(buffer.ConstData().at(index).Value >=
// parameters().threshold);
m_threshold );
}
};
//template <typename VoxelT>
//inline void VoxFilterAlgorithmThreshold<VoxelT>::init_parameters()
//{
// parameters().threshold = 0;
//}
}
#endif // VOXIMAGEFILTERTHRESHOLD_HPP

View File

@@ -0,0 +1,35 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "Core/Vector.h"
#include "Dense.h"
#include "VoxImage.h"
#include "VoxImageFilterUser.h"
using namespace uLib;

202
src/Math/VoxRaytracer.cpp Normal file
View File

@@ -0,0 +1,202 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "VoxRaytracer.h"
#include "Utils.h"
inline float fast_sign(float f) { return 1 - 2 * (f < 0); }
namespace uLib {
////////////////////////////////////////////////////////////////////////////////
///// RAY DATA /////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void VoxRaytracer::RayData::AddElement(Id_t id, float L)
{
Element el = {id, L};
m_Data.push_back(el);
m_TotalLength += L;
}
void VoxRaytracer::RayData::AppendRay(const VoxRaytracer::RayData &in)
{
if (unlikely(!in.m_Data.size())) {
std::cout << "Warinig: PoCA on exit border!\n";
return;
}
else if (unlikely(!m_Data.size())) {
m_Data = in.m_Data;
std::cout << "Warinig: PoCA on entrance border!\n";
return;
}
else {
// Opzione 1) un voxel in piu' //
m_Data.reserve(m_Data.size() + in.m_Data.size());
m_Data.insert(m_Data.end(), in.m_Data.begin(), in.m_Data.end());
// Opzione 2) merge dei voxel nel poca.
// RayData::Element &e1 = m_Data.back();
// const RayData::Element &e2 = in.m_Data.front();
// if(e1.vox_id == e2.vox_id)
// {
// m_Data.reserve(m_Data.size() + in.m_Data.size() - 1);
// e1.L += e2.L; //fix//
// m_Data.insert(m_Data.end(), in.m_Data.begin()+1, in.m_Data.end());
// }
// else {
// m_Data.reserve(m_Data.size() + in.m_Data.size());
// m_Data.insert(m_Data.end(), in.m_Data.begin(), in.m_Data.end());
// }
m_TotalLength += in.m_TotalLength;
}
}
void VoxRaytracer::RayData::PrintSelf(std::ostream &o)
{
o << "Ray: \n";
Vector<Element>::Iterator it;
for(it = m_Data.begin(); it < m_Data.end(); ++it)
o << "[ " << (*it).vox_id << ", " << (*it).L << "] \n";
}
////////////////////////////////////////////////////////////////////////////////
//// RAY TRACER ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
bool VoxRaytracer::GetEntryPoint(const HLine3f line, Vector4f &pt)
{
Vector4f s = m_Image->GetLocalPoint(line.direction());
pt = m_Image->GetLocalPoint(line.origin());
// Considers Structured grid dimensions //
Vector4f dims = m_Image->GetDims().homogeneous().cast<float>();
pt = pt.cwiseQuotient(dims);
s = s.cwiseQuotient(dims);
float l = s.head(3).norm();
Vector3f L(l/s(0), l/s(1), l/s(2));
Vector3f offset;
for(int i=0;i<3;++i)
offset(i) = (s(i)>0) - (pt(i)-floor(pt(i))) ;
offset = offset.cwiseProduct(L).cwiseAbs();
int id; float d;
for(int loop=0; loop<8; loop++)
{
int check_border = 0;
for ( int i=0; i<3 ;++i) {
check_border += pt(i) > 1;
check_border += pt(i) < 0;
}
if(check_border == 0) {
for(int i=0;i<3;++i)
pt(i) *= (float)dims(i);
pt = m_Image->GetWorldPoint(pt);
return true;
}
d = offset.minCoeff(&id);
for(int i=0; i<3; ++i)
pt(i) += d / L(i);
pt(id) = rintf(pt(id));
offset.array() -= d;
offset(id) = fabs(L(id));
}
for(int i=0;i<3;++i)
pt(i) *= (float)dims(i);
pt = m_Image->GetWorldPoint(pt);
return false;
}
bool VoxRaytracer::GetExitPoint(const HLine3f line, Vector4f &pt)
{
HLine3f out = line;
out.direction() *= -1;
return GetEntryPoint(out,pt);
}
VoxRaytracer::RayData VoxRaytracer::TraceBetweenPoints(const Vector4f &in,
const Vector4f &out)
const
{
RayData ray;
Vector4f pt1 = m_Image->GetLocalPoint(in);
Vector4f pt2 = m_Image->GetLocalPoint(out);
Vector4f s = pt2 - pt1;
Vector3f scale; // FIXXX
scale << (m_Image->GetWorldMatrix() * Vector4f(1,0,0,0)).norm(),
(m_Image->GetWorldMatrix() * Vector4f(0,1,0,0)).norm(),
(m_Image->GetWorldMatrix() * Vector4f(0,0,1,0)).norm();
float l = s.head(3).norm();
Vector3f L(l/s(0), l/s(1), l/s(2));
Vector3f offset;
for(int i=0;i<3;++i)
offset(i) = (s(i)>=0) - (pt1(i)-floor(pt1(i))) ;
offset = offset.cwiseProduct(L).cwiseAbs();
L = L.cwiseAbs();
int id; float d;
Vector3i vid = m_Image->Find(in);
while(l>0)
{
d = offset.minCoeff(&id);
if(m_Image->IsInsideGrid(vid))
ray.AddElement(m_Image->Map(vid), d * scale(id) );
// nan check //
// if(unlikely(!isFinite(d * scale(id)))) {
// std:: cout << "NAN in raytracer\n";
// exit(1);
// }
vid(id) += (int)fast_sign(s(id));
l -= d;
offset.array() -= d;
offset(id) = fmin(L(id),l);
}
return ray;
}
}

86
src/Math/VoxRaytracer.h Normal file
View File

@@ -0,0 +1,86 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 VOXRAYTRACER_H
#define VOXRAYTRACER_H
#include <math.h>
#include "Core/Vector.h"
#include "Math/ImageData.h"
#include "Math/Line.h"
#include "Math/VectorSpace.h"
namespace uLib {
class VoxRaytracer {
public:
class RayData {
public:
RayData() : m_TotalLength(0) {}
typedef struct {
Id_t vox_id;
Scalarf L;
} Element;
inline void AddElement(Id_t id, float L);
void AppendRay ( const RayData &in);
uLibConstRefMacro(Data,Vector<Element>)
uLibConstRefMacro(TotalLength,Scalarf)
void PrintSelf(std::ostream &o);
private:
Vector<Element> m_Data;
Scalarf m_TotalLength;
};
public:
VoxRaytracer(ImageData &image) : m_Image(&image) {}
bool GetEntryPoint(const HLine3f line, Vector4f &pt);
bool GetExitPoint(const HLine3f line, Vector4f &pt);
RayData TraceBetweenPoints(const Vector4f &in, const Vector4f &out) const;
uLibGetMacro(Image,ImageData *)
private:
ImageData *m_Image;
};
}
#endif // VOXRAYTRACER_H

View File

@@ -0,0 +1,108 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "Math/Accumulator.h"
#include "testing-prototype.h"
#include <TRandom.h>
//#include <boost/accumulators/framework/accumulator_set.hpp>
//#include <boost/accumulators/statistics/count.hpp>
//#include <boost/accumulators/accumulators.hpp>
using namespace uLib;
int test_ABTrim() {
Accumulator_ABTrim<float> acc;
acc.SetABTrim(1,1);
Vector<float> v;
v << 1,5,5,5,300;
for(Vector<float>::Iterator itr=v.begin(); itr<v.end(); itr++)
acc += *itr;
std::cout << "Accumulating Trim(1,1) vector: "
<< v << " ... out = " << acc() << "\n";
return( acc() == 15.0 );
}
int test_Mean() {
Accumulator_Mean<float> mean;
TRandom rnd;
const int c = 10000000;
Vector<float> v;
v.reserve(c);
for(int i=0;i<c;++i) v.push_back( rnd.Gaus(2000,5) );
float m = 0;
for(int i=0;i<c;++i) m += v[i];
m /= c;
std::cout << "simple mean: " << m << "\n";
for(int i=0;i<c;++i) mean(v[i]);
std::cout << "mean pass: " << mean() << "\n";
mean.AddPass();
for(int i=0;i<c;++i) mean(v[i]);
std::cout << "mean pass: " << mean() << "\n";
mean.AddPass();
for(int i=0;i<c;++i) mean(v[i]);
std::cout << "mean pass: " << mean() << "\n";
mean.AddPass();
for(int i=0;i<c;++i) mean(v[i]);
std::cout << "mean pass: " << mean() << "\n";
mean.AddPass();
for(int i=0;i<c;++i) mean(v[i]);
std::cout << "mean pass: " << mean() << "\n";
mean.AddPass();
for(int i=0;i<c;++i) mean(v[i]);
std::cout << "mean pass: " << mean() << "\n";
mean.AddPass();
for(int i=0;i<c;++i) mean(v[i]);
std::cout << "mean pass: " << mean() << "\n";
}
int main(void) {
BEGIN_TESTING(Accumulator);
//TEST1( test_ABTrim() );
test_Mean();
END_TESTING;
}

View File

@@ -0,0 +1,19 @@
#include "testing-prototype.h"
#include "Math/Dense.h"
#include "Math/BitCode.h"
#include <iostream>
#include <math.h>
using namespace uLib;
int main() {
BEGIN_TESTING(BitCode Test);
BitCode<uint,1,2> bc;
bc.PrintSelf();
END_TESTING;
}

View File

@@ -0,0 +1,19 @@
set(TESTS
MathVectorTest
GeometryTest
ContainerBoxTest
VoxImageTest
VoxRaytracerTest
StructuredDataTest
StructuredGridTest
VoxImageFilterTest
PolicyTest
AccumulatorTest
VoxImageCopyTest
TriangleMeshTest
BitCodeTest
)
uLib_add_tests(${uLib-module})

View File

@@ -0,0 +1,95 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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/Dense.h"
#include "Math/VectorSpace.h"
#include "Math/ContainerBox.h"
#include <iostream>
#include <math.h>
using namespace uLib;
int Vector4f0(Vector4f c)
{
c(3) = 0;
if ( fabs(c(0)) < 0.001 && fabs(c(1)) < 0.001 && fabs(c(2)) < 0.001 )
return 0;
else
return 1;
}
int main()
{
BEGIN_TESTING(Math ContainerBox);
ContainerBox Cnt;
// // Local transform:
Cnt.SetOrigin(Vector3f(-1,-1,-1));
Cnt.SetSize(Vector3f(2,2,2)); // scaling //
std::cout << "Container scale is: " << Cnt.GetSize().transpose() << "\n";
std::cout << "Container scale is: " << Cnt.GetSize().transpose() << "\n";
TEST0( Vector4f0(Cnt.GetSize().homogeneous() - HVector3f(2,2,2)) );
ContainerBox Box;
Box.SetPosition(Vector3f(1,1,1));
Box.SetSize(Vector3f(2,2,2));
Box.EulerYZYRotate(Vector3f(0,0,0));
Vector4f pt = Box.GetLocalPoint(HPoint3f(2,3,2));
Vector4f wp = Box.GetWorldPoint(pt);
TEST0( Vector4f0(wp - HPoint3f(2,3,2)) );
//// // Global
// Cnt.SetPosition(Vector3f(1,1,1));
// Cnt.EulerYZYRotate(Vector3f(M_PI_2,M_PI_2,0));
// HPoint3f p = Cnt.GetWorldPoint(1,1,1);
// //std::cout << p.transpose() << "\n";
// TEST0( Vector4f0(p - HVector3f(2,1,2)) );
// p = Cnt.GetWorldPoint(1,2,3);
// //std::cout << p.transpose() << "\n";
// TEST0( Vector4f0(p - HVector3f(4,1,3)) );
// // scaling //
_fail > 0 ? std::cout << "FAIL\n" : std::cout << "OK!\n";
END_TESTING;
}

View File

@@ -0,0 +1,99 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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/Dense.h"
#include "Math/VectorSpace.h"
#include "Math/Geometry.h"
#include "Math/ContainerBox.h"
#include <iostream>
#include <math.h>
using namespace uLib;
int Vector4f0(Vector4f c)
{
c(3) = 0;
if ( fabs(c(0)) < 0.001 && fabs(c(1)) < 0.001 && fabs(c(2)) < 0.001 )
return 0;
else
return 1;
}
int main()
{
BEGIN_TESTING(Math Geometry);
//////////////////////////////////////////////////////////////////////////////
///////////////// GEOMETRY TESTING ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Geometry Geo;
Geo.SetPosition(Vector3f(1,1,1));
Geo.EulerYZYRotate(Vector3f(0,0,0));
Vector4f pt = Geo.GetLocalPoint(HPoint3f(2,3,2));
Vector4f wp = Geo.GetWorldPoint(pt);
TEST0( Vector4f0(wp - HPoint3f(2,3,2)) );
Geo.Scale(Vector3f(2,2,2));
wp = Geo.GetWorldPoint(HPoint3f(1,1,1));
TEST0( Vector4f0(wp - HPoint3f(3,3,3)) );
Geo.Scale(Vector3f( .5 , .5 , .5 ));
Geo.EulerYZYRotate(Vector3f(M_PI_2,0,0));
wp = Geo.GetWorldPoint(HPoint3f(1,1,1));
TEST0( Vector4f0(wp - HPoint3f(2,2,0)) );
Geo.EulerYZYRotate(Vector3f(0,M_PI_2,0));
wp = Geo.GetWorldPoint(HPoint3f(1,1,1));
TEST0( Vector4f0(wp - HPoint3f(2,2,2)) );
Geo.EulerYZYRotate(Vector3f(0,0,M_PI_2));
wp = Geo.GetWorldPoint(HPoint3f(1,1,1));
// std::cout << "Geometry matrix\n" << Geo.GetTransform() << "\n";
// std::cout << "World 1,1,1 coords\n" << wp << "\n";
TEST0( Vector4f0(wp - HPoint3f(0,2,2)) );
// TESTING FLIP AXES //
Geo.SetPosition(Vector3f(0,0,0));
Geo.EulerYZYRotate(Vector3f(-M_PI_2,-M_PI_2,-M_PI_2)); // reset previous
Geo.EulerYZYRotate(Vector3f(M_PI_2,0,0)); // PI_2 along X
Geo.FlipAxes(0,2); // flip X-Z
Vector4f p = Geo.GetWorldPoint(Vector3f(1,0,0).homogeneous());
TEST0( Vector4f0(p - HVector3f(1,0,0)) ); // after flip and rotation X->X
_fail > 0 ? std::cout << "FAIL\n" : std::cout << "OK!\n";
END_TESTING;
}

View File

@@ -0,0 +1,28 @@
include $(top_srcdir)/Common.am
#AM_DEFAULT_SOURCE_EXT = .cpp
# if HAVE_CHECK
TESTS = MathVectorTest \
GeometryTest \
ContainerBoxTest \
VoxImageTest \
VoxRaytracerTest \
StructuredDataTest \
VoxImageFilterTest \
PolicyTest \
AccumulatorTest \
VoxImageCopyTest \
TriangleMeshTest
# else
# TEST =
# endif
LDADD = $(top_srcdir)/libmutom-${PACKAGE_VERSION}.la $(AM_LIBS_ALL)
all: ${TESTS}
check_PROGRAMS = $(TESTS)

View File

@@ -0,0 +1,120 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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/Dense.h"
#include "Math/VectorSpace.h"
#include "Core/Archives.h"
#include "Core/String.h"
#include <iostream>
#include <math.h>
using namespace uLib;
int Vector4f0(Vector4f c)
{
c(3) = 0;
if ( fabs(c(0)) < 0.001 && fabs(c(1)) < 0.001 && fabs(c(2)) < 0.001 )
return 0;
else
return 1;
}
int main()
{
BEGIN_TESTING(uLib Math);
//////////////////////////////////////////////////////////////////////////////
///////////////// VECTOR TESTING /////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
std::cout << "Testing Vectors\n";
{ // ROW COLUMN ACCESSOR ORDER TEST //
Matrix2i test;
test << 1,2,3,4;
std::cout << "print row column test ";
std::cout << test(0,0) << " " <<
test(0,1) << " " <<
test(1,0) << " " <<
test(1,1) << " " << "\n";
TEST1( test(0,1) == 2 && test(1,0) == 3 );
}
{
Vector3i test(0,0,0);
++test(1);
std::cout << "increment of single dim inside vector: " << test.transpose() << "\n";
TEST1( test == Vector3i(0,1,0) );
}
{
Matrix4f mat;
mat << 1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1;
std::cout << "determinant" << mat.determinant() << "\n";
}
{
// Vector3f v(1,2,3);
// std::string str("2 3 5");
// std::istringstream istr(str);
// uLib::Archive::hrt_iarchive hi(istr);
// hi >> v;
// uLib::Archive::hrt_oarchive ho(std::cout);
// ho << v;
}
{
// VECTOR STRING INTERACTION //
Vector3f v;
std::string str("2 3 5");
str >> v;
std::string sstr;
sstr << v << "\n";
std::cout << sstr;
}
END_TESTING;
}

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 <iostream>
#include "testing-prototype.h"
// Curiously Recurring Template Pattern //
template < typename V, typename T >
class Base {
public:
Base(int a) : value(a) {
Algorithm = static_cast<T*>(this);
}
void Print()
{
std::cout << Algorithm->Value() << "\n";
}
int value;
V voxel;
T* Algorithm;
};
template < typename V >
class Derived : public Base< V ,Derived<V> > {
typedef Base< V ,Derived<V> > BaseClass;
public:
Derived(int a) : BaseClass(a) {}
int Value() { return this->value+1; }
};
int main()
{
BEGIN_TESTING(Policy);
Derived<int> my(5);
my.Print();
Base<int, Derived<int> > my2(5);
my2.Print();
END_TESTING;
}

View File

@@ -0,0 +1,129 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 <stddef.h>
#include "testing-prototype.h"
#include "Math/Dense.h"
#include "Math/ContainerBox.h"
#include "Math/StructuredData.h"
#include "Math/DataSet.h"
#include <iostream>
using namespace uLib;
struct Data {
Data() : d1(123), d2(234), i3(345) {}
float d1,d2;
int i3;
float D2() { return d2; }
float operator()() { return d2; }
};
int main() {
BEGIN_TESTING(Structured Data);
Data data;
VectorData<Data> vd;
vd.push_back(data);
vd.push_back(Data());
vd.push_back(Data());
vd.SetScalar(sizeof(float));
std::cout << vd.Value<float>(2) << "\n";
vd.SetScalar(offsetof(Data,i3));
std::cout << vd.Value<int>(2) << "\n";
// vd.SetBindScalar(&data,&Data::D2);
// std::cout << vd.BindValue(2) << "\n";
END_TESTING;
}
int main2() {
BEGIN_TESTING(Structured Data);
{ // testing unmap function ////////////////////////////////////////////////
StructuredData sdata(Vector3i(2,3,4));
for(int i=0; i < sdata.GetDims().prod() ; ++i) {
Vector3i d = sdata.UnMap(i);
std::cout << "TEST1( sdata.UnMap(" << i << ") == Vector3i("
<< d(0) << "," << d(1) << "," << d(2) << ")); \n";
}
TEST1( sdata.UnMap(0) == Vector3i(0,0,0));
TEST1( sdata.UnMap(1) == Vector3i(0,1,0));
TEST1( sdata.UnMap(2) == Vector3i(0,2,0));
TEST1( sdata.UnMap(3) == Vector3i(1,0,0));
TEST1( sdata.UnMap(4) == Vector3i(1,1,0));
TEST1( sdata.UnMap(5) == Vector3i(1,2,0));
TEST1( sdata.UnMap(6) == Vector3i(0,0,1));
TEST1( sdata.UnMap(7) == Vector3i(0,1,1));
TEST1( sdata.UnMap(8) == Vector3i(0,2,1));
TEST1( sdata.UnMap(9) == Vector3i(1,0,1));
TEST1( sdata.UnMap(10) == Vector3i(1,1,1));
TEST1( sdata.UnMap(11) == Vector3i(1,2,1));
TEST1( sdata.UnMap(12) == Vector3i(0,0,2));
TEST1( sdata.UnMap(13) == Vector3i(0,1,2));
TEST1( sdata.UnMap(14) == Vector3i(0,2,2));
TEST1( sdata.UnMap(15) == Vector3i(1,0,2));
TEST1( sdata.UnMap(16) == Vector3i(1,1,2));
TEST1( sdata.UnMap(17) == Vector3i(1,2,2));
TEST1( sdata.UnMap(18) == Vector3i(0,0,3));
TEST1( sdata.UnMap(19) == Vector3i(0,1,3));
TEST1( sdata.UnMap(20) == Vector3i(0,2,3));
TEST1( sdata.UnMap(21) == Vector3i(1,0,3));
TEST1( sdata.UnMap(22) == Vector3i(1,1,3));
TEST1( sdata.UnMap(23) == Vector3i(1,2,3));
// testing map /////////////////////////////////////////////////////////
int i=0;
// PREDEFINED ORDER IS YXZ //
for(int z=0; z<sdata.GetDims()(2) ; ++z )
for(int x=0; x<sdata.GetDims()(0) ; ++x )
for(int y=0; y<sdata.GetDims()(1) ; ++y )
{
Vector3i index(x,y,z);
std::cout << sdata.Map(index) << " ";
TEST1( sdata.Map(index) == i++ );
}
std::cout << "\n";
}
END_TESTING;
}

View File

@@ -0,0 +1,72 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 <Math/VoxImage.h>
#include "testing-prototype.h"
using namespace uLib;
int main()
{
BEGIN_TESTING(Math Structured Grid);
VoxImage<Voxel> f_grid(Vector3i(100,100,100));
f_grid[Vector3i(1,2,3)]() = 5552368;
TEST1( f_grid.At(30102)() == 5552368 );
// TESTING MAP //
VoxImage<Voxel> test_order(Vector3i(2,2,2));
int index =0;
for(int z=0;z<2;++z)
for(int y=0;y<2;++y)
for(int x=0;x<2;x++)
{
test_order[Vector3i(x,y,z)]() = index;
std::cout << x << " " << y << " " << z << " -> " <<
test_order.At(Vector3i(x,y,z))() << "\n";
index++;
}
test_order.Data().PrintSelf(std::cout);
int result[8] = {0,2,1,3,4,6,5,7};
for(int i=0;i<8;++i)
TEST1( test_order.At(i)() == result[i] );
// TESTING UNMAP //
std::cout << "unmap = " << test_order.UnMap(7).transpose() << "\n";
TEST1( test_order.UnMap(7) == Vector3i(1,1,1) );
TEST1( test_order.UnMap(6) == Vector3i(1,0,1) );
TEST1( test_order.UnMap(5) == Vector3i(0,1,1) );
// ...
_fail > 0 ? std::cout << "FaIL\n" : std::cout << "OK!\n";
END_TESTING
}

View File

@@ -0,0 +1,52 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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/TriangleMesh.h"
using namespace uLib;
int main()
{
BEGIN_TESTING(Triangle Mesh);
TriangleMesh mesh;
mesh.AddPoint(Vector3f(0,0,0));
mesh.AddPoint(Vector3f(0,1,0));
mesh.AddPoint(Vector3f(1,0,0));
mesh.AddTriangle(Vector3i(0,1,2));
mesh.PrintSelf(std::cout);
END_TESTING;
}

View File

@@ -0,0 +1,77 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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/VoxImage.h"
using namespace uLib;
struct TestVoxel {
Scalarf Value;
unsigned int Count;
};
int main() {
BEGIN_TESTING(Math VoxImage Copy);
{
VoxImage<TestVoxel> img(Vector3i(10,10,10));
TestVoxel zero = {0,0};
img.InitVoxels(zero);
TestVoxel nonzero = {5.552368, 0};
img[Vector3i(5,1,7)] = nonzero;
img[img.Find(HPoint3f(3,3,3))].Value = 5.552369;
TEST1( img.GetValue(Vector3i(5,1,7)) == 5.552368f );
img.SetOrigin(Vector3f(4,5,6));
std::cout << "\n";
img.PrintSelf(std::cout);
VoxImage<TestVoxel> img2 = img;
img2.PrintSelf(std::cout);
TEST1( img.GetOrigin() == img2.GetOrigin() );
TEST1( img.GetSpacing() == img2.GetSpacing() );
img2 = img;
}
std::cout << "returns " << _fail << "\n";
END_TESTING;
}

View File

@@ -0,0 +1,150 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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"
#include "Math/VoxImage.h"
#include "Math/VoxImageFilter.h"
using namespace uLib;
struct TestVoxel {
Scalarf Value;
unsigned int Count;
};
float GaussianShape(float d)
{
// normalized manually .. fix //
return 4.5 * exp(-d * 4.5);
}
class GaussianShapeClass : public Interface::VoxImageFilterShape {
public:
GaussianShapeClass(float sigma) :
m_sigma(sigma)
{}
float operator ()(float d) {
return (1/m_sigma) * exp(-d/m_sigma);
}
private:
float m_sigma;
};
static float MaxInVector(const Vector<float> &v)
{
float max = 0;
for(int i=0; i<v.size(); ++i)
if(v.at(i) > max) max = v.at(i);
return max;
}
int main()
{
BEGIN_TESTING(VoxImageFilters);
VoxImage<TestVoxel> image(Vector3i(20,30,40));
image[Vector3i(10,10,10)].Value = 1;
//image[Vector3i(10,10,8)].Value = 1;
image.ExportToVtk("test_filter_original.vtk",0);
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// RPS //
{
VoxFilterAlgorithmSPR<TestVoxel> filter(Vector3i(2,3,4));
VoxImage<TestVoxel> filtered = image;
Vector<float> values;
for(int i=0; i < filter.GetKernelData().GetDims().prod(); ++i) {
values.push_back(1.);
std::cout << values[i] << " ";
}
std::cout << "\n";
filter.SetImage(&filtered);
filter.SetKernelNumericXZY(values);
filter.SetABTrim(0,2);
filter.GetKernelData().PrintSelf(std::cout);
filter.Run();
filtered.ExportToVtk("filter_RPS_out.vtk",0);
}
{
VoxImage<TestVoxel> image(Vector3i(20,30,40));
image[Vector3i(10,10,10)].Value = 1;
image[Vector3i(9,10,8)].Value = 2;
image.ExportToVtk("test_filter_max_original.vtk",0);
VoxFilterAlgorithmCustom<TestVoxel> filter(Vector3i(3,3,4));
Vector<float> values;
for(int i=0; i < filter.GetKernelData().GetDims().prod(); ++i) {
values.push_back(static_cast<float>(1));
}
filter.SetImage(&image);
filter.SetKernelNumericXZY(values);
filter.SetCustomEvaluate(MaxInVector);
filter.Run();
image.ExportToVtk("test_filter_max.vtk",0);
}
END_TESTING;
}

View File

@@ -0,0 +1,107 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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"
#include "Math/VoxImage.h"
using namespace uLib;
struct TestVoxel {
Scalarf Value;
unsigned int Count;
};
int main() {
BEGIN_TESTING(Math ImageData);
{ // SIMPLE TESTS //
ImageData img(Vector3i(10,10,10));
img.SetSpacing(Vector3f(3,3,3));
TEST1( img.GetWorldPoint(2,0,0) == HPoint3f(6,0,0) );
TEST1( img.GetWorldPoint(1,1,1) == HPoint3f(3,3,3) );
img.SetPosition(Vector3f(1,1,1));
TEST1( img.GetWorldPoint(1,1,1) == HPoint3f(4,4,4) );
TEST1( img.GetLocalPoint(4,4,4) == HPoint3f(1,1,1) );
TEST0( img.IsInsideBounds(HPoint3f(5,33,-5)));
TEST0( img.IsInsideBounds(HPoint3f(0,0,0)));
TEST1( img.IsInsideBounds(HPoint3f(1,1,1)));
}
{ // TEST WITH ORIGIN //
ImageData img(Vector3i(10,10,10));
img.SetSpacing(Vector3f(3,3,3));
img.SetOrigin(Vector3f(-1,1,-1));
img.SetPosition(Vector3f(1,1,1));
TEST1( img.GetWorldPoint(1,1,1) == HPoint3f(3,5,3) );
}
{
VoxImage<TestVoxel> img(Vector3i(10,10,10));
TestVoxel zero = {0,0};
img.InitVoxels(zero);
TestVoxel nonzero = {5.552368, 0};
img[Vector3i(5,1,7)] = nonzero;
img[img.Find(HPoint3f(3,3,3))].Value = 5.552369;
img.ExportToVtk("./test_vox_image.vtk",0);
img.ExportToVtkXml("./test_vox_image.vti",0);
TEST1( img.GetValue(Vector3i(5,1,7)) == 5.552368f );
}
{
VoxImage<TestVoxel> img(Vector3i(4,4,4));
TestVoxel zero = {0,0};
img.InitVoxels(zero);
img.SetSpacing(Vector3f(2,2,2));
img.SetPosition(Vector3f(-4,-4,-4));
TEST1( img.GetWorldPoint(img.GetLocalPoint(HPoint3f(5,5,5))) == HPoint3f(5,5,5));
}
{
VoxImage<TestVoxel> imgR(Vector3i(0,0,0));
imgR.ImportFromVtk("./test_vox_image.vtk");
imgR.ExportToVtk("./read_and_saved.vtk");
}
{
VoxImage<TestVoxel> img1(Vector3i(5,5,5));
VoxImage<TestVoxel> img2;
img2 = img1;
TEST1( img1.GetDims() == img2.GetDims() );
}
END_TESTING
}

View File

@@ -0,0 +1,151 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "Math/ImageData.h"
#include "Math/VoxRaytracer.h"
#include "testing-prototype.h"
using namespace uLib;
int Vector4f0(Vector4f c)
{
c(3) = 0;
if ( fabs(c(0)) < 0.001 && fabs(c(1)) < 0.001 && fabs(c(2)) < 0.001 )
return 0;
else
return 1;
}
//bool Comapare(const &t1, const T2 &t2)
//{
// int out = 0;
// out += t1.vox_id != t2.vox_id;
// out += (fabs(t1.L) - fabs(t2.L)) > 0.001;
// return out == 0;
//}
typedef VoxRaytracer Raytracer;
int main()
{
BEGIN_TESTING(Math VoxRaytracer);
ImageData img(Vector3i(2,2,2));
img.SetSpacing(Vector3f(2,2,2));
img.SetPosition(Vector3f(-2,0,-2));
{
HLine3f line;
line.origin << 0.1, 4.1, 0.1, 1;
line.direction << 0.1, -0.1, 0.1,0;
Raytracer rt(img);
HPoint3f pt;
TEST1( rt.GetEntryPoint(line,pt) );
TEST0( Vector4f0( pt - HPoint3f(0.2,4,0.2) ) );
}
{
HLine3f line;
line.origin << 4,0,4, 1;
line.direction << -0.1, 0.1, -0.1, 0;
Raytracer rt(img);
HPoint3f pt;
TEST1( rt.GetEntryPoint(line,pt) );
TEST0( Vector4f0( pt - HPoint3f(2,2,2) ) );
}
{ // Test a point inside image //
ImageData img(Vector3i(4,4,4));
img.SetSpacing(Vector3f(2,2,2));
img.SetPosition(Vector3f(-4,-4,-4));
Raytracer ray(img);
HPoint3f pt;
HLine3f line;
line.origin = HPoint3f(-3,-3,-3);
// line.direction = HVector3f(1,1,1); //
TEST1( ray.GetEntryPoint(line,pt) );
TEST1( pt == HPoint3f(-3,-3,-3) );
Raytracer::RayData rdata = ray.TraceBetweenPoints(HPoint3f(-3,-3,-3), HPoint3f(3,3,3));
foreach (const Raytracer::RayData::Element &el, rdata.Data())
{
std::cout << " " << el.vox_id << " , " << el.L << "\n";
}
}
{
Vector4f pt1(1,-0.5,1);
Vector4f pt2(1,4.5,1);
Raytracer rt(img);
Raytracer::RayData ray = rt.TraceBetweenPoints(pt1,pt2);
TEST1( ray.Data().size() == 2 );
TEST1( ray.Data().at(0).vox_id == 6 );
TEST1( ray.Data().at(1).vox_id == 7 );
ray.PrintSelf(std::cout);
}
{
Vector4f pt1(5,1,1);
Vector4f pt2(-3,1,1);
Raytracer rt(img);
Raytracer::RayData ray = rt.TraceBetweenPoints(pt1,pt2);
TEST1( ray.Data().size() == 2 );
TEST1( ray.Data().at(0).vox_id == 6 );
TEST1( ray.Data().at(1).vox_id == 4 );
ray.PrintSelf(std::cout);
}
{
Vector4f pt1(1,1,1);
Vector4f pt2(-1,3,-1);
Raytracer rt(img);
Raytracer::RayData ray = rt.TraceBetweenPoints(pt1,pt2);
TEST1( ray.Data().size() == 4 );
TEST1( ray.Data().at(0).vox_id == 6 );
TEST1( ray.Data().at(1).vox_id == 4 );
TEST1( ray.Data().at(2).vox_id == 5 );
TEST1( ray.Data().at(3).vox_id == 1 );
ray.PrintSelf(std::cout);
}
END_TESTING
}

View File

@@ -0,0 +1,37 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 <stdio.h>
#define BEGIN_TESTING(name) \
static int _fail = 0; \
printf("..:: Testing " #name " ::..\n");
#define TEST1(val) _fail += (val)==0
#define TEST0(val) _fail += (val)!=0
#define END_TESTING return _fail;