Skip to content

Add Vec (Part 1) #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tray/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE src/main.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE src)

if(CMAKE_CXX_COMPILER_ID STREQUAL Clang OR CMAKE_CXX_COMPILER_ID STREQUAL GNU)
target_compile_options(${PROJECT_NAME} PRIVATE
Expand Down
25 changes: 13 additions & 12 deletions tray/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#include <tray/vec.hpp>
#include <array>
#include <cassert>
#include <iostream>
#include <ostream>
#include <span>

using namespace tray;

namespace {
struct Rgb {
std::array<std::uint8_t, 3> values{};
};
struct Rgb : Vec<std::uint8_t, 3> {};

struct ImageData {
std::span<Rgb const> data{};
std::array<std::uint32_t, 2> extent{};
uvec2 extent{};
};

std::ostream& operator<<(std::ostream& out, ImageData const& image) {
assert(image.data.size() == image.extent[0] * image.extent[1]);
assert(image.data.size() == image.extent.x() * image.extent.y());
// write header
out << "P3\n" << image.extent[0] << ' ' << image.extent[1] << "\n255\n";
out << "P3\n" << image.extent.x() << ' ' << image.extent.y() << "\n255\n";
// write each row
for (std::uint32_t row = 0; row < image.extent[1]; ++row) {
for (std::uint32_t row = 0; row < image.extent.y(); ++row) {
// write each column
for (std::uint32_t col = 0; col < image.extent[0]; ++col) {
for (std::uint32_t col = 0; col < image.extent.x(); ++col) {
// compute index
auto const index = row * image.extent[0] + col;
auto const index = row * image.extent.x() + col;
// obtain corresponding Rgb
auto const& rgb = image.data[index];
// write out each channel
Expand All @@ -36,9 +37,9 @@ std::ostream& operator<<(std::ostream& out, ImageData const& image) {
} // namespace

int main() {
static constexpr auto extent = std::array{40U, 30U};
static constexpr auto white_v = Rgb{.values = {0xff, 0xff, 0xff}};
auto buffer = std::array<Rgb, extent[0] * extent[1]>{};
static constexpr auto extent = uvec2{40U, 30U};
static constexpr auto white_v = Rgb{0xff, 0xff, 0xff};
auto buffer = std::array<Rgb, extent.x() * extent.y()>{};
std::fill_n(buffer.data(), buffer.size(), white_v);
auto const image_data = ImageData{.data = buffer, .extent = extent};
std::cout << image_data;
Expand Down
25 changes: 25 additions & 0 deletions tray/src/tray/vec.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <array>
#include <cstdint>

namespace tray {
template <typename Type, std::size_t Dim>
struct Vec {
std::array<Type, Dim> values{};

constexpr Type& x() { return values[0]; }
constexpr Type const& x() const { return values[0]; }
constexpr Type& y() requires(Dim > 1) { return values[1]; }
constexpr Type const& y() const requires(Dim > 1) { return values[1]; }
constexpr Type& z() requires(Dim > 2) { return values[2]; }
constexpr Type const& z() const requires(Dim > 2) { return values[2]; }
};

using fvec2 = Vec<float, 2>;
using uvec2 = Vec<std::uint32_t, 2>;
using ivec2 = Vec<std::int32_t, 2>;

using fvec3 = Vec<float, 3>;
using uvec3 = Vec<std::uint32_t, 3>;
using ivec3 = Vec<std::int32_t, 3>;
} // namespace tray