MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
accum.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef MATH_ACCUM_HEADER
11#define MATH_ACCUM_HEADER
12
13#include "math/functions.h"
14#include "math/defines.h"
15
17
33template <typename T>
34class Accum
35{
36public:
37 T v;
38 float w;
39
40public:
42 Accum (void);
43
45 Accum (T const& init);
46
48 void add (T const& value, float weight);
49
51 void sub (T const& value, float weight);
52
58 T normalized (float weight) const;
59
65 T normalized (void) const;
66};
67
68/* ------------------------- Implementation ----------------------- */
69
70template <typename T>
71inline
73 : w(0.0f)
74{
75}
76
77template <typename T>
78inline
79Accum<T>::Accum (T const& init)
80 : v(init), w(0.0f)
81{
82}
83
84template <typename T>
85inline void
86Accum<T>::add (T const& value, float weight)
87{
88 this->v += value * weight;
89 this->w += weight;
90}
91
92template <typename T>
93inline void
94Accum<T>::sub (T const& value, float weight)
95{
96 this->v -= value * weight;
97 this->w -= weight;
98}
99
100template <typename T>
101inline T
102Accum<T>::normalized (float weight) const
103{
104 return this->v / weight;
105}
106
107template <typename T>
108inline T
110{
111 return this->v / this->w;
112}
113
114/* ------------------------- Specialization ----------------------- */
115
116template <>
117class Accum<unsigned char>
118{
119public:
120 float v;
121 float w;
122
123public:
124 Accum (void);
125 Accum (unsigned char const& init);
126 void add (unsigned char const& value, float weight);
127 void sub (unsigned char const& value, float weight);
128 unsigned char normalized (float weight) const;
129 unsigned char normalized (void) const;
130};
131
132/* ------------------------- Implementation ----------------------- */
133
134inline
136 : w(0.0f)
137{
138}
139
140inline
141Accum<unsigned char>::Accum (unsigned char const& init)
142 : v(init), w(0.0f)
143{
144}
145
146inline void
147Accum<unsigned char>::add (unsigned char const& value, float weight)
148{
149 this->v += static_cast<float>(value) * weight;
150 this->w += weight;
151}
152
153inline void
154Accum<unsigned char>::sub (unsigned char const& value, float weight)
155{
156 this->v -= static_cast<float>(value) * weight;
157 this->w -= weight;
158}
159
160inline unsigned char
161Accum<unsigned char>::normalized (float weight) const
162{
163 return static_cast<unsigned char>(math::round(this->v / weight));
164}
165
166inline unsigned char
168{
169 return static_cast<unsigned char>(math::round(this->v / this->w));
170}
171
173
174#endif /* MATH_ACCUM_HEADER */
void add(unsigned char const &value, float weight)
Accum(unsigned char const &init)
unsigned char normalized(float weight) const
void sub(unsigned char const &value, float weight)
unsigned char normalized(void) const
Accumulator class that operates on arbitrary types.
Definition accum.h:35
T normalized(void) const
Returns a normalized version of the internal value, i.e.
Definition accum.h:109
void sub(T const &value, float weight)
Subtracts the weighted given value from the internal value.
Definition accum.h:94
void add(T const &value, float weight)
Adds the weighted given value to the internal value.
Definition accum.h:86
Accum(void)
Leaves internal value uninitialized.
Definition accum.h:72
float w
Definition accum.h:38
#define MATH_NAMESPACE_BEGIN
Definition defines.h:15
#define MATH_NAMESPACE_END
Definition defines.h:16
T round(T const &x)
Removes the fractional part of the value to the closest integer.
Definition functions.h:70