BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
FrameUtil.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Axis/FrameUtil.cpp
6 //! @brief Implements namespace DataUtils.
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 #include "Base/Axis/FrameUtil.h"
16 #include "Base/Axis/Bin.h"
17 #include "Base/Axis/Frame.h"
18 #include "Base/Axis/IAxis.h"
19 
20 // For axis FixedBinAxis("axis", 8, -5.0, 3.0) the coordinate x=-4.5 (center of bin #0) will
21 // be converted into 0.5 (which is a bin center expressed in bin fraction coordinates).
22 // The coordinate -5.0 (outside of axis definition) will be converted to -0.5
23 // (center of non-existing bin #-1).
24 // Used for Mask conversion.
25 
26 double FramUtil::coordinateToBinf(double coordinate, const IAxis& axis)
27 {
28  size_t index = axis.findClosestIndex(coordinate);
29  Bin1D bin = axis.bin(index);
30  double f = (coordinate - bin.m_lower) / bin.binSize();
31  return static_cast<double>(index) + f;
32 }
33 
34 double FramUtil::coordinateFromBinf(double value, const IAxis& axis)
35 {
36  int index = static_cast<int>(value);
37 
38  double result(0);
39  if (index < 0) {
40  Bin1D bin = axis.bin(0);
41  result = bin.m_lower + value * bin.binSize();
42  } else if (index >= static_cast<int>(axis.size())) {
43  Bin1D bin = axis.bin(axis.size() - 1);
44  result = bin.m_upper + (value - axis.size()) * bin.binSize();
45  } else {
46  Bin1D bin = axis.bin(static_cast<size_t>(index));
47  result = bin.m_lower + (value - static_cast<double>(index)) * bin.binSize();
48  }
49 
50  return result;
51 }
52 
53 void FramUtil::coordinatesToBinf(double& x, double& y, const Frame& data)
54 {
55  x = coordinateToBinf(x, data.xAxis());
56  y = coordinateToBinf(y, data.yAxis());
57 }
58 
59 void FramUtil::coordinatesFromBinf(double& x, double& y, const Frame& data)
60 {
61  x = coordinateFromBinf(x, data.xAxis());
62  y = coordinateFromBinf(y, data.yAxis());
63 }
Defines structs Bin1D, Bin1DCVector.
Defines namespace DataUtils.
Defines and implements templated class Frame.
Defines interface IAxis.
Definition: Bin.h:20
double binSize() const
Definition: Bin.h:31
double m_upper
upper bound of the bin
Definition: Bin.h:29
double m_lower
lower bound of the bin
Definition: Bin.h:28
Holds one or two axes.
Definition: Frame.h:27
const IAxis & xAxis() const
Definition: Frame.h:47
const IAxis & yAxis() const
Definition: Frame.h:48
Abstract base class for one-dimensional axes.
Definition: IAxis.h:27
virtual size_t findClosestIndex(double value) const =0
find bin index which is best match for given value
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.
void coordinatesToBinf(double &x, double &y, const Frame &frame)
Transforms x,y coordinate from Datafield axes coordinates to bin-fraction-coordinates.
Definition: FrameUtil.cpp:53
void coordinatesFromBinf(double &x, double &y, const Frame &frame)
Transforms x,y coordinate from bin-fraction-coordinates to Datafield's axes coordinates.
Definition: FrameUtil.cpp:59
double coordinateToBinf(double coordinate, const IAxis &axis)
Transforms coordinate on axis into the bin-fraction-coordinate.
Definition: FrameUtil.cpp:26
double coordinateFromBinf(double value, const IAxis &axis)
Transforms bin-fraction-coordinate into axis coordinate.
Definition: FrameUtil.cpp:34