Xinqi Bao's Git

XbSlicer, re-organized the entire project
[XbSlicer.git] / src / Layer.cc
1
2 #include "Layer.hh"
3
4 #include <cmath>
5
6 #include "Configurations.hh"
7 using namespace std;
8
9 double Command::e = 0;
10 void Command::G0_high(std::ostream& s, double z)
11 {
12 s << "G0 F6600 Z" << z << '\n';
13 }
14
15 void Command::resetE(std::ostream& s)
16 {
17 e = 0;
18 s << "G92 E0 ; reset the expected extruder position\n";
19 }
20
21 std::ostream& operator<<(std::ostream& s, const Command& c)
22 {
23 switch (c.cmd)
24 { // TODO: figure out the more efficient way, without switch!!
25 case 0:
26 return s << "G0 F6600 X" << c.x << " Y" << c.y << '\n';
27 case 1:
28 Command::e += c.de;
29 return s << "G1 F783 X" << c.x << " Y" << c.y << " E" << Command::e
30 << '\n';
31 }
32 }
33
34 void Command::G0_high(BufferWrite& buf, double z)
35 {
36 buf << "G0 F6600 Z" << z << '\n';
37 }
38
39 void Command::resetE(BufferWrite& buf)
40 {
41 e = 0;
42 buf << "G92 E0 ; reset the expected extruder position\n";
43 }
44
45 BufferWrite& operator<<(BufferWrite& buf, const Command& c)
46 {
47 switch (c.cmd)
48 { // TODO: figure out the more efficient way, without switch!!
49 case 0:
50 buf.writeG0(c.x, c.y);
51 return buf;
52 case 1:
53 Command::e += c.de;
54 buf.writeG1(c.x, c.y, Command::e);
55 return buf;
56 }
57 }
58
59 Command::Command(uint16_t cmd, double x, double y) : cmd(cmd), x(x), y(y) {}
60
61 Layer::Layer(double z) : z(z) {}
62
63 double Layer::getZ() const { return z; }
64
65 double& Layer::getMinX() { return minX; }
66
67 double& Layer::getMaxX() { return maxX; }
68
69 vector<Loop>& Layer::getLoops() { return loops; }
70
71 vector<Command>& Layer::getCommands() { return commands; }
72
73 vector<vector<Cross>>& Layer::getParts() { return parts; }
74
75 void Layer::commandsOut(ostream& s) const
76 {
77 Command::G0_high(s, z);
78 Command::resetE(s);
79 for (const auto& c : commands) s << c;
80 s.flush();
81 }
82
83 void Layer::commandsOut(BufferWrite& buf) const
84 {
85 Command::G0_high(buf, z);
86 Command::resetE(buf);
87 for (const auto& c : commands) buf << c;
88 // buf.flush();
89 }
90
91 void Layer::generateDe()
92 {
93 tx = commands.front().x;
94 ty = commands.front().y;
95 for (auto& tc : commands)
96 {
97 if (tc.cmd == 1)
98 tc.de = sqrt(pow(tc.x - tx, 2) + pow(tc.y - ty, 2)) * ef;
99 tx = tc.x;
100 ty = tc.y;
101 }
102 }