-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeom.cpp
More file actions
executable file
·301 lines (214 loc) · 7 KB
/
Copy pathgeom.cpp
File metadata and controls
executable file
·301 lines (214 loc) · 7 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "geom.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <random>
#include <iostream>
#include <set>
#include <vector>
#define EPSILON .001
using namespace std;
double det2(double a1, double a2, double b1, double b2) {
return a1 * b1 - a2 * b2;
}
double det3(double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3) {
return a1 * det2(b2, b3, c2, c3) - a2 * det2(b1, b3, c1, c3) + a3 * det2(b1, b2, c1, c2);
}
/* returns 6 times the signed volume of abcd. The volume is positive
if d is behind abc (i.e. on opposite side as the normal); negative
if d is in front (i.e. same side as the normal) of abc, and 0 if
abcd are coplanar.
*/
double signed_volume(point3d a, point3d b, point3d c, point3d d) {
double det = a.x * det3(b.y, b.z, 1, c.y, c.z, 1, d.y, d.z, 1)
- a.y * det3(b.x, b.z, 1, c.x, c.y, 1, d.x, d.z, 1)
+ a.z * det3(b.x, b.y, 1, c.x, c.y, 1, d.x, d.y, 1)
- det3(b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z);
return det;
}
/* return True if points are on the same plane, and False otherwise */
bool coplanar(point3d a, point3d b, point3d c, point3d d) {
double vol = signed_volume(a, b, c, d);
if (vol < EPSILON && vol > - EPSILON) return true;
return false;
}
/* return True if d is strictly in front of abc; False otherwise */
bool infront (point3d a, point3d b, point3d c, point3d d) {
double vol = signed_volume(a, b, c, d);
return (vol < EPSILON);
}
bool face_is_extreme(int i, int j, int k, vector<point3d> & points) {
// printf("face:");
int nFront = 0;
int nBack = 0;
for (int s = 0; s < points.size(); s++) {
if ((s == i) || (s == j) || (s == k)) continue;
if (coplanar(points[i], points[j], points[k], points[s])) continue;
assert(!coplanar(points[i], points[j], points[k], points[s]));
if (infront(points[i], points[j], points[k], points[s])) {
//printf("front\n");
nFront++;
}
else {
//printf("back\n");
nBack++;
}
//printf(" %d %d", nFront, nBack);
if (nFront * nBack > 0) {
return false;
}
}
if (nFront == 0 || nBack == 0) {
return true;
}
return false;
}
bool pointEqual(point3d p1, point3d p2) {
return (p1.x == p2.x) && (p1.y == p2.y) && (p1.z == p2.z);
}
bool face_is_extreme(point3d p1, point3d p2, point3d p3, vector<point3d> & points) {
// printf("face:");
int nFront = 0;
int nBack = 0;
for (int s = 0; s < points.size(); s++) {
if ((pointEqual(p1, points[s])) || pointEqual(p2, points[s]) || pointEqual(p3, points[s])) continue;
if (coplanar(p1, p2, p3, points[s])) continue;
assert(!coplanar(p1, p2, p3, points[s]));
if (infront(p1, p2, p3, points[s])) {
//printf("front\n");
nFront++;
}
else {
//printf("back\n");
nBack++;
}
//printf(" %d %d", nFront, nBack);
if (nFront * nBack > 0) {
return false;
}
}
if (nFront == 0 || nBack == 0) {
return true;
}
return false;
}
/* compute the convex hull of the points */
void naive_hull(vector<point3d>& points, vector<triangle3d>& hull) {
//your code goes here
for (int i = 0; i < points.size(); i++) {
for (int j = i + 1; j < points.size(); j++) {
for (int k = j + 1; k < points.size(); k++) {
if (face_is_extreme(i, j, k, points)) {
printf("face is extreme\n");
triangle3d t;
t.a = & points[i];
t.b = & points[j];
t.c = & points[k];
t.color.r = (float)rand()/RAND_MAX;
t.color.g = (float)rand()/RAND_MAX;
t.color.b = (float)rand()/RAND_MAX;
hull.push_back(t);
}
}
}
}
}
/* compute the convex hull of the points */
void incremental_hull(vector<point3d>& points, vector<triangle3d> & hull) {
//your code goes here
}
/* compute the convex hull of the points */
void giftwrapping_hull(vector<point3d>& points, vector<triangle3d>& hull) {
vector<point3d> & remainingPoints = points;
set<edge> edges;
point3d firstPoint;
int firstPointIndex;
double currMin = -INT_MAX;
for (int i = 0; i < points.size(); i++) {
if (points[i].x > currMin) {
firstPoint = points[i];
firstPointIndex = i;
currMin = points[i].x;
}
}
// find second point
point3d tempPoint;
tempPoint.x = firstPoint.x + 1;
tempPoint.y = firstPoint.y;
tempPoint.z = firstPoint.z;
int secondPointIndex = -1;
for (int i = 0; i < points.size(); i++) {
if (i == firstPointIndex) continue;
if (secondPointIndex == -1) {
secondPointIndex = i;
}
else {
if (infront(points[firstPointIndex], tempPoint, points[secondPointIndex], points[i])) {
secondPointIndex = i;
}
}
}
point3d secondPoint = points[secondPointIndex];
// find third point
int thirdPointIndex = -1;
for (int i = 0; i < points.size(); i++) {
if (i == firstPointIndex) continue;
if (i == secondPointIndex) continue;
if (thirdPointIndex == -1) {
thirdPointIndex = i;
}
else {
if (infront(points[firstPointIndex], points[secondPointIndex], points[thirdPointIndex], points[i])) {
thirdPointIndex = i;
}
}
}
point3d thirdPoint = points[thirdPointIndex];
cout << "got here" << endl;
triangle3d face;
face.a = & points[firstPointIndex];
face.b = & points[secondPointIndex];
face.c = & points[thirdPointIndex];
face.color.r = (float)rand()/RAND_MAX;
face.color.g = (float)rand()/RAND_MAX;
face.color.b = (float)rand()/RAND_MAX;
hull.push_back(face);
remainingPoints.erase(remainingPoints.begin() + firstPointIndex);
remainingPoints.erase(remainingPoints.begin() + secondPointIndex);
remainingPoints.erase(remainingPoints.begin() + thirdPointIndex);
while (remainingPoints.size() > 0) {
// take a random edge in the existing hull
edge edgeChosen;
for (int i = 0; i < hull.size(); i++) {
edge currentEdge;
currentEdge.a = * hull[i].a;
currentEdge.b = * hull[i].b;
if (!(edges.find(currentEdge) == edges.end())) {
edgeChosen = currentEdge;
}
currentEdge.a = * hull[i].a;
currentEdge.b = * hull[i].c;
if (!(edges.find(currentEdge) == edges.end())) {
edgeChosen = currentEdge;
}
currentEdge.a = * hull[i].b;
currentEdge.b = * hull[i].c;
if (!(edges.find(currentEdge) == edges.end())) {
edgeChosen = currentEdge;
}
}
// add this edge to the edges set to avoid choosing it again in the future
edges.insert(edgeChosen);
// now, begin to look through each remaining point and see which face formed is extreme. If extreme, add to the hull
for (int j = 0; j < remainingPoints.size(); j++) {
triangle3d newFace;
newFace.a = & edgeChosen.a;
newFace.b = & edgeChosen.b;
newFace.c = & remainingPoints[j];
if (face_is_extreme(* newFace.a, * newFace.b, * newFace.c, remainingPoints)) {
hull.push_back(newFace);
remainingPoints.erase(remainingPoints.begin() + j);
}
}
}
}