Skip to content

Commit

Permalink
get tile from cell
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasaunai committed Nov 9, 2024
1 parent ffbc7fc commit 80c5501
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 56 deletions.
15 changes: 10 additions & 5 deletions src/core/data/tiles/tile_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "core/def.hpp"
#include "core/data/ndarray/ndarray_vector.hpp"

#include <iostream>
#include <array>
#include <tuple>
#include <utility>
#include <string>

namespace PHARE::core
Expand All @@ -19,7 +18,7 @@ class TileSet
public:
static auto constexpr dimension = Tile::dimension;

TileSet(Box<int, dimension> const& box, std::array<int, dimension> const& tile_size)
TileSet(Box<int, dimension> const& box, std::array<std::size_t, dimension> const& tile_size)
: box_{box}
, tile_size_{tile_size}
, shape_{[&]() {
Expand Down Expand Up @@ -68,6 +67,12 @@ class TileSet
NO_DISCARD auto& operator[](std::size_t i) { return tiles_[i]; }
NO_DISCARD auto const& operator[](std::size_t i) const { return tiles_[i]; }

template<typename... Index>
NO_DISCARD auto& at(Index... indexes)
{
return cells_(indexes...);
}

private:
void consistent_tile_size_() const
{
Expand Down Expand Up @@ -132,7 +137,7 @@ class TileSet
}


//! bri
//! store the pointer to the tile associated with each cell
void tag_cells_()
{
for (auto& tile : tiles_)
Expand All @@ -146,7 +151,7 @@ class TileSet


Box<int, dimension> box_;
std::array<int, dimension> tile_size_;
std::array<std::size_t, dimension> tile_size_;
std::array<int, dimension> shape_;
std::vector<Tile> tiles_;
NdArrayVector<dimension, Tile*> cells_;
Expand Down
2 changes: 1 addition & 1 deletion tests/core/data/particles/test_particle_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TYPED_TEST(ParticleTileTest, constructs)
{
constexpr auto dim = TypeParam::dimension;
Box<int, dim> box{ConstArray<int, dim>(0), ConstArray<int, dim>(50)};
auto const tile_size = PHARE::core::ConstArray<int, dim>(4);
auto const tile_size = PHARE::core::ConstArray<std::size_t, dim>(4);
TileSet<TypeParam> particleTileSet{box, tile_size};
}

Expand Down
136 changes: 86 additions & 50 deletions tests/core/data/tiles/test_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,83 +15,107 @@ using namespace PHARE::core;


template<typename TileSet>
class TileTest : public ::testing::Test
class TileTestBase
{
public:
static auto constexpr dimension = TileSet::dimension;

TileTestBase(Box<int, dimension> box_, std::array<std::size_t, dimension> const& tile_size)
: box{box_}
, tileSet{box, tile_size}
{
}

Box<int, dimension> box;
TileSet tileSet;
};

template<typename TileSet>
class TileTestBoxShapeNotMultipleTileSize : public TileTestBase<TileSet>, public ::testing::Test
{
public:
TileTest() {}
TileTestBoxShapeNotMultipleTileSize()
: TileTestBase<TileSet>{
Box<int, TileSet::dimension>{ConstArray<int, TileSet::dimension>(0),
ConstArray<int, TileSet::dimension>(54)},
ConstArray<std::size_t, TileSet::dimension>(4)}
{
}
};

template<typename TileSet>
class TileTestBoxShapeMultipleTileSize : public TileTestBase<TileSet>, public ::testing::Test
{
public:
TileTestBoxShapeMultipleTileSize()
: TileTestBase<TileSet>{
Box<int, TileSet::dimension>{ConstArray<int, TileSet::dimension>(0),
ConstArray<int, TileSet::dimension>(47)},
ConstArray<std::size_t, TileSet::dimension>(4)}
{
}
};


template<typename TileSet>
class TileTest : public ::testing::Test
{
};


template<std::size_t dim>
class TileMock : public Box<int, dim>
{
};

using DimTiles = testing::Types<TileSet<TileMock<1>>, TileSet<TileMock<2>>, TileSet<TileMock<3>>>;

TYPED_TEST_SUITE(TileTestBoxShapeNotMultipleTileSize, DimTiles);
TYPED_TEST_SUITE(TileTestBoxShapeMultipleTileSize, DimTiles);
TYPED_TEST_SUITE(TileTest, DimTiles);

TYPED_TEST(TileTest, expectedNbrOfTilesPerDimToCoverTheBox)
{
constexpr auto dim = TypeParam::dimension;
Box<int, dim> box{ConstArray<int, dim>(0), ConstArray<int, dim>(50)};
auto const tile_size = PHARE::core::ConstArray<int, dim>(4);
TypeParam cs{box, tile_size};


auto const& shape = cs.shape();
for (auto i = 0u; i < dim; ++i)
EXPECT_EQ(shape[i], 13);
TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, expectedNbrOfTilesPerDimToCoverTheBox)
{
auto const& shape = this->tileSet.shape();
for (auto i = 0u; i < this->dimension; ++i)
EXPECT_EQ(shape[i], 14);
}

TYPED_TEST(TileTest, cluserSetSizeIsCorrect)
TYPED_TEST(TileTestBoxShapeMultipleTileSize, cluserSetSizeIsCorrect)
{
constexpr auto dim = TypeParam::dimension;
Box<int, dim> box{ConstArray<int, dim>(0), ConstArray<int, dim>(47)};
auto const tile_size = PHARE::core::ConstArray<int, dim>(4);
TypeParam cs{box, tile_size};

EXPECT_EQ(cs.size(), std::pow(12, dim));
EXPECT_EQ(this->tileSet.size(), std::pow(12, this->dimension));
}


TYPED_TEST(TileTest, totalTileSetSurfaceIsEqualToBoxSurface)
TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, totalTileSetSurfaceIsEqualToBoxSurface)
{
constexpr auto dim = TypeParam::dimension;
Box<int, dim> box{ConstArray<int, dim>(0), ConstArray<int, dim>(49)};
auto const tile_size = PHARE::core::ConstArray<int, dim>(4);
TypeParam cs{box, tile_size};

auto surface = 0.;
for (auto i = 0u; i < cs.size(); ++i)
for (auto i = 0u; i < this->tileSet.size(); ++i)
{
auto current_surface = 1.;
for (auto d = 0u; d < dim; ++d)
for (auto d = 0u; d < this->dimension; ++d)
{
auto l = (cs[i].upper[d] - cs[i].lower[d] + 1);
auto l = (this->tileSet[i].upper[d] - this->tileSet[i].lower[d] + 1);
current_surface *= l;
}
surface += current_surface;
}
EXPECT_EQ(surface, box.size());
EXPECT_EQ(surface, this->box.size());
}



TYPED_TEST(TileTest, tileHasNoOverlapWithOthers)
TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, tileHasNoOverlapWithOthers)
{
constexpr auto dim = TypeParam::dimension;
Box<int, dim> box{ConstArray<int, dim>(0), ConstArray<int, dim>(54)};
auto const tile_size = PHARE::core::ConstArray<int, dim>(4);
TypeParam cs{box, tile_size};

for (auto const& tile : cs)
for (auto const& tile : this->tileSet)
{
for (auto const& other : cs)
for (auto const& other : this->tileSet)
{
if (&tile != &other)
{
auto const box1 = Box<int, dim>{tile.lower, tile.upper};
auto const box2 = Box<int, dim>{other.lower, other.upper};
auto const box1 = Box<int, this->dimension>{tile.lower, tile.upper};
auto const box2 = Box<int, this->dimension>{other.lower, other.upper};
auto overlap = box1 * box2;
EXPECT_FALSE(overlap.has_value());
}
Expand All @@ -100,16 +124,13 @@ TYPED_TEST(TileTest, tileHasNoOverlapWithOthers)
}


TYPED_TEST(TileTest, retrieveTilesFromBoxOverlap)
TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, retrieveTilesFromBoxOverlap)
{
constexpr auto dim = TypeParam::dimension;
Box<int, dim> box{ConstArray<int, dim>(0), ConstArray<int, dim>(54)};
auto const tile_size = PHARE::core::ConstArray<int, dim>(4);
TypeParam cs{box, tile_size};
Box<int, dim> selection_box{ConstArray<int, dim>(11), ConstArray<int, dim>(34)};
Box<int, this->dimension> selection_box{ConstArray<int, this->dimension>(11),
ConstArray<int, this->dimension>(34)};

auto expected_nbr = std::pow(7, dim);
auto overlapeds = cs.export_overlaped_with(selection_box);
auto expected_nbr = std::pow(7, this->dimension);
auto overlapeds = this->tileSet.export_overlaped_with(selection_box);
EXPECT_EQ(overlapeds.size(), expected_nbr);

auto completes = 0.;
Expand All @@ -122,20 +143,35 @@ TYPED_TEST(TileTest, retrieveTilesFromBoxOverlap)
else
++incompletes;
}
EXPECT_EQ(completes, std::pow(5, dim));
EXPECT_EQ(incompletes, std::pow(7, dim) - std::pow(5, dim));
EXPECT_EQ(completes, std::pow(5, this->dimension));
EXPECT_EQ(incompletes, std::pow(7, this->dimension) - std::pow(5, this->dimension));
}


TYPED_TEST(TileTest, cannotCreateTileWithTileSizeBiggerThanBox)
{
constexpr auto dim = TypeParam::dimension;
Box<int, dim> box{ConstArray<int, dim>(0), ConstArray<int, dim>(5)};
auto const tile_size = PHARE::core::ConstArray<int, dim>(7); // larger than box shape
auto const tile_size = PHARE::core::ConstArray<std::size_t, dim>(7); // larger than box shape
EXPECT_THROW(std::make_unique<TypeParam>(box, tile_size), std::runtime_error);
}


TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, canRetrieveTileFromCell)
{
auto tile = [&]() {
if constexpr (this->dimension == 1)
return this->tileSet.at(13);
else if constexpr (this->dimension == 2)
return this->tileSet.at(13, 13);
else if constexpr (this->dimension == 3)
return this->tileSet.at(13, 13, 13);
}();
auto const expected_box = Box<int, this->dimension>{ConstArray<int, this->dimension>(12),
ConstArray<int, this->dimension>(15)};
EXPECT_TRUE(*tile == expected_box);
}



int main(int argc, char** argv)
Expand Down

0 comments on commit 80c5501

Please sign in to comment.