BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
PointwiseAxis Class Reference

Description

Axis containing arbitrary (non-equidistant) coordinate values. Lower boundary of the first bin and upper boundary of the last bin correspond to first and last passed coordinates. Other bin boundaries are computed as arithmetical mean of two adjacent coordinates. One should be aware, that bin centers reported by PointwiseAxis::binCenter do not coincide with the values produced by Bin1D::center. On-axis values are bounded by minimum/maximum values passed to the constructor.

Definition at line 33 of file PointwiseAxis.h.

Inheritance diagram for PointwiseAxis:
[legend]
Collaboration diagram for PointwiseAxis:
[legend]

Public Member Functions

template<class String , class Vector >
 PointwiseAxis (String &&name, Vector &&coordinate_values)
 
 ~PointwiseAxis () override
 
std::string axisName () const
 Returns the label of the axis. More...
 
Bin1D bin (size_t index) const override
 retrieve a 1d bin for the given index More...
 
std::vector< double > binBoundaries () const override
 
double binCenter (size_t index) const override
 Returns the coordinate corresponding to the given index. For this type of axis ("pointwise") this equals returning an explicitly defined coordinate. More...
 
std::vector< double > binCenters () const override
 
std::pair< double, double > bounds () const
 Returns lower and upper bound in a pair. first is lower, second is upper. More...
 
double center () const
 Returns midpoint of axis. More...
 
void clip (double lower, double upper) override
 Clips this axis to the given values. More...
 
void clip (std::pair< double, double > bounds)
 Convenience overload to clip this axis to the given values. bounds.first is lower, bounds.second is upper value. More...
 
PointwiseAxisclone () const override
 clone function More...
 
virtual bool contains (double value) const
 Returns true if axis contains given point. More...
 
size_t findClosestIndex (double value) const override
 find index of the coordinate closest to the given value More...
 
double max () const override
 Returns value of last on-axis point. More...
 
double min () const override
 Returns value of first on-axis point. More...
 
bool operator!= (const IAxis &right) const
 
bool operator== (const IAxis &right) const
 test for equality More...
 
double operator[] (size_t index) const override
 indexed accessor retrieves a sample More...
 
void setAxisName (std::string name)
 Sets the axis label. More...
 
size_t size () const override
 retrieve the number of bins More...
 
double span () const
 Returns distance from first to last point. More...
 

Private Member Functions

void checkIndex (size_t index) const
 
bool equals (const IAxis &other) const override
 
double maxary (size_t index) const
 
double minary (size_t index) const
 
void print (std::ostream &ostr) const override
 
void sanityCheck () const
 

Private Attributes

std::vector< double > m_coordinates
 
std::string m_name
 axis name More...
 

Constructor & Destructor Documentation

◆ PointwiseAxis()

template<class String , class Vector >
PointwiseAxis::PointwiseAxis ( String &&  name,
Vector &&  coordinate_values 
)
inline

Definition at line 36 of file PointwiseAxis.h.

37  : IAxis(std::forward<String>(name))
38  , m_coordinates(std::forward<Vector>(coordinate_values))
39  {
40  sanityCheck();
41  }
IAxis(std::string name)
Definition: IAxis.h:29
std::vector< double > m_coordinates
Definition: PointwiseAxis.h:86
void sanityCheck() const

References sanityCheck().

Referenced by clone().

Here is the call graph for this function:

◆ ~PointwiseAxis()

PointwiseAxis::~PointwiseAxis ( )
overridedefault

Member Function Documentation

◆ axisName()

std::string IAxis::axisName ( ) const
inlineinherited

◆ bin()

Bin1D PointwiseAxis::bin ( size_t  index) const
overridevirtual

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 30 of file PointwiseAxis.cpp.

31 {
32  checkIndex(index);
33  return Bin1D(minary(index), maxary(index));
34 }
Definition: Bin.h:20
void checkIndex(size_t index) const
double maxary(size_t index) const
double minary(size_t index) const

References checkIndex(), maxary(), and minary().

Here is the call graph for this function:

◆ binBoundaries()

std::vector< double > PointwiseAxis::binBoundaries ( ) const
overridevirtual

Reimplemented from IAxis.

Definition at line 65 of file PointwiseAxis.cpp.

66 {
67  std::vector<double> result;
68  const size_t v_size = m_coordinates.size();
69  result.reserve(v_size + 1);
70  for (size_t i = 0; i < v_size; ++i)
71  result.push_back(minary(i));
72  result.push_back(maxary(v_size - 1));
73  return result;
74 }

References m_coordinates, maxary(), and minary().

Here is the call graph for this function:

◆ binCenter()

double PointwiseAxis::binCenter ( size_t  index) const
overridevirtual

Returns the coordinate corresponding to the given index. For this type of axis ("pointwise") this equals returning an explicitly defined coordinate.

Implements IAxis.

Definition at line 46 of file PointwiseAxis.cpp.

47 {
48  checkIndex(index);
49  return m_coordinates[index];
50 }

References checkIndex(), and m_coordinates.

Referenced by operator[]().

Here is the call graph for this function:

◆ binCenters()

std::vector<double> PointwiseAxis::binCenters ( ) const
inlineoverridevirtual

Reimplemented from IAxis.

Definition at line 71 of file PointwiseAxis.h.

71 { return m_coordinates; }

References m_coordinates.

◆ bounds()

std::pair< double, double > IAxis::bounds ( ) const
inherited

Returns lower and upper bound in a pair. first is lower, second is upper.

Definition at line 50 of file IAxis.cpp.

51 {
52  return {min(), max()};
53 }
virtual double max() const =0
Returns value of last point of axis.
virtual double min() const =0
Returns value of first point of axis.

References IAxis::max(), and IAxis::min().

Referenced by IAxis::clip().

Here is the call graph for this function:

◆ center()

double IAxis::center ( ) const
inherited

Returns midpoint of axis.

Definition at line 60 of file IAxis.cpp.

61 {
62  return (max() + min()) / 2;
63 }

References IAxis::max(), and IAxis::min().

Here is the call graph for this function:

◆ checkIndex()

void PointwiseAxis::checkIndex ( size_t  index) const
private

Definition at line 123 of file PointwiseAxis.cpp.

124 {
125  if (m_coordinates.size() > index)
126  return;
127  std::string message = "Error in PointwiseAxis::binCenter: passed index ";
128  message += std::to_string(index) + " exceeds the size ";
129  message += std::to_string(m_coordinates.size()) + " of the axis";
130  throw std::runtime_error(message);
131 }

References m_coordinates.

Referenced by bin(), and binCenter().

◆ clip() [1/2]

void PointwiseAxis::clip ( double  lower,
double  upper 
)
overridevirtual

Clips this axis to the given values.

Reimplemented from IAxis.

Definition at line 76 of file PointwiseAxis.cpp.

77 {
78  if (lower >= upper)
79  throw std::runtime_error(
80  "PointwiseAxis::clip() -> Error. 'lower' should be smaller than 'upper'");
81 
82  using diff_t = std::vector<double>::iterator::difference_type;
83  const auto begin = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(lower));
84  const auto end = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(upper)) + 1;
85 
86  m_coordinates = std::vector<double>(begin, end);
87  sanityCheck();
88 }
size_t findClosestIndex(double value) const override
find index of the coordinate closest to the given value

References findClosestIndex(), m_coordinates, and sanityCheck().

Here is the call graph for this function:

◆ clip() [2/2]

void IAxis::clip ( std::pair< double, double >  bounds)
inherited

Convenience overload to clip this axis to the given values. bounds.first is lower, bounds.second is upper value.

Definition at line 40 of file IAxis.cpp.

41 {
42  return clip(bounds.first, bounds.second);
43 }
std::pair< double, double > bounds() const
Returns lower and upper bound in a pair. first is lower, second is upper.
Definition: IAxis.cpp:50
virtual void clip(double lower, double upper)
Clips this axis to the given values.
Definition: IAxis.cpp:35

References IAxis::bounds(), and IAxis::clip().

Here is the call graph for this function:

◆ clone()

PointwiseAxis * PointwiseAxis::clone ( ) const
overridevirtual

clone function

Implements IAxis.

Definition at line 25 of file PointwiseAxis.cpp.

26 {
27  return new PointwiseAxis(axisName(), m_coordinates);
28 }
std::string axisName() const
Returns the label of the axis.
Definition: IAxis.h:61
PointwiseAxis(String &&name, Vector &&coordinate_values)
Definition: PointwiseAxis.h:36

References PointwiseAxis(), IAxis::axisName(), and m_coordinates.

Here is the call graph for this function:

◆ contains()

bool IAxis::contains ( double  value) const
virtualinherited

Returns true if axis contains given point.

Definition at line 45 of file IAxis.cpp.

46 {
47  return value >= min() && value < max();
48 }

References IAxis::max(), and IAxis::min().

Referenced by RectangularDetector::indexOfSpecular(), and SphericalDetector::indexOfSpecular().

Here is the call graph for this function:

◆ equals()

bool PointwiseAxis::equals ( const IAxis other) const
overrideprivatevirtual

Reimplemented from IAxis.

Definition at line 100 of file PointwiseAxis.cpp.

101 {
102  if (!IAxis::equals(other))
103  return false;
104  if (const auto* otherAxis = dynamic_cast<const PointwiseAxis*>(&other))
105  return m_coordinates == otherAxis->binCenters();
106  return false;
107 }
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:20
Axis containing arbitrary (non-equidistant) coordinate values. Lower boundary of the first bin and up...
Definition: PointwiseAxis.h:33

References IAxis::equals(), and m_coordinates.

Here is the call graph for this function:

◆ findClosestIndex()

size_t PointwiseAxis::findClosestIndex ( double  value) const
overridevirtual

find index of the coordinate closest to the given value

Implements IAxis.

Definition at line 52 of file PointwiseAxis.cpp.

53 {
54  if (value <= m_coordinates.front())
55  return 0;
56  if (value >= m_coordinates.back())
57  return m_coordinates.size() - 1;
58 
59  const auto begin = m_coordinates.begin();
60  auto result = std::lower_bound(begin, m_coordinates.end(), value);
61  auto index = static_cast<size_t>(std::distance(begin, result));
62  return value < minary(index) ? index - 1 : index;
63 }

References m_coordinates, and minary().

Referenced by clip().

Here is the call graph for this function:

◆ max()

double PointwiseAxis::max ( ) const
overridevirtual

Returns value of last on-axis point.

Implements IAxis.

Definition at line 41 of file PointwiseAxis.cpp.

42 {
43  return maxary(m_coordinates.size() - 1);
44 }

References m_coordinates, and maxary().

Here is the call graph for this function:

◆ maxary()

double PointwiseAxis::maxary ( size_t  index) const
private

Definition at line 116 of file PointwiseAxis.cpp.

117 {
118  if (index + 1 == m_coordinates.size())
119  return m_coordinates.back();
120  return 0.5 * (m_coordinates[index] + m_coordinates[index + 1]);
121 }

References m_coordinates.

Referenced by bin(), binBoundaries(), and max().

◆ min()

double PointwiseAxis::min ( ) const
overridevirtual

Returns value of first on-axis point.

Implements IAxis.

Definition at line 36 of file PointwiseAxis.cpp.

37 {
38  return minary(0);
39 }

References minary().

Here is the call graph for this function:

◆ minary()

double PointwiseAxis::minary ( size_t  index) const
private

Definition at line 109 of file PointwiseAxis.cpp.

110 {
111  if (index == 0)
112  return m_coordinates.front();
113  return 0.5 * (m_coordinates[index] + m_coordinates[index - 1]);
114 }

References m_coordinates.

Referenced by bin(), binBoundaries(), findClosestIndex(), and min().

◆ operator!=()

bool IAxis::operator!= ( const IAxis right) const
inlineinherited

Definition at line 90 of file IAxis.h.

90 { return !(*this == right); }

◆ operator==()

bool IAxis::operator== ( const IAxis right) const
inlineinherited

test for equality

Definition at line 89 of file IAxis.h.

89 { return equals(right); }

References IAxis::equals().

Here is the call graph for this function:

◆ operator[]()

double PointwiseAxis::operator[] ( size_t  index) const
inlineoverridevirtual

indexed accessor retrieves a sample

Implements IAxis.

Definition at line 52 of file PointwiseAxis.h.

52 { return binCenter(index); }
double binCenter(size_t index) const override
Returns the coordinate corresponding to the given index. For this type of axis ("pointwise") this equ...

References binCenter().

Here is the call graph for this function:

◆ print()

void PointwiseAxis::print ( std::ostream &  ostr) const
overrideprivatevirtual

Implements IAxis.

Definition at line 90 of file PointwiseAxis.cpp.

91 {
92  auto precision = std::setprecision(std::numeric_limits<double>::digits10 + 2);
93  ostr << "PointwiseAxis(\"" << axisName() << "\", "
94  << ", [";
95  for (size_t i = 0, fin = m_coordinates.size() - 1; i < fin; ++i)
96  ostr << precision << m_coordinates[i] << ",";
97  ostr << precision << m_coordinates.back() << "])";
98 }

References IAxis::axisName(), and m_coordinates.

Here is the call graph for this function:

◆ sanityCheck()

void PointwiseAxis::sanityCheck ( ) const
private

Definition at line 133 of file PointwiseAxis.cpp.

134 {
135  if (m_coordinates.size() < min_axis_size)
136  throw std::runtime_error(
137  "Error in PointwiseAxis::PointwiseAxis: the size of passed coordinate array is "
138  "less than minimum acceptable value");
139 
140  const auto begin = m_coordinates.begin();
141  const auto end = m_coordinates.end();
142 
143  if (!std::is_sorted(begin, end))
144  throw std::runtime_error("Error in PointwiseAxis::PointwiseAxis: passed coordinates are "
145  "not sorted in ascending order");
146 
147  if (std::adjacent_find(begin, end) != end)
148  throw std::runtime_error("Error in PointwiseAxis::PointwiseAxis: passed coordinate vector "
149  "contains repeating values");
150 }
const size_t min_axis_size

References m_coordinates, and min_axis_size.

Referenced by PointwiseAxis(), and clip().

◆ setAxisName()

void IAxis::setAxisName ( std::string  name)
inlineinherited

Sets the axis label.

Definition at line 39 of file IAxis.h.

39 { m_name = name; }

References IAxis::m_name.

◆ size()

size_t PointwiseAxis::size ( ) const
inlineoverridevirtual

retrieve the number of bins

Implements IAxis.

Definition at line 49 of file PointwiseAxis.h.

49 { return m_coordinates.size(); }

References m_coordinates.

◆ span()

double IAxis::span ( ) const
inherited

Returns distance from first to last point.

Definition at line 55 of file IAxis.cpp.

56 {
57  return max() - min();
58 }

References IAxis::max(), and IAxis::min().

Referenced by RectangularDetector::height(), and RectangularDetector::width().

Here is the call graph for this function:

Member Data Documentation

◆ m_coordinates

std::vector<double> PointwiseAxis::m_coordinates
private

◆ m_name

std::string IAxis::m_name
privateinherited

axis name

Definition at line 103 of file IAxis.h.

Referenced by IAxis::axisName(), and IAxis::setAxisName().


The documentation for this class was generated from the following files: