diff options
Diffstat (limited to 'src/Math')
| -rw-r--r-- | src/Math/Grid.cpp | 36 | ||||
| -rw-r--r-- | src/Math/Grid.hpp | 6 | ||||
| -rw-r--r-- | src/Math/Perlin.cpp | 8 |
3 files changed, 31 insertions, 19 deletions
diff --git a/src/Math/Grid.cpp b/src/Math/Grid.cpp index 422cf87..de5fa69 100644 --- a/src/Math/Grid.cpp +++ b/src/Math/Grid.cpp @@ -1,12 +1,18 @@ #include "Grid.hpp" +#include "../Common/Lambda.hpp" + namespace Math { -GridCellBoundaries grid_cell_for_point(Vector<2> point) { - auto x1 = std::trunc(point.x()); - auto y1 = std::trunc(point.y()); - auto x2 = x1 + 1.0f; - auto y2 = y1 + 1.0f; +GridCellBoundaries grid_cell_containing_point(Vec2 point, Vec2 cell_size) { + return grid_cell_from_point(point.map(LAMBDA(std::trunc, 1)), cell_size); +} + +GridCellBoundaries grid_cell_from_point(Vec2 point, Vec2 cell_size) { + auto x1 = point.x(); + auto y1 = point.y(); + auto x2 = x1 + cell_size.x(); + auto y2 = y1 + cell_size.y(); return GridCellBoundaries{x1, x2, y1, y2}; } @@ -27,13 +33,17 @@ Vector<2> GridCellBoundaries::bottom_right() const { return {x2, y2}; } -CubeCellBoundaries cube_cell_for_point(Vector<3> point) { - auto x1 = std::trunc(point.x()); - auto y1 = std::trunc(point.y()); - auto z1 = std::trunc(point.z()); - auto x2 = x1 + 1.0f; - auto y2 = y1 + 1.0f; - auto z2 = z1 + 1.0f; +CubeCellBoundaries cube_cell_containing_point(Vec3 point, Vec3 cell_size) { + return cube_cell_from_point(point.map(LAMBDA(std::trunc, 1)), cell_size); +} + +CubeCellBoundaries cube_cell_from_point(Vec3 point, Vec3 cell_size) { + auto x1 = point.x(); + auto y1 = point.y(); + auto z1 = point.z(); + auto x2 = x1 + cell_size.x(); + auto y2 = y1 + cell_size.y(); + auto z2 = z1 + cell_size.z(); return CubeCellBoundaries{x1, x2, y1, y2, z1, z2}; } @@ -74,4 +84,4 @@ GridCellBoundaries CubeCellBoundaries::grid_cell() const { return {x1, x2, y1, y2}; } -} \ No newline at end of file +} diff --git a/src/Math/Grid.hpp b/src/Math/Grid.hpp index 8c0c423..e2a5051 100644 --- a/src/Math/Grid.hpp +++ b/src/Math/Grid.hpp @@ -13,7 +13,8 @@ struct GridCellBoundaries { [[nodiscard]] Vector<2> bottom_right() const; }; -GridCellBoundaries grid_cell_for_point(Vector<2> point); +GridCellBoundaries grid_cell_containing_point(Vec2 point, Vec2 cell_size = Vec2(1.0)); +GridCellBoundaries grid_cell_from_point(Vec2 point, Vec2 cell_size = Vec2(1.0)); struct CubeCellBoundaries { Real x1, x2, y1, y2, z1, z2; @@ -30,6 +31,7 @@ struct CubeCellBoundaries { [[nodiscard]] GridCellBoundaries grid_cell() const; }; -CubeCellBoundaries cube_cell_for_point(Vector<3> point); +CubeCellBoundaries cube_cell_containing_point(Vec3 point, Vec3 cell_size = Vec3(1.0)); +CubeCellBoundaries cube_cell_from_point(Vec3 point, Vec3 cell_size = Vec3(1.0)); } \ No newline at end of file diff --git a/src/Math/Perlin.cpp b/src/Math/Perlin.cpp index 992a08e..9a1c9f8 100644 --- a/src/Math/Perlin.cpp +++ b/src/Math/Perlin.cpp @@ -24,10 +24,10 @@ Vector<2> gradient(Vector<2> pos) { } Real raw(Vector<2> pos) { - auto cell = grid_cell_for_point(pos); + auto cell = grid_cell_containing_point(pos); auto uv = pos - cell.top_left(); - auto unit = grid_cell_for_point({}); + auto unit = grid_cell_containing_point({}); auto l11 = unit.top_left() - uv; auto l21 = unit.top_right() - uv; auto l12 = unit.bottom_left() - uv; @@ -58,10 +58,10 @@ Vector<3> gradient(Vector<3> pos) { } Real raw(Vector<3> pos) { - auto cell = cube_cell_for_point(pos); + auto cell = cube_cell_containing_point(pos); auto uv = pos - cell.front_top_left(); - auto unit = cube_cell_for_point({}); + auto unit = cube_cell_containing_point({}); auto l111 = unit.front_top_left() - uv; auto l211 = unit.front_top_right() - uv; auto l121 = unit.front_bottom_left() - uv; |
