Daniele Bartolini 10 سال پیش
والد
کامیت
ad51229443
6فایلهای تغییر یافته به همراه91 افزوده شده و 62 حذف شده
  1. 2 2
      src/core/math/math_utils.h
  2. 3 3
      src/core/math/sphere.h
  3. 15 8
      src/core/math/vector2.h
  4. 15 8
      src/core/math/vector3.h
  5. 15 8
      src/core/math/vector4.h
  6. 41 33
      src/lua/lua_math.cpp

+ 2 - 2
src/core/math/math_utils.h

@@ -67,9 +67,9 @@ inline bool is_pow_2(uint32_t x)
 }
 
 /// Returns the linear interpolated value between @a p0 and @a p1 at time @a t
-inline float linear(const float p0, const float p1, float t)
+inline float lerp(const float p0, const float p1, float t)
 {
-	return p0 + t * (p1 - p0);
+	return (1.0f - t) * p0 + t * p1;
 }
 
 /// Returns the cosine interpolated value between @a p0 and @a p1 at time @a t

+ 3 - 3
src/core/math/sphere.h

@@ -49,7 +49,7 @@ namespace sphere
 		{
 			const Vector3* p = (const Vector3*)points;
 
-			const float dist = squared_length(*p - s.c);
+			const float dist = length_squared(*p - s.c);
 			if (dist > s.r*s.r)
 				s.r = sqrtf(dist);
 
@@ -66,7 +66,7 @@ namespace sphere
 	{
 		for (uint32_t i = 0; i < num; ++i)
 		{
-			const float dist = squared_length(spheres[i].c - s.c);
+			const float dist = length_squared(spheres[i].c - s.c);
 
 			if (dist < (spheres[i].r + s.r) * (spheres[i].r + s.r))
 			{
@@ -78,7 +78,7 @@ namespace sphere
 
 	inline bool contains_point(const Sphere& s, const Vector3& p)
 	{
-		float dist = squared_length(p - s.c);
+		float dist = length_squared(p - s.c);
 		return dist < s.r*s.r;
 	}
 } // namespace sphere

+ 15 - 8
src/core/math/vector2.h

@@ -108,28 +108,29 @@ inline float dot(const Vector2& a, const Vector2& b)
 	return a.x * b.x + a.y * b.y;
 }
 
-/// Returns the lenght of @a a.
-inline float length(const Vector2& a)
+/// Returns the squared length of @a a.
+inline float length_squared(const Vector2& a)
 {
-	return sqrtf(a.x * a.x + a.y * a.y);
+	return dot(a, a);
 }
 
-/// Returns the squared length of @a a.
-inline float squared_length(const Vector2& a)
+/// Returns the length of @a a.
+inline float length(const Vector2& a)
 {
-	return a.x * a.x + a.y * a.y;
+	return sqrtf(length_squared(a));
 }
 
 /// Normalizes @a a and returns the result.
 inline Vector2 normalize(Vector2& a)
 {
-	float inv_len = 1.0f / length(a);
+	const float len = length(a);
+	const float inv_len = 1.0f / len;
 	a.x *= inv_len;
 	a.y *= inv_len;
 	return a;
 }
 
-/// Sets the lenght of @a a to @a len.
+/// Sets the length of @a a to @a len.
 inline void set_length(Vector2& a, float len)
 {
 	normalize(a);
@@ -137,6 +138,12 @@ inline void set_length(Vector2& a, float len)
 	a.y *= len;
 }
 
+/// Returns the squared distance between the points @a a and @a b.
+inline float distance_squared(const Vector2& a, const Vector2& b)
+{
+	return length_squared(b - a);
+}
+
 /// Returns the distance between the points @a a and @a b.
 inline float distance(const Vector2& a, const Vector2& b)
 {

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

@@ -120,29 +120,30 @@ inline Vector3 cross(const Vector3& a, const Vector3& b)
 	return vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
 }
 
-/// Returns the lenght of @a a.
-inline float length(const Vector3& a)
+/// Returns the squared length of @a a.
+inline float length_squared(const Vector3& a)
 {
-	return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
+	return dot(a, a);
 }
 
-/// Returns the squared length of @a a.
-inline float squared_length(const Vector3& a)
+/// Returns the length of @a a.
+inline float length(const Vector3& a)
 {
-	return a.x * a.x + a.y * a.y + a.z * a.z;
+	return sqrtf(length_squared(a));
 }
 
 /// Normalizes @a a and returns the result.
 inline Vector3 normalize(Vector3& a)
 {
-	float inv_len = 1.0f / length(a);
+	const float len = length(a);
+	const float inv_len = 1.0f / len;
 	a.x *= inv_len;
 	a.y *= inv_len;
 	a.z *= inv_len;
 	return a;
 }
 
-/// Sets the lenght of @a a to @a len.
+/// Sets the length of @a a to @a len.
 inline void set_length(Vector3& a, float len)
 {
 	normalize(a);
@@ -151,6 +152,12 @@ inline void set_length(Vector3& a, float len)
 	a.z *= len;
 }
 
+/// Returns the squared distance between the points @a a and @a b.
+inline float distance_squared(const Vector3& a, const Vector3& b)
+{
+	return length_squared(b - a);
+}
+
 /// Returns the distance between the points @a a and @a b.
 inline float distance(const Vector3& a, const Vector3& b)
 {

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

@@ -136,22 +136,23 @@ inline float dot(const Vector4& a, const Vector4& b)
 	return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
 }
 
-/// Returns the lenght of @a a.
-inline float length(const Vector4& a)
+/// Returns the squared length of @a a.
+inline float length_squared(const Vector4& a)
 {
-	return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w);
+	return dot(a, a);
 }
 
-/// Returns the squared length of @a a.
-inline float squared_length(const Vector4& a)
+/// Returns the length of @a a.
+inline float length(const Vector4& a)
 {
-	return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
+	return sqrtf(length_squared(a));
 }
 
 /// Normalizes @a a and returns the result.
 inline Vector4 normalize(Vector4& a)
 {
-	float inv_len = 1.0f / length(a);
+	const float len = length(a);
+	const float inv_len = 1.0f / len;
 	a.x *= inv_len;
 	a.y *= inv_len;
 	a.z *= inv_len;
@@ -159,7 +160,7 @@ inline Vector4 normalize(Vector4& a)
 	return a;
 }
 
-/// Sets the lenght of @a a to @a len.
+/// Sets the length of @a a to @a len.
 inline void set_length(Vector4& a, float len)
 {
 	normalize(a);
@@ -169,6 +170,12 @@ inline void set_length(Vector4& a, float len)
 	a.w *= len;
 }
 
+/// Returns the squared distance between the points @a a and @a b.
+inline float distance_squared(const Vector4& a, const Vector4& b)
+{
+	return length_squared(b - a);
+}
+
 /// Returns the distance between the points @a a and @a b.
 inline float distance(const Vector4& a, const Vector4& b)
 {

+ 41 - 33
src/lua/lua_math.cpp

@@ -192,10 +192,10 @@ static int vector3_length(lua_State* L)
 	return 1;
 }
 
-static int vector3_squared_length(lua_State* L)
+static int vector3_length_squared(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_float(squared_length(stack.get_vector3(1)));
+	stack.push_float(length_squared(stack.get_vector3(1)));
 	return 1;
 }
 
@@ -220,6 +220,13 @@ static int vector3_distance(lua_State* L)
 	return 1;
 }
 
+static int vector3_distance_squared(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_float(distance_squared(stack.get_vector3(1), stack.get_vector3(2)));
+	return 1;
+}
+
 static int vector3_angle(lua_State* L)
 {
 	LuaStack stack(L);
@@ -888,37 +895,38 @@ void load_math(LuaEnvironment& env)
 	env.load_module_function("Math", "ray_sphere_intersection", math_ray_sphere_intersection);
 	env.load_module_function("Math", "ray_obb_intersection",    math_ray_obb_intersection);
 
-	env.load_module_function("Vector3", "new",            vector3_new);
-	env.load_module_function("Vector3", "x",              vector3_x);
-	env.load_module_function("Vector3", "y",              vector3_y);
-	env.load_module_function("Vector3", "z",              vector3_z);
-	env.load_module_function("Vector3", "set_x",          vector3_set_x);
-	env.load_module_function("Vector3", "set_y",          vector3_set_y);
-	env.load_module_function("Vector3", "set_z",          vector3_set_z);
-	env.load_module_function("Vector3", "values",         vector3_values);
-	env.load_module_function("Vector3", "add",            vector3_add);
-	env.load_module_function("Vector3", "subtract",       vector3_subtract);
-	env.load_module_function("Vector3", "multiply",       vector3_multiply);
-	env.load_module_function("Vector3", "divide",         vector3_divide);
-	env.load_module_function("Vector3", "dot",            vector3_dot);
-	env.load_module_function("Vector3", "cross",          vector3_cross);
-	env.load_module_function("Vector3", "equal",          vector3_equal);
-	env.load_module_function("Vector3", "length",         vector3_length);
-	env.load_module_function("Vector3", "squared_length", vector3_squared_length);
-	env.load_module_function("Vector3", "set_length",     vector3_set_length);
-	env.load_module_function("Vector3", "normalize",      vector3_normalize);
-	env.load_module_function("Vector3", "distance",       vector3_distance);
-	env.load_module_function("Vector3", "angle",          vector3_angle);
-	env.load_module_function("Vector3", "max",            vector3_max);
-	env.load_module_function("Vector3", "min",            vector3_min);
-	env.load_module_function("Vector3", "forward",        vector3_forward);
-	env.load_module_function("Vector3", "backward",       vector3_backward);
-	env.load_module_function("Vector3", "left",           vector3_left);
-	env.load_module_function("Vector3", "right",          vector3_right);
-	env.load_module_function("Vector3", "up",             vector3_up);
-	env.load_module_function("Vector3", "down",           vector3_down);
-	env.load_module_function("Vector3", "zero",           vector3_zero);
-	env.load_module_function("Vector3", "to_string",      vector3_to_string);
+	env.load_module_function("Vector3", "new",              vector3_new);
+	env.load_module_function("Vector3", "x",                vector3_x);
+	env.load_module_function("Vector3", "y",                vector3_y);
+	env.load_module_function("Vector3", "z",                vector3_z);
+	env.load_module_function("Vector3", "set_x",            vector3_set_x);
+	env.load_module_function("Vector3", "set_y",            vector3_set_y);
+	env.load_module_function("Vector3", "set_z",            vector3_set_z);
+	env.load_module_function("Vector3", "values",           vector3_values);
+	env.load_module_function("Vector3", "add",              vector3_add);
+	env.load_module_function("Vector3", "subtract",         vector3_subtract);
+	env.load_module_function("Vector3", "multiply",         vector3_multiply);
+	env.load_module_function("Vector3", "divide",           vector3_divide);
+	env.load_module_function("Vector3", "dot",              vector3_dot);
+	env.load_module_function("Vector3", "cross",            vector3_cross);
+	env.load_module_function("Vector3", "equal",            vector3_equal);
+	env.load_module_function("Vector3", "length",           vector3_length);
+	env.load_module_function("Vector3", "length_squared",   vector3_length_squared);
+	env.load_module_function("Vector3", "set_length",       vector3_set_length);
+	env.load_module_function("Vector3", "normalize",        vector3_normalize);
+	env.load_module_function("Vector3", "distance",         vector3_distance);
+	env.load_module_function("Vector3", "distance_squared", vector3_distance_squared);
+	env.load_module_function("Vector3", "angle",            vector3_angle);
+	env.load_module_function("Vector3", "max",              vector3_max);
+	env.load_module_function("Vector3", "min",              vector3_min);
+	env.load_module_function("Vector3", "forward",          vector3_forward);
+	env.load_module_function("Vector3", "backward",         vector3_backward);
+	env.load_module_function("Vector3", "left",             vector3_left);
+	env.load_module_function("Vector3", "right",            vector3_right);
+	env.load_module_function("Vector3", "up",               vector3_up);
+	env.load_module_function("Vector3", "down",             vector3_down);
+	env.load_module_function("Vector3", "zero",             vector3_zero);
+	env.load_module_function("Vector3", "to_string",        vector3_to_string);
 
 	env.load_module_constructor("Vector3", vector3_ctor);