Moved most love.math functions out of the Math class, since they're pure functions.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2016-03-28 19:46:29 -03:00
parent 734789eb93
commit cc1b9bc619
6 changed files with 162 additions and 182 deletions
+90 -92
View File
@@ -30,7 +30,6 @@
#include "common/math.h"
#include "common/Vector.h"
#include "common/int.h"
#include "common/StringMap.h"
// Noise
#include "libraries/noise1234/noise1234.h"
@@ -46,6 +45,91 @@ namespace math
class BezierCurve;
enum EncodeFormat
{
ENCODE_BASE64,
ENCODE_HEX,
ENCODE_MAX_ENUM
};
/**
* Triangulate a simple polygon.
*
* @param polygon Polygon to triangulate. Must not intersect itself.
* @return List of triangles the polygon is composed of.
**/
std::vector<Triangle> triangulate(const std::vector<Vertex> &polygon);
/**
* Checks whether a polygon is convex.
*
* @param polygon Polygon to test.
* @return True if the polygon is convex, false otherwise.
**/
bool isConvex(const std::vector<Vertex> &polygon);
/**
* Converts a value from the sRGB (gamma) colorspace to linear RGB.
**/
float gammaToLinear(float c);
/**
* Converts a value from linear RGB to the sRGB (gamma) colorspace.
**/
float linearToGamma(float c);
/**
* Calculate noise for the specified coordinate(s).
*
* @return Noise value in the range of [0, 1].
**/
static float noise1(float x);
static float noise2(float x, float y);
static float noise3(float x, float y, float z);
static float noise4(float x, float y, float z, float w);
/**
* Compresses a block of memory using the given compression format.
*
* @param format The compression format to use.
* @param rawdata The data to compress.
* @param level The amount of compression to apply (between 0 and 9.)
* A value of -1 indicates the default amount of compression.
* Specific formats may not use every level.
* @return The newly compressed data.
**/
CompressedData *compress(Compressor::Format format, Data *rawdata, int level = -1);
CompressedData *compress(Compressor::Format format, const char *rawbytes, size_t rawsize, int level = -1);
/**
* Decompresses existing compressed data into raw bytes.
*
* @param[in] data The compressed data to decompress.
* @param[out] decompressedsize The size in bytes of the decompressed data.
* @return The newly decompressed data (allocated with new[]).
**/
char *decompress(CompressedData *data, size_t &decompressedsize);
/**
* Decompresses existing compressed data into raw bytes.
*
* @param[in] format The compression format the data is in.
* @param[in] cbytes The compressed data to decompress.
* @param[in] compressedsize The size in bytes of the compressed data.
* @param[in,out] rawsize On input, the size in bytes of the original
* uncompressed data, or 0 if unknown. On return, the size in
* bytes of the newly decompressed data.
* @return The newly decompressed data (allocated with new[]).
**/
char *decompress(Compressor::Format format, const char *cbytes, size_t compressedsize, size_t &rawsize);
char *encode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen, size_t linelen = 0);
char *decode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen);
bool getConstant(const char *in, EncodeFormat &out);
bool getConstant(EncodeFormat in, const char *&out);
class Math : public Module
{
private:
@@ -54,13 +138,6 @@ private:
public:
enum EncodeFormat
{
ENCODE_BASE64,
ENCODE_HEX,
ENCODE_MAX_ENUM
};
virtual ~Math();
RandomGenerator *getRandomGenerator()
@@ -89,100 +166,21 @@ public:
return "love.math";
}
/**
* Triangulate a simple polygon.
*
* @param polygon Polygon to triangulate. Must not intersect itself.
* @return List of triangles the polygon is composed of.
**/
std::vector<Triangle> triangulate(const std::vector<Vertex> &polygon);
/**
* Checks whether a polygon is convex.
*
* @param polygon Polygon to test.
* @return True if the polygon is convex, false otherwise.
**/
bool isConvex(const std::vector<Vertex> &polygon);
/**
* Converts a value from the sRGB (gamma) colorspace to linear RGB.
**/
float gammaToLinear(float c) const;
/**
* Converts a value from linear RGB to the sRGB (gamma) colorspace.
**/
float linearToGamma(float c) const;
/**
* Calculate noise for the specified coordinate(s).
*
* @return Noise value in the range of [0, 1].
**/
float noise(float x) const;
float noise(float x, float y) const;
float noise(float x, float y, float z) const;
float noise(float x, float y, float z, float w) const;
/**
* Compresses a block of memory using the given compression format.
*
* @param format The compression format to use.
* @param rawdata The data to compress.
* @param level The amount of compression to apply (between 0 and 9.)
* A value of -1 indicates the default amount of compression.
* Specific formats may not use every level.
* @return The newly compressed data.
**/
CompressedData *compress(Compressor::Format format, Data *rawdata, int level = -1);
CompressedData *compress(Compressor::Format format, const char *rawbytes, size_t rawsize, int level = -1);
/**
* Decompresses existing compressed data into raw bytes.
*
* @param[in] data The compressed data to decompress.
* @param[out] decompressedsize The size in bytes of the decompressed data.
* @return The newly decompressed data (allocated with new[]).
**/
char *decompress(CompressedData *data, size_t &decompressedsize);
/**
* Decompresses existing compressed data into raw bytes.
*
* @param[in] format The compression format the data is in.
* @param[in] cbytes The compressed data to decompress.
* @param[in] compressedsize The size in bytes of the compressed data.
* @param[in,out] rawsize On input, the size in bytes of the original
* uncompressed data, or 0 if unknown. On return, the size in
* bytes of the newly decompressed data.
* @return The newly decompressed data (allocated with new[]).
**/
char *decompress(Compressor::Format format, const char *cbytes, size_t compressedsize, size_t &rawsize);
char *encode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen, size_t linelen = 0);
char *decode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen);
static bool getConstant(const char *in, EncodeFormat &out);
static bool getConstant(EncodeFormat in, const char *&out);
static Math instance;
private:
Math();
static StringMap<EncodeFormat, ENCODE_MAX_ENUM>::Entry encoderEntries[];
static StringMap<EncodeFormat, ENCODE_MAX_ENUM> encoders;
}; // Math
inline float Math::noise(float x) const
static inline float noise1(float x)
{
return SimplexNoise1234::noise(x) * 0.5f + 0.5f;
}
inline float Math::noise(float x, float y) const
static inline float noise2(float x, float y)
{
return SimplexNoise1234::noise(x, y) * 0.5f + 0.5f;
}
@@ -190,12 +188,12 @@ inline float Math::noise(float x, float y) const
// Perlin noise is used instead of Simplex noise in the 3D and 4D cases to avoid
// patent issues.
inline float Math::noise(float x, float y, float z) const
static inline float noise3(float x, float y, float z)
{
return Noise1234::noise(x, y, z) * 0.5f + 0.5f;
}
inline float Math::noise(float x, float y, float z, float w) const
static inline float noise4(float x, float y, float z, float w)
{
return Noise1234::noise(x, y, z, w) * 0.5f + 0.5f;
}