File: image_series.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 (393 lines) | stat: -rw-r--r-- 13,206 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-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/image_series.hpp"

#include "data/detail/series_impl.hxx"

#include <data/exception.hpp>
#include <data/image.hpp>
#include <data/registry/macros.hpp>

SIGHT_REGISTER_DATA(sight::data::image_series)

namespace sight::data
{

image_series::image_series() :
    has_fiducials(m_signals)
{
}

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

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

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

    m_dicom_reference  = other->m_dicom_reference;
    m_fiducials_series = other->m_fiducials_series;

    series::shallow_copy(other);

    base_class_t::shallow_copy(other);
}

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

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

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

    m_dicom_reference = data::object::copy(other->m_dicom_reference);

    m_fiducials_series = std::make_shared<fiducials_series>();
    m_fiducials_series->deep_copy(other->m_fiducials_series, _cache);

    series::deep_copy(other, _cache);

    base_class_t::deep_copy(other, _cache);
}

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

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

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

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

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

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

std::vector<double> image_series::window_center() const noexcept
{
    return series::window_center();
}

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

void image_series::set_window_center(const std::vector<double>& _window_centers)
{
    series::set_window_center(_window_centers);
    image::set_window_center(_window_centers);
}

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

void image_series::set_window_width(const std::vector<double>& _window_widths)
{
    series::set_window_width(_window_widths);
    image::set_window_width(_window_widths);
}

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

std::vector<double> image_series::window_width() const noexcept
{
    return series::window_width();
}

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

void image_series::set_rows(const std::optional<std::uint16_t>& _rows)
{
    series::set_rows(_rows);

    // Resize the image (if possible and needed...)
    if(const auto pixel_format = this->pixel_format(); pixel_format != sight::data::image::pixel_format_t::undefined)
    {
        const auto rows_value = _rows.value_or(1);

        if(const auto& original_size = size(); original_size[0] != rows_value)
        {
            resize({std::size_t(rows_value), original_size[1], original_size[2]}, type(), pixel_format);
        }
    }
}

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

void image_series::set_columns(const std::optional<std::uint16_t>& _columns)
{
    series::set_columns(_columns);

    // Resize the image (if possible and needed...)
    if(const auto pixel_format = this->pixel_format(); pixel_format != sight::data::image::pixel_format_t::undefined)
    {
        const auto columns_value = _columns.value_or(1);

        if(const auto& original_size = size(); original_size[1] != columns_value)
        {
            resize({original_size[0], std::size_t(columns_value), original_size[2]}, type(), pixel_format);
        }
    }
}

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

std::size_t image_series::resize(const image::size_t& _size, const core::type& _type, pixel_format_t _format)
{
    series::shrink_frames(_size[2]);
    return image::resize(_size, _type, _format);
}

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

void image_series::set_image_position_patient(
    const std::vector<double>& _image_position_patient,
    const std::optional<std::size_t>& _frame_index
)
{
    // If we set the position the shared group, we also set the image origin
    if(!_frame_index)
    {
        image::set_origin({_image_position_patient[0], _image_position_patient[1], _image_position_patient[2]});
    }

    series::set_image_position_patient(_image_position_patient, _frame_index);
}

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

void image_series::set_origin(const origin_t& _origin)
{
    image::set_origin(_origin);

    if(get_sop_keyword() != dicom::sop::Keyword::INVALID && is_multi_frame())
    {
        // We know if we are multi-frame or not, so we can use series method
        // Check if we are an ultrasound volume and if we have a specific acquisition geometry
        switch(get_ultrasound_acquisition_geometry())
        {
            case data::dicom::ultrasound_acquisition_geometry_t::apex:
            {
                // Search for Volume to Transducer Mapping Matrix
                if(auto transducer_mapping = get_volume_to_transducer_mapping_matrix(); transducer_mapping)
                {
                    transducer_mapping->set_position(_origin);
                    set_volume_to_transducer_mapping_matrix(transducer_mapping);
                }
                else
                {
                    matrix4 new_mapping;
                    new_mapping.set_position(_origin);
                    set_volume_to_transducer_mapping_matrix(new_mapping);
                }

                break;
            }

            case data::dicom::ultrasound_acquisition_geometry_t::patient:
            {
                if(auto table_mapping = get_volume_to_table_mapping_matrix(); table_mapping)
                {
                    table_mapping->set_position(_origin);
                    set_volume_to_table_mapping_matrix(table_mapping);
                }
                else
                {
                    matrix4 new_mapping;
                    new_mapping.set_position(_origin);
                    set_volume_to_table_mapping_matrix(new_mapping);
                }

                break;
            }

            default:
            {
                // This is the default case, we set the shared group if multi-frame or the root ImagePositionPatient
                // In either case it won't overwrite per-frame positions
                set_image_position_patient({_origin[0], _origin[1], _origin[2]}, std::nullopt);
                break;
            }
        }
    }
    else
    {
        // Store it as "global" ImagePositionPatient
        m_pimpl->set_values<gdcm::Keywords::ImagePositionPatient>({_origin[0], _origin[1], _origin[2]}, 0);
    }
}

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

void image_series::set_image_orientation_patient(
    const std::vector<double>& _orientation,
    const std::optional<std::size_t>& _frame_index
)
{
    SIGHT_ASSERT(
        "The image orientation patient must have 6 direction cosines.",
        _orientation.size() == 6
    );

    // If we set the orientation the shared group, we also set the image orientation
    if(!_frame_index)
    {
        image::set_orientation(from_dicom_orientation(_orientation));
    }

    series::set_image_orientation_patient(_orientation, _frame_index);
}

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

void image_series::set_orientation(const orientation_t& _orientation)
{
    image::set_orientation(_orientation);

    if(get_sop_keyword() != dicom::sop::Keyword::INVALID && is_multi_frame())
    {
        // We know if we are multi-frame or not, so we can use series method
        // Check if we are an ultrasound volume and if we have a specific acquisition geometry
        switch(get_ultrasound_acquisition_geometry())
        {
            case data::dicom::ultrasound_acquisition_geometry_t::apex:
            {
                // Search for Volume to Transducer Mapping Matrix
                if(auto transducer_mapping = get_volume_to_transducer_mapping_matrix(); transducer_mapping)
                {
                    transducer_mapping->set_orientation(_orientation);
                    set_volume_to_transducer_mapping_matrix(transducer_mapping);
                }
                else
                {
                    matrix4 new_mapping;
                    new_mapping.set_orientation(_orientation);
                    set_volume_to_transducer_mapping_matrix(new_mapping);
                }

                break;
            }

            case data::dicom::ultrasound_acquisition_geometry_t::patient:
            {
                if(auto table_mapping = get_volume_to_table_mapping_matrix(); table_mapping)
                {
                    table_mapping->set_orientation(_orientation);
                    set_volume_to_table_mapping_matrix(table_mapping);
                }
                else
                {
                    matrix4 new_mapping;
                    new_mapping.set_orientation(_orientation);
                    set_volume_to_table_mapping_matrix(new_mapping);
                }

                break;
            }

            default:
            {
                // This is the default case, we set the shared group if multi-frame or the root ImageOrientationPatient
                // In either case it won't overwrite per-frame positions
                set_image_orientation_patient(
                    {
                        _orientation[0], _orientation[3], _orientation[6],
                        _orientation[1], _orientation[4], _orientation[7],
                    },
                    std::nullopt
                );
                break;
            }
        }
    }
    else
    {
        // Store it as "global" ImageOrientationPatient
        m_pimpl->set_values<gdcm::Keywords::ImageOrientationPatient>(
            {
                _orientation[0], _orientation[3], _orientation[6],
                _orientation[1], _orientation[4], _orientation[7]
            },
            0
        );
    }
}

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

void image_series::set_patient_id(const std::string& _patient_id)
{
    this->series::set_patient_id(_patient_id);
    this->m_fiducials_series->set_patient_id(_patient_id);
}

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

void image_series::set_patient_name(const std::string& _patient_name)
{
    this->series::set_patient_name(_patient_name);
    this->m_fiducials_series->set_patient_id(_patient_name);
}

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

void image_series::set_study_instance_uid(const std::string& _study_instance_uid)
{
    this->series::set_study_instance_uid(_study_instance_uid);
    this->m_fiducials_series->set_study_instance_uid(_study_instance_uid);
}

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

void image_series::set_slice_thickness(const std::optional<double>& _slice_thickness)
{
    this->series::set_slice_thickness(_slice_thickness);
    if(_slice_thickness)
    {
        spacing_t spacing = this->spacing();
        spacing[2] = *_slice_thickness;
        this->set_spacing(spacing);
    }
}

} // namespace sight::data