简述思路
借助CGAL几何库,分为以下步骤:
- 曲面为surface mesh类型,因为要polygon processing接口,其他格式可以用copy_face_graph转换
- 为surface mesh添加add_property_map,add_property_map也可以添加如法线或其他属性
- 遍历赋值property_map
问题
- 统一上色可以直接给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;
}