使用 gmsh 在复杂几何体中生成六面体网格

问题描述 投票:0回答:0

我编写了以下代码以在复杂几何体中生成块网格,但我在 gmsh::model::mesh::generate(3) 中遇到错误。这是错误

gmsh.exe 中 0x00007FFE19C7CD29 处的未处理异常:Microsoft C++ 异常:std::basic_string 位于内存位置 0x0000002CEF0FEC08.

这是输出的一部分。

信息:在曲线循环 1 中启动子循环 1525(你确定吗?) 信息:在曲线循环 1 中开始子循环 1526(你确定吗?) 信息:在曲线循环 1 中开始子循环 1527(你确定吗?) 信息:在曲线循环 1 中开始子循环 1528(你确定吗?) 信息:在曲线循环 1 中开始子循环 1529(你确定吗?) 信息:在曲线循环 1 中开始子循环 1530(你确定吗?) 信息:在曲线循环 1 中开始子循环 1531(你确定吗?) 信息:在曲线循环 1 中开始子循环 1532(你确定吗?) 信息:在曲线循环 1 中开始子循环 1533(你确定吗?) 信息:在曲线循环 1 中开始子循环 1534(你确定吗?) 信息:在曲线循环 1 中开始子循环 1535(你确定吗?) 错误:网格模块未编译

也许提及我的表面有 1536 个三角形会有所帮助。似乎它已经遍历了 1536 个三角形并试图超出界限。此外,当我评论 gmsh::model::mesh::generate(3) 时,代码运行并生成一个包含一堆点但缺少连接的 txt 文件。代码如何在不调用生成函数的情况下生成点。

我重新安装了库,仍然是同样的问题。我使用 vcpkg 安装了库。我非常感谢你的帮助。

#include <gmsh.h>
#include <gmshc.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>


struct Point {
    float x;
    float y;
    float z;
};

struct Triangle {
    float x1, y1, z1;
    float x2, y2, z2;
    float x3, y3, z3;
    float nx, ny, nz;
};



std::vector<Triangle> readSTLASCII(const std::string& filename) {
    std::vector<Triangle> triangles;
    std::ifstream file(filename);
    if (!file.is_open()) {
        std::cerr << "Error: cannot open file " << filename << std::endl;
        return triangles;
    }

    std::string line;
    std::stringstream ss;
    std::string facet, normal, outer, loop, vertex, endloop, endfacet;
    float x, y, z;
    Triangle triangle;
    int triangle_count = 0;
    while (getline(file, line)) {
        ss.clear();
        ss.str(line);
        ss >> facet >> normal >> triangle.nx >> triangle.ny >> triangle.nz;
        if (ss.fail() || facet != "facet" || normal != "normal") {
            continue;
        }

        getline(file, line);
        ss.clear();
        ss.str(line);
        ss >> outer >> loop;
        if (ss.fail() || outer != "outer" || loop != "loop") {
            continue;
        }

        //std::cout << "Triangle " << triangle_count << ": ";
        for (int i = 0; i < 3; ++i) {
            getline(file, line);
            ss.clear();
            ss.str(line);
            ss >> vertex >> x >> y >> z;
            if (ss.fail() || vertex != "vertex") {
                break;
            }
            switch (i) {
            case 0:
                triangle.x1 = x;
                triangle.y1 = y;
                triangle.z1 = z;
                // std::cout << x << "," << y << "," << z << ";";
                break;
            case 1:
                triangle.x2 = x;
                triangle.y2 = y;
                triangle.z2 = z;
                //std::cout << x << "," << y << "," << z << ";";
                break;
            case 2:
                triangle.x3 = x;
                triangle.y3 = y;
                triangle.z3 = z;
                //std::cout << x << "," << y << "," << z << ";";
                break;
            }
        }

        // std::cout << triangle.nx << "," << triangle.ny << "," << triangle.nz << std::endl;
        getline(file, line);
        ss.clear();
        ss.str(line);
        ss >> endloop;
        if (ss.fail() || endloop != "endloop") {
            continue;
        }

        getline(file, line);
        ss.clear();
        ss.str(line);
        ss >> endfacet;
        if (ss.fail() || endfacet != "endfacet") {
            continue;
        }

        triangles.push_back(triangle);
        triangle_count++;
    }

    file.close();
    return triangles;
}


void generateMesh(const std::vector<Triangle>& triangles) {
    // Initialize Gmsh
    gmsh::initialize();

    // Create a new model
    gmsh::model::add("hex_mesh");

    // Define the geometry
    std::vector<std::size_t> pointTags;
    std::vector<int> curveTags;
    for (const auto& t : triangles) {
        std::vector<std::size_t> currentPoints;
        currentPoints.push_back(gmsh::model::geo::addPoint(t.x1, t.y1, t.z1, 1e-3));
        currentPoints.push_back(gmsh::model::geo::addPoint(t.x2, t.y2, t.z2, 1e-3));
        currentPoints.push_back(gmsh::model::geo::addPoint(t.x3, t.y3, t.z3, 1e-3));
        std::vector<int> currentCurves;
        for (std::size_t i = 0; i < currentPoints.size(); ++i) {
            std::size_t j = (i + 1) % currentPoints.size();
            currentCurves.push_back(gmsh::model::geo::addLine(currentPoints[i], currentPoints[j]));
        }
        curveTags.insert(curveTags.end(), currentCurves.begin(), currentCurves.end());
        pointTags.insert(pointTags.end(), currentPoints.begin(), currentPoints.end());
    }
    gmsh::model::geo::addCurveLoop(curveTags);
    gmsh::model::geo::addPlaneSurface({ 1 });

    // Define the mesh
    gmsh::model::geo::synchronize();

    // Set the characteristic length of the mesh
    gmsh::vectorpair entities;
    for (auto tag : pointTags) {
        entities.emplace_back(0, tag);
    }
    gmsh::model::mesh::setSize(entities, 0.1);

    // Generate the mesh
    gmsh::model::mesh::generate(3);

    // Save the mesh to a file
    gmsh::write("hex_mesh.msh");

    // Finalize Gmsh
    gmsh::finalize();
}

int main() {

    std::vector<Triangle>Triangles = readSTLASCII("cube.stl");
    generateMesh(Triangles);
    return 0;
}

c++ block mesh gmsh
© www.soinside.com 2019 - 2024. All rights reserved.