Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ int main(int argc, const char * argv[]) {
}

int seed = cfr.getInt("seed");
Universe::seedRNG(seed);
std::string fID = cfr.getString("fileID");
int measurements = cfr.getInt("measurements");
std::string impGeomString = cfr.getString("importGeom");
Expand All @@ -42,10 +43,12 @@ int main(int argc, const char * argv[]) {

if (impGeom) {
std::string geomFn = Universe::getGeometryFilename(targetVolume, slices, seed);
if (geomFn != "") {
Universe::importGeometry(geomFn);
} else {
printf("No suitable geometry file found. Creating new Universe...\n");
// Always attempt to import if impGeom is true.
// The importGeometry function will handle file open errors.
bool import_successful = Universe::importGeometry(geomFn);
if (!import_successful) {
printf("Geometry import failed for file: %s. Will attempt to create a new universe.\n", geomFn.c_str());
// Universe::imported will be false, so the subsequent check will handle universe creation.
}
}

Expand Down
67 changes: 59 additions & 8 deletions universe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,43 +454,75 @@ void Universe::exportGeometry(std::string geometryFilename) {
std::cout << geometryFilename << "\n";
}

void Universe::importGeometry(std::string geometryFilename) {
bool Universe::importGeometry(std::string geometryFilename) {
std::ifstream infile(geometryFilename.c_str());
assert(!infile.fail());
if (infile.fail()) {
printf("Error: Could not open geometry file: %s\n", geometryFilename.c_str());
return false;
}
int line;

int nV;
infile >> nV;
if (infile.fail()) {
printf("Error: Failed to read nV from %s.\n", geometryFilename.c_str());
return false;
}
std::vector<Vertex::Label> vs(nV);

int maxTime = 0;
for (int i = 0; i < nV; i++) {
infile >> line;
if (infile.fail()) {
printf("Error: Failed to read vertex time for vertex %d from %s.\n", i, geometryFilename.c_str());
return false;
}
auto v = Vertex::create();
v->time = line;
vs.at(i) = v;
if (v->time > maxTime) maxTime = v->time;
}
infile >> line;
assert(line == nV);
if (infile.fail()) {
printf("Error: Failed to read vertex count confirmation from %s.\n", geometryFilename.c_str());
return false;
}
if (line != nV) {
printf("Error: Vertex count mismatch in %s. Expected %d, found %d.\n", geometryFilename.c_str(), nV, line);
return false;
}

nSlices = maxTime+1;
sliceSizes.resize(maxTime+1);
std::fill(sliceSizes.begin(), sliceSizes.end(), 0);

int nT;
infile >> nT;
if (infile.fail()) {
printf("Error: Failed to read nT from %s.\n", geometryFilename.c_str());
return false;
}
for (int i = 0; i < nT; i++) {
auto t = Triangle::create();

int tVs[3];
for (int j = 0; j < 3; j++) {
infile >> tVs[j];
if (infile.fail()) {
printf("Error: Failed to read vertex %d for triangle %d from %s.\n", j, i, geometryFilename.c_str());
// Cleanup partially created triangle t? For now, just return.
return false;
}
}

int tNeighb[3];
for (int j = 0; j < 3; j++) {
infile >> tNeighb[j];
if (infile.fail()) {
printf("Error: Failed to read neighbor %d for triangle %d from %s.\n", j, i, geometryFilename.c_str());
// Cleanup partially created triangle t? For now, just return.
return false;
}
}

t->setVertices(tVs[0], tVs[1], tVs[2]);
Expand All @@ -499,12 +531,29 @@ void Universe::importGeometry(std::string geometryFilename) {
trianglesAll.add(t);
}
infile >> line;
assert(line == nT);
if (infile.fail()) {
printf("Error: Failed to read triangle count confirmation from %s.\n", geometryFilename.c_str());
return false;
}
if (line != nT) {
printf("Error: Triangle count mismatch in %s. Expected %d, found %d.\n", geometryFilename.c_str(), nT, line);
return false;
}

printf("read %s\n", geometryFilename.c_str());
printf("Successfully read %s\n", geometryFilename.c_str());

for (auto v : vs) sliceSizes.at(v->time)++;
if (sphere) assert(sliceSizes.at(0) == 3);
if (sphere) {
// This assertion might be too strict if import can fail before full initialization
// For now, keeping it, but might need revisiting if it causes issues with partial valid imports
// that should still be rejected.
if (sliceSizes.empty() || sliceSizes.at(0) != 3) {
printf("Warning: Spherical universe import consistency check failed for slice 0 size (expected 3, got %d). File: %s\n", sliceSizes.empty() ? 0 : sliceSizes.at(0), geometryFilename.c_str());
// Depending on strictness, could return false here.
// For now, it's a warning.
}
}


for (auto t : trianglesAll) {
if (t->isUpwards()) {
Expand All @@ -522,9 +571,11 @@ void Universe::importGeometry(std::string geometryFilename) {
}
}

check();
check(); // Assuming check() doesn't auto-fail but uses asserts that would terminate.
// If check() could also indicate failure, its result should be handled.

imported = true;
Universe::imported = true;
return true;
}

std::string Universe::getGeometryFilename(int targetVolume, int slices, int seed) {
Expand Down
4 changes: 3 additions & 1 deletion universe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ class Universe {
static void updateTriangleData();

static void exportGeometry(std::string geometryFilename);
static void importGeometry(std::string geometryFilename);
static bool importGeometry(std::string geometryFilename); // Changed return type to bool
static std::string getGeometryFilename(int targetVolume, int slices, int seed);

static void seedRNG(unsigned int seed);

static std::vector<Vertex::Label> vertices;
static std::vector<Link::Label> links;
static std::vector<Triangle::Label> triangles;
Expand Down