File: transfer_function_test.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 (623 lines) | stat: -rw-r--r-- 31,215 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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-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 "transfer_function_test.hpp"

#include <data/color.hpp>
#include <data/string.hpp>
#include <data/transfer_function.hpp>

#include <glm/common.hpp>
#include <glm/gtc/epsilon.hpp>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::data::ut::transfer_function_test);

static const double EPSILON = 1e-5;

// There might be some uncertainty when sampling, so we need to include an epsilon when testing equality
#define ASSERT_COLOR_EQUALS(c1, c2) \
        CPPUNIT_ASSERT(glm::all(glm::epsilonEqual(c1, c2, EPSILON)));

namespace sight::data::ut
{

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

void transfer_function_test::setUp()
{
    // Set up context before running a test.
}

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

void transfer_function_test::tearDown()
{
    // Clean up after the test run.
}

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

void transfer_function_test::constructor_test()
{
    data::transfer_function::sptr tf = std::make_shared<data::transfer_function>();

    CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong level ", 0.0, tf->level());
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong window", 2.0, tf->window());

    CPPUNIT_ASSERT_EQUAL(std::string(), tf->name());
    CPPUNIT_ASSERT(transfer_function::color_t() == tf->background_color());

    auto tf_data = tf->pieces().emplace_back(std::make_shared<data::transfer_function_piece>());

    CPPUNIT_ASSERT_EQUAL(transfer_function::interpolation_mode::linear, tf_data->get_interpolation_mode());
    CPPUNIT_ASSERT_EQUAL(true, tf_data->clamped());
    CPPUNIT_ASSERT_EQUAL(std::size_t(0), tf_data->size());
}

//------------------------------------------------------------------------------
void transfer_function_test::default_tf_test()
{
    data::transfer_function::csptr tf = data::transfer_function::create_default_tf();

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Wrong level ", 50.0, tf->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Wrong window", 500.0, tf->window(), EPSILON);

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Wrong level ", 50.0, tf->pieces()[0]->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Wrong window", 500.0, tf->pieces()[0]->window(), EPSILON);

    CPPUNIT_ASSERT_EQUAL(transfer_function::DEFAULT_TF_NAME, tf->name());
    CPPUNIT_ASSERT(transfer_function::color_t() == tf->background_color());

    const auto first_piece = tf->pieces().front();

    CPPUNIT_ASSERT_EQUAL(transfer_function::interpolation_mode::linear, first_piece->get_interpolation_mode());
    CPPUNIT_ASSERT_EQUAL(false, first_piece->clamped());
    CPPUNIT_ASSERT_EQUAL(std::size_t(2), first_piece->size());
}

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

void transfer_function_test::classic_get_set_test()
{
    data::transfer_function::sptr tf = data::transfer_function::create_default_tf();

    // Test getTFData()
    const auto tf_data                    = tf->pieces().front();
    auto itr                              = tf_data->cbegin();
    const transfer_function::value_t key1 = itr->first;
    const transfer_function::value_t key2 = (++itr)->first;

    auto itr_color                          = tf_data->cbegin();
    const transfer_function::color_t color1 = itr_color->second;
    const transfer_function::color_t color2 = (++itr_color)->second;

    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, key1, 1e-10);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, key2, 1e-10);

    const transfer_function::color_t expected_color1(0.0, 0.0, 0.0, 0.0);
    const transfer_function::color_t expected_color2(1.0, 1.0, 1.0, 1.0);
    CPPUNIT_ASSERT(expected_color1 == color1);
    CPPUNIT_ASSERT(expected_color2 == color2);
    CPPUNIT_ASSERT(expected_color1 == tf_data->find(key1)->second);

    // Test erase
    tf_data->erase(1.0);
    CPPUNIT_ASSERT_EQUAL((std::size_t) 1, tf_data->size());

    // Test clear()
    tf_data->clear();
    const std::size_t expected_cleared_size = 0;
    CPPUNIT_ASSERT_EQUAL(expected_cleared_size, tf_data->size());
}

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

void transfer_function_test::shallow_and_deep_copy_test()
{
    const data::transfer_function::sptr tf = sight::data::ut::transfer_function_test::create_tf_color();
    tf->set_level(900.6);
    tf->set_window(-200.02);

    sight::data::ut::transfer_function_test::check_tf_color(tf);

    const data::transfer_function::sptr shallow_copy_tf = std::make_shared<data::transfer_function>();
    shallow_copy_tf->shallow_copy(tf);
    sight::data::ut::transfer_function_test::check_tf_color(shallow_copy_tf);
    CPPUNIT_ASSERT_EQUAL(tf->pieces()[0], shallow_copy_tf->pieces()[0]);

    const data::transfer_function::sptr deep_copy_tf = data::object::copy(tf);
    sight::data::ut::transfer_function_test::check_tf_color(deep_copy_tf);
    // Pointer addresses must be different
    CPPUNIT_ASSERT(tf->pieces()[0] != deep_copy_tf->pieces()[0]);
}

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

data::transfer_function::sptr transfer_function_test::create_tf_color()
{
    data::transfer_function::sptr tf = std::make_shared<data::transfer_function>();

    tf->set_background_color(data::transfer_function::color_t(1.0, 0.3, 0.6, 0.1));
    tf->set_name("color_t");
    tf->set_window_min_max({-40.33, 150.});

    auto tf_data = tf->pieces().emplace_back(std::make_shared<data::transfer_function_piece>());
    tf_data->set_clamped(false);
    tf_data->set_interpolation_mode(data::transfer_function::interpolation_mode::nearest);
    tf_data->set_window_min_max({-40.33, 150.});

    tf_data->insert({-40.33, data::transfer_function::color_t(0.9, 0.2, 0.3, 0.4)});
    tf_data->insert({3, data::transfer_function::color_t(0.1, 0.2, 0.9, 0.4)}); // Invert point 3 <=> -0.2, for
    // tests
    tf_data->insert({-0.2, data::transfer_function::color_t(0.1, 0.9, 0.3, 0.4)});
    tf_data->insert({150, data::transfer_function::color_t(0.1, 0.2, 0.3, 0.9)});

    data::string::sptr my_string = std::make_shared<data::string>("fieldStringValue");
    tf->set_field("fieldStringKey", my_string);

    return tf;
}

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

void transfer_function_test::check_tf_color(data::transfer_function::sptr _tf)
{
    CPPUNIT_ASSERT(data::transfer_function::color_t(1.0, 0.3, 0.6, 0.1) == _tf->background_color());
    CPPUNIT_ASSERT_EQUAL(std::string("color_t"), _tf->name());

    CPPUNIT_ASSERT_EQUAL(-200.02, _tf->window());
    CPPUNIT_ASSERT_EQUAL(900.6, _tf->level());

    const auto tf_data = _tf->pieces().front();
    CPPUNIT_ASSERT_EQUAL(false, tf_data->clamped());
    CPPUNIT_ASSERT_EQUAL(data::transfer_function::interpolation_mode::nearest, tf_data->get_interpolation_mode());

    CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(4), tf_data->size());
    CPPUNIT_ASSERT_EQUAL(-40.33, tf_data->min_max().first);
    CPPUNIT_ASSERT_EQUAL(150., tf_data->min_max().second);

    CPPUNIT_ASSERT(data::transfer_function::color_t(0.9, 0.2, 0.3, 0.4) == tf_data->find(-40.33)->second);
    CPPUNIT_ASSERT(data::transfer_function::color_t(0.1, 0.9, 0.3, 0.4) == tf_data->find(-0.2)->second);
    CPPUNIT_ASSERT(data::transfer_function::color_t(0.1, 0.2, 0.9, 0.4) == tf_data->find(3)->second);
    CPPUNIT_ASSERT(data::transfer_function::color_t(0.1, 0.2, 0.3, 0.9) == tf_data->find(150)->second);

    CPPUNIT_ASSERT_EQUAL(
        std::string("fieldStringValue"),
        std::dynamic_pointer_cast<data::string>(_tf->get_field("fieldStringKey"))->value()
    );
}

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

void transfer_function_test::set_tf_data_test()
{
    data::transfer_function::sptr tf = sight::data::ut::transfer_function_test::create_tf_color();
    tf->set_level(900.6);
    tf->set_window(-200.02);

    auto piece = tf->pieces()[0];
    data::transfer_function::data_t tf_data;
    tf_data[-40.33] = data::transfer_function::color_t(0.9, 0.2, 0.3, 0.4);
    tf_data[3]      = data::transfer_function::color_t(0.1, 0.2, 0.9, 0.4);
    tf_data[-0.2]   = data::transfer_function::color_t(0.1, 0.9, 0.3, 0.4);
    tf_data[150]    = data::transfer_function::color_t(0.1, 0.2, 0.3, 0.9);
    std::copy(tf_data.begin(), tf_data.end(), inserter(*piece));

    sight::data::ut::transfer_function_test::check_tf_color(tf);
}

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

void transfer_function_test::linear_color_test()
{
    data::transfer_function::sptr tf = sight::data::ut::transfer_function_test::create_tf_color();

    // Value = -40.33 => color : {0.9, 0.2, 0.3, 0.4}
    // Value = -0.2   => color : {0.1, 0.9, 0.3, 0.4}
    // Value = 3      => color : {0.1, 0.2, 0.9, 0.4}
    // Value = 150    => color : {0.1, 0.2, 0.3, 0.9}
    auto piece = tf->pieces()[0];

    piece->set_clamped(true);
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.0, .0, .0, .0), piece->sample_linear(-120));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.0, .0, .0, .0), piece->sample_linear(200));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.9, .2, .3, .4), piece->sample_linear(-40.33));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .3, .9), piece->sample_linear(150));
    ASSERT_COLOR_EQUALS(
        data::transfer_function::color_t(.1, .2, .6, .65),
        piece->sample_linear(((150. - 3.) / 2.) + 3)
    );
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .9, .3, .4), piece->sample_linear(-0.2));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .9, .4), piece->sample_linear(3));
    ASSERT_COLOR_EQUALS(
        data::transfer_function::color_t(.1, .2, .75, .525),
        piece->sample_linear(((150. - 3.) / 4.) + 3)
    );

    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.0, .0, .0, .0), tf->sample_linear(-120));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.0, .0, .0, .0), tf->sample_linear(200));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.9, .2, .3, .4), tf->sample_linear(-40.33));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .3, .9), tf->sample_linear(150));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .6, .65), tf->sample_linear(((150. - 3.) / 2.) + 3));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .9, .3, .4), tf->sample_linear(-0.2));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .9, .4), tf->sample_linear(3));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .75, .525), tf->sample_linear(((150. - 3.) / 4.) + 3));

    piece->set_interpolation_mode(transfer_function::interpolation_mode::linear);
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.0, .0, .0, .0), piece->sample(-120));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.0, .0, .0, .0), piece->sample(200));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.9, .2, .3, .4), piece->sample(-40.33));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .3, .9), piece->sample(150));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .6, .65), piece->sample(((150. - 3.) / 2.) + 3));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .9, .3, .4), piece->sample(-0.2));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .9, .4), piece->sample(3));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .75, .525), piece->sample(((150. - 3.) / 4.) + 3));

    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.0, .0, .0, .0), tf->sample(-120));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.0, .0, .0, .0), tf->sample(200));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.9, .2, .3, .4), tf->sample(-40.33));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .3, .9), tf->sample(150));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .6, .65), tf->sample(((150. - 3.) / 2.) + 3));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .9, .3, .4), tf->sample(-0.2));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .9, .4), tf->sample(3));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .75, .525), tf->sample(((150. - 3.) / 4.) + 3));

    piece->set_clamped(false);
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.9, .2, .3, .4), piece->sample_linear(-120));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .3, .9), piece->sample_linear(200));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.9, .2, .3, .4), piece->sample_linear(-40.33));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .3, .9), piece->sample_linear(150));
    ASSERT_COLOR_EQUALS(
        data::transfer_function::color_t(.1, .2, .6, .65),
        piece->sample_linear(((150. - 3.) / 2.) + 3)
    );
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .9, .3, .4), piece->sample_linear(-0.2));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .9, .4), piece->sample_linear(3));
    ASSERT_COLOR_EQUALS(
        data::transfer_function::color_t(.1, .2, .75, .525),
        piece->sample_linear(((150. - 3.) / 4.) + 3)
    );

    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.9, .2, .3, .4), tf->sample_linear(-120));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .3, .9), tf->sample_linear(200));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.9, .2, .3, .4), tf->sample_linear(-40.33));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .3, .9), tf->sample_linear(150));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .6, .65), tf->sample_linear(((150. - 3.) / 2.) + 3));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .9, .3, .4), tf->sample_linear(-0.2));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .9, .4), tf->sample_linear(3));
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(.1, .2, .75, .525), tf->sample_linear(((150. - 3.) / 4.) + 3));
}

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

void transfer_function_test::nearest_color_test()
{
    data::transfer_function::sptr tf = sight::data::ut::transfer_function_test::create_tf_color();

    // Value = -40.33 => color : {0.9, 0.2, 0.3, 0.4}
    // Value = -0.2  => color : {0.1, 0.9, 0.3, 0.4}
    // Value = 3     => color : {0.1, 0.2, 0.9, 0.4}
    // Value = 150   => color : {0.1, 0.2, 0.3, 0.9}

    auto piece = tf->pieces()[0];

    piece->set_clamped(true);
    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == piece->sample_nearest(-120));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == piece->sample_nearest(200));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.9, .2, .3, .4) == piece->sample_nearest(-40.33));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == piece->sample_nearest(150));
    CPPUNIT_ASSERT(
        data::transfer_function::color_t(
            .1,
            .2,
            .3,
            .9
        ) == piece->sample_nearest(((150. - 3.) / 2.) + 3 + .1)
    );
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .9, .3, .4) == piece->sample_nearest(-0.2));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == piece->sample_nearest(3));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == piece->sample_nearest(((150. - 3.) / 4.) + 3));

    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == tf->sample_nearest(-120));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == tf->sample_nearest(200));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.9, .2, .3, .4) == tf->sample_nearest(-40.33));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == tf->sample_nearest(150));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == tf->sample_nearest(((150. - 3.) / 2.) + 3 + .1));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .9, .3, .4) == tf->sample_nearest(-0.2));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == tf->sample_nearest(3));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == tf->sample_nearest(((150. - 3.) / 4.) + 3));

    piece->set_interpolation_mode(transfer_function::interpolation_mode::nearest);
    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == piece->sample(-120));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == piece->sample(200));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.9, .2, .3, .4) == piece->sample(-40.33));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == piece->sample(150));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == piece->sample(((150. - 3.) / 2.) + 3 + 0.1));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .9, .3, .4) == piece->sample(-0.2));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == piece->sample(3));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == piece->sample(((150. - 3.) / 4.) + 3));

    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == tf->sample(-120));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == tf->sample(200));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.9, .2, .3, .4) == tf->sample(-40.33));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == tf->sample(150));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == tf->sample(((150. - 3.) / 2.) + 3 + 0.1));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .9, .3, .4) == tf->sample(-0.2));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == tf->sample(3));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == tf->sample(((150. - 3.) / 4.) + 3));

    piece->set_clamped(false);
    CPPUNIT_ASSERT(data::transfer_function::color_t(.9, .2, .3, .4) == piece->sample_nearest(-120));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == piece->sample_nearest(200));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.9, .2, .3, .4) == piece->sample_nearest(-40.33));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == piece->sample_nearest(150));
    CPPUNIT_ASSERT(
        data::transfer_function::color_t(
            .1,
            .2,
            .3,
            .9
        ) == piece->sample_nearest(((150. - 3.) / 2.) + 3 + .1)
    );
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .9, .3, .4) == piece->sample_nearest(-0.2));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == piece->sample_nearest(3));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == piece->sample_nearest(((150. - 3.) / 4.) + 3));

    CPPUNIT_ASSERT(data::transfer_function::color_t(.9, .2, .3, .4) == tf->sample_nearest(-120));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == tf->sample_nearest(200));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.9, .2, .3, .4) == tf->sample_nearest(-40.33));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == tf->sample_nearest(150));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .3, .9) == tf->sample_nearest(((150. - 3.) / 2.) + 3 + .1));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .9, .3, .4) == tf->sample_nearest(-0.2));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == tf->sample_nearest(3));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.1, .2, .9, .4) == tf->sample_nearest(((150. - 3.) / 4.) + 3));
}

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

void transfer_function_test::map_values_test()
{
    const auto tf_data = std::make_shared<data::transfer_function_piece>();

    tf_data->set_clamped(false);
    tf_data->set_window_min_max({-200, 300.});
    tf_data->insert({0., {0., 0., 0., 0.}});
    tf_data->insert({1., {1., 1., 1., 1.}});

    CPPUNIT_ASSERT_EQUAL(-200., tf_data->map_value_to_window(0.));
    CPPUNIT_ASSERT_EQUAL(50., tf_data->map_value_to_window(0.5));
    CPPUNIT_ASSERT_EQUAL(300., tf_data->map_value_to_window(1.));

    CPPUNIT_ASSERT_EQUAL(0., tf_data->map_value_from_window(-200.));
    CPPUNIT_ASSERT_EQUAL(.5, tf_data->map_value_from_window(50.));
    CPPUNIT_ASSERT_EQUAL(1., tf_data->map_value_from_window(300.));

    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == tf_data->sample_nearest(-200));
    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == tf_data->sample_nearest(0));
    CPPUNIT_ASSERT(data::transfer_function::color_t(1.0, 1.0, 1.0, 1.0) == tf_data->sample_nearest(55));
    CPPUNIT_ASSERT(data::transfer_function::color_t(1.0, 1.0, 1.0, 1.0) == tf_data->sample_nearest(100));

    // Insert a point that changes the maximum value
    tf_data->insert({2., {1., 1., 0., 1.}});

    CPPUNIT_ASSERT_EQUAL(-200., tf_data->map_value_to_window(0.));
    CPPUNIT_ASSERT_EQUAL(-75., tf_data->map_value_to_window(0.5));
    CPPUNIT_ASSERT_EQUAL(50., tf_data->map_value_to_window(1.));
    CPPUNIT_ASSERT_EQUAL(300., tf_data->map_value_to_window(2.));

    CPPUNIT_ASSERT_EQUAL(0., tf_data->map_value_from_window(-200.));
    CPPUNIT_ASSERT_EQUAL(.5, tf_data->map_value_from_window(-75.));
    CPPUNIT_ASSERT_EQUAL(1., tf_data->map_value_from_window(50.));
    CPPUNIT_ASSERT_EQUAL(2., tf_data->map_value_from_window(300.));

    CPPUNIT_ASSERT(data::transfer_function::color_t(.0, .0, .0, .0) == tf_data->sample_nearest(-200));
    CPPUNIT_ASSERT(data::transfer_function::color_t(1.0, 1.0, 1.0, 1.0) == tf_data->sample_nearest(0));
    CPPUNIT_ASSERT(data::transfer_function::color_t(1.0, 1.0, .5, 1.0) == tf_data->sample_linear(175));
    CPPUNIT_ASSERT(data::transfer_function::color_t(1.0, 1.0, 0., 1.0) == tf_data->sample_nearest(300.));
}

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

void transfer_function_test::piecewise_function_test()
{
    auto tf = std::make_shared<data::transfer_function>();

    auto piece_0 = std::make_shared<data::transfer_function_piece>();

    CPPUNIT_ASSERT(tf->empty());

    auto& pieces = tf->pieces();
    pieces.emplace_back(piece_0);

    piece_0->set_clamped(false);
    piece_0->set_window_min_max({-10, 0.});
    piece_0->insert({-10.0, {0.0, 0.0, 0.0, 0.0}}); // 1
    piece_0->insert({-5.0, {1.0, 0.3, 0.0, 1.0}});  // 2
    piece_0->insert({0.0, {0.0, 0.0, 1.0, 0.4}});   // 3

    CPPUNIT_ASSERT_EQUAL(std::size_t(3), piece_0->size());
    CPPUNIT_ASSERT_EQUAL(-5., piece_0->level());
    CPPUNIT_ASSERT_EQUAL(10., piece_0->window());

    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(0.0, 0.0, 0.0, 0.0), piece_0->find(-10.)->second);
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(1.0, 0.3, 0.0, 1.0), piece_0->find(-5.)->second);
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(0.0, 0.0, 1.0, 0.4), piece_0->find(0)->second);

    piece_0->set_clamped(true);

    CPPUNIT_ASSERT_EQUAL(std::size_t(3), piece_0->size());
    CPPUNIT_ASSERT_EQUAL(-5., piece_0->level());
    CPPUNIT_ASSERT_EQUAL(10.0, piece_0->window());

    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(0.0, 0.0, 0.0, 0.0), piece_0->find(-10.)->second);
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(1.0, 0.3, 0.0, 1.0), piece_0->find(-5.)->second);
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(0.0, 0.0, 1.0, 0.4), piece_0->find(0)->second);

    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(0.0, 0.0, 0.0, 0.0), piece_0->begin()->second);
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(0.0, 0.0, 1.0, 0.4), piece_0->rbegin()->second);

    auto piece_1 = std::make_shared<data::transfer_function_piece>();
    pieces.emplace_back(piece_1);

    piece_1->set_clamped(false);
    piece_1->set_window_min_max({0, 100.});
    piece_1->insert({0.0, {0.0, 0.6, 0.0, 0.5}});   // 4
    piece_1->insert({50.0, {1.0, 1.0, 0.0, 1.0}});  // 5
    piece_1->insert({100.0, {1.0, 0.0, 1.0, 0.5}}); // 6

    CPPUNIT_ASSERT_EQUAL(std::size_t(3), piece_1->size());

    tf->fit_window();
    CPPUNIT_ASSERT_DOUBLES_EQUAL(45.0, tf->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(110.0, tf->window(), EPSILON);

    // Yields 1 and 4 because the second tf is unclamped
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(0.0, 0.6, 0.0, 0.5), tf->sample(-10.));
    // Yields 2 and 4 because the second tf is unclamped
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(1.0, 0.6, 0.0, 1.0), tf->sample(-5.));
    // Yields 3 and exactly 4
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(0.0, 0.6, 0.8, 0.5), tf->sample(0.));
    // Yields exactly 5
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(1.0, 1.0, 0.0, 1.0), tf->sample(50.));
    // Yields exactly 6
    ASSERT_COLOR_EQUALS(data::transfer_function::color_t(1.0, 0.0, 1.0, 0.5), tf->sample(100.));

    // Test windowing
    tf->set_level(200);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(200.0, tf->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(110.0, tf->window(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(150.0, pieces[0]->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(10.0, pieces[0]->window(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(205.0, pieces[1]->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(100.0, pieces[1]->window(), EPSILON);

    tf->set_window(55);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(200.0, tf->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(55.0, tf->window(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(150.0, pieces[0]->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(5.0, pieces[0]->window(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(205.0, pieces[1]->level(), EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(50.0, pieces[1]->window(), EPSILON);
}

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

void transfer_function_test::equality_test()
{
    auto function1 = std::make_shared<data::transfer_function>();
    auto function2 = std::make_shared<data::transfer_function>();

    CPPUNIT_ASSERT(*function1 == *function2 && !(*function1 != *function2));

    // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
    #define TEST(op) \
            function1->op; \
            CPPUNIT_ASSERT_MESSAGE( \
                "Transfer functions should be different when using " #op " on the first one", \
                *function1 != *function2 && !(*function1 == *function2) \
            ); \
            function2->op; \
            CPPUNIT_ASSERT_MESSAGE( \
                "Transfer functions should be equal when using " #op " on both", \
                *function1 == *function2 && !(*function1 != *function2) \
            );

    // transfer_function::setWindow and transfer_function::setLevel aren't tested here, as the behavior is
    // counterintuitive: The equality of the TransferFunction isn't changed by setWindow et setLevel if the list of
    // transfer_function_piece is empty. Whether this is the correct behavior or not is still to be determined.

    TEST(set_name("3"));
    TEST(set_background_color({4, 5, 6, 7}));
    TEST(pieces().push_back(std::make_shared<data::transfer_function_piece>()));

    #undef TEST
}

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

void transfer_function_test::merge_test()
{
    auto function_0      = std::make_shared<data::transfer_function>();
    auto function_1      = std::make_shared<data::transfer_function>();
    auto function_backup = std::make_shared<data::transfer_function>();

    auto piece_0 = std::make_shared<data::transfer_function_piece>();
    {
        {
            piece_0->set_clamped(false);
            piece_0->set_window_min_max({-10, 0.});
            piece_0->insert({-10.0, {0.0, 0.0, 0.0, 0.0}});
            piece_0->insert({-5.0, {1.0, 0.3, 0.0, 1.0}});
            piece_0->insert({0.0, {0.0, 0.0, 1.0, 0.4}});
            function_0->pieces().push_back(piece_0);
        }

        // Copy this piece
        {
            auto a_piece_copy = std::make_shared<data::transfer_function_piece>();
            *a_piece_copy = *piece_0;
            function_1->pieces().push_back(a_piece_copy);
        }

        // The same as the snippet above, but for function_backup
        {
            auto another_piece_copy = std::make_shared<data::transfer_function_piece>();
            *another_piece_copy = *piece_0;
            function_backup->pieces().push_back(another_piece_copy);
        }
    }

    auto piece_1 = std::make_shared<data::transfer_function_piece>();
    {
        piece_1->set_clamped(false);
        piece_1->set_window_min_max({0, 100.});
        piece_1->insert({0.0, {0.0, 0.6, 0.0, 0.5}});
        piece_1->insert({50.0, {1.0, 1.0, 0.0, 1.0}});
        piece_1->insert({100.0, {1.0, 0.0, 1.0, 0.5}});

        function_1->pieces().push_back(piece_1);
    }

    // 1: Test that attempting to merge one TF into itself doesn't do anything
    {
        CPPUNIT_ASSERT_MESSAGE("Initialisation of the copy failed.", *function_0 == *function_backup);
        sight::data::transfer_function::merge(*function_0, *function_0);
        CPPUNIT_ASSERT_MESSAGE("Merging a TF into itself mustn't affect it.", *function_0 == *function_backup);
    }

    // 2: Add another piece to the second TF, and confirm that only new pieces were added
    {
        sight::data::transfer_function::merge(*function_0, *function_1);
        CPPUNIT_ASSERT_MESSAGE("Pieces should match.", *function_0 == *function_1);
        CPPUNIT_ASSERT_MESSAGE("No piece was inserted.", *function_0 != *function_backup);
        CPPUNIT_ASSERT_MESSAGE("Unexpected piece was added.", function_0->pieces().size() == 2);

        // Check that the information was correctly forwarded (no piece got modified in the process)
        for(const auto& piece : function_0->pieces())
        {
            CPPUNIT_ASSERT_MESSAGE("Pieces must not have been modified.", *piece == *piece_0 or * piece == *piece_1);
        }
    }
}

} // namespace sight::data::ut