Sfoglia il codice sorgente

core: use templated min()/max()/clamp()

Daniele Bartolini 7 anni fa
parent
commit
6d312cdc72

+ 12 - 12
src/core/math/aabb.cpp

@@ -23,12 +23,12 @@ namespace aabb
 		{
 			point = (f32*)pts;
 
-			b.min.x = fmin(b.min.x, point[0]);
-			b.min.y = fmin(b.min.y, point[1]);
-			b.min.z = fmin(b.min.z, point[2]);
-			b.max.x = fmax(b.max.x, point[0]);
-			b.max.y = fmax(b.max.y, point[1]);
-			b.max.z = fmax(b.max.z, point[2]);
+			b.min.x = min(b.min.x, point[0]);
+			b.min.y = min(b.min.y, point[1]);
+			b.min.z = min(b.min.z, point[2]);
+			b.max.x = max(b.max.x, point[0]);
+			b.max.y = max(b.max.y, point[1]);
+			b.max.z = max(b.max.z, point[2]);
 		}
 	}
 
@@ -43,12 +43,12 @@ namespace aabb
 
 		for (u32 i = 1; i < num; ++i)
 		{
-			b.min.x = fmin(b.min.x, boxes[i].min.x);
-			b.min.y = fmin(b.min.y, boxes[i].min.y);
-			b.min.z = fmin(b.min.z, boxes[i].min.z);
-			b.max.x = fmax(b.max.x, boxes[i].max.x);
-			b.max.y = fmax(b.max.y, boxes[i].max.y);
-			b.max.z = fmax(b.max.z, boxes[i].max.z);
+			b.min.x = min(b.min.x, boxes[i].min.x);
+			b.min.y = min(b.min.y, boxes[i].min.y);
+			b.min.z = min(b.min.z, boxes[i].min.z);
+			b.max.x = max(b.max.x, boxes[i].max.x);
+			b.max.y = max(b.max.y, boxes[i].max.y);
+			b.max.z = max(b.max.z, boxes[i].max.z);
 		}
 	}
 

+ 1 - 1
src/core/math/intersection.cpp

@@ -210,7 +210,7 @@ f32 ray_mesh_intersection(const Vector3& from, const Vector3& dir, const Matrix4
 		if (t > FLOAT_EPSILON)
 		{
 			hit = true;
-			tmin = fmin(t, tmin);
+			tmin = min(t, tmin);
 		}
 	}
 

+ 0 - 15
src/core/math/math.cpp

@@ -16,21 +16,6 @@ bool fequal(f32 a, f32 b, f32 epsilon)
 		;
 }
 
-f32 fmin(f32 a, f32 b)
-{
-	return a < b ? a : b;
-}
-
-f32 fmax(f32 a, f32 b)
-{
-	return a < b ? b : a;
-}
-
-f32 fclamp(f32 val, f32 min, f32 max)
-{
-	return fmin(fmax(min, val), max);
-}
-
 f32 ffract(f32 a)
 {
 	return a - floorf(a);

+ 0 - 9
src/core/math/math.h

@@ -20,15 +20,6 @@ static const f32 FLOAT_EPSILON = 1.0e-7f;
 /// Returns whether @a a and @a b are equal according to @a epsilon.
 bool fequal(f32 a, f32 b, f32 epsilon = FLOAT_EPSILON);
 
-/// Returns the minimum of @a a and @a b.
-f32 fmin(f32 a, f32 b);
-
-/// Returns the maximum of @a a and @a b.
-f32 fmax(f32 a, f32 b);
-
-/// Clamps @a val to @a min and @a max.
-f32 fclamp(f32 val, f32 min, f32 max);
-
 /// Returns the fractional part of @a a.
 f32 ffract(f32 a);
 

+ 1 - 1
src/core/math/sphere.cpp

@@ -18,7 +18,7 @@ namespace sphere
 		{
 			const Vector3& pi = *(const Vector3*)pts;
 
-			rr = fmax(rr, length_squared(pi - s.c));
+			rr = max(rr, length_squared(pi - s.c));
 		}
 
 		s.r = fsqrt(rr);

+ 4 - 4
src/core/math/vector2.h

@@ -147,8 +147,8 @@ inline f32 angle(const Vector2& a, const Vector2& b)
 inline Vector2 max(const Vector2& a, const Vector2& b)
 {
 	Vector2 v;
-	v.x = fmax(a.x, b.x);
-	v.y = fmax(a.y, b.y);
+	v.x = max(a.x, b.x);
+	v.y = max(a.y, b.y);
 	return v;
 }
 
@@ -156,8 +156,8 @@ inline Vector2 max(const Vector2& a, const Vector2& b)
 inline Vector2 min(const Vector2& a, const Vector2& b)
 {
 	Vector2 v;
-	v.x = fmin(a.x, b.x);
-	v.y = fmin(a.y, b.y);
+	v.x = min(a.x, b.x);
+	v.y = min(a.y, b.y);
 	return v;
 }
 

+ 10 - 8
src/core/math/vector3.h

@@ -161,22 +161,24 @@ inline f32 angle(const Vector3& a, const Vector3& b)
 }
 
 /// Returns a vector that contains the largest value for each element from @a a and @a b.
-inline Vector3 max(const Vector3& a, const Vector3& b)
+template <>
+inline Vector3 max<Vector3>(const Vector3& a, const Vector3& b)
 {
 	Vector3 v;
-	v.x = fmax(a.x, b.x);
-	v.y = fmax(a.y, b.y);
-	v.z = fmax(a.z, b.z);
+	v.x = max(a.x, b.x);
+	v.y = max(a.y, b.y);
+	v.z = max(a.z, b.z);
 	return v;
 }
 
 /// Returns a vector that contains the smallest value for each element from @a a and @a b.
-inline Vector3 min(const Vector3& a, const Vector3& b)
+template <>
+inline Vector3 min<Vector3>(const Vector3& a, const Vector3& b)
 {
 	Vector3 v;
-	v.x = fmin(a.x, b.x);
-	v.y = fmin(a.y, b.y);
-	v.z = fmin(a.z, b.z);
+	v.x = min(a.x, b.x);
+	v.y = min(a.y, b.y);
+	v.z = min(a.z, b.z);
 	return v;
 }
 

+ 8 - 8
src/core/math/vector4.h

@@ -165,10 +165,10 @@ inline f32 angle(const Vector4& a, const Vector4& b)
 inline Vector4 max(const Vector4& a, const Vector4& b)
 {
 	Vector4 v;
-	v.x = fmax(a.x, b.x);
-	v.y = fmax(a.y, b.y);
-	v.z = fmax(a.z, b.z);
-	v.w = fmax(a.w, b.w);
+	v.x = max(a.x, b.x);
+	v.y = max(a.y, b.y);
+	v.z = max(a.z, b.z);
+	v.w = max(a.w, b.w);
 	return v;
 }
 
@@ -176,10 +176,10 @@ inline Vector4 max(const Vector4& a, const Vector4& b)
 inline Vector4 min(const Vector4& a, const Vector4& b)
 {
 	Vector4 v;
-	v.x = fmin(a.x, b.x);
-	v.y = fmin(a.y, b.y);
-	v.z = fmin(a.z, b.z);
-	v.w = fmin(a.w, b.w);
+	v.x = min(a.x, b.x);
+	v.y = min(a.y, b.y);
+	v.z = min(a.z, b.z);
+	v.w = min(a.w, b.w);
 	return v;
 }
 

+ 1 - 2
src/core/strings/fixed_string.h

@@ -6,7 +6,6 @@
 #pragma once
 
 #include "core/strings/string.h"
-#include <algorithm>
 
 namespace crown
 {
@@ -71,7 +70,7 @@ inline bool operator==(const FixedString& a, const FixedString& b)
 
 inline bool operator<(const FixedString& a, const FixedString& b)
 {
-	const u32 len = std::max(a._length, b._length);
+	const u32 len = max(a._length, b._length);
 	return strncmp(a._data, b._data, len) < 0;
 }
 

+ 18 - 0
src/core/types.h

@@ -42,6 +42,24 @@ inline void exchange(T& a, T& b)
 	b = c;
 }
 
+template <typename T>
+inline T min(const T& a, const T& b)
+{
+	return a < b ? a : b;
+}
+
+template <typename T>
+inline T max(const T& a, const T& b)
+{
+	return a > b ? a : b;
+}
+
+template <typename T>
+inline T clamp(T val, T mmin, T mmax)
+{
+	return min(max(mmin, val), mmax);
+}
+
 } // namespace crown
 
 #if defined(_MSC_VER)

+ 1 - 0
src/resource/data_compiler.cpp

@@ -38,6 +38,7 @@
 #include "resource/texture_resource.h"
 #include "resource/types.h"
 #include "resource/unit_resource.h"
+#include <algorithm>
 
 namespace { const crown::log_internal::System DATA_COMPILER = { "data_compiler" }; }
 

+ 10 - 11
tools-imgui/level_editor.cpp

@@ -237,38 +237,37 @@ struct SpriteImporter
 		ImGui::BeginGroup();
 		ImGui::LabelText("Resolution", "%d x %d", width, height);
 
-		// FIXME: replace fclamp
 		ImGui::InputInt("Cells H", &cells_h);
-		cells_h = (int)fclamp((f32)cells_h, 1.0f, 256.0f);
+		cells_h = clamp(cells_h, 1, 256);
 
 		ImGui::InputInt("Cells V", &cells_v);
-		cells_v = (int)fclamp((f32)cells_v, 1.0f, 256.0f);
+		cells_v = clamp(cells_v, 1, 256);
 
 		ImGui::Checkbox("Cell WH auto", &cell_wh_auto);
 		ImGui::InputInt("Cell W", &cell_w);
-		cell_w = (int)fclamp((f32)cell_w, 1.0f, 4096.0f);
+		cell_w = clamp(cell_w, 1, 4096);
 
 		ImGui::InputInt("Cell H", &cell_h);
-		cell_h = (int)fclamp((f32)cell_h, 1.0f, 4096.0f);
+		cell_h = clamp(cell_h, 1, 4096);
 
 		ImGui::InputInt("Offset X", &offset_x);
-		offset_x = (int)fclamp((f32)offset_x, 0.0f, 128.0f);
+		offset_x = clamp(offset_x, 0, 128);
 
 		ImGui::InputInt("Offset Y", &offset_y);
-		offset_y = (int)fclamp((f32)offset_y, 0.0f, 128.0f);
+		offset_y = clamp(offset_y, 0, 128);
 
 		ImGui::InputInt("Spacing X", &spacing_x);
-		spacing_x = (int)fclamp((f32)spacing_x, 0.0f, 128.0f);
+		spacing_x = clamp(spacing_x, 0, 128);
 
 		ImGui::InputInt("Spacing Y", &spacing_y);
-		spacing_y = (int)fclamp((f32)spacing_y, 0.0f, 128.0f);
+		spacing_y = clamp(spacing_y, 0, 128);
 
 		ImGui::Combo("Pivot", &pivot, pivot_names, Pivot::COUNT);
 		ImGui::InputInt("Layer", &layer);
-		layer = (int)fclamp((f32)layer, 0.0f, 7.0f);
+		layer = clamp(layer, 0, 7);
 
 		ImGui::InputInt("Depth", &depth);
-		depth = (int)fclamp((f32)depth, 0.0f, 9999.0f);
+		depth = clamp(depth, 0, 9999);
 
 		ImGui::EndGroup();
 	}