Xinqi Bao's Git
6 #include "Configurations.hh"
9 Slicing::Slicing(Stl
* stl
, Layer
* layer
) : Slicing(*stl
, *layer
) {}
11 Slicing::Slicing(Stl
& stl
, Layer
& layer
)
12 : z(layer
.getZ()), loops(layer
.getLoops()), triangles(stl
.getLevel(z
))
19 // return true if this pair of vectors cross this level
20 inline bool Slicing::isCross(const Vec3d
& v1
, const Vec3d
& v2
) const
21 { // not include at the vertex
22 return (v1
.z
+ thr
< z
&& z
< v2
.z
- thr
) ||
23 (v2
.z
+ thr
< z
&& z
< v1
.z
- thr
);
26 // check if the cut through both triangles' sides
27 bool Slicing::isSectionExist(Cross
& cr
) const
29 for (const auto& temp
: intersections
)
30 if (temp
.equal(cr
)) return true;
34 // push every sections into the vetor
35 // Total as 10 different cross cases, side-side for 3, vertex-side for 3,
36 // vertex-vertex for 3, through whole facet for 1.
37 // More rare cases been checked more, saving whole execution time.
38 void Slicing::intersection()
40 intersections
.reserve(triangles
.size() >> 1);
41 for (const auto& t
: triangles
)
43 if (isCross(t
.v1
, t
.v2
))
45 if (isCross(t
.v1
, t
.v3
))
46 { // Cross v1--v2 and v1--v3
47 intersections
.push_back(
48 Cross(Vec2d(t
.v1
, t
.v2
, z
), Vec2d(t
.v1
, t
.v3
, z
)));
51 else if (fabs(z
- t
.v3
.z
) < thr
)
52 { // Cross v1--v2 and v3
53 intersections
.push_back(
54 Cross(Vec2d(t
.v1
, t
.v2
, z
), Vec2d(t
.v3
.x
, t
.v3
.y
)));
58 { // definitely cross v1--v2 and v2--v3
59 intersections
.push_back(
60 Cross(Vec2d(t
.v1
, t
.v2
, z
), Vec2d(t
.v2
, t
.v3
, z
)));
64 else if (isCross(t
.v1
, t
.v3
))
66 if (fabs(z
- t
.v2
.z
) < thr
)
67 { // Cross v1--v3 and v2
68 intersections
.push_back(
69 Cross(Vec2d(t
.v1
, t
.v3
, z
), Vec2d(t
.v2
.x
, t
.v2
.y
)));
73 { // Cross v1--v3 and v2--v3
74 intersections
.push_back(
75 Cross(Vec2d(t
.v1
, t
.v3
, z
), Vec2d(t
.v2
, t
.v3
, z
)));
79 else if (fabs(z
- t
.v1
.z
) < thr
)
81 if (isCross(t
.v2
, t
.v3
))
82 { // Cross v1 and v2--v3
83 intersections
.push_back(
84 Cross(Vec2d(t
.v1
.x
, t
.v1
.y
), Vec2d(t
.v2
, t
.v3
, z
)));
87 else if (fabs(z
- t
.v2
.z
) < thr
)
89 if (fabs(z
- t
.v3
.z
) < thr
) // Through the whole facet
93 Cross
cr(Vec2d(t
.v1
.x
, t
.v1
.y
), Vec2d(t
.v2
.x
, t
.v2
.y
));
94 if (!isSectionExist(cr
)) intersections
.push_back(cr
);
98 else if (fabs(z
- t
.v3
.z
) < thr
)
99 { // v1 and v3, still need "if()", because may only cross one
101 Cross
cr(Vec2d(t
.v1
.x
, t
.v1
.y
), Vec2d(t
.v3
.x
, t
.v3
.y
));
102 if (!isSectionExist(cr
)) intersections
.push_back(cr
);
106 else if (fabs(z
- t
.v2
.z
) < thr
)
108 if (fabs(z
- t
.v3
.z
) < thr
)
110 Cross
cr(Vec2d(t
.v2
.x
, t
.v2
.y
), Vec2d(t
.v3
.x
, t
.v3
.y
));
111 if (!isSectionExist(cr
)) intersections
.push_back(cr
);
118 // Combine intersections to loops
119 // TODO: vector.erase() take to much time, need to be optimied
120 void Slicing::mkLoop()
122 if (intersections
.empty()) return;
123 #if 1 // implement with subLoops
124 Vec2d
p1(intersections
.back().p1
);
125 Vec2d
p2(intersections
.back().p2
);
126 Loop
lp(p1
, intersections
.size() >> 1);
128 intersections
.erase(intersections
.end() - 1);
129 while (!intersections
.empty())
131 if (!lp
.isCompleted())
134 for (int i
= intersections
.size() - 1; i
>= 0; i
--)
136 if (p2
.equal(intersections
[i
].p1
))
139 p2
= intersections
[i
].p2
;
140 intersections
.erase(intersections
.begin() + i
);
144 else if (p2
.equal(intersections
[i
].p2
))
147 p2
= intersections
[i
].p1
;
148 intersections
.erase(intersections
.begin() + i
);
155 for (const auto& temp : intersections) {
156 if (p2.equal(temp.p1)) {
159 intersections.erase(intersections.begin() + i);
163 else if (p2.equal(temp.p2)) {
166 intersections.erase(intersections.begin() + i);
174 { // creat a new Loop
175 bool isSubLoop
= false;
176 for (int i
= loops
.size() - 1; i
>= 0; i
--)
178 isSubLoop
= loops
[i
].checkSubLoop(lp
);
179 if (isSubLoop
) break;
180 if (lp
.checkSubLoop(loops
[i
]))
182 // lp.subLoops.push_back(loops[i]);
183 loops
.erase(loops
.begin() + i
);
186 if (!isSubLoop
) loops
.push_back(lp
);
187 p1
= intersections
.back().p1
;
188 p2
= intersections
.back().p2
;
189 lp
= Loop(p1
, intersections
.size() >> 1);
191 intersections
.erase(intersections
.end());
194 bool isSubLoop
= false;
195 for (int i
= loops
.size() - 1; i
>= 0; i
--)
197 isSubLoop
= loops
[i
].checkSubLoop(lp
);
198 if (isSubLoop
) break;
199 if (lp
.checkSubLoop(loops
[i
]))
201 // lp.subLoops.push_back(loops[i]);
202 loops
.erase(loops
.begin() + i
);
205 if (!isSubLoop
) loops
.push_back(lp
);
208 //Vec2d p1(intersections.front().p1);
209 //Vec2d p2(intersections.front().p2);
210 Vec2d
p1(intersections
.back().p1
);
211 Vec2d
p2(intersections
.back().p2
);
212 loops
.push_back(Loop(p1
, intersections
.size() >> 1));
213 loops
.back().add(p2
);
214 //intersections.erase(intersections.begin());
215 intersections
.pop_back();
216 while (!intersections
.empty()) {
217 if (!loops
.back().isCompleted()) {
219 for (int i
= intersections
.size()-1; i
>= 0; i
--) {
220 if (p2
.equal(intersections
[i
].p1
)) {
222 p2
= intersections
[i
].p2
;
223 intersections
.erase(intersections
.begin() + i
);
224 loops
.back().add(p2
);
227 else if (p2
.equal(intersections
[i
].p2
)) {
229 p2
= intersections
[i
].p1
;
230 intersections
.erase(intersections
.begin() + i
);
231 loops
.back().add(p2
);
237 for (const auto& temp : intersections) {
238 if (p2.equal(temp.p1)) {
241 intersections.erase(intersections.begin() + i);
242 loops.back().add(p2);
245 else if (p2.equal(temp.p2)) {
248 intersections.erase(intersections.begin() + i);
249 loops.back().add(p2);
255 else { //creat a new Loop
256 //p1 = intersections.front().p1;
257 //p2 = intersections.front().p2;
258 p1
= intersections
.back().p1
;
259 p2
= intersections
.back().p2
;
260 loops
.push_back(Loop(p1
, intersections
.size() >> 1));
261 loops
.back().add(p2
);
262 //intersections.erase(intersections.begin());
263 intersections
.pop_back();
267 #if 0 // Why take more time??
268 int size
= intersections
.size();
269 bool* bools
= new bool[size
];
270 for (int i
= 0; i
< size
; i
++)
272 Vec2d
p1(intersections
.front().p1
);
273 Vec2d
p2(intersections
.front().p2
);
274 loops
.push_back(Loop(p1
, intersections
.size() >> 1));
275 loops
.back().add(p2
);
277 for (int i
= 1; i
< size
; i
++) {
278 if (!loops
.back().isCompleted()) {
279 for (int k
= 0; k
< size
; k
++) {
282 if (p2
.equal(intersections
[k
].p1
)) {
284 p2
= intersections
[k
].p2
;
285 loops
.back().add(p2
);
289 else if (p2
.equal(intersections
[k
].p2
)) {
291 p2
= intersections
[k
].p1
;
292 loops
.back().add(p2
);
299 for (int k
= 0; k
< size
; k
++) {
302 Vec2d
p1(intersections
[k
].p1
);
303 Vec2d
p2(intersections
[k
].p2
);
304 loops
.push_back(Loop(p1
, intersections
.size() >> 1));
305 loops
.back().add(p2
);
312 for (auto& lp
: loops
) lp
.optimize();
315 void Slicing::printVector() const
317 cout
<< "\nZ as " << z
<< ":\tcontain " << intersections
.size() << "\n";
318 for (const auto& i
: intersections
)
324 void Slicing::printLoop() const
326 for (int i
= 0; i
< loops
.size(); i
++)
328 cout
<< "\nLoop " << i
<< ", Z as " << z
<< '\n';