117 lines
4.0 KiB
C++
117 lines
4.0 KiB
C++
#include "patternIO.hpp"
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
PatternIO::PatternIO() {}
|
|
|
|
void PatternIO::save(const std::string &filepath,
|
|
const PatternSet &patternSet) {
|
|
std::ofstream fileStream;
|
|
if (std::filesystem::exists(filepath)) {
|
|
fileStream.open(filepath, std::ios_base::app);
|
|
} else {
|
|
fileStream.open(filepath);
|
|
fileStream << "#Nodes" << std::endl;
|
|
for (vcg::Point2d node : patternSet.nodes) {
|
|
fileStream << "n " << node.X() << " " << node.Y() << std::endl;
|
|
}
|
|
fileStream << "#Patterns" << std::endl;
|
|
}
|
|
|
|
for (const Pattern &pattern : patternSet.patterns) {
|
|
fileStream << "p " << pattern.labels.size() << " " << pattern.edges.size()
|
|
<< " ";
|
|
for (size_t labelIndex = 0; labelIndex < pattern.labels.size() - 1;
|
|
labelIndex++) {
|
|
fileStream << pattern.labels[labelIndex] << " ";
|
|
}
|
|
fileStream << pattern.labels[pattern.labels.size() - 1] << " ";
|
|
|
|
for (const vcg::Point2i &edge : pattern.edges) {
|
|
fileStream << " " << edge[0] << " " << edge[1] << " ";
|
|
}
|
|
fileStream << std::endl;
|
|
}
|
|
}
|
|
|
|
void PatternIO::save(const std::string &filepath, const Pattern &pattern) {
|
|
std::ofstream fileStream;
|
|
if (std::filesystem::exists(filepath)) {
|
|
fileStream.open(filepath, std::ios_base::app);
|
|
} else {
|
|
fileStream.open(filepath);
|
|
fileStream << "#Nodes" << std::endl;
|
|
fileStream << "#Patterns" << std::endl;
|
|
}
|
|
|
|
fileStream << "p " << pattern.labels.size() << " " << pattern.edges.size()
|
|
<< " ";
|
|
for (size_t labelIndex = 0; labelIndex < pattern.labels.size() - 1;
|
|
labelIndex++) {
|
|
fileStream << pattern.labels[labelIndex] << " ";
|
|
}
|
|
fileStream << pattern.labels[pattern.labels.size() - 1] << " ";
|
|
|
|
for (const vcg::Point2i &edge : pattern.edges) {
|
|
fileStream << " " << edge[0] << " " << edge[1] << " ";
|
|
}
|
|
fileStream << std::endl;
|
|
}
|
|
|
|
void PatternIO::load(const std::string &filepath, PatternSet &patternSet,
|
|
const std::vector<PatternLabel> &targetLabels) {
|
|
std::ifstream fileStream(filepath);
|
|
std::string line;
|
|
std::vector<PatternLabel> sortedTargetPatternLabels(targetLabels);
|
|
std::sort(sortedTargetPatternLabels.begin(), sortedTargetPatternLabels.end());
|
|
while (std::getline(fileStream, line)) {
|
|
std::cout << line << std::endl;
|
|
std::istringstream iss(line);
|
|
char lineID;
|
|
iss >> lineID;
|
|
if (lineID == 'n') {
|
|
double x, y;
|
|
iss >> x >> y;
|
|
std::cout << x << " " << y << std::endl;
|
|
} else if (lineID == 'p') {
|
|
Pattern pattern;
|
|
size_t numberOfLabels, numberOfEdges;
|
|
iss >> numberOfLabels >> numberOfEdges;
|
|
std::cout << "NumberOfLabels:" << numberOfLabels << std::endl;
|
|
std::cout << "NumberOfEdges:" << numberOfEdges << std::endl;
|
|
std::vector<PatternLabel> patternLabels;
|
|
for (size_t labelIndex = 0; labelIndex < numberOfLabels; labelIndex++) {
|
|
size_t patternLabel;
|
|
iss >> patternLabel;
|
|
PatternLabel pl = static_cast<PatternLabel>(patternLabel);
|
|
std::cout << "Pattern label read:" << patternLabel << std::endl;
|
|
patternLabels.push_back(pl);
|
|
}
|
|
if (!targetLabels.empty()) {
|
|
std::sort(patternLabels.begin(), patternLabels.end());
|
|
std::vector<PatternLabel> labelIntersection;
|
|
std::set_intersection(patternLabels.begin(), patternLabels.end(),
|
|
sortedTargetPatternLabels.begin(),
|
|
sortedTargetPatternLabels.end(),
|
|
labelIntersection.begin());
|
|
if (!labelIntersection.empty()) {
|
|
pattern.labels = patternLabels;
|
|
} else {
|
|
continue;
|
|
}
|
|
} else {
|
|
pattern.labels = patternLabels;
|
|
}
|
|
for (size_t edgeIndex = 0; edgeIndex < numberOfEdges; edgeIndex++) {
|
|
size_t ni0, ni1;
|
|
iss >> ni0 >> ni1;
|
|
vcg::Point2i edge(ni0, ni1);
|
|
pattern.edges.push_back(edge);
|
|
std::cout << "Node indices read:" << ni0 << "," << ni1 << std::endl;
|
|
}
|
|
}
|
|
}
|
|
}
|