-
Notifications
You must be signed in to change notification settings - Fork 4
/
results_generator.cpp
138 lines (126 loc) · 3.65 KB
/
results_generator.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
#include "stdafx.h"
#include <algorithm>
#include <functional>
#include "errors.h"
#include "results_generator.h"
namespace Denisenko {
namespace Raskroy {
void ResultsGenerator::RemoveExostedSizes(void)
{
for (auto s = 0; s <= 1; s++)
{
auto pSize = m_sizes[s].begin();
while (pSize != m_sizes[s].end())
{
auto pOtherSize = pSize->other_sizes.begin();
while (pOtherSize != pSize->other_sizes.end())
{
if (std::all_of(pOtherSize->parts.begin(),
pOtherSize->parts.end(),
[this](Part * part) {
return m_remains[part->AmountOffset] == 0;
}))
{
pSize->other_sizes.erase(pOtherSize);
pOtherSize = pSize->other_sizes.begin();
}
else
pOtherSize++;
}
if (pSize->other_sizes.empty())
{
m_sizes[s].erase(pSize);
pSize = m_sizes[s].begin();
}
else
{
pSize->other_sizes.SetMin();
pSize++;
}
}
}
}
void ResultsGenerator::Begin(Parts &parts, const Parts &sheets)
{
m_remains.clear();
for (auto s = 0; s <= 1; s++)
{
m_sizes[s].clear();
for (auto pPart = parts.begin(); pPart != parts.end(); pPart++)
m_sizes[s].AddPart(*pPart, s);
// order from big to small
std::sort(m_sizes[s].begin(), m_sizes[s].end(), std::greater_equal<Size>());
for (auto pSize = m_sizes[s].begin(); pSize != m_sizes[s].end(); pSize++)
{
std::sort(pSize->other_sizes.begin(), pSize->other_sizes.end(),
std::greater_equal<OtherSize>());
// set pointer to the smallest size
pSize->other_sizes.SetMin();
}
}
m_sheets = sheets;
}
float ResultsGenerator::GetPercentCompleted()
{
float total = (float)(m_sheets.size() * (m_sizes[0].size() + m_sizes[1].size()));
if(total == 0.0f)
{
return 100.0f;
}
else
{
return (float)m_layout2d.GetCompletedCounter() / total * 100.0f;
}
}
bool ResultsGenerator::NextResult(Result& out)
{
// check if there are still remaining parts
auto pRemain = m_remains.begin();
for (; pRemain != m_remains.end(); pRemain++)
if (*pRemain > 0)
break;
if (pRemain == m_remains.end())
return false; // no more parts
Result bestResult;
Amounts bestRashod(m_remains.size());
bool first = true;
m_layout2d.ResetCompletedCounter();
for (auto pSheet = m_sheets.begin(); pSheet != m_sheets.end(); pSheet++)
{
Stat stat;
stat.MakeZero();
OldLayoutResult raskroy;
Amounts rashod(m_remains.size());
if (!m_layout2d.Optimize(pSheet->rect, stat, 0, raskroy, rashod))
continue;
if (bestResult.Statistics < stat || first) {
bestResult.amount = m_remains / rashod;
if (ControlRemains && bestResult.amount > pSheet->Amount)
continue; // not enough sheets
bestResult.Statistics = stat;
bestResult.raskroy = raskroy;
bestResult.sheet = pSheet;
bestRashod = rashod;
first = false;
}
}
if (first)
throw CannotSetPartsException(&m_sheets, m_sizes, &m_remains);
m_remains -= bestRashod * bestResult.amount;
RemoveExostedSizes();
if (ControlRemains)
bestResult.sheet->Amount -= bestResult.amount;
#ifdef _DEBUG
CheckResult(bestResult);
#endif
out = bestResult;
return true;
}
void ResultsGenerator::CheckResult(const Result& result)
{
Stat stat;
result.raskroy.CheckAndCalcStat(this->get_SawThickness(), result.sheet->rect, &stat);
assert(result.Statistics.IsEqual(stat, 1000000.0));
}
} // namespace Denisenko
} // namespace Raskroy