lsst.afw
g3a5ebb7d8a+28b83bf6a5
Toggle main menu visibility
Loading...
Searching...
No Matches
src
image
detail
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"
26
#include "
lsst/afw/image/detail/MaskDict.h
"
27
28
namespace
lsst
{
29
namespace
afw
{
30
namespace
image
{
31
namespace
detail
{
32
33
// A thread-safe singleton that manages MaskDict's global state.
34
class
MaskDict::GlobalState
final {
35
public
:
36
static
GlobalState &
get
() {
37
static
GlobalState instance;
38
return
instance;
39
}
40
41
std::shared_ptr<MaskDict>
copyOrGetDefault
(
MaskPlaneDict
const
&mpd) {
42
std::lock_guard<std::recursive_mutex>
lock(_mutex);
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) {
52
std::lock_guard<std::recursive_mutex>
lock(_mutex);
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
62
std::shared_ptr<MaskDict>
detachDefault
() {
63
std::lock_guard<std::recursive_mutex>
lock(_mutex);
64
_defaultMaskDict =
copy
(*_defaultMaskDict);
65
return
_defaultMaskDict;
66
}
67
68
template
<
typename
Functor>
69
void
forEachMaskDict
(Functor functor) {
70
std::lock_guard<std::recursive_mutex>
lock(_mutex);
71
_prune();
// guarantees dereference below is safe
72
for
(
auto
const
&ptr : _allMaskDicts) {
73
functor(*ptr.lock());
74
}
75
}
76
77
private
:
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
104
std::set<std::weak_ptr<MaskDict>
,
std::owner_less<std::weak_ptr<MaskDict>
>> _allMaskDicts;
105
std::shared_ptr<MaskDict>
_defaultMaskDict;
106
};
107
108
std::shared_ptr<MaskDict>
MaskDict::copyOrGetDefault
(
MaskPlaneDict
const
&mpd) {
109
return
GlobalState::get
().
copyOrGetDefault
(mpd);
110
}
111
112
std::shared_ptr<MaskDict>
MaskDict::getDefault
() {
return
GlobalState::get
().
getDefault
(); }
113
114
void
MaskDict::setDefault
(
std::shared_ptr<MaskDict>
dict) {
GlobalState::get
().
setDefault
(
std::move
(dict)); }
115
116
std::shared_ptr<MaskDict>
MaskDict::detachDefault
() {
return
GlobalState::get
().
detachDefault
(); }
117
118
void
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
131
MaskDict::~MaskDict
() noexcept = default;
132
133
std
::shared_ptr<MaskDict> MaskDict::
clone
()
const
{
return
GlobalState::get
().copy(*
this
); }
134
135
int
MaskDict::getUnusedPlane
()
const
{
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
157
int
MaskDict::getMaskPlane
(
std::string
const
&name)
const
{
158
auto
iter =
find
(name);
159
return
(iter ==
end
()) ? -1 : iter->second;
160
}
161
162
void
MaskDict::print
()
const
{
163
for
(
auto
const
&item : *
this
) {
164
std::cout
<<
"Plane "
<< item.second <<
" -> "
<< item.first <<
std::endl
;
165
}
166
}
167
168
bool
MaskDict::operator==
(MaskDict
const
&rhs)
const
{
169
return
this
== &rhs || (_hash == rhs._hash && _dict == rhs._dict);
170
}
171
172
void
MaskDict::add
(
std::string
const
&name,
int
bitId) {
173
_dict[name] = bitId;
174
_hash = boost::hash<MaskPlaneDict>()(_dict);
175
}
176
177
void
MaskDict::erase
(
std::string
const
&name) {
178
_dict.
erase
(name);
179
_hash = boost::hash<MaskPlaneDict>()(_dict);
180
}
181
182
void
MaskDict::clear
() {
183
_dict.clear();
184
_hash = 0x0;
185
}
186
187
void
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
203
MaskDict::MaskDict() : _dict(), _hash(0x0) {}
204
205
MaskDict::MaskDict(
MaskPlaneDict
const
&dict) : _dict(dict), _hash(
boost
::hash<
MaskPlaneDict
>()(_dict)) {}
206
207
}
// namespace detail
208
}
// namespace image
209
}
// namespace afw
210
}
// namespace lsst
MaskDict.h
std::cout
std::string
lsst::afw::image::detail::MaskDict::GlobalState
Definition
MaskDict.cc:34
lsst::afw::image::detail::MaskDict::GlobalState::getDefault
std::shared_ptr< MaskDict > getDefault() const noexcept
Definition
MaskDict.cc:58
lsst::afw::image::detail::MaskDict::GlobalState::get
static GlobalState & get()
Definition
MaskDict.cc:36
lsst::afw::image::detail::MaskDict::GlobalState::copyOrGetDefault
std::shared_ptr< MaskDict > copyOrGetDefault(MaskPlaneDict const &mpd)
Definition
MaskDict.cc:41
lsst::afw::image::detail::MaskDict::GlobalState::copy
std::shared_ptr< MaskDict > copy(MaskDict const &dict)
Definition
MaskDict.cc:51
lsst::afw::image::detail::MaskDict::GlobalState::setDefault
void setDefault(std::shared_ptr< MaskDict > dict)
Definition
MaskDict.cc:60
lsst::afw::image::detail::MaskDict::GlobalState::forEachMaskDict
void forEachMaskDict(Functor functor)
Definition
MaskDict.cc:69
lsst::afw::image::detail::MaskDict::GlobalState::detachDefault
std::shared_ptr< MaskDict > detachDefault()
Definition
MaskDict.cc:62
lsst::afw::image::detail::MaskDict::getMaskPlane
int getMaskPlane(std::string const &name) const
Definition
MaskDict.cc:157
lsst::afw::image::detail::MaskDict::begin
const_iterator begin() const noexcept
Definition
MaskDict.h:131
lsst::afw::image::detail::MaskDict::~MaskDict
~MaskDict() noexcept
lsst::afw::image::detail::MaskDict::addAllMasksPlane
static void addAllMasksPlane(std::string const &name, int bitId)
Definition
MaskDict.cc:118
lsst::afw::image::detail::MaskDict::detachDefault
static std::shared_ptr< MaskDict > detachDefault()
Definition
MaskDict.cc:116
lsst::afw::image::detail::MaskDict::end
const_iterator end() const noexcept
Definition
MaskDict.h:132
lsst::afw::image::detail::MaskDict::getDefault
static std::shared_ptr< MaskDict > getDefault()
Definition
MaskDict.cc:112
lsst::afw::image::detail::MaskDict::clone
std::shared_ptr< MaskDict > clone() const
Definition
MaskDict.cc:133
lsst::afw::image::detail::MaskDict::setDefault
static void setDefault(std::shared_ptr< MaskDict > dict)
Definition
MaskDict.cc:114
lsst::afw::image::detail::MaskDict::operator==
bool operator==(MaskDict const &rhs) const
Definition
MaskDict.cc:168
lsst::afw::image::detail::MaskDict::find
const_iterator find(std::string const &name) const
Definition
MaskDict.h:135
lsst::afw::image::detail::MaskDict::print
void print() const
Definition
MaskDict.cc:162
lsst::afw::image::detail::MaskDict::clear
void clear()
Definition
MaskDict.cc:182
lsst::afw::image::detail::MaskDict::getUnusedPlane
int getUnusedPlane() const
Definition
MaskDict.cc:135
lsst::afw::image::detail::MaskDict::add
void add(std::string const &name, int bitId)
Definition
MaskDict.cc:172
lsst::afw::image::detail::MaskDict::copyOrGetDefault
static std::shared_ptr< MaskDict > copyOrGetDefault(MaskPlaneDict const &dict)
Definition
MaskDict.cc:108
lsst::afw::image::detail::MaskDict::empty
bool empty() const noexcept
Definition
MaskDict.h:141
lsst::afw::image::detail::MaskDict::erase
void erase(std::string const &name)
Definition
MaskDict.cc:177
std::map::empty
T empty(T... args)
std::endl
T endl(T... args)
std::find_if
T find_if(T... args)
std::lock_guard
std::max_element
T max_element(T... args)
std::move
T move(T... args)
boost
lsst::afw::image::detail
Definition
MaskDict.h:29
lsst::afw::image::detail::MaskPlaneDict
std::map< std::string, int > MaskPlaneDict
Definition
Mask.h:58
lsst::afw::image
Definition
imageAlgorithm.dox:1
lsst::afw
Definition
imageAlgorithm.dox:1
lsst
std
STL namespace.
std::owner_less
std::recursive_mutex
std::set
std::shared_ptr
Generated on
for lsst.afw by
1.17.0