-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimitive.h
199 lines (163 loc) · 3.77 KB
/
primitive.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef PRIMITIVE_H
#define PRIMITIVE_H
#include "vector.h"
#include "material.h"
#define EPSILON 0.0001f
class Ray {
public:
Vector3 startp;
Vector3 dir;
void SetFromPoints(const Vector3& s, const Vector3& e)
{
startp = s;
dir = e - s;
dir.Normalize();
}
Ray(){}
Ray(const Vector3& s, const Vector3& d)
{
startp = s; dir = d;
}
};
class Geometry {
public:
virtual bool CheckIntersection(const Ray& ray) const = 0;
virtual float FindIntersection(const Ray& ray) const = 0;
virtual float FindInnerIntersection(const Ray& ray) const = 0;
virtual Vector3 GetNormalAtPos(const Vector3& pos) const = 0;
Vector3 GetIntersectionPos(float t, const Ray& ray) const
{
return Vector3(ray.dir * t + ray.startp);
}
};
class Plane : public Geometry {
public:
Vector3 normal;
float D;
Plane(){}
Plane(const Vector3& n, float d)
{
normal = n;
D = d;
}
bool CheckIntersection(const Ray& ray) const
{
if ( (ray.dir * normal) >= 0.0f )
return false;
else
return true;
}
float FindIntersection(const Ray& ray) const
{
float t;
float det;
det = ray.dir * normal;
if ( det < EPSILON && det > -EPSILON)
return -999999.0f;
else
{
t = (D - normal * ray.startp) / det;
return t;
}
}
float FindInnerIntersection(const Ray& ray) const
{
float t;
float det;
det = ray.dir * normal;
if ( det < EPSILON && det > -EPSILON)
return -999999.0f;
else
{
t = (D - normal * ray.startp) / det;
return t;
}
}
Vector3 GetNormalAtPos(const Vector3& pos) const
{
return normal;
}
};
class Sphere : public Geometry {
public:
Vector3 center;
float R;
Sphere(){}
Sphere(const Vector3& c, float r)
{
center = c;
R = r;
}
bool CheckIntersection(const Ray& ray) const
{
// Diameter btw Sphere & Ray
Vector3 vSphere = center - ray.startp;
float S = ray.dir * vSphere;
if ( S < 0.0f ) return false;
Vector3 vLight = S * ray.dir;
Vector3 vDiameter = vLight - vSphere;
float Diameter = vDiameter.Length();
if ( Diameter >= R ) return false;
return true;
}
float FindIntersection(const Ray& ray) const
{
float A, B, C, D, tp, tm;
// Diameter btw Sphere & Ray
float S = ray.dir * (center - ray.startp);
if ( S < 0.0f ) return -999.0f;
Vector3 vDiameter = ray.dir * S - (center - ray.startp);
float Diameter = vDiameter.Length();
if ( Diameter >= R ) return -999.0f;
// Intersection
A = ray.dir * ray.dir;
B = 2 * (ray.startp - center) * ray.dir;
C = (ray.startp - center) * (ray.startp - center) - R * R;
D = B * B - 4 * A * C;
float sqrtD = sqrtf(D);
float recA2 = 1 / (2 * A);
tp = (-B + sqrtD) * recA2;
tm = (-B - sqrtD) * recA2;
if (tp < 0.0f || tm < 0.0f) return -999.0f;
if (tp > tm){
return tm;
}
else{
return tp;
}
}
float FindInnerIntersection(const Ray& ray) const
{
float A, B, C, D, tp, tm;
// Diameter btw Sphere & Ray
float S = ray.dir * (center - ray.startp);
if ( S < 0.0f ) return -999.0f;
Vector3 vDiameter = ray.dir * S - (center - ray.startp);
float Diameter = vDiameter.Length();
if ( Diameter >= R ) return -999.0f;
// Intersection
A = ray.dir * ray.dir;
B = 2 * (ray.startp - center) * ray.dir;
C = (ray.startp - center) * (ray.startp - center) - R * R;
D = B * B - 4 * A * C;
float sqrtD = sqrtf(D);
float recA2 = 1 / (2 * A);
tp = (-B + sqrtD) * recA2;
tm = (-B - sqrtD) * recA2;
if (tp < 0.0f && tm < 0.0f) return -999.0f;
if (tp > tm){
return tp;
}
else{
return tm;
}
}
Vector3 GetNormalAtPos(const Vector3& pos) const
{
Vector3 n;
n = pos - center;
n.Normalize();
return n;
}
};
#endif