Skip to content

Instantly share code, notes, and snippets.

@dahmadjid
Last active November 16, 2023 01:41
Show Gist options
  • Save dahmadjid/c60e5048124409b48977f1410d1eef9c to your computer and use it in GitHub Desktop.
Save dahmadjid/c60e5048124409b48977f1410d1eef9c to your computer and use it in GitHub Desktop.
template<typename T>
concept Hittable = requires (T& t, const Vec3<float>& position, const Ray& ray){
{ t.position() } -> std::same_as<Vec3<float>>;
{ t.set_position(position) } -> std::same_as<void>;
{ t.hit(ray) } -> std::same_as<HitPayload>;
};
template<Hittable...Ts>
class HittableList {
public:
template<same_as_any<Ts...> T>
auto add_object(T&& hittable_object) {
return std::get<std::vector<T>>(m_hittable_objects).emplace_back(hittable_object);
}
auto hit(const Ray& ray) {
std::vector<HitPayload> ret_vec;
ret_vec.reserve((std::get<std::vector<Ts>>(m_hittable_objects).size() + ...));
std::apply([&](const std::vector<Ts>&... v) {
(std::for_each(v.begin(), v.end(), [&](auto object){ret_vec.emplace_back(object.hit(ray));}), ...);
}, m_hittable_objects);
return ret_vec;
}
private:
std::tuple<std::vector<Ts>...> m_hittable_objects;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment