-
Notifications
You must be signed in to change notification settings - Fork 1
/
cut.cc
74 lines (65 loc) · 2.44 KB
/
cut.cc
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
#include "cut.h"
using namespace Slic3r;
void cut(
float *positions,
unsigned int numPositions,
unsigned int *faces,
unsigned int numFaces,
float *position,
float *quaternion,
float *scale,
float *outPositions,
unsigned int *numOutPositions,
unsigned int *outFaces,
unsigned int *numOutFaces
) {
TriangleMesh mesh(positions, faces, numFaces);
TransformationMatrix matrix = TransformationMatrix::multiply(
TransformationMatrix::mat_translation(position[0], position[1], position[2]),
TransformationMatrix::multiply(
TransformationMatrix::mat_rotation(quaternion[0], quaternion[1], quaternion[2], quaternion[3]),
TransformationMatrix::mat_scale(scale[0], scale[1], scale[2])
));
TransformationMatrix matrixInverse = matrix.inverse();
mesh.transform(matrixInverse);
TriangleMesh upper;
TriangleMesh lower;
mesh.cut(Axis::Y, 0, &upper, &lower);
upper.repair();
lower.repair();
{
const std::vector<Pointf3> &upperPositions = upper.vertices();
numOutPositions[0] = 0;
for (size_t i = 0; i < upperPositions.size(); i++) {
const Pointf3 &p = matrix.transform(upperPositions[i]);
outPositions[numOutPositions[0]++] = p.x;
outPositions[numOutPositions[0]++] = p.y;
outPositions[numOutPositions[0]++] = p.z;
}
const std::vector<Point3> &upperIndices = upper.facets();
memcpy(outFaces, upperIndices.data(), upperIndices.size()*3*sizeof(upperIndices[0]));
numOutFaces[0] = upperIndices.size()*3;
}
{
const std::vector<Pointf3> &upperPositions = lower.vertices();
numOutPositions[1] = 0;
for (size_t i = 0; i < upperPositions.size(); i++) {
const Pointf3 &p = matrix.transform(upperPositions[i]);
outPositions[numOutPositions[0] + numOutPositions[1]++] = p.x;
outPositions[numOutPositions[0] + numOutPositions[1]++] = p.y;
outPositions[numOutPositions[0] + numOutPositions[1]++] = p.z;
}
const std::vector<Point3> &upperIndices = lower.facets();
memcpy(outFaces + numOutFaces[0], upperIndices.data(), upperIndices.size()*3*sizeof(upperIndices[1]));
numOutFaces[1] = upperIndices.size()*3;
}
/* csgjs_plane plane;
plane.normal = csgjs_vector(0, 1, 0);
plane.w = 0.5;
csgjs_polygon polygon;
std::vector<csgjs_polygon> coplanarFront;
std::vector<csgjs_polygon> coplanarBack;
std::vector<csgjs_polygon> front;
std::vector<csgjs_polygon> back;
plane.splitPolygon(polygon, coplanarFront, coplanarBack, front, back); */
}