comparison OrthancStone/Sources/Toolbox/BucketAccumulator1D.cpp @ 1891:3716d72161d2

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 19 Jan 2022 12:32:15 +0100
parents
children 07964689cb0b
comparison
equal deleted inserted replaced
1890:6ce81914f7e4 1891:3716d72161d2
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 "BucketAccumulator1D.h"
25
26 #include "LinearAlgebra.h"
27
28 #include <OrthancException.h>
29
30
31 namespace OrthancStone
32 {
33 BucketAccumulator1D::BucketAccumulator1D(double minValue,
34 double maxValue,
35 size_t countBuckets,
36 bool storeValues) :
37 mapper_(minValue, maxValue, countBuckets),
38 buckets_(countBuckets),
39 storeValues_(storeValues)
40 {
41 }
42
43
44 size_t BucketAccumulator1D::GetBucketContentSize(size_t i) const
45 {
46 mapper_.CheckIndex(i);
47 return buckets_[i].count_;
48 }
49
50
51 void BucketAccumulator1D::AddValue(double value)
52 {
53 Bucket& bucket = buckets_[mapper_.GetBucketIndex(value)];
54
55 bucket.count_++;
56
57 if (storeValues_)
58 {
59 bucket.values_.push_back(value);
60 }
61 }
62
63
64 size_t BucketAccumulator1D::FindBestBucket() const
65 {
66 size_t best = 0;
67
68 for (size_t i = 0; i < buckets_.size(); i++)
69 {
70 if (buckets_[i].count_ > buckets_[best].count_)
71 {
72 best = i;
73 }
74 }
75
76 return best;
77 }
78
79
80 double BucketAccumulator1D::ComputeBestMedian() const
81 {
82 if (!storeValues_)
83 {
84 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
85 }
86
87 const std::list<double>& values = buckets_[FindBestBucket()].values_;
88
89 std::vector<double> v;
90 v.reserve(values.size());
91 for (std::list<double>::const_iterator it = values.begin(); it != values.end(); ++it)
92 {
93 v.push_back(*it);
94 }
95
96 return LinearAlgebra::ComputeMedian(v);
97 }
98 }