-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.h
55 lines (50 loc) · 1.25 KB
/
plane.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
#ifndef PLANE_H
#define PLANE_H
#include <cassert>
#include <limits>
#include "utils.h"
#include "object.h"
#include "ray.h"
class Plane : public Object {
public:
Plane() {}
void prepare_coords() {
Vector3f v1, v2;
o = Vector3f::ZERO;
if (fabs(norm[0]) > 1e-4) o[0] = d / norm[0];
else if (fabs(norm[1]) > 1e-4) o[1] = d / norm[1];
else o[2] = d / norm[2];
createCoordinateSystem(norm, v1, v2);
//norm,v1,v2 factorization
matrix = Matrix3f(norm, v1, v2).inverse();
}
Plane(const Vector3f& norm, float d, Material*m):
norm(norm),d(d),Object(m) {
d /= this->norm.length();
this->norm.normalize();
if (m->need_coords()) prepare_coords();
}
bool intersect(const Ray& ray, Hit& h, float tmin) override {
float g = d - Vector3f::dot(ray.o, norm), w = Vector3f::dot(ray.d, norm);
if (w == 0) return 0;
float t = g / w;
if (t < tmin || t >= h.t) return 0;
h.t = t; h.obj = this; h.norm = norm;if (m->need_coords()) {
Vector3f p = matrix * (ray.o + ray.d * t - o);
// std::cerr << p[0] << "," << p[1] << "," << p[2] << "\n";
h.pos = Vector2f(p[1], p[2]);
}
else h.pos = Vector2f(0, 0);
return 1;
}
BoundingBox getBound()
{
return BoundingBox::FULL;
}
protected:
Vector3f norm;
float d;
Vector3f o;
Matrix3f matrix;
};
#endif