Loading...
Searching...
No Matches
yoda.hpp
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <cmath>
18#include <string>
19#include <vector>
20#include <optional>
21#include <functional>
22
33namespace yoda {
34
46namespace palette {
47 constexpr unsigned int base03 = 0x002b36;
48 constexpr unsigned int base02 = 0x073642;
49 constexpr unsigned int base01 = 0x586e75;
50 constexpr unsigned int base00 = 0x657b83;
51 constexpr unsigned int base0 = 0x839496;
52 constexpr unsigned int base1 = 0x93a1a1;
53 constexpr unsigned int base2 = 0xeee8d5;
54 constexpr unsigned int base3 = 0xfdf6e3;
55
56 constexpr unsigned int yellow = 0xb58900;
57 constexpr unsigned int orange = 0xcb4b16;
58 constexpr unsigned int red = 0xdc322f;
59 constexpr unsigned int magenta = 0xd33682;
60 constexpr unsigned int violet = 0x6c71c4;
61 constexpr unsigned int blue = 0x268bd2;
62 constexpr unsigned int cyan = 0x2aa198;
63 constexpr unsigned int green = 0x859900;
64} // namespace palette
65
81template <typename T>
82class Vec2 {
83public:
84 T x;
85 T y;
86
92 Vec2(T x = T{}, T y = T{}) : x(x), y(y) {}
93
100 [[nodiscard]] double magnitude() const {
101 return std::sqrt(static_cast<double>(x * x + y * y));
102 }
103
111 [[nodiscard]] Vec2<double> normalized() const {
112 double m = magnitude();
113 return {static_cast<double>(x) / m, static_cast<double>(y) / m};
114 }
115
127 [[nodiscard]] T dot(const Vec2& other) const {
128 return x * other.x + y * other.y;
129 }
130
136 Vec2 operator+(const Vec2& rhs) const {
137 return {x + rhs.x, y + rhs.y};
138 }
139
145 Vec2 operator*(T scalar) const {
146 return {x * scalar, y * scalar};
147 }
148};
149
152
155
167
185inline double lerp(double a, double b, double t) {
186 return a + t * (b - a);
187}
188
205template <typename T>
206std::vector<T> map(const std::vector<T>& values,
207 std::function<T(T)> fn) {
208 std::vector<T> result;
209 result.reserve(values.size());
210 for (const auto& v : values) {
211 result.push_back(fn(v));
212 }
213 return result;
214}
215
227template <typename T>
228std::optional<T> find_if(const std::vector<T>& values,
229 std::function<bool(const T&)> pred) {
230 for (const auto& v : values) {
231 if (pred(v)) return v;
232 }
233 return std::nullopt;
234}
235
245public:
252
258 explicit ScopedTimer(std::string label, Unit unit = Unit::Milliseconds);
259
266
268 [[nodiscard]] const std::string& label() const { return label_; }
269
270private:
271 std::string label_;
272 Unit unit_;
273};
274
275} // namespace yoda
Unit
Timer resolution units.
Definition yoda.hpp:247
@ Milliseconds
Report in milliseconds.
Definition yoda.hpp:249
@ Microseconds
Report in microseconds.
Definition yoda.hpp:250
@ Seconds
Report in seconds.
Definition yoda.hpp:248
const std::string & label() const
Get the label for this timer.
Definition yoda.hpp:268
ScopedTimer(std::string label, Unit unit=Unit::Milliseconds)
Start the timer with a label.
~ScopedTimer()
Stop the timer and print elapsed time.
A 2D vector with common geometric operations.
Definition yoda.hpp:82
double magnitude() const
Compute the Euclidean magnitude.
Definition yoda.hpp:100
Vec2 operator*(T scalar) const
Scalar multiplication.
Definition yoda.hpp:145
Vec2(T x=T{}, T y=T{})
Construct a new Vec2.
Definition yoda.hpp:92
T dot(const Vec2 &other) const
Compute the dot product of two vectors.
Definition yoda.hpp:127
Vec2< double > normalized() const
Return a normalized (unit-length) copy of this vector.
Definition yoda.hpp:111
Vec2 operator+(const Vec2 &rhs) const
Vector addition.
Definition yoda.hpp:136
Color palette constants from the Solarized scheme.
Definition yoda.hpp:46
constexpr unsigned int base02
Dark background.
Definition yoda.hpp:48
constexpr unsigned int base01
Optional emphasized content.
Definition yoda.hpp:49
constexpr unsigned int base2
Light background highlights.
Definition yoda.hpp:53
constexpr unsigned int magenta
Accent: magenta.
Definition yoda.hpp:59
constexpr unsigned int green
Accent: green.
Definition yoda.hpp:63
constexpr unsigned int red
Accent: red.
Definition yoda.hpp:58
constexpr unsigned int blue
Accent: blue.
Definition yoda.hpp:61
constexpr unsigned int base03
Darkest background.
Definition yoda.hpp:47
constexpr unsigned int base0
Body text (light mode)
Definition yoda.hpp:51
constexpr unsigned int base00
Body text (dark mode)
Definition yoda.hpp:50
constexpr unsigned int violet
Accent: violet.
Definition yoda.hpp:60
constexpr unsigned int yellow
Accent: yellow.
Definition yoda.hpp:56
constexpr unsigned int base1
Optional de-emphasized content.
Definition yoda.hpp:52
constexpr unsigned int cyan
Accent: cyan.
Definition yoda.hpp:62
constexpr unsigned int base3
Lightest background.
Definition yoda.hpp:54
constexpr unsigned int orange
Accent: orange.
Definition yoda.hpp:57
The Yoda namespace — core utilities for the doxyYoda demo.
Definition yoda.hpp:33
Vec2< double > Vec2d
Convenience alias for a double-precision 2D vector.
Definition yoda.hpp:151
Vec2< int > Vec2i
Convenience alias for an integer 2D vector.
Definition yoda.hpp:154
Interpolation
Interpolation methods available for smoothing operations.
Definition yoda.hpp:161
@ Linear
Simple linear interpolation: .
Definition yoda.hpp:162
@ Cosine
Cosine-smoothed interpolation.
Definition yoda.hpp:163
@ Cubic
Cubic Hermite interpolation.
Definition yoda.hpp:164
@ CatmullRom
Catmull-Rom spline interpolation.
Definition yoda.hpp:165
std::optional< T > find_if(const std::vector< T > &values, std::function< bool(const T &)> pred)
Find the first element satisfying a predicate.
Definition yoda.hpp:228
double lerp(double a, double b, double t)
Linearly interpolate between two values.
Definition yoda.hpp:185
std::vector< T > map(const std::vector< T > &values, std::function< T(T)> fn)
Apply a function element-wise to a vector of values.
Definition yoda.hpp:206