libimgdoc2
Loading...
Searching...
No Matches
ITileCoordinate.h
1// SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <functional>
8#include <vector>
9#include "types.h"
10
11namespace imgdoc2
12{
16 {
17 public:
22 virtual bool TryGetCoordinate(imgdoc2::Dimension dim, int* coordVal) const = 0;
23
27 virtual void EnumCoordinates(const std::function<bool(imgdoc2::Dimension, int)>& f) const = 0;
28
29 virtual ~ITileCoordinate() = default;
30
34 inline void EnumDimensions(const std::function<bool(imgdoc2::Dimension)>& f) const
35 {
36 this->EnumCoordinates([&](imgdoc2::Dimension d, int v)->bool {return f(d); });
37 }
38
48 [[nodiscard]] static bool AreEqual(const ITileCoordinate* a, const ITileCoordinate* b)
49 {
50 if (a == nullptr || b == nullptr)
51 {
52 return false;
53 }
54
55 if (a == b)
56 {
57 return true;
58 }
59
60 bool are_equal = true;
61
62 // first, we enumerate the dimensions in a and check if they are also in b (and have the same value)
64 [=, &are_equal](imgdoc2::Dimension dimension, int value_a)->bool
65 {
66 int value_b;
67 if (!b->TryGetCoordinate(dimension, &value_b))
68 {
69 are_equal = false;
70 return false;
71 }
72
73 if (value_a != value_b)
74 {
75 are_equal = false;
76 return false;
77 }
78
79 return true;
80 });
81
82 if (are_equal)
83 {
84 // Ok, this means that for all dimensions present in a, we have the same value in b (and dimension is also present in b of course).
85 // However, it may be that b has more dimensions than a. So we need to check that as well (but we don't need to check the equality
86 // of the values anymore this time).
88 [=, &are_equal](imgdoc2::Dimension dimension, int)->bool
89 {
90 if (!a->TryGetCoordinate(dimension, nullptr))
91 {
92 are_equal = false;
93 return false;
94 }
95
96 return true;
97 });
98 }
99
100 return are_equal;
101 }
102
106 bool operator==(const ITileCoordinate& other) const
107 {
108 return ITileCoordinate::AreEqual(this, &other);
109 }
110
114 bool operator!=(const ITileCoordinate& other) const
115 {
116 return !ITileCoordinate::AreEqual(this, &other);
117 }
118
121 [[nodiscard]] std::vector<imgdoc2::Dimension> GetDimensions() const
122 {
123 std::vector<imgdoc2::Dimension> vec;
124 this->EnumDimensions(
125 [&vec](imgdoc2::Dimension d)->bool
126 {
127 vec.push_back(d);
128 return true;
129 });
130
131 return vec;
132 }
133
137 [[nodiscard]] inline static bool IsValidDimension(imgdoc2::Dimension dimension)
138 {
139 return imgdoc2::IsDimensionValid(dimension);
140 }
141 };
142
145 {
146 public:
148 virtual void Clear() = 0;
149
153 virtual void Set(imgdoc2::Dimension d, int value) = 0;
154
155 ~ITileCoordinateMutate() override = default;
156 };
157}
This interface provides modify access to the object and allows to mutate the object.
Definition: ITileCoordinate.h:145
virtual void Set(imgdoc2::Dimension d, int value)=0
virtual void Clear()=0
Clears this object to its blank/initial state.
Definition: ITileCoordinate.h:16
bool operator==(const ITileCoordinate &other) const
Definition: ITileCoordinate.h:106
virtual bool TryGetCoordinate(imgdoc2::Dimension dim, int *coordVal) const =0
bool operator!=(const ITileCoordinate &other) const
Definition: ITileCoordinate.h:114
static bool AreEqual(const ITileCoordinate *a, const ITileCoordinate *b)
Definition: ITileCoordinate.h:48
void EnumDimensions(const std::function< bool(imgdoc2::Dimension)> &f) const
Definition: ITileCoordinate.h:34
virtual void EnumCoordinates(const std::function< bool(imgdoc2::Dimension, int)> &f) const =0
static bool IsValidDimension(imgdoc2::Dimension dimension)
Definition: ITileCoordinate.h:137
std::vector< imgdoc2::Dimension > GetDimensions() const
Definition: ITileCoordinate.h:121