1891
|
1 /**
|
|
2 * Stone of Orthanc
|
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
|
|
4 * Department, University Hospital of Liege, Belgium
|
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
|
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
|
|
7 *
|
|
8 * This program is free software: you can redistribute it and/or
|
|
9 * modify it under the terms of the GNU Lesser General Public License
|
|
10 * as published by the Free Software Foundation, either version 3 of
|
|
11 * the License, or (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful, but
|
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 * Lesser General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU Lesser General Public
|
|
19 * License along with this program. If not, see
|
|
20 * <http://www.gnu.org/licenses/>.
|
|
21 **/
|
|
22
|
|
23
|
|
24 #include "BucketMapper.h"
|
|
25
|
|
26 #include <OrthancException.h>
|
|
27
|
|
28 #include <cassert>
|
|
29 #include <cmath>
|
|
30
|
|
31
|
|
32 namespace OrthancStone
|
|
33 {
|
|
34 namespace Internals
|
|
35 {
|
|
36 BucketMapper::BucketMapper(double minValue,
|
|
37 double maxValue,
|
|
38 size_t bucketsCount) :
|
|
39 minValue_(minValue),
|
|
40 maxValue_(maxValue),
|
|
41 bucketsCount_(bucketsCount)
|
|
42 {
|
|
43 if (minValue >= maxValue ||
|
|
44 bucketsCount <= 0)
|
|
45 {
|
|
46 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
|
|
47 }
|
|
48 }
|
|
49
|
|
50
|
|
51 void BucketMapper::CheckIndex(size_t i) const
|
|
52 {
|
|
53 if (i >= bucketsCount_)
|
|
54 {
|
|
55 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
|
|
56 }
|
|
57 }
|
|
58
|
|
59
|
|
60 double BucketMapper::GetBucketLow(size_t i) const
|
|
61 {
|
|
62 CheckIndex(i);
|
|
63 double alpha = static_cast<double>(i) / static_cast<double>(bucketsCount_);
|
|
64 return (1.0 - alpha) * minValue_ + alpha * maxValue_;
|
|
65 }
|
|
66
|
|
67
|
|
68 double BucketMapper::GetBucketHigh(size_t i) const
|
|
69 {
|
|
70 CheckIndex(i);
|
|
71 double alpha = static_cast<double>(i + 1) / static_cast<double>(bucketsCount_);
|
|
72 return (1.0 - alpha) * minValue_ + alpha * maxValue_;
|
|
73 }
|
|
74
|
|
75
|
|
76 double BucketMapper::GetBucketCenter(size_t i) const
|
|
77 {
|
|
78 CheckIndex(i);
|
|
79 double alpha = (static_cast<double>(i) + 0.5) / static_cast<double>(bucketsCount_);
|
|
80 return (1.0 - alpha) * minValue_ + alpha * maxValue_;
|
|
81 }
|
|
82
|
|
83
|
|
84 size_t BucketMapper::GetBucketIndex(double value) const
|
|
85 {
|
|
86 if (value < minValue_ ||
|
|
87 value > maxValue_)
|
|
88 {
|
|
89 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
|
|
90 }
|
|
91 else
|
|
92 {
|
|
93 double tmp = (value - minValue_) / (maxValue_ - minValue_) * static_cast<double>(bucketsCount_);
|
|
94 assert(tmp >= 0 && tmp <= static_cast<double>(bucketsCount_));
|
|
95
|
|
96 size_t bucket = static_cast<unsigned int>(std::floor(tmp));
|
|
97 if (bucket == bucketsCount_) // This is the case if "value == maxValue_"
|
|
98 {
|
|
99 return bucketsCount_ - 1;
|
|
100 }
|
|
101 else
|
|
102 {
|
|
103 return bucket;
|
|
104 }
|
|
105 }
|
|
106 }
|
|
107 }
|
|
108 }
|