【OpenMesh】创建一个正方体
原文出處: http://openmesh.org/Documentation/OpenMesh-Doc-Latest/tutorial.html
這個例程演示了:
如何聲明MyMesh
如何添加頂點和面到Mesh中
如何使用IO函數寫入網格數據
每一個程序第一步都是定義MyMesh類型。OpenMesh支持常見的多邊形網格(面由可變頂點數的多邊形組成)以及特殊的三角網格(所有面都是三角形的)。在這個例子中我們希望構建一個六面的正方體,因此我們選擇多邊形網格。
OpenMesh同樣支持不同的網格內核,指定頂點、邊和面是如何存儲的(參考Mesh Kernels)。然而,存儲器必須提供一個陣列接口。在這篇指南中我們使用提供的ArrayKernel。預先定義的TriMesh/PolyMesh和內核包含在OpenMesh/src/OpenMesh/Core/Mesh中,我們使用PolyMesh_ArrayKernelT。
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh> typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;
限制我們定義了MyMesh類型,我們只用添加8個頂點和6個四邊形來構建一個正方體。使用add_vertex方法添加頂點。這個函數獲取坐標值返回添加頂點的handle。我們保存所有的頂點到array中,我們需要這些頂點指定面。
vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1, 1)); vhandle[1] = mesh.add_vertex(MyMesh::Point( 1, -1, 1)); vhandle[2] = mesh.add_vertex(MyMesh::Point( 1, 1, 1)); vhandle[3] = mesh.add_vertex(MyMesh::Point(-1, 1, 1));
為了添加面到網格中,我們必須構建一個vector保存面的頂點的handle。這個vector當做add_face的參數被傳入。下列的代碼使用四個點創建一個面:
std::vector<MyMesh::VertexHandle> face_vhandles; face_vhandles.clear(); face_vhandles.push_back(vhandle[0]); face_vhandles.push_back(vhandle[1]); face_vhandles.push_back(vhandle[2]); face_vhandles.push_back(vhandle[3]); mesh.add_face(face_vhandles);
面的方向由提供的點的方向來定義:如果你朝著多邊形的前面看,這些點的方向是逆時針的。
當創建完所有的面的時候,我們希望將網格結果寫入標準輸出中。OpenMesh提供基本的輸入輸出方法,定義在名字空間OpenMesh::IO:
if ( !OpenMesh::IO::write_mesh(mesh, "output.off") )
使用IO的函數首先應包含MeshIO.hh
#include <OpenMesh/Core/IO/MeshIO.hh> #include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
完整的代碼像這樣:
//=============================================================================
//
// OpenMesh
// Copyright (C) 2001-2005 by Computer Graphics Group, RWTH Aachen
// www.openmesh.org
//
//-----------------------------------------------------------------------------
//
// License
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Library General Public License as published
// by the Free Software Foundation, version 2.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//-----------------------------------------------------------------------------
//
// $Revision: 83 $
// $Date: 2009-02-27 17:31:45 +0100 (Fri, 27 Feb 2009) $
//
//=============================================================================
#include <iostream>
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
// ----------------------------------------------------------------------------
typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;
// ----------------------------------------------------------------------------
// Build a simple cube and write it to std::cout
int main()
{
MyMesh mesh;
// generate vertices
MyMesh::VertexHandle vhandle[8];
vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1, 1));
vhandle[1] = mesh.add_vertex(MyMesh::Point( 1, -1, 1));
vhandle[2] = mesh.add_vertex(MyMesh::Point( 1, 1, 1));
vhandle[3] = mesh.add_vertex(MyMesh::Point(-1, 1, 1));
vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
vhandle[5] = mesh.add_vertex(MyMesh::Point( 1, -1, -1));
vhandle[6] = mesh.add_vertex(MyMesh::Point( 1, 1, -1));
vhandle[7] = mesh.add_vertex(MyMesh::Point(-1, 1, -1));
// generate (quadrilateral) faces
std::vector<MyMesh::VertexHandle> face_vhandles;
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[3]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[4]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[4]);
face_vhandles.push_back(vhandle[5]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[6]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[7]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[4]);
mesh.add_face(face_vhandles);
// write mesh to output.obj
try
{
if ( !OpenMesh::IO::write_mesh(mesh, "output.off") )
{
std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
return 1;
}
}
catch( std::exception& x )
{
std::cerr << x.what() << std::endl;
return 1;
}
return 0;
}
總結
以上是生活随笔為你收集整理的【OpenMesh】创建一个正方体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷4092 树
- 下一篇: 公主兔多少钱一只(公主兔好养吗)