Blender V5.0
scene/curves.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/curves.h"
6
7#include "util/math.h"
8
10
11/* Curve functions */
12
13void curvebounds(float *lower, float *upper, float3 *p, const int dim)
14{
15 float *p0 = &p[0].x;
16 float *p1 = &p[1].x;
17 float *p2 = &p[2].x;
18 float *p3 = &p[3].x;
19
20 /* Catmull-Rom weights. */
21 float curve_coef[4];
22 curve_coef[0] = p1[dim];
23 curve_coef[1] = 0.5f * (-p0[dim] + p2[dim]);
24 curve_coef[2] = 0.5f * (2 * p0[dim] - 5 * p1[dim] + 4 * p2[dim] - p3[dim]);
25 curve_coef[3] = 0.5f * (-p0[dim] + 3 * p1[dim] - 3 * p2[dim] + p3[dim]);
26
27 float discroot = curve_coef[2] * curve_coef[2] - 3 * curve_coef[3] * curve_coef[1];
28 float ta = -1.0f;
29 float tb = -1.0f;
30
31 if (discroot >= 0 && curve_coef[3] != 0.0f) {
32 discroot = sqrtf(discroot);
33 ta = (-curve_coef[2] - discroot) / (3 * curve_coef[3]);
34 tb = (-curve_coef[2] + discroot) / (3 * curve_coef[3]);
35 ta = (ta > 1.0f || ta < 0.0f) ? -1.0f : ta;
36 tb = (tb > 1.0f || tb < 0.0f) ? -1.0f : tb;
37 }
38
39 *upper = max(p1[dim], p2[dim]);
40 *lower = min(p1[dim], p2[dim]);
41
42 float exa = p1[dim];
43 float exb = p2[dim];
44
45 if (ta >= 0.0f) {
46 const float t2 = ta * ta;
47 const float t3 = t2 * ta;
48 exa = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * ta + curve_coef[0];
49 }
50 if (tb >= 0.0f) {
51 const float t2 = tb * tb;
52 const float t3 = t2 * tb;
53 exb = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * tb + curve_coef[0];
54 }
55
56 *upper = max(*upper, max(exa, exb));
57 *lower = min(*lower, min(exa, exb));
58}
59
#define CCL_NAMESPACE_END
#define sqrtf
CCL_NAMESPACE_BEGIN void curvebounds(float *lower, float *upper, float3 *p, const int dim)
#define min(a, b)
Definition sort.cc:36
float x
Definition sky_math.h:136
max
Definition text_draw.cc:251