annotate OrthancStone/Sources/Toolbox/Internals/BucketMapper.cpp @ 1893:90b5e116a5f9

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