MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
functions.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_FUNCTIONS_HEADER
11#define MATH_FUNCTIONS_HEADER
12
13#include <bitset>
14#include <cmath>
15#include <cinttypes>
16
17#ifdef _MSC_VER
18# include <intrin.h>
19#endif
20
21#include "math/defines.h"
22#include "math/vector.h"
23#include "math/matrix.h"
24
26
27/* ------------------------ Gaussian functions -------------------- */
28
34template <typename T>
35inline T
36gaussian (T const& x, T const& sigma)
37{
38 return std::exp(-((x * x) / (T(2) * sigma * sigma)));
39}
40
47template <typename T>
48inline T
49gaussian_xx (T const& xx, T const& sigma)
50{
51 return std::exp(-(xx / (T(2) * sigma * sigma)));
52}
53
57template <typename T>
58inline T
59gaussian_2d (T const& x, T const& y, T const& sigma_x, T const& sigma_y)
60{
61 return std::exp(-(x * x) / (T(2) * sigma_x * sigma_x)
62 - (y * y) / (T(2) * sigma_y * sigma_y));
63}
64
68template <typename T>
69inline T
70round (T const& x)
71{
72 return x > T(0) ? std::floor(x + T(0.5)) : std::ceil(x - T(0.5));
73}
74
75/* ------------------------- Interpolation ------------------------ */
76
78template <typename T>
79inline T
80interpolate (T const& v1, float w1)
81{
82 return v1 * w1;
83}
84
86template <>
87inline unsigned char
88interpolate (unsigned char const& v1, float w1)
89{
90 return (unsigned char)((float)v1 * w1 + 0.5f);
91}
92
94template <typename T>
95inline T
96interpolate (T const& v1, T const& v2, float w1, float w2)
97{
98 return v1 * w1 + v2 * w2;
99}
100
102template <>
103inline unsigned char
104interpolate (unsigned char const& v1, unsigned char const& v2,
105 float w1, float w2)
106{
107 return (unsigned char)((float)v1 * w1 + (float)v2 * w2 + 0.5f);
108}
109
111template <typename T>
112inline T
113interpolate (T const& v1, T const& v2, T const& v3,
114 float w1, float w2, float w3)
115{
116 return v1 * w1 + v2 * w2 + v3 * w3;
117}
118
120template <>
121inline unsigned char
122interpolate (unsigned char const& v1, unsigned char const& v2,
123 unsigned char const& v3, float w1, float w2, float w3)
124{
125 return (unsigned char)((float)v1 * w1 + (float)v2 * w2
126 + (float)v3 * w3 + 0.5f);
127}
128
130template <typename T>
131inline T
132interpolate (T const& v1, T const& v2, T const& v3, T const& v4,
133 float w1, float w2, float w3, float w4)
134{
135 return v1 * w1 + v2 * w2 + v3 * w3 + v4 * w4;
136}
137
139template <>
140inline unsigned char
141interpolate (unsigned char const& v1, unsigned char const& v2,
142 unsigned char const& v3, unsigned char const& v4,
143 float w1, float w2, float w3, float w4)
144{
145 return (unsigned char)((float)v1 * w1 + (float)v2 * w2
146 + (float)v3 * w3 + (float)v4 * w4 + 0.5f);
147}
148
149/* --------------------------- Special functions ------------------ */
150
154template <typename T>
155inline T
156sinc (T const& x)
157{
158 return (x == T(0) ? T(1) : std::sin(x) / x);
159}
160
161/* ----------------------------- Popcount ------------------------- */
162
164template <typename T>
165std::size_t constexpr
166popcount (T const x)
167{
168 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
169 "T must be an unsigned integral type.");
170 return std::bitset<sizeof(T) * 8>(x).count();
171}
172
173#ifdef _MSC_VER // MSVS fails to optimize std::bitset::count
174std::size_t inline
175popcount (std::uint32_t const x)
176{
177# if !(__amd64__ || __x86_64__ || _WIN64 || _M_X64)
178 __pragma (message("Warning: CPU support for _mm_popcnt_u32 is assumed, "
179 "but could not be verified."))
180# endif
181 return static_cast<std::size_t>(_mm_popcnt_u32(x));
182}
183
184std::size_t inline
185popcount (std::uint64_t const x)
186{
187# if __amd64__ || __x86_64__ || _WIN64 || _M_X64
188 return static_cast<std::size_t >(_mm_popcnt_u64(x));
189# else
190 std::uint32_t const low_bits = static_cast<std::uint32_t>(x);
191 std::uint32_t const high_bits = static_cast<std::uint32_t>(x >> 32);
192 return popcount(low_bits) + popcount(high_bits);
193# endif
194}
195#endif
196
197/* ------------------------------ Misc ---------------------------- */
198
202template <typename T>
203T const&
204clamp (T const& v, T const& min = T(0), T const& max = T(1))
205{
206 return (v < min ? min : (v > max ? max : v));
207}
208
209template <typename T>
210T
211bound_mirror (T const& v, T const& min, T const& max)
212{
213 return (v < min ? min - v : (v > max ? max + max - v : v));
214}
215
217template <typename T>
218T const&
219min (T const& a, T const& b, T const& c)
220{
221 return std::min(a, std::min(b, c));
222}
223
225template <typename T>
226T const&
227max (T const& a, T const& b, T const& c)
228{
229 return std::max(a, std::max(b, c));
230}
231
233template <typename T>
234T
235fastpow (T const& base, unsigned int exp)
236{
237 T ret = (exp == 0 ? T(1) : base);
238 unsigned int const exp2 = exp / 2;
239 unsigned int i = 1;
240 for (; i <= exp2; i = i * 2)
241 ret = ret * ret;
242 for (; i < exp; ++i)
243 ret = ret * base;
244 return ret;
245}
246
247inline int
249{
250 return bin ^ (bin >> 1);
251}
252
253inline int
255{
256 int b = 0, ret = 0;
257 for (int i = 31; i >= 0; --i)
258 {
259 b = (b + ((gc & (1 << i)) >> i)) % 2;
260 ret |= b << i;
261 }
262 return ret;
263}
264
266
267#endif // MATH_FUNCTIONS_HEADER
#define MATH_NAMESPACE_BEGIN
Definition defines.h:15
#define MATH_NAMESPACE_END
Definition defines.h:16
T sinc(T const &x)
Sinc function.
Definition functions.h:156
T const & clamp(T const &v, T const &min=T(0), T const &max=T(1))
Returns value 'v' clamped to the interval specified by 'min' and 'max'.
Definition functions.h:204
T round(T const &x)
Removes the fractional part of the value to the closest integer.
Definition functions.h:70
T fastpow(T const &base, unsigned int exp)
Takes base to the integer power of 'exp'.
Definition functions.h:235
T const & max(T const &a, T const &b, T const &c)
Returns the maximum value of three arguments.
Definition functions.h:227
T interpolate(T const &v1, float w1)
Generic interpolation (weighting) of a single value.
Definition functions.h:80
int from_gray_code(int gc)
Definition functions.h:254
std::size_t constexpr popcount(T const x)
Returns the number of one bits of an integer.
Definition functions.h:166
T const & min(T const &a, T const &b, T const &c)
Returns the minimum value of three arguments.
Definition functions.h:219
int to_gray_code(int bin)
Definition functions.h:248
T gaussian_xx(T const &xx, T const &sigma)
Gaussian function that expects x to be squared.
Definition functions.h:49
T bound_mirror(T const &v, T const &min, T const &max)
Definition functions.h:211
T gaussian_2d(T const &x, T const &y, T const &sigma_x, T const &sigma_y)
Gaussian function in 2D.
Definition functions.h:59