macOS配置OpenMesh

安装OpenMesh库

使用Homebrew安装,非常简单,一个命令搞定

1
brew install open-mesh

安装后可能不知道库的具体位置,通过下面命令

1
brew info open-mesh

可以查看库的信息,其中就包括了库的路径,我得到的是

1
/usr/local/Cellar/open-mesh/6.3

用Qt Creator测试OpenMesh

Creator的安装这里不再赘述,新建控制台项目,如test。
在test.pro中添加保护目录和库文件

1
2
3
4
INCLUDEPATH += /usr/local/Cellar/open-mesh/6.3/include

LIBS += /usr/local/Cellar/open-mesh/6.3/lib/libOpenMeshCore.a
LIBS += /usr/local/Cellar/open-mesh/6.3/lib/libOpenMeshTools.a

main函数中的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <QCoreApplication>
#include <iostream>
#include <string>
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/IO/reader/OBJReader.hh>
#include <OpenMesh/Core/IO/writer/OBJWriter.hh>

typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

OpenMesh::IO::_OBJReader_();
OpenMesh::IO::_OBJWriter_();
MyMesh mesh;
std::string file = "bunny.obj";
if (!OpenMesh::IO::read_mesh(mesh, file))
{
std::cout << "Failed to read mesh" << std::endl;
return a.exec();
}

if (!OpenMesh::IO::write_mesh(mesh, "out.obj"))
{
std::cout << "Failed to save mesh" << std::endl;
return a.exec();
}
return a.exec();
}

程序正确运行。

注意:一开始程序中没加

1
2
#include <OpenMesh/Core/IO/reader/OBJReader.hh>
#include <OpenMesh/Core/IO/writer/OBJWriter.hh>

1
2
OpenMesh::IO::_OBJReader_();
OpenMesh::IO::_OBJWriter_();

运行时报错[OpenMesh::IO::_IOManager_] No reading modules available!,搜索这里发现需要加上面的内容。

在Cmake中使用OpenMesh

在Cmake中使用OpenMesh,CmakeList.txt如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
cmake_minimum_required(VERSION 3.9)
project(testOpenMeshCMake)

set(SOURCE_FILES main.cpp)

set(CMAKE_CXX_STANDARD 11)

add_executable(testOpenMeshCMake main.cpp)

target_link_libraries (testOpenMeshCMake
/usr/local/Cellar/open-mesh/6.3/lib/libOpenMeshTools.6.3.dylib
/usr/local/Cellar/open-mesh/6.3/lib/libOpenMeshCore.6.3.dylib
)

Reference