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 TextAxis &axis2) const { return compareToTextAxis(axis2); }
82
83bool TextAxis::compareToTextAxis(const TextAxis &axis2) const {
84 if (length() != axis2.length()) {
85 return false;
86 }
87 return std::equal(m_values.begin(), m_values.end(), axis2.m_values.begin());
88}
89
94bool TextAxis::operator==(const Axis &axis2) const {
95 const auto *spec2 = dynamic_cast<const TextAxis *>(&axis2);
96 if (!spec2) {
97 return false;
98 }
99 return compareToTextAxis(*spec2);
100}
101
107std::string TextAxis::label(const std::size_t &index) const { return m_values.at(index); }
108
114void TextAxis::setLabel(const std::size_t &index, const std::string &lbl) {
115 if (index >= length()) {
116 throw Kernel::Exception::IndexError(index, length() - 1, "TextAxis: Index out of range.");
117 }
118
119 m_values[index] = lbl;
120}
121
123double TextAxis::getMin() const {
124 try {
125 return boost::lexical_cast<double>(m_values.front());
126 } catch (boost::bad_lexical_cast &) {
127 return 0; // Default min
128 }
129}
130
132double TextAxis::getMax() const {
133 try {
134 return boost::lexical_cast<double>(m_values.back());
135 } catch (boost::bad_lexical_cast &) {
136 return getMin() + 1; // Default max
137 }
138}
139
140} // namespace Mantid::API
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
IntArray spectraNumbers
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
Class to represent the axis of a workspace.
Definition Axis.h:30
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:132
double getMin() const override
returns min value defined on axis
Definition TextAxis.cpp:123
std::vector< std::string > m_values
A vector holding the axis values for the axis.
Definition TextAxis.h:63
void setValue(const std::size_t &index, const double &value) override
Set the value at the specified index.
Definition TextAxis.cpp:60
bool compareToTextAxis(const TextAxis &axis2) const
Definition TextAxis.cpp:83
std::size_t length() const override
Get the length of the axis.
Definition TextAxis.h:41
bool operator==(const TextAxis &) const
Check if two TextAxis are equivalent.
Definition TextAxis.cpp:81
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:114
std::string label(const std::size_t &index) const override
Get the label at the specified index.
Definition TextAxis.cpp:107
TextAxis(const std::size_t &length)
Constructor.
Definition TextAxis.cpp:20
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:42