当前位置:网站首页>How to use openmesh to realize the conversion between 3D files of different formats
How to use openmesh to realize the conversion between 3D files of different formats
2022-06-29 16:40:00 【CSU kayah】
The possible transformations are as follows

( See the reference document at the end of the text for the source link )
This article example formats text (ASCII) Of stl File to binary format (Binary).
Catalog
step 1 To configure openmesh Environmental Science
step 4 Run the program to get the output file
step 1 To configure openmesh Environmental Science
You can refer to
Environment configuration | VS2017 To configure OpenMesh Source code and environment
step2 Code implementation
notes :debug Run down the same way ,release No way , I don't know why
#include <iostream>
#include <iterator>
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Utils/getopt.h>
// ----------------------------------------------------------------------------
using namespace OpenMesh;
// ----------------------------------------------------------------------------
typedef TriMesh_ArrayKernelT<> MyMesh;
// ----------------------------------------------------------------------------
#define CHKROPT( Option ) \
std::cout << " provides " << #Option \
<< (ropt.check(IO::Options:: Option)?": yes\n":": no\n")
#define CHKWOPT( Option ) \
std::cout << " write " << #Option \
<< (wopt.check(IO::Options:: Option)?": yes\n":": no\n")
#define MESHOPT( msg, tf ) \
std::cout << " " << msg << ": " << ((tf)?"yes\n":"no\n")
// ----------------------------------------------------------------------------
void parse_commandline(int _argc, char **_argv, MyMesh& _mesh,
IO::Options &ropt, IO::Options &wopt);
void usage_and_exit(int xcode);
// ----------------------------------------------------------------------------
int main(int argc, char **argv)
{
MyMesh mesh;
IO::Options ropt, wopt;
// -------------------- evaluate commandline
parse_commandline(argc, argv, mesh, ropt, wopt);
// -------------------- read mesh
if (!IO::read_mesh(mesh, argv[optind], ropt))
{
std::cerr << "Error loading mesh from file " << argv[optind] << std::endl;
return 1;
}
// -------------------- show options
std::cout << "File " << argv[optind] << std::endl;
std::cout << " is binary: "
<< (ropt.check(IO::Options::Binary) ? " yes\n" : " no\n");
std::cout << " byte order: ";
if (ropt.check(IO::Options::Swap))
std::cout << "swapped\n";
else if (ropt.check(IO::Options::LSB))
std::cout << "little endian\n";
else if (ropt.check(IO::Options::MSB))
std::cout << "big endian\n";
else
std::cout << "don't care\n";
std::cout << " provides VertexNormal"
<< ( // strange layout for doxygen
ropt.check(IO::Options::VertexNormal)
? ": yes\n" : ": no\n");
CHKROPT(VertexColor);
CHKROPT(VertexTexCoord);
CHKROPT(FaceNormal);
CHKROPT(FaceColor);
// -------------------- mesh stats
std::cout << "# Vertices: " << mesh.n_vertices() << std::endl;
std::cout << "# Edges : " << mesh.n_faces() << std::endl;
std::cout << "# Faces : " << mesh.n_faces() << std::endl;
// -------------------- show write options
std::cout << "Selected write options:\n";
std::cout << " use binary: "
<< (wopt.check(IO::Options::Binary) ? " yes\n" : " no\n");
std::cout << " byte order: ";
if (wopt.check(IO::Options::Swap))
std::cout << "swapped\n";
else if (wopt.check(IO::Options::LSB))
std::cout << "little endian\n";
else if (wopt.check(IO::Options::MSB))
std::cout << "big endian\n";
else
std::cout << "don't care\n";
std::cout << " write VertexNormal"
<< (wopt.check(IO::Options::VertexNormal) ? ": yes\n" : ": no\n");
CHKWOPT(VertexColor);
CHKWOPT(VertexTexCoord);
CHKWOPT(FaceNormal);
CHKWOPT(FaceColor);
// -------------------- show mesh capabilities
std::cout << "Mesh supports\n";
MESHOPT("vertex normals", mesh.has_vertex_normals());
MESHOPT("vertex colors", mesh.has_vertex_colors());
MESHOPT("texcoords", mesh.has_vertex_texcoords2D());
MESHOPT("face normals", mesh.has_face_normals());
MESHOPT("face colors", mesh.has_face_colors());
// -------------------- write mesh
std::cout << "Write mesh to " << argv[optind + 1] << "..";
if (!IO::write_mesh(mesh, argv[optind + 1], wopt))
{
std::cerr << "Error" << std::endl;
std::cerr << "Possible reasons:\n";
std::cerr << "1. Chosen format cannot handle an option!\n";
std::cerr << "2. Mesh does not provide necessary information!\n";
std::cerr << "3. Or simply cannot open file for writing!\n";
return 1;
}
else
std::cout << "Ok.\n";
return 0;
}
// ----------------------------------------------------------------------------
void parse_commandline(int _argc, char **_argv, MyMesh& _mesh,
IO::Options &ropt, IO::Options &wopt)
{
int c;
while ((c = getopt(_argc, _argv, "bhsBF:LMSV:X:")) != -1)
{
switch (c)
{
// -------------------- read options
// force binary input
case 'b':
ropt += IO::Options::Binary;
break;
// force swapping the byte order, when reading a binary file
case 's':
ropt += IO::Options::Swap;
break;
// -------------------- write options
// Write binary variant of format if possible
case 'B':
wopt += IO::Options::Binary;
break;
//
case 'F':
for (size_t i = 0; optarg[i]; ++i)
switch (optarg[i]) {
case 'n': wopt += IO::Options::FaceNormal; break;
case 'c': wopt += IO::Options::FaceColor; break;
}
break;
// Use little endian when writing binary data
case 'L':
wopt += IO::Options::LSB;
break;
// Use big endian when writing binary data
case 'M':
wopt += IO::Options::MSB;
break;
// Swap byte order when writing binary data
case 'S':
wopt += IO::Options::Swap;
break;
//
case 'V':
{
for (size_t i = 0; optarg[i]; ++i)
switch (optarg[i]) {
case 'n': // dont't change layout!!
wopt += IO::Options::VertexNormal;
break;
case 't': wopt += IO::Options::VertexTexCoord; break;
case 'c': wopt += IO::Options::VertexColor; break;
}
break;
}
// -------------------- request mesh' standard properties
case 'X':
{
char entity = '\0';
for (size_t i = 0; optarg[i]; ++i)
switch (optarg[i]) {
case 'v':
case 'f': entity = optarg[i]; break;
case 'n':
switch (entity) {
case 'v': _mesh.request_vertex_normals(); break;
case 'f': _mesh.request_face_normals(); break;
}
break;
case 'c':
switch (entity) {
case 'v': _mesh.request_vertex_colors(); break;
case 'f': _mesh.request_face_colors(); break;
}
break;
case 't':
switch (entity) {
case 'v': _mesh.request_vertex_texcoords2D(); break;
}
break;
}
break;
}
// -------------------- help
case 'h':
usage_and_exit(0);
default:
usage_and_exit(1);
}
}
if (_argc - optind != 2)
usage_and_exit(1);
}
// ----------------------------------------------------------------------------
void usage_and_exit(int xcode)
{
std::ostream &os = xcode ? std::cerr : std::cout;
os << "Usage: io_options [Options] <input> <output>\n"
<< std::endl;
os << " Read and write a mesh, using OpenMesh::IO::Options\n"
<< std::endl;
os << "Options:\n"
<< std::endl;
os << "a) read options\n"
<< std::endl
<< " -b\n"
<< "\tAssume input file is a binary file\n"
<< std::endl
<< " -s\n"
<< "\tSwap byte order when reading a binary file!\n"
<< std::endl;
os << "b) write options\n"
<< std::endl
<< " -B\n"
<< "\tWrite binary data\n"
<< std::endl
<< " -S\n"
<< "\tSwap byte order, when writing binary data\n"
<< std::endl
<< " -M/-L\n"
<< "\tUse MSB/LSB byte ordering, when writing binary data\n"
<< std::endl
<< " -V{n|t|c}\n"
<< "\tWrite vertex normals, texcoords, and/or colors\n"
<< std::endl
<< " -F{n|c}\n"
<< "\tWrite face normals, and/or colors\n"
<< std::endl;
os << "c) Mesh properties\n"
<< std::endl
<< " -Xv{n|c|t}\n"
<< "\tRequest vertex property normals|colors|texcoords\n"
<< std::endl
<< " -Xf{n|c}\n"
<< "\tRequest face property normals|colors\n"
<< std::endl;
exit(xcode);
}
// end of file
// ============================================================================step3 Set command parameters
Command parameters are separated by Space Not semicolons

Here I set 3 Parameters
-B Indicates that the output file is binary
C:\Kings3D\OFF&STL\little_0.stl Input file
C:\Kings3D\OFF&STL\little_0_out.stl The output file
step 4 Run the program to get the output file
Open the converted binary with a visualizer stl, The verification is correct

Reference documents
边栏推荐
- Telnet+ftp to control and upgrade the equipment
- 「科普大佬说」AI与创造力
- MySQL cdc jobmanager 中存了哪些比较耗内存的数据呢?
- [day 28] given a string s, please judge whether it is a palindrome string | palindrome judgment
- 基础 | 在物理引擎中画圆弧
- Key sprite fighting monsters - multi window and multi thread background skills
- 论文笔记:E(n) Equivariant Graph Neural Networks
- Key sprite fighting monsters - window binding protection skills and click skills
- How to configure logback? 30 minutes for you to thoroughly learn the code to stay up late and knock
- C语言微博用户管理系统
猜你喜欢

Huaxia Fund: sharing of digital transformation practice achievements in the fund industry

我,大厂测试员,降薪50%去国企,后悔了...

Selenium 凭什么成为 Web 自动化测试的首选?(内附源码)

iNFTnews | Meta在元宇宙中的后续计划会是什么?

隐私计算助力数据的安全流通与共享

Technology sharing | broadcast function design in integrated dispatching

为防止被00后整顿,一公司招聘要求员工不能起诉公司

C language -- printf print base prefix

C winfrom chart chart control bar chart and line chart

Apache atlas breakpoint view
随机推荐
全面剖析Seata 分布式事务 AT 与XA
「科普大佬说」AI与创造力
After 3 years of testing experience, do you know the state transition method for use case design?
为防止被00后整顿,一公司招聘要求员工不能起诉公司
Redis布隆过滤器和布谷鸟过滤器
实战 | 神奇的 conic-gradient 圆锥渐变
Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse
To solve the stubborn problem of Lake + warehouse hybrid architecture, Star Ring Technology launched an independent controllable cloud native Lake warehouse integrated platform
MySQL基础——事务
Sophon base 3.1 launches mlops function to provide wings for enterprise AI capability operation
After reading the complete code
一个简单但是能上分的特征标准化方法
又拍云 Redis 的改进之路
自己实现一个ThreadLocal
Flutter技术与实战(2)
Key wizard play monster learning - multi window and multi thread background judgment of character, pet blood volume and pet happiness
What memory consuming data is stored in MySQL CDC jobmanager?
关于onReachButton 不触发可能原因
Huaxia Fund: sharing of digital transformation practice achievements in the fund industry