-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessFile.c
79 lines (77 loc) · 2.27 KB
/
processFile.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "structures.h"
#include "octreeOps.h"
model loadFile(FILE* input);
extern oct* generateOctree(model target, double modelSize);
void writeRawOctree(FILE* output, oct* tree);
void processFile(FILE* inputFile, FILE* outputFile, double modelSize){
model target = loadFile(inputFile);//create a model structure from the stl
oct* tree = generateOctree(target, modelSize);
#ifndef NDEBUG
validateOctree(tree);
#endif
free(target.facets);
printTreeStats(tree);
oct* inverse = invertOctree(tree);
#ifndef NDEBUG
validateOctree(inverse);
#endif
freeOctree(tree);
tree = NULL;
printTreeStats(inverse);
long int sideLen = sidelen(inverse->mag);
oct* inverseContiguous = findContiguousOctree(inverse, (pt){.l={-sideLen/2, -sideLen/2, -sideLen/2}});
#ifndef NDEBUG
validateOctree(inverseContiguous);
#endif
freeOctree(inverse);
inverse = NULL;
printTreeStats(inverseContiguous);
tree = invertOctree(inverseContiguous);//leaving you with just the contiguous
#ifndef NDEBUG
validateOctree(tree);
#endif
freeOctree(inverseContiguous);
inverseContiguous = NULL;
printTreeStats(tree);
if(expandflag){
//expanded is guaranteed to fail validateOctree
oct* expanded = expandOctree(tree);
freeOctree(tree);
tree = expanded;
expanded = NULL;
printTreeStats(tree);
}
if(stlflag){
//write the octree to file as stl
writeStlOutput(outputFile, tree);
}else{
writeRawOctree(outputFile, tree);
}
fclose(outputFile);
freeOctree(tree);
}
void writeRawOctree(FILE* output, oct* tree){
fprintf(output, "NHC3_%04X_", tree->mag);
for(long unsigned int offset = 0; offset < tree->tusage; offset++){
fputc(tree->t[offset], output);
}
fflush(output);
}
model loadFile(FILE* input){//This does not require the fancy stuff done during rendering where triangles have point references. We are doing this quick and dirty
model ret;
//skip header
fseek(input, 80, SEEK_SET);
//read triangle count
fread(&(ret.facetCount), sizeof(uint32_t), 1, input);
fprintf(stderr, "%d facets\n", ret.facetCount);
//allocate space for the triangles
ret.facets = calloc(ret.facetCount, sizeof(facet));
//read in all the triangles
fread(ret.facets, sizeof(facet), ret.facetCount, input);
fclose(input);
return ret;
}