OpenMesh入門1(譯自OpenMesh6.3Documents)

本例子說明以下問題:

1.如何聲明你自己的mesh類型MyMesh

2.如何向mesh對象添加節點和麪

3.如何通過IO函數將mesh寫至硬盤

利用OpenMesh進行開發,首先需要定義你自己的mesh類型MyMeshOpenMesh支持一般的多邊形mesh和特殊的三角面mesh。在這個例子中,我們希望使用留個四邊形建立一個立方體,所以,我們選擇多邊形mesh

OpenMesh也支持不同的mesh內核,指明所有的節點、邊緣和麪在內部如何存儲,這樣,內存必須提供數組形式的接口。本例中,我們使用內建的ArrayKernelOpenMesh/src/OpenMesh/Core/Mesh中預定義了TriMesh/PolyMesh及其內核,我們使用PolyMesh_ArrayKernelT模板。

#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>

typedef OpenMesh::PolyMesh_ArrayKernelT<>MyMesh;

現在,我們已經聲明瞭我們自己的mesh類型MyMesh,下面,爲了建立立方體,我們還需要添加8個節點和6個四邊形,使用add_vertex函數實現。這個函數輸入座標,輸出爲節點句柄。由於需要使用它們來指定表面,我們使用數組存儲所有的句柄。

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));

爲了在mesh中添加表面,我們不得不建立一個vector,來存儲面的節點的索引。這個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);

面的方向由vector中存儲節點的順序指定:如果你從面的“前面”看,節點排列順序是逆時針的。創建完所有的6個面後,我們希望將我們剛剛創建的mesh進行標準化輸出。OpenMesh在命名空間OpenMesh::IO中提供了一些基本的輸入/輸出函數。

if ( !OpenMesh::IO::write_mesh(mesh,"output.off") )


使用OpenMesh::IO前,應當首先包含MeshIO.hh文件。.

#include <OpenMesh/Core/IO/MeshIO.hh>

#include<OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>

完整的代碼如下:

/*========================================================================= *

* *

* OpenMesh *

* Copyright (c) 2001-2015, RWTH-AachenUniversity *

* Department of Computer Graphics andMultimedia *

* All rights reserved. *

* www.openmesh.org *

* *

*---------------------------------------------------------------------------*

* This file is part of OpenMesh. *

*---------------------------------------------------------------------------*

* *

* Redistribution and use in source andbinary forms, with or without *

* modification, are permitted provided thatthe following conditions *

* are met: *

* *

* 1. Redistributions of source code mustretain the above copyright notice, *

* this list of conditions and the followingdisclaimer. *

* *

* 2. Redistributions in binary form mustreproduce the above copyright *

* notice, this list of conditions and thefollowing disclaimer in the *

* documentation and/or other materialsprovided with the distribution. *

* *

* 3. Neither the name of the copyrightholder nor the names of its *

* contributors may be used to endorse orpromote products derived from *

* this software without specific priorwritten permission. *

* *

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHTHOLDERS AND CONTRIBUTORS *

* "AS IS" AND ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *

* TO, THE IMPLIED WARRANTIES OFMERCHANTABILITY AND FITNESS FOR A *

* PARTICULAR PURPOSE ARE DISCLAIMED. IN NOEVENT SHALL THE COPYRIGHT HOLDER *

* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL, SPECIAL, *

* EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, *

* PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR *

* PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF *

* LIABILITY, WHETHER IN CONTRACT, STRICTLIABILITY, OR TORT (INCLUDING *

* NEGLIGENCE OR OTHERWISE) ARISING IN ANYWAY OUT OF THE USE OF THIS *

* SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE. *

* *

*========================================================================= */

/*===========================================================================*\

* *

* $Revision$ *

* $Date$ *

* *

\*===========================================================================*/

#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 tostd::cout

int main()

{

MyMesh mesh;

// generate vertices

MyMesh::VertexHandlevhandle[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;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章