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

Description

Axis with fixed bin size in sin(angle) space.

Definition at line 23 of file ConstKBinAxis.h.

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

Public Member Functions

 ConstKBinAxis (const std::string &name, size_t nbins, double start, double end)
 ConstKBinAxis constructor. More...
 
 ~ConstKBinAxis () 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...
 
ConstKBinAxisclone () 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

 ConstKBinAxis (const std::string &name, size_t nbins)
 
bool equals (const IAxis &other) const override
 
void print (std::ostream &ostr) const override
 
void setBinBoundaries (const std::vector< double > &bin_boundaries)
 

Protected Attributes

double m_end
 
size_t m_nbins
 
double m_start
 

Private Attributes

std::vector< double > m_bin_boundaries
 vector containing the bin limits More...
 
std::string m_name
 axis name More...
 

Constructor & Destructor Documentation

◆ ConstKBinAxis() [1/2]

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

ConstKBinAxis constructor.

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

Definition at line 28 of file ConstKBinAxis.cpp.

29  : VariableBinAxis(name, nbins)
30  , m_start(start)
31  , m_end(end)
32 {
33  if (m_start >= m_end)
34  throw std::runtime_error(
35  "ConstKBinAxis::ConstKBinAxis() -> Error. start >= end is not allowed.");
36 
37  double start_sin = std::sin(m_start);
38  double end_sin = std::sin(m_end);
39  double step = (end_sin - start_sin) / (m_nbins);
40 
41  std::vector<double> bin_boundaries;
42  bin_boundaries.resize(m_nbins + 1, 0.0);
43  for (size_t i = 0; i < bin_boundaries.size(); ++i)
44  bin_boundaries[i] = std::asin(start_sin + step * i);
45  setBinBoundaries(bin_boundaries);
46 }
void setBinBoundaries(const std::vector< double > &bin_boundaries)
VariableBinAxis(const std::string &name, const std::vector< double > &bin_boundaries)
VariableBinAxis constructor.

References m_end, VariableBinAxis::m_nbins, m_start, and VariableBinAxis::setBinBoundaries().

Referenced by clone().

Here is the call graph for this function:

◆ ~ConstKBinAxis()

ConstKBinAxis::~ConstKBinAxis ( )
overridedefault

◆ ConstKBinAxis() [2/2]

ConstKBinAxis::ConstKBinAxis ( const std::string &  name,
size_t  nbins 
)
protected

Definition at line 21 of file ConstKBinAxis.cpp.

22  : VariableBinAxis(name, nbins)
23  , m_start(0)
24  , m_end(0)
25 {
26 }

Member Function Documentation

◆ axisName()

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

◆ bin()

Bin1D VariableBinAxis::bin ( size_t  index) const
overridevirtualinherited

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 45 of file VariableBinAxis.cpp.

46 {
47  if (index >= m_nbins)
48  throw std::runtime_error("VariableBinAxis::bin() -> Error. Wrong index.");
49 
50  Bin1D result(m_bin_boundaries[index], m_bin_boundaries[index + 1]);
51  return result;
52 }
Definition: Bin.h:20
std::vector< double > m_bin_boundaries
vector containing the bin limits

References VariableBinAxis::m_bin_boundaries, and VariableBinAxis::m_nbins.

Referenced by VariableBinAxis::binCenter(), VariableBinAxis::binCenters(), clip(), VariableBinAxis::clip(), and VariableBinAxis::operator[]().

◆ binBoundaries()

std::vector<double> VariableBinAxis::binBoundaries ( ) const
inlineoverridevirtualinherited

Reimplemented from IAxis.

Definition at line 48 of file VariableBinAxis.h.

48 { return m_bin_boundaries; }

References VariableBinAxis::m_bin_boundaries.

Referenced by clip().

◆ binCenter()

double VariableBinAxis::binCenter ( size_t  index) const
overridevirtualinherited

Implements IAxis.

Definition at line 64 of file VariableBinAxis.cpp.

65 {
66  return bin(index).center();
67 }
double center() const
Definition: Bin.h:30
Bin1D bin(size_t index) const override
retrieve a 1d bin for the given index

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

Here is the call graph for this function:

◆ binCenters()

std::vector< double > VariableBinAxis::binCenters ( ) const
overridevirtualinherited

Reimplemented from IAxis.

Definition at line 86 of file VariableBinAxis.cpp.

87 {
88  std::vector<double> result;
89  result.resize(size(), 0.0);
90  for (size_t i = 0; i < size(); ++i)
91  result[i] = bin(i).center();
92  return result;
93 }
size_t size() const override
Returns the number of bins.

References VariableBinAxis::bin(), Bin1D::center(), and VariableBinAxis::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 ConstKBinAxis::clip ( double  lower,
double  upper 
)
overridevirtual

Clips this axis to the given values.

Reimplemented from IAxis.

Definition at line 53 of file ConstKBinAxis.cpp.

54 {
55  if (lower >= upper)
56  throw std::runtime_error(
57  "ConstKBinAxis::clip() -> Error. 'lower' should be smaller than 'upper'");
58 
59  if (lower < min())
60  lower = bin(0).center();
61  if (upper >= max())
62  upper = bin(size() - 1).center();
63 
64  size_t nbin1 = findClosestIndex(lower);
65  size_t nbin2 = findClosestIndex(upper);
66 
67  size_t new_nbins = nbin2 - nbin1 + 1;
68  std::vector<double> new_boundaries;
69  std::vector<double> old_boundaries = binBoundaries();
70  for (size_t i = 0; i < new_nbins + 1; ++i)
71  new_boundaries.push_back(old_boundaries[nbin1 + i]);
72 
73  m_nbins = new_nbins;
74  m_start = new_boundaries.front();
75  m_end = new_boundaries.back();
76  setBinBoundaries(new_boundaries);
77 }
double max() const override
Returns value of last point of axis.
std::vector< double > binBoundaries() const override
size_t findClosestIndex(double value) const override
find bin index which is best match for given value
double min() const override
Returns value of first point of axis.

References VariableBinAxis::bin(), VariableBinAxis::binBoundaries(), Bin1D::center(), VariableBinAxis::findClosestIndex(), m_end, VariableBinAxis::m_nbins, m_start, VariableBinAxis::max(), VariableBinAxis::min(), VariableBinAxis::setBinBoundaries(), and VariableBinAxis::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()

ConstKBinAxis * ConstKBinAxis::clone ( ) const
overridevirtual

Implements IAxis.

Definition at line 48 of file ConstKBinAxis.cpp.

49 {
50  return new ConstKBinAxis(axisName(), m_nbins, m_start, m_end);
51 }
ConstKBinAxis(const std::string &name, size_t nbins, double start, double end)
ConstKBinAxis constructor.
std::string axisName() const
Returns the label of the axis.
Definition: IAxis.h:61

References ConstKBinAxis(), IAxis::axisName(), m_end, VariableBinAxis::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 ConstKBinAxis::equals ( const IAxis other) const
overrideprotectedvirtual

Reimplemented from IAxis.

Definition at line 79 of file ConstKBinAxis.cpp.

80 {
81  if (!IAxis::equals(other))
82  return false;
83  if (const auto* otherAxis = dynamic_cast<const ConstKBinAxis*>(&other)) {
84  if (size() != otherAxis->size())
85  return false;
86  if (!BaseUtils::algo::almostEqual(m_start, otherAxis->m_start))
87  return false;
88  if (!BaseUtils::algo::almostEqual(m_end, otherAxis->m_end))
89  return false;
90  return true;
91  }
92  return false;
93 }
Axis with fixed bin size in sin(angle) space.
Definition: ConstKBinAxis.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 VariableBinAxis::size().

Here is the call graph for this function:

◆ findClosestIndex()

size_t VariableBinAxis::findClosestIndex ( double  value) const
overridevirtualinherited

find bin index which is best match for given value

Implements IAxis.

Definition at line 69 of file VariableBinAxis.cpp.

70 {
71  if (m_bin_boundaries.size() < 2)
72  throw std::runtime_error("VariableBinAxis::findClosestIndex() -> Error! "
73  "VariableBinAxis not correctly initialized");
74  if (value < min())
75  return 0;
76  if (value >= max())
77  return m_nbins - 1;
78 
79  auto top_limit = std::lower_bound(m_bin_boundaries.begin(), m_bin_boundaries.end(), value);
80  if (*top_limit != value)
81  --top_limit;
82  size_t nbin = top_limit - m_bin_boundaries.begin();
83  return nbin;
84 }

References VariableBinAxis::m_bin_boundaries, VariableBinAxis::m_nbins, VariableBinAxis::max(), and VariableBinAxis::min().

Referenced by clip(), and VariableBinAxis::clip().

Here is the call graph for this function:

◆ max()

double VariableBinAxis::max ( ) const
overridevirtualinherited

Returns value of last point of axis.

Implements IAxis.

Definition at line 59 of file VariableBinAxis.cpp.

60 {
61  return m_bin_boundaries.back();
62 }

References VariableBinAxis::m_bin_boundaries.

Referenced by clip(), VariableBinAxis::clip(), and VariableBinAxis::findClosestIndex().

◆ min()

double VariableBinAxis::min ( ) const
overridevirtualinherited

Returns value of first point of axis.

Implements IAxis.

Definition at line 54 of file VariableBinAxis.cpp.

55 {
56  return m_bin_boundaries.front();
57 }

References VariableBinAxis::m_bin_boundaries.

Referenced by clip(), VariableBinAxis::clip(), and VariableBinAxis::findClosestIndex().

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

indexed accessor retrieves a sample

Implements IAxis.

Definition at line 40 of file VariableBinAxis.cpp.

41 {
42  return bin(index).center();
43 }

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

Here is the call graph for this function:

◆ print()

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

Implements IAxis.

Definition at line 95 of file ConstKBinAxis.cpp.

96 {
97  ostr << "ConstKBinAxis(\"" << axisName() << "\", " << size() << ", "
98  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << m_start << ", " << m_end
99  << ")";
100 }

References IAxis::axisName(), m_end, m_start, and VariableBinAxis::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.

◆ setBinBoundaries()

void VariableBinAxis::setBinBoundaries ( const std::vector< double > &  bin_boundaries)
protectedinherited

Definition at line 146 of file VariableBinAxis.cpp.

147 {
148  // checking that values are sorted
149  std::vector<double> vec_sorted = bin_boundaries;
150  std::sort(vec_sorted.begin(), vec_sorted.end());
151  for (size_t i = 0; i < bin_boundaries.size(); ++i) {
152  if (vec_sorted[i] != bin_boundaries[i])
153  throw std::runtime_error("VariableBinAxis::setBinBoundaries() -> Error. "
154  "Array with bin edges is not sorted.");
155  }
156 
157  std::vector<double> vec = bin_boundaries;
158  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
159 
160  if (vec.size() != bin_boundaries.size())
161  throw std::runtime_error("VariableBinAxis::setBinBoundaries() -> Error. "
162  "Array with bin edges contains repeating values.");
163 
164  m_bin_boundaries = bin_boundaries;
165 }

References VariableBinAxis::m_bin_boundaries.

Referenced by ConstKBinAxis(), CustomBinAxis::CustomBinAxis(), VariableBinAxis::VariableBinAxis(), clip(), and VariableBinAxis::clip().

◆ size()

size_t VariableBinAxis::size ( ) const
inlineoverridevirtualinherited

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

◆ m_end

double ConstKBinAxis::m_end
protected

Definition at line 44 of file ConstKBinAxis.h.

Referenced by ConstKBinAxis(), clip(), clone(), equals(), and print().

◆ 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

◆ m_start

double ConstKBinAxis::m_start
protected

Definition at line 43 of file ConstKBinAxis.h.

Referenced by ConstKBinAxis(), clip(), clone(), equals(), and print().


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