-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
383 lines (332 loc) · 13.1 KB
/
main.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <stdio.h>
#include <iterator>
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "tinygltf/tiny_gltf.h"
#include <unistd.h>
#include <iostream>
#include <fstream>
#include "point.h"
#include "octree.h"
using std::cout;
using std::endl;
// the 8 corners of
// the 8 corners of a cube
void vertices(std::vector<Pointf> out) {
const float s = 0.5;
// front side (-y) starting top left (-x, +z)
out.push_back(Pointf(-s, -s, s)); // 0
out.push_back(Pointf( s, -s, s)); // 1
out.push_back(Pointf(-s, -s, -s)); // 2
out.push_back(Pointf( s, -s, s)); // 3
// backside (+y) starting top left (-x, +z)
out.push_back(Pointf(-s, s, s)); // 4
out.push_back(Pointf( s, s, s)); // 5
out.push_back(Pointf(-s, s, -s)); // 6
out.push_back(Pointf( s, s, -s)); // 7
}
const Pointf points[] {
// front side (-y) starting top left (-x, +z)
Pointf(-0.5f, -0.5f, 0.5f), // 0
Pointf( 0.5f, -0.5f, 0.5f), // 1
Pointf(-0.5f, -0.5f, -0.5f), // 2
Pointf( 0.5f, -0.5f, -0.5f), // 3
// backside (+y) starting top left (-x, +z)
Pointf(-0.5f, 0.5f, 0.5f), // 4
Pointf( 0.5f, 0.5f, 0.5f), // 5
Pointf(-0.5f, 0.5f, -0.5f), // 6
Pointf( 0.5f, 0.5f, -0.5f) // 7
};
const int planes[] {
//-x: 0
0,4,6,2,
//+x: 1
3,7,5,1,
// -y: 2
0,2,3,1,
// +y: 3
5,7,6,4,
//-z: 4
6,7,3,2,
//+z: 5
1,5,4,0,
};
void add(std::vector<Pointf> &vert, std::vector<int> &ind, Pointf point) {
for (int i = 0; i < vert.size(); i++) {
if (point.closeto(vert[i])) {
ind.push_back(i);
return;
}
}
ind.push_back(vert.size());
vert.push_back(point);
}
void cube(std::vector<Pointf> &vert, std::vector<int> &ind, Pointf origin, int direction) {
add(vert, ind, origin + points[planes[direction*4 + 0]]);
add(vert, ind, origin + points[planes[direction*4 + 1]]);
add(vert, ind, origin + points[planes[direction*4 + 2]]);
add(vert, ind, origin + points[planes[direction*4 + 3]]);
}
int main(int argc, char **argv) {
using namespace tinygltf;
Model model;
TinyGLTF loader;
std::string err;
std::string warn;
char *filename = argv[1];
if (!filename) {
filename = "../../PUSHILIN_forest.glb";
filename = "../../DamagedHelmet.glb";
// printf("No model file\n");
// return 1;
}
printf("%s exists: %s\n", filename, std::filesystem::exists(filename) ? "yes" : "no");
// bool ret = loader.LoadASCIIFromFile(&model, &err, &warn, filename);
bool ret = loader.LoadBinaryFromFile(&model, &err, &warn, filename); // for binary glTF(.glb)
if (!warn.empty()) {
printf("Warn: %s\n", warn.c_str());
}
if (!err.empty()) {
printf("Err: %s\n", err.c_str());
}
if (!ret) {
printf("Failed to parse glTF\n");
return -1;
}
// Collect points and find the size
Point min(MAXFLOAT, MAXFLOAT, MAXFLOAT);
Point max(-MAXFLOAT, -MAXFLOAT, -MAXFLOAT);
std::vector<Pointf> points;
std::vector<Triangle<int>> triangleIndices;
for (auto &mesh : model.meshes) {
for (auto primitive : mesh.primitives) {
const tinygltf::Accessor& indicesAccessor = model.accessors[primitive.indices];
const tinygltf::BufferView& indicesBufferView = model.bufferViews[indicesAccessor.bufferView];
const tinygltf::Buffer& indicesBuffer = model.buffers[indicesBufferView.buffer];
const unsigned short* indices = reinterpret_cast<const unsigned short*>(&indicesBuffer.data[indicesBufferView.byteOffset + indicesAccessor.byteOffset]);
for (size_t i = 0; i < indicesAccessor.count; i+=3) {
triangleIndices.push_back(Triangle<int>(indices[i], indices[i+1], indices[i+2]));
}
const tinygltf::Accessor& accessor = model.accessors[primitive.attributes["POSITION"]];
const tinygltf::BufferView& bufferView = model.bufferViews[accessor.bufferView];
// cast to float type read only. Use accessor and bufview byte offsets to determine where position data
// is located in the buffer.
const tinygltf::Buffer& buffer = model.buffers[bufferView.buffer];
// bufferView byteoffset + accessor byteoffset tells you where the actual position data is within the buffer. From there
// you should already know how the data needs to be interpreted.
const float* positions = reinterpret_cast<const float*>(&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
// From here, you choose what you wish to do with this position data.
for (size_t i = 0; i < accessor.count; ++i) {
// Positions are Vec3 components, so for each vec3 stride, offset for x, y, and z.
// std::cout << "(" << positions[i * 3 + 0] << ", "// x
// << positions[i * 3 + 1] << ", " // y
// << positions[i * 3 + 2] << ")" // z
// << "\n";
Point point(positions[i * 3 + 0], positions[i * 3 + 1], positions[i * 3 + 2]);
min.x = point.x < min.x ? point.x : min.x;
min.y = point.y < min.y ? point.y : min.y;
min.z = point.z < min.z ? point.z : min.z;
max.x = point.x > max.x ? point.x : max.x;
max.y = point.y > max.y ? point.y : max.y;
max.z = point.z > max.z ? point.z : max.z;
points.push_back(point);
}
}
}
Point offset = Point<float>() - min;
min = min;
max = max;
Point size = max - min;
cout << "min: " << min << endl;
cout << "max: " << max << endl;
cout << "size: " << size << endl;
struct Grid {
bool *data;
Point<int> dim;
Point<float> size;
Grid(Point<int> dimension, Point<float> size) : dim(dimension), size(size) {
Point ds = Point(1,1,1) + dim;
this->data = new bool[ds.x * ds.y * ds.z];
for(int i = 0; i < ds.x * ds.y * ds.z; i++) {
this->data[i] = false;
}
}
~Grid() {
delete[] data;
}
int index(int x, int y, int z) {
assert(x >= 0 && y >= 0 && z >= 0);
assert(x <= dim.x && y <= dim.y && z <= dim.z);
int i = z * (dim.x * dim.y) + y * (dim.x) + x;
// assert(i < dim.x * dim.y * dim.z);
return i;
}
void add(Point<float> point) {
int x = dim.x * (point.x / size.x);
int y = dim.y * (point.y / size.y);
int z = dim.z * (point.z / size.z);
data[index(x, y, z)] = true;
}
bool get(int x, int y, int z) {
return data[index(x, y, z)];
}
void set(int x, int y, int z, bool value) {
data[index(x, y, z)] = value;
}
};
int divs = 6;
int gridsize = pow(2, divs);
Pointf base(gridsize, gridsize, gridsize);
Pointf aspect = Pointf(size.x / size.x, size.y/size.x, size.z/size.x);
Pointf sized = base * aspect;
Point<int> dim((int)sized.x, (int)sized.y, (int)sized.z);
Grid grid(dim, size);
// for (int i = 0; i < points.size(); i++) {
// points[i] += offset;
// }
// grow a tree
Octree tree(points, min, max, divs);
for (int i = 0; i < triangleIndices.size(); i++) {
auto t = triangleIndices[i];
Triangle<Pointf> tp = Triangle<Pointf>(points[t.p1], points[t.p2], points[t.p3]);
tree.addTriangle(t, tp);
}
// put leafs containing a point into the grid
for (auto leaf : tree.leafs()) {
if (leaf->triangles.size() > 0) {
grid.add(offset + leaf->center());
}
}
// for (auto point : points) {
// grid.add(offset + point);
// }
cout << "Grid dimension: " << dim << endl;
if (false) {
// Connect all corners to make a homogenious thing
int added_count;
int maxiter = 1;
for (;maxiter > 0; maxiter--) {
added_count = 0;
for (int z = 0; z < grid.dim.z; z++) {
for (int y = 0; y < grid.dim.y; y++) {
for (int x = 0; x < grid.dim.x; x++) {
if (grid.get(x, y, z)) {
int min = 0xffffff, max = -0xffffff;
for (int i = 0; i < grid.dim.x; i++) {
if (grid.get(i, y, z) && i != x) {
min = i < min ? i : min;
max = i > max ? i : max;
}
}
for (int i = min; i < max; i++) {
grid.set(i, y, z, true);
added_count++;
}
min = min = 0xffffff, max = -0xffffff;
for (int i = 0; i < grid.dim.y; i++) {
if (grid.get(x, i, z) && i != y) {
min = i < min ? i : min;
max = i > max ? i : max;
}
}
for (int i = min; i < max; i++) {
grid.set(x, i, z, true);
added_count++;
}
min = min = 0xffffff, max = -0xffffff;
for (int i = 0; i < grid.dim.z; i++) {
if (grid.get(x, y, i) && i != z) {
min = i < min ? i : min;
max = i > max ? i : max;
}
}
for (int i = min; i < max; i++) {
grid.set(x, y, i, true);
added_count++;
}
}
}
}
}
}
}
if (false) {
// print a slice of the grid?
for (int z = 0; z < grid.dim.z; z += 2) {
for (int y = 0; y < grid.dim.y; y++) {
for (int x = 0; x < grid.dim.x; x++) {
if (x == 0) cout << endl;
// auto filled = plane[i];
auto filled = grid.get(x, y, z);
cout << (filled ? "0 " : " ");
}
}
cout << endl;
}
}
// project to one plane
bool *plane = new bool[grid.dim.x * grid.dim.y];
// print a slice of the grid?
for (int z = 0; z < grid.dim.z; z++) {
for (int y = 0; y < grid.dim.y; y++) {
for (int x = 0; x < grid.dim.x; x++) {
// auto filled = plane[i];
auto filled = grid.get(x, y, z);
plane[grid.index(x, y, 0)] |= filled;
}
}
}
for (int y = 0; y < grid.dim.y; y++) {
for (int x = 0; x < grid.dim.x; x++) {
if (x == 0) cout << endl;
// auto filled = plane[i];
auto filled = plane[grid.index(x, y, 0)];
cout << (filled ? "0 " : " ");
}
}
cout << endl;
// marchinf of the cubes
std::vector<Pointf> vertices;
std::vector<int> indices;
for (int z = 0; z < grid.dim.z; z++) {
for (int y = 0; y < grid.dim.y; y++) {
for (int x = 0; x < grid.dim.x; x++) {
if (!grid.get(x, y, z)) continue;
//-x: 0
//+x: 1
// -y: 2
// +y: 3
//-z: 4
//+z: 5
bool hits[] = {
(x > 0 && grid.get(x-1, y, z)),
(x < (grid.dim.x-1) && grid.get(x+1, y, z)),
(y > 0 && grid.get(x, y-1, z)),
(y < (grid.dim.y-1) && grid.get(x, y+1, z)),
(z > 0 && grid.get(x, y, z-1)),
(z < (grid.dim.z-1) && grid.get(x, y, z+1))
};
// add geometry where neighbors are missing
for (int side = 0; side < 6; side++) {
if (hits[side] == false) {
Pointf origin = Pointf(x, y, z);
cube(vertices, indices, origin, side);
}
}
}
}
}
// Build obj file
std::string contents = "";
std::ofstream file;
file.open("out.obj");
for (auto point : vertices) {
file << "v " << point.x << " " << point.y << " " << point.z << endl;
}
file << endl;
for (int i = 0; i < indices.size(); i+=4) {
file << "f " << indices[i+0]+1 << " " << indices[i+1]+1 << " " << indices[i+2]+1 << " " << indices[i+3]+1 << endl;
}
file.close();
}