summaryrefslogtreecommitdiff
path: root/ray.h
blob: 5a68557745d1ec0ead1a1ce74fd0f09994a5df64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// Created by Keuin on 2022/4/11.
//

#ifndef RT_RAY_H
#define RT_RAY_H

#include "vec.h"

// A ray is a half-line, starts from a 3d point, along the direction of a unit vector, to the infinity
template<typename T>
class ray3 {
    const vec3<T> source_;
    const vec3<T> direction_; // unit vector

public:
    ray3() = delete;
    ray3(const vec3<T> &source, const vec3<T> &direction) : source_(source), direction_(direction.unit_vec()) {}

    // Get the source point from where the ray emits.
    vec3<T> source() const {
        return source_;
    }

    // Get the unit vector along the ray's direction.
    vec3<T> direction() const {
        return direction_;
    }

    // Compute the point this ray reaches at the time `t`.
    template<typename U>
    vec3<T> at(U t) const {
        return source_ + direction_ * t;
    }
};

using ray3d = ray3<double>;

#endif //RT_RAY_H