forked from Ultimaker/CuraEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizedModel.h
209 lines (192 loc) · 7.76 KB
/
optimizedModel.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
200
201
202
203
204
205
206
207
208
209
/** Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License */
#ifndef OPTIMIZED_MODEL_H
#define OPTIMIZED_MODEL_H
#include "utils/intpoint.h"
#include <map>
#include <vector>
class OptimizedFace
{
public:
int index[3];
int touching[3];
};
class OptimizedPoint3
{
public:
Point3 p;
std::vector<uint32_t> faceIndexList;
OptimizedPoint3(Point3 p): p(p) {}
};
#define MELD_DIST 30
class OptimizedModel;
class OptimizedVolume
{
public:
OptimizedModel* model;
std::vector<OptimizedPoint3> points;
std::vector<OptimizedFace> faces;
OptimizedVolume(SimpleVolume* volume, OptimizedModel* model)
: model(model)
{
points.reserve(volume->faces.size() * 3);
faces.reserve(volume->faces.size());
std::map<uint32_t, std::vector<uint32_t> > indexMap;
double t = getTime();
for(uint32_t i=0; i<volume->faces.size(); i++)
{
OptimizedFace f;
if((i%1000==0) && (getTime()-t)>2.0) logProgress("optimized", i + 1, volume->faces.size());
for(uint32_t j=0; j<3; j++)
{
Point3 p = volume->faces[i].v[j];
int hash = ((p.x + MELD_DIST/2) / MELD_DIST) ^ (((p.y + MELD_DIST/2) / MELD_DIST) << 10) ^ (((p.z + MELD_DIST/2) / MELD_DIST) << 20);
uint32_t idx;
bool add = true;
for(unsigned int n = 0; n < indexMap[hash].size(); n++)
{
if ((points[indexMap[hash][n]].p - p).testLength(MELD_DIST))
{
idx = indexMap[hash][n];
add = false;
break;
}
}
if (add)
{
indexMap[hash].push_back(points.size());
idx = points.size();
points.push_back(p);
}
f.index[j] = idx;
}
if (f.index[0] != f.index[1] && f.index[0] != f.index[2] && f.index[1] != f.index[2])
{
//Check if there is a face with the same points
bool duplicate = false;
for(unsigned int _idx0 = 0; _idx0 < points[f.index[0]].faceIndexList.size(); _idx0++)
{
for(unsigned int _idx1 = 0; _idx1 < points[f.index[1]].faceIndexList.size(); _idx1++)
{
for(unsigned int _idx2 = 0; _idx2 < points[f.index[2]].faceIndexList.size(); _idx2++)
{
if (points[f.index[0]].faceIndexList[_idx0] == points[f.index[1]].faceIndexList[_idx1] && points[f.index[0]].faceIndexList[_idx0] == points[f.index[2]].faceIndexList[_idx2])
duplicate = true;
}
}
}
if (!duplicate)
{
points[f.index[0]].faceIndexList.push_back(faces.size());
points[f.index[1]].faceIndexList.push_back(faces.size());
points[f.index[2]].faceIndexList.push_back(faces.size());
faces.push_back(f);
}
}
}
//fprintf(stdout, "\rAll faces are optimized in %5.1fs.\n",timeElapsed(t));
int openFacesCount = 0;
for(unsigned int i=0;i<faces.size();i++)
{
OptimizedFace* f = &faces[i];
f->touching[0] = getFaceIdxWithPoints(f->index[0], f->index[1], i);
f->touching[1] = getFaceIdxWithPoints(f->index[1], f->index[2], i);
f->touching[2] = getFaceIdxWithPoints(f->index[2], f->index[0], i);
if (f->touching[0] == -1)
openFacesCount++;
if (f->touching[1] == -1)
openFacesCount++;
if (f->touching[2] == -1)
openFacesCount++;
}
//fprintf(stdout, " Number of open faces: %i\n", openFacesCount);
}
int getFaceIdxWithPoints(int idx0, int idx1, int notFaceIdx)
{
for(unsigned int i=0;i<points[idx0].faceIndexList.size();i++)
{
int f0 = points[idx0].faceIndexList[i];
if (f0 == notFaceIdx) continue;
for(unsigned int j=0;j<points[idx1].faceIndexList.size();j++)
{
int f1 = points[idx1].faceIndexList[j];
if (f1 == notFaceIdx) continue;
if (f0 == f1) return f0;
}
}
return -1;
}
};
class OptimizedModel
{
public:
vector<OptimizedVolume> volumes;
Point3 modelSize;
Point3 vMin, vMax;
OptimizedModel(SimpleModel* model, Point3 center)
{
for(unsigned int i=0; i<model->volumes.size(); i++)
volumes.push_back(OptimizedVolume(&model->volumes[i], this));
vMin = model->min();
vMax = model->max();
Point3 vOffset((vMin.x + vMax.x) / 2, (vMin.y + vMax.y) / 2, vMin.z);
vOffset -= center;
for(unsigned int i=0; i<volumes.size(); i++)
for(unsigned int n=0; n<volumes[i].points.size(); n++)
volumes[i].points[n].p -= vOffset;
modelSize = vMax - vMin;
vMin -= vOffset;
vMax -= vOffset;
}
void saveDebugSTL(const char* filename)
{
char buffer[80] = "Cura_Engine_STL_export";
uint32_t n;
uint16_t s;
float flt;
OptimizedVolume* vol = &volumes[0];
FILE* f = fopen(filename, "wb");
fwrite(buffer, 80, 1, f);
n = vol->faces.size();
fwrite(&n, sizeof(n), 1, f);
for(unsigned int i=0;i<vol->faces.size();i++)
{
flt = 0;
s = 0;
fwrite(&flt, sizeof(flt), 1, f);
fwrite(&flt, sizeof(flt), 1, f);
fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[0]].p.x / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[0]].p.y / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[0]].p.z / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[1]].p.x / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[1]].p.y / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[1]].p.z / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[2]].p.x / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[2]].p.y / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
flt = vol->points[vol->faces[i].index[2]].p.z / 1000.0; fwrite(&flt, sizeof(flt), 1, f);
fwrite(&s, sizeof(s), 1, f);
}
fclose(f);
//Export the open faces so you can view the with Cura (hacky)
/*
char gcodeFilename[1024];
strcpy(gcodeFilename, filename);
strcpy(strchr(gcodeFilename, '.'), ".gcode");
f = fopen(gcodeFilename, "w");
for(unsigned int i=0;i<faces.size();i++)
{
for(int j=0;j<3;j++)
{
if (faces[i].touching[j] == -1)
{
Point3 p0 = points[faces[i].index[j]].p;
Point3 p1 = points[faces[i].index[(j+1)%3]].p;
fprintf(f, ";Model error(open face): (%f, %f, %f) (%f, %f, %f)\n", p0.x / 1000.0, p0.y / 1000.0, p0.z / 1000.0, p1.x / 1000.0, p1.y / 1000.0, p1.z / 1000.0);
}
}
}
fclose(f);
*/
}
};
#endif//OPTIMIZED_MODEL_H