My Project
nccsum.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_nccsum_hh
22 #define mia_core_nccsum_hh
23 
24 #include <utility>
25 #include <mia/core/defines.hh>
26 
27 #if defined(__SSE2__)
28 #include <emmintrin.h>
29 #endif
30 
32 
34 {
35 public:
37  m_sumab_by_a2b2(0.0), m_sumab_by_suma2(0.0),
38  m_mean_a(0.0), m_mean_b(0.0)
39  {
40  }
41 
42  NCCGradHelper(double sumab_by_a2b2, double sumab_by_suma2, double mean_a, double mean_b):
43  m_sumab_by_a2b2(2.0 * sumab_by_a2b2), m_sumab_by_suma2(sumab_by_suma2),
44  m_mean_a(mean_a), m_mean_b(mean_b)
45  {
46  }
47 
48  float get_gradient_scale(double a, double b) const
49  {
50  return m_sumab_by_a2b2 * ( m_sumab_by_suma2 * ( a - m_mean_a ) - (b - m_mean_b));
51  }
52 
53 
54 private:
55  double m_sumab_by_a2b2;
56  double m_sumab_by_suma2;
57  double m_mean_a;
58  double m_mean_b;
59 };
60 
61 #ifdef __SSE2__
62 
63 
64 class EXPORT_CORE NCCSums
65 {
66  typedef double v2df __attribute__ ((vector_size (16)));
67 public:
68  NCCSums(): m_sumab(0.0), m_n(0.0)
69  {
70  double zero[2] = {0.0, 0.0};
71  m_sum = _mm_loadu_pd(zero);
72  m_sum2 = m_sum;
73  }
74 
75  void add(double a, double b)
76  {
77  v2df val = {static_cast<double>(a), static_cast<double>(b)};
78  v2df sq = val * val;
79  m_sum += val;
80  m_sum2 += sq;
81  m_sumab += a * b;
82  m_n += 1.0;
83  }
84 
85  NCCSums& operator += (const NCCSums& other)
86  {
87  m_sum += other.m_sum;
88  m_sum2 += other.m_sum2;
89  m_sumab += other.m_sumab;
90  m_n += other.m_n;
91  return *this;
92  }
93 
94  bool has_samples() const
95  {
96  return m_n > 0;
97  }
98 
99  double value() const;
100 
101  std::pair<double, NCCGradHelper> get_grad_helper() const;
102 
103 private:
104  v2df m_sum;
105  v2df m_sum2;
106  double m_sumab;
107  double m_n;
108 
109 };
110 
111 #else
112 
114 {
115 public:
116  NCCSums(): m_suma(0.0), m_sumb(0.0),
117  m_suma2(0.0), m_sumb2(0.0),
118  m_sumab(0.0), m_n(0.0)
119  {
120  }
121 
122  void add(double a, double b)
123  {
124  m_suma += a;
125  m_sumb += b;
126  m_suma2 += a * a;
127  m_sumb2 += b * b;
128  m_sumab += a * b;
129  m_n += 1.0;
130  }
131 
132  NCCSums& operator += (const NCCSums& other)
133  {
134  m_suma += other.m_suma;
135  m_sumb += other.m_sumb;
136  m_suma2 += other.m_suma2;
137  m_sumb2 += other.m_sumb2;
138  m_sumab += other.m_sumab;
139  m_n += other.m_n;
140  return *this;
141  }
142 
143  double value() const;
144 
145  std::pair<double, NCCGradHelper> get_grad_helper() const;
146 
147 
148  bool has_samples() const
149  {
150  return m_n > 0;
151  }
152 private:
153  double m_suma;
154  double m_sumb;
155  double m_suma2;
156  double m_sumb2;
157  double m_sumab;
158  double m_n;
159 };
160 
161 
162 #endif
163 
164 
165 inline NCCSums operator + (const NCCSums& lhs, const NCCSums& rhs)
166 {
167  NCCSums result(lhs);
168  result += rhs;
169  return result;
170 }
171 
173 
174 #endif
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
NCCSums::has_samples
bool has_samples() const
Definition: nccsum.hh:148
operator+
NCCSums operator+(const NCCSums &lhs, const NCCSums &rhs)
Definition: nccsum.hh:165
NCCGradHelper::NCCGradHelper
NCCGradHelper(double sumab_by_a2b2, double sumab_by_suma2, double mean_a, double mean_b)
Definition: nccsum.hh:42
NCCGradHelper
Definition: nccsum.hh:34
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
NCCGradHelper::get_gradient_scale
float get_gradient_scale(double a, double b) const
Definition: nccsum.hh:48
operator+=
EXPORT_2D C2DFVectorfield & operator+=(C2DFVectorfield &a, const C2DFVectorfield &b)
NCCSums::add
void add(double a, double b)
Definition: nccsum.hh:122
EXPORT_CORE
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
NCCGradHelper::NCCGradHelper
NCCGradHelper()
Definition: nccsum.hh:36
NCCSums::NCCSums
NCCSums()
Definition: nccsum.hh:116
NCCSums::value
double value() const
NCCSums::get_grad_helper
std::pair< double, NCCGradHelper > get_grad_helper() const
defines.hh
NCCSums
Definition: nccsum.hh:114