From 192529aa785b2469c291587b511f2f64ca262680 Mon Sep 17 00:00:00 2001 From: Paolo Andreetto Date: Thu, 19 Dec 2019 10:47:28 +0100 Subject: [PATCH] New classes for absorption --- src/Math/CMakeLists.txt | 4 + src/Math/Structured2DGrid.cpp | 130 ++++++++++++++++++++++++++++++++ src/Math/Structured2DGrid.h | 76 +++++++++++++++++++ src/Math/Structured4DGrid.cpp | 136 ++++++++++++++++++++++++++++++++++ src/Math/Structured4DGrid.h | 76 +++++++++++++++++++ 5 files changed, 422 insertions(+) create mode 100644 src/Math/Structured2DGrid.cpp create mode 100644 src/Math/Structured2DGrid.h create mode 100644 src/Math/Structured4DGrid.cpp create mode 100644 src/Math/Structured4DGrid.h diff --git a/src/Math/CMakeLists.txt b/src/Math/CMakeLists.txt index c3e73b1..fd99ebb 100644 --- a/src/Math/CMakeLists.txt +++ b/src/Math/CMakeLists.txt @@ -22,6 +22,8 @@ set(HEADERS Accumulator.h TriangleMesh.h BitCode.h + Structured2DGrid.h + Structured4DGrid.h ) @@ -33,6 +35,8 @@ set(SOURCES VoxImage.cpp TriangleMesh.cpp Dense.cpp + Structured2DGrid.h + Structured4DGrid.h ) set(LIBRARIES diff --git a/src/Math/Structured2DGrid.cpp b/src/Math/Structured2DGrid.cpp new file mode 100644 index 0000000..93d8f8c --- /dev/null +++ b/src/Math/Structured2DGrid.cpp @@ -0,0 +1,130 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova + All rights reserved + + Authors: Sara Vanini < sara.vanini@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 "Structured2DGrid.h" + +using namespace uLib; + +//_____________________________________________________________ +Structured2DGrid::Structured2DGrid() +{ + // reset uni-dim space + m_Dims = Vector2i(0,0); + m_Origin = Vector2f(0.,0.); + m_Increments = Vector2i(0,0); + m_binW = Vector2f(1.,1.); + m_debug = true; +} + + +//_____________________________________________________________ +void Structured2DGrid::SetDims(const Vector2i &size) +{ + this->m_Dims = size; + + // set increments + this->m_Increments[0] = 1; + this->m_Increments[1] = m_Dims[0]; + } + +//_____________________________________________________________ +void Structured2DGrid::SetPhysicalSpace(const Vector2f &origin, const Vector2f &spacing) +{ + m_Origin = origin; + m_binW = spacing; + + m_uni2phys_T = Translation(m_Origin) * Scaling(m_binW); + m_phys2uni_T = m_uni2phys_T.matrix().inverse(); + + return; +} + + +//_____________________________________________________________ +bool Structured2DGrid::IsInsideBounds(const Vector2f &pt) const +{ + Vector2i pLoc = this->PhysicsToUnitSpace(pt); + return this->IsInsideGrid(pLoc); +} + + +//_____________________________________________________________ +Vector2i Structured2DGrid::UnMap(int index) const +{ + Vector2i v( 0,0 ); + Vector2i iv = m_Increments; + + int id = 0; + for(int k=0; k<2; ++k) { + int inc = iv.maxCoeff(&id); + v(id) = index / inc; + index -= v(id) * inc; + iv(id) = 0; + } + return v; +} + +//_____________________________________________________________ +bool Structured2DGrid::IsInsideGrid(const Vector2i &v) const +{ + int vok = 1; + vok *= (v(0) >= 0 && v(0) < m_Dims[0]); + vok *= (v(1) >= 0 && v(1) < m_Dims[1]); + + return vok; +} + + +//_____________________________________________________________ +Vector2i Structured2DGrid::PhysicsToUnitSpace(const Vector2f &pt) const +{ + Vector2i out; + Vector2f pLoc = m_phys2uni_T * pt; + + out << (int)floor(pLoc(0)), + (int)floor(pLoc(1)); + + return out; +} + +//_____________________________________________________________ +Vector2f Structured2DGrid::UnitToPhysicsSpace(const Vector2i &ind) const +{ + Vector2f p; + p = m_uni2phys_T * ind.cast(); + + return p; +} + +//_____________________________________________________________ +void Structured2DGrid::PrintSelf(std::ostream &o) +{ + o << "uLib Structured Data 2D: \n" + << "dim = " << this->GetDims().transpose() << "\n" + << "spacing = " << this->GetSpacing().transpose() << "\n"; +} + diff --git a/src/Math/Structured2DGrid.h b/src/Math/Structured2DGrid.h new file mode 100644 index 0000000..abb9aa6 --- /dev/null +++ b/src/Math/Structured2DGrid.h @@ -0,0 +1,76 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova + All rights reserved + + Authors: Sara Vanini < sara.vanini@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 Structured2DGrid_H +#define Structured2DGrid_H + +#include "Core/Macros.h" +#include "Core/Object.h" +#include "Math/Dense.h" + +#include +#include + +using namespace Eigen; + +namespace uLib { + +class Structured2DGrid : public Object { +public: + + Structured2DGrid(); + + /// unit-space + void SetDims(const Vector2i &size); + inline Vector2i GetDims() {return m_Dims; } + bool IsInsideGrid(const Vector2i &v) const; + inline int Map(Vector2i index) const { return (m_Increments.transpose() * index);} + Vector2i UnMap(int index) const; + + /// physics-space + void SetPhysicalSpace(const Vector2f &origin, const Vector2f &spacing); + inline Vector2f GetSpacing() const { return m_binW; } + inline Vector2f GetOrigin() const { return m_Origin; } + bool IsInsideBounds(const Vector2f &pt) const; + + /// operations + Vector2i PhysicsToUnitSpace(const Vector2f &pt) const; + Vector2f UnitToPhysicsSpace(const Vector2i &ind) const; + void PrintSelf(std::ostream &o); + void SetDebug(bool debug){ m_debug = debug; }; + +private: + Vector2i m_Dims; + Vector2i m_Increments; + Vector2f m_binW; + Vector2f m_Origin; + bool m_debug; + + Transform m_uni2phys_T; + Transform m_phys2uni_T; +}; +} + +#endif // Structured2DGrid_H diff --git a/src/Math/Structured4DGrid.cpp b/src/Math/Structured4DGrid.cpp new file mode 100644 index 0000000..543ba60 --- /dev/null +++ b/src/Math/Structured4DGrid.cpp @@ -0,0 +1,136 @@ +/*////////////////////////////////////////////////////////////////////////////// +// 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 "Structured4DGrid.h" + +using namespace uLib; + +//_____________________________________________________________ +Structured4DGrid::Structured4DGrid() +{ + // reset uni-dim space + m_Dims = Vector4i(0,0,0,0); + m_Origin = Vector4f(0.,0.,0.,0.); + m_Increments = Vector4i(0,0,0,0); + m_binW = Vector4f(1.,1.,1.,1.); + m_debug = true; +} + + +//_____________________________________________________________ +void Structured4DGrid::SetDims(const Vector4i &size) +{ + this->m_Dims = size; + + // set increments + this->m_Increments[0] = 1; + this->m_Increments[1] = m_Dims[0]; + this->m_Increments[2] = m_Dims[0] * m_Dims[1]; + this->m_Increments[3] = m_Dims[0] * m_Dims[1] * m_Dims[2]; + +} + +//_____________________________________________________________ +void Structured4DGrid::SetPhysicalSpace(const Vector4f &origin, const Vector4f &spacing) +{ + m_Origin = origin; + m_binW = spacing; + + m_uni2phys_T = Translation(m_Origin) * Scaling(m_binW); + m_phys2uni_T = m_uni2phys_T.matrix().inverse(); + + return; +} + + +//_____________________________________________________________ +bool Structured4DGrid::IsInsideBounds(const Vector4f &pt) const +{ + Vector4i pLoc = this->PhysicsToUnitSpace(pt); + return this->IsInsideGrid(pLoc); +} + + +//_____________________________________________________________ +Vector4i Structured4DGrid::UnMap(int index) const +{ + Vector4i v( 0,0,0, 0 ); + Vector4i iv = m_Increments; + + int id = 0; + for(int k=0; k<4; ++k) { + int inc = iv.maxCoeff(&id); + v(id) = index / inc; + index -= v(id) * inc; + iv(id) = 0; + } + return v; +} + +//_____________________________________________________________ +bool Structured4DGrid::IsInsideGrid(const Vector4i &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]); + vok *= (v(3) >= 0 && v(3) < m_Dims[3]); + return vok; +} + + +//_____________________________________________________________ +Vector4i Structured4DGrid::PhysicsToUnitSpace(const Vector4f &pt) const +{ + Vector4i out; + Vector4f pLoc = m_phys2uni_T * pt; + + out << (int)floor(pLoc(0)), + (int)floor(pLoc(1)), + (int)floor(pLoc(2)), + (int)floor(pLoc(3)); + + return out; +} + +//_____________________________________________________________ +Vector4f Structured4DGrid::UnitToPhysicsSpace(const Vector4i &ind) const +{ + Vector4f p; + p = m_uni2phys_T * ind.cast(); + + return p; +} + +//_____________________________________________________________ +void Structured4DGrid::PrintSelf(std::ostream &o) +{ + o << "uLib Structured Data 4D: \n" + << "dim = " << this->GetDims().transpose() << "\n" + << "spacing = " << this->GetSpacing().transpose() << "\n"; +} + diff --git a/src/Math/Structured4DGrid.h b/src/Math/Structured4DGrid.h new file mode 100644 index 0000000..5dc1cc0 --- /dev/null +++ b/src/Math/Structured4DGrid.h @@ -0,0 +1,76 @@ +/*////////////////////////////////////////////////////////////////////////////// +// 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 Structured4DGrid_H +#define Structured4DGrid_H + +#include "Core/Macros.h" +#include "Core/Object.h" +#include "Math/Dense.h" + +#include +#include + +using namespace Eigen; + +namespace uLib { + +class Structured4DGrid : public Object { +public: + + Structured4DGrid(); + + /// unit-space + void SetDims(const Vector4i &size); + inline Vector4i GetDims() {return m_Dims; } + bool IsInsideGrid(const Vector4i &v) const; + inline int Map(Vector4i index) const { return (m_Increments.transpose() * index);} + Vector4i UnMap(int index) const; + + /// physics-space + void SetPhysicalSpace(const Vector4f &origin, const Vector4f &spacing); + inline Vector4f GetSpacing() const { return m_binW; } + inline Vector4f GetOrigin() const { return m_Origin; } + bool IsInsideBounds(const Vector4f &pt) const; + + /// operations + Vector4i PhysicsToUnitSpace(const Vector4f &pt) const; + Vector4f UnitToPhysicsSpace(const Vector4i &ind) const; + void PrintSelf(std::ostream &o); + void SetDebug(bool debug){ m_debug = debug; }; + +private: + Vector4i m_Dims; + Vector4i m_Increments; + Vector4f m_binW; + Vector4f m_Origin; + bool m_debug; + + Transform m_uni2phys_T; + Transform m_phys2uni_T; +}; +} + +#endif // Structured4DGrid_H