Skip to content

Commit

Permalink
math module
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Apr 24, 2024
1 parent 060d4d1 commit 931d9cf
Show file tree
Hide file tree
Showing 27 changed files with 2,882 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: AfterColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 100
ColumnLimit: 110
CommentPragmas: "^ IWYU pragma:"
QualifierAlignment: Leave
CompactNamespaces: false
Expand Down
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ add_library(wuff
)
target_link_libraries(${PROJECT_NAME} PRIVATE wuff)

# noise1234
add_library(noise1234
libraries/noise1234/noise1234.cpp
libraries/noise1234/noise1234.h
libraries/noise1234/simplexnoise1234.cpp
libraries/noise1234/simplexnoise1234.h
)
target_link_libraries(${PROJECT_NAME} PRIVATE noise1234)

if (NINTENDO_WIIU)
target_link_libraries(${PROJECT_NAME} PRIVATE vorbisfile vorbis)
else()
Expand Down Expand Up @@ -335,6 +344,14 @@ source/modules/joystick/wrap_Joystick.cpp
source/modules/joystick/wrap_JoystickModule.cpp
source/modules/keyboard/wrap_Keyboard.cpp
source/modules/love/love.cpp
source/modules/math/BezierCurve.cpp
source/modules/math/MathModule.cpp
source/modules/math/RandomGenerator.cpp
source/modules/math/Transform.cpp
source/modules/math/wrap_BezierCurve.cpp
source/modules/math/wrap_MathModule.cpp
source/modules/math/wrap_RandomGenerator.cpp
source/modules/math/wrap_Transform.cpp
source/modules/sensor/Sensor.cpp
source/modules/sensor/wrap_Sensor.cpp
source/modules/sound/Decoder.cpp
Expand Down
10 changes: 8 additions & 2 deletions include/common/luax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,14 @@ namespace love

int luax_convobj(lua_State* L, int index, const char* module, const char* function);

int luax_convobj(lua_State* L, std::span<int> indices, const char* module,
const char* function);
int luax_convobj(lua_State* L, std::span<int> indices, const char* module, const char* function);

void luax_gettypemetatable(lua_State* L, const Type& type);

void luax_runwrapper(lua_State* L, const char* filedata, size_t datalen, const char* filename,
const Type& type);

lua_Number luax_checknumberclamped01(lua_State* L, int index);

// #endregion

Expand Down
54 changes: 54 additions & 0 deletions include/modules/math/BezierCurve.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include "common/Object.hpp"
#include "common/Vector.hpp"

#include <vector>

namespace love
{
class BezierCurve : public Object
{
public:
static Type type;

BezierCurve(const std::vector<Vector2>& controlPoints);

size_t getDegree() const
{
return this->controlPoints.size() - 1;
}

BezierCurve getDerivative() const;

const Vector2& getControlPoint(int i) const;

void setControlPoint(int i, const Vector2& point);

void insertControlPoint(const Vector2& point, int pos = -1);

void removeControlPoint(int i);

size_t getControlPointCount() const
{
return this->controlPoints.size();
}

void translate(const Vector2& offset);

void rotate(double phi, const Vector2& center);

void scale(double scale, const Vector2& center);

Vector2 evaluate(double time) const;

BezierCurve* getSegment(double start, double end) const;

std::vector<Vector2> render(int accuracy = 4) const;

std::vector<Vector2> renderSegment(double start, double end, int accuracy = 4) const;

private:
std::vector<Vector2> controlPoints;
};
} // namespace love
101 changes: 101 additions & 0 deletions include/modules/math/MathModule.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#pragma once

#include "modules/math/RandomGenerator.hpp"

#include "common/Module.hpp"
#include "common/StrongRef.hpp"
#include "common/Vector.hpp"
#include "common/int.hpp"
#include "common/math.hpp"

#include "noise1234.h"
#include "simplexnoise1234.h"

#include <vector>

namespace love
{
class BezierCurve;
class Transform;

struct Triangle
{
Triangle(const Vector2& x, const Vector2& y, const Vector2& z) : a(x), b(y), c(z)
{}

Vector2 a, b, c;
};

std::vector<Triangle> triangulate(const std::vector<love::Vector2>& polygon);

bool isConvex(const std::vector<Vector2>& polygon);

float gammaToLinear(float c);

float linearToGamma(float c);

static inline double simplexNoise1(double x)
{
return SimplexNoise1234::noise(x) * 0.5 + 0.5;
}

static inline double simplexNoise2(double x, double y)
{
return SimplexNoise1234::noise(x, y) * 0.5 + 0.5;
}

static inline double simplexNoise3(double x, double y, double z)
{
return SimplexNoise1234::noise(x, y, z) * 0.5 + 0.5;
}

static inline double simplexNoise4(double x, double y, double z, double w)
{
return SimplexNoise1234::noise(x, y, z, w) * 0.5 + 0.5;
}

static inline double perlinNoise1(double x)
{
return Noise1234::noise(x) * 0.5 + 0.5;
}

static inline double perlinNoise2(double x, double y)
{
return Noise1234::noise(x, y) * 0.5 + 0.5;
}

static inline double perlinNoise3(double x, double y, double z)
{
return Noise1234::noise(x, y, z) * 0.5 + 0.5;
}

static inline double perlinNoise4(double x, double y, double z, double w)
{
return Noise1234::noise(x, y, z, w) * 0.5 + 0.5;
}

class Math : public Module
{
public:
Math();

virtual ~Math();

RandomGenerator* getRandomGenerator()
{
return this->random.get();
}

RandomGenerator* newRandomGenerator() const;

BezierCurve* newBezierCurve(const std::vector<Vector2>& points) const;

Transform* newTransform() const;

Transform* newTransform(float x, float y, float angle, float sx, float sy, float ox, float oy,
float kx, float ky) const;

private:
StrongRef<RandomGenerator> random;
};
} // namespace love
84 changes: 84 additions & 0 deletions include/modules/math/RandomGenerator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

#include "common/Exception.hpp"
#include "common/Object.hpp"
#include "common/int.hpp"
#include "common/math.hpp"

#include <limits>
#include <string>

namespace love
{
class RandomGenerator : public Object
{
public:
static Type type;

union Seed
{
uint64_t b64;
struct
{
#if defined(LOVE_BIG_ENDIAN)
uint32_t high;
uint32_t low;
#else
uint32_t low;
uint32_t high;
#endif
} b32;
};

RandomGenerator();

virtual ~RandomGenerator()
{}

uint64_t rand();

inline double random()
{
uint64_t value = rand();

union
{
uint64_t i;
double d;
} u;

u.i = ((0x3FFULL) << 52) | (value >> 12);
return u.d - 1.0;
}

inline double random(double max)
{
return random() * max;
}

inline double random(double min, double max)
{
return random() * (max - min) + min;
}

double randomNormal(double stddev);

void setSeed(Seed seed);

Seed getSeed() const;

void setState(const std::string& state);

std::string getState() const;

private:
static constexpr auto DEFAULT_LOW = 0xCBBF7A44;
static constexpr auto DEFAULT_HIGH = 0x0139408D;

static constexpr auto MULTIPLIER = 2685821657736338717ULL;

Seed seed;
Seed state;
double lastRandomNormal;
};
} // namespace love
80 changes: 80 additions & 0 deletions include/modules/math/Transform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include "common/Map.hpp"
#include "common/Matrix.hpp"
#include "common/Object.hpp"
#include "common/Vector.hpp"

namespace love
{
class Transform : public Object
{
public:
enum MatrixLayout
{
MATRIX_ROW_MAJOR,
MATRIX_COLUMN_MAJOR,
MATRIX_MAX_ENUM
};

static Type type;

Transform();

Transform(const Matrix4& m);

Transform(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky);

virtual ~Transform();

Transform* clone();

Transform* inverse();

void apply(Transform* other);

void translate(float x, float y);

void rotate(float angle);

void scale(float x, float y);

void shear(float x, float y);

void reset();

void setTransformation(float x, float y, float a, float sx, float sy, float ox, float oy, float kx,
float ky);

Vector2 transformPoint(Vector2 point) const;

Vector2 inverseTransformPoint(Vector2 point);

const Matrix4& getMatrix() const;

void setMatrix(const Matrix4& m);

// clang-format off
STRINGMAP_DECLARE(MatrixLayouts, MatrixLayout,
{ "row", MATRIX_ROW_MAJOR },
{ "column", MATRIX_COLUMN_MAJOR }
);
// clang-format on

private:
inline const Matrix4& getInverseMatrix()
{
if (this->inverseDirty)
{
this->inverseMatrix = matrix.inverse();
this->inverseDirty = false;
}

return this->inverseMatrix;
}

Matrix4 matrix;
bool inverseDirty;
Matrix4 inverseMatrix;
};
} // namespace love
Loading

0 comments on commit 931d9cf

Please sign in to comment.