Blender V5.3
math_filter.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10
11#include "BLI_assert.hh"
12#include "BLI_math_constants.hh"
13#include "BLI_math_filter.hh"
14
15namespace blender::math {
16
17static float filt_quadratic(float x)
18{
19 if (x < 0.0f) {
20 x = -x;
21 }
22 if (x < 0.5f) {
23 return 0.75f - (x * x);
24 }
25 if (x < 1.5f) {
26 return 0.50f * (x - 1.5f) * (x - 1.5f);
27 }
28 return 0.0f;
29}
30
31static float filt_cubic(float x)
32{
33 float x2 = x * x;
34
35 if (x < 0.0f) {
36 x = -x;
37 }
38
39 if (x < 1.0f) {
40 return 0.5f * x * x2 - x2 + 2.0f / 3.0f;
41 }
42 if (x < 2.0f) {
43 return (2.0f - x) * (2.0f - x) * (2.0f - x) / 6.0f;
44 }
45 return 0.0f;
46}
47
48static float filt_catrom(float x)
49{
50 float x2 = x * x;
51
52 if (x < 0.0f) {
53 x = -x;
54 }
55 if (x < 1.0f) {
56 return 1.5f * x2 * x - 2.5f * x2 + 1.0f;
57 }
58 if (x < 2.0f) {
59 return -0.5f * x2 * x + 2.5f * x2 - 4.0f * x + 2.0f;
60 }
61 return 0.0f;
62}
63
64static float filt_mitchell(float x)
65{
66 float b = 1.0f / 3.0f, c = 1.0f / 3.0f;
67 float p0 = (6.0f - 2.0f * b) / 6.0f;
68 float p2 = (-18.0f + 12.0f * b + 6.0f * c) / 6.0f;
69 float p3 = (12.0f - 9.0f * b - 6.0f * c) / 6.0f;
70 float q0 = (8.0f * b + 24.0f * c) / 6.0f;
71 float q1 = (-12.0f * b - 48.0f * c) / 6.0f;
72 float q2 = (6.0f * b + 30.0f * c) / 6.0f;
73 float q3 = (-b - 6.0f * c) / 6.0f;
74
75 if (x < -2.0f) {
76 return 0.0f;
77 }
78 if (x < -1.0f) {
79 return (q0 - x * (q1 - x * (q2 - x * q3)));
80 }
81 if (x < 0.0f) {
82 return (p0 + x * x * (p2 - x * p3));
83 }
84 if (x < 1.0f) {
85 return (p0 + x * x * (p2 + x * p3));
86 }
87 if (x < 2.0f) {
88 return (q0 + x * (q1 + x * (q2 + x * q3)));
89 }
90 return 0.0f;
91}
92
94{
95 constexpr float scale = 1.6f;
96
97 x = fabsf(x);
98
99 switch (kernel) {
101 if (x > 1.0f) {
102 return 0.0f;
103 }
104 return 1.0f;
106 if (x > 1.0f) {
107 return 0.0f;
108 }
109 return 1.0f - x;
111 return filt_quadratic(x * scale);
113 return filt_cubic(x * scale);
115 return filt_catrom(x * scale);
116 case FilterKernel::Gauss: {
117 constexpr float two_scale2 = 2.0f * scale * scale;
118 x *= 3.0f * scale;
119 return 1.0f / sqrtf(float(M_PI) * two_scale2) * expf(-x * x / two_scale2);
120 }
122 return filt_mitchell(x * scale);
123 default:
125 }
126 return 0.0f;
127}
128
129} // namespace blender::math
#define M_PI
#define BLI_assert_unreachable()
#define expf(x)
static float filt_quadratic(float x)
MatBase< T, NumCol, NumRow > scale(const MatBase< T, NumCol, NumRow > &mat, const VectorT &scale)
static float filt_cubic(float x)
static float filt_mitchell(float x)
static float filt_catrom(float x)
float filter_kernel_value(FilterKernel kernel, float x)
#define fabsf
#define sqrtf