annotate OrthancStone/Sources/Toolbox/BucketAccumulator1D.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 07964689cb0b
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 "BucketAccumulator1D.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 "LinearAlgebra.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 <OrthancException.h>
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 namespace OrthancStone
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 BucketAccumulator1D::BucketAccumulator1D(double minValue,
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 double maxValue,
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 size_t countBuckets,
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 bool storeValues) :
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 mapper_(minValue, maxValue, countBuckets),
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 buckets_(countBuckets),
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 storeValues_(storeValues)
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 }
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 size_t BucketAccumulator1D::GetBucketContentSize(size_t i) const
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 mapper_.CheckIndex(i);
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 return buckets_[i].count_;
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 BucketAccumulator1D::AddValue(double value)
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 Bucket& bucket = buckets_[mapper_.GetBucketIndex(value)];
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 bucket.count_++;
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 if (storeValues_)
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 bucket.values_.push_back(value);
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 size_t BucketAccumulator1D::FindBestBucket() const
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 size_t best = 0;
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 for (size_t i = 0; i < buckets_.size(); i++)
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 if (buckets_[i].count_ > buckets_[best].count_)
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 best = i;
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 return best;
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 double BucketAccumulator1D::ComputeBestMedian() const
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 if (!storeValues_)
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 }
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 const std::list<double>& values = buckets_[FindBestBucket()].values_;
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 std::vector<double> v;
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 v.reserve(values.size());
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 for (std::list<double>::const_iterator it = values.begin(); it != values.end(); ++it)
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 {
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 v.push_back(*it);
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 }
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 return LinearAlgebra::ComputeMedian(v);
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
3716d72161d2 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 }