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

Description

Axis with fixed bin size.

Definition at line 23 of file FixedBinAxis.h.

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

Public Member Functions

 FixedBinAxis (const std::string &name, size_t nbins, double start, double end)
 FixedBinAxis constructor. More...
 
 ~FixedBinAxis () override=default
 
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
 
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...
 
FixedBinAxisclone () const override
 
virtual bool contains (double value) const
 Returns true if axis contains given point. More...
 
size_t findClosestIndex (double value) const override
 find bin index which is best match for given value More...
 
double max () const override
 Returns value of last point of axis. More...
 
double min () const override
 Returns value of first point of axis. 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
 Returns the number of bins. More...
 
double span () const
 Returns distance from first to last point. More...
 

Protected Member Functions

bool equals (const IAxis &other) const override
 
void print (std::ostream &ostr) const override
 

Private Attributes

double m_end
 
std::string m_name
 axis name More...
 
size_t m_nbins
 
double m_start
 

Constructor & Destructor Documentation

◆ FixedBinAxis()

FixedBinAxis::FixedBinAxis ( const std::string &  name,
size_t  nbins,
double  start,
double  end 
)

FixedBinAxis constructor.

Parameters
nameAxis name
nbinsnumber of bins
startlow edge of first bin
endupper edge of last bin

Definition at line 21 of file FixedBinAxis.cpp.

22  : IAxis(name)
23  , m_nbins(nbins)
24  , m_start(start)
25  , m_end(end)
26 {
27 }
double m_start
Definition: FixedBinAxis.h:60
double m_end
Definition: FixedBinAxis.h:61
size_t m_nbins
Definition: FixedBinAxis.h:59
IAxis(std::string name)
Definition: IAxis.h:29

Referenced by clone().

◆ ~FixedBinAxis()

FixedBinAxis::~FixedBinAxis ( )
overridedefault

Member Function Documentation

◆ axisName()

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

◆ bin()

Bin1D FixedBinAxis::bin ( size_t  index) const
overridevirtual

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 44 of file FixedBinAxis.cpp.

45 {
46  if (index >= m_nbins)
47  throw std::runtime_error("FixedBinAxis::bin() -> Error. Wrong index.");
48 
49  double step = (m_end - m_start) / m_nbins;
50  Bin1D result(m_start + step * index, m_start + step * (index + 1));
51  return result;
52 }
Definition: Bin.h:20

References m_end, m_nbins, and m_start.

Referenced by binBoundaries(), binCenters(), and clip().

◆ binBoundaries()

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

Reimplemented from IAxis.

Definition at line 74 of file FixedBinAxis.cpp.

75 {
76  std::vector<double> result;
77  result.resize(size() + 1, 0.0);
78  for (size_t i = 0; i < size(); ++i)
79  result[i] = bin(i).m_lower;
80  result[size()] = bin(size() - 1).m_upper;
81  return result;
82 }
double m_upper
upper bound of the bin
Definition: Bin.h:29
double m_lower
lower bound of the bin
Definition: Bin.h:28
size_t size() const override
Returns the number of bins.
Definition: FixedBinAxis.h:35
Bin1D bin(size_t index) const override
retrieve a 1d bin for the given index

References bin(), Bin1D::m_lower, Bin1D::m_upper, and size().

Here is the call graph for this function:

◆ binCenter()

double FixedBinAxis::binCenter ( size_t  index) const
inlineoverridevirtual

Implements IAxis.

Definition at line 44 of file FixedBinAxis.h.

44 { return (*this)[index]; }

◆ binCenters()

std::vector< double > FixedBinAxis::binCenters ( ) const
overridevirtual

Reimplemented from IAxis.

Definition at line 65 of file FixedBinAxis.cpp.

66 {
67  std::vector<double> result;
68  result.resize(size(), 0.0);
69  for (size_t i = 0; i < size(); ++i)
70  result[i] = bin(i).center();
71  return result;
72 }
double center() const
Definition: Bin.h:30

References bin(), Bin1D::center(), and size().

Here is the call graph for this function:

◆ 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:

◆ clip() [1/2]

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

Clips this axis to the given values.

Reimplemented from IAxis.

Definition at line 84 of file FixedBinAxis.cpp.

85 {
86  if (lower >= upper)
87  throw std::runtime_error(
88  "FixedBinAxis::clip() -> Error. 'lower' should be smaller than 'upper'");
89 
90  if (lower < min())
91  lower = bin(0).center();
92  if (upper >= max())
93  upper = bin(size() - 1).center();
94 
95  const size_t nbin1 = findClosestIndex(lower);
96  const size_t nbin2 = findClosestIndex(upper);
97 
98  // create tmp vars until everything is calculated, otherwise the calculation will be corrupted
99  // by partially changed values
100  const auto newStart = bin(nbin1).m_lower;
101  const auto newEnd = bin(nbin2).m_upper;
102 
103  m_nbins = nbin2 - nbin1 + 1;
104  m_start = newStart;
105  m_end = newEnd;
106 }
size_t findClosestIndex(double value) const override
find bin index which is best match for given value
double max() const override
Returns value of last point of axis.
Definition: FixedBinAxis.h:42
double min() const override
Returns value of first point of axis.
Definition: FixedBinAxis.h:41

References bin(), Bin1D::center(), findClosestIndex(), m_end, Bin1D::m_lower, m_nbins, m_start, Bin1D::m_upper, max(), min(), and size().

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()

FixedBinAxis * FixedBinAxis::clone ( ) const
overridevirtual

Implements IAxis.

Definition at line 29 of file FixedBinAxis.cpp.

30 {
31  auto* result = new FixedBinAxis(axisName(), m_nbins, m_start, m_end);
32  return result;
33 }
FixedBinAxis(const std::string &name, size_t nbins, double start, double end)
FixedBinAxis constructor.
std::string axisName() const
Returns the label of the axis.
Definition: IAxis.h:61

References FixedBinAxis(), IAxis::axisName(), m_end, m_nbins, and m_start.

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 FixedBinAxis::equals ( const IAxis other) const
overrideprotectedvirtual

Reimplemented from IAxis.

Definition at line 115 of file FixedBinAxis.cpp.

116 {
117  if (!IAxis::equals(other))
118  return false;
119  if (const auto* otherAxis = dynamic_cast<const FixedBinAxis*>(&other)) {
120  if (size() != otherAxis->size())
121  return false;
122  if (!BaseUtils::algo::almostEqual(m_start, otherAxis->m_start))
123  return false;
124  if (!BaseUtils::algo::almostEqual(m_end, otherAxis->m_end))
125  return false;
126  return true;
127  }
128  return false;
129 }
Axis with fixed bin size.
Definition: FixedBinAxis.h:23
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:20
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:33

References BaseUtils::algo::almostEqual(), IAxis::equals(), m_end, m_start, and size().

Here is the call graph for this function:

◆ findClosestIndex()

size_t FixedBinAxis::findClosestIndex ( double  value) const
overridevirtual

find bin index which is best match for given value

Implements IAxis.

Definition at line 54 of file FixedBinAxis.cpp.

55 {
56  if (value < min())
57  return 0;
58  if (value >= max())
59  return m_nbins - 1;
60 
61  double step = (m_end - m_start) / m_nbins;
62  return int((value - m_start) / step);
63 }

References m_end, m_nbins, m_start, max(), and min().

Referenced by clip().

Here is the call graph for this function:

◆ max()

double FixedBinAxis::max ( ) const
inlineoverridevirtual

Returns value of last point of axis.

Implements IAxis.

Definition at line 42 of file FixedBinAxis.h.

42 { return m_end; }

References m_end.

Referenced by clip(), findClosestIndex(), and print().

◆ min()

double FixedBinAxis::min ( ) const
inlineoverridevirtual

Returns value of first point of axis.

Implements IAxis.

Definition at line 41 of file FixedBinAxis.h.

41 { return m_start; }

References m_start.

Referenced by clip(), findClosestIndex(), and print().

◆ 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 FixedBinAxis::operator[] ( size_t  index) const
overridevirtual

indexed accessor retrieves a sample

Implements IAxis.

Definition at line 35 of file FixedBinAxis.cpp.

36 {
37  if (index >= m_nbins)
38  throw std::runtime_error("FixedBinAxis::operator[] -> Error. Wrong index.");
39 
40  double step = (m_end - m_start) / m_nbins;
41  return m_start + (index + 0.5) * step;
42 }

References m_end, m_nbins, and m_start.

◆ print()

void FixedBinAxis::print ( std::ostream &  ostr) const
overrideprotectedvirtual

Implements IAxis.

Definition at line 108 of file FixedBinAxis.cpp.

109 {
110  ostr << "FixedBinAxis(\"" << axisName() << "\", " << size() << ", "
111  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << min() << ", " << max()
112  << ")";
113 }

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

Here is the call graph for this function:

◆ 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 FixedBinAxis::size ( ) const
inlineoverridevirtual

Returns the number of bins.

Implements IAxis.

Definition at line 35 of file FixedBinAxis.h.

35 { return m_nbins; }

References m_nbins.

Referenced by binBoundaries(), binCenters(), clip(), equals(), and print().

◆ 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_end

double FixedBinAxis::m_end
private

Definition at line 61 of file FixedBinAxis.h.

Referenced by bin(), clip(), clone(), equals(), findClosestIndex(), max(), and operator[]().

◆ 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().

◆ m_nbins

size_t FixedBinAxis::m_nbins
private

Definition at line 59 of file FixedBinAxis.h.

Referenced by bin(), clip(), clone(), findClosestIndex(), operator[](), and size().

◆ m_start

double FixedBinAxis::m_start
private

Definition at line 60 of file FixedBinAxis.h.

Referenced by bin(), clip(), clone(), equals(), findClosestIndex(), min(), and operator[]().


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