-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.cpp
285 lines (245 loc) · 5.41 KB
/
geometry.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "geometry.h"
#include <limits>
#include <math.h>
#include <stdio.h>
#include <iostream>
#define EPSILON 1e-4
/*
* Class Point
*/
Point::Point() {
x = 0;
y = 0;
z = 0;
}
Point::Point(float xVal, float yVal, float zVal) {
x = xVal;
y = yVal;
z = zVal;
}
void Point::add(Vector v) {
x+=v.x;
y+=v.y;
z+=v.z;
}
void Point::sub(Vector v) {
x-=v.x;
y-=v.y;
z-=v.z;
}
Vector Point::sub(Point p) {
return Vector(x-p.x, y-p.y, z-p.z);
}
/*
* Class Vector
*/
Vector::Vector() {
x = 0;
y = 0;
z = 0;
// this->normalize();
}
Vector::Vector(float xVal, float yVal, float zVal) {
x = xVal;
y = yVal;
z = zVal;
}
Vector::Vector(Point point) {
x = point.x;
y = point.y;
z = point.z;
}
void Vector::scale(float k) {
x = x * k;
y = y * k;
z = z * k;
}
void Vector::normalize() {
float l2norm = norm();
l2norm = 1.0 / l2norm;
scale(l2norm);
}
float Vector::norm() {
return sqrt(pow(x,2) + pow(y,2) + pow(z,2));
}
float Vector::dot(Vector vector) {
return x * vector.x + y * vector.y + z * vector.z;
}
Vector Vector::cross(Vector v) {
return Vector(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
}
void Vector::add(Vector vector) {
x = x + vector.x;
y = y + vector.y;
z = z + vector.z;
}
void Vector::sub(Vector vector) {
x = x - vector.x;
y = y - vector.y;
z = z - vector.z;
}
void Vector::add(Point point) {
x = x + point.x;
y = y + point.y;
z = z + point.z;
}
void Vector::sub(Point point) {
x = x - point.x;
y = y - point.y;
z = z - point.z;
}
/*
* Class Ray
*/
Ray::Ray() {
origin = Point();
dir = Vector();
}
Ray::Ray(Point orig, Vector direction) {
origin = orig;
dir = direction;
dir.normalize();
tMin = EPSILON;
tMax = std::numeric_limits<float>::infinity();
}
Ray::Ray(Point orig, Vector direction, float tMaxVal) {
origin = orig;
dir = direction;
dir.normalize();
tMin = EPSILON;
tMax = tMaxVal;
}
Ray::Ray(Point orig, Vector direction, float tMinVal, float tMaxVal) {
origin = orig;
dir = direction;
dir.normalize();
tMin = tMinVal;
tMax = tMaxVal;
}
Point Ray::at(float t) {
if (t < tMin) t = tMin;
else if (t > tMax) t = tMax;
Point point = origin;
Vector direction = dir;
direction.scale(t);
point.add(direction);
return point;
}
LocalGeo::LocalGeo() {
pos = Point();
normal = Vector();
}
LocalGeo::LocalGeo(Point p, Vector n) {
pos = p;
normal = n;
normal.normalize();
}
Point LocalGeo::getPosition() {
return pos;
}
Vector LocalGeo::getNormal() {
return normal;
}
void LocalGeo::setPosition(Point p) {
pos = p;
}
void LocalGeo::setNormal(Vector n) {
normal = n;
normal.normalize();
}
float Shape::intersect(Ray& ray) {
}
float Shape::intersect(Ray& ray, LocalGeo* geo) {
}
Sphere::Sphere() {
center = Point();
radius = 1;
}
Sphere::Sphere(Point c, float r) {
center = c;
radius = r;
}
float Sphere::intersect(Ray& ray) {
float a = ray.dir.dot(ray.dir);
Vector temp = ray.origin;
temp.sub(center);
float b = ray.dir.dot(temp) * 2;
float c = temp.dot(temp) - pow(radius,2);
float discriminant = pow(b,2) - 4*a*c;
if (discriminant < 0) return -1;
else if (discriminant == 1) {
float t = -b/(2*a);
if (t >= ray.tMin && t <= ray.tMax) return t;
else return -1.0;
} else {
float t1 = (-b + sqrt(discriminant)) / (2*a);
float t2 = (-b - sqrt(discriminant)) / (2*a);
// Rest of the code assumes that t1 <= t2
if (t2 < t1) {
double temp = t1;
t1 = t2;
t2 = temp;
}
if (t1 <= ray.tMax && t1 >= ray.tMin) return t1;
else if (t2 >= ray.tMin && t2 <= ray.tMax) return t2;
else return -1.0;
}
}
float Sphere::intersect(Ray& ray, LocalGeo* local) {
float intersection = intersect(ray);
if (intersection != -1.0) {
// std::cout << "RAY:" << std::endl;
// std::cout << ray.origin.x << " " << ray.origin.y << " " << ray.origin.z << std::endl;
// std::cout << ray.dir.x << " " << ray.dir.y << " " << ray.dir.z << std::endl;
Point point = ray.at(intersection);
Vector normal = Vector(point);
local->setPosition(point);
normal.sub(center);
normal.normalize();
// std::cout << "SHAPE NORMAL:" << std::endl;
// std::cout << normal.x << " " << normal.y << " " << normal.z << std::endl;
local->setNormal(normal);
}
return intersection;
}
Triangle::Triangle() {
vertex = Point();
edgeOne = Vector();
edgeTwo = Vector();
}
Triangle::Triangle(Point point1, Point point2, Point point3) {
vertex = point1;
edgeOne = point2.sub(point1);
edgeTwo = point3.sub(point1);
}
float Triangle::intersect(Ray& ray) {
// Citation:
// http://tinyurl.com/2zve8a
Vector pVec = ray.dir.cross(edgeTwo);
float det = pVec.dot(edgeOne);
if (det > -EPSILON && det < EPSILON) return -1.0;
Vector tVec = ray.origin;
tVec.sub(vertex);
float u, v;
u = tVec.dot(pVec)/det;
if (u>1.0 || u<0.0) return -1.0;
Vector qVec = tVec.cross(edgeOne);
v = ray.dir.dot(qVec)/det;
if (v+u>1.0 || v<0.0) return -1.0;
float t = edgeTwo.dot(qVec)/det;
if (t<ray.tMin || t>ray.tMax) return -1.0;
return t;
}
float Triangle::intersect(Ray& ray, LocalGeo* local) {
float intersection = intersect(ray);
if (intersection != -1.0) {
local->setPosition(ray.at(intersection));
Vector triNormal = Vector(edgeOne.cross(edgeTwo));
triNormal.normalize();
Vector geomNormal;
geomNormal = Vector(edgeOne.cross(edgeTwo));
geomNormal.normalize();
local->setNormal(geomNormal);
}
return intersection;
}