@@ -1,58 +1,62 | |||
|
1 | 1 | #ifndef SCIQLOP_OPTIONALAXIS_H |
|
2 | 2 | #define SCIQLOP_OPTIONALAXIS_H |
|
3 | 3 | |
|
4 | 4 | #include "CoreGlobal.h" |
|
5 | 5 | #include "Unit.h" |
|
6 | 6 | |
|
7 | 7 | #include <memory> |
|
8 | 8 | |
|
9 | 9 | template <int Dim> |
|
10 | 10 | class ArrayData; |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * @brief The OptionalAxis class defines an optional data axis for a series of data. |
|
14 | 14 | * |
|
15 | 15 | * An optional data axis is an axis that can be defined or not for a data series. If defined, it |
|
16 | 16 | * contains a unit and data (1-dim ArrayData). It is then possible to access the data or the unit. |
|
17 | 17 | * In the case of an undefined axis, the axis has no data and no unit. The methods for accessing the |
|
18 | 18 | * data or the unit are always callable but will return undefined values. |
|
19 | 19 | * |
|
20 | 20 | * @sa DataSeries |
|
21 | 21 | * @sa ArrayData |
|
22 | 22 | */ |
|
23 | 23 | class SCIQLOP_CORE_EXPORT OptionalAxis { |
|
24 | 24 | public: |
|
25 | 25 | /// Ctor for an undefined axis |
|
26 | 26 | explicit OptionalAxis(); |
|
27 | 27 | /// Ctor for a defined axis |
|
28 | 28 | /// @param data the axis' data |
|
29 | 29 | /// @param unit the axis' unit |
|
30 | 30 | /// @throws std::invalid_argument if no data is associated to the axis |
|
31 | 31 | explicit OptionalAxis(std::shared_ptr<ArrayData<1> > data, Unit unit); |
|
32 | 32 | |
|
33 | 33 | /// Copy ctor |
|
34 | 34 | OptionalAxis(const OptionalAxis &other); |
|
35 | 35 | /// Assignment operator |
|
36 | 36 | OptionalAxis &operator=(OptionalAxis other); |
|
37 | 37 | |
|
38 | 38 | /// @return the flag that indicates if the axis is defined or not |
|
39 | 39 | bool isDefined() const; |
|
40 | 40 | |
|
41 | 41 | /// @return gets the data at the index passed in parameter, NaN if the index is outside the |
|
42 | 42 | /// bounds of the axis, or if the axis is undefined |
|
43 | 43 | double at(int index) const; |
|
44 | ||
|
45 | ///@return the min and max values of the data on the axis, NaN values if there is no data | |
|
46 | std::pair<double, double> bounds() const; | |
|
47 | ||
|
44 | 48 | /// @return the number of data on the axis, 0 if the axis is not defined |
|
45 | 49 | int size() const; |
|
46 | 50 | /// @return the unit of the axis, an empty unit if the axis is not defined |
|
47 | 51 | Unit unit() const; |
|
48 | 52 | |
|
49 | 53 | bool operator==(const OptionalAxis &other); |
|
50 | 54 | bool operator!=(const OptionalAxis &other); |
|
51 | 55 | |
|
52 | 56 | private: |
|
53 | 57 | bool m_Defined; ///< Axis is defined or not |
|
54 | 58 | std::shared_ptr<ArrayData<1> > m_Data; ///< Axis' data |
|
55 | 59 | Unit m_Unit; ///< Axis' unit |
|
56 | 60 | }; |
|
57 | 61 | |
|
58 | 62 | #endif // SCIQLOP_OPTIONALAXIS_H |
@@ -1,74 +1,99 | |||
|
1 | 1 | #include <Data/OptionalAxis.h> |
|
2 | 2 | |
|
3 | 3 | #include "Data/ArrayData.h" |
|
4 | 4 | |
|
5 | 5 | OptionalAxis::OptionalAxis() : m_Defined{false}, m_Data{nullptr}, m_Unit{} |
|
6 | 6 | { |
|
7 | 7 | } |
|
8 | 8 | |
|
9 | 9 | OptionalAxis::OptionalAxis(std::shared_ptr<ArrayData<1> > data, Unit unit) |
|
10 | 10 | : m_Defined{true}, m_Data{data}, m_Unit{std::move(unit)} |
|
11 | 11 | { |
|
12 | 12 | if (m_Data == nullptr) { |
|
13 | 13 | throw std::invalid_argument{"Data can't be null for a defined axis"}; |
|
14 | 14 | } |
|
15 | 15 | } |
|
16 | 16 | |
|
17 | 17 | OptionalAxis::OptionalAxis(const OptionalAxis &other) |
|
18 | 18 | : m_Defined{other.m_Defined}, |
|
19 | 19 | m_Data{other.m_Data ? std::make_shared<ArrayData<1> >(*other.m_Data) : nullptr}, |
|
20 | 20 | m_Unit{other.m_Unit} |
|
21 | 21 | { |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | OptionalAxis &OptionalAxis::operator=(OptionalAxis other) |
|
25 | 25 | { |
|
26 | 26 | std::swap(m_Defined, other.m_Defined); |
|
27 | 27 | std::swap(m_Data, other.m_Data); |
|
28 | 28 | std::swap(m_Unit, other.m_Unit); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | bool OptionalAxis::isDefined() const |
|
32 | 32 | { |
|
33 | 33 | return m_Defined; |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | double OptionalAxis::at(int index) const |
|
37 | 37 | { |
|
38 | 38 | if (m_Defined) { |
|
39 | 39 | return (index >= 0 && index < m_Data->size()) ? m_Data->at(index) |
|
40 | 40 | : std::numeric_limits<double>::quiet_NaN(); |
|
41 | 41 | } |
|
42 | 42 | else { |
|
43 | 43 | return std::numeric_limits<double>::quiet_NaN(); |
|
44 | 44 | } |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | std::pair<double, double> OptionalAxis::bounds() const | |
|
48 | { | |
|
49 | if (!m_Defined || m_Data->size() == 0) { | |
|
50 | return std::make_pair(std::numeric_limits<double>::quiet_NaN(), | |
|
51 | std::numeric_limits<double>::quiet_NaN()); | |
|
52 | } | |
|
53 | else { | |
|
54 | ||
|
55 | auto minIt = std::min_element( | |
|
56 | m_Data->cbegin(), m_Data->cend(), [](const auto &it1, const auto &it2) { | |
|
57 | return SortUtils::minCompareWithNaN(it1.first(), it2.first()); | |
|
58 | }); | |
|
59 | ||
|
60 | // Gets the iterator on the max of all values data | |
|
61 | auto maxIt = std::max_element( | |
|
62 | m_Data->cbegin(), m_Data->cend(), [](const auto &it1, const auto &it2) { | |
|
63 | return SortUtils::maxCompareWithNaN(it1.first(), it2.first()); | |
|
64 | }); | |
|
65 | ||
|
66 | auto pair = std::make_pair(minIt->first(), maxIt->first()); | |
|
67 | ||
|
68 | return std::make_pair(minIt->first(), maxIt->first()); | |
|
69 | } | |
|
70 | } | |
|
71 | ||
|
47 | 72 | int OptionalAxis::size() const |
|
48 | 73 | { |
|
49 | 74 | return m_Defined ? m_Data->size() : 0; |
|
50 | 75 | } |
|
51 | 76 | |
|
52 | 77 | Unit OptionalAxis::unit() const |
|
53 | 78 | { |
|
54 | 79 | return m_Defined ? m_Unit : Unit{}; |
|
55 | 80 | } |
|
56 | 81 | |
|
57 | 82 | bool OptionalAxis::operator==(const OptionalAxis &other) |
|
58 | 83 | { |
|
59 | 84 | // Axis not defined |
|
60 | 85 | if (!m_Defined) { |
|
61 | 86 | return !other.m_Defined; |
|
62 | 87 | } |
|
63 | 88 | |
|
64 | 89 | // Axis defined |
|
65 | 90 | return m_Unit == other.m_Unit |
|
66 | 91 | && std::equal( |
|
67 | 92 | m_Data->cbegin(), m_Data->cend(), other.m_Data->cbegin(), other.m_Data->cend(), |
|
68 | 93 | [](const auto &it1, const auto &it2) { return it1.values() == it2.values(); }); |
|
69 | 94 | } |
|
70 | 95 | |
|
71 | 96 | bool OptionalAxis::operator!=(const OptionalAxis &other) |
|
72 | 97 | { |
|
73 | 98 | return !(*this == other); |
|
74 | 99 | } |
General Comments 0
You need to be logged in to leave comments.
Login now