Mantid
Loading...
Searching...
No Matches
TextAxis.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
10#include "MantidAPI/TextAxis.h"
14
15namespace Mantid::API {
16
20TextAxis::TextAxis(const std::size_t &length) : Axis() { m_values.resize(length); }
21
26Axis *TextAxis::clone(const MatrixWorkspace *const parentWorkspace) {
27 UNUSED_ARG(parentWorkspace)
28 return new TextAxis(*this);
29}
30
31Axis *TextAxis::clone(const std::size_t length, const MatrixWorkspace *const parentWorkspace) {
32 UNUSED_ARG(parentWorkspace)
33 auto newAxis = new TextAxis(*this);
34 newAxis->m_values.clear();
35 newAxis->m_values.resize(length);
36 return newAxis;
37}
38
46double TextAxis::operator()(const std::size_t &index, const std::size_t &verticalIndex) const {
47 UNUSED_ARG(verticalIndex)
48 if (index >= length()) {
49 throw Kernel::Exception::IndexError(index, length() - 1, "TextAxis: Index out of range.");
50 }
51
52 return EMPTY_DBL();
53}
54
60void TextAxis::setValue(const std::size_t &index, const double &value) {
63 throw std::domain_error("setValue method cannot be used on a TextAxis.");
64}
68size_t TextAxis::indexOfValue(const double value) const {
69 std::vector<double> spectraNumbers;
70 spectraNumbers.reserve(length());
71 for (size_t i = 0; i < length(); i++) {
72 spectraNumbers.emplace_back(static_cast<double>(i));
73 }
75}
76
81bool TextAxis::operator==(const Axis &axis2) const {
82 if (length() != axis2.length()) {
83 return false;
84 }
85 const auto *spec2 = dynamic_cast<const TextAxis *>(&axis2);
86 if (!spec2) {
87 return false;
88 }
89 return std::equal(m_values.begin(), m_values.end(), spec2->m_values.begin());
90}
91
97std::string TextAxis::label(const std::size_t &index) const { return m_values.at(index); }
98
104void TextAxis::setLabel(const std::size_t &index, const std::string &lbl) {
105 if (index >= length()) {
106 throw Kernel::Exception::IndexError(index, length() - 1, "TextAxis: Index out of range.");
107 }
108
109 m_values[index] = lbl;
110}
111
113double TextAxis::getMin() const {
114 try {
115 return boost::lexical_cast<double>(m_values.front());
116 } catch (boost::bad_lexical_cast &) {
117 return 0; // Default min
118 }
119}
120
122double TextAxis::getMax() const {
123 try {
124 return boost::lexical_cast<double>(m_values.back());
125 } catch (boost::bad_lexical_cast &) {
126 return getMin() + 1; // Default max
127 }
128}
129
130} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
IntArray spectraNumbers
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
Class to represent the axis of a workspace.
Definition: Axis.h:30
virtual std::size_t length() const =0
Get the length of the axis.
Base MatrixWorkspace Abstract Class.
Class to represent a text axis of a workspace.
Definition: TextAxis.h:36
Axis * clone(const MatrixWorkspace *const parentWorkspace) override
Virtual constructor.
Definition: TextAxis.cpp:26
double getMax() const override
returns max value defined on axis
Definition: TextAxis.cpp:122
double getMin() const override
returns min value defined on axis
Definition: TextAxis.cpp:113
std::vector< std::string > m_values
A vector holding the axis values for the axis.
Definition: TextAxis.h:62
void setValue(const std::size_t &index, const double &value) override
Set the value at the specified index.
Definition: TextAxis.cpp:60
std::size_t length() const override
Get the length of the axis.
Definition: TextAxis.h:41
size_t indexOfValue(const double value) const override
Returns the value that has been passed to it as a size_t.
Definition: TextAxis.cpp:68
double operator()(const std::size_t &index, const std::size_t &verticalIndex=0) const override
Get a value at the specified index.
Definition: TextAxis.cpp:46
void setLabel(const std::size_t &index, const std::string &lbl)
Set the label at the given index.
Definition: TextAxis.cpp:104
std::string label(const std::size_t &index) const override
Get the label at the specified index.
Definition: TextAxis.cpp:97
TextAxis(const std::size_t &length)
Constructor.
Definition: TextAxis.cpp:20
bool operator==(const Axis &) const override
Check if two axis defined as spectra or numeric axis are equivalent.
Definition: TextAxis.cpp:81
Exception for index errors.
Definition: Exception.h:284
size_t MANTID_KERNEL_DLL indexOfValueFromCenters(const std::vector< double > &bin_centers, const double value)
Gets the bin of a value from a vector of bin centers and throws exception if out of range.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43