Skip to content

Commit

Permalink
[math] removed unnecessary functions (std provides them)
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Jan 22, 2025
1 parent 9612f84 commit 5ed8325
Show file tree
Hide file tree
Showing 40 changed files with 232 additions and 266 deletions.
10 changes: 5 additions & 5 deletions editor/Widgets/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#pragma once

//= INCLUDES ===========
//= INCLUDES ======
#include "Widget.h"
#include "Math/Helper.h"
#include <array>
//======================
#include <utility>
//=================

struct Timings
{
Timings() { Clear(); }

void AddSample(const float sample)
{
m_min = spartan::math::helper::Min(m_min, sample);
m_max = spartan::math::helper::Max(m_max, sample);
m_min = std::min(m_min, sample);
m_max = std::max(m_max, sample);
m_sum += sample;
m_sample_count++;
m_avg = float(m_sum / static_cast<float>(m_sample_count));
Expand Down
4 changes: 2 additions & 2 deletions editor/Widgets/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void Properties::ShowLight(shared_ptr<Light> light) const
static vector<string> types = { "Directional", "Point", "Spot" };
float intensity = light->GetIntensityLumens();
float temperature_kelvin = light->GetTemperature();
float angle = light->GetAngle() * math::helper::RAD_TO_DEG * 2.0f;
float angle = light->GetAngle() * math::RAD_TO_DEG * 2.0f;
bool shadows = light->GetFlag(spartan::LightFlags::Shadows);
bool shadows_transparent = light->GetFlag(spartan::LightFlags::ShadowsTransparent);
bool shadows_screen_space = light->GetFlag(spartan::LightFlags::ShadowsScreenSpace);
Expand Down Expand Up @@ -362,7 +362,7 @@ void Properties::ShowLight(shared_ptr<Light> light) const

//= MAP ===================================================================================================================
if (intensity != light->GetIntensityLumens()) light->SetIntensity(intensity);
if (angle != light->GetAngle() * math::helper::RAD_TO_DEG * 0.5f) light->SetAngle(angle * math::helper::DEG_TO_RAD * 0.5f);
if (angle != light->GetAngle() * math::RAD_TO_DEG * 0.5f) light->SetAngle(angle * math::DEG_TO_RAD * 0.5f);
if (range != light->GetRange()) light->SetRange(range);
if (m_colorPicker_light->GetColor() != light->GetColor()) light->SetColor(m_colorPicker_light->GetColor());
if (temperature_kelvin != light->GetTemperature()) light->SetTemperature(temperature_kelvin);
Expand Down
2 changes: 1 addition & 1 deletion editor/Widgets/RenderOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ namespace
ImGui::InputFloat("", &value, step, 0.0f, format);
ImGui::PopItemWidth();
ImGui::PopID();
value = math::helper::Clamp(value, min, max);
value = math::Clamp(value, min, max);

// Only update if changed
if (Renderer::GetOption<float>(render_option) != value)
Expand Down
4 changes: 2 additions & 2 deletions editor/Widgets/TextureViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void TextureViewer::OnTickVisible()
ImGui::PushItemWidth(85 * spartan::Window::GetDpiScale());
ImGui::InputInt("Mip", &mip_level);
ImGui::PopItemWidth();
mip_level = math::helper::Clamp(mip_level, 0, static_cast<int>(texture_current->GetMipCount()) - 1);
mip_level = math::Clamp(mip_level, 0, static_cast<int>(texture_current->GetMipCount()) - 1);
}

// array level control
Expand All @@ -179,7 +179,7 @@ void TextureViewer::OnTickVisible()
ImGui::PushItemWidth(85 * spartan::Window::GetDpiScale());
ImGui::InputInt("Array", &array_level);
ImGui::PopItemWidth();
array_level = math::helper::Clamp(array_level, 0, static_cast<int>(texture_current->GetDepth()) - 1);
array_level = math::Clamp(array_level, 0, static_cast<int>(texture_current->GetDepth()) - 1);
}

ImGui::BeginGroup();
Expand Down
10 changes: 5 additions & 5 deletions runtime/Core/GeometryProcessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ namespace spartan::geometry_processing
Vector3 tangent = Vector3(1, 0, 0);
vertices->emplace_back(Vector3(0, radius, 0), Vector2::Zero, normal, tangent);

const float phiStep = helper::PI / stacks;
const float thetaStep = 2.0f * helper::PI / slices;
const float phiStep = PI / stacks;
const float thetaStep = 2.0f * PI / slices;

for (int i = 1; i <= stacks - 1; i++)
{
Expand All @@ -181,7 +181,7 @@ namespace spartan::geometry_processing

Vector3 t = Vector3(-radius * sin(phi) * sin(theta), 0, radius * sin(phi) * cos(theta)).Normalized();
Vector3 n = p.Normalized();
Vector2 uv = Vector2(theta / (helper::PI * 2), phi / helper::PI);
Vector2 uv = Vector2(theta / (PI * 2), phi / PI);
vertices->emplace_back(p, uv, n, t);
}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ namespace spartan::geometry_processing
{
const float y = -0.5f * height + i * stackHeight;
const float r = radiusBottom + i * radiusStep;
const float dTheta = 2.0f * helper::PI / slices;
const float dTheta = 2.0f * PI / slices;
for (int j = 0; j <= slices; j++)
{
const float c = cos(j * dTheta);
Expand Down Expand Up @@ -270,7 +270,7 @@ namespace spartan::geometry_processing
// Build top cap
int baseIndex = (int)vertices->size();
float y = 0.5f * height;
const float dTheta = 2.0f * helper::PI / slices;
const float dTheta = 2.0f * PI / slices;

Vector3 normal;
Vector3 tangent;
Expand Down
2 changes: 1 addition & 1 deletion runtime/Core/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace spartan
}

// clamp to a minimum of 10 FPS to avoid unresponsiveness
fps_in = math::helper::Clamp(fps_in, fps_min, fps_max);
fps_in = math::Clamp(fps_in, fps_min, fps_max);

if (fps_limit == fps_in)
return;
Expand Down
1 change: 1 addition & 0 deletions runtime/Core/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <chrono>
#include <random>
#include <future>
#include <utility>
//===========================

//= RUNTIME =========================
Expand Down
44 changes: 22 additions & 22 deletions runtime/Game/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace spartan
constexpr float brake_ramp_speed = 5000.0f; // rate at which brake force increases (human pressing the brake and vehicle applying brake pads)

// steering
constexpr float steering_angle_max = 40.0f * math::helper::DEG_TO_RAD; // the maximum steering angle of the front wheels
constexpr float steering_angle_max = 40.0f * math::DEG_TO_RAD; // the maximum steering angle of the front wheels
constexpr float steering_return_speed = 5.0f; // the speed at which the steering wheel returns to the center

// aerodynamics
Expand Down Expand Up @@ -141,12 +141,12 @@ namespace spartan
oss << fixed << setprecision(2);

oss << "Wheel: " << wheel_name << "\n";
oss << "Steering: " << static_cast<float>(wheel_info.m_steering) * math::helper::RAD_TO_DEG << " deg\n";
oss << "Steering: " << static_cast<float>(wheel_info.m_steering) * math::RAD_TO_DEG << " deg\n";
oss << "Angular velocity: " << static_cast<float>(wheel_info.m_deltaRotation * 0.5f) / static_cast<float>(Physics::GetTimeStepInternalSec()) << " rad/s\n";
oss << "Torque: " << wheel_info.m_engineForce << " N\n";
oss << "Suspension length: " << wheel_info.m_raycastInfo.m_suspensionLength << " m\n";
oss << "Slip ratio: " << parameters.pacejka_slip_ratio[wheel_index] << " ( Fz: " << parameters.pacejka_fz[wheel_index] << " N ) \n";
oss << "Slip angle: " << parameters.pacejka_slip_angle[wheel_index] * math::helper::RAD_TO_DEG << " ( Fx: " << parameters.pacejka_fx[wheel_index] << " N ) \n";
oss << "Slip angle: " << parameters.pacejka_slip_angle[wheel_index] * math::RAD_TO_DEG << " ( Fx: " << parameters.pacejka_fx[wheel_index] << " N ) \n";

return oss.str();
}
Expand All @@ -166,7 +166,7 @@ namespace spartan
oss.clear();
oss << fixed << setprecision(2);

oss << "Speed: " << math::helper::Abs<float>(speed) << " Km/h\n"; // meters per second
oss << "Speed: " << abs(speed) << " Km/h\n"; // meters per second
oss << "Torque: " << parameters.engine_torque << " N·m \n"; // Newton meters
oss << "RPM: " << parameters.engine_rpm << " rpm\n"; // revolutions per minute, not an SI unit, but commonly used
oss << "Gear: " << parameters.gear << "\n"; // gear has no unit
Expand Down Expand Up @@ -221,13 +221,13 @@ namespace spartan
else // reverse
{
// in reverse, both velocities are negative, so we take their absolute values
numerator = math::helper::Abs(velocity_vehicle) - math::helper::Abs(velocity_wheel);
numerator = abs(velocity_vehicle) - abs(velocity_wheel);
}

float denominator = math::helper::Max(math::helper::Abs(velocity_wheel), math::helper::SMALL_FLOAT);
float denominator = max(abs(velocity_wheel), math::SMALL_FLOAT);
float slip_ratio = numerator / denominator;

return math::helper::Clamp<float>(slip_ratio, -1.0f, 1.0f);
return math::Clamp<float>(slip_ratio, -1.0f, 1.0f);
}

float compute_slip_angle(const btVector3& wheel_forward, const btVector3& wheel_side, const btVector3& vehicle_velocity)
Expand All @@ -241,9 +241,9 @@ namespace spartan
if (vehicle_velocity.length() < 0.05f)
return 0.0f;

float v_z = math::helper::Abs(vehicle_velocity.dot(wheel_forward));
float v_x = math::helper::Abs(vehicle_velocity.dot(wheel_side));
float slip_angle = atan2(v_x, v_z + math::helper::SMALL_FLOAT);
float v_z = abs(vehicle_velocity.dot(wheel_forward));
float v_x = abs(vehicle_velocity.dot(wheel_side));
float slip_angle = atan2(v_x, v_z + math::SMALL_FLOAT);

return slip_angle;
}
Expand All @@ -262,7 +262,7 @@ namespace spartan
}
else // slip angle
{
slip *= math::helper::RAD_TO_DEG; // to degrees
slip *= math::RAD_TO_DEG; // to degrees
}

// coefficients from the pacejka '94 model
Expand All @@ -285,10 +285,10 @@ namespace spartan
// compute the parameters for the Pacejka ’94 formula
float Fz = normal_load;
float C = b0;
float D = Fz * (b1 * Fz + b2) + math::helper::SMALL_FLOAT;
float D = Fz * (b1 * Fz + b2) + math::SMALL_FLOAT;
float BCD = (b3 * Fz * Fz + b4 * Fz) * exp(-b5 * Fz);
float B = BCD / (C * D);
float E = (b6 * Fz * Fz + b7 * Fz + b8) * (1 - b13 * math::helper::Sign(slip + (b9 * Fz + b10)));
float E = (b6 * Fz * Fz + b7 * Fz + b8) * (1 - b13 * math::Sign(slip + (b9 * Fz + b10)));
float H = b9 * Fz + b10;
float V = b11 * Fz + b12;
float Bx1 = B * (slip + H);
Expand Down Expand Up @@ -496,10 +496,10 @@ namespace spartan
{
btWheelInfo* wheel_info = &parameters.vehicle->getWheelInfo(0);
float wheel_angular_velocity = wheel_info->m_deltaRotation / static_cast<float>(Timer::GetDeltaTimeSec());
float wheel_rpm = (wheel_angular_velocity * 60.0f) / (2.0f * math::helper::PI);
float wheel_rpm = (wheel_angular_velocity * 60.0f) / (2.0f * math::PI);
float target_rpm = tuning::engine_idle_rpm + wheel_rpm * parameters.gear_ratio * tuning::gearbox_final_drive;
target_rpm *= math::helper::Abs<float>(parameters.throttle);
target_rpm = math::helper::Clamp(target_rpm, tuning::engine_idle_rpm, tuning::engine_max_rpm);
target_rpm *= abs(parameters.throttle);
target_rpm = math::Clamp(target_rpm, tuning::engine_idle_rpm, tuning::engine_max_rpm);

const float rev_up_down_speed = 0.1f;
parameters.engine_rpm = lerp(parameters.engine_rpm, target_rpm, rev_up_down_speed);
Expand Down Expand Up @@ -669,7 +669,7 @@ namespace spartan
// compute engine torque and/or breaking force
{
// determine when to stop breaking
if (math::helper::Abs<float>(GetSpeedMetersPerSecond()) < 0.1f)
if (abs(GetSpeedMetersPerSecond()) < 0.1f)
{
m_parameters.break_until_opposite_torque = false;
}
Expand Down Expand Up @@ -719,7 +719,7 @@ namespace spartan
}

// lerp to new steering angle - real life vehicles don't snap their wheels to the target angle
m_parameters.steering_angle = math::helper::Lerp<float>(m_parameters.steering_angle, steering_angle_target, tuning::steering_return_speed * delta_time_sec);
m_parameters.steering_angle = math::Lerp<float>(m_parameters.steering_angle, steering_angle_target, tuning::steering_return_speed * delta_time_sec);

// set the steering angle
m_parameters.vehicle->setSteeringValue(m_parameters.steering_angle, tuning::wheel_fl);
Expand Down Expand Up @@ -780,11 +780,11 @@ namespace spartan

if (breaking > 0.0f)
{
m_parameters.break_force = math::helper::Min<float>(m_parameters.break_force + tuning::brake_ramp_speed * delta_time_sec * breaking, tuning::brake_force_max);
m_parameters.break_force = min(m_parameters.break_force + tuning::brake_ramp_speed * delta_time_sec * breaking, tuning::brake_force_max);
}
else
{
m_parameters.break_force = math::helper::Max<float>(m_parameters.break_force - tuning::brake_ramp_speed * delta_time_sec, 0.0f);
m_parameters.break_force = max(m_parameters.break_force - tuning::brake_ramp_speed * delta_time_sec, 0.0f);
}

float bullet_brake_force = m_parameters.break_force * 0.03f;
Expand All @@ -806,7 +806,7 @@ namespace spartan
// steering wheel
if (m_parameters.transform_steering_wheel)
{
m_parameters.transform_steering_wheel->SetRotationLocal(Quaternion::FromEulerAngles(0.0f, 0.0f, -m_parameters.steering_angle * math::helper::RAD_TO_DEG));
m_parameters.transform_steering_wheel->SetRotationLocal(Quaternion::FromEulerAngles(0.0f, 0.0f, -m_parameters.steering_angle * math::RAD_TO_DEG));
}

// wheels
Expand All @@ -826,7 +826,7 @@ namespace spartan
float x, y, z;
transform_bt.getRotation().getEulerZYX(x, y, z);
float steering_angle_rad = m_parameters.vehicle->getSteeringValue(wheel_index);
Quaternion rotation = Quaternion::FromEulerAngles(z * math::helper::RAD_TO_DEG, steering_angle_rad * math::helper::RAD_TO_DEG, 0.0f);
Quaternion rotation = Quaternion::FromEulerAngles(z * math::RAD_TO_DEG, steering_angle_rad * math::RAD_TO_DEG, 0.0f);
transform->SetRotationLocal(rotation);
}
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/Input/InputGamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ namespace spartan
if (!gamepad.is_connected)
return false;

Uint16 low_frequency_rumble = static_cast<uint16_t>(helper::Clamp(left_motor_speed, 0.0f, 1.0f) * 65535); // convert [0, 1] to [0, 65535]
Uint16 high_frequency_rumble = static_cast<uint16_t>(helper::Clamp(right_motor_speed, 0.0f, 1.0f) * 65535); // convert [0, 1] to [0, 65535]
Uint16 low_frequency_rumble = static_cast<uint16_t>(Clamp(left_motor_speed, 0.0f, 1.0f) * 65535); // convert [0, 1] to [0, 65535]
Uint16 high_frequency_rumble = static_cast<uint16_t>(Clamp(right_motor_speed, 0.0f, 1.0f) * 65535); // convert [0, 1] to [0, 65535]
Uint32 duration_ms = 0xFFFFFFFF;

if (SDL_GameControllerRumble(static_cast<SDL_GameController*>(gamepad.sdl_pointer), low_frequency_rumble, high_frequency_rumble, duration_ms) == -1)
Expand Down
42 changes: 21 additions & 21 deletions runtime/Math/BoundingBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ namespace spartan::math

for (uint32_t i = 0; i < point_count; i++)
{
m_max.x = helper::Max(m_max.x, points[i].x);
m_max.y = helper::Max(m_max.y, points[i].y);
m_max.z = helper::Max(m_max.z, points[i].z);
m_max.x = std::max(m_max.x, points[i].x);
m_max.y = std::max(m_max.y, points[i].y);
m_max.z = std::max(m_max.z, points[i].z);

m_min.x = helper::Min(m_min.x, points[i].x);
m_min.y = helper::Min(m_min.y, points[i].y);
m_min.z = helper::Min(m_min.z, points[i].z);
m_min.x = std::min(m_min.x, points[i].x);
m_min.y = std::min(m_min.y, points[i].y);
m_min.z = std::min(m_min.z, points[i].z);
}
}

Expand All @@ -64,13 +64,13 @@ namespace spartan::math

for (uint32_t i = 0; i < vertex_count; i++)
{
m_max.x = helper::Max(m_max.x, vertices[i].pos[0]);
m_max.y = helper::Max(m_max.y, vertices[i].pos[1]);
m_max.z = helper::Max(m_max.z, vertices[i].pos[2]);
m_max.x = std::max(m_max.x, vertices[i].pos[0]);
m_max.y = std::max(m_max.y, vertices[i].pos[1]);
m_max.z = std::max(m_max.z, vertices[i].pos[2]);

m_min.x = helper::Min(m_min.x, vertices[i].pos[0]);
m_min.y = helper::Min(m_min.y, vertices[i].pos[1]);
m_min.z = helper::Min(m_min.z, vertices[i].pos[2]);
m_min.x = std::min(m_min.x, vertices[i].pos[0]);
m_min.y = std::min(m_min.y, vertices[i].pos[1]);
m_min.z = std::min(m_min.z, vertices[i].pos[2]);
}
}

Expand Down Expand Up @@ -115,23 +115,23 @@ namespace spartan::math
const Vector3 extent_old = GetExtents();
const Vector3 extend_new = Vector3
(
helper::Abs(transform.m00) * extent_old.x + helper::Abs(transform.m10) * extent_old.y + helper::Abs(transform.m20) * extent_old.z,
helper::Abs(transform.m01) * extent_old.x + helper::Abs(transform.m11) * extent_old.y + helper::Abs(transform.m21) * extent_old.z,
helper::Abs(transform.m02) * extent_old.x + helper::Abs(transform.m12) * extent_old.y + helper::Abs(transform.m22) * extent_old.z
abs(transform.m00) * extent_old.x + abs(transform.m10) * extent_old.y + abs(transform.m20) * extent_old.z,
abs(transform.m01) * extent_old.x + abs(transform.m11) * extent_old.y + abs(transform.m21) * extent_old.z,
abs(transform.m02) * extent_old.x + abs(transform.m12) * extent_old.y + abs(transform.m22) * extent_old.z
);

return BoundingBox(center_new - extend_new, center_new + extend_new);
}

void BoundingBox::Merge(const BoundingBox& box)
{
m_min.x = helper::Min(m_min.x, box.m_min.x);
m_min.y = helper::Min(m_min.y, box.m_min.y);
m_min.z = helper::Min(m_min.z, box.m_min.z);
m_min.x = std::min(m_min.x, box.m_min.x);
m_min.y = std::min(m_min.y, box.m_min.y);
m_min.z = std::min(m_min.z, box.m_min.z);

m_max.x = helper::Max(m_max.x, box.m_max.x);
m_max.y = helper::Max(m_max.y, box.m_max.y);
m_max.z = helper::Max(m_max.z, box.m_max.z);
m_max.x = std::max(m_max.x, box.m_max.x);
m_max.y = std::max(m_max.y, box.m_max.y);
m_max.z = std::max(m_max.z, box.m_max.z);
}

bool BoundingBox::Contains(const Vector3& point) const
Expand Down
Loading

0 comments on commit 5ed8325

Please sign in to comment.