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 public:
36  m_sumab_by_a2b2(0.0), m_sumab_by_suma2(0.0),
37  m_mean_a(0.0), m_mean_b(0.0)
38  {
39  }
40 
41  NCCGradHelper(double sumab_by_a2b2, double sumab_by_suma2, double mean_a, double mean_b):
42  m_sumab_by_a2b2(2.0 * sumab_by_a2b2), m_sumab_by_suma2(sumab_by_suma2),
43  m_mean_a(mean_a), m_mean_b(mean_b)
44  {
45  }
46 
47  float get_gradient_scale(double a, double b) const {
48  return m_sumab_by_a2b2 * ( m_sumab_by_suma2 * ( a - m_mean_a ) - (b - m_mean_b));
49  }
50 
51 
52 private:
53  double m_sumab_by_a2b2;
54  double m_sumab_by_suma2;
55  double m_mean_a;
56  double m_mean_b;
57 };
58 
59 #ifdef __SSE2__
60 
61 
62 class EXPORT_CORE NCCSums {
63  typedef double v2df __attribute__ ((vector_size (16)));
64 public:
65  NCCSums():m_sumab(0.0), m_n(0.0) {
66  double zero[2] = {0.0, 0.0};
67  m_sum = _mm_loadu_pd(zero);
68  m_sum2 = m_sum;
69  }
70 
71  void add(double a, double b) {
72  v2df val = {static_cast<double>(a), static_cast<double>(b)};
73  v2df sq = val * val;
74  m_sum += val;
75  m_sum2 += sq;
76  m_sumab += a * b;
77  m_n += 1.0;
78  }
79 
80  NCCSums& operator += (const NCCSums& other) {
81  m_sum += other.m_sum;
82  m_sum2 += other.m_sum2;
83  m_sumab += other.m_sumab;
84  m_n += other.m_n;
85  return *this;
86  }
87 
88  bool has_samples() const {
89  return m_n > 0;
90  }
91 
92  double value() const;
93 
94  std::pair<double, NCCGradHelper> get_grad_helper() const;
95 
96 private:
97  v2df m_sum;
98  v2df m_sum2;
99  double m_sumab;
100  double m_n;
101 
102 };
103 
104 #else
105 
106 class EXPORT_CORE NCCSums {
107 public:
108  NCCSums():m_suma(0.0), m_sumb(0.0),
109  m_suma2(0.0), m_sumb2(0.0),
110  m_sumab(0.0), m_n(0.0) {
111  }
112 
113  void add(double a, double b) {
114  m_suma += a;
115  m_sumb += b;
116  m_suma2 += a * a;
117  m_sumb2 += b * b;
118  m_sumab += a * b;
119  m_n += 1.0;
120  }
121 
122  NCCSums& operator += (const NCCSums& other) {
123  m_suma += other.m_suma;
124  m_sumb += other.m_sumb;
125  m_suma2 += other.m_suma2;
126  m_sumb2 += other.m_sumb2;
127  m_sumab += other.m_sumab;
128  m_n += other.m_n;
129  return *this;
130  }
131 
132  double value() const;
133 
134  std::pair<double, NCCGradHelper> get_grad_helper() const;
135 
136 
137  bool has_samples() const {
138  return m_n > 0;
139  }
140 private:
141  double m_suma;
142  double m_sumb;
143  double m_suma2;
144  double m_sumb2;
145  double m_sumab;
146  double m_n;
147 };
148 
149 
150 #endif
151 
152 
153 inline NCCSums operator + (const NCCSums& lhs, const NCCSums& rhs)
154 {
155  NCCSums result(lhs);
156  result += rhs;
157  return result;
158 }
159 
161 
162 #endif
NCCSums operator+(const NCCSums &lhs, const NCCSums &rhs)
Definition: nccsum.hh:153
NCCSums()
Definition: nccsum.hh:108
NCCGradHelper()
Definition: nccsum.hh:35
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
EXPORT_2D C2DFVectorfield & operator+=(C2DFVectorfield &a, const C2DFVectorfield &b)
float get_gradient_scale(double a, double b) const
Definition: nccsum.hh:47
bool has_samples() const
Definition: nccsum.hh:137
void add(double a, double b)
Definition: nccsum.hh:113
NCCGradHelper(double sumab_by_a2b2, double sumab_by_suma2, double mean_a, double mean_b)
Definition: nccsum.hh:41
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36