BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Frame.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Axis/Frame.cpp
6 //! @brief Implements class Frame.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "Base/Axis/Frame.h"
16 #include "Base/Axis/Bin.h"
17 #include "Base/Axis/IAxis.h"
18 #include "Base/Util/Assert.h"
19 
20 Frame::Frame(const std::vector<IAxis*>& axes)
21  : m_axes(axes)
22 {
23  m_size = 1;
24  for (size_t k = 0; k < rank(); ++k) {
25  ASSERT(axis(k).size() > 0);
26  m_size *= axis(k).size();
27  }
28 }
29 
30 Frame::~Frame() = default;
31 
32 std::vector<IAxis*> Frame::cloned_axes() const
33 {
34  return m_axes.cloned_vector();
35 }
36 
37 size_t Frame::projectedSize(size_t k_axis) const
38 {
39  return m_axes[k_axis]->size();
40 }
41 
42 double Frame::projectedCoord(size_t i_flat, size_t k_axis) const
43 {
44  auto axis_index = projectedIndex(i_flat, k_axis);
45  return (*m_axes[k_axis])[axis_index];
46 }
47 
48 std::vector<int> Frame::allIndices(size_t i_flat) const
49 {
50  std::vector<int> result(rank());
51  for (size_t k = 0; k < rank(); ++k)
52  result[k] = projectedIndex(i_flat, k);
53  return result;
54 }
55 
56 size_t Frame::projectedIndex(size_t i_flat, size_t k_axis) const
57 {
58  if (rank() == 1) {
59  return i_flat;
60  } else if (rank() == 2) {
61  if (k_axis == 0)
62  return (i_flat / m_axes[1]->size()) % m_axes[0]->size();
63  if (k_axis == 1)
64  return i_flat % m_axes[1]->size();
65  ASSERT(0);
66  }
67  ASSERT(0);
68  /* // generic code for rank>2 currently unused
69  size_t remainder(i_flat);
70  for (int k = rank() - 1; k >= 0; --k) {
71  size_t result = remainder % m_axes[k]->size();
72  if (k_axis == k)
73  return result;
74  remainder /= m_axes[k]->size();
75  }
76  */
77 }
78 
79 size_t Frame::toGlobalIndex(const std::vector<unsigned>& axes_indices) const
80 {
81  ASSERT(axes_indices.size() == rank());
82  size_t result = 0;
83  size_t step_size = 1;
84  for (int k = rank() - 1; k >= 0; --k) {
85  ASSERT(axes_indices[k] < m_axes[k]->size());
86  result += axes_indices[k] * step_size;
87  step_size *= m_axes[k]->size();
88  }
89  return result;
90 }
91 
92 bool Frame::operator==(const Frame& o) const
93 {
94  if (rank() != o.rank())
95  return false;
96  for (size_t k = 0; k < rank(); ++k)
97  if (axis(k) != o.axis(k))
98  return false;
99  return true;
100 }
101 
102 bool Frame::hasSameSizes(const Frame& o) const
103 {
104  if (rank() != o.rank())
105  return false;
106  for (size_t k = 0; k < rank(); ++k)
107  if (axis(k).size() != o.axis(k).size())
108  return false;
109  return true;
110 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:45
Defines structs Bin1D, Bin1DCVector.
Defines and implements templated class Frame.
Defines interface IAxis.
Holds one or two axes.
Definition: Frame.h:27
size_t projectedIndex(size_t i_flat, size_t k_axis) const
Returns axis bin index for given global index.
Definition: Frame.cpp:56
const IAxis & axis(size_t k_axis) const
Returns axis with given serial number.
Definition: Frame.h:46
size_t size() const
Returns total number of bins.
Definition: Frame.h:37
std::vector< int > allIndices(size_t i_flat) const
Returns vector of axes indices for given global index.
Definition: Frame.cpp:48
bool operator==(const Frame &) const
Returns true if both Frames have same rank, and all axes are equal.
Definition: Frame.cpp:92
OwningVector< IAxis > m_axes
Definition: Frame.h:80
size_t toGlobalIndex(const std::vector< unsigned > &axes_indices) const
Returns global index for specified indices of axes.
Definition: Frame.cpp:79
size_t projectedSize(size_t k_axis) const
Returns number of bins along axis.
Definition: Frame.cpp:37
Frame(const std::vector< IAxis * > &axes)
Constructor that takes ownership of supplied axes.
Definition: Frame.cpp:20
size_t m_size
Definition: Frame.h:83
virtual ~Frame()
std::vector< IAxis * > cloned_axes() const
Returns cloned axes.
Definition: Frame.cpp:32
double projectedCoord(size_t i_flat, size_t k_axis) const
Returns the value of selected axis for given i_flat.
Definition: Frame.cpp:42
size_t rank() const
Returns number of dimensions.
Definition: Frame.h:34
bool hasSameSizes(const Frame &) const
Returns true if both Frames have same rank, and all axes have same sizes.
Definition: Frame.cpp:102
virtual size_t size() const =0
Returns the number of bins.
std::vector< T * > cloned_vector() const
Definition: OwningVector.h:81
size_t size() const
Definition: OwningVector.h:70