forked from MatterHackers/MatterControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
310 lines (273 loc) · 8.58 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
#include <igl/readOFF.h>
#include <igl/readSTL.h>
#include <igl/writeSTL.h>
//#undef IGL_STATIC_LIBRARY
#include <igl/copyleft/cgal/mesh_boolean.h>
#include <igl/opengl/glfw/Viewer.h>
#include <Eigen/Core>
#include <iostream>
#include "tutorial_shared_path.h"
//#include <igl/barycenter.h>
//#include <igl/boundary_facets.h>
//#include <igl/copyleft/tetgen/tetrahedralize.h>
//#include <igl/copyleft/tetgen/cdt.h>
//#include <igl/winding_number.h>
//#include <igl/unique_simplices.h>
//#include <igl/remove_unreferenced.h>
//#include <igl/copyleft/cgal/remesh_self_intersections.h>
//#include <igl/redrum.h>
#include <Windows.h>
#define DBOUT( s ) \
{ \
std::ostringstream os_; \
os_ << s; \
OutputDebugString( os_.str().c_str() ); \
}
Eigen::MatrixXd VA, VB, VC;
Eigen::MatrixXd NA, NB, NC;
Eigen::VectorXi J, I;
Eigen::MatrixXi FA, FB, FC;
igl::MeshBooleanType boolean_type(
igl::MESH_BOOLEAN_TYPE_UNION);
void update(igl::opengl::glfw::Viewer &viewer)
{
Eigen::MatrixXd C(FC.rows(), 3);
for (size_t f = 0; f < C.rows(); f++)
{
if (J(f) < FA.rows())
{
C.row(f) = Eigen::RowVector3d(1, 0, 0);
}
else
{
C.row(f) = Eigen::RowVector3d(0, 1, 0);
}
}
viewer.data().clear();
viewer.data().set_mesh(VC, FC);
viewer.data().set_colors(C);
}
extern "C"
{
__declspec(dllexport) void DoBooleanOperation(double* pVA, int sizeVA, int* pFA, int sizeFA,
double* pVB, int sizeVB, int* pFB, int sizeFB,
int opperation,
double** ppVC, int* pSizeVC, int** ppFC, int* pSizeFC)
{
using namespace Eigen;
igl::MeshBooleanType booleanType(igl::MESH_BOOLEAN_TYPE_UNION);
if (opperation == 1)
{
booleanType = igl::MESH_BOOLEAN_TYPE_MINUS;
}
else if (opperation == 2)
{
booleanType = igl::MESH_BOOLEAN_TYPE_INTERSECT;
}
// make local data to hold our arrays
Eigen::MatrixXd VA, VB, VC;
Eigen::MatrixXi FA, FB, FC;
VA = Map<Matrix<double, Dynamic, Dynamic, RowMajor> >(pVA, sizeVA / 3, 3);
FA = Map<Matrix<int, Dynamic, Dynamic, RowMajor> >(pFA, sizeFA / 3, 3);
VB = Map<Matrix<double, Dynamic, Dynamic, RowMajor> >(pVB, sizeVB / 3, 3);
FB = Map<Matrix<int, Dynamic, Dynamic, RowMajor> >(pFB, sizeFB / 3, 3);
//DBOUT("The value of x is " << VA);
igl::copyleft::cgal::mesh_boolean(VA, FA, VB, FB, booleanType, VC, FC, J);
//igl::writeSTL("c:\\Temp\\test.stl", VC, FC, true);
int count = VC.rows() * VC.cols();
*pSizeVC = count;
*ppVC = new double[count];
int VCcols = VC.cols();
for (int row = 0; row < VC.rows(); row++)
{
for (int col = 0; col < VCcols; col++)
{
ppVC[0][row*VCcols + col] = VC(row, col);
}
}
count = FC.rows() * FC.cols();
*pSizeFC = count;
*ppFC = new int[count];
int FCcols = FC.cols();
for (int row = 0; row < FC.rows(); row++)
{
for (int col = 0; col < FCcols; col++)
{
ppFC[0][row*FCcols + col] = FC(row, col);
}
}
}
__declspec(dllexport) void DeleteDouble(double** data)
{
delete(*data);
}
__declspec(dllexport) void DeleteInt(int** data)
{
delete(*data);
}
__declspec(dllexport) int add(int a, int b)
{
return a + b;
}
__declspec(dllexport) int subtract(int a, int b)
{
return a - b;
}
}
//bool clean(
// const Eigen::MatrixXd & V,
// const Eigen::MatrixXi & F,
// Eigen::MatrixXd & CV,
// Eigen::MatrixXi & CF,
// Eigen::VectorXi & IM)
//{
// using namespace igl;
// using namespace igl::copyleft::tetgen;
// using namespace igl::copyleft::cgal;
// using namespace Eigen;
// using namespace std;
// //writeOBJ("VF.obj",V,F);
// const auto & validate_IM = [](
// const Eigen::MatrixXd & V,
// const Eigen::MatrixXd & CV,
// const Eigen::VectorXi & IM)
// {
// assert(IM.size() >= CV.rows());
// for (int i = 0; i < CV.rows(); i++)
// {
// if (IM(i) < V.rows() && IM(i) >= 0)
// {
// double diff = (V.row(IM(i)) - CV.row(i)).norm();
// if (diff > 1e-6)
// {
// cout << i << ": " << IM(i) << " " << diff << endl;
// }
// }
// }
// };
// {
// MatrixXi _1;
// VectorXi _2;
// cout << "clean: remesh_self_intersections" << endl;
// remesh_self_intersections(V, F, { false,false,false }, CV, CF, _1, _2, IM);
// for_each(CF.data(), CF.data() + CF.size(), [&IM](int & a) {a = IM(a); });
// //validate_IM(V,CV,IM);
// cout << "clean: remove_unreferenced" << endl;
// {
// MatrixXi oldCF = CF;
// unique_simplices(oldCF, CF);
// }
// MatrixXd oldCV = CV;
// MatrixXi oldCF = CF;
// VectorXi nIM;
// remove_unreferenced(oldCV, oldCF, CV, CF, nIM);
// // reindex nIM through IM
// for_each(IM.data(), IM.data() + IM.size(), [&nIM](int & a) {a = a >= 0 ? nIM(a) : a; });
// //validate_IM(V,CV,IM);
// }
// MatrixXd TV;
// MatrixXi TT;
// {
// MatrixXi _1;
// // c convex hull
// // Y no boundary steiners
// // p polygon input
// // T1e-16 sometimes helps tetgen
// cout << "clean: tetrahedralize" << endl;
// writeOBJ("CVCF.obj", CV, CF);
// CDTParam params;
// params.flags = "CYT1e-16";
// params.use_bounding_box = true;
// if (cdt(CV, CF, params, TV, TT, _1) != 0)
// {
// cout << REDRUM("CDT failed.") << endl;
// return false;
// }
// //writeMESH("TVTT.mesh",TV,TT,MatrixXi());
// }
// {
// MatrixXd BC;
// barycenter(TV, TT, BC);
// VectorXd W;
// cout << "clean: winding_number" << endl;
// winding_number(V, F, BC, W);
// W = W.array().abs();
// const double thresh = 0.5;
// const int count = (W.array() > thresh).cast<int>().sum();
// MatrixXi CT(count, TT.cols());
// int c = 0;
// for (int t = 0; t < TT.rows(); t++)
// {
// if (W(t) > thresh)
// {
// CT.row(c++) = TT.row(t);
// }
// }
// assert(c == count);
// boundary_facets(CT, CF);
// //writeMESH("CVCTCF.mesh",TV,CT,CF);
// cout << "clean: remove_unreferenced" << endl;
// // Force all original vertices to be referenced
// MatrixXi FF = F;
// for_each(FF.data(), FF.data() + FF.size(), [&IM](int & a) {a = IM(a); });
// int ncf = CF.rows();
// MatrixXi ref(ncf + FF.rows(), 3);
// ref << CF, FF;
// VectorXi nIM;
// remove_unreferenced(TV, ref, CV, CF, nIM);
// // Only keep boundary faces
// CF.conservativeResize(ncf, 3);
// cout << "clean: IM.minCoeff(): " << IM.minCoeff() << endl;
// // reindex nIM through IM
// for_each(IM.data(), IM.data() + IM.size(), [&nIM](int & a) {a = a >= 0 ? nIM(a) : a; });
// cout << "clean: IM.minCoeff(): " << IM.minCoeff() << endl;
// //validate_IM(V,CV,IM);
// }
// return true;
//}
int main(int argc, char *argv[])
{
using namespace Eigen;
using namespace std;
double t1[] = { 90, 115, 10, 90, 135, 10, 90, 115, 0, 90, 115, 0, 90, 135, 10, 90, 135, 0, 90, 115, 10, 110, 115, 10, 110, 135, 10, 90, 135, 10, 90, 115, 10, 110, 135, 10, 90, 115, 0, 110, 115, 0, 110, 115, 10, 90, 115, 10, 90, 115, 0, 110, 115, 10, 90, 135, 0, 110, 135, 0, 90, 115, 0, 90, 115, 0, 110, 135, 0, 110, 115, 0, 90, 135, 10, 110, 135, 10, 90, 135, 0, 90, 135, 0, 110, 135, 10, 110, 135, 0, 110, 115, 0, 110, 135, 0, 110, 135, 10, 110, 115, 10, 110, 115, 0, 110, 135, 10 };
int t2[] = { 0, 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 };
double t3[] = { 100, 102, 10, 100, 122, 10, 100, 102, 0, 100, 102, 0, 100, 122, 10, 100, 122, 0, 100, 102, 10, 120, 102, 10, 120, 122, 10, 100, 122, 10, 100, 102, 10, 120, 122, 10, 100, 102, 0, 120, 102, 0, 120, 102, 10, 100, 102, 10, 100, 102, 0, 120, 102, 10, 100, 122, 0, 120, 122, 0, 100, 102, 0, 100, 102, 0, 120, 122, 0, 120, 102, 0, 100, 122, 10, 120, 122, 10, 100, 122, 0, 100, 122, 0, 120, 122, 10, 120, 122, 0, 120, 102, 0, 120, 122, 0, 120, 122, 10, 120, 102, 10, 120, 102, 0, 120, 122, 10 };
int t4[] = { 0, 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 };
double* pVC = 0;
int vc = 0;
int* pFC = 0;
int fc = 0;
DoBooleanOperation(t1, 108, t2, 36, t3, 108, t4, 36, 1, &pVC, &vc, &pFC, &fc);
if (argc != 5)
{
cout <<
"In STL A" << endl <<
"In STL B" << endl <<
"Out STL" << endl <<
"Operation [+,-,&] (union, subtract, intersect)" << endl;
return -1;
}
igl::readSTL(argv[1], VA, FA, NA);
igl::readSTL(argv[2], VB, FB, NB);
igl::MeshBooleanType booleanType(igl::MESH_BOOLEAN_TYPE_UNION);
if (strcmp(argv[4], "-") == 0)
{
booleanType = igl::MESH_BOOLEAN_TYPE_MINUS;
}
else if (strcmp(argv[4], "&") == 0)
{
booleanType = igl::MESH_BOOLEAN_TYPE_INTERSECT;
}
igl::copyleft::cgal::mesh_boolean(VA, FA, VB, FB, booleanType, VC, FC, J);
igl::writeSTL(argv[3], VC, FC, true);
if(false)
{
// Plot the mesh with pseudocolors
igl::opengl::glfw::Viewer viewer;
// Initialize
update(viewer);
viewer.data().show_lines = true;
viewer.core.camera_dnear = 3.9;
viewer.launch();
}
}