MySources/patternIO.cpp

142 lines
4.7 KiB
C++
Executable File

#include "patternIO.hpp"
#include "trianglepatterngeometry.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
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';
}
fileStream << "#Patterns" << '\n';
}
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);
}
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.save(
std::filesystem::path(outputDirectoryPath)
.append(std::to_string(patterns[patternIndex].name) + ".ply").string());
tiled.save(std::filesystem::path(outputDirectoryPath)
.append("tiled_" +
std::to_string(patterns[patternIndex].name) +
".ply").string());
}
}
void PatternIO::save(const std::string &filePath,
const PatternSet &patternSet) {
std::ofstream fileStream;
if (!std::filesystem::exists(filePath)) {
fileStream.open(filePath);
write(fileStream, patternSet.nodes);
} else {
fileStream.open(filePath, std::ios_base::app);
}
for (const Pattern &pattern : patternSet.patterns) {
write(fileStream, pattern);
fileStream << '\n';
}
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();
}
std::ofstream fileStream;
fileStream.open(filePath, std::ios_base::app);
write(fileStream, pattern);
fileStream << '\n';
}
void PatternIO::load(const std::string &filepath,
std::vector<vcg::Point3d> &vertices,
std::vector<Pattern> &patterns, const int &startIndex,
const int &endIndex) {
std::ifstream fileStream(filepath);
std::string line;
int patternIndex = 0;
while (std::getline(fileStream, line) && patternIndex <= endIndex) {
// std::cout << line << '\n';
std::istringstream iss(line);
char lineID;
iss >> lineID;
if (lineID == 'n') {
double x, y, z = 0;
iss >> x >> y;
// std::cout << x << " " << y << z << '\n';
vertices.push_back(vcg::Point3d(x, y, z));
} else if (lineID == 'p') {
patternIndex++;
if (startIndex > patternIndex) {
continue;
}
Pattern pattern;
iss >> pattern.name;
int numberOfEdges;
iss >> numberOfEdges;
for (int edgeIndex = 0; edgeIndex < numberOfEdges; edgeIndex++) {
size_t ni0, ni1;
iss >> ni0 >> ni1;
vcg::Point2i edge(ni0, ni1);
pattern.edges.push_back(edge);
}
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();
}
}
}