Skip to content

Commit

Permalink
N dimensional math is done
Browse files Browse the repository at this point in the history
just need to continue reformatting for N dimensional support
  • Loading branch information
GDBobby committed Sep 28, 2023
1 parent 1a5391a commit 9c245c2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 252 deletions.
58 changes: 0 additions & 58 deletions noiz/include/noiz/noizN/detail/data.hpp

This file was deleted.

2 changes: 0 additions & 2 deletions noiz/include/noiz/noizN/detail/dimension_data.hpp

This file was deleted.

88 changes: 0 additions & 88 deletions noiz/include/noiz/noizN/detail/grid.hpp

This file was deleted.

49 changes: 0 additions & 49 deletions noiz/include/noiz/noizN/index.hpp

This file was deleted.

87 changes: 53 additions & 34 deletions noiz/include/noizN/detail/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace noiz::detail {
template <std::floating_point Type>
auto make_populated_grid(Index3 const grid_extent, Seed seed = Generator::make_random_seed()) -> Grid<Type> {
auto make_populated_grid(Index const grid_extent, Seed seed = Generator::make_random_seed()) -> Grid<Type> {
auto ret = make_grid<Type>(grid_extent);
auto generator = Generator{seed};
for (auto& corner : ret.corners) { generator.next(corner.gradient); }
Expand All @@ -15,47 +15,66 @@ template <std::floating_point Type>
constexpr auto compute_offsets(CornerCell<Type> const& corner, Vec<Type> const point) -> Cell<Type> {

Cell<Type> ret;
ret.resize(corner.size());
return Cell3<Type>{
.left_top_below = point - corner.left_top_below.location,
.right_top_below = point - corner.right_top_below.location,
.left_bottom_below = point - corner.left_bottom_below.location,
.right_bottom_below = point - corner.right_bottom_below.location,
.left_top_above = point - corner.left_top_above.location,
.right_top_above = point - corner.right_top_above.location,
.left_bottom_above = point - corner.left_bottom_above.location,
.right_bottom_above = point - corner.right_bottom_above.location,
};
ret.components.resize(corner.size());

for(int i = 0; i < ret.components.size(); i++){
ret.components[i] = point - corner.location.components[i];
}
return ret;
}

template <std::floating_point Type>
constexpr auto compute_dot_products(CornerCell3<Type> const& corner, Cell3<Type> const& offset) -> TCell3<Type> {
return TCell3<Type>{
.left_top_below = dot(corner.left_top_below.gradient, offset.left_top_below),
.right_top_below = dot(corner.right_top_below.gradient, offset.right_top_below),
.left_bottom_below = dot(corner.left_bottom_below.gradient, offset.left_bottom_below),
.right_bottom_below = dot(corner.right_bottom_below.gradient, offset.right_bottom_below),
constexpr auto compute_dot_products(CornerCell<Type> const& corner, Cell<Type> const& offset) -> TCell<Type> {

Cell<Type> ret;
ret.components.resize(corner.size());

for(int i = 0; i < ret.components.size(); i++){
ret.components[i] = dot(corner.gradient.components[i], offset.components[i]);
}
return ret;
}

template <std::floating_point Type>
constexpr auto interpolate_assistant(Vec<Type> const point, TCell<Type> const& dot_products, uint8_t dimension_count, Vec<Type> cell_interpolated_position, uint16_t corner_index) -> Type {
if(dimension_count > 1){
Type ret = (Type)0;

//at the highest level, corner_index will be passed in as 0
//in 3 dimensions, i want to add 4
//2^3 == 8, /2 == 4
//2 ^ (dimensions - 1)

.left_top_above = dot(corner.left_top_above.gradient, offset.left_top_above),
.right_top_above = dot(corner.right_top_above.gradient, offset.right_top_above),
.left_bottom_above = dot(corner.left_bottom_above.gradient, offset.left_bottom_above),
.right_bottom_above = dot(corner.right_bottom_above.gradient, offset.right_bottom_above),
};

auto const value_a = interpolate_assistant(point, dot_products, dimension_count - 1, corner_index);
uint16_t corner_offset = 2;
for(int i = 1; i < dimension_count - 1; i++){
corner_offset *= 2;
}
auto const value_b = interpolate_assistant(point, dot_products, dimension_count - 1, corner_offset);
return std::lerp(
value_a,
value_b,
cell_interpolated_position[dimension_count - 1]
);

}
else{
//currently in the first dimension, the final dimension to be calculated
return std::lerp(
dot_products.corners[corner_index],
dot_products.corners[corner_index + 1],
cell_interpolated_position[0];
);
}
}


template <std::floating_point Type>
constexpr auto interpolate(Vec3<Type> const point, TCell3<Type> const& dot_products) -> Type {
auto const uvw = point.fract().fade();

auto const below_a = std::lerp(dot_products.left_top_below, dot_products.right_top_below, uvw.x);
auto const below_b = std::lerp(dot_products.left_bottom_below, dot_products.right_bottom_below, uvw.x);
auto const below = std::lerp(below_a, below_b, uvw.y);

auto const above_a = std::lerp(dot_products.left_top_above, dot_products.right_top_above, uvw.x);
auto const above_b = std::lerp(dot_products.left_bottom_above, dot_products.right_bottom_above, uvw.x);
auto const above = std::lerp(above_a, above_b, uvw.y);
auto const cell_interpolated_position = point.fract().fade();
uint8_t dimension_count = point.components.size();

//i might need to swap the position of below and above, not really sure
return std::lerp(below, above, uvw.z);
return interpolate_assistant(point, dot_products, dimension_count, cell_interpolated_position, 0);
}
} // namespace noiz::detail
32 changes: 11 additions & 21 deletions noiz/include/noizN/detail/grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ template <std::floating_point Type>

template <std::floating_point Type>
[[nodiscard]] auto make_grid(Index grid_extent, uint8_t dimension_count) -> Grid<Type> {
auto const corner_count = (grid_extent.x + 1) * (grid_extent.y + 1) * (grid_extent.z + 1);
auto const grid_dimensions = grid_extent.components.size();
assert(grid_dimensions > 0);

int64_t corner_count = grid_extent.components[0] + 1;
for(int i = 1; i < grid_dimensions; i++){
corner_count *= grid_extent.components[i] + 1;
}
if (corner_count <= 0) { return {}; }

auto ret = Grid<Type>{
Expand All @@ -62,42 +68,26 @@ template <std::floating_point Type>
};

Index index;
auto const grid_dimensions = grid_extent.components.size();
index.components.resize(grid_dimensions, 0);

auto back_dimension_grid_extent = grid_extent.components[grid_dimensions - 1] + 1;
auto& back_component_position = index.components[grid_dimensions - 1];

auto& first_dimension_position = cindex.components[0];
auto& first_dimension_position = index.components[0];
auto first_dimension_grid_extent = (grid_extent.components[0] + 1);

do{
auto const flat_index = index.flatten(grid_extent);
ret.corners.at(index).location = to_vec<Type>(index);

first_dimension_position++;
for(uint8_t i = 0; i < (dimension_count - 1); i++){
index.components[i + 1] += index.components[i] == (grid_extent.components[i] + 1);
index.components[i] %= grid_extent.components[i] + 1;
}
auto const flat_index = static_cast<std::size_t>(index.flatten(grid_extent));
ret.corners.at(index).location = to_vec<Type>(index);
}
while(back_component_position < back_dimension_grid_extent);


for(int depth = 0; depth <= grid_extent.z; ++depth) {
for (int row = 0; row <= grid_extent.y; ++row) {
for (int col = 0; col <= grid_extent.x; ++col) {
Index index;
index.reserve(dimension_count);
for(int i = 0; i < dimension_count; i++){

}
= Index{.x = col, .y = row, .z = depth};
auto const index = static_cast<std::size_t>(index.flatten(grid_extent));
ret.corners.at(index).location = to_vec3<Type>(Index{.x = col, .y = row, .z = depth});
}
}
}

return ret;
}
} // namespace noiz::detail

0 comments on commit 9c245c2

Please sign in to comment.