Skip to content

Commit

Permalink
Merge pull request #37547 from aaronfranke/tau
Browse files Browse the repository at this point in the history
Use Math_TAU and deg2rad/etc in more places and optimize code
  • Loading branch information
akien-mga authored Feb 1, 2021
2 parents 264504d + 1d5042c commit d2e1216
Show file tree
Hide file tree
Showing 34 changed files with 149 additions and 130 deletions.
4 changes: 2 additions & 2 deletions core/math/camera_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void CameraMatrix::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_
}

real_t sine, cotangent, deltaZ;
real_t radians = p_fovy_degrees / 2.0 * Math_PI / 180.0;
real_t radians = Math::deg2rad(p_fovy_degrees / 2.0);

deltaZ = p_z_far - p_z_near;
sine = Math::sin(radians);
Expand All @@ -116,7 +116,7 @@ void CameraMatrix::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_

real_t left, right, modeltranslation, ymax, xmax, frustumshift;

ymax = p_z_near * tan(p_fovy_degrees * Math_PI / 360.0f);
ymax = p_z_near * tan(Math::deg2rad(p_fovy_degrees / 2.0));
xmax = ymax * p_aspect;
frustumshift = (p_intraocular_dist / 2.0) * p_z_near / p_convergence_dist;

Expand Down
15 changes: 9 additions & 6 deletions core/math/geometry_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,11 @@ Vector<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) {
Vector<Plane> Geometry3D::build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis) {
Vector<Plane> planes;

const double sides_step = Math_TAU / p_sides;
for (int i = 0; i < p_sides; i++) {
Vector3 normal;
normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides);
normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides);
normal[(p_axis + 1) % 3] = Math::cos(i * sides_step);
normal[(p_axis + 2) % 3] = Math::sin(i * sides_step);

planes.push_back(Plane(normal, p_radius));
}
Expand All @@ -805,10 +806,11 @@ Vector<Plane> Geometry3D::build_sphere_planes(real_t p_radius, int p_lats, int p
axis_neg[(p_axis + 2) % 3] = 1.0;
axis_neg[p_axis] = -1.0;

const double lon_step = Math_TAU / p_lons;
for (int i = 0; i < p_lons; i++) {
Vector3 normal;
normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_lons);
normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_lons);
normal[(p_axis + 1) % 3] = Math::cos(i * lon_step);
normal[(p_axis + 2) % 3] = Math::sin(i * lon_step);

planes.push_back(Plane(normal, p_radius));

Expand All @@ -835,10 +837,11 @@ Vector<Plane> Geometry3D::build_capsule_planes(real_t p_radius, real_t p_height,
axis_neg[(p_axis + 2) % 3] = 1.0;
axis_neg[p_axis] = -1.0;

const double sides_step = Math_TAU / p_sides;
for (int i = 0; i < p_sides; i++) {
Vector3 normal;
normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides);
normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides);
normal[(p_axis + 1) % 3] = Math::cos(i * sides_step);
normal[(p_axis + 2) % 3] = Math::sin(i * sides_step);

planes.push_back(Plane(normal, p_radius));

Expand Down
8 changes: 4 additions & 4 deletions core/math/math_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ class Math {
return value;
}

static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * Math_PI / 180.0; }
static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * Math_PI / 180.0; }
static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * (Math_PI / 180.0); }
static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * (Math_PI / 180.0); }

static _ALWAYS_INLINE_ double rad2deg(double p_y) { return p_y * 180.0 / Math_PI; }
static _ALWAYS_INLINE_ float rad2deg(float p_y) { return p_y * 180.0 / Math_PI; }
static _ALWAYS_INLINE_ double rad2deg(double p_y) { return p_y * (180.0 / Math_PI); }
static _ALWAYS_INLINE_ float rad2deg(float p_y) { return p_y * (180.0 / Math_PI); }

static _ALWAYS_INLINE_ double lerp(double p_from, double p_to, double p_weight) { return p_from + (p_to - p_from) * p_weight; }
static _ALWAYS_INLINE_ float lerp(float p_from, float p_to, float p_weight) { return p_from + (p_to - p_from) * p_weight; }
Expand Down
38 changes: 18 additions & 20 deletions editor/node_3d_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ static float _find_closest_angle_to_half_pi_arc(const Vector3 &p_from, const Vec

//min_p = p_arc_xform.affine_inverse().xform(min_p);
float a = (Math_PI * 0.5) - Vector2(min_p.x, -min_p.z).angle();
return a * 180.0 / Math_PI;
return Math::rad2deg(a);
}

void Light3DGizmoPlugin::set_handle(EditorNode3DGizmo *p_gizmo, int p_idx, Camera3D *p_camera, const Point2 &p_point) {
Expand Down Expand Up @@ -1033,12 +1033,9 @@ void Light3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
p_gizmo->add_lines(points_primary, material_primary, false, color);
p_gizmo->add_lines(points_secondary, material_secondary, false, color);

const float ra = 16 * Math_PI * 2.0 / 64.0;
const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;

Vector<Vector3> handles;
handles.push_back(Vector3(0, 0, -r));
handles.push_back(Vector3(a.x, a.y, -d));
handles.push_back(Vector3(w, 0, -d));

p_gizmo->add_handles(handles, get_material("handles"));
p_gizmo->add_unscaled_billboard(icon, 0.05, color);
Expand Down Expand Up @@ -1095,8 +1092,8 @@ void AudioStreamPlayer3DGizmoPlugin::set_handle(EditorNode3DGizmo *p_gizmo, int
float closest_angle = 1e20;

for (int i = 0; i < 180; i++) {
float a = i * Math_PI / 180.0;
float an = (i + 1) * Math_PI / 180.0;
float a = Math::deg2rad((float)i);
float an = Math::deg2rad((float)(i + 1));

Vector3 from(Math::sin(a), 0, -Math::cos(a));
Vector3 to(Math::sin(an), 0, -Math::cos(an));
Expand Down Expand Up @@ -1145,9 +1142,10 @@ void AudioStreamPlayer3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Vector<Vector3> points_primary;
points_primary.resize(200);

real_t step = Math_TAU / 100.0;
for (int i = 0; i < 100; i++) {
const float a = i * 2.0 * Math_PI / 100.0;
const float an = (i + 1) * 2.0 * Math_PI / 100.0;
const float a = i * step;
const float an = (i + 1) * step;

const Vector3 from(Math::sin(a) * radius, Math::cos(a) * radius, ofs);
const Vector3 to(Math::sin(an) * radius, Math::cos(an) * radius, ofs);
Expand All @@ -1163,7 +1161,7 @@ void AudioStreamPlayer3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
points_secondary.resize(16);

for (int i = 0; i < 8; i++) {
const float a = i * 2.0 * Math_PI / 8.0;
const float a = i * (Math_TAU / 8.0);
const Vector3 from(Math::sin(a) * radius, Math::cos(a) * radius, ofs);

points_secondary.write[i * 2 + 0] = from;
Expand Down Expand Up @@ -2616,8 +2614,8 @@ void GPUParticlesCollision3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Vector<Vector3> collision_segments;

for (int i = 0; i < 64; i++) {
float ra = i * Math_PI * 2.0 / 64.0;
float rb = (i + 1) * Math_PI * 2.0 / 64.0;
float ra = i * (Math_TAU / 64.0);
float rb = (i + 1) * (Math_TAU / 64.0);
Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;

Expand Down Expand Up @@ -3317,7 +3315,7 @@ void BakedLightmapGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
int stack_count = 8;
int sector_count = 16;

float sector_step = 2 * Math_PI / sector_count;
float sector_step = (Math_PI * 2.0) / sector_count;
float stack_step = Math_PI / stack_count;

Vector<Vector3> vertices;
Expand Down Expand Up @@ -3454,7 +3452,7 @@ void LightmapProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
int stack_count = 8;
int sector_count = 16;

float sector_step = 2 * Math_PI / sector_count;
float sector_step = (Math_PI * 2.0) / sector_count;
float stack_step = Math_PI / stack_count;

Vector<Vector3> vertices;
Expand Down Expand Up @@ -3854,8 +3852,8 @@ void CollisionShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Vector<Vector3> collision_segments;

for (int i = 0; i < 64; i++) {
float ra = i * Math_PI * 2.0 / 64.0;
float rb = (i + 1) * Math_PI * 2.0 / 64.0;
float ra = i * (Math_TAU / 64.0);
float rb = (i + 1) * (Math_TAU / 64.0);
Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;

Expand Down Expand Up @@ -3939,8 +3937,8 @@ void CollisionShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Vector<Vector3> collision_segments;

for (int i = 0; i < 64; i++) {
float ra = i * Math_PI * 2.0 / 64.0;
float rb = (i + 1) * Math_PI * 2.0 / 64.0;
float ra = i * (Math_TAU / 64.0);
float rb = (i + 1) * (Math_TAU / 64.0);
Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;

Expand Down Expand Up @@ -4002,8 +4000,8 @@ void CollisionShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
Vector<Vector3> collision_segments;

for (int i = 0; i < 64; i++) {
float ra = i * Math_PI * 2.0 / 64.0;
float rb = (i + 1) * Math_PI * 2.0 / 64.0;
float ra = i * (Math_TAU / 64.0);
float rb = (i + 1) * (Math_TAU / 64.0);
Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;

Expand Down
10 changes: 5 additions & 5 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,17 @@ class SnapDialog : public ConfirmationDialog {
grid_step_x->set_value(p_grid_step.x);
grid_step_y->set_value(p_grid_step.y);
primary_grid_steps->set_value(p_primary_grid_steps);
rotation_offset->set_value(p_rotation_offset * (180 / Math_PI));
rotation_step->set_value(p_rotation_step * (180 / Math_PI));
rotation_offset->set_value(Math::rad2deg(p_rotation_offset));
rotation_step->set_value(Math::rad2deg(p_rotation_step));
scale_step->set_value(p_scale_step);
}

void get_fields(Point2 &p_grid_offset, Point2 &p_grid_step, int &p_primary_grid_steps, float &p_rotation_offset, float &p_rotation_step, float &p_scale_step) {
p_grid_offset = Point2(grid_offset_x->get_value(), grid_offset_y->get_value());
p_grid_step = Point2(grid_step_x->get_value(), grid_step_y->get_value());
p_primary_grid_steps = int(primary_grid_steps->get_value());
p_rotation_offset = rotation_offset->get_value() / (180 / Math_PI);
p_rotation_step = rotation_step->get_value() / (180 / Math_PI);
p_rotation_offset = Math::deg2rad(rotation_offset->get_value());
p_rotation_step = Math::deg2rad(rotation_step->get_value());
p_scale_step = scale_step->get_value();
}
};
Expand Down Expand Up @@ -5638,7 +5638,7 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) {
primary_grid_steps = 8; // A power-of-two value works better as a default
grid_step_multiplier = 0;
snap_rotation_offset = 0;
snap_rotation_step = 15 / (180 / Math_PI);
snap_rotation_step = Math::deg2rad(15.0);
snap_scale_step = 0.1f;
smart_snap_active = false;
grid_snap_active = false;
Expand Down
12 changes: 7 additions & 5 deletions editor/plugins/editor_preview_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {

int lats = 32;
int lons = 32;
float radius = 1.0;
const double lat_step = Math_TAU / lats;
const double lon_step = Math_TAU / lons;
real_t radius = 1.0;

Vector<Vector3> vertices;
Vector<Vector3> normals;
Expand All @@ -391,20 +393,20 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
Basis tt = Basis(Vector3(0, 1, 0), Math_PI * 0.5);

for (int i = 1; i <= lats; i++) {
double lat0 = Math_PI * (-0.5 + (double)(i - 1) / lats);
double lat0 = lat_step * (i - 1) - Math_TAU / 4;
double z0 = Math::sin(lat0);
double zr0 = Math::cos(lat0);

double lat1 = Math_PI * (-0.5 + (double)i / lats);
double lat1 = lat_step * i - Math_TAU / 4;
double z1 = Math::sin(lat1);
double zr1 = Math::cos(lat1);

for (int j = lons; j >= 1; j--) {
double lng0 = 2 * Math_PI * (double)(j - 1) / lons;
double lng0 = lon_step * (j - 1);
double x0 = Math::cos(lng0);
double y0 = Math::sin(lng0);

double lng1 = 2 * Math_PI * (double)(j) / lons;
double lng1 = lon_step * j;
double x1 = Math::cos(lng1);
double y1 = Math::sin(lng1);

Expand Down
15 changes: 9 additions & 6 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5358,9 +5358,10 @@ void Node3DEditor::_init_indicators() {

int arrow_sides = 16;

const real_t arrow_sides_step = Math_TAU / arrow_sides;
for (int k = 0; k < arrow_sides; k++) {
Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
Basis ma(ivec, k * arrow_sides_step);
Basis mb(ivec, (k + 1) * arrow_sides_step);

for (int j = 0; j < arrow_points - 1; j++) {
Vector3 points[4] = {
Expand Down Expand Up @@ -5435,13 +5436,14 @@ void Node3DEditor::_init_indicators() {
int n = 128; // number of circle segments
int m = 6; // number of thickness segments

real_t step = Math_TAU / n;
for (int j = 0; j < n; ++j) {
Basis basis = Basis(ivec, (Math_PI * 2.0f * j) / n);
Basis basis = Basis(ivec, j * step);

Vector3 vertex = basis.xform(ivec2 * GIZMO_CIRCLE_SIZE);

for (int k = 0; k < m; ++k) {
Vector2 ofs = Vector2(Math::cos((Math_PI * 2.0 * k) / m), Math::sin((Math_PI * 2.0 * k) / m));
Vector2 ofs = Vector2(Math::cos((Math_TAU * k) / m), Math::sin((Math_TAU * k) / m));
Vector3 normal = ivec * ofs.x + ivec2 * ofs.y;

surftool->set_normal(basis.xform(normal));
Expand Down Expand Up @@ -5568,9 +5570,10 @@ void Node3DEditor::_init_indicators() {

int arrow_sides = 4;

const real_t arrow_sides_step = Math_TAU / arrow_sides;
for (int k = 0; k < 4; k++) {
Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
Basis ma(ivec, k * arrow_sides_step);
Basis mb(ivec, (k + 1) * arrow_sides_step);

for (int j = 0; j < arrow_points - 1; j++) {
Vector3 points[4] = {
Expand Down
26 changes: 14 additions & 12 deletions modules/csg/csg_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,25 +927,27 @@ CSGBrush *CSGSphere3D::_build_brush() {
bool *invertw = invert.ptrw();

int face = 0;
const double lat_step = Math_TAU / rings;
const double lon_step = Math_TAU / radial_segments;

for (int i = 1; i <= rings; i++) {
double lat0 = Math_PI * (-0.5 + (double)(i - 1) / rings);
double lat0 = lat_step * (i - 1) - Math_TAU / 4;
double z0 = Math::sin(lat0);
double zr0 = Math::cos(lat0);
double u0 = double(i - 1) / rings;

double lat1 = Math_PI * (-0.5 + (double)i / rings);
double lat1 = lat_step * i - Math_TAU / 4;
double z1 = Math::sin(lat1);
double zr1 = Math::cos(lat1);
double u1 = double(i) / rings;

for (int j = radial_segments; j >= 1; j--) {
double lng0 = 2 * Math_PI * (double)(j - 1) / radial_segments;
double lng0 = lon_step * (j - 1);
double x0 = Math::cos(lng0);
double y0 = Math::sin(lng0);
double v0 = double(i - 1) / radial_segments;

double lng1 = 2 * Math_PI * (double)(j) / radial_segments;
double lng1 = lon_step * j;
double x1 = Math::cos(lng1);
double y1 = Math::sin(lng1);
double v1 = double(i) / radial_segments;
Expand Down Expand Up @@ -1266,8 +1268,8 @@ CSGBrush *CSGCylinder3D::_build_brush() {
float inc = float(i) / sides;
float inc_n = float((i + 1)) / sides;

float ang = inc * Math_PI * 2.0;
float ang_n = inc_n * Math_PI * 2.0;
float ang = inc * Math_TAU;
float ang_n = inc_n * Math_TAU;

Vector3 base(Math::cos(ang), 0, Math::sin(ang));
Vector3 base_n(Math::cos(ang_n), 0, Math::sin(ang_n));
Expand Down Expand Up @@ -1508,8 +1510,8 @@ CSGBrush *CSGTorus3D::_build_brush() {
float inci = float(i) / sides;
float inci_n = float((i + 1)) / sides;

float angi = inci * Math_PI * 2.0;
float angi_n = inci_n * Math_PI * 2.0;
float angi = inci * Math_TAU;
float angi_n = inci_n * Math_TAU;

Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
Expand All @@ -1518,8 +1520,8 @@ CSGBrush *CSGTorus3D::_build_brush() {
float incj = float(j) / ring_sides;
float incj_n = float((j + 1)) / ring_sides;

float angj = incj * Math_PI * 2.0;
float angj_n = incj_n * Math_PI * 2.0;
float angj = incj * Math_TAU;
float angj_n = incj_n * Math_TAU;

Vector2 normalj = Vector2(Math::cos(angj), Math::sin(angj)) * radius + Vector2(min_radius + radius, 0);
Vector2 normalj_n = Vector2(Math::cos(angj_n), Math::sin(angj_n)) * radius + Vector2(min_radius + radius, 0);
Expand Down Expand Up @@ -1891,8 +1893,8 @@ CSGBrush *CSGPolygon3D::_build_brush() {
float inci = float(i) / spin_sides;
float inci_n = float((i + 1)) / spin_sides;

float angi = -(inci * spin_degrees / 360.0) * Math_PI * 2.0;
float angi_n = -(inci_n * spin_degrees / 360.0) * Math_PI * 2.0;
float angi = -Math::deg2rad(inci * spin_degrees);
float angi_n = -Math::deg2rad(inci_n * spin_degrees);

Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
Expand Down
6 changes: 3 additions & 3 deletions modules/opensimplex/open_simplex_noise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ Ref<Image> OpenSimplexNoise::get_seamless_image(int p_size) const {
float ii = (float)i / (float)p_size;
float jj = (float)j / (float)p_size;

ii *= 2.0 * Math_PI;
jj *= 2.0 * Math_PI;
ii *= Math_TAU;
jj *= Math_TAU;

float radius = p_size / (2.0 * Math_PI);
float radius = p_size / Math_TAU;

float x = radius * Math::sin(jj);
float y = radius * Math::cos(jj);
Expand Down
Loading

0 comments on commit d2e1216

Please sign in to comment.