You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.9 KiB
99 lines
2.9 KiB
#include "OutputSurfMesh.h"
|
|
#include <fstream>
|
|
#include <stdexcept>
|
|
#include <cmath>
|
|
#include <filesystem>
|
|
#include "vtr.h"
|
|
#include <omp.h> // include this if not already
|
|
|
|
void OutputSurfMesh::readFromFile(const std::string& filename)
|
|
{
|
|
|
|
std::ifstream infile(filename);
|
|
if (!infile.is_open())
|
|
{
|
|
std::cerr << "[ERROR] Could not open: " << filename << "\n";
|
|
throw std::runtime_error("Failed to open file: " + filename);
|
|
}
|
|
|
|
infile >> scale >> num_nodes;
|
|
|
|
xyznode.resize(3, std::vector<double>(num_nodes));
|
|
for (int i = 0; i < num_nodes; ++i) {
|
|
double x, y, z;
|
|
infile >> x >> y >> z;
|
|
xyznode[0][i] = x * scale;
|
|
xyznode[1][i] = y * scale;
|
|
xyznode[2][i] = z * scale;
|
|
}
|
|
|
|
std::string dummy;
|
|
std::getline(infile, dummy); // consume leftover newline
|
|
|
|
infile >> num_triangles;
|
|
triangles.resize(3, std::vector<int>(num_triangles));
|
|
for (int i = 0; i < num_triangles; ++i) {
|
|
int n0, n1, n2;
|
|
infile >> n0 >> n1 >> n2;
|
|
triangles[0][i] = n0;
|
|
triangles[1][i] = n1;
|
|
triangles[2][i] = n2;
|
|
}
|
|
|
|
infile.close();
|
|
}
|
|
|
|
|
|
|
|
void OutputSurfMesh::printSummary() const {
|
|
std::cout << "-------- Summary --------- " << "\n";
|
|
std::cout << "Scale: " << scale << "\n";
|
|
std::cout << "Number of Nodes: " << num_nodes << "\n";
|
|
std::cout << "Number of Triangles: " << num_triangles << "\n";
|
|
std::cout << "First Triangle: " << triangles[0][0] << "," << triangles[1][0] << "," << triangles[2][0] << "\n";
|
|
std::cout << "Last Triangle: " << triangles[0][num_triangles-1] << "," << triangles[1][num_triangles-1] << "," << triangles[2][num_triangles-1] << "\n";
|
|
}
|
|
|
|
const std::vector<double>& OutputSurfMesh::getNode(int idx) const {
|
|
static std::vector<double> node(3);
|
|
node[0] = xyznode[0][idx];
|
|
node[1] = xyznode[1][idx];
|
|
node[2] = xyznode[2][idx];
|
|
return node;
|
|
}
|
|
|
|
std::vector<int> OutputSurfMesh::getTriangle(int idx) const {
|
|
return {triangles[0][idx], triangles[1][idx], triangles[2][idx]};
|
|
}
|
|
|
|
std::vector<double> OutputSurfMesh::getNormal(int idx) const {
|
|
return {normals[idx][0], normals[idx][1], normals[idx][2]};
|
|
}
|
|
|
|
void OutputSurfMesh::computeTriangleNormals()
|
|
{
|
|
normals.resize(num_triangles, std::vector<double>(3, 0.0));
|
|
|
|
#pragma omp parallel for schedule(static)
|
|
for (int i = 0; i < num_triangles; ++i)
|
|
{
|
|
int n0 = triangles[0][i];
|
|
int n1 = triangles[1][i];
|
|
int n2 = triangles[2][i];
|
|
|
|
// Get coordinates of the triangle nodes
|
|
vtr node0(xyznode[0][n0],xyznode[1][n0],xyznode[2][n0]);
|
|
vtr node1(xyznode[0][n1],xyznode[1][n1],xyznode[2][n1]);
|
|
vtr node2(xyznode[0][n2],xyznode[1][n2],xyznode[2][n2]);
|
|
|
|
vtr lvtr0 = node1 - node0;
|
|
vtr lvtr1 = node2 - node0;
|
|
vtr normal = lvtr0 * lvtr1;
|
|
normal.unitvtr();
|
|
|
|
normals[i][0] = normal.getx();
|
|
normals[i][1] = normal.gety();
|
|
normals[i][2] = normal.getz();
|
|
}
|
|
}
|
|
|
|
|