Cleanup: use M_PI_2 and M_PI_4 where possible

The constant M_PI_4 is added to GLSL to ensure it works there too.

Differential Revision: https://developer.blender.org/D14288
This commit is contained in:
Hallam Roberts 2022-03-11 16:14:05 +01:00 committed by Brecht Van Lommel
parent 62a0984d72
commit 3da84d8b08
Notes: blender-bot 2023-02-14 11:00:17 +01:00
Referenced by commit 7dc4ac71e8, Fix T97278: wrong hair particle shape with kink spiral
Referenced by issue #97278, Regression: Increasing the shape value for spiral kink generates hair to infinite
30 changed files with 51 additions and 50 deletions

View File

@ -326,7 +326,7 @@ void GHOST_Wintab::getInput(std::vector<GHOST_WintabInfoWin32> &outWintabInfo)
ORIENTATION ort = pkt.pkOrientation;
/* Convert raw fixed point data to radians. */
float altRad = (float)((fabs((float)ort.orAltitude) / (float)m_maxAltitude) * M_PI / 2.0);
float altRad = (float)((fabs((float)ort.orAltitude) / (float)m_maxAltitude) * M_PI_2);
float azmRad = (float)(((float)ort.orAzimuth / (float)m_maxAzimuth) * M_PI * 2.0);
/* Find length of the stylus' projected vector on the XY plane. */
@ -334,7 +334,7 @@ void GHOST_Wintab::getInput(std::vector<GHOST_WintabInfoWin32> &outWintabInfo)
/* From there calculate X and Y components based on azimuth. */
out.tabletData.Xtilt = sin(azmRad) * vecLen;
out.tabletData.Ytilt = (float)(sin(M_PI / 2.0 - azmRad) * vecLen);
out.tabletData.Ytilt = (float)(sin(M_PI_2 - azmRad) * vecLen);
}
out.time = pkt.pkTime;

View File

@ -193,7 +193,7 @@ void IK_QJacobian::InvertSDLS()
// DLS. The SDLS damps individual singular values, instead of using a single
// damping term.
double max_angle_change = M_PI / 4.0;
double max_angle_change = M_PI_4;
double epsilon = 1e-10;
int i, j;

View File

@ -285,7 +285,7 @@ TEST(vec_roll_to_mat3_normalized, Roll1)
const float expected_roll_mat[3][3] = {{0.211324856f, 0.577350259f, -0.788675129f},
{0.577350259f, 0.577350259f, 0.577350259f},
{0.788675129f, -0.577350259f, -0.211324856f}};
test_vec_roll_to_mat3_normalized(input, float(M_PI * 0.5), expected_roll_mat);
test_vec_roll_to_mat3_normalized(input, float(M_PI_2), expected_roll_mat);
}
/** Test that the matrix is orthogonal for an input close to -Y. */

View File

@ -609,7 +609,7 @@ float BKE_mask_spline_project_co(MaskSpline *spline,
if (len_squared_v2(v1) > proj_eps_sq) {
ang1 = angle_v2v2(v1, n1);
if (ang1 > (float)M_PI / 2.0f) {
if (ang1 > (float)M_PI_2) {
ang1 = (float)M_PI - ang1;
}
@ -635,7 +635,7 @@ float BKE_mask_spline_project_co(MaskSpline *spline,
if (len_squared_v2(v2) > proj_eps_sq) {
ang2 = angle_v2v2(v2, n2);
if (ang2 > (float)M_PI / 2.0f) {
if (ang2 > (float)M_PI_2) {
ang2 = (float)M_PI - ang2;
}

View File

@ -85,7 +85,7 @@ static void do_kink_spiral_deform(ParticleKey *state,
* and goes up to the Golden Spiral for 1.0
* https://en.wikipedia.org/wiki/Golden_spiral
*/
const float b = shape * (1.0f + sqrtf(5.0f)) / (float)M_PI * 0.25f;
const float b = shape * (1.0f + sqrtf(5.0f)) / (float)M_PI_4;
/* angle of the spiral against the curve (rotated opposite to make a smooth transition) */
const float start_angle = ((b != 0.0f) ? atanf(1.0f / b) : (float)-M_PI_2) +
(b > 0.0f ? -(float)M_PI_2 : (float)M_PI_2);

View File

@ -95,10 +95,10 @@ MINLINE float saacos(float fac)
MINLINE float saasin(float fac)
{
if (UNLIKELY(fac <= -1.0f)) {
return (float)-M_PI / 2.0f;
return (float)-M_PI_2;
}
else if (UNLIKELY(fac >= 1.0f)) {
return (float)M_PI / 2.0f;
return (float)M_PI_2;
}
else {
return asinf(fac);
@ -131,10 +131,10 @@ MINLINE float saacosf(float fac)
MINLINE float saasinf(float fac)
{
if (UNLIKELY(fac <= -1.0f)) {
return (float)-M_PI / 2.0f;
return (float)-M_PI_2;
}
else if (UNLIKELY(fac >= 1.0f)) {
return (float)M_PI / 2.0f;
return (float)M_PI_2;
}
else {
return asinf(fac);

View File

@ -289,7 +289,7 @@ void mid_v3_v3v3_angle_weighted(float r[3], const float a[3], const float b[3])
BLI_ASSERT_UNIT_V3(b);
add_v3_v3v3(r, a, b);
angle = ((float)(1.0 / (M_PI / 2.0)) *
angle = ((float)M_2_PI *
/* normally we would only multiply by 2,
* but instead of an angle make this 0-1 factor */
2.0f) *
@ -305,7 +305,7 @@ void mid_v3_angle_weighted(float r[3])
/* double check they are normalized */
BLI_assert(len_squared_v3(r) <= 1.0f + FLT_EPSILON);
angle = ((float)(1.0 / (M_PI / 2.0)) *
angle = ((float)M_2_PI *
/* normally we would only multiply by 2,
* but instead of an angle make this 0-1 factor */
2.0f) *

View File

@ -4497,7 +4497,7 @@ void blo_do_versions_280(FileData *fd, Library *UNUSED(lib), Main *bmain)
clmd->sim_parms->max_internal_tension = 15.0f;
clmd->sim_parms->internal_compression = 15.0f;
clmd->sim_parms->max_internal_compression = 15.0f;
clmd->sim_parms->internal_spring_max_diversion = M_PI / 4.0f;
clmd->sim_parms->internal_spring_max_diversion = M_PI_4;
}
}
}

View File

@ -7106,7 +7106,7 @@ static void find_even_superellipse_chords(int n, float r, double *xvals, double
return;
}
if (r == PRO_CIRCLE_R) {
double temp = (M_PI / 2) / n;
double temp = M_PI_2 / n;
/* Angle spacing. */
for (int i = 0; i <= n; i++) {
xvals[i] = sin(i * temp);

View File

@ -613,7 +613,7 @@ void EEVEE_lightprobes_planar_data_from_object(Object *ob,
eplanar->clip_edge_y_neg = dot_v3v3(eplanar->clip_vec_y, vec);
/* Facing factors */
float max_angle = max_ff(1e-2f, 1.0f - probe->falloff) * M_PI * 0.5f;
float max_angle = max_ff(1e-2f, 1.0f - probe->falloff) * M_PI_2;
float min_angle = 0.0f;
eplanar->facing_scale = 1.0f / max_ff(1e-8f, cosf(min_angle) - cosf(max_angle));
eplanar->facing_bias = -min_ff(1.0f - 1e-8f, cosf(max_angle)) * eplanar->facing_scale;

View File

@ -217,7 +217,7 @@ void EEVEE_lookdev_cache_init(EEVEE_Data *vedata,
float x_rot_matrix[3][3];
DRW_view_viewmat_get(NULL, view_matrix, false);
copy_m3_m4(view_rot_matrix, view_matrix);
axis_angle_to_mat3_single(x_rot_matrix, 'X', M_PI / 2.0f);
axis_angle_to_mat3_single(x_rot_matrix, 'X', M_PI_2);
mul_m3_m3m3(view_rot_matrix, x_rot_matrix, view_rot_matrix);
mul_m3_m3m3(view_rot_matrix, g_data->studiolight_matrix, view_rot_matrix);
copy_m3_m3(studiolight_matrix, view_rot_matrix);

View File

@ -103,7 +103,7 @@ void main()
color4 = colors[3] * weights[3];
/* Extend to cover at least the unit circle */
const float extend = (cos(M_PI / 4.0) + 1.0) * 2.0;
const float extend = (cos(M_PI_4) + 1.0) * 2.0;
/* Crappy diagram
* ex 1
* | \

View File

@ -40,7 +40,7 @@ float light_translucent_power_with_falloff(LightData ld, vec3 N, vec4 l_vector)
if (ld.l_type >= AREA_RECT) {
power = (ld.l_sizex * ld.l_sizey * 4.0 * M_PI) * (1.0 / 80.0);
if (ld.l_type == AREA_ELLIPSE) {
power *= M_PI * 0.25;
power *= M_PI_4;
}
power *= 0.3 * 20.0 *
max(0.0, dot(-ld.l_forward, l_vector.xyz / l_vector.w)); /* XXX ad hoc, empirical */

View File

@ -387,7 +387,7 @@ void gpencil_light_pool_populate(GPENCIL_LightPool *lightpool, Object *ob)
normalize_m4_m4(mat, ob->obmat);
invert_m4(mat);
gp_light->type = GP_LIGHT_TYPE_SPOT;
gp_light->spotsize = cosf(M_PI * 0.5f);
gp_light->spotsize = cosf(M_PI_2);
gp_light->spotblend = (1.0f - gp_light->spotsize) * 1.0f;
}
else if (la->type == LA_SUN) {

View File

@ -31,22 +31,22 @@ static void square_to_circle(float x, float y, float *r, float *T)
if (x > -y) {
if (x > y) {
*r = x;
*T = (M_PI / 4.0f) * (y / x);
*T = M_PI_4 * (y / x);
}
else {
*r = y;
*T = (M_PI / 4.0f) * (2 - (x / y));
*T = M_PI_4 * (2 - (x / y));
}
}
else {
if (x < y) {
*r = -x;
*T = (M_PI / 4.0f) * (4 + (y / x));
*T = M_PI_4 * (4 + (y / x));
}
else {
*r = -y;
if (y != 0) {
*T = (M_PI / 4.0f) * (6 - (x / y));
*T = M_PI_4 * (6 - (x / y));
}
else {
*T = 0.0f;

View File

@ -6,6 +6,7 @@
#define M_PI 3.14159265358979323846 /* pi */
#define M_2PI 6.28318530717958647692 /* 2*pi */
#define M_PI_2 1.57079632679489661923 /* pi/2 */
#define M_PI_4 0.78539816339744830962 /* pi/4 */
#define M_1_PI 0.318309886183790671538 /* 1/pi */
#define M_1_2PI 0.159154943091895335768 /* 1/(2*pi) */
#define M_1_PI2 0.101321183642337771443 /* 1/(pi^2) */

View File

@ -4906,7 +4906,7 @@ bool ed_editnurb_spin(
copy_m3_m4(bmat, obedit->obmat);
invert_m3_m3(imat, bmat);
axis_angle_to_mat3(cmat, axis, M_PI / 4.0);
axis_angle_to_mat3(cmat, axis, M_PI_4);
mul_m3_m3m3(tmat, cmat, bmat);
mul_m3_m3m3(rotmat, imat, tmat);

View File

@ -44,7 +44,7 @@
# include "BKE_editmesh.h"
#endif
static const float dial_angle_partial = M_PI / 2;
static const float dial_angle_partial = M_PI_2;
static const float dial_angle_partial_margin = 0.92f;
#define ORTHO_AXIS_OFFSET 2

View File

@ -1153,16 +1153,16 @@ static int view_axis_exec(bContext *C, wmOperator *op)
float quat_test[4];
if (viewnum == RV3D_VIEW_LEFT) {
axis_angle_to_quat(quat_rotate, rv3d->viewinv[1], -M_PI / 2.0f);
axis_angle_to_quat(quat_rotate, rv3d->viewinv[1], -M_PI_2);
}
else if (viewnum == RV3D_VIEW_RIGHT) {
axis_angle_to_quat(quat_rotate, rv3d->viewinv[1], M_PI / 2.0f);
axis_angle_to_quat(quat_rotate, rv3d->viewinv[1], M_PI_2);
}
else if (viewnum == RV3D_VIEW_TOP) {
axis_angle_to_quat(quat_rotate, rv3d->viewinv[0], -M_PI / 2.0f);
axis_angle_to_quat(quat_rotate, rv3d->viewinv[0], -M_PI_2);
}
else if (viewnum == RV3D_VIEW_BOTTOM) {
axis_angle_to_quat(quat_rotate, rv3d->viewinv[0], M_PI / 2.0f);
axis_angle_to_quat(quat_rotate, rv3d->viewinv[0], M_PI_2);
}
else if (viewnum == RV3D_VIEW_FRONT) {
unit_qt(quat_rotate);

View File

@ -2059,7 +2059,7 @@ static float p_collapse_cost(PEdge *edge, PEdge *pair)
a1 = a1 - M_PI / 3.0;
a2 = a2 - M_PI / 3.0;
a3 = a3 - M_PI / 3.0;
shapeold = (a1 * a1 + a2 * a2 + a3 * a3) / ((M_PI / 2) * (M_PI / 2));
shapeold = (a1 * a1 + a2 * a2 + a3 * a3) / (M_PI_2 * M_PI_2);
nshapeold++;
}
@ -2068,7 +2068,7 @@ static float p_collapse_cost(PEdge *edge, PEdge *pair)
a1 = a1 - M_PI / 3.0;
a2 = a2 - M_PI / 3.0;
a3 = a3 - M_PI / 3.0;
shapenew = (a1 * a1 + a2 * a2 + a3 * a3) / ((M_PI / 2) * (M_PI / 2));
shapenew = (a1 * a1 + a2 * a2 + a3 * a3) / (M_PI_2 * M_PI_2);
nshapenew++;
}
@ -3730,7 +3730,7 @@ static float p_chart_minimum_area_angle(PChart *chart)
minarea = 1e10;
minangle = 0.0;
while (rotated <= (float)(M_PI / 2.0)) { /* INVESTIGATE: how far to rotate? */
while (rotated <= (float)M_PI_2) { /* INVESTIGATE: how far to rotate? */
/* rotate with the smallest angle */
i_min = 0;
mina = 1e10;
@ -3777,8 +3777,8 @@ static float p_chart_minimum_area_angle(PChart *chart)
}
/* try keeping rotation as small as possible */
if (minangle > (float)(M_PI / 4)) {
minangle -= (float)(M_PI / 2.0);
if (minangle > (float)M_PI_4) {
minangle -= (float)M_PI_2;
}
MEM_freeN(angles);

View File

@ -106,7 +106,7 @@ void semi_definite_symmetric_eigen(const double *mat, int n, double *eigen_vec,
delta = a_ll - a_mm;
if (delta == 0.0) {
x = -M_PI / 4;
x = -M_PI_4;
}
else {
x = -atan((a_lm + a_lm) / delta) / 2.0;

View File

@ -474,10 +474,10 @@ inline static real angle(WOEdge *h)
const Vec3r v = h->GetVec();
real sine = (n1 ^ n2) * v / v.norm();
if (sine >= 1.0) {
return M_PI / 2.0;
return M_PI_2;
}
if (sine <= -1.0) {
return -M_PI / 2.0;
return -M_PI_2;
}
return ::asin(sine);
}

View File

@ -1400,11 +1400,11 @@ inline void WOEdge::setVecAndAngle()
if (_paFace && _pbFace) {
float sine = (_pbFace->GetNormal() ^ _paFace->GetNormal()) * _vec / _vec.norm();
if (sine >= 1.0) {
_angle = M_PI / 2.0;
_angle = M_PI_2;
return;
}
if (sine <= -1.0) {
_angle = -M_PI / 2.0;
_angle = -M_PI_2;
return;
}
_angle = ::asin(sine);

View File

@ -181,7 +181,7 @@ static void imm_draw_circle_partial(GPUPrimType prim_type,
float sweep)
{
/* shift & reverse angle, increase 'nsegments' to match gluPartialDisk */
const float angle_start = -(DEG2RADF(start)) + (float)(M_PI / 2);
const float angle_start = -(DEG2RADF(start)) + (float)M_PI_2;
const float angle_end = -(DEG2RADF(sweep) - angle_start);
nsegments += 1;
immBegin(prim_type, nsegments);
@ -215,7 +215,7 @@ static void imm_draw_disk_partial(GPUPrimType prim_type,
CLAMP(sweep, -max_angle, max_angle);
/* shift & reverse angle, increase 'nsegments' to match gluPartialDisk */
const float angle_start = -(DEG2RADF(start)) + (float)(M_PI / 2);
const float angle_start = -(DEG2RADF(start)) + (float)M_PI_2;
const float angle_end = -(DEG2RADF(sweep) - angle_start);
nsegments += 1;
immBegin(prim_type, nsegments * 2);

View File

@ -14,8 +14,8 @@
UnitConverter::UnitConverter() : up_axis(COLLADAFW::FileInfo::Z_UP)
{
axis_angle_to_mat4_single(x_up_mat4, 'Y', -0.5 * M_PI);
axis_angle_to_mat4_single(y_up_mat4, 'X', 0.5 * M_PI);
axis_angle_to_mat4_single(x_up_mat4, 'Y', -M_PI_2);
axis_angle_to_mat4_single(y_up_mat4, 'X', M_PI_2);
unit_m4(z_up_mat4);
unit_m4(scale_mat4);

View File

@ -102,7 +102,7 @@ static void convert_to_z_up(pxr::UsdStageRefPtr stage, ImportSettings *r_setting
/* Rotate 90 degrees about the X-axis. */
float rmat[3][3];
float axis[3] = {1.0f, 0.0f, 0.0f};
axis_angle_normalized_to_mat3(rmat, axis, M_PI / 2.0f);
axis_angle_normalized_to_mat3(rmat, axis, M_PI_2);
unit_m4(r_settings->conversion_mat);
copy_m4_m3(r_settings->conversion_mat, rmat);

View File

@ -145,7 +145,7 @@
.compression_damp = 5.0f, \
.shear_damp = 5.0f, \
.internal_spring_max_length = 0.0f, \
.internal_spring_max_diversion = M_PI / 4.0f, \
.internal_spring_max_diversion = M_PI_4, \
.vgroup_intern = 0, \
.internal_tension = 15.0f, \
.internal_compression = 15.0f, \

View File

@ -2984,7 +2984,7 @@ static void rna_def_brush(BlenderRNA *brna)
prop = RNA_def_property(srna, "falloff_angle", PROP_FLOAT, PROP_ANGLE);
RNA_def_property_float_sdna(prop, NULL, "falloff_angle");
RNA_def_property_range(prop, 0, M_PI / 2);
RNA_def_property_range(prop, 0, M_PI_2);
RNA_def_property_ui_text(
prop,
"Falloff Angle",

View File

@ -392,13 +392,13 @@ static void rna_def_camera_stereo_data(BlenderRNA *brna)
RNA_def_property_update(prop, NC_OBJECT | ND_DRAW, NULL);
prop = RNA_def_property(srna, "pole_merge_angle_from", PROP_FLOAT, PROP_ANGLE);
RNA_def_property_range(prop, 0.0f, M_PI / 2.0);
RNA_def_property_range(prop, 0.0f, M_PI_2);
RNA_def_property_ui_text(
prop, "Pole Merge Start Angle", "Angle at which interocular distance starts to fade to 0");
RNA_def_property_update(prop, NC_OBJECT | ND_DRAW, NULL);
prop = RNA_def_property(srna, "pole_merge_angle_to", PROP_FLOAT, PROP_ANGLE);
RNA_def_property_range(prop, 0.0f, M_PI / 2.0);
RNA_def_property_range(prop, 0.0f, M_PI_2);
RNA_def_property_ui_text(
prop, "Pole Merge End Angle", "Angle at which interocular distance is 0");
RNA_def_property_update(prop, NC_OBJECT | ND_DRAW, NULL);

View File

@ -922,7 +922,7 @@ static void rna_def_cloth_sim_settings(BlenderRNA *brna)
prop = RNA_def_property(srna, "internal_spring_max_diversion", PROP_FLOAT, PROP_ANGLE);
RNA_def_property_float_sdna(prop, NULL, "internal_spring_max_diversion");
RNA_def_property_range(prop, 0.0f, M_PI / 4.0f);
RNA_def_property_range(prop, 0.0f, M_PI_4);
RNA_def_property_ui_text(prop,
"Internal Spring Max Diversion",
"How much the rays used to connect the internal points can diverge "