feat(meshIO): added linear rescale capability to meshIO

loaded meshes can be linearly rescaled (so for example a unit sphere can be rescaled to a sphere of any radius)
This commit is contained in:
2025-04-25 11:13:15 -04:00
parent 9a76c447f1
commit 2b3330b4f6
2 changed files with 39 additions and 5 deletions

View File

@@ -2,7 +2,7 @@
//
// Copyright (C) 2025 -- The 4D-STAR Collaboration
// File Author: Emily Boudreaux
// Last Modified: February 16, 2025
// Last Modified: March 18, 2025
//
// 4DSSE is free software; you can use it and/or modify
// it under the terms and restrictions the GNU General Library Public
@@ -22,11 +22,12 @@
#include <string>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "meshIO.h"
MeshIO::MeshIO(const std::string mesh_file)
MeshIO::MeshIO(const std::string &mesh_file, double scale_factor)
{
mesh_file_ = mesh_file;
std::ifstream mesh_stream(mesh_file);
@@ -37,8 +38,9 @@ MeshIO::MeshIO(const std::string mesh_file)
}
else
{
mesh_ = mfem::Mesh(mesh_stream, 1, 2);
mesh_ = mfem::Mesh(mesh_stream, 1, 10);
loaded_ = true;
if (scale_factor != 1.0) { LinearRescale(scale_factor); }
}
}
@@ -46,6 +48,32 @@ MeshIO::~MeshIO()
{
}
void MeshIO::LinearRescale(double scale_factor) {
if (!loaded_) {
throw std::runtime_error("Mesh not loaded before rescaling.");
}
if (scale_factor <= 0.0) {
throw std::invalid_argument("Scale factor must be positive.");
}
// Ensure there are nodes even for linear order meshes
mesh_.EnsureNodes();
const mfem::GridFunction *nodes = mesh_.GetNodes();
double *node_data = nodes->GetData(); // THIS IS KEY
int data_size = nodes->Size();
for (int i = 0; i < data_size; ++i) {
node_data[i] *= scale_factor;
}
// nodes->Update(); /updates the fespace
mesh_.NodesUpdated();
}
bool MeshIO::IsLoaded() const
{
return loaded_;

View File

@@ -2,7 +2,7 @@
//
// Copyright (C) 2025 -- The 4D-STAR Collaboration
// File Author: Emily Boudreaux
// Last Modified: February 16, 2025
// Last Modified: March 18, 2025
//
// 4DSSE is free software; you can use it and/or modify
// it under the terms and restrictions the GNU General Library Public
@@ -39,13 +39,19 @@ public:
* @brief Constructor that initializes the MeshIO object with a mesh file.
* @param mesh_file The name of the mesh file.
*/
MeshIO(const std::string mesh_file);
MeshIO(const std::string &mesh_file, double scale_factor = 1.0);
/**
* @brief Destructor for the MeshIO class.
*/
~MeshIO();
/**
* @brief Rescale the mesh by a linear factor.
* @param scale_factor The factor by which to scale the mesh.
*/
void LinearRescale(double scale_factor);
/**
* @brief Get the mesh object.
* @return Reference to the mesh object.