-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShape.h
82 lines (60 loc) · 2.21 KB
/
Shape.h
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Created by Bray, Matthew D ERDC-RDE-GSL-MS CIV on 11/23/22.
//
#ifndef RAYCASTER_SHAPE_H
#define RAYCASTER_SHAPE_H
#include <cstdlib>
#include <vector>
#include <memory>
#include <Eigen/Core>
#include "Material.h"
#include "Ray.h"
#include "Intersection.h"
using Vector4f = Eigen::Vector4f;
using Matrix4f = Eigen::Matrix4f;
static int shape_count{0};
struct Shape
{
virtual ~Shape() = default;
Shape() = default;
bool operator==(const Shape& object) const {
return object.id == id;
}
inline void set_material(const Material& m) {
material = m;
}
inline void set_transform(const Matrix4f& transform) {
m_transform = transform;
m_inverse_transform = m_transform.inverse();
}
[[nodiscard]] inline auto matrix() const {
return m_transform;
}
[[nodiscard]] inline auto inverse() const {
return m_inverse_transform;
}
[[nodiscard]] virtual Vector4f normal_at(const Vector4f& world_point, const Intersection& i={}) const {
const Vector4f local_point = m_inverse_transform * world_point;
const Vector4f local_normal = local_normal_at(local_point, i);
Vector4f world_normal = m_inverse_transform.transpose() * local_normal;
world_normal[3] = 0;
return world_normal.normalized();
}
[[nodiscard]] virtual Vector4f local_normal_at(const Vector4f& world_point, const Intersection& i={}) const = 0;
[[nodiscard]] std::vector<Intersection> intersect(const Ray& r) const {
const auto local_ray = r.transform_ray(m_inverse_transform);
return local_intersect(local_ray);
}
[[nodiscard]] virtual std::vector<Intersection> local_intersect(const Ray& r) const = 0;
const int id{shape_count++};
Material material;
private:
Matrix4f m_transform{ Matrix4f::Identity() };
Matrix4f m_inverse_transform{ Matrix4f::Identity() };
};
inline Color pattern_at_object(const std::shared_ptr<Pattern>& pattern, const Shape* shape, const Vector4f& world_point) {
const Vector4f object_point = shape->inverse() * world_point;
const Vector4f pattern_point = pattern->inverse() * object_point;
return pattern->pattern_at(pattern_point);
}
#endif //RAYCASTER_SHAPE_H