代码实践篇五 如何给曲面染色?

简述思路

借助CGAL几何库,分为以下步骤:

  1. 曲面为surface mesh类型,因为要polygon processing接口,其他格式可以用copy_face_graph转换
  2. 为surface mesh添加add_property_map,add_property_map也可以添加如法线或其他属性
  3. 遍历赋值property_map

问题

  1. 统一上色可以直接给face染色,也可以给顶点或边染色,给face染色是"f:color"

接口原型

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>
#include <CGAL/Vector_3.h>
#include <CGAL/Surface_mesh.h>

using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Point_3 = Kernel::Point_3;
using Mesh = CGAL::Surface_mesh<Kernel::Point_3>;

void ColorMesh(Mesh& mesh, CGAL::IO::Color color);

代码

void ColorMesh(Mesh& mesh, CGAL::IO::Color color)
{
    using face_descriptor = typename boost::graph_traits<Mesh>::face_descriptor;
	Mesh::Property_map<face_descriptor, CGAL::IO::Color>
	color_map = Terrain.add_property_map<face_descriptor, CGAL::IO::Color>("f:color").first;
    for (auto&f : Terrain.faces())
    {
        color_map[f] = color;
    }

相关源码

在CGAL\draw_surface_mesh.h中可以看到分别可以针对顶点,边,面染色

template <typename K>
struct Surface_mesh_basic_viewer_color_map
  : DefaultColorFunctorFaceGraph
{
  using Base = DefaultColorFunctorFaceGraph;

  using SM = ::CGAL::Surface_mesh<K>;
  using vertex_descriptor = typename boost::graph_traits<SM>::vertex_descriptor;
  using edge_descriptor = typename boost::graph_traits<SM>::edge_descriptor;
  using face_descriptor = typename boost::graph_traits<SM>::face_descriptor;

  using vertex_colors = typename SM::template Property_map<vertex_descriptor, CGAL::IO::Color>;
  using edge_colors = typename SM::template Property_map<edge_descriptor, CGAL::IO::Color>;
  using face_colors = typename SM::template Property_map<face_descriptor, CGAL::IO::Color>;

  Surface_mesh_basic_viewer_color_map(const SM& amesh)
  {
    bool found = false;
    std::tie(vcolors, found) = amesh.template property_map<vertex_descriptor, CGAL::IO::Color>("v:color");
    std::tie(ecolors, found) = amesh.template property_map<edge_descriptor, CGAL::IO::Color>("e:color");
    std::tie(fcolors, found) = amesh.template property_map<face_descriptor, CGAL::IO::Color>("f:color");
    CGAL_USE(found);
  }

  CGAL::IO::Color operator()(const Surface_mesh<K>& amesh,
                             const vertex_descriptor v) const
  {
    return vcolors ? get(vcolors, v) : Base::operator()(amesh, v);
  }

  CGAL::IO::Color operator()(const Surface_mesh<K>& amesh,
                             const edge_descriptor e) const
  {
    return ecolors ? get(ecolors, e) : Base::operator()(amesh, e);
  }

  CGAL::IO::Color operator()(const Surface_mesh<K>& amesh,
                             const face_descriptor f) const
  {
    return fcolors ? get(fcolors, f) : Base::operator()(amesh, f);
  }

private:
  vertex_colors vcolors;
  edge_colors ecolors;
  face_colors fcolors;
};

可以直接通过CGAL::IO::writexx保存。如PLY.h中,会有has_simplex_specific_property函数检测特定属性以用于保存

bool has_simplex_specific_property(internal::PLY_read_number* property, Face_index)
  {
    const std::string& name = property->name();
    if(name == "vertex_indices" || name == "vertex_index")
    {
      CGAL_assertion(dynamic_cast<PLY_read_typed_list<std::int32_t>*>(property)
                     || dynamic_cast<PLY_read_typed_list<std::uint32_t>*>(property));
      m_index_tag  = name;
      m_use_int32_t = dynamic_cast<PLY_read_typed_list<std::int32_t>*>(property);
      return true;
    }
    if(name == "red" ||
       name == "green" ||
       name == "blue")
    {
      ++ m_fcolors;
      if(m_fcolors == 3)
        m_fcolor_map = m_mesh.template add_property_map<Face_index, CGAL::IO::Color>("f:color").first;
      return true;
    }

    return false;
  }

效果

染色结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值