BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
IAxis.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Axis/IAxis.h
6 //! @brief Defines interface IAxis.
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 #ifndef USER_API
16 #ifndef BORNAGAIN_BASE_AXIS_IAXIS_H
17 #define BORNAGAIN_BASE_AXIS_IAXIS_H
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 class Bin1D;
24 
25 //! Abstract base class for one-dimensional axes.
26 
27 class IAxis {
28 public:
29  IAxis(std::string name)
30  : m_name(std::move(name))
31  {
32  }
33  IAxis(const IAxis&) = delete;
34  virtual ~IAxis();
35 
36  virtual IAxis* clone() const = 0;
37 
38  //! Sets the axis label
39  void setAxisName(std::string name) { m_name = name; }
40 
41  //! Returns the number of bins
42  virtual size_t size() const = 0;
43 
44  //! Returns value of first point of axis
45  virtual double min() const = 0;
46 
47  //! Returns value of last point of axis
48  virtual double max() const = 0;
49 
50  //! Returns lower and upper bound in a pair.
51  //! first is lower, second is upper.
52  std::pair<double, double> bounds() const;
53 
54  //! Returns distance from first to last point
55  double span() const;
56 
57  //! Returns midpoint of axis
58  double center() const;
59 
60  //! Returns the label of the axis
61  std::string axisName() const { return m_name; }
62 
63  virtual std::vector<double> binCenters() const;
64 
65  virtual std::vector<double> binBoundaries() const;
66 
67  //! indexed accessor retrieves a sample
68  virtual double operator[](size_t index) const = 0;
69 
70  //! retrieve a 1d bin for the given index
71  virtual Bin1D bin(size_t index) const = 0;
72 
73  virtual double binCenter(size_t index) const = 0;
74 
75  //! find bin index which is best match for given value
76  virtual size_t findClosestIndex(double value) const = 0;
77 
78  //! Returns true if axis contains given point
79  virtual bool contains(double value) const;
80 
81  //! Clips this axis to the given values
82  virtual void clip(double lower, double upper);
83 
84  //! Convenience overload to clip this axis to the given values.
85  //! bounds.first is lower, bounds.second is upper value.
86  void clip(std::pair<double, double> bounds);
87 
88  //! test for equality
89  bool operator==(const IAxis& right) const { return equals(right); }
90  bool operator!=(const IAxis& right) const { return !(*this == right); }
91 
92  friend std::ostream& operator<<(std::ostream& ostr, const IAxis& m)
93  {
94  m.print(ostr);
95  return ostr;
96  }
97 
98 protected:
99  virtual void print(std::ostream& ostr) const = 0;
100  virtual bool equals(const IAxis& other) const; // overloaded in child classes
101 
102 private:
103  std::string m_name; //!< axis name
104 };
105 
106 #endif // BORNAGAIN_BASE_AXIS_IAXIS_H
107 #endif // USER_API
Definition: Bin.h:20
Abstract base class for one-dimensional axes.
Definition: IAxis.h:27
double span() const
Returns distance from first to last point.
Definition: IAxis.cpp:55
virtual bool contains(double value) const
Returns true if axis contains given point.
Definition: IAxis.cpp:45
void setAxisName(std::string name)
Sets the axis label.
Definition: IAxis.h:39
virtual std::vector< double > binCenters() const
Definition: IAxis.cpp:25
bool operator==(const IAxis &right) const
test for equality
Definition: IAxis.h:89
virtual std::vector< double > binBoundaries() const
Definition: IAxis.cpp:30
virtual size_t findClosestIndex(double value) const =0
find bin index which is best match for given value
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:20
virtual double max() const =0
Returns value of last point of axis.
virtual void print(std::ostream &ostr) const =0
virtual IAxis * clone() const =0
friend std::ostream & operator<<(std::ostream &ostr, const IAxis &m)
Definition: IAxis.h:92
virtual ~IAxis()
virtual double min() const =0
Returns value of first point of axis.
virtual Bin1D bin(size_t index) const =0
retrieve a 1d bin for the given index
virtual size_t size() const =0
Returns the number of bins.
std::string m_name
axis name
Definition: IAxis.h:103
std::pair< double, double > bounds() const
Returns lower and upper bound in a pair. first is lower, second is upper.
Definition: IAxis.cpp:50
IAxis(const IAxis &)=delete
virtual double operator[](size_t index) const =0
indexed accessor retrieves a sample
IAxis(std::string name)
Definition: IAxis.h:29
bool operator!=(const IAxis &right) const
Definition: IAxis.h:90
virtual double binCenter(size_t index) const =0
std::string axisName() const
Returns the label of the axis.
Definition: IAxis.h:61
virtual void clip(double lower, double upper)
Clips this axis to the given values.
Definition: IAxis.cpp:35
double center() const
Returns midpoint of axis.
Definition: IAxis.cpp:60