File: line_drawer.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 (193 lines) | stat: -rw-r--r-- 5,734 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
/************************************************************************
 *
 * Copyright (C) 2018-2025 IRCAD France
 * Copyright (C) 2018-2021 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 "filter/image/line_drawer.hpp"

#include <core/spy_log.hpp>

#include <data/helper/medical_image.hpp>

#include <utility>

namespace sight::filter::image
{

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

line_drawer::line_drawer(data::image::sptr _img, data::image::csptr _roi) :
    m_image(std::move(_img)),
    m_roi_image(std::move(_roi))
{
    m_use_roi = data::helper::medical_image::check_image_validity(m_roi_image);

    m_image_type_size = std::uint8_t(m_image->type().size());
    m_roi_type_size   = m_use_roi ? std::uint8_t(m_roi_image->type().size()) : 0;
    const auto& size = m_image->size();
    m_y_pitch = size[0];
    m_z_pitch = size[1] * m_y_pitch;
}

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

bool line_drawer::draw_ellipse(
    const line_drawer::coordinates_t& _c,
    const data::image::buffer_t* _value,
    const double _radius,
    const std::size_t _first_dim,
    const std::size_t _second_dim,
    const bool _overwrite,
    image_diff& _diff
)
{
    bool modified = false;

    const auto& spacing = m_image->spacing();
    const auto& size    = m_image->size();

    const double width  = _radius / spacing[_first_dim];
    const double height = _radius / spacing[_second_dim];

    int orig_x = static_cast<int>(_c[_first_dim]);
    int orig_y = static_cast<int>(_c[_second_dim]);

    line_drawer::coordinates_t point = _c;

    int w_begin = std::max(static_cast<int>(-width), -orig_x);
    int h_begin = std::max(static_cast<int>(-height), -orig_y);

    int w_end = std::min(static_cast<int>(width), static_cast<int>(size[_first_dim]) - 1 - orig_x);
    int h_end = std::min(static_cast<int>(height), static_cast<int>(size[_second_dim]) - 1 - orig_y);

    for(int y = h_begin ; y <= h_end ; y++)
    {
        for(int x = w_begin ; x <= w_end ; x++)
        {
            double dx = x / width;
            double dy = y / height;
            if(dx * dx + dy * dy <= 1)
            {
                point[_first_dim]  = static_cast<data::image::index_t>(orig_x) + static_cast<data::image::index_t>(x);
                point[_second_dim] = static_cast<data::image::index_t>(orig_y) + static_cast<data::image::index_t>(y);

                const data::image::index_t index = point[0] + point[1] * m_y_pitch + point[2] * m_z_pitch;

                modified |= this->draw_pixel(index, _value, _overwrite, _diff);
            }
        }
    }

    return modified;
}

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

bool line_drawer::draw_pixel(
    const data::image::index_t _index,
    const data::image::buffer_t* _value,
    const bool _overwrite,
    image_diff& _diff
)
{
    const data::image::buffer_t* pix_buf =
        reinterpret_cast<data::image::buffer_t*>(m_image->get_pixel(_index));

    if(m_use_roi)
    {
        const auto* roi_val =
            reinterpret_cast<const data::image::buffer_t*>(m_roi_image->get_pixel(_index));
        if(data::helper::medical_image::is_buf_null(roi_val, m_roi_type_size))
        {
            return false;
        }
    }

    if(std::equal(pix_buf, pix_buf + m_image_type_size, _value))
    {
        return false;
    }

    if(!_overwrite && !data::helper::medical_image::is_buf_null(pix_buf, m_image_type_size))
    {
        return false;
    }

    _diff.add_diff(_index, pix_buf, _value);
    m_image->set_pixel(_index, _value);

    return true;
}

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

image_diff line_drawer::draw(
    const bresenham_line::Orientation _orientation,
    const coordinates_t& _start_coord,
    const coordinates_t& _end_coord,
    const data::image::buffer_t* _value,
    const double _thickness,
    const bool _overwrite
)
{
    image_diff diff(m_image_type_size, 128);

    std::size_t dim0 = 0;
    std::size_t dim1 = 0;

    switch(_orientation)
    {
        case bresenham_line::Orientation::z_axis:
            dim0 = 0;
            dim1 = 1;
            break;

        case bresenham_line::Orientation::y_axis:
            dim0 = 2;
            dim1 = 0;
            break;

        case bresenham_line::Orientation::x_axis:
            dim0 = 1;
            dim1 = 2;
            break;

        default:
            SIGHT_ASSERT("Unknown axis", false);
            dim0 = 0;
            dim1 = 1;
    }

    bresenham_line::path_t path = bresenham_line::draw(_orientation, _start_coord, _end_coord);

    auto pixel     = path.begin();
    auto end_pixel = path.end();

    bool modified = false;

    for( ; pixel != end_pixel ; ++pixel)
    {
        modified = this->draw_ellipse(*pixel, _value, _thickness / 2.0, dim0, dim1, _overwrite, diff) || modified;
    }

    return diff;
}

} // namespace sight::filter::image