LSST Applications g00274db5b6+edbf708997,g00d0e8bbd7+edbf708997,g199a45376c+5137f08352,g1fd858c14a+1d4b6db739,g262e1987ae+f4d9505c4f,g29ae962dfc+7156fb1a53,g2cef7863aa+73c82f25e4,g35bb328faa+edbf708997,g3e17d7035e+5b3adc59f5,g3fd5ace14f+852fa6fbcb,g47891489e3+6dc8069a4c,g53246c7159+edbf708997,g64539dfbff+9f17e571f4,g67b6fd64d1+6dc8069a4c,g74acd417e5+ae494d68d9,g786e29fd12+af89c03590,g7ae74a0b1c+a25e60b391,g7aefaa3e3d+536efcc10a,g7cc15d900a+d121454f8d,g87389fa792+a4172ec7da,g89139ef638+6dc8069a4c,g8d7436a09f+28c28d8d6d,g8ea07a8fe4+db21c37724,g92c671f44c+9f17e571f4,g98df359435+b2e6376b13,g99af87f6a8+b0f4ad7b8d,gac66b60396+966efe6077,gb88ae4c679+7dec8f19df,gbaa8f7a6c5+38b34f4976,gbf99507273+edbf708997,gc24b5d6ed1+9f17e571f4,gca7fc764a6+6dc8069a4c,gcc769fe2a4+97d0256649,gd7ef33dd92+6dc8069a4c,gdab6d2f7ff+ae494d68d9,gdbb4c4dda9+9f17e571f4,ge410e46f29+6dc8069a4c,geaed405ab2+e194be0d2b,w.2025.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
MaskDict.cc
Go to the documentation of this file.
1/*
2 * Developed for the LSST Data Management System.
3 * This product includes software developed by the LSST Project
4 * (https://www.lsst.org).
5 * See the COPYRIGHT file at the top-level directory of this distribution
6 * for details of code ownership.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <mutex>
23#include <set>
24
25#include "boost/functional/hash.hpp"
27
28namespace lsst {
29namespace afw {
30namespace image {
31namespace detail {
32
33// A thread-safe singleton that manages MaskDict's global state.
35public:
36 static GlobalState &get() {
37 static GlobalState instance;
38 return instance;
39 }
40
43 if (!mpd.empty()) {
44 std::shared_ptr<MaskDict> dict(new MaskDict(mpd));
45 _allMaskDicts.insert(dict);
46 return dict;
47 }
48 return _defaultMaskDict;
49 }
50
51 std::shared_ptr<MaskDict> copy(MaskDict const &dict) {
53 std::shared_ptr<MaskDict> result(new MaskDict(dict));
54 _allMaskDicts.insert(result);
55 return result;
56 }
57
58 std::shared_ptr<MaskDict> getDefault() const noexcept { return _defaultMaskDict; }
59
60 void setDefault(std::shared_ptr<MaskDict> dict) { _defaultMaskDict = std::move(dict); }
61
64 _defaultMaskDict = copy(*_defaultMaskDict);
65 return _defaultMaskDict;
66 }
67
68 template <typename Functor>
69 void forEachMaskDict(Functor functor) {
71 _prune(); // guarantees dereference below is safe
72 for (auto const &ptr : _allMaskDicts) {
73 functor(*ptr.lock());
74 }
75 }
76
77private:
78 GlobalState() : _defaultMaskDict(new MaskDict()) {
79 _allMaskDicts.insert(_defaultMaskDict);
80 _defaultMaskDict->_addInitialMaskPlanes();
81 }
82
83 GlobalState(GlobalState const &) = delete;
84 GlobalState(GlobalState &&) = delete;
85
86 GlobalState &operator=(GlobalState const &) = delete;
87 GlobalState &operator=(GlobalState &&) = delete;
88
89 ~GlobalState() = default;
90
91 // Prune expired weak_ptrs from _allMaskDicts. Not thread safe; should
92 // only be called by routines that have already locked the mutex.
93 void _prune() {
94 for (auto iter = _allMaskDicts.begin(); iter != _allMaskDicts.end();) {
95 if (iter->expired()) {
96 iter = _allMaskDicts.erase(iter);
97 } else {
98 ++iter;
99 }
100 }
101 }
102
103 std::recursive_mutex _mutex; // guards _allMaskDicts and synchronizes updates to it and _defaultMaskDict
105 std::shared_ptr<MaskDict> _defaultMaskDict;
106};
107
111
113
115
117
118void MaskDict::addAllMasksPlane(std::string const &name, int bitId) {
119 GlobalState::get().forEachMaskDict([&name, bitId](MaskDict &dict) {
120 auto const found = std::find_if(dict.begin(), dict.end(),
121 [bitId](auto const &item) { return item.second == bitId; });
122 if (found == dict.end()) {
123 // is name already in use?
124 if (dict.find(name) == dict.end()) {
125 dict.add(name, bitId);
126 }
127 }
128 });
129}
130
131MaskDict::~MaskDict() noexcept = default;
132
133std::shared_ptr<MaskDict> MaskDict::clone() const { return GlobalState::get().copy(*this); }
134
136 if (empty()) {
137 return 0;
138 }
139
140 auto const maxIter = std::max_element(begin(), end(),
141 [](auto const &a, auto const &b) { return a.second < b.second; });
142 assert(maxIter != end());
143 int id = maxIter->second + 1; // The maskPlane to use if there are no gaps
144
145 for (int i = 0; i < id; ++i) {
146 // is i already used in this Mask?
147 auto const sameIter =
148 std::find_if(begin(), end(), [i](auto const &item) { return item.second == i; });
149 if (sameIter == end()) { // Not used; so we'll use it
150 return i;
151 }
152 }
153
154 return id;
155}
156
157int MaskDict::getMaskPlane(std::string const &name) const {
158 auto iter = find(name);
159 return (iter == end()) ? -1 : iter->second;
160}
161
162void MaskDict::print() const {
163 for (auto const &item : *this) {
164 std::cout << "Plane " << item.second << " -> " << item.first << std::endl;
165 }
166}
167
168bool MaskDict::operator==(MaskDict const &rhs) const {
169 return this == &rhs || (_hash == rhs._hash && _dict == rhs._dict);
170}
171
172void MaskDict::add(std::string const &name, int bitId) {
173 _dict[name] = bitId;
174 _hash = boost::hash<MaskPlaneDict>()(_dict);
175}
176
177void MaskDict::erase(std::string const &name) {
178 _dict.erase(name);
179 _hash = boost::hash<MaskPlaneDict>()(_dict);
180}
181
183 _dict.clear();
184 _hash = 0x0;
185}
186
187void MaskDict::_addInitialMaskPlanes() {
188 int i = -1;
189 _dict["BAD"] = ++i;
190 _dict["SAT"] = ++i;
191 _dict["INTRP"] = ++i;
192 _dict["CR"] = ++i;
193 _dict["EDGE"] = ++i;
194 _dict["DETECTED"] = ++i;
195 _dict["DETECTED_NEGATIVE"] = ++i;
196 _dict["SUSPECT"] = ++i;
197 _dict["NO_DATA"] = ++i;
198 _dict["VIGNETTED"] = ++i;
199 _dict["STREAK"] = ++i;
200 _hash = boost::hash<MaskPlaneDict>()(_dict);
201}
202
203MaskDict::MaskDict() : _dict(), _hash(0x0) {}
204
205MaskDict::MaskDict(MaskPlaneDict const &dict) : _dict(dict), _hash(boost::hash<MaskPlaneDict>()(_dict)) {}
206
207} // namespace detail
208} // namespace image
209} // namespace afw
210} // namespace lsst
std::shared_ptr< MaskDict > getDefault() const noexcept
Definition MaskDict.cc:58
std::shared_ptr< MaskDict > copyOrGetDefault(MaskPlaneDict const &mpd)
Definition MaskDict.cc:41
std::shared_ptr< MaskDict > copy(MaskDict const &dict)
Definition MaskDict.cc:51
void setDefault(std::shared_ptr< MaskDict > dict)
Definition MaskDict.cc:60
std::shared_ptr< MaskDict > detachDefault()
Definition MaskDict.cc:62
int getMaskPlane(std::string const &name) const
Definition MaskDict.cc:157
const_iterator begin() const noexcept
Definition MaskDict.h:131
static void addAllMasksPlane(std::string const &name, int bitId)
Definition MaskDict.cc:118
static std::shared_ptr< MaskDict > detachDefault()
Definition MaskDict.cc:116
const_iterator end() const noexcept
Definition MaskDict.h:132
static std::shared_ptr< MaskDict > getDefault()
Definition MaskDict.cc:112
std::shared_ptr< MaskDict > clone() const
Definition MaskDict.cc:133
static void setDefault(std::shared_ptr< MaskDict > dict)
Definition MaskDict.cc:114
bool operator==(MaskDict const &rhs) const
Definition MaskDict.cc:168
const_iterator find(std::string const &name) const
Definition MaskDict.h:135
void add(std::string const &name, int bitId)
Definition MaskDict.cc:172
static std::shared_ptr< MaskDict > copyOrGetDefault(MaskPlaneDict const &dict)
Definition MaskDict.cc:108
bool empty() const noexcept
Definition MaskDict.h:141
void erase(std::string const &name)
Definition MaskDict.cc:177
T empty(T... args)
T endl(T... args)
T find_if(T... args)
T max_element(T... args)
T move(T... args)
std::map< std::string, int > MaskPlaneDict
Definition Mask.h:58
STL namespace.