File: medical_image.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (368 lines) | stat: -rw-r--r-- 11,023 bytes parent folder | download
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/************************************************************************
 *
 * Copyright (C) 2018-2025 IRCAD France
 * Copyright (C) 2018-2020 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/helper/medical_image.hpp"

#include "data/helper/field.hpp"

#include <data/boolean.hpp>
#include <data/image.hpp>
#include <data/integer.hpp>
#include <data/map.hpp>
#include <data/point.hpp>
#include <data/point_list.hpp>
#include <data/real.hpp>
#include <data/string.hpp>
#include <data/vector.hpp>

#include <cmath>
#include <numeric>
#include <utility>

namespace sight::data::helper::medical_image
{

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

bool check_image_validity(data::image::csptr _p_img)
{
    return _p_img ? check_image_validity(*_p_img) : false;
}

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

bool check_image_validity(const data::image& _image)
{
    // Test if the image is allocated
    bool data_image_is_allocated = (_image.allocated_size_in_bytes() > 0);

    if(data_image_is_allocated)
    {
        std::size_t nb_dim = _image.num_dimensions();
        data_image_is_allocated &= nb_dim > 1;

        for(std::size_t k = 0 ; data_image_is_allocated && k < nb_dim ; ++k)
        {
            data_image_is_allocated = data_image_is_allocated && (_image.size()[k] >= 1);
        }
    }

    return data_image_is_allocated;
}

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

bool check_image_slice_index(data::image& _p_img)
{
    bool field_is_modified = false;

    const data::image::size_t& image_size = _p_img.size();

    const auto axial_idx    = get_slice_index(_p_img, axis_t::axial);
    const auto frontal_idx  = get_slice_index(_p_img, axis_t::frontal);
    const auto sagittal_idx = get_slice_index(_p_img, axis_t::sagittal);

    std::array<std::int64_t, 3> index_values = {0, 0, 0};

    // Check if values are out of bounds
    if(!axial_idx.has_value()
       || (axial_idx.has_value() && image_size[2] > 0
           && std::cmp_less(image_size[2], axial_idx.value())))
    {
        index_values[2]   = static_cast<std::int64_t>(image_size[2] / 2);
        field_is_modified = true;
    }

    if(!frontal_idx.has_value()
       || (axial_idx.has_value() && image_size[1] > 0
           && std::cmp_less(image_size[1], frontal_idx.value())))
    {
        index_values[1]   = static_cast<std::int64_t>(image_size[1] / 2);
        field_is_modified = true;
    }

    if(!sagittal_idx.has_value()
       || (sagittal_idx.has_value() && image_size[0] > 0
           && std::cmp_less(image_size[0], sagittal_idx.value())))
    {
        index_values[0]   = static_cast<std::int64_t>(image_size[0] / 2);
        field_is_modified = true;
    }

    // Update or create fields.
    if(field_is_modified)
    {
        set_slice_index(_p_img, axis_t::axial, index_values[axis_t::axial]);
        set_slice_index(_p_img, axis_t::frontal, index_values[axis_t::frontal]);
        set_slice_index(_p_img, axis_t::sagittal, index_values[axis_t::sagittal]);
    }

    return field_is_modified;
}

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

bool is_buf_null(const data::image::buffer_t* _buffer, const unsigned int _len)
{
    bool is_null = 0 == std::accumulate(
        _buffer,
        _buffer + _len,
        0,
        std::bit_or<>()
    );
    return is_null;
}

//-------------------------------------------------------------------------------
std::optional<std::int64_t> get_slice_index(
    const data::image& _image,
    const axis_t& _axis
)
{
    std::string axis_name;
    switch(_axis)
    {
        case axis_t::axial:
            axis_name = std::string(id::AXIAL_SLICE_INDEX);
            break;

        case axis_t::sagittal:
            axis_name = std::string(id::SAGITTAL_SLICE_INDEX);
            break;

        case axis_t::frontal:
            axis_name = std::string(id::FRONTAL_SLICE_INDEX);
            break;

        default:
            SIGHT_THROW_EXCEPTION(data::exception("Wrong orientation type."));
    }

    // Test if field exists
    const auto field = _image.get_field(axis_name);
    if(field)
    {
        // Test if the type is data::integer.
        const auto field_int = std::dynamic_pointer_cast<data::integer>(field);
        if(field_int)
        {
            // Get value.
            return field_int->value();
        }
    }

    return {};
}

//-------------------------------------------------------------------------------
void set_slice_index(
    data::image& _image,
    const axis_t& _axis,
    std::int64_t _slice_idx
)
{
    data::integer::sptr value = std::make_shared<data::integer>();
    value->set_value(_slice_idx);

    std::string axis_name;
    switch(_axis)
    {
        case axis_t::axial:
            axis_name = std::string(id::AXIAL_SLICE_INDEX);
            break;

        case axis_t::sagittal:
            axis_name = std::string(id::SAGITTAL_SLICE_INDEX);
            break;

        case axis_t::frontal:
            axis_name = std::string(id::FRONTAL_SLICE_INDEX);
            break;

        default:
            SIGHT_THROW_EXCEPTION(data::exception("Wrong orientation type."));
    }

    _image.set_field(axis_name, value);
}

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

std::optional<double_t> get_slice_position(
    const data::image& _image,
    const axis_t& _axis
)
{
    const auto slice_idx_opt = get_slice_index(_image, _axis);
    if(!slice_idx_opt)
    {
        return {};
    }

    const auto& spacing          = _image.spacing();
    const auto& origin           = _image.origin();
    const std::int64_t slice_idx = slice_idx_opt.value();
    return origin[_axis] + static_cast<double>(slice_idx) * spacing[_axis];
}

//------------------------------------------------------------------------------
void set_slice_position(
    data::image& _image,
    const axis_t& _orientation,
    double _position_in_image
)
{
    const auto& spacing = _image.spacing();
    SIGHT_ASSERT("Spacing cannot be zero", !core::is_equal(0.0, spacing[_orientation]));

    const auto& origin   = _image.origin();
    const auto new_index = std::int64_t(
        std::round((_position_in_image - origin[_orientation]) / spacing[_orientation])
    );

    set_slice_index(_image, _orientation, new_index);
}

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

data::point_list::sptr get_landmarks(const data::image& _image)
{
    return _image.get_field<data::point_list>(std::string(id::LANDMARKS));
}

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

void set_landmarks(data::image& _image, const data::point_list::sptr& _landmarks)
{
    if(_landmarks)
    {
        _image.set_field(std::string(id::LANDMARKS), _landmarks);
    }
    else
    {
        SIGHT_THROW_EXCEPTION(data::exception("Trying to set nullptr as landmark field."));
    }
}

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

data::vector::sptr get_distances(const data::image& _image)
{
    return _image.get_field<data::vector>(std::string(id::DISTANCES));
}

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

void set_distances(data::image& _image, const data::vector::sptr& _distances)
{
    if(_distances)
    {
        _image.set_field(std::string(id::DISTANCES), _distances);
    }
    else
    {
        SIGHT_ERROR("Trying to set nullptr to distances field.");
    }
}

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

bool get_distance_visibility(const data::image& _image)
{
    const auto visibility = _image.get_field<boolean>(std::string(id::DISTANCE_VISIBILITY));

    if(visibility)
    {
        return visibility->value();
    }

    // default value is true.
    return true;
}

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

void set_distance_visibility(data::image& _image, bool _visibility)
{
    _image.set_field(std::string(id::DISTANCE_VISIBILITY), std::make_shared<data::boolean>(_visibility));
}

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

bool get_landmarks_visibility(const data::image& _image)
{
    const auto visibility = _image.get_field<boolean>(std::string(id::LANDMARKS_VISIBILITY));

    if(visibility)
    {
        return visibility->value();
    }

    // default value is true.
    return true;
}

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

void set_landmarks_visibility(data::image& _image, bool _visibility)
{
    _image.set_field(std::string(id::LANDMARKS_VISIBILITY), std::make_shared<data::boolean>(_visibility));
}

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

data::matrix4::sptr get_direction(const data::image& _image)
{
    FW_DEPRECATED_MSG("medical_image::get_direction() is deprecated, use image::orientation() instead.", "26.0");

    if(auto direction = _image.get_field<data::matrix4>(std::string(id::DIRECTION)); direction)
    {
        return direction;
    }

    // Compatibility code while we still use medical_image::get_direction()
    // medical_image::get_direction() will be removed in the future and replaced by image::orientation()
    auto matrix4 = std::make_shared<data::matrix4>();
    matrix4->set_orientation(_image.orientation());

    return matrix4;
}

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

void set_direction(data::image& _image, data::matrix4::sptr _direction)
{
    FW_DEPRECATED_MSG("medical_image::set_direction() is deprecated, use image::set_orientation() instead.", "26.0");

    if(_direction)
    {
        _image.set_field(std::string(id::DIRECTION), _direction);

        // Compatibility code while we still use medical_image::set_direction()
        // medical_image::set_direction() will be removed in the future and replaced by image::set_orientation()
        _image.set_orientation(_direction->orientation());
    }
}

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

} // namespace sight::data::helper::medical_image