File: transfer_function.hpp

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 (509 lines) | stat: -rw-r--r-- 17,088 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/************************************************************************
 *
 * Copyright (C) 2009-2025 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/>.
 *
 ***********************************************************************/

#pragma once

#include <sight/data/config.hpp>

#include "data/container.hpp"

#include <core/type.hpp>

#include <glm/vec4.hpp>

#include <span>

namespace sight::data
{

class SIGHT_DATA_CLASS_API transfer_function_base
{
public:

    /// Defines the type of values of each point.
    using value_t = double;

    /// Defines the color of each point.
    using color_t = glm::dvec4;

    /// Defines the type used to store transfer function data.
    using data_t = std::map<value_t, color_t>;

    using min_max_t = std::pair<value_t, value_t>;

    /// Defines the available modes {LINEAR, NEAREST} to interpolate color between two TF color points.
    enum class interpolation_mode
    {
        linear,
        nearest
    };
    SIGHT_DATA_API virtual ~transfer_function_base() = default;

    /// Scale the intensity value from the transfer function space to the window space.
    [[nodiscard]] SIGHT_DATA_API value_t map_value_to_window(value_t _value) const;

    /// Scale the intensity value from the window space space to the transfer function.
    [[nodiscard]] SIGHT_DATA_API value_t map_value_from_window(value_t _value) const;

    /// Gets the nearest color of a value, taking into account the window range
    [[nodiscard]] SIGHT_DATA_API color_t sample_nearest(value_t _value) const;

    /// Gets the color for a value (the color is computed with a linear interpolation), taking into account the window
    /// range
    [[nodiscard]] SIGHT_DATA_API color_t sample_linear(value_t _value) const;

    /// Gets the min/max of the window level.
    [[nodiscard]] SIGHT_DATA_API min_max_t window_min_max() const;

    /// Sets the min/max of the window level.
    SIGHT_DATA_API void set_window_min_max(const min_max_t& _min_max);

    /// Gets the level.
    [[nodiscard]] value_t level() const;

    /// Gets the window.
    [[nodiscard]] value_t window() const;

    /// Sets the level.
    SIGHT_DATA_API virtual void set_level(value_t _value) = 0;

    /// Sets the window.
    SIGHT_DATA_API virtual void set_window(value_t _value) = 0;

    /// Gets the first and last point values of the tf data.
    [[nodiscard]] SIGHT_DATA_API virtual min_max_t min_max() const = 0;

    /// Gets the interpolated color of the TF for a value, taking into account the window range
    /// @param _value input value in the curve
    /// @param _mode  interpolation mode, if not specified, use the mode specified by setInterpolationMode()
    [[nodiscard]] SIGHT_DATA_API virtual color_t sample(
        value_t _value,
        std::optional<enum interpolation_mode> _mode = std::nullopt
    ) const                                          = 0;

    /// Sets the current visualization level.
    double m_level {0.};

    /// Sets the current visualization window.
    double m_window {2.};
};

/**
 * @brief Stores a transfer function properties. A list of points associating a value to a RGBA color, and windowing
 * parameters which simplifies the scaling of the function.
 */
class SIGHT_DATA_CLASS_API transfer_function_piece final : public container_wrapper<std::map<transfer_function_base::value_t,
                                                                                             transfer_function_base::color_t> >,
                                                           public transfer_function_base
{
public:

    SIGHT_DECLARE_CLASS(transfer_function_piece);

    using container_wrapper<data_t>::container_wrapper;

    SIGHT_DATA_API ~transfer_function_piece() final = default;

    static transfer_function_piece::sptr make();

    /// Equality comparison operators
    /// @{
    bool operator==(const transfer_function_piece& _other) const noexcept;
    bool operator!=(const transfer_function_piece& _other) const noexcept;
    /// @}

    transfer_function_piece& operator=(const transfer_function_piece& _other);

    /// Sets the level.
    SIGHT_DATA_API void set_level(value_t _value) final;

    /// Sets the window.
    SIGHT_DATA_API void set_window(value_t _value) final;

    /// Gets the first and last point values of the tf data.
    [[nodiscard]] SIGHT_DATA_API min_max_t min_max() const final;

    /// Gets the interpolated color of the TF for a value, taking into account the window range
    /// @param _value input value in the curve
    /// @param _mode  interpolation mode, if not specified, use the mode specified by setInterpolationMode()
    [[nodiscard]] SIGHT_DATA_API color_t sample(
        value_t _value,
        std::optional<enum interpolation_mode> _mode = std::nullopt
    ) const final;

    /// Gets the interpolation mode.
    [[nodiscard]] enum interpolation_mode get_interpolation_mode() const;

    /// Sets the interpolation mode.
    void set_interpolation_mode(enum interpolation_mode _value);

    /// Gets if the TF is clamped.
    [[nodiscard]] bool clamped() const;

    /// Sets if the TF is clamped.
    void set_clamped(bool _value);

    /// Gets if the TF was generated by an algorithm and not defined by the user or read from disk.
    [[nodiscard]] bool generated() const;

    /// Sets if the TF is generated by an algorithm and not defined by the user or read from disk.
    void set_generated(bool _value);

private:

    /// Defines the current interpolation mode.
    enum interpolation_mode m_interpolation_mode
    {
        interpolation_mode::linear
    };

    /**
     *  @brief Defines interpolation mode on extremities.
     *
     *  if m_clamped == true then after extremity point, the returned TF color is color_t(0,0,0,0).
     *  if m_clamped == false then after extremity point, the returned TF color is one of the extremity color value.
     **/
    bool m_clamped {true};
    bool m_generated {false};
};

/**
 * @brief This class defines a piecewise transfer function.
 *
 * A transfer function is composed of a list of points that associates a value, typically an image intensity,
 * to a RGBA color.
 *
 * The function is composed of individual pieces that can be accessed and edited independently thanks to the pieces()
 * function that returns a vector of transfer_function_piece.
 */
class SIGHT_DATA_CLASS_API transfer_function final : public object,
                                                     public transfer_function_base
{
public:

    SIGHT_DECLARE_CLASS(transfer_function, object);

    /// Defines the type of values of each point.
    using value_t = transfer_function_piece::value_t;

    /// Defines the color of each point.
    using color_t = transfer_function_piece::color_t;

    /// Defines the type used to store transfer function data.
    using data_t = transfer_function_piece::data_t;

    using min_max_t = transfer_function_piece::min_max_t;

    using interpolation_mode_t = transfer_function_piece::interpolation_mode;

    /// Constructors / Destructor / Assignment operators
    /// @{
    SIGHT_DATA_API transfer_function();
    SIGHT_DATA_API ~transfer_function() final = default;
    /// @}

    /// Equality comparison operators
    /// @{
    SIGHT_DATA_API bool operator==(const transfer_function& _other) const noexcept;
    SIGHT_DATA_API bool operator!=(const transfer_function& _other) const noexcept;
    /// @}

    /// Sets the defaults transfer function name.
    SIGHT_DATA_API static const std::string DEFAULT_TF_NAME;

    /// Creates a default TF.
    SIGHT_DATA_API static transfer_function::sptr create_default_tf();

    /// Creates a default TF according to the pixel type.
    SIGHT_DATA_API static transfer_function::sptr create_default_tf(core::type _type);

    /**
     * Merges a range of transfer functions into one.
     * @param _dst Destination transfer function. Its pieces are conserved too.
     * @param _src Source transfer function.
     * @pre None of the pieces of _src must be nullptr.
     */
    SIGHT_DATA_API
    static void merge(
        sight::data::transfer_function& _dst,
        const sight::data::transfer_function& _src
    );

    /// Gets the transfer function name.
    [[nodiscard]] const std::string& name() const;

    /// Sets the transfer function name.
    void set_name(const std::string& _value);

    /// Gets whether the transfer function will be resample or not.
    [[nodiscard]] bool resample_to_max_texture_size() const;

    /// Sets whether the transfer function will be resampled or nor.
    void set_resample_to_max_texture_size(const bool _value);

    /// Gets the TF background color when tf 'isClamped' is true.
    [[nodiscard]] const color_t& background_color() const;

    /// Set the TF background color when tf 'isClamped' is true.
    void set_background_color(const color_t& _value);

    /// Returns all the pieces of the piecewise function
    std::vector<transfer_function_piece::sptr>& pieces();
    const std::vector<transfer_function_piece::sptr>& pieces() const;

    /// Returns true if the function is empty
    [[nodiscard]] bool empty() const;

    /// Recompute the window parameters by taking the minimum and the maximum of the pieces.
    SIGHT_DATA_API void fit_window();

    /// Sets the level of the transfer function and its pieces.
    SIGHT_DATA_API void set_level(value_t _value) final;

    /// Sets the window of the transfer function and its pieces.
    SIGHT_DATA_API void set_window(value_t _value) final;

    /// Gets the first and last point values of the tf data.
    [[nodiscard]] SIGHT_DATA_API min_max_t min_max() const final;

    /// Gets the interpolated color of the TF for a value, taking into account the window range
    /// @param _value input value in the curve
    /// @param _mode  interpolation mode, if not specified, use the mode specified by setInterpolationMode()
    [[nodiscard]] SIGHT_DATA_API color_t sample(
        value_t _value,
        std::optional<interpolation_mode_t> _mode = std::nullopt
    ) const final;

    /// @name Signals
    /// @{
    /// Defines the type of signal sent when points are modified.
    using points_modified_signal_t = core::com::signal<void ()>;
    SIGHT_DATA_API static const core::com::signals::key_t POINTS_MODIFIED_SIG;

    /// Defines the type of signal sent when window-level is modified (window, level).
    using windowing_modified_signal_t = core::com::signal<void (value_t, value_t)>;
    SIGHT_DATA_API static const core::com::signals::key_t WINDOWING_MODIFIED_SIG;
    /// @}

    /// Defines shallow copy
    /// @throws data::exception if an errors occurs during copy
    /// @param[in] _source the source object to copy
    SIGHT_DATA_API void shallow_copy(const object::csptr& _source) final;

    /// Defines deep copy
    /// @throws data::exception if an errors occurs during copy
    /// @param _source source object to copy
    /// @param _cache cache used to deduplicate pointers
    SIGHT_DATA_API void deep_copy(
        const object::csptr& _source,
        const std::unique_ptr<deep_copy_cache_t>& _cache = std::make_unique<deep_copy_cache_t>()
    ) final;

private:

    /// Sets the transfer function name.
    std::string m_name;

    /// Resamples the transfer function to the max texture size when uploading as a texture.
    bool m_resample_to_max_texture_size {true};

    /// Sets the recommended background color to use this TF.
    color_t m_background_color {0., 0., 0., 0.};

    /// The transfer function object is a piecewise function
    std::vector<transfer_function_piece::sptr> m_pieces;
};

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

inline transfer_function_piece::sptr transfer_function_piece::make()
{
    return std::make_shared<transfer_function_piece>();
}

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

inline transfer_function_piece& transfer_function_piece::operator=(const transfer_function_piece& _other)
{
    if(&_other != this)
    {
        this->clear();

        container_wrapper<data_t>::operator=(_other);

        this->m_level              = _other.m_level;
        this->m_window             = _other.m_window;
        this->m_interpolation_mode = _other.m_interpolation_mode;
        this->m_clamped            = _other.m_clamped;
        this->m_generated          = _other.m_generated;
    }

    return *this;
}

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

inline bool transfer_function_piece::operator==(const transfer_function_piece& _other) const noexcept
{
    if(!core::is_equal(m_level, _other.m_level)
       || !core::is_equal(m_window, _other.m_window)
       || m_interpolation_mode != _other.m_interpolation_mode
       || m_clamped != _other.m_clamped
       || m_generated != _other.m_generated)
    {
        return false;
    }

    // Super class last
    return core::is_equal(
        *this,
        _other
    );
}

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

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

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

inline transfer_function_base::value_t transfer_function_base::level() const
{
    return m_level;
}

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

inline transfer_function_base::value_t transfer_function_base::window() const
{
    return m_window;
}

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

inline enum transfer_function::interpolation_mode transfer_function_piece::get_interpolation_mode() const
{
    return m_interpolation_mode;
}

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

inline void transfer_function_piece::set_interpolation_mode(enum interpolation_mode _value)
{
    m_interpolation_mode = _value;
}

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

inline bool transfer_function_piece::clamped() const
{
    return m_clamped;
}

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

inline void transfer_function_piece::set_clamped(bool _value)
{
    m_clamped = _value;
}

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

inline bool transfer_function_piece::generated() const
{
    return m_generated;
}

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

inline void transfer_function_piece::set_generated(bool _value)
{
    m_generated = _value;
}

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

inline const std::string& transfer_function::name() const
{
    return m_name;
}

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

inline bool transfer_function::resample_to_max_texture_size() const
{
    return m_resample_to_max_texture_size;
}

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

inline void transfer_function::set_resample_to_max_texture_size(bool _value)
{
    m_resample_to_max_texture_size = _value;
}

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

inline void transfer_function::set_name(const std::string& _value)
{
    m_name = _value;
}

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

inline const transfer_function::color_t& transfer_function::background_color() const
{
    return m_background_color;
}

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

inline void transfer_function::set_background_color(const color_t& _value)
{
    m_background_color = _value;
}

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

inline std::vector<transfer_function_piece::sptr>& transfer_function::pieces()
{
    return m_pieces;
}

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

inline const std::vector<transfer_function_piece::sptr>& transfer_function::pieces() const
{
    return m_pieces;
}

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

inline bool transfer_function::empty() const
{
    return m_pieces.empty();
}

} // namespace sight::data