Skip to content

Commit

Permalink
cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
ptahmose committed Nov 18, 2023
1 parent 24d91b4 commit 5dbd9f6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
27 changes: 24 additions & 3 deletions Src/libCZI/SingleChannelAccessorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,36 @@ class CSingleChannelAccessorBase
/// is assumed to be done with the 'painter's algorithm", so what is rendered last is on top.
/// - We return a list of indices which are to be rendered, potentially leaving out some which have been determined
/// as not being visible. The indices returned are "indices as used by the 'get_subblock_index' functor", i.e.
/// it is **not** the subblock-number, but the argument that was passed to the functor.
/// it is **not** the subblock-index, but the argument that was passed to the functor.
/// - The caller can then use this list to render the subblocks (in the order as given in this vector).
///
/// \param roi The roi - if this is empty or invalid, then an empty vector is returned.
/// \param count Number of subblocks (specifying how many times the get_subblock_index-functor is being called.
/// \param get_subblock_index Functor which gives the subblock index to check.
/// \param count Number of subblocks (specifying how many times the get_subblock_index-functor is being called).
/// \param get_subblock_index Functor which gives the subblock index to check. This index is the index in the subblock repository.
///
/// \returns A list of indices of "arguments to the functor which delivered a visible subblock".
std::vector<int> CheckForVisibility(const libCZI::IntRect& roi, int count, const std::function<int(int)>& get_subblock_index) const;

/// Do a visibility check for a list of subblocks. This is the core method, which is used by the public method 'CheckForVisibility'.
/// What this function does, is:
/// - The method is given a ROI, and the number of subblocks to check.
/// - The function 'get_subblock_index' will be called with the argument being a counter, starting with count-1 and counting down to zero.
/// If called with value count-1, the subblock is the **last** one to be rendered; if called with value count-2, the subblock is the
/// second-to-the-last one to be rendered, and so on. If called with 0, the subblock is the **first** one to be rendered.
/// - The value it returned by the functor 'get_subblock_index' is then used with the functor 'get_rect_of_subblock' to get the rectangle. The
/// index returned by 'get_subblock_index' is passed in to the functor 'get_rect_of_subblock'. The rectangle returned by 'get_rect_of_subblock'
/// is the rectangle of the subblock, the region where this subblock is rendered.
/// - We return a list of indices which are to be rendered, potentially leaving out some which have been determined
/// as not being visible. The indices returned are "indices as used by the 'get_subblock_index' functor", i.e.
/// it is **not** the subblock-index, but the argument that was passed to the functor.
///
/// \param roi The roi - if this is empty or invalid, then an empty vector is returned.
/// \param count Number of subblocks (specifying how many times the get_subblock_index-functor is being called).
/// \param get_subblock_index Functor which gives the subblock index for a given counter. The counter starts with count-1 and counts down to zero.
/// \param get_rect_of_subblock Functor which gives the subblock rectangle for a subblock-index (as returned by 'get_subblock_index').
///
/// \returns A list of indices of "arguments to the functor which delivered a visible subblock". If the subblocks are rendered in the order
/// given here, then the result is guaranteed to be the same as if all subblocks were rendered. Non-visible subblocks are not
/// part of this list.
static std::vector<int> CheckForVisibilityCore(const libCZI::IntRect& roi, int count, const std::function<int(int)>& get_subblock_index, const std::function<libCZI::IntRect(int)>& get_rect_of_subblock);
};
13 changes: 8 additions & 5 deletions Src/libCZI/SingleChannelTileAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "utilities.h"
#include "SingleChannelTileCompositor.h"
#include "Site.h"
#include <iterator>
#include "bitmapData.h"

using namespace libCZI;
Expand Down Expand Up @@ -45,20 +44,24 @@ CSingleChannelTileAccessor::CSingleChannelTileAccessor(const std::shared_ptr<ISu

void CSingleChannelTileAccessor::ComposeTiles(libCZI::IBitmapData* pBm, int xPos, int yPos, const std::vector<IndexAndM>& subBlocksSet, const ISingleChannelTileAccessor::Options& options)
{
Compositors::ComposeSingleTileOptions composeOptions; composeOptions.Clear();
Compositors::ComposeSingleTileOptions composeOptions;
composeOptions.Clear();
composeOptions.drawTileBorder = options.drawTileBorder;

if (options.useVisibilityCheckOptimization)
{
// Try to reduce the number of subblocks to be rendered by doing a visibility check, and only rendering those which are visible.
// We report the subblocks in the order as they are given in the vector 'subBlocksSet', the lambda will be called with the
// argument 'index' counting down from subBlocksSet.size()-1 to 0. The subblock index we report for 'index=0' is the first one
// to be rendered, and 'index=subBlocksSet.size()-1' is the last one to be rendered (on top of all the others).
// We get a vector with the indices of the subblocks to be rendered, and then render them in the order as given in this vector
// (index here means - the number as passed to the lambda).
const auto indices_of_visible_tiles = this->CheckForVisibility(
{ xPos, yPos, static_cast<int>(pBm->GetWidth()), static_cast<int>(pBm->GetHeight()) },
static_cast<int>(subBlocksSet.size()),
[&](int index)->int
{
return subBlocksSet[index].index;
//auto it = subBlocksSet.rbegin(); // Reverse iterator pointing to the last element
//std::advance(it, index); // Advance it index times
//return it->index;
});

Compositors::ComposeSingleChannelTiles(
Expand Down
21 changes: 21 additions & 0 deletions Src/libCZI_UnitTests/test_TileAccessorCoverageOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,24 @@ TEST(TileAccessorCoverageOptimization, CheckForVisibility_TestCase3)
EXPECT_EQ(indices_of_visible_tiles[0], 1);
EXPECT_EQ(indices_of_visible_tiles[1], 4);
}

TEST(TileAccessorCoverageOptimization, CheckForVisibility_TestCase4)
{
static constexpr array<IntRect, 6> kSubBlocks{ IntRect{0,0,1,1}, IntRect{0,0,1,1}, IntRect{1,0,1,2}, IntRect{2,0,3,3}, IntRect{2,0,1,1}, IntRect{1,0,2,3} };

const auto indices_of_visible_tiles = CSingleChannelAccessorBaseToTestStub::CheckForVisibilityCore(
{ 0, 0, 3, 3 },
kSubBlocks.size(),
[&](int index)->int
{
return index;
},
[&](int subblock_index)->IntRect
{
return kSubBlocks[subblock_index];
});

ASSERT_EQ(indices_of_visible_tiles.size(), 2);
EXPECT_EQ(indices_of_visible_tiles[0], 1);
EXPECT_EQ(indices_of_visible_tiles[1], 5);
}

0 comments on commit 5dbd9f6

Please sign in to comment.