Skip to content

Commit 9117412

Browse files
authored
Add Vec (Part 1) (#2)
- Add `src` to include path. - Replace ad-hoc arrays with `Vec`s.
1 parent f2d30e9 commit 9117412

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

tray/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_executable(${PROJECT_NAME})
22
target_sources(${PROJECT_NAME} PRIVATE src/main.cpp)
3+
target_include_directories(${PROJECT_NAME} PRIVATE src)
34

45
if(CMAKE_CXX_COMPILER_ID STREQUAL Clang OR CMAKE_CXX_COMPILER_ID STREQUAL GNU)
56
target_compile_options(${PROJECT_NAME} PRIVATE

tray/src/main.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1+
#include <tray/vec.hpp>
12
#include <array>
23
#include <cassert>
34
#include <iostream>
45
#include <ostream>
56
#include <span>
67

8+
using namespace tray;
9+
710
namespace {
8-
struct Rgb {
9-
std::array<std::uint8_t, 3> values{};
10-
};
11+
struct Rgb : Vec<std::uint8_t, 3> {};
1112

1213
struct ImageData {
1314
std::span<Rgb const> data{};
14-
std::array<std::uint32_t, 2> extent{};
15+
uvec2 extent{};
1516
};
1617

1718
std::ostream& operator<<(std::ostream& out, ImageData const& image) {
18-
assert(image.data.size() == image.extent[0] * image.extent[1]);
19+
assert(image.data.size() == image.extent.x() * image.extent.y());
1920
// write header
20-
out << "P3\n" << image.extent[0] << ' ' << image.extent[1] << "\n255\n";
21+
out << "P3\n" << image.extent.x() << ' ' << image.extent.y() << "\n255\n";
2122
// write each row
22-
for (std::uint32_t row = 0; row < image.extent[1]; ++row) {
23+
for (std::uint32_t row = 0; row < image.extent.y(); ++row) {
2324
// write each column
24-
for (std::uint32_t col = 0; col < image.extent[0]; ++col) {
25+
for (std::uint32_t col = 0; col < image.extent.x(); ++col) {
2526
// compute index
26-
auto const index = row * image.extent[0] + col;
27+
auto const index = row * image.extent.x() + col;
2728
// obtain corresponding Rgb
2829
auto const& rgb = image.data[index];
2930
// write out each channel
@@ -36,9 +37,9 @@ std::ostream& operator<<(std::ostream& out, ImageData const& image) {
3637
} // namespace
3738

3839
int main() {
39-
static constexpr auto extent = std::array{40U, 30U};
40-
static constexpr auto white_v = Rgb{.values = {0xff, 0xff, 0xff}};
41-
auto buffer = std::array<Rgb, extent[0] * extent[1]>{};
40+
static constexpr auto extent = uvec2{40U, 30U};
41+
static constexpr auto white_v = Rgb{0xff, 0xff, 0xff};
42+
auto buffer = std::array<Rgb, extent.x() * extent.y()>{};
4243
std::fill_n(buffer.data(), buffer.size(), white_v);
4344
auto const image_data = ImageData{.data = buffer, .extent = extent};
4445
std::cout << image_data;

tray/src/tray/vec.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include <array>
3+
#include <cstdint>
4+
5+
namespace tray {
6+
template <typename Type, std::size_t Dim>
7+
struct Vec {
8+
std::array<Type, Dim> values{};
9+
10+
constexpr Type& x() { return values[0]; }
11+
constexpr Type const& x() const { return values[0]; }
12+
constexpr Type& y() requires(Dim > 1) { return values[1]; }
13+
constexpr Type const& y() const requires(Dim > 1) { return values[1]; }
14+
constexpr Type& z() requires(Dim > 2) { return values[2]; }
15+
constexpr Type const& z() const requires(Dim > 2) { return values[2]; }
16+
};
17+
18+
using fvec2 = Vec<float, 2>;
19+
using uvec2 = Vec<std::uint32_t, 2>;
20+
using ivec2 = Vec<std::int32_t, 2>;
21+
22+
using fvec3 = Vec<float, 3>;
23+
using uvec3 = Vec<std::uint32_t, 3>;
24+
using ivec3 = Vec<std::int32_t, 3>;
25+
} // namespace tray

0 commit comments

Comments
 (0)