| 1 |
/************************************************************************* |
| 2 |
* 2022 Tóth János * |
| 3 |
* * |
| 4 |
* This program is free software; it is licensed under the terms of the * |
| 5 |
* GNU General Public License v2 or later. See file LICENSE for details. * |
| 6 |
*************************************************************************/ |
| 7 |
|
| 8 |
#include "PolyMesh.hpp" |
| 9 |
|
| 10 |
#include <lib/base/Math.hpp> |
| 11 |
|
| 12 |
#include <boost/python/tuple.hpp> |
| 13 |
|
| 14 |
#include <stdarg.h> |
| 15 |
#include <stdio.h> |
| 16 |
|
| 17 |
namespace yade { |
| 18 |
namespace ymport { |
| 19 |
namespace foamfile { |
| 20 |
|
| 21 |
PolyMesh::PolyMesh(const std::string& baseDirPath, bool patchAsWall, bool emptyAsWall) |
| 22 |
: m_baseDirPath(baseDirPath) |
| 23 |
{ |
| 24 |
if (boost::filesystem::is_directory(m_baseDirPath) == false) { error("Not a directory."); } |
| 25 |
|
| 26 |
// NOTE: Support for .gz files? |
| 27 |
m_pointsParser = std::make_unique<PointsParser>(m_baseDirPath + "/points"); |
| 28 |
|
| 29 |
m_pointsParser->parse(); |
| 30 |
|
| 31 |
m_points = m_pointsParser->points(); |
| 32 |
|
| 33 |
if (m_points.size() == 0) { error("No points were found."); } |
| 34 |
|
| 35 |
m_facesParser = std::make_unique<FacesParser>(m_baseDirPath + "/faces", m_points.size()); |
| 36 |
|
| 37 |
m_facesParser->parse(); |
| 38 |
|
| 39 |
m_faces = m_facesParser->faces(); |
| 40 |
|
| 41 |
if (m_faces.size() == 0) { error("No faces were found."); } |
| 42 |
|
| 43 |
m_boundaryParser = std::make_unique<BoundaryParser>(m_baseDirPath + "/boundary"); |
| 44 |
|
| 45 |
m_boundaryParser->parse(); |
| 46 |
|
| 47 |
m_boundaries = m_boundaryParser->boundaries(); |
| 48 |
|
| 49 |
if (m_boundaries.size() == 0) { error("No boundaries were found."); } |
| 50 |
|
| 51 |
buildFacets(patchAsWall, emptyAsWall); |
| 52 |
} |
| 53 |
|
| 54 |
void PolyMesh::buildFacets(bool patchAsWall, bool emptyAsWall) |
| 55 |
{ |
| 56 |
for (auto& b : m_boundaries) { |
| 57 |
if (b.type == BoundaryParser::Boundary::Type::PATCH && patchAsWall == false) { continue; } |
| 58 |
|
| 59 |
if (b.type == BoundaryParser::Boundary::Type::EMPTY && emptyAsWall == false) { continue; } |
| 60 |
|
| 61 |
std::vector<FacesParser::Face> faces(b.nFaces); |
| 62 |
for (int i = 0; i < b.nFaces; ++i) { |
| 63 |
faces.emplace_back(m_faces[b.startFace + i]); |
| 64 |
} |
| 65 |
|
| 66 |
for (FacesParser::Face f : faces) { |
| 67 |
int v0i = f.v0; |
| 68 |
int v1i = f.v1; |
| 69 |
int v2i = f.v2; |
| 70 |
int v3i = f.v3; |
| 71 |
|
| 72 |
Vector3r f0p0(m_points[v0i].x, m_points[v0i].y, m_points[v0i].z); |
| 73 |
Vector3r f0p1(m_points[v1i].x, m_points[v1i].y, m_points[v1i].z); |
| 74 |
Vector3r f0p2(m_points[v2i].x, m_points[v2i].y, m_points[v2i].z); |
| 75 |
|
| 76 |
m_facetCoords.append(py::make_tuple(f0p0, f0p1, f0p2)); |
| 77 |
|
| 78 |
Vector3r f1p0(m_points[v2i].x, m_points[v2i].y, m_points[v2i].z); |
| 79 |
Vector3r f1p1(m_points[v3i].x, m_points[v3i].y, m_points[v3i].z); |
| 80 |
Vector3r f1p2(m_points[v0i].x, m_points[v0i].y, m_points[v0i].z); |
| 81 |
|
| 82 |
m_facetCoords.append(py::make_tuple(f1p0, f1p1, f1p2)); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
void PolyMesh::error(const char* fmt, ...) |
| 88 |
{ |
| 89 |
va_list args; |
| 90 |
|
| 91 |
va_start(args, fmt); |
| 92 |
size_t size = vsnprintf(NULL, 0, fmt, args); |
| 93 |
va_end(args); |
| 94 |
|
| 95 |
std::vector<char> data(size + 1); |
| 96 |
|
| 97 |
va_start(args, fmt); |
| 98 |
vsnprintf(data.data(), data.size(), fmt, args); |
| 99 |
va_end(args); |
| 100 |
|
| 101 |
std::string message = m_baseDirPath + ": "; |
| 102 |
message.append(std::string(data.begin(), data.end())); |
| 103 |
|
| 104 |
throw std::runtime_error(message); |
| 105 |
} |
| 106 |
|
| 107 |
} // namespace foamfile |
| 108 |
} // namespace ymport |
| 109 |
} // namespace yade |