MySources/patternIO.cpp

142 lines
4.7 KiB
C++
Raw Normal View History

2020-11-27 11:47:21 +01:00
#include "patternIO.hpp"
2021-03-15 18:04:29 +01:00
#include "trianglepatterngeometry.hpp"
2020-11-27 11:47:21 +01:00
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
2021-03-15 18:04:29 +01:00
void PatternIO::write(std::ofstream &fileStream, const Pattern &pattern) {
fileStream << "p " << pattern.name << " " << pattern.edges.size();
for (const vcg::Point2i &edge : pattern.edges) {
fileStream << " " << edge[0] << " " << edge[1];
}
}
void PatternIO::write(std::ofstream &fileStream,
const std::vector<vcg::Point3d> &vertices) {
fileStream << "#Nodes" << '\n';
for (vcg::Point3d node : vertices) {
fileStream << "n " << node.X() << " " << node.Y() << " " << node.Z()
<< '\n';
2020-11-27 11:47:21 +01:00
}
2021-03-15 18:04:29 +01:00
fileStream << "#Patterns" << '\n';
}
2020-11-27 11:47:21 +01:00
2021-03-15 18:04:29 +01:00
void PatternIO::exportPLY(const std::string &inputFilePath,
const int &startIndex, const int &endIndex) {
assert(std::filesystem::path(inputFilePath).extension() == ".patt");
const std::string outputDir =
std::filesystem::path(inputFilePath)
.parent_path()
.append("PLYFiles")
.append(std::to_string(startIndex) + "_" + std::to_string(endIndex))
.string();
exportPLY(inputFilePath, outputDir, startIndex, endIndex);
}
2020-11-27 11:47:21 +01:00
2021-03-15 18:04:29 +01:00
void PatternIO::exportPLY(const std::string &patternSetFilePath,
const std::string &outputDirectoryPath,
const int &startIndex, const int &endIndex) {
assert(std::filesystem::path(patternSetFilePath).extension() == ".patt");
std::vector<vcg::Point3d> vertices;
std::vector<Pattern> patterns;
load(patternSetFilePath, vertices, patterns, startIndex, endIndex);
std::filesystem::create_directories(outputDirectoryPath);
for (int patternIndex = 0; patternIndex < patterns.size(); patternIndex++) {
PatternGeometry p;
p.add(vertices, patterns[patternIndex].edges);
auto tiled = p.createTile(p);
p.savePly(
std::filesystem::path(outputDirectoryPath)
.append(std::to_string(patterns[patternIndex].name) + ".ply"));
tiled.savePly(std::filesystem::path(outputDirectoryPath)
.append("tiled_" +
std::to_string(patterns[patternIndex].name) +
".ply"));
2020-11-27 11:47:21 +01:00
}
}
2021-03-15 18:04:29 +01:00
void PatternIO::save(const std::string &filePath,
const PatternSet &patternSet) {
2020-11-27 11:47:21 +01:00
std::ofstream fileStream;
2021-03-15 18:04:29 +01:00
if (!std::filesystem::exists(filePath)) {
fileStream.open(filePath);
write(fileStream, patternSet.nodes);
2020-11-27 11:47:21 +01:00
} else {
2021-03-15 18:04:29 +01:00
fileStream.open(filePath, std::ios_base::app);
2020-11-27 11:47:21 +01:00
}
2021-03-15 18:04:29 +01:00
for (const Pattern &pattern : patternSet.patterns) {
write(fileStream, pattern);
fileStream << '\n';
2020-11-27 11:47:21 +01:00
}
2021-03-15 18:04:29 +01:00
fileStream.close();
}
void PatternIO::save(std::ofstream &fileStream, const Pattern &pattern) {
assert(fileStream.is_open());
write(fileStream, pattern);
fileStream << '\n';
}
void PatternIO::save(const std::string &filePath, const Pattern &pattern) {
if (!std::filesystem::exists(filePath)) {
std::cerr << "File must already exist. The node information must already "
"be in the file."
<< '\n';
std::terminate();
2020-11-27 11:47:21 +01:00
}
2021-03-15 18:04:29 +01:00
std::ofstream fileStream;
fileStream.open(filePath, std::ios_base::app);
write(fileStream, pattern);
fileStream << '\n';
2020-11-27 11:47:21 +01:00
}
2021-03-15 18:04:29 +01:00
void PatternIO::load(const std::string &filepath,
std::vector<vcg::Point3d> &vertices,
std::vector<Pattern> &patterns, const int &startIndex,
const int &endIndex) {
2020-11-27 11:47:21 +01:00
std::ifstream fileStream(filepath);
std::string line;
2021-03-15 18:04:29 +01:00
int patternIndex = 0;
while (std::getline(fileStream, line) && patternIndex <= endIndex) {
// std::cout << line << '\n';
2020-11-27 11:47:21 +01:00
std::istringstream iss(line);
char lineID;
iss >> lineID;
if (lineID == 'n') {
2021-03-15 18:04:29 +01:00
double x, y, z = 0;
2020-11-27 11:47:21 +01:00
iss >> x >> y;
2021-03-15 18:04:29 +01:00
// std::cout << x << " " << y << z << '\n';
vertices.push_back(vcg::Point3d(x, y, z));
2020-11-27 11:47:21 +01:00
} else if (lineID == 'p') {
2021-03-15 18:04:29 +01:00
patternIndex++;
if (startIndex > patternIndex) {
continue;
2020-11-27 11:47:21 +01:00
}
2021-03-15 18:04:29 +01:00
Pattern pattern;
iss >> pattern.name;
int numberOfEdges;
iss >> numberOfEdges;
for (int edgeIndex = 0; edgeIndex < numberOfEdges; edgeIndex++) {
2020-11-27 11:47:21 +01:00
size_t ni0, ni1;
iss >> ni0 >> ni1;
vcg::Point2i edge(ni0, ni1);
pattern.edges.push_back(edge);
}
2021-03-15 18:04:29 +01:00
patterns.push_back(pattern);
// PatternGeometry fpg;
// fpg.add(vertices, pattern.edges);
// fpg.registerForDrawing(glm::vec3(1, 0, 0));
// PatternGeometry tiledPattern = fpg.createTile(fpg);
// tiledPattern.deleteDanglingVertices();
// tiledPattern.registerForDrawing();
// polyscope::show();
// polyscope::removeAllStructures();
2020-11-27 11:47:21 +01:00
}
}
}