From b3beb74d84c512e8cf11b5a2ad2733628002cf84 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 9 May 2017 09:15:52 +1000 Subject: [PATCH] Port Vector3I to C --- src/Client/Client.vcxproj | 2 + src/Client/Client.vcxproj.filters | 6 +++ src/Client/ExtMath.c | 6 +++ src/Client/ExtMath.h | 6 +++ src/Client/Vector3I.c | 59 +++++++++++++++++++++ src/Client/Vector3I.h | 87 +++++++------------------------ 6 files changed, 99 insertions(+), 67 deletions(-) create mode 100644 src/Client/ExtMath.c create mode 100644 src/Client/Vector3I.c diff --git a/src/Client/Client.vcxproj b/src/Client/Client.vcxproj index 4cf751d86..5c5ce3631 100644 --- a/src/Client/Client.vcxproj +++ b/src/Client/Client.vcxproj @@ -203,6 +203,7 @@ + @@ -212,6 +213,7 @@ + diff --git a/src/Client/Client.vcxproj.filters b/src/Client/Client.vcxproj.filters index 0cf6eb81a..bff9b4840 100644 --- a/src/Client/Client.vcxproj.filters +++ b/src/Client/Client.vcxproj.filters @@ -200,5 +200,11 @@ Source Files\Graphics + + Source Files\Math + + + Source Files\Math + \ No newline at end of file diff --git a/src/Client/ExtMath.c b/src/Client/ExtMath.c new file mode 100644 index 000000000..ea80380bd --- /dev/null +++ b/src/Client/ExtMath.c @@ -0,0 +1,6 @@ +#include "ExtMath.h" + +Int32 Math_Floor(Real32 value) { + Int32 valueI = (Int32)value; + return value < valueI ? valueI - 1 : valueI; +} \ No newline at end of file diff --git a/src/Client/ExtMath.h b/src/Client/ExtMath.h index b7718d48e..55e5b1e2e 100644 --- a/src/Client/ExtMath.h +++ b/src/Client/ExtMath.h @@ -1,6 +1,10 @@ #ifndef CS_MATH_H #define CS_MATH_H #include +#include "Typedefs.h" +/* Simple math functions. + Copyright 2017 ClassicalSharp | Licensed under BSD-3 +*/ #define MATH_PI 3.1415926535897931f @@ -16,4 +20,6 @@ #define Math_Sqrt(x) sqrtf(x) +/* Integer floor of a floating-point value. */ +Int32 Math_Floor(Real32 value); #endif \ No newline at end of file diff --git a/src/Client/Vector3I.c b/src/Client/Vector3I.c new file mode 100644 index 000000000..5409b917c --- /dev/null +++ b/src/Client/Vector3I.c @@ -0,0 +1,59 @@ +#include "Vector3I.h" +#include "Funcs.h" +#include "ExtMath.h" + +Vector3I Vector3I_Create1(Int32 value) { + Vector3I v; v.X = value; v.Y = value; v.Z = value; return v; +} + +Vector3I Vector3I_Create3(Int32 x, Int32 y, Int32 z) { + Vector3I v; v.X = x; v.Y = y; v.Z = z; return v; +} + + +void Vector3I_Add(Vector3I* a, Vector3I* b, Vector3I* result) { + result->X = a->X + b->X; + result->Y = a->Y + b->Y; + result->Z = a->Z + b->Z; +} + +void Vector3I_Subtract(Vector3I* a, Vector3I* b, Vector3I* result) { + result->X = a->X - b->X; + result->Y = a->Y - b->Y; + result->Z = a->Z - b->Z; +} + +void Vector3I_Multiply1(Vector3I* a, Int32 scale, Vector3I* result) { + result->X = a->X * scale; + result->Y = a->Y * scale; + result->Z = a->Z * scale; +} + +void Vector3I_Negate(Vector3I* a, Vector3I* result) { + result->X = -a->X; + result->Y = -a->Y; + result->Z = -a->Z; +} + + +bool Vector3I_Equals(Vector3I* a, Vector3I* b) { + return a->X == b->X && a->Y == b->Y && a->Z == b->Z; +} + +void Vector3I_Floor(Vector3* a, Vector3I* result) { + result->X = Math_Floor(a->X); + result->Y = Math_Floor(a->Y); + result->Z = Math_Floor(a->Z); +} + +void Vector3I_Min(Vector3I* a, Vector3I* b, Vector3I* result) { + result->X = min(a->X, b->X); + result->Y = min(a->Y, b->Y); + result->Z = min(a->Z, b->Z); +} + +void Vector3I_Max(Vector3I* a, Vector3I* b, Vector3I* result) { + result->X = max(a->X, b->X); + result->Y = max(a->Y, b->Y); + result->Z = max(a->Z, b->Z); +} \ No newline at end of file diff --git a/src/Client/Vector3I.h b/src/Client/Vector3I.h index 31bded5e9..99c68b13e 100644 --- a/src/Client/Vector3I.h +++ b/src/Client/Vector3I.h @@ -1,6 +1,7 @@ #ifndef CS_VECTOR3I_H #define CS_VECTOR3I_H #include "Typedefs.h" +#include "Vectors.h" /* Represents 3 dimensional integer vector. Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ @@ -13,12 +14,6 @@ Vector3I Vector3I_Create1(Int32 value); /* Constructs a 3D vector from the given coordinates. */ Vector3I Vector3I_Create3(Int32 x, Int32 y, Int32 z); -/* Returns the length of the given vector. */ -Real32 Vector3I_Length(Vector3I* v); - -/* Returns the squared length of the given vector. */ -Int32 Vector3I_LengthSquared(Vector3I* v); - #define Vector3I_UnitX Vector3I_Create3(1, 0, 0) #define Vector3I_UnitY Vector3I_Create3(0, 1, 0) @@ -28,70 +23,28 @@ Int32 Vector3I_LengthSquared(Vector3I* v); #define Vector3I_MinusOne Vector3I_Create3(-1, -1, -1) +/* Adds a and b. */ +void Vector3I_Add(Vector3I* a, Vector3I* b, Vector3I* result); + +/* Subtracts b from a. */ +void Vector3I_Subtract(Vector3I* a, Vector3I* b, Vector3I* result); + +/* Multiplies all components of a by scale. */ +void Vector3I_Multiply1(Vector3I* a, Int32 scale, Vector3I* result); + +/* Negates components of a. */ +void Vector3I_Negate(Vector3I* a, Vector3I* result); - public Vector3 Normalise() { - float len = Length; - return new Vector3(X / len, Y / len, Z / len); - } +/* Returns whether the two vectors are exact same on all axes. */ +bool Vector3I_Equals(Vector3I* a, Vector3I* b); - public static Vector3I operator * (Vector3I left, int right) { - return new Vector3I(left.X * right, left.Y * right, left.Z * right); - } +/* Returns a vector such that each component is floor of input floatin-point component.*/ +void Vector3I_Floor(Vector3* a, Vector3I* result); - public static Vector3I operator + (Vector3I left, Vector3I right) { - return new Vector3I(left.X + right.X, left.Y + right.Y, left.Z + right.Z); - } +/* Returns a vector such that each component is minimum of corresponding a and b component.*/ +void Vector3I_Min(Vector3I* a, Vector3I* b, Vector3I* result); - public static Vector3I operator - (Vector3I left, Vector3I right) { - return new Vector3I(left.X - right.X, left.Y - right.Y, left.Z - right.Z); - } - - public static Vector3I operator - (Vector3I left) { - return new Vector3I(-left.X, -left.Y, -left.Z); - } - - public static explicit operator Vector3I(Vector3 value) { - return Truncate(value); - } - - public static explicit operator Vector3(Vector3I value) { - return new Vector3(value.X, value.Y, value.Z); - } - - public bool Equals(Vector3I other) { - return X == other.X && Y == other.Y && Z == other.Z; - } - - public static Vector3I Truncate(Vector3 vector) { - int x = (int)vector.X; - int y = (int)vector.Y; - int z = (int)vector.Z; - return new Vector3I(x, y, z); - } - - public static Vector3I Floor(Vector3 value) { - return new Vector3I(Utils.Floor(value.X), Utils.Floor(value.Y), Utils.Floor(value.Z)); - } - - public static Vector3I Floor(float x, float y, float z) { - return new Vector3I(Utils.Floor(x), Utils.Floor(y), Utils.Floor(z)); - } - - public static Vector3I Min(Vector3I p1, Vector3I p2) { - return new Vector3I(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y), Math.Min(p1.Z, p2.Z)); - } - - public static Vector3I Min(int x1, int y1, int z1, int x2, int y2, int z2) { - return new Vector3I(Math.Min(x1, x2), Math.Min(y1, y2), Math.Min(z1, z2)); - } - - public static Vector3I Max(Vector3I p1, Vector3I p2) { - return new Vector3I(Math.Max(p1.X, p2.X), Math.Max(p1.Y, p2.Y), Math.Max(p1.Z, p2.Z)); - } - - public static Vector3I Max(int x1, int y1, int z1, int x2, int y2, int z2) { - return new Vector3I(Math.Max(x1, x2), Math.Max(y1, y2), Math.Max(z1, z2)); - } - } +/* Returns a vector such that each component is maximum of corresponding a and b component.*/ +void Vector3I_Max(Vector3I* a, Vector3I* b, Vector3I* result); #endif \ No newline at end of file