Skip to content

Commit

Permalink
Support a color palette for toSvg
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardmgruber committed Feb 13, 2024
1 parent 1ca58fa commit cf20b6a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
44 changes: 35 additions & 9 deletions include/llama/DumpMapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ namespace llama
{
namespace internal
{
inline auto color(std::string_view recordCoordTags) -> std::size_t
inline auto color(std::string_view recordCoordTags) -> std::uint32_t
{
auto c = std::hash<std::string_view>{}(recordCoordTags) &std::size_t{0xFFFFFF};
c |= std::size_t{0x404040}; // ensure color per channel is at least 0x40.
auto c = static_cast<uint32_t>(std::hash<std::string_view>{}(recordCoordTags) &std::size_t{0xFFFFFF});
c |= 0x404040u; // ensure color per channel is at least 0x40.
return c;
}

Expand Down Expand Up @@ -82,6 +82,7 @@ namespace llama
struct FieldBox
{
ArrayIndex arrayIndex;
std::size_t flatRecordCoord;
std::string_view recordCoordTags;
NrAndOffset<std::size_t> nrAndOffset;
std::size_t size;
Expand All @@ -105,8 +106,14 @@ namespace llama
using Mapping = typename View::Mapping;
using RecordDim = typename Mapping::RecordDim;

auto emitInfo = [&](auto nrAndOffset, std::size_t size) {
infos.push_back({ai, prettyRecordCoord<RecordDim>(rc), nrAndOffset, size});
auto emitInfo = [&](auto nrAndOffset, std::size_t size)
{
infos.push_back(
{ai,
flatRecordCoord<RecordDim, RecordCoord>,
prettyRecordCoord<RecordDim>(rc),
nrAndOffset,
size});
};

using Type = GetType<RecordDim, decltype(rc)>;
Expand Down Expand Up @@ -194,6 +201,7 @@ namespace llama
const auto [nr, off] = mapping.blobNrAndOffset(ai, rc);
infos.push_back(
{ai,
flatRecordCoord<RecordDim, decltype(rc)>,
prettyRecordCoord<RecordDim>(rc),
{static_cast<std::size_t>(nr), static_cast<std::size_t>(off)},
sizeof(Type)});
Expand Down Expand Up @@ -234,9 +242,15 @@ namespace llama

/// Returns an SVG image visualizing the memory layout created by the given mapping. The created memory blocks are
/// wrapped after wrapByteCount bytes.
/// @param palette RGB colors as 0x00RRGGBB assigned cyclically. When empty, colors are assigned by hashing the
/// record coordinate.
LLAMA_EXPORT
template<typename Mapping>
auto toSvg(const Mapping& mapping, std::size_t wrapByteCount = 64, bool breakBoxes = true) -> std::string
auto toSvg(
const Mapping& mapping,
std::size_t wrapByteCount = 64,
bool breakBoxes = true,
const std::vector<std::uint32_t>& palette = {}) -> std::string
{
constexpr auto byteSizeInPixel = 30;
constexpr auto blobBlockWidth = 60;
Expand Down Expand Up @@ -309,7 +323,12 @@ namespace llama
}();
auto x = (offset % wrapByteCount) * byteSizeInPixel + blobBlockWidth;
auto y = (offset / wrapByteCount) * byteSizeInPixel + blobY;
const auto fill = internal::color(info.recordCoordTags);
const auto fillColor = [&]
{
if(palette.empty())
return internal::color(info.recordCoordTags);
return palette[info.flatRecordCoord % palette.size()];
}();
const auto width = byteSizeInPixel * info.size;

const auto nextOffset = [&]
Expand Down Expand Up @@ -339,13 +358,13 @@ namespace llama
y = 0;
}
svg += fmt::format(
R"(<rect x="{}" y="{}" width="{}" height="{}" fill="#{:X}" stroke="#000" fill-opacity="{}"/>
R"(<rect x="{}" y="{}" width="{}" height="{}" fill="#{:06X}" stroke="#000" fill-opacity="{}"/>
)",
x,
y,
width,
byteSizeInPixel,
fill,
fillColor,
isOverlapped ? 0.3 : 1.0);
for(std::size_t i = 1; i < info.size; i++)
{
Expand Down Expand Up @@ -390,6 +409,13 @@ namespace llama
return svg;
}

LLAMA_EXPORT
template<typename Mapping>
auto toSvg(const Mapping& mapping, const std::vector<std::uint32_t>& palette) -> std::string
{
return toSvg(mapping, 64, true, palette);
}

/// Returns an HTML document visualizing the memory layout created by the given mapping. The visualization is
/// resizeable.
LLAMA_EXPORT
Expand Down
11 changes: 9 additions & 2 deletions tests/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace
using ArrayExtents = decltype(extents);

template<typename Mapping>
void dump(const Mapping& mapping)
void dump(const Mapping& mapping, const std::vector<std::uint32_t>& palette = {})
{
const auto outputDir = std::string{"dump"};
std::filesystem::create_directory(outputDir);
// undocumented Catch feature, see: https://github.com/catchorg/Catch2/issues/510
const auto filename = outputDir + "/" + Catch::getResultCapture().getCurrentTestName();
std::ofstream{filename + ".svg"} << llama::toSvg(mapping);
std::ofstream{filename + ".svg"} << llama::toSvg(mapping, palette);
std::ofstream{filename + ".html"} << llama::toHtml(mapping);
}
} // namespace
Expand Down Expand Up @@ -406,6 +406,13 @@ TEST_CASE("dump.AdePT.track")
dump(llama::mapping::AoS<llama::ArrayExtents<int, 8>, Track>{{}});
}

TEST_CASE("dump.AdePT.track.palette")
{
dump(
llama::mapping::AoS<llama::ArrayExtents<int, 8>, Track>{{}},
{0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF});
}

TEST_CASE("dump.LHCb.Custom4")
{
dump(LhcbCustom4{{3}});
Expand Down

0 comments on commit cf20b6a

Please sign in to comment.