BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
PointwiseAxis Class Reference

Axis containing arbitrary (non-equidistant) coordinate values. More...

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=default
 
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. More...
 
std::vector< double > binCenters () const override
 
double center () const
 Returns midpoint of axis. More...
 
PointwiseAxisclone () const override
 clone function More...
 
virtual bool contains (double value) const
 Returns true if axis contains given point. More...
 
PointwiseAxiscreateClippedAxis (double left, double right) const override
 Creates a new clipped axis. More...
 
size_t findClosestIndex (double value) const override
 find index of the coordinate closest to the given value More...
 
std::string getName () const
 retrieve the label of the axis More...
 
double lowerBound () 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 setName (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...
 
double upperBound () const override
 Returns value of last on-axis point. More...
 

Private Member Functions

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

Private Attributes

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

Detailed 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::getMidPoint. On-axis values are bounded by minimum/maximum values passed to the constructor.

Definition at line 37 of file PointwiseAxis.h.

Constructor & Destructor Documentation

◆ PointwiseAxis()

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

Definition at line 40 of file PointwiseAxis.h.

41  : IAxis(std::forward<String>(name)), m_coordinates(std::forward<Vector>(coordinate_values))
42  {
43  sanityCheck();
44  }
IAxis(const std::string &name)
constructors
Definition: IAxis.h:28
std::vector< double > m_coordinates
Definition: PointwiseAxis.h:90
void sanityCheck() const
QString const & name(EShape k)
Definition: particles.cpp:21

References sanityCheck().

Referenced by clone(), and createClippedAxis().

Here is the call graph for this function:

◆ ~PointwiseAxis()

PointwiseAxis::~PointwiseAxis ( )
overridedefault

Member Function Documentation

◆ bin()

Bin1D PointwiseAxis::bin ( size_t  index) const
overridevirtual

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 27 of file PointwiseAxis.cpp.

28 {
29  checkIndex(index);
30  return Bin1D(lowerBoundary(index), upperBoundary(index));
31 }
double upperBoundary(size_t index) const
double lowerBoundary(size_t index) const
void checkIndex(size_t index) const
Definition: Bin.h:20

References checkIndex(), lowerBoundary(), and upperBoundary().

Here is the call graph for this function:

◆ binBoundaries()

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

Reimplemented from IAxis.

Definition at line 62 of file PointwiseAxis.cpp.

63 {
64  std::vector<double> result;
65  const size_t v_size = m_coordinates.size();
66  result.reserve(v_size + 1);
67  for (size_t i = 0; i < v_size; ++i)
68  result.push_back(lowerBoundary(i));
69  result.push_back(upperBoundary(v_size - 1));
70  return result;
71 }

References lowerBoundary(), m_coordinates, and upperBoundary().

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 43 of file PointwiseAxis.cpp.

44 {
45  checkIndex(index);
46  return m_coordinates[index];
47 }

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 74 of file PointwiseAxis.h.

74 { return m_coordinates; }

References m_coordinates.

◆ center()

double IAxis::center ( ) const
inherited

Returns midpoint of axis.

Definition at line 47 of file IAxis.cpp.

48 {
49  return (upperBound() + lowerBound()) / 2;
50 }
virtual double upperBound() const =0
Returns value of last point of axis.
virtual double lowerBound() const =0
Returns value of first point of axis.

References IAxis::lowerBound(), and IAxis::upperBound().

Here is the call graph for this function:

◆ checkIndex()

void PointwiseAxis::checkIndex ( size_t  index) const
private

Definition at line 119 of file PointwiseAxis.cpp.

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

References m_coordinates.

Referenced by bin(), and binCenter().

◆ clone()

PointwiseAxis * PointwiseAxis::clone ( ) const
overridevirtual

clone function

Implements IAxis.

Definition at line 22 of file PointwiseAxis.cpp.

23 {
24  return new PointwiseAxis(getName(), m_coordinates);
25 }
std::string getName() const
retrieve the label of the axis
Definition: IAxis.h:40
PointwiseAxis(String &&name, Vector &&coordinate_values)
Definition: PointwiseAxis.h:40

References PointwiseAxis(), IAxis::getName(), 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 37 of file IAxis.cpp.

38 {
39  return value >= lowerBound() && value < upperBound();
40 }

References IAxis::lowerBound(), and IAxis::upperBound().

Referenced by Histogram1D::crop(), Histogram2D::crop(), Histogram1D::fill(), RectangularDetector::indexOfSpecular(), and SphericalDetector::indexOfSpecular().

Here is the call graph for this function:

◆ createClippedAxis()

PointwiseAxis * PointwiseAxis::createClippedAxis ( double  left,
double  right 
) const
overridevirtual

Creates a new clipped axis.

Reimplemented from IAxis.

Definition at line 73 of file PointwiseAxis.cpp.

74 {
75  if (left >= right)
76  throw std::runtime_error("Error in PointwiseAxis::createClippedAxis: "
77  "'left' should be smaller than 'right'");
78 
79  using diff_t = std::vector<double>::iterator::difference_type;
80  auto begin = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(left));
81  auto end = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(right)) + 1;
82 
83  return new PointwiseAxis(getName(), std::vector<double>(begin, end));
84 }
size_t findClosestIndex(double value) const override
find index of the coordinate closest to the given value

References PointwiseAxis(), findClosestIndex(), IAxis::getName(), and m_coordinates.

Here is the call graph for this function:

◆ equals()

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

Reimplemented from IAxis.

Definition at line 96 of file PointwiseAxis.cpp.

97 {
98  if (!IAxis::equals(other))
99  return false;
100  if (const PointwiseAxis* otherAxis = dynamic_cast<const PointwiseAxis*>(&other))
101  return m_coordinates == otherAxis->binCenters();
102  return false;
103 }
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:17
Axis containing arbitrary (non-equidistant) coordinate values.
Definition: PointwiseAxis.h:37

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 49 of file PointwiseAxis.cpp.

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

References lowerBoundary(), and m_coordinates.

Referenced by createClippedAxis().

Here is the call graph for this function:

◆ getName()

◆ lowerBound()

double PointwiseAxis::lowerBound ( ) const
overridevirtual

Returns value of first on-axis point.

Implements IAxis.

Definition at line 33 of file PointwiseAxis.cpp.

34 {
35  return lowerBoundary(0);
36 }

References lowerBoundary().

Here is the call graph for this function:

◆ lowerBoundary()

double PointwiseAxis::lowerBoundary ( size_t  index) const
private

Definition at line 105 of file PointwiseAxis.cpp.

106 {
107  if (index == 0)
108  return m_coordinates.front();
109  return 0.5 * (m_coordinates[index] + m_coordinates[index - 1]);
110 }

References m_coordinates.

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

◆ operator!=()

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

Definition at line 70 of file IAxis.h.

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

◆ operator==()

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

test for equality

Definition at line 69 of file IAxis.h.

69 { 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 55 of file PointwiseAxis.h.

55 { return binCenter(index); }
double binCenter(size_t index) const override
Returns the coordinate corresponding to the given index.

References binCenter().

Here is the call graph for this function:

◆ print()

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

Implements IAxis.

Definition at line 86 of file PointwiseAxis.cpp.

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

References IAxis::getName(), and m_coordinates.

Here is the call graph for this function:

◆ sanityCheck()

void PointwiseAxis::sanityCheck ( ) const
private

Definition at line 129 of file PointwiseAxis.cpp.

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

References m_coordinates, and min_axis_size.

Referenced by PointwiseAxis().

◆ setName()

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

Sets the axis label.

Definition at line 43 of file IAxis.h.

43 { m_name = name; }

References IAxis::m_name, and RealSpace::Particles::name().

Here is the call graph for this function:

◆ size()

size_t PointwiseAxis::size ( ) const
inlineoverridevirtual

retrieve the number of bins

Implements IAxis.

Definition at line 52 of file PointwiseAxis.h.

52 { return m_coordinates.size(); }

References m_coordinates.

◆ span()

double IAxis::span ( ) const
inherited

Returns distance from first to last point.

Definition at line 42 of file IAxis.cpp.

43 {
44  return upperBound() - lowerBound();
45 }

References IAxis::lowerBound(), and IAxis::upperBound().

Referenced by RectangularDetector::getHeight(), RectangularDetector::getWidth(), and DetectorUtils::isQuadratic().

Here is the call graph for this function:

◆ upperBound()

double PointwiseAxis::upperBound ( ) const
overridevirtual

Returns value of last on-axis point.

Implements IAxis.

Definition at line 38 of file PointwiseAxis.cpp.

39 {
40  return upperBoundary(m_coordinates.size() - 1);
41 }

References m_coordinates, and upperBoundary().

Here is the call graph for this function:

◆ upperBoundary()

double PointwiseAxis::upperBoundary ( size_t  index) const
private

Definition at line 112 of file PointwiseAxis.cpp.

113 {
114  if (index + 1 == m_coordinates.size())
115  return m_coordinates.back();
116  return 0.5 * (m_coordinates[index] + m_coordinates[index + 1]);
117 }

References m_coordinates.

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

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 96 of file IAxis.h.

Referenced by IAxis::getName(), and IAxis::setName().


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