File: landmarks.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (340 lines) | stat: -rw-r--r-- 11,833 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/************************************************************************
 *
 * Copyright (C) 2017-2024 IRCAD France
 * Copyright (C) 2017-2019 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "data/landmarks.hpp"

#include "data/exception.hpp"
#include "data/registry/macros.hpp"

#include <core/com/signal.hpp>
#include <core/com/signal.hxx>

SIGHT_REGISTER_DATA(sight::data::landmarks);

namespace sight::data
{

const core::com::signals::key_t landmarks::GROUP_ADDED_SIG      = "groupAdded";
const core::com::signals::key_t landmarks::GROUP_REMOVED_SIG    = "groupMoved";
const core::com::signals::key_t landmarks::POINT_ADDED_SIG      = "point_added";
const core::com::signals::key_t landmarks::POINT_REMOVED_SIG    = "point_removed";
const core::com::signals::key_t landmarks::POINT_INSERTED_SIG   = "pointInserted";
const core::com::signals::key_t landmarks::GROUP_MODIFIED_SIG   = "groupModified";
const core::com::signals::key_t landmarks::POINT_MODIFIED_SIG   = "pointModified";
const core::com::signals::key_t landmarks::GROUP_RENAMED_SIG    = "groupRenamed";
const core::com::signals::key_t landmarks::POINT_SELECTED_SIG   = "pointSelected";
const core::com::signals::key_t landmarks::POINT_DESELECTED_SIG = "pointDeselected";

//------------------------------------------------------------------------------

landmarks::landmarks()
{
    new_signal<group_added_signal_t>(GROUP_ADDED_SIG);
    new_signal<group_removed_signal_t>(GROUP_REMOVED_SIG);
    new_signal<point_added_signal_t>(POINT_ADDED_SIG);
    new_signal<point_removed_signal_t>(POINT_REMOVED_SIG);
    new_signal<point_inserted_signal_t>(POINT_INSERTED_SIG);
    new_signal<group_modified_signal_t>(GROUP_MODIFIED_SIG);
    new_signal<point_modified_sig_t>(POINT_MODIFIED_SIG);
    new_signal<group_renamed_signal_t>(GROUP_RENAMED_SIG);
    new_signal<point_selected_signal_t>(POINT_SELECTED_SIG);
    new_signal<point_deselected_signal_t>(POINT_DESELECTED_SIG);
}

//------------------------------------------------------------------------------

void landmarks::shallow_copy(const object::csptr& _source)
{
    const auto& other = std::dynamic_pointer_cast<const landmarks>(_source);

    SIGHT_THROW_EXCEPTION_IF(
        exception(
            "Unable to copy " + (_source ? _source->get_classname() : std::string("<NULL>"))
            + " to " + get_classname()
        ),
        !bool(other)
    );

    m_landmarks = other->m_landmarks;

    base_class_t::shallow_copy(other);
}

//------------------------------------------------------------------------------

void landmarks::deep_copy(const object::csptr& _source, const std::unique_ptr<deep_copy_cache_t>& _cache)
{
    const auto& other = std::dynamic_pointer_cast<const landmarks>(_source);

    SIGHT_THROW_EXCEPTION_IF(
        exception(
            "Unable to copy " + (_source ? _source->get_classname() : std::string("<NULL>"))
            + " to " + get_classname()
        ),
        !bool(other)
    );

    m_landmarks = other->m_landmarks;

    base_class_t::deep_copy(other, _cache);
}

//------------------------------------------------------------------------------

void landmarks::add_group(
    const std::string& _name,
    const landmarks::color_t& _color,
    const landmarks::size_t _size,
    const landmarks::shape _shape,
    const bool _visibility
)
{
    landmarks_group group(_color, _size, _shape, _visibility);
    const auto iter = m_landmarks.find(_name);
    SIGHT_THROW_EXCEPTION_IF(data::exception("Group '" + _name + "' already exists"), iter != m_landmarks.end());
    m_landmarks.insert(std::make_pair(_name, group));
}

//------------------------------------------------------------------------------

landmarks::GroupNameContainer landmarks::get_group_names() const
{
    landmarks::GroupNameContainer names;

    std::transform(
        m_landmarks.begin(),
        m_landmarks.end(),
        std::back_inserter(names),
        [](const landmarks_container::value_type& _pair){return _pair.first;});

    return names;
}

//------------------------------------------------------------------------------

const landmarks::landmarks_group& landmarks::get_group(const std::string& _name) const
{
    const auto iter = m_landmarks.find(_name);
    SIGHT_THROW_EXCEPTION_IF(data::exception("Group '" + _name + "' does not exist"), iter == m_landmarks.end());
    const landmarks::landmarks_group& group = iter->second;
    return group;
}

//------------------------------------------------------------------------------

landmarks::landmarks_group& landmarks::get_group(const std::string& _name)
{
    auto iter = m_landmarks.find(_name);
    SIGHT_THROW_EXCEPTION_IF(data::exception("Group '" + _name + "' does not exist"), iter == m_landmarks.end());

    landmarks::landmarks_group& group = iter->second;
    return group;
}

//------------------------------------------------------------------------------

bool landmarks::has_group(const std::string& _name) const noexcept
{
    return m_landmarks.contains(_name);
}

//------------------------------------------------------------------------------

void landmarks::rename_group(const std::string& _old_name, const std::string& _new_name)
{
    const auto iter = m_landmarks.find(_old_name);
    SIGHT_THROW_EXCEPTION_IF(data::exception("Group '" + _old_name + "' does not exist"), iter == m_landmarks.end());
    const auto iter2 = m_landmarks.find(_new_name);
    SIGHT_THROW_EXCEPTION_IF(data::exception("Group '" + _new_name + "' already exists"), iter2 != m_landmarks.end());

    const landmarks::landmarks_group group = iter->second;
    m_landmarks.insert(std::make_pair(_new_name, group));
    m_landmarks.erase(_old_name);
}

//------------------------------------------------------------------------------

void landmarks::remove_group(const std::string& _name)
{
    const auto iter = m_landmarks.find(_name);
    SIGHT_THROW_EXCEPTION_IF(data::exception("Group '" + _name + "' does not exist"), iter == m_landmarks.end());

    m_landmarks.erase(_name);
}

//------------------------------------------------------------------------------

void landmarks::set_group_color(const std::string& _name, const landmarks::color_t& _color)
{
    landmarks::landmarks_group& group = this->get_group(_name);
    group.m_color = _color;
}

//------------------------------------------------------------------------------

void landmarks::set_group_size(const std::string& _name, const landmarks::size_t _size)
{
    landmarks::landmarks_group& group = this->get_group(_name);
    group.m_size = _size;
}

//------------------------------------------------------------------------------

void landmarks::set_group_shape(const std::string& _name, const landmarks::shape _shape)
{
    landmarks::landmarks_group& group = this->get_group(_name);
    group.m_shape = _shape;
}

//------------------------------------------------------------------------------

void landmarks::set_group_visibility(const std::string& _name, const bool _visibility)
{
    landmarks::landmarks_group& group = this->get_group(_name);
    group.m_visibility = _visibility;
}

//------------------------------------------------------------------------------

void landmarks::add_point(const std::string& _name, const landmarks::point_t& _point)
{
    landmarks::landmarks_group& group = this->get_group(_name);
    group.m_points.push_back(_point);
}

//------------------------------------------------------------------------------

void landmarks::insert_point(const std::string& _name, const std::size_t _index, const landmarks::point_t& _point)
{
    landmarks::landmarks_group& group = this->get_group(_name);
    auto iter                         = group.m_points.begin() + static_cast<point_container::difference_type>(_index);
    group.m_points.insert(iter, _point);
}

//------------------------------------------------------------------------------

const landmarks::point_t& landmarks::get_point(const std::string& _name, std::size_t _index) const
{
    const landmarks::landmarks_group& group = this->get_group(_name);
    return group.m_points.at(_index);
}

//------------------------------------------------------------------------------

landmarks::point_t& landmarks::get_point(const std::string& _name, std::size_t _index)
{
    landmarks::landmarks_group& group = this->get_group(_name);
    return group.m_points.at(_index);
}

//------------------------------------------------------------------------------

const landmarks::point_container& landmarks::get_points(const std::string& _name) const
{
    const landmarks::landmarks_group& group = this->get_group(_name);
    return group.m_points;
}

//------------------------------------------------------------------------------

void landmarks::remove_point(const std::string& _name, std::size_t _index)
{
    landmarks::landmarks_group& group = this->get_group(_name);

    SIGHT_THROW_EXCEPTION_IF(
        std::out_of_range("index out of range in group '" + _name + "'"),
        _index >= group.m_points.size()
    );

    auto iter = group.m_points.begin() + static_cast<point_container::difference_type>(_index);
    group.m_points.erase(iter);
}

//------------------------------------------------------------------------------

void landmarks::clear_points(const std::string& _name)
{
    landmarks::landmarks_group& group = this->get_group(_name);
    group.m_points.clear();
}

//------------------------------------------------------------------------------

std::size_t landmarks::num_points() const
{
    std::size_t nb = 0;
    for(const auto& elt : m_landmarks)
    {
        const landmarks_group group = elt.second;
        nb += group.m_points.size();
    }

    return nb;
}

//------------------------------------------------------------------------------

std::size_t landmarks::num_points(const std::string& _name) const
{
    const landmarks_group& group = this->get_group(_name);
    return group.m_points.size();
}

//------------------------------------------------------------------------------

bool landmarks::landmarks_group::operator==(const landmarks_group& _other) const noexcept
{
    return !(!core::is_equal(m_color, _other.m_color)
             || !core::is_equal(m_size, _other.m_size)
             || m_shape != _other.m_shape
             || m_visibility != _other.m_visibility
             || !core::is_equal(m_points, _other.m_points));
}

//------------------------------------------------------------------------------

bool landmarks::landmarks_group::operator!=(const landmarks_group& _other) const noexcept
{
    return !(*this == _other);
}

//------------------------------------------------------------------------------

bool landmarks::operator==(const landmarks& _other) const noexcept
{
    if(!core::is_equal(m_landmarks, _other.m_landmarks))
    {
        return false;
    }

    // Super class last
    return base_class_t::operator==(_other);
}

//------------------------------------------------------------------------------

bool landmarks::operator!=(const landmarks& _other) const noexcept
{
    return !(*this == _other);
}

} // namespace sight::data