-
Notifications
You must be signed in to change notification settings - Fork 0
/
ep_feeder_combiner.cpp
295 lines (236 loc) · 10.1 KB
/
ep_feeder_combiner.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
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <iomanip>
#include <algorithm>
#include <numeric>
int MAX_WEFT_FEEDERS = 12;
int REGULATOR_HOOK = 22;
int PICK_VARIATION_HOOK = 17;
class JacquardDesignReader {
private:
std::string filename;
int16_t totalPicks; // Total Picks
int16_t totalHooks; // Total Design Hooks
std::vector<std::vector<bool>> designData; // Store hooks state for each card
int calculateCardSizeBytes() const {
return (totalHooks + 32 + 7) / 8; // Rounds up to the nearest byte
}
std::vector<uint8_t> convertCardToByteVector(const std::vector<bool>& cardHooks) const {
int cardSizeBytes = calculateCardSizeBytes();
std::vector<uint8_t> cardData(cardSizeBytes, 0);
for (int bitPos = 0; bitPos < cardHooks.size(); ++bitPos) {
if (cardHooks[bitPos]) {
int byteIndex = bitPos / 8;
int bitIndex = (bitPos % 8);
cardData[byteIndex] |= (1 << bitIndex);
}
}
return cardData;
}
public:
explicit JacquardDesignReader(const std::string &file) : filename(file), totalPicks(0), totalHooks(0) {}
bool loadFile() {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << filename << std::endl;
return false;
}
// Read Total Picks (at address 0x03)
file.seekg(0x03, std::ios::beg);
file.read(reinterpret_cast<char *>(&totalPicks), sizeof(totalPicks));
// Read Total Hooks (at address 0x20)
file.seekg(0x20, std::ios::beg);
file.read(reinterpret_cast<char *>(&totalHooks), sizeof(totalHooks));
if (totalPicks <= 0 || totalHooks <= 0) {
std::cerr << "Error: Invalid picks or hooks count in file." << std::endl;
return false;
}
file.seekg(0x22, std::ios::beg);
int cardSizeBytes = (totalHooks + 32 + 7) / 8;
for (int i = 0; i < totalPicks; ++i) {
std::vector<bool> cardHooks(totalHooks + 32, false);
std::vector<uint8_t> cardData(cardSizeBytes, 0);
file.read(reinterpret_cast<char *>(cardData.data()), cardSizeBytes);
// Convert each byte to individual bits and store as hook states
for (int bitPos = 0; bitPos < totalHooks + 32; ++bitPos) {
int byteIndex = bitPos / 8;
int bitIndex = (bitPos % 8); // Bit order within byte
cardHooks[bitPos] = (cardData[byteIndex] & (1 << bitIndex)) != 0;
}
designData.push_back(cardHooks);
}
file.close();
return true;
}
void printDesignData() const {
std::cout << "Total Picks: " << totalPicks << std::endl;
std::cout << "Total Hooks: " << totalHooks << std::endl;
}
std::vector<bool> getCard(int cardNumber) const {
if (cardNumber < 1 || cardNumber > totalPicks) {
throw std::out_of_range("Error: Card number out of range.");
}
return designData[cardNumber - 1]; // Card numbers are 1-based, vector is 0-based
}
void displayCard(int cardNumber) const {
try {
auto cardHooks = getCard(cardNumber);
std::cout << "Hooks for Card " << cardNumber << ": ";
for (bool hook : cardHooks) {
std::cout << (hook ? '1' : '0');
}
std::cout << std::endl;
} catch (const std::out_of_range &e) {
std::cerr << e.what() << std::endl;
}
}
int getTotalPicks() const { return totalPicks; }
int getTotalHooks() const { return totalHooks; }
bool getHookState(int cardNumber, int hookNumber) const {// Current state of hook (0,1 )
return designData[cardNumber-1][hookNumber-1];
}
bool getNextHookState(int cardNumber, int hookNumber) const {
if (hookNumber > getTotalPicks()) {
return 0;
}
return designData[cardNumber][hookNumber - 1];
}
void setHookState(int cardNumber, int hookNumber, int value) {
designData[cardNumber-1][hookNumber-1] = value;
}
std::vector<int> getHookActiveCards(int hookNumber) {// Card Numbers for which hook is active
std::vector<int> hookActiveCards;
for (int cardNum = 1 ; cardNum <= totalPicks; cardNum++) {
if (designData[cardNum - 1][hookNumber - 1]) {
hookActiveCards.push_back(cardNum);
}
}
return hookActiveCards;
}
std::vector<bool> getActiveWeft() const {
std::vector<bool> activeWeft(12,0);
for(int card_no = 1; card_no < getTotalPicks() + 1; card_no++) {
std::vector<bool> card = getCard(card_no);
for ( int i = 1;i<=MAX_WEFT_FEEDERS ; i++) {
if (getHookState(card_no, i)) {
activeWeft[i-1] = 1;
}
}
}
return activeWeft;
}
int getActiveWeftCount() const {
std::vector<bool> active = getActiveWeft();
int count = 0;
count = std::accumulate(active.begin(), active.end(), 0);
return count;
}
bool writeToFile(const std::string &outputFilename) const {
std::ifstream inputFile(filename, std::ios::binary);
if (!inputFile.is_open()) {
std::cerr << "Error: Could not open file " << filename << " for reading." << std::endl;
return false;
}
std::ofstream outputFile(outputFilename, std::ios::binary);
if (!outputFile.is_open()) {
std::cerr << "Error: Could not open file " << outputFilename << " for writing." << std::endl;
return false;
}
inputFile.seekg(0, std::ios::end); // Move to the end of the file
std::streampos originalFileSize = inputFile.tellg(); // Get the file size
inputFile.seekg(0, std::ios::beg); // Move back to the beginning for further processing
// Copy everything before the design data (header and part of the file before card data)
char buffer[1024];
inputFile.read(buffer, 0x22); // Read up to the start of the design data (address 0x22)
outputFile.write(buffer, inputFile.gcount());
int cardSizeBytes = calculateCardSizeBytes();
for (int i = 0; i < totalPicks; ++i) {
std::vector<uint8_t> cardData = convertCardToByteVector(designData[i]);
outputFile.write(reinterpret_cast<const char*>(cardData.data()), cardData.size());
}
// Copy everything after the card data (if there's any trailing data in the file)
inputFile.seekg(0x22 + totalPicks * cardSizeBytes, std::ios::beg);
while (inputFile.read(buffer, sizeof(buffer))) {
outputFile.write(buffer, inputFile.gcount());
}
std::streampos currentOutputFileSize = outputFile.tellp();
std::streampos paddingNeeded = originalFileSize - currentOutputFileSize;
if (paddingNeeded > 0) {
// Write exactly the remaining padding bytes as zero to match the original file size
std::vector<char> paddingBuffer(static_cast<size_t>(paddingNeeded), 0);
outputFile.write(paddingBuffer.data(), paddingBuffer.size());
}
inputFile.close();
outputFile.close();
return true;
}
};
void loadOrCreateConfig(const std::string& configFile) {
// Check if the file exists by attempting to open it
std::ifstream file(configFile);
if (!file.is_open()) {
// File does not exist, so create it with default values
std::ofstream outFile(configFile);
if (!outFile.is_open()) {
std::cerr << "Error: Could not create config file: " << configFile << std::endl;
return;
}
// Write default values to the new config file
outFile << "MAX_WEFT_FEEDERS=" << MAX_WEFT_FEEDERS << std::endl;
outFile << "REGULATOR_HOOK=" << REGULATOR_HOOK << std::endl;
outFile << "PICK_VARIATION_HOOK=" << PICK_VARIATION_HOOK << std::endl;
outFile.close();
std::cout << "Config file created with default values: " << configFile << std::endl;
} else {
// Config file exists, so read values from it
std::string line;
while (std::getline(file, line)) {
std::istringstream lineStream(line);
std::string key;
if (std::getline(lineStream, key, '=')) {
std::string valueStr;
if (std::getline(lineStream, valueStr)) {
int value = std::stoi(valueStr);
if (key == "MAX_WEFT_FEEDERS") {
MAX_WEFT_FEEDERS = value;
} else if(key == "REGULATOR_HOOK") {
REGULATOR_HOOK = value;
} else if (key == "PICK_VARIATION_HOOK") {
PICK_VARIATION_HOOK = value;
}
}
}
}
file.close();
std::cout << "Config file loaded: " << configFile << std::endl;
}
}
int main() {
loadOrCreateConfig("config.txt");
std::string filename = "INPUT.EP"; // Adjust this filename as needed
JacquardDesignReader reader(filename);
if (reader.loadFile()) {
reader.writeToFile("output.ep");
int keep,remove;
std::cout << "Enter Feeder to Keep" << std::endl;
std::cin >> keep;
std::cout << "Enter Feeder to Remove" << std::endl;
std::cin >> remove;
std::cout << "Processing..." << std::endl;
for ( int card = 1; card < reader.getTotalPicks() + 1 ; card++) {
std::vector<bool> card_value = reader.getCard(card);
bool to_remove = card_value[remove - 1];
if (to_remove) {
reader.setHookState(card, remove, 0);
reader.setHookState(card, keep, 1);
}
}
reader.writeToFile("OUTPUT.EP");
} else {
std::cerr << "Failed to load design file." << std::endl;
return 1;
}
return 0;
}