构建一个立方体

  • 对于每个程序,第一步是定义您的MyMesh类型。 OpenMesh支持常规多边形网格(面是具有不同数量顶点的多边形)以及专用三角形网格(所有面都是三角形)。 在这个例子中,我们想要从六个四边形构建一个立方体,因此我们选择多边形网格。
  • OpenMesh还支持不同的网格内核,指定内部存储所有顶点,边和面的方式(另请参见网格内核)。 但是,存储必须提供类似接口的数组。 对于本教程,我们使用提供的ArrayKernel。 TriMesh / PolyMesh和内核的预定义组合包含在OpenMesh / src / OpenMesh / Core / Mesh中,我们使用PolyMesh_ArrayKernelT。
  • 既然我们已经声明了MyMesh类型,我们只需要添加8个顶点和6个四边形来构建一个立方体。 使用add_vertex方法添加顶点。 它获取一个坐标并返回插入顶点的句柄。 我们将所有句柄存储在一个数组中,因为我们需要它们来指定面。
  • 为了向网格添加一个面,我们必须构建一个矢量,将手柄保持在面的顶点。 此向量将传递给add_face方法。 以下块将从前四个顶点创建一个面:
  • 面的方向由顶点的给定顺序定义:如果查看多边形的前面,则顶点按逆时针顺序排列。
  • 在创建了所有六个面之后,我们希望将生成的网格写入标准输出。 OpenMesh在命名空间OpenMesh :: IO中提供了一些基本的输入/输出方法:
  • 要使用OpenMesh的IO工具,请确保首先包含包含MeshIO.hh。
  • 示例源代码
  • /* ========================================================================= *
     *                                                                           *
     *                               OpenMesh                                    *
     *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
     *           Department of Computer Graphics and Multimedia                  *
     *                          All rights reserved.                             *
     *                            www.openmesh.org                               *
     *                                                                           *
     *---------------------------------------------------------------------------*
     * This file is part of OpenMesh.                                            *
     *---------------------------------------------------------------------------*
     *                                                                           *
     * Redistribution and use in source and binary forms, with or without        *
     * modification, are permitted provided that the following conditions        *
     * are met:                                                                  *
     *                                                                           *
     * 1. Redistributions of source code must retain the above copyright notice, *
     *    this list of conditions and the following disclaimer.                  *
     *                                                                           *
     * 2. Redistributions in binary form must reproduce the above copyright      *
     *    notice, this list of conditions and the following disclaimer in the    *
     *    documentation and/or other materials provided with the distribution.   *
     *                                                                           *
     * 3. Neither the name of the copyright holder nor the names of its          *
     *    contributors may be used to endorse or promote products derived from   *
     *    this software without specific prior written permission.               *
     *                                                                           *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
     * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
     * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 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 OR SERVICES; LOSS OF USE, DATA, OR        *
     * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
     * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
     * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 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 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;
    }

     

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值